mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-26 04:27:21 +01:00
Compare commits
2 Commits
95a1f62fc4
...
004b4b0b29
| Author | SHA1 | Date | |
|---|---|---|---|
| 004b4b0b29 | |||
| 17b20e5fae |
@ -100,6 +100,11 @@ repos:
|
||||
# ===========================================================================
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: no-inline-python
|
||||
name: Block Python logic embedded in shell scripts
|
||||
entry: meta/scripts/check_no_inline_python.sh
|
||||
language: script
|
||||
types_or: [shell, zsh]
|
||||
- id: no-polling-antipatterns
|
||||
name: Block polling script anti-patterns
|
||||
entry: meta/scripts/check_polling_antipatterns.sh
|
||||
|
||||
319
CLAUDE.md
319
CLAUDE.md
@ -1,258 +1,141 @@
|
||||
# Copilot Instructions for testsAndMisc
|
||||
# testsAndMisc
|
||||
|
||||
## Project Overview
|
||||
|
||||
A mixed-language monorepo containing Python packages, Bash scripts, and misc automation. Actively-developed
|
||||
components span personal productivity tools: alarm/shutdown scheduling, Linux system configuration, and
|
||||
Android phone focus enforcement.
|
||||
|
||||
Extracted to their own repos:
|
||||
|
||||
- [`steam-backlog-enforcer`](https://github.com/kuhyx/steam-backlog-enforcer)
|
||||
- [`screen-locker`](https://github.com/kuhyx/screen-locker)
|
||||
- [`diet-guard`](https://github.com/kuhyx/diet-guard)
|
||||
- [`wake-alarm`](https://github.com/kuhyx/wake-alarm)
|
||||
- [`dufs-cloud`](https://github.com/kuhyx/dufs-cloud) — self-hosted dufs cloud:
|
||||
React web gallery (`web/`), Flutter app (`app/`), and the dufs/media setup
|
||||
scripts (`scripts/`). Extracted from `cloud_gallery/` + the
|
||||
`linux_configuration` dufs scripts + the standalone `dufs_client` repo. The
|
||||
live `media-cloud-sync` systemd units now run the scripts from
|
||||
`~/dufs-cloud/scripts/`.
|
||||
- [`build-your-x`](https://github.com/kuhyx/build-your-x) — build-your-own-x
|
||||
difficulty ladder + `byox` progress tracker (crdt_sync-backed) and the builds
|
||||
themselves (`builds/`). Extracted from `python_pkg/byox_ladder/` + the
|
||||
standalone `~/build_your_x` builds. Lives at `~/build_your_x`.
|
||||
|
||||
Archived / unmaintained projects live in the sibling repository
|
||||
[`testsAndMisc-archive`](https://github.com/kuhyx/testsAndMisc-archive).
|
||||
Mixed-language monorepo: Python packages, Bash automation, and several apps.
|
||||
|
||||
## Repository Layout
|
||||
|
||||
| Path | Description |
|
||||
| ---------------------- | --------------------------------------------------------------------------------------------- |
|
||||
| `python_pkg/` | Python packages — each maintained subpackage lives here |
|
||||
| `billsplit/` | Receipt bill-splitting Flutter app (Dart, e-paragon import, 100 % line coverage gate) |
|
||||
| `linux_configuration/` | Arch Linux setup, i3 config, system maintenance scripts |
|
||||
| `phone_focus_mode/` | GPS-based Android focus enforcer (Bash, ADB, Magisk) |
|
||||
| `reverse_survivors/` | Reverse Survivors — React/Vite game, you play the horde (TypeScript, 100 % coverage gate) |
|
||||
| `meta/` | Repo-wide tooling: `pyproject.toml`, `requirements.txt`, `run.sh`, `lint_python.sh`, `.fvmrc` |
|
||||
| `scripts/` | Workspace-level helper scripts and pre-commit hooks (moved to `meta/scripts/`) |
|
||||
| `docs/` | Reference docs; `docs/superpowers/` holds AI workflow artifacts |
|
||||
| `third_party/` | Vendored upstream skills/agents |
|
||||
| Path | Description |
|
||||
| --------------------------- | --------------------------------------------------------------------------------------------------------- |
|
||||
| `python_pkg/` | Python packages — every `.py` in the repo belongs here or in an allowlisted dir |
|
||||
| `meta/` | Repo-wide tooling: `pyproject.toml`, `requirements.txt`, `run.sh`, `lint_python.sh`, `.fvmrc`, `scripts/` |
|
||||
| `linux_configuration/` | Arch setup, i3 config, system maintenance, and the tracked git hooks |
|
||||
| `phone_focus_mode/` | GPS-based Android focus enforcer (Bash, ADB, Magisk) |
|
||||
| `billsplit/` | Receipt bill-splitting Flutter app (Dart) |
|
||||
| `reverse_survivors/` | React/Vite game — you play the horde (TypeScript) |
|
||||
| `poker-stakes/` | Poker stakes trainer (TypeScript, Vite) |
|
||||
| `kcd2_dice_solver/` | KCD2 dice solver |
|
||||
| `focus_owner/` | Android Device-Owner focus enforcer (Kotlin) |
|
||||
| `bucket_catch/`, `bottles/` | Small standalone projects |
|
||||
| `docs/` | Reference docs; `docs/superpowers/` holds AI workflow artifacts |
|
||||
| `third_party/` | Vendored upstream skills/agents |
|
||||
|
||||
> **Note**: Root-level `pyproject.toml`, `requirements.txt`, `requirements.txt`, `run.sh`, and `.fvmrc`
|
||||
> are symlinks into `meta/`. Edit files there, not the symlinks.
|
||||
Root `pyproject.toml`, `requirements.txt`, `run.sh`, `lint_python.sh` and
|
||||
`.fvmrc` are **symlinks into `meta/`** — edit the files in `meta/`, not the
|
||||
symlinks.
|
||||
|
||||
> **Note**: `billsplit/` is the only Flutter app in this repo, and the only place
|
||||
> binaries are committed. Its platform launcher icons are build inputs, so they
|
||||
> get the `.binary-allowlist` exception plus matching `!` overrides at the end of
|
||||
> `.gitignore` — both are required, since an ignored file fails silently on
|
||||
> `git add` rather than erroring. Adding another Flutter app here means repeating
|
||||
> that pair. Icons come from the shared generator in `python_pkg/app_icons/`, and
|
||||
> there is no local `dart format` / `flutter analyze` hook — Dart style is gated
|
||||
> by `.github/workflows/billsplit-ci.yml` only. `billsplit/` holds **no Python**:
|
||||
> its coverage gate lives at `python_pkg/billsplit_coverage/`, because the
|
||||
> `check-python-location` hook requires every `.py` under `python_pkg/`.
|
||||
Extracted to their own repos: [`steam-backlog-enforcer`](https://github.com/kuhyx/steam-backlog-enforcer),
|
||||
[`screen-locker`](https://github.com/kuhyx/screen-locker),
|
||||
[`diet-guard`](https://github.com/kuhyx/diet-guard),
|
||||
[`wake-alarm`](https://github.com/kuhyx/wake-alarm),
|
||||
[`dufs-cloud`](https://github.com/kuhyx/dufs-cloud),
|
||||
[`build-your-x`](https://github.com/kuhyx/build-your-x) (lives at `~/build_your_x`).
|
||||
Archived work: [`testsAndMisc-archive`](https://github.com/kuhyx/testsAndMisc-archive).
|
||||
|
||||
## Architecture
|
||||
## Adding a Flutter app
|
||||
|
||||
### Python Packages (`python_pkg/`)
|
||||
`billsplit/` is currently the only one, and the only place binaries are
|
||||
committed. To add another, repeat **both** halves or the icons fail silently:
|
||||
|
||||
- **brother_printer/** — Brother printer status checker via CUPS and USB/network query
|
||||
- `check_brother_printer.py` — main status check
|
||||
- `cups_queue.py` / `cups_service.py` — CUPS integration
|
||||
- `network_query.py` / `usb_query.py` — device discovery
|
||||
- `tests/` — pytest tests
|
||||
1. Add the icon globs to `.binary-allowlist`.
|
||||
2. Add matching `!` overrides at the end of `.gitignore` — an ignored file
|
||||
fails silently on `git add` rather than erroring.
|
||||
|
||||
- **shared/** — Shared utilities across python_pkg subpackages
|
||||
- **random_jpg/** — Random JPEG downloader utility
|
||||
- **geo_cache/** — Geographic coordinate cache helper
|
||||
|
||||
### Phone Focus Mode (`phone_focus_mode/`)
|
||||
|
||||
Location-based app restriction for rooted Android. Automatically disables non-whitelisted apps within
|
||||
500 m of home using ADB + Magisk.
|
||||
|
||||
- `focus_ctl.sh` / `focus_daemon.sh` — focus enforcement scripts
|
||||
- `dns_enforcer.sh` — DNS-level blocking (netd cache restart for YouTube)
|
||||
- `hosts_enforcer.sh` — `/etc/hosts` manipulation
|
||||
- `launcher_enforcer.sh` — launcher restriction
|
||||
- `workout_detector.sh` — workout detection integration
|
||||
- `magisk_service.sh` — Magisk module hook (prevents module self-disabling)
|
||||
- `config.sh` — configuration constants
|
||||
- `deploy.sh` — ADB deployment script
|
||||
- `systemd/` — systemd units for scheduling
|
||||
- `lib/` — shared shell library functions
|
||||
|
||||
### Linux Configuration (`linux_configuration/`)
|
||||
|
||||
Arch Linux setup and ongoing system automation.
|
||||
|
||||
```
|
||||
linux_configuration/
|
||||
├── install_core_system.sh # Core system installer
|
||||
├── scripts/
|
||||
│ ├── single_use/ # One-time setup scripts
|
||||
│ │ ├── fresh-install/
|
||||
│ │ ├── features/
|
||||
│ │ ├── fixes/
|
||||
│ │ └── misc/
|
||||
│ └── periodic_background/ # Ongoing daemons / scheduled scripts
|
||||
│ ├── digital_wellbeing/ # Compulsive-opening blocker, LeechBlock
|
||||
│ ├── hosts/ # DNS/hosts guard and generation
|
||||
│ ├── i3-configuration/ # i3 window manager config
|
||||
│ ├── system-maintenance/ # Usage reporting, system checks
|
||||
│ └── utils/
|
||||
├── tests/ # Shell-based test harness
|
||||
└── test_results.log
|
||||
```
|
||||
|
||||
Key scripts:
|
||||
|
||||
- `scripts/periodic_background/hosts/generate_hosts_file.sh` — Generates `/etc/hosts` blocklist
|
||||
- `scripts/periodic_background/system-maintenance/bin/usage_report.py` — Daily usage report
|
||||
|
||||
### `meta/` — Repo-wide Tooling
|
||||
|
||||
All root-level config files are symlinks into `meta/`. Edit here:
|
||||
|
||||
- `meta/pyproject.toml` — ruff, mypy, pylint, bandit, pytest, coverage config
|
||||
- `meta/requirements.txt` — runtime + dev dependencies
|
||||
- `meta/run.sh` — usage report entrypoint + polling script profiler/diagnostics
|
||||
- `meta/lint_python.sh` — manual lint helper
|
||||
- `meta/scripts/` — pre-commit hook scripts (`check_no_binaries.sh`, `check_ai_evidence.sh`, etc.)
|
||||
|
||||
### `docs/superpowers/` — AI Workflow Artifacts
|
||||
|
||||
Pre-commit **requires** an evidence file for every commit that changes source code:
|
||||
|
||||
```
|
||||
docs/superpowers/
|
||||
├── evidence/ # ← Required: one JSON per commit touching code
|
||||
│ └── template.json
|
||||
├── contracts/ # Acceptance criteria / objective contracts per task
|
||||
├── sessions/ # Append-only session logs (JSONL)
|
||||
├── specs/ # Task specifications / design docs
|
||||
├── plans/ # Implementation plans
|
||||
├── memory/ # Persistent context (CONTEXT.md, etc.)
|
||||
└── workflows/ # Agent workflow definitions
|
||||
```
|
||||
|
||||
**Rule**: copy `docs/superpowers/evidence/template.json`, fill it in, and stage it with your code changes
|
||||
before committing. The `ai-evidence-contract` hook will reject commits without it.
|
||||
Icons come from the shared generator in `python_pkg/app_icons/`. Dart style is
|
||||
gated by CI only (`.github/workflows/billsplit-ci.yml`), not by a local hook.
|
||||
Keep app dirs free of Python: the `check-python-location` hook requires every
|
||||
`.py` to sit under `python_pkg/` (or `linux_configuration/`, `phone_focus_mode/`,
|
||||
`meta/scripts/`). `billsplit/`'s coverage gate therefore lives at
|
||||
`python_pkg/billsplit_coverage/`.
|
||||
|
||||
## Git Workflow
|
||||
|
||||
Work directly on `main` — no need to create branches for this repository. Commit and push straight to `main`.
|
||||
Work directly on `main`; commit and push straight there.
|
||||
|
||||
### Hooks are mandatory
|
||||
`core.hooksPath` points at the tracked `linux_configuration/.githooks/`, so
|
||||
hooks travel with the repo. `.git/hooks/` is unused — **never run
|
||||
`pre-commit install`**.
|
||||
|
||||
`core.hooksPath` points at the **tracked** directory `linux_configuration/.githooks/`,
|
||||
so the hooks travel with the repo and cannot be missing on a clone. `.git/hooks/`
|
||||
is unused and irrelevant — never run `pre-commit install`, which writes there and
|
||||
is ignored (newer pre-commit refuses outright while `hooksPath` is set).
|
||||
- **pre-commit**: shfmt + shellcheck + jscpd (fails above 2% duplication),
|
||||
then `pre-commit run --hook-stage pre-commit`.
|
||||
- **pre-push**: `pre-commit run --hook-stage pre-push` — `prettier` and
|
||||
`ci-mirror`. `ci-mirror` runs a clean-venv install, `pre-commit run` over all
|
||||
files, and pytest for changed packages.
|
||||
|
||||
- `pre-commit` — shfmt + shellcheck + jscpd, then `pre-commit run --hook-stage pre-commit`
|
||||
- `pre-push` — `pre-commit run --hook-stage pre-push` (prettier, ci-mirror, pytest-coverage)
|
||||
over the pushed commit range
|
||||
|
||||
Bootstrap a new machine or clone with:
|
||||
Bootstrap a clone or new machine:
|
||||
|
||||
```bash
|
||||
./meta/scripts/install_hooks.sh # wire hooks + install missing tools
|
||||
./meta/scripts/install_hooks.sh --check # verify, change nothing, exit 1 if incomplete
|
||||
./meta/scripts/install_hooks.sh --check # verify only; exit 1 if incomplete
|
||||
```
|
||||
|
||||
Both hooks **fail closed**: every external binary they invoke (`pre-commit`,
|
||||
`prettier`, `shellcheck`, `shfmt`, `zsh`, `node`, `npm`, `python3`) is verified on
|
||||
every run and auto-installed via `pacman` when possible; if it cannot be installed
|
||||
non-interactively the commit or push aborts with the exact command to run. A missing
|
||||
tool never means "skip the check" — that failure mode is why `prettier` was absent
|
||||
for an unknown length of time without anyone noticing. The required-tool list lives
|
||||
in `REQUIRED_TOOLS` in `linux_configuration/.githooks/lib/common.sh`; extend it when
|
||||
adding a `language: system` hook.
|
||||
|
||||
Each hook also re-asserts `core.hooksPath`, so drift self-heals after one run. The
|
||||
one gap it cannot close is a fresh clone where the config was never set and no hook
|
||||
runs at all — that is exactly what `install_hooks.sh` is for.
|
||||
Both hooks fail closed: every external binary is verified each run and
|
||||
auto-installed via pacman where possible. A missing tool aborts the commit —
|
||||
it never means "skip the check". Extend `REQUIRED_TOOLS` in
|
||||
`linux_configuration/.githooks/lib/common.sh` when adding a `language: system`
|
||||
hook. `common.sh` also warns when local shellcheck/prettier/shfmt/zsh drift
|
||||
from the versions pinned in `.github/workflows/pre-commit.yml`.
|
||||
|
||||
## Development Workflow
|
||||
|
||||
do NOT run tests unless specifically instructed to do so or before committing
|
||||
If tests fail on the same issue twice in a row, STOP and ask the user how to proceed instead of continuing to fix and retry.
|
||||
ALWAYS confirm that the feature you add / bug you fixed behaves as it should by running the program after your changes (not tests!) and inspecting output comparing it with what user wanted, after confirming by yourself ask user if the program behaves as they intended
|
||||
After running tests fix all coverage gaps and issues, do not ignore unless specifically instructed to do so
|
||||
- Do NOT run tests unless instructed, or before committing.
|
||||
- If tests fail on the same issue twice in a row, STOP and ask.
|
||||
- Confirm a change works by **running the program** and inspecting output —
|
||||
not by running tests. Then ask the user to confirm the behaviour.
|
||||
- Fix coverage gaps; do not ignore them.
|
||||
|
||||
### AI Evidence Requirement
|
||||
### AI evidence (enforced)
|
||||
|
||||
For every commit that touches `.py`, `.sh`, `.c`, `.go`, `.ts`, etc.:
|
||||
Every commit touching code needs an evidence artifact:
|
||||
|
||||
1. Copy `docs/superpowers/evidence/template.json` → `docs/superpowers/evidence/<task-slug>-<date>.json`
|
||||
2. Fill in the fields (objective, steps taken, verification result)
|
||||
3. Stage and include it with your code changes
|
||||
1. Copy `docs/superpowers/evidence/template.json` →
|
||||
`docs/superpowers/evidence/<task-slug>-<date>.json`.
|
||||
2. Fill in intent, scope, changes, verification, risks, rollback.
|
||||
3. Stage it with the code.
|
||||
|
||||
### Linting Tools (configured in `meta/pyproject.toml`)
|
||||
`validate_evidence.py` rejects empty `verification[]` and the phrases
|
||||
"should work", "probably fine", "seems right".
|
||||
|
||||
- **ruff**: `select = ["ALL"]` — all rules enabled, Google docstrings
|
||||
- **mypy**: `strict = true` with full type checking
|
||||
- **pylint**: all checks enabled
|
||||
- **coverage**: `fail_under = 100`, branch coverage required
|
||||
**Staging ≥4 code files additionally requires a fresh
|
||||
`docs/superpowers/contracts/*.json`** in the same commit.
|
||||
|
||||
Deletions in `docs/superpowers/sessions/*` abort the commit (append-only).
|
||||
|
||||
## Code Conventions
|
||||
|
||||
### Python Style
|
||||
Anything already enforced by a hook is omitted here — run
|
||||
`pre-commit run --all-files` and read `.pre-commit-config.yaml` for the
|
||||
complete list. What follows is what a linter cannot check.
|
||||
|
||||
- Use `from __future__ import annotations` for forward references
|
||||
- Google docstring convention
|
||||
- Absolute imports only (`ban-relative-imports = "all"`)
|
||||
- Type hints required on all functions
|
||||
- Private functions/modules prefixed with `_` (e.g., `_smart_plug.py`, `_process_game_event`)
|
||||
### Python
|
||||
|
||||
### Shell Style
|
||||
- `from __future__ import annotations` for forward references.
|
||||
- Prefix private functions/modules with `_` (`_smart_plug.py`,
|
||||
`_process_game_event`).
|
||||
- Mock external calls in tests; never hit real APIs or the filesystem.
|
||||
- For branch coverage: prefer an explicit `while True` with
|
||||
`try`/`except StopIteration` over `for` when iterator exhaustion needs
|
||||
covering, and mock threads/subprocesses to keep tests fast.
|
||||
|
||||
- Always `set -euo pipefail`
|
||||
- Double-quote all variable expansions
|
||||
- Avoid fork-heavy patterns: prefer `/proc`, `/sys`, bash builtins over `$(...)` in hot paths
|
||||
- Use `jq`/`yq` for JSON/YAML, not `grep`/`awk`
|
||||
- **NEVER embed Python program logic inline in a shell script** — no multi-line
|
||||
`python -c "..."` and no `python <<'PY' ... PY` heredocs that contain real logic.
|
||||
Put the code in a separate `.py` file so the repo's Python tooling (ruff, mypy,
|
||||
pylint, bandit, tests) applies to it, and invoke it as `python3 path/to/file.py "$arg"`.
|
||||
Resolve the path relative to the script (e.g. `"${0:A:h}/helper.py"` in zsh,
|
||||
`"$(dirname "$0")/helper.py"` in bash). The only permitted inline Python is a
|
||||
single-line availability/version probe with no logic, e.g. `python3 -c 'import kasa'`
|
||||
or `python3 -c 'import sys; print(sys.version_info[0])'`.
|
||||
Full linting/coverage configuration lives in `meta/pyproject.toml`
|
||||
(ruff `select = ["ALL"]`, mypy, pylint `enable = "all"`, coverage
|
||||
`fail_under = 100` with branch coverage). Note the mypy **hook** disables a
|
||||
number of error codes, so it is weaker than the `strict = true` in the config.
|
||||
|
||||
### Test Patterns
|
||||
### Shell
|
||||
|
||||
```python
|
||||
# Type aliases for test dicts (keeps mypy happy)
|
||||
Event = dict[str, Any]
|
||||
|
||||
# Mock external calls — never hit real APIs/filesystem
|
||||
with patch("python_pkg.screen_locker.screen_lock.some_func") as mock:
|
||||
...
|
||||
|
||||
# Use PropertyMock for property exceptions
|
||||
type(mock_obj).property_name = PropertyMock(side_effect=TypeError())
|
||||
```
|
||||
|
||||
### Branch Coverage Tips
|
||||
|
||||
- Use explicit `while True` + `try/except StopIteration` instead of `for` loops when iterator
|
||||
exhaustion needs coverage
|
||||
- Mock threads/subprocesses to avoid slow tests
|
||||
- Every `if`/`else` branch needs a corresponding test
|
||||
- `set -euo pipefail` in every script.
|
||||
- Prefer `/proc`, `/sys` and bash builtins over `$(...)` in hot paths — see
|
||||
the `efficient-polling-scripts` skill. The `no-polling-antipatterns` hook
|
||||
only inspects functions named `*_loop`, `*_daemon` or `poll*`, so a fork
|
||||
storm elsewhere passes clean.
|
||||
- Use `jq`/`yq` for JSON/YAML, not `grep`/`awk`.
|
||||
|
||||
## Key Files
|
||||
|
||||
- `meta/pyproject.toml` — All tool configs (ruff, mypy, pylint, pytest, coverage)
|
||||
- `.pre-commit-config.yaml` — Pre-commit hook definitions
|
||||
- `meta/requirements.txt` — Runtime + dev dependencies
|
||||
- `.github/workflows/python-tests.yml` — CI: runs all pytest on `python_pkg/**` changes
|
||||
- `.github/workflows/pre-commit.yml` — CI: runs pre-commit checks
|
||||
- `docs/superpowers/evidence/template.json` — Template for AI evidence artifacts
|
||||
|
||||
## Per-File Ignores (in `meta/pyproject.toml`)
|
||||
|
||||
Test files allow: `S101` (assert), `PLR2004` (magic values), `S310`, `S607`, `PLC0415`
|
||||
- `meta/pyproject.toml` — all Python tool configs
|
||||
- `.pre-commit-config.yaml` — hook definitions
|
||||
- `linux_configuration/.githooks/` — the tracked git hooks
|
||||
- `meta/requirements.txt` — runtime + dev dependencies
|
||||
- `docs/superpowers/evidence/template.json` — evidence template
|
||||
- `.github/workflows/` — `pre-commit`, `python-tests`, `shell-tests`, and one
|
||||
CI workflow per app
|
||||
|
||||
@ -0,0 +1,39 @@
|
||||
{
|
||||
"intent": "Shrink CLAUDE.md from 259 to 141 lines by deleting rules that hooks already enforce, correcting claims that were factually wrong, and rewriting the remainder as HOW rather than WHY.",
|
||||
"scope": [
|
||||
"CLAUDE.md (rewritten)",
|
||||
"Non-goal: no hook behaviour changes in this commit; the one new gate (no-inline-python) landed separately in 17b20e5"
|
||||
],
|
||||
"changes": [
|
||||
"Deleted prose duplicating existing gates: ruff/mypy/pylint/coverage settings, 'double-quote expansions' (shellcheck SC2086, no severity filter), absolute imports (TID252), Google docstrings, type hints (ANN), the inline-Python rule (now the no-inline-python hook), and the evidence-file procedure detail.",
|
||||
"Corrected four factual errors found by auditing the config: (1) pytest-coverage was listed as a pre-push hook but is stages:[pre-commit] -- pre-push is prettier + ci-mirror only; (2) the root-symlink list named requirements.txt twice and omitted lint_python.sh; (3) 'scripts/ -- workspace-level helpers' described a directory that does not exist at repo root; (4) per-file-ignores were listed as S101/PLR2004/S310/S607/PLC0415 when the actual set is ARG/D/PLC0415/PLR2004/S101/SLF001.",
|
||||
"Added five top-level directories the layout table omitted: poker-stakes, bottles, bucket_catch, focus_owner, kcd2_dice_solver.",
|
||||
"Documented three enforced-but-undocumented gates an agent would otherwise hit blind: the 4-file contract threshold, append-only session logs, and the 2% jscpd duplication limit.",
|
||||
"Recorded two places where the gate is weaker than it looks: the mypy hook disables ~20 error codes despite strict=true in config, and no-polling-antipatterns only inspects *_loop/*_daemon/poll* functions."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "bash ~/utils/scripts/check_file_length.sh CLAUDE.md",
|
||||
"result": "pass",
|
||||
"evidence": "259 -> 141 lines; exits 0, comfortably under the 250-line cap with headroom rather than trimming to exactly 250."
|
||||
},
|
||||
{
|
||||
"command": "grep checks against check_python_location.sh, validate_evidence.py, check_append_only_sessions.sh, check_agent_contract.sh, check_polling_antipatterns.sh, .githooks/pre-commit",
|
||||
"result": "pass",
|
||||
"evidence": "Every retained claim verified against its source: allowlist dirs match; banned phrases are exactly 'should work'/'probably fine'/'seems right'; sessions regex is ^docs/superpowers/sessions/.*\\.(jsonl|log|txt)$; MULTI_FILE_THRESHOLD=4; in_poll awk guard matches *_loop|*_daemon|poll*; jscpd --threshold 2."
|
||||
},
|
||||
{
|
||||
"command": "test -d for each of the 13 directories named in the layout table",
|
||||
"result": "pass",
|
||||
"evidence": "No MISSING lines -- every path in the table exists."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"Deleting enforced prose relies on the hooks staying wired. That is the intended trade: a gate that fails the commit is stronger than a sentence an agent may not read, and .pre-commit-config.yaml is now named as the source of truth.",
|
||||
"The audit that classified each rule was produced by a subagent; every claim retained or corrected here was independently re-verified against the config files before editing."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert this commit to restore the 259-line version.",
|
||||
"Validate with: bash ~/utils/scripts/check_file_length.sh CLAUDE.md (will then fail at 259 lines)."
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
{
|
||||
"intent": "Make the 'no inline Python in shell scripts' rule a gate that fails the commit, instead of prose in CLAUDE.md that nothing enforced.",
|
||||
"scope": [
|
||||
"meta/scripts/check_no_inline_python.sh (new pre-commit hook script)",
|
||||
".pre-commit-config.yaml (register the no-inline-python hook)",
|
||||
"linux_configuration/scripts/single_use/utils/install_plagiarism_tools.sh (the one real violation)",
|
||||
"linux_configuration/scripts/single_use/utils/download_nltk_data.py (extracted from the inline block)",
|
||||
"Non-goal: the remaining 250-line file-length work; that lands separately"
|
||||
],
|
||||
"changes": [
|
||||
"Added check_no_inline_python.sh: flags `python <<HEREDOC` and multi-line `python -c`, while permitting the single-line availability/version probes the rule explicitly allows. Skips commented-out examples so the rule's own documentation does not fail the hook.",
|
||||
"Extracted a 6-line inline `python3 -c` NLTK download block into download_nltk_data.py, invoked as `python3 \"$(dirname \"$0\")/download_nltk_data.py\"`. It now returns a non-zero exit on partial failure, which the inline version silently swallowed.",
|
||||
"Registered the hook with types_or: [shell, zsh]."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "find . -name '*.sh' ... | xargs bash meta/scripts/check_no_inline_python.sh",
|
||||
"result": "pass",
|
||||
"evidence": "Scanned every shell file in the repo: 1 genuine violation found (install_plagiarism_tools.sh:84), 0 false positives across ~250 files. After the extraction, the whole tree is clean."
|
||||
},
|
||||
{
|
||||
"command": "pre-commit run no-inline-python --all-files",
|
||||
"result": "pass",
|
||||
"evidence": "Block Python logic embedded in shell scripts.............................Passed"
|
||||
},
|
||||
{
|
||||
"command": "pre-commit run no-inline-python --files ./canary_tmp.sh (staged canary with a real heredoc)",
|
||||
"result": "pass",
|
||||
"evidence": "Hook exited non-zero and reported canary_tmp.sh:2: python3 <<'PY' -- proving the gate fires rather than passing vacuously. Canary deleted afterwards."
|
||||
},
|
||||
{
|
||||
"command": "bash -n + shellcheck install_plagiarism_tools.sh; ruff check + ruff format --check download_nltk_data.py",
|
||||
"result": "pass",
|
||||
"evidence": "bash -n OK; shellcheck OK; 'All checks passed!' and '1 file already formatted'. No suppressions added: T201 avoided via sys.stderr.write and INP001 via the #!/usr/bin/env python3 shebang, matching the sibling fast_count.py."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"download_nltk_data.py imports nltk at module scope, which is not in meta/requirements.txt because it belongs to the plagiarism toolchain, not this repo. The installer pip-installs it immediately before invoking the module, so the import cannot fail in the real call path.",
|
||||
"The multi-line `python -c` regex keys on an unterminated quote. A single-line -c containing an escaped quote could in principle be misread; no such case exists in the repo today and the whole-tree scan found zero false positives."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert the commit; the hook stops running and the inline block returns.",
|
||||
"Validate by re-running: pre-commit run no-inline-python --all-files"
|
||||
]
|
||||
}
|
||||
53
linux_configuration/scripts/single_use/utils/download_nltk_data.py
Executable file
53
linux_configuration/scripts/single_use/utils/download_nltk_data.py
Executable file
@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Download the NLTK corpora the plagiarism tools need.
|
||||
|
||||
Extracted from `install_plagiarism_tools.sh`, which used to run this as an
|
||||
inline `python3 -c` block. Inline Python is invisible to ruff, mypy, pylint,
|
||||
bandit and pytest, so it lives in its own module instead.
|
||||
|
||||
`nltk` is a dependency of the plagiarism toolchain, not of this repo, so it is
|
||||
absent from `meta/requirements.txt` on purpose. The installer script pip-installs
|
||||
it immediately before invoking this module, and probes for it first.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import sys
|
||||
|
||||
#: Corpora required by the plagiarism checker. `punkt_tab` is the post-3.8.2
|
||||
#: replacement for `punkt`; both are listed so the script works on either.
|
||||
CORPORA = (
|
||||
"punkt",
|
||||
"stopwords",
|
||||
"punkt_tab",
|
||||
"averaged_perceptron_tagger",
|
||||
"wordnet",
|
||||
)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""Download every corpus, reporting any that fail.
|
||||
|
||||
Returns 1 if any download failed, so the calling shell script's `set -e`
|
||||
sees a real failure instead of a silent partial install.
|
||||
"""
|
||||
# Imported dynamically, not at module scope: nltk belongs to the plagiarism
|
||||
# toolchain rather than this repo, so it is deliberately absent from
|
||||
# meta/requirements.txt and unresolvable to static analysis. The installer
|
||||
# pip-installs it immediately before invoking this module.
|
||||
try:
|
||||
nltk = importlib.import_module("nltk")
|
||||
except ImportError:
|
||||
sys.stderr.write("error: nltk is not installed\n")
|
||||
return 1
|
||||
|
||||
failed = [corpus for corpus in CORPORA if not nltk.download(corpus)]
|
||||
if failed:
|
||||
sys.stderr.write(f"error: failed to download: {', '.join(failed)}\n")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@ -81,14 +81,7 @@ success "Python NLP packages installed"
|
||||
|
||||
# Download NLTK data
|
||||
echo "Downloading NLTK data (stopwords, punkt tokenizer)..."
|
||||
python3 -c "
|
||||
import nltk
|
||||
nltk.download('punkt')
|
||||
nltk.download('stopwords')
|
||||
nltk.download('punkt_tab')
|
||||
nltk.download('averaged_perceptron_tagger')
|
||||
nltk.download('wordnet')
|
||||
"
|
||||
python3 "$(dirname "$0")/download_nltk_data.py"
|
||||
success "NLTK data downloaded"
|
||||
|
||||
# Download spaCy English model (small)
|
||||
|
||||
62
meta/scripts/check_no_inline_python.sh
Executable file
62
meta/scripts/check_no_inline_python.sh
Executable file
@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env bash
|
||||
# Block Python program logic embedded inline in a shell script.
|
||||
#
|
||||
# Inline Python is invisible to the repo's Python tooling: ruff, mypy, pylint,
|
||||
# bandit and pytest all skip it, so it is the one place a type error or a
|
||||
# security finding can live undetected. Extract it to a .py file under
|
||||
# python_pkg/ and invoke it as `python3 "$(dirname "$0")/helper.py" "$arg"`.
|
||||
#
|
||||
# Permitted, and deliberately not flagged: a SINGLE-LINE availability or
|
||||
# version probe with no logic, e.g.
|
||||
# python3 -c 'import kasa'
|
||||
# python -c "import sys; print(sys.version_info[0])"
|
||||
# These carry no logic worth linting -- they are a yes/no question about the
|
||||
# interpreter, and extracting them to a file would be sillier than inlining.
|
||||
#
|
||||
# Used as a pre-commit hook; receives staged file paths as arguments.
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
errors=()
|
||||
|
||||
# A `python <<EOF` / `python3 <<-'PY'` heredoc: always real logic, never a
|
||||
# probe. Matches an optional redirect target so `python3 <<'PY' >out` is caught.
|
||||
readonly HEREDOC_RE='(^|[^[:alnum:]_])python3?[[:space:]]+<<-?'
|
||||
|
||||
# The opening of a `python -c "` whose closing quote is NOT on the same line,
|
||||
# i.e. a multi-line inline program. A single-line -c closes its own quote and
|
||||
# so does not match.
|
||||
readonly MULTILINE_C_RE='(^|[^[:alnum:]_])python3?[[:space:]]+(-[[:alnum:]]+[[:space:]]+)*-c[[:space:]]*("[^"]*$|'"'"'[^'"'"']*$)'
|
||||
|
||||
for file in "$@"; do
|
||||
case "$file" in
|
||||
*.sh | *.bash | *.zsh) ;;
|
||||
*) continue ;;
|
||||
esac
|
||||
[[ -f "$file" ]] || continue
|
||||
|
||||
while IFS=: read -r lineno text; do
|
||||
[[ -z "${lineno:-}" ]] && continue
|
||||
stripped="$(printf '%s' "$text" | sed 's/^[[:space:]]*//')"
|
||||
# A commented-out example is documentation, not shipped logic. Without
|
||||
# this, the rule's own explanatory comments would fail the hook.
|
||||
[[ "$stripped" == \#* ]] && continue
|
||||
errors+=("$file:$lineno: $stripped")
|
||||
done < <(grep -nE "$HEREDOC_RE|$MULTILINE_C_RE" "$file" 2>/dev/null || true)
|
||||
done
|
||||
|
||||
if [[ ${#errors[@]} -gt 0 ]]; then
|
||||
echo "ERROR: Python program logic embedded in a shell script."
|
||||
echo "Inline Python is skipped by ruff, mypy, pylint, bandit and pytest."
|
||||
echo ""
|
||||
for err in "${errors[@]}"; do
|
||||
echo " $err"
|
||||
done
|
||||
echo ""
|
||||
echo "Move the code to its own .py file and call it:"
|
||||
echo " python3 \"\$(dirname \"\$0\")/helper.py\" \"\$arg\" # bash"
|
||||
echo " python3 \"\${0:A:h}/helper.py\" \"\$arg\" # zsh"
|
||||
echo ""
|
||||
echo "A single-line probe such as python3 -c 'import kasa' is allowed."
|
||||
exit 1
|
||||
fi
|
||||
Loading…
Reference in New Issue
Block a user