diff --git a/.claude/memory/journal.md b/.claude/memory/journal.md
index 1ba99a3..ebd2e25 100644
--- a/.claude/memory/journal.md
+++ b/.claude/memory/journal.md
@@ -664,3 +664,4 @@ rules:
- /feat rules-h3-extract (feature/rules-h3-extract): extract takes `'### title'`, sections --depth 3, split S4/S0 rows; 3 lenses SOLID/CONCERNS(2)/CONCERNS(1) + confirmation FATAL(2) (LC_COLLATE glob order, re-emission leaked to ##) → r3; feater DONE 116/116; GATE 0 MET; verifier ECARTS(1 floor: SC2016 suppression) → CONFORME 8/8; auditor PASS (3 pre-existing MEDIUM queued); ab5049e. LRN-234. Merge on user go, then 0b.
- Phase 0b run (10 repos, faunosteo excluded: on the user's feature branch): 3 splits on chore/claude-md-split, merge on user go — zenquality 4 rules (Memory files 19.4k → 14.5k, project row 10k → 5k), game 2 rules (16k → 14.9k), angelscar356 1 rule (16.2k → 15.4k); 7 keep-everything (overview/stack/commands/structure/conventions only, S4 rule). `gitflow start` refreshed zenquality's own .githooks → S0 STOP on a dirty tree, committed as chore ed10340, re-dispatched. Three onboarder runs in parallel, one repo each, all clean first pass.
- faunosteo split on the user's feature branch (user choice): 11 rules (10 ### under Project conventions + Docker), Memory files 31.9k → 27.2k, project row 22.3k → 17.7k, 7a75974. Phase 0b done: 4 repos split, 7 keep everything.
+- /feat extract-hardening (feature/extract-hardening 14a93e3 + docs 9160ce2): `_rules_out_path_ok` (absolute, `..`, exists or dangling link → rc 2, reason on stderr), mkdir + rule-write rc checked before the source rewrite, S0.3 prose in lib case order, 116 → 139 checks. Plan r1 `if ! { … } > "$out"` blind to a failed redirect (2 BLOCKER, bash 5.3.9 probe) → `|| { …; return 2; }`; r2 confirmation CONCERNS(1) (GNU mkdir stderr also says "cannot create"). Verifier CONFORME 8/8, security PASS. LRN-235. Three follow-ups in TODO (emit rc, rewrite keeps $tmp, lexical `..`). Merge on user go.
diff --git a/.claude/memory/learnings.md b/.claude/memory/learnings.md
index 0578dba..b51844b 100644
--- a/.claude/memory/learnings.md
+++ b/.claude/memory/learnings.md
@@ -250,6 +250,7 @@ rules:
| LRN-232 | 2026-10-27 | Path rules load inside sub-agents (agent_id in hook log); a "does X load" proof prompt must name the Read tool call | per-repo split pass, any load proof |
| LRN-233 | 2026-10-27 | Bash under pipefail: head after producer = rc 141, blanked PATH hides bash (rc 127), unchecked mktemp+rewrite chain = rc 0 with source unchanged; compgen -G no braces, dotglob over-matches, globstar skips symlinks under ** | any bash lib with pipes, temp files, globs |
| LRN-234 | 2026-10-27 | Bash glob order follows LC_COLLATE, make test pins no locale: tests count or name files, never assert a listing; a new-case behaviour fix is gated on the case and proven by an old-vs-new differential on real files | any test listing a dir; any change to a lib with an existing output contract |
+| LRN-235 | 2026-10-27 | bash `!` on a redirected compound command is blind to the redirect failure (rc 1 before `!`): catch with `cmd || { …; return 2; }`; a guard whose only observable is a stderr line is asserted on that line, anchored on the lib prefix | any `if ! { … } > f`; any test on a stderr reason |
---
@@ -1922,3 +1923,7 @@ Rule: when editing a doctrine file under structure locks, grep the test's lock s
## LRN-234 — Glob order follows LC_COLLATE; gate a new-case fix on the case, prove the old contract by differential
- **Context**: feat rules-h3-extract 2026-10-27. Confirmation challenger ran the planned suite under en_US.UTF-8: `printf '%s ' *` gave `sub2.md sub.md `, want `sub.md sub2.md ` (punctuation ignored in UTF-8 collation; C order differs). `make test` (Makefile) pins no `LC_ALL`. Same pass: `###` separator re-emission written as `RLENGTH - 1 < lvl` also fired for `##` ended by an H1 → 5 of 2,162 `##` extracts on this repo's md files changed vs develop, against the immutable REQUEST "`##` behaviour unchanged"; the suite (T7-T13) stayed green either way.
- **Apply**: a test never asserts a glob listing order: count (`printf '%s\n' * | wc -l`), name files, or pin `LC_ALL=C` in the recipe. A fix for a new case is gated on that case (`lvl == 3 && …`) so the existing contract stays byte-identical, and the proof is an old-vs-new differential on real inputs (every `##` of every tracked md), not the suite alone. Also: a `# shellcheck disable` in a diff trips the floor guard unless the contract authorizes it; build the offending literal instead (`printf '\140\140\140'` for backticks). Links [[LRN-233]], [[BDR-128]], [[LRN-096]].
+
+## LRN-235 — bash `!` blind to a failed redirect on a compound command; assert guards on their stderr line, anchored
+- **Context**: feat extract-hardening 2026-10-27. Plan r1 wrote `if ! { printf …; body; } > "$out"; then … return 2`. Two challengers + own probe on bash 5.3.9: redirect fails → bash returns 1 BEFORE applying `!`, `if` sees false, source rewritten, rc 0 = the bug unchanged. `! { echo a; } > bad; echo $?` → 1. shellcheck clean, floor guard blind. LRN-233 form works only because its redirects sit INSIDE the group. Same pass: `grep -c 'cannot create'` on captured stderr = 2 on GNU coreutils (mkdir prints `mkdir: cannot create directory …`), 1 on uutils (this host) → host-dependent test. mkdir rc check + write rc check give same rc and same fs state; only the stderr line tells them apart.
+- **Apply**: a redirected group is checked as `{ a && b; } > f || { cleanup; echo … >&2; return 2; }` (chain inside with `&&`, else the last command masks printf). Never `if ! { … } > f`. A guard whose only observable is a message gets a test on that message, `grep -c '^project-rules: …'` anchored on the lib's own prefix, never a bare word a system tool may also print. A fixture escape (`../x` from the fixture root, `ln -s /nonexistent`) runs from a subdir of the fixture and links inside it. Links [[LRN-233]], [[LRN-096]], [[BDR-128]].
diff --git a/.claude/tasks/TODO.md b/.claude/tasks/TODO.md
index fd88b9c..bbf810f 100644
--- a/.claude/tasks/TODO.md
+++ b/.claude/tasks/TODO.md
@@ -23,7 +23,10 @@ Shared rules for every phase:
- [ ] Phase 2 folds: narrow the pre-commit `.claude/**` exemption so a rules-only commit cannot land on develop/main (deferred 2026-10-26).
- [x] **Phase 0b, per-repo pass** (2026-10-27: 10 repos classified; split on branches awaiting the user's merge: zenquality 1942dcf 4 rules 46%, game 527bdcc 2 rules, angelscar356 ab75ecd 1 rule; keep-everything verdict, no branch: config, integration_alphalink, robin_petier, trucks_at_work, bchanot-cv, for_you, nuit-folle (all sections always-on by the S4 rule); faunosteo 7a75974 11 rules on the user's feature branch, 31.9k → 27.2k; zenquality/game/angelscar356 merged 0ff05f1/d9be6b1/853b55b; proof per repo = the user's next session there) (after phase 0 merges; via `/onboard split`, one `chore/claude-md-split` per repo off develop, approval per repo, never an automatic merge; `/context` before/after in that repo's journal). Order by size: faunosteo 52,322 chars (on feature/v1-1-clients-animals with 5 dirty files: finish or stash first, S1 refuses otherwise), zenquality 23,378, game 14,918 (on feature/replay-hint), robin_petier 12,895, config 4,990, integration_alphalink 4,249. None has `.claude/rules/` yet; all use the `.claude/*` file form (BDR-003). Sections a sub-agent needs stay always-on unless the phase 0 sub-agent proof says path rules load there (proof 2026-10-26: they do; verify per repo).
- [x] Before faunosteo: `###` granularity shipped 2026-10-27 (feature/rules-h3-extract ab5049e: `extract '### title'`, `sections --depth 3`, split rows `###
`). Was: decide `###` granularity (STEP 6 review 2026-10-26).
- - [ ] /hotfix after 0b, lib/project-rules.sh `extract` (security gate 2026-10-27, pre-existing, non-blocking): check the `> "$out"` group and `mkdir -p` rc before rewriting `$src` (today a failed rule write still removes the section, rc 0); `[ -e "$out" ] || [ -L "$out" ]` and refuse `..`/absolute ``; onboarder S0.3 says `####x` is a bare title but the lib rejects it (align the prose). Its path-bound content (i18n, Schemas, Stores, PDF, Supabase, Storage, 12.6 KB) sits in `###` subsections of the always-on `## Project conventions`; `##`-only extract moves ~1.5% of the file. Option: `extract` accepts a `###` heading (section ends at the next heading of the same or higher level), `sections --depth 3`. User decides; `game` also has a `'` in `## 4. POINTS D'ATTENTION…` (S4 refuses it until renamed, or extract by index).
+ - [x] /feat extract-hardening (2026-10-27, 14a93e3: `_rules_out_path_ok` refuses absolute, `..`, existing or dangling-link out; mkdir and rule-write rc checked before the source rewrite; S0.3 prose in the lib case order; 139 checks). Was: /hotfix after 0b, lib/project-rules.sh `extract` (security gate 2026-10-27, pre-existing, non-blocking): check the `> "$out"` group and `mkdir -p` rc before rewriting `$src` (today a failed rule write still removes the section, rc 0); `[ -e "$out" ] || [ -L "$out" ]` and refuse `..`/absolute ``; onboarder S0.3 says `####x` is a bare title but the lib rejects it (align the prose). Its path-bound content (i18n, Schemas, Stores, PDF, Supabase, Storage, 12.6 KB) sits in `###` subsections of the always-on `## Project conventions`; `##`-only extract moves ~1.5% of the file. Option: `extract` accepts a `###` heading (section ends at the next heading of the same or higher level), `sections --depth 3`. User decides; `game` also has a `'` in `## 4. POINTS D'ATTENTION…` (S4 refuses it until renamed, or extract by index).
+ - [ ] `emit`: reuse `_rules_out_path_ok` (today `-e` only, a dangling link at the target is followed) and check the `mkdir -p` and `> "$2"` rc (today "emitted" rc 0 on a failed write).
+ - [ ] `extract` rewrite failure mid-`cat` (ENOSPC/EIO after the truncation of `$src`): keep `$tmp` and name it on stderr instead of `rm -f` (today "$src not rewritten" can be false, the two copies gone; onboarder S1 rollback covers the split path, not direct lib use).
+ - [ ] shortcut `_rules_out_path_ok`: the `..` check is lexical, a symlinked parent component (`.claude` or `.claude/rules` pointing elsewhere) is not resolved; upgrade = `cd -P` on the parent dir, compare with `$PWD` (no portable `realpath -m`).
- [ ] **Phase 4a, memory v2 write side: shell, no mod** (shrunk 2026-10-25: the measured recall channels never read Meta; the measured status bug is a reconcile parser bug)
- Facts 2026-10-24 (scratchpad memeval, 434 entries): registries ~1M chars, never in always-on context, read only by /feat /bugfix /ship-feature (analyze-before-plan); Index drift 6 rows; status in 3 places (title tag, body Status line, Index column) → reconcile false-open BLK-032..034; no single writer (Edit needs a Read of a 357k file, so real writes are ad hoc python/bash); 8 projects on this machine hold v1 registries. Verified 2026-10-25: `lib/reconcile.sh:60-67` drops the entry's OWN header while BLK-032..034 carry `[resolved]` there (the false-open); nothing machine-reads `## Index` (analyze-before-plan.md:42, reconcile.sh:9, test T1); 1 of 1,290 entries across the 8 projects above a 1% accent ratio.
diff --git a/.claude/tasks/contracts/2026-10-27-extract-hardening-1838.md b/.claude/tasks/contracts/2026-10-27-extract-hardening-1838.md
new file mode 100644
index 0000000..ec6c6d6
--- /dev/null
+++ b/.claude/tasks/contracts/2026-10-27-extract-hardening-1838.md
@@ -0,0 +1,37 @@
+# CONTRACT — extract-hardening
+- date: 2026-10-27 | flow: feat | branch: feature/extract-hardening (off develop 06012ea)
+- status: active
+
+## REQUEST (verbatim — IMMUTABLE)
+lib/project-rules.sh `extract` hardening (TODO.md line 26, security gate 2026-10-27 follow-up): (1) check the rc of the `> "$out"` rule write group and of `mkdir -p` before rewriting `$src`, so a failed rule write leaves the source untouched and returns rc 2 (today the section is still removed with rc 0); (2) refuse an `` that already exists (`[ -e "$out" ] || [ -L "$out" ]`), is absolute, or contains a `..` component, rc 2 with nothing written; (3) align agents/onboarder.md MODE: split S0.3 prose with the lib (`####x` is rejected, not a bare title). Tests in lib/tests/project-rules.test.sh for each refusal and for the untouched source on a failed write.
+
+## CLARIFICATIONS
+(pass A: none — request complete)
+
+## ACCEPTANCE CRITERIA
+1. `extract` whose `mkdir -p "$(dirname "$out")"` fails (a file sits where the parent dir must be) is rc 2, nothing written, source unchanged.
+2. `extract` whose rule write (`{ … } > "$out"`) fails (parent dir not writable) is rc 2, no rule file left behind, source unchanged: the heading still occurs once.
+ CHECK: bash lib/tests/project-rules.test.sh | tail -1
+ EXPECT: FAIL=0
+ EVIDENCE: MET exit=0 marker-found :: PASS=139 FAIL=0
+3. `extract` refuses, rc 2 and nothing written: an absolute ``, an `` with a `..` component (`a/../b`, `../b`, `a/..`), an `` that exists, an `` that is a dangling symlink (`-L` true, `-e` false). The refusal names the reason on stderr. Each refusal runs before any glob check or temp file.
+4. Every pre-existing extract contract holds: T7-T13 of the suite unchanged and green; `##` behaviour byte-identical (no change to the awk helpers).
+5. `agents/onboarder.md` MODE: split S0.3 follows the lib's case order, refusal first: a heading starting with `# ` or `####` (space or not) is refused by the lib (rc 2, STOP with the row); a row starting with `## ` → STOP (a bare row carries no `## `); then `### ` is a subsection; then any other leading `#` (`#1 priority`) is a bare `##` title. A sequential reader can no longer land `####x` on the bare-title clause. No other prose change.
+ CHECK: grep -q '####' agents/onboarder.md && echo H4-NAMED
+ EXPECT: H4-NAMED
+ EVIDENCE: MET exit=0 marker-found :: H4-NAMED
+6. `lib/project-rules.sh` stays shellcheck clean, every line ≤ 80 chars, functions ≤ 25 logic lines and ≤ 5 locals; no `# shellcheck disable` added.
+ CHECK: shellcheck lib/project-rules.sh lib/tests/project-rules.test.sh && [ -z "$(awk 'length > 80' lib/project-rules.sh)" ] && ! grep -q 'shellcheck disable' lib/project-rules.sh && echo WIDTH-OK
+ EXPECT: WIDTH-OK
+ EVIDENCE: MET exit=0 marker-found :: WIDTH-OK
+7. `make test` all GREEN.
+ CHECK: make test 2>&1 | tail -1
+ EXPECT: all suites green
+ EVIDENCE: MET exit=0 marker-found :: all suites green
+8. `bash lib/project-rules.sh check` still prints `4 rule(s) ok` on this repo.
+ CHECK: bash lib/project-rules.sh check | tail -1
+ EXPECT: project-rules: 4 rule(s) ok
+ EVIDENCE: MET exit=0 marker-found :: project-rules: 4 rule(s) ok
+
+## FILE SCOPE
+lib/project-rules.sh, lib/tests/project-rules.test.sh, agents/onboarder.md (§ MODE: split S0.3 only)
diff --git a/.claude/tasks/plans/2026-10-27-extract-hardening-1838.md b/.claude/tasks/plans/2026-10-27-extract-hardening-1838.md
new file mode 100644
index 0000000..3799354
--- /dev/null
+++ b/.claude/tasks/plans/2026-10-27-extract-hardening-1838.md
@@ -0,0 +1,115 @@
+# PLAN — extract-hardening (feat, 2026-10-27, r2 after the 3-lens challenge)
+Contract: `.claude/tasks/contracts/2026-10-27-extract-hardening-1838.md`
+
+## Files
+- `lib/project-rules.sh` — `project_rules_extract` (lines 185-205); one new helper `_rules_out_path_ok` placed right before it.
+- `lib/tests/project-rules.test.sh` — new blocks `T9o`..`T9z` after T9k (line ~184), before T10.
+- `agents/onboarder.md` — MODE: split S0.3, lines 213-215 (the heading sentence only).
+
+## Approach
+1. New helper, same shape as `_rules_heading_line` (stderr message + rc 2). The `..` check is lexical, on the argument string; a symlinked component is not resolved (no GNU-only `realpath -m`), named by a `shortcut:` comment:
+ ```bash
+ _rules_out_path_ok() { # → rc 2 when absolute, has a .. part, or exists
+ # shortcut: lexical only, a symlinked component is not resolved (no
+ # portable realpath -m); upgrade = resolve the parent dir with cd -P
+ case "$1" in
+ /*) echo "project-rules: out must be relative: $1" >&2; return 2 ;;
+ ..|../*|*/..|*/../*)
+ echo "project-rules: out must not contain ..: $1" >&2; return 2 ;;
+ esac
+ if [ -e "$1" ] || [ -L "$1" ]; then
+ echo "project-rules: $1 exists" >&2; return 2
+ fi
+ }
+ ```
+ `a/../b`, `../b`, `a/..` and `..` each match one of the four patterns; `a/..b` or `..a` do not (ordinary names). Keep the existing `$out exists` wording so T8c reads the same.
+2. In `project_rules_extract`, replace line 191 (`[ -e "$out" ] && …`) by `_rules_out_path_ok "$out" || return 2`, same position: after the heading-count check, before `_rules_globs_all_match` (no glob check, no temp file before a refusal).
+3. `mkdir -p` checked in the file's one-statement idiom (line 194 form), before `mktemp`:
+ ```bash
+ mkdir -p "$(dirname "$out")" \
+ || { echo "project-rules: cannot create $(dirname "$out")" >&2; return 2; }
+ ```
+4. The rule write becomes a checked statement. NOT `if ! { …; } > "$out"`: when the redirection of a compound command fails, bash returns 1 before applying `!`, so the failure is missed (probed on bash 5.3.9: `if ! { echo hi; } > /proc/nowhere/x` prints MISSED). The `||` form catches it (probed: rc 2). `printf` and the body are chained with `&&` so a failed printf is not masked by an empty body:
+ ```bash
+ { printf -- '---\npaths: %s\n---\n\n# %s\n' "$globs" "${hline#* }" \
+ && _rules_section_body "$src" "$hline"; } > "$out" \
+ || { rm -f "$tmp" "$out"
+ echo "project-rules: $out not written, $src untouched" >&2
+ return 2; }
+ ```
+ Then the existing source rewrite block (lines 197-202), unchanged. The three awk helpers are not touched (`##` behaviour byte-identical).
+5. Logic-line budget: the helper absorbs the three path refusals; `project_rules_extract` stays ≤ 25 logic lines and 5 locals (`src out globs hline tmp`). Every line ≤ 80 chars (the `||` lines above are ≤ 79). No `# shellcheck disable`.
+6. `agents/onboarder.md` S0.3: replace
+ `a heading whose leading `#` run is followed by a space starts with `### `; any other leading `#` is a bare `##` title (`#1 priority`);`
+ by the lib's case order, refusal first:
+ `a heading starting with `# ` or `####` (space or not) is refused by the lib (rc 2): STOP with the row; one starting with `## ` → STOP too (a bare row carries no `## `); one starting with `### ` is a subsection; any other leading `#` is a bare `##` title (`#1 priority`);`
+ Nothing else in the file changes. (The lib passes `'## '*` through as written; a row written that way would be looked up as `## ## Foo` at S0.4, so S0.3 stops it, as today's prose does.)
+
+## Edge cases
+- Dangling symlink as ``: `-e` false, `-L` true → refused, the link stays.
+- `mkdir -p` ENOTDIR: a regular file where the parent dir must be; the stderr line `cannot create` is what distinguishes this guard from the write check (both give rc 2 and an untouched source), so the test asserts it.
+- Write failure independent of root: `` with a trailing slash (`.claude/rules/beta.md/`) passes the path helper, `mkdir -p .claude/rules` succeeds, the open fails with EISDIR; nothing is created. The chmod-based case (`ro/`) stays as the second probe, with the LRN-233 root limit.
+- `rm -f "$tmp"` on the write-failure path: `$tmp` exists at that point (mktemp ran before the write).
+- All refusals rc 2, never 1. Each new refusal is asserted by its stderr reason, not by rc alone (rc 2 is shared).
+
+## Tests (lib/tests/project-rules.test.sh, after T9k, in the `$FIX` root where CLAUDE.md holds `## Beta` once; `src/app.ts` exists there)
+```bash
+# T9o extract refuses an absolute, a .. or a symlinked , before any write,
+# each refusal named on stderr
+err=$(bash "$PR" extract CLAUDE.md 'Beta' "$FIX/abs.md" '["nowhere/**"]' 2>&1 >/dev/null)
+check T9o-absolute-out-rc "$?" 2
+check T9o2-absolute-reason-before-globs "$(printf '%s\n' "$err" | grep -c 'must be relative')" 1
+check T9p-absolute-nothing-written "$([ -e "$FIX/abs.md" ] && echo yes || echo no)" no
+err=$(bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/../escape.md '["src/**"]' 2>&1 >/dev/null)
+check T9q-dotdot-out-rc "$?" 2
+check T9q2-dotdot-reason "$(printf '%s\n' "$err" | grep -c 'must not contain')" 1
+check T9r-dotdot-nothing-written "$([ -e .claude/escape.md ] && echo yes || echo no)" no
+bash "$PR" extract CLAUDE.md 'Beta' nope/.. '["src/**"]' 2>/dev/null
+check T9r2-trailing-dotdot-rc "$?" 2
+check T9r3-trailing-dotdot-no-dir "$([ -e nope ] && echo yes || echo no)" no
+mkdir -p "$FIX/sub/src" && cd "$FIX/sub" || exit 1
+echo x > src/a.ts
+printf '## Beta\nbbb\n' > CLAUDE.md
+bash "$PR" extract CLAUDE.md 'Beta' ../escape.md '["src/**"]' 2>/dev/null
+check T9s-leading-dotdot-rc "$?" 2
+check T9s2-leading-dotdot-nothing-written "$([ -e "$FIX/escape.md" ] && echo yes || echo no)" no
+cd "$FIX" || exit 1
+ln -s "$FIX/absent.md" .claude/rules/dangling.md
+err=$(bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/dangling.md '["src/**"]' 2>&1 >/dev/null)
+check T9t-dangling-symlink-rc "$?" 2
+check T9t2-dangling-reason "$(printf '%s\n' "$err" | grep -c 'exists')" 1
+check T9u-dangling-still-link "$([ -L .claude/rules/dangling.md ] && echo yes || echo no)" yes
+rm -f .claude/rules/dangling.md
+check T9v-refusals-keep-beta "$(grep -c '^## Beta' CLAUDE.md)" 1
+
+# T9w a failed mkdir (named on stderr) or a failed rule write is rc 2, the
+# source untouched and no rule left behind; the trailing-slash case holds as root
+touch .claude/rules/notadir
+err=$(bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/notadir/beta.md '["src/**"]' 2>&1 >/dev/null)
+check T9w-mkdir-fails-rc "$?" 2
+check T9w2-mkdir-fails-reason "$(printf '%s\n' "$err" | grep -c '^project-rules: cannot create')" 1
+check T9x-mkdir-fails-beta-kept "$(grep -c '^## Beta' CLAUDE.md)" 1
+bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md/ '["src/**"]' 2>/dev/null
+check T9x2-eisdir-write-rc "$?" 2
+check T9x3-eisdir-beta-kept "$(grep -c '^## Beta' CLAUDE.md)" 1
+check T9x4-eisdir-nothing-written "$([ -e .claude/rules/beta.md ] && echo yes || echo no)" no
+mkdir .claude/rules/ro && chmod a-w .claude/rules/ro
+bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/ro/beta.md '["src/**"]' 2>/dev/null
+check T9y-write-fails-rc "$?" 2
+check T9z-write-fails-beta-kept "$(grep -c '^## Beta' CLAUDE.md)" 1
+check T9z2-write-fails-no-rule "$([ -e .claude/rules/ro/beta.md ] && echo yes || echo no)" no
+chmod u+w .claude/rules/ro
+```
+Lines over 80 chars in the test file follow the file's existing practice (T9e already exceeds it); the width limit in AC6 applies to the lib. `err` reuses the T5h variable (line 78); every use follows its own assignment. No `ls`, no `wc` (SC2012, BSD padding). Suite total: 116 + 23 = 139 checks, FAIL=0, if the count differs the names above are the reference, not the number.
+
+## Follow-ups (TODO.md, not this run; REQUEST is extract-only)
+- `emit`: reuse `_rules_out_path_ok`, check `mkdir -p` and the `> "$2"` write rc (today "emitted" rc 0 on failure, `-e` only so a dangling link is followed).
+- `extract` rewrite failure mid-`cat` (ENOSPC/EIO after truncation): keep `$tmp` and name it on stderr instead of `rm -f` (today the message "$src not rewritten" can be false).
+- `_rules_out_path_ok` shortcut: `..` check is lexical, a symlinked component (`.claude` or `.claude/rules` pointing elsewhere) is not resolved; upgrade = `cd -P` on the parent dir, compare against `$PWD`.
+- The main loop writes these three lines under TODO.md at CAPITALIZE; the executor touches no file outside FILE SCOPE.
+
+## Disposition
+- honors LRN-233 by: redirection failures caught with `cmd || { …; return 2; }` (the `if ! { …; } > f` form proven blind on bash 5.3.9), `printf && body` chained, one failure-path test per new rc check with its stderr reason, `mktemp` still checked.
+- honors LRN-234 by: no glob listing order asserted, no `# shellcheck disable`, the awk helpers untouched so `##` stays byte-identical.
+- honors LRN-096 by: every new guard has a flip test that goes red when the guard is removed (reason on stderr, file state), never rc 2 alone.
+- honors BDR-128 by: refusals stay in the one oracle, rc 2, culprit named on stderr; `/onboard split` prose follows the lib's case order.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 349ac0e..b391e4b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,7 +17,7 @@ Format follows [Keep a Changelog](https://keepachangelog.com/) and this project
- **model-router prompt classifier**: a plain prompt typed in the composer (12 characters or more, not a slash command, not mid-turn, never under `-p` or in a sub-agent) that no keyword rule, skill, run slot, `/route` or floor covers is labelled by one call to the engine's small model (`design`, `analysis`, `coding`, `bookkeeping`, `chat`), bounded to 3 s. The label maps to plan, reflect, implement or apply and the turn takes that phase's route: its effort, and its model under the switch and the downgrade cap, never above the session model (an `ultrathink` floor raises the effort only, the guess's model holds); no dialog, no file write, gone at the end of the turn, replaced by any route call or typed skill, and dropped for that turn by a `/effort` typed between turns. Three failures in a row pause it for `cooldownMinutes` (one toast, `classifier: paused` in the status line). `"classify": false` in `~/.claude/model-router.json` and `/route classify on|off` turn it off; `/route show` prints `classify:`. The status line reads `route: classifier ` and `/route verbose on` adds `(: classifier)`.
- **model-router cold-read caps**: both main-loop caps measure the cold read a switch costs, from a per-model record of main's last step, warm within the engine's prompt-cache TTL (`cache_ttl` 5m or 1h from main's model-switch events, 1 h before the first, kept across `/clear`; records cleared by `/clear` and `/route reload`), plus the submitted prompt the engine's usage may not count yet (code points / 3 of its text, context entries and a typed skill's body, added at the turn's first main step unless usage already grew by half of it; a `document` attachment blocks the switch for that step); `downgradeMaxTokens` (default 120000) joins `upgradeMaxTokens` (200000). A model-loaded helper skill inside a best-tier run moves effort only; main routed onto a model whose window the context outgrew returns to the session model. `/route show` prints `switch: on (downgrade, cap 120000 tokens cold, ttl 60 min)`, the status line shows ` · switch off` only when the switch is off, the skip line is logged once per cap crossing and the change toast names a cap hold. A built-in `general-purpose` agent that main spawns with no model while main sits below the session model starts on the session model instead of inheriting the lower one.
- **/explain-diff skill**: turns a change (commit, range, branch, merge, PR, staged or uncommitted work) into one self-contained HTML page saved outside the repo, under `~/Documents/explanations//YYYY-MM-DD-.html`. The page has a background, an intuition built on toy data with HTML diagrams, a code walkthrough grouped by concern and a 5-question interactive quiz. Adapted from Geoffrey Litt's explain-diff gist. A bundled template (light and dark, works offline) and a checker (`scripts/check_explanation.py`: file name, location, sections, quiz, code escaping, no ASCII art, no em-dash) hold every page to the same floor. Routing row `reflect`; profiles full, max and dev.
-- **Project path rules (CLAUDE.md split)**: `lib/project-rules.sh` (`check`, `matches`, `sections`, `extract`, `emit`, optional `-C `) is the glob oracle for `.claude/rules/.md` rules, one-line `paths:` frontmatter, loaded on Read of a matching file; suite `lib/tests/project-rules.test.sh`. `/onboard split` classifies an existing project CLAUDE.md section by section behind a gate and moves the path-bound ones into rules on a `chore/claude-md-split` branch. `extract` also moves a `###` subsection (heading passed as `'### '`; a bare heading stays `##`), which ends at the next heading of level 3 or less outside a fence, and `sections --depth 3` lists each `##` section's subsections with their sizes, so `/onboard split` can move a path-bound subsection out of an always-on parent (one `### ` row; a row's parent is never itself a move row). `/onboard` and `/init-project` emit `docker`, `testing` and `ci` rules from `templates/project-rules/` when their globs match. This repo's CLAUDE.md now keeps its header, Health Stack and a pointer; four sections load as path rules.
+- **Project path rules (CLAUDE.md split)**: `lib/project-rules.sh` (`check`, `matches`, `sections`, `extract`, `emit`, optional `-C `) is the glob oracle for `.claude/rules/.md` rules, one-line `paths:` frontmatter, loaded on Read of a matching file; suite `lib/tests/project-rules.test.sh`. `/onboard split` classifies an existing project CLAUDE.md section by section behind a gate and moves the path-bound ones into rules on a `chore/claude-md-split` branch. `extract` also moves a `###` subsection (heading passed as `'### '`; a bare heading stays `##`), which ends at the next heading of level 3 or less outside a fence, and `sections --depth 3` lists each `##` section's subsections with their sizes, so `/onboard split` can move a path-bound subsection out of an always-on parent (one `### ` row; a row's parent is never itself a move row). `extract` refuses an absolute ``, one with a `..` part, or one that already exists (a dangling symlink included), with rc 2 and the reason on stderr, and leaves the source CLAUDE.md untouched when the rule directory or file cannot be written. `/onboard` and `/init-project` emit `docker`, `testing` and `ci` rules from `templates/project-rules/` when their globs match. This repo's CLAUDE.md now keeps its header, Health Stack and a pointer; four sections load as path rules.
- **InstructionsLoaded logger**: `hooks/instructions-loaded.sh`, wired in `settings.json`, appends one JSON line per instruction file loaded to `~/.claude/logs/instructions-loaded.jsonl` (rotated above 5 MB), the proof that a path rule loaded.
### Changed
diff --git a/agents/onboarder.md b/agents/onboarder.md
index cd201b9..bcedd11 100644
--- a/agents/onboarder.md
+++ b/agents/onboarder.md
@@ -211,8 +211,10 @@ S0 — PREFLIGHT (no write before every step passes):
only) is fixed by the user before any split.
3. Per row, by reading the row (never a shell test that embeds it):
`` matches `[a-z0-9][a-z0-9-]*`; the heading has no `'`; a heading
- whose leading `#` run is followed by a space starts with `### `; any other
- leading `#` is a bare `##` title (`#1 priority`); every
+ starting with `# ` or `####` (space or not) is refused by the lib (rc 2):
+ STOP with the row; one starting with `## ` → STOP too (a bare row carries
+ no `## `); one starting with `### ` is a subsection; any other leading
+ `#` is a bare `##` title (`#1 priority`); every
glob is relative, with no `'`, no leading `/`, no `..`. A row that
fails → STOP with the row and the field.
4. Per row: `PR sections --depth 3 CLAUDE.md` lists the row's heading exactly
diff --git a/lib/project-rules.sh b/lib/project-rules.sh
index 8b96a72..47c0c80 100644
--- a/lib/project-rules.sh
+++ b/lib/project-rules.sh
@@ -182,18 +182,35 @@ _rules_globs_all_match() { # → rc 0 valid and every glob matches
done <<< "$globs"
}
+_rules_out_path_ok() { # → rc 2 when absolute, has a .. part, or exists
+ # shortcut: lexical only, a symlinked component is not resolved (no
+ # portable realpath -m); upgrade = resolve the parent dir with cd -P
+ case "$1" in
+ /*) echo "project-rules: out must be relative: $1" >&2; return 2 ;;
+ ..|../*|*/..|*/../*)
+ echo "project-rules: out must not contain ..: $1" >&2; return 2 ;;
+ esac
+ if [ -e "$1" ] || [ -L "$1" ]; then
+ echo "project-rules: $1 exists" >&2; return 2
+ fi
+}
+
project_rules_extract() { #
local src="$1" out="$3" globs="$4" hline tmp
hline=$(_rules_heading_line "$2") || return 2
[ "$(_rules_heading_count "$src" "$hline")" = 1 ] || {
echo "project-rules: heading must occur exactly once: $hline" >&2
return 2; }
- [ -e "$out" ] && { echo "project-rules: $out exists" >&2; return 2; }
+ _rules_out_path_ok "$out" || return 2
_rules_globs_all_match "$globs" || return 2
- mkdir -p "$(dirname "$out")"
+ mkdir -p "$(dirname "$out")" \
+ || { echo "project-rules: cannot create $(dirname "$out")" >&2; return 2; }
tmp=$(mktemp) || { echo "project-rules: no temp file" >&2; return 2; }
- { printf -- '---\npaths: %s\n---\n\n# %s\n' "$globs" "${hline#* }"
- _rules_section_body "$src" "$hline"; } > "$out"
+ { printf -- '---\npaths: %s\n---\n\n# %s\n' "$globs" "${hline#* }" \
+ && _rules_section_body "$src" "$hline"; } > "$out" \
+ || { rm -f "$tmp" "$out"
+ echo "project-rules: $out not written, $src untouched" >&2
+ return 2; }
if ! { _rules_without_section "$src" "$hline" > "$tmp" \
&& cat "$tmp" > "$src"; }; then
rm -f "$tmp" "$out"
diff --git a/lib/tests/project-rules.test.sh b/lib/tests/project-rules.test.sh
index ecbbfab..1978961 100644
--- a/lib/tests/project-rules.test.sh
+++ b/lib/tests/project-rules.test.sh
@@ -183,6 +183,52 @@ check T9m-h1-intro-kept "$(grep -c '^intro' CLAUDE.md)" 1
check T9n-rule-body-stops-at-h1 "$(tail -1 .claude/rules/a.md)" aaa
cd "$FIX" || exit 1
+# T9o extract refuses an absolute, a .. or a symlinked , before any write,
+# each refusal named on stderr
+err=$(bash "$PR" extract CLAUDE.md 'Beta' "$FIX/abs.md" '["nowhere/**"]' 2>&1 >/dev/null)
+check T9o-absolute-out-rc "$?" 2
+check T9o2-absolute-reason-before-globs "$(printf '%s\n' "$err" | grep -c 'must be relative')" 1
+check T9p-absolute-nothing-written "$([ -e "$FIX/abs.md" ] && echo yes || echo no)" no
+err=$(bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/../escape.md '["src/**"]' 2>&1 >/dev/null)
+check T9q-dotdot-out-rc "$?" 2
+check T9q2-dotdot-reason "$(printf '%s\n' "$err" | grep -c 'must not contain')" 1
+check T9r-dotdot-nothing-written "$([ -e .claude/escape.md ] && echo yes || echo no)" no
+bash "$PR" extract CLAUDE.md 'Beta' nope/.. '["src/**"]' 2>/dev/null
+check T9r2-trailing-dotdot-rc "$?" 2
+check T9r3-trailing-dotdot-no-dir "$([ -e nope ] && echo yes || echo no)" no
+mkdir -p "$FIX/sub/src" && cd "$FIX/sub" || exit 1
+echo x > src/a.ts
+printf '## Beta\nbbb\n' > CLAUDE.md
+bash "$PR" extract CLAUDE.md 'Beta' ../escape.md '["src/**"]' 2>/dev/null
+check T9s-leading-dotdot-rc "$?" 2
+check T9s2-leading-dotdot-nothing-written "$([ -e "$FIX/escape.md" ] && echo yes || echo no)" no
+cd "$FIX" || exit 1
+ln -s "$FIX/absent.md" .claude/rules/dangling.md
+err=$(bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/dangling.md '["src/**"]' 2>&1 >/dev/null)
+check T9t-dangling-symlink-rc "$?" 2
+check T9t2-dangling-reason "$(printf '%s\n' "$err" | grep -c 'exists')" 1
+check T9u-dangling-still-link "$([ -L .claude/rules/dangling.md ] && echo yes || echo no)" yes
+rm -f .claude/rules/dangling.md
+check T9v-refusals-keep-beta "$(grep -c '^## Beta' CLAUDE.md)" 1
+
+# T9w a failed mkdir (named on stderr) or a failed rule write is rc 2, the
+# source untouched and no rule left behind; the trailing-slash case holds as root
+touch .claude/rules/notadir
+err=$(bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/notadir/beta.md '["src/**"]' 2>&1 >/dev/null)
+check T9w-mkdir-fails-rc "$?" 2
+check T9w2-mkdir-fails-reason "$(printf '%s\n' "$err" | grep -c '^project-rules: cannot create')" 1
+check T9x-mkdir-fails-beta-kept "$(grep -c '^## Beta' CLAUDE.md)" 1
+bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md/ '["src/**"]' 2>/dev/null
+check T9x2-eisdir-write-rc "$?" 2
+check T9x3-eisdir-beta-kept "$(grep -c '^## Beta' CLAUDE.md)" 1
+check T9x4-eisdir-nothing-written "$([ -e .claude/rules/beta.md ] && echo yes || echo no)" no
+mkdir .claude/rules/ro && chmod a-w .claude/rules/ro
+bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/ro/beta.md '["src/**"]' 2>/dev/null
+check T9y-write-fails-rc "$?" 2
+check T9z-write-fails-beta-kept "$(grep -c '^## Beta' CLAUDE.md)" 1
+check T9z2-write-fails-no-rule "$([ -e .claude/rules/ro/beta.md ] && echo yes || echo no)" no
+chmod u+w .claude/rules/ro
+
# T10 the hook appends one jq line, prints nothing, never fails
HOOK="$ROOT/hooks/instructions-loaded.sh"
LOG="$FIX/il.jsonl"