Merge bugfix/project-rules-residuals into develop

This commit is contained in:
bchanot committed 2026-11-13 13:43:33 +01:00
commit e6e8241863
10 files changed
+157 -28

No files matched your search

+1
View File
@@ -737,3 +737,4 @@ rules:
- /feat design-hook-removal (block E1): hooks/design-toolchain-reminder.sh + test + UserPromptSubmit entry deleted (9c038fd), T7c → T8i synthetic fixture, docs 19ec5b7 (CHANGELOG Removed wording = doc-syncer draft, user asked no mid-run questions). BDR-148. Gates 0 MET / 1 four oracles MET / 2 PASS, make test all green. Open sessions log one non-blocking hook error per prompt until restart.
- /feat skill-routing-descriptions (block E2): CLAUDE.global.md § Skill routing 311 → 306 lines, 6 lines dropped into bugfix/ship-feature descriptions (3b30153, docs 88b67ab). Feater died mid-run (API 403 Cloudflare) after the 3 edits: gates proved the diff, no re-dispatch. Verifier caught a contract ledger bug: 1b. row absorbed by gates.sh → LRN-284, row renumbered 6. BDR-149.
- /bugfix cc-oom-score-adj (block G): BLK-044 root cause = sshd drop-in OOMScoreAdjust=-1000 inherited; lib/cc.sh raises oom_score_adj to 500 raise-only (6c10d3a, T7-oom, docs f5ce4ae). Gates 0 MET / 1 two rounds (80-col + redirect order) / 2 PASS, make test green. Live proof needs a fresh cc session (user). LRN-285.
- /bugfix project-rules-residuals (block H1, phase 0 leftovers): emit via _rules_out_path_ok + write guard, extract keeps tmp+out on rewrite failure, physical parent check (9d5530a, T9h + T9z3-z13, docs cd90080). 3 lenses: BLOCKER silent refusal on a file parent (T9w2), MAJOR section's last copy deleted by r1; callers onboarder/scaffolder stop on rc 2 ≠ exists. LRN-286.
+5
View File
@@ -2077,3 +2077,8 @@ Rule: when editing a doctrine file under structure locks, grep the test's lock s
- **Context**: BLK-044 (block G 2026-11-13): claude via `cc` at oom_score_adj -1000, origin "unknown" for weeks.
- **Pattern**: oom_score_adj is inherited across fork/exec; a service-level `OOMScoreAdjust=` (here sshd's drop-in `/etc/systemd/system/ssh.service.d/override.conf`) leaks into every interactive descendant (tmux server → shell → dtach → claude). Trace = walk `/proc/<pid>/oom_score_adj` up the parent chain, then `systemctl cat` the first unit. `systemd-run --scope -p OOMScoreAdjust=…` fails ("Unknown assignment": scopes take no exec-context properties). Raising one's own value needs no privilege; lowering does.
- **Apply**: a per-session fix = write `/proc/self/oom_score_adj` in the shell that `exec`s the target (raise-only guard `-lt N`, stderr silenced BEFORE the `>` open so a failed open stays quiet). Proof = `/proc/<pid>/oom_score_adj` of a fresh session; a running session never reflects the fix. `choom -n N -- cmd` (util-linux) is the equivalent binary.
## LRN-286 — Physical under-the-project path check in bash: walk -d ancestors, CDPATH='' cd -P, pwd -P both sides; a post-truncation failure keeps every copy (2026-11-13)
- **Context**: lib/project-rules.sh `_rules_out_path_ok` (phase 0 leftover, block H1 2026-11-13), 3 challengers + 1 confirmation.
- **Pattern**: a "stays under the project" check for a path that may not exist yet = walk `dirname` up while `[ ! -d ]` (skip file/dangling-link ancestors so `mkdir -p` keeps naming them), then `CDPATH='' cd -P -- "./$parent" && pwd -P` vs `pwd -P` (never `$PWD`: `-C` does a logical cd); prefix pattern `"${here%/}"|"${here%/}"/*`. `CDPATH= cd` trips shellcheck SC1007; `cd -P` of a relative name consults CDPATH and prints the dir.
- **Apply**: a failure branch after an O_TRUNC write must keep every copy and name them (the "keep $tmp" ask alone would drop the only copy of the moved section); a caller mapping "rc 2 = exists" must learn the new rc-2 causes in the same change; a `1b.`-style test idea often duplicates an existing case: check labels before adding.
+1 -4
View File
@@ -52,10 +52,7 @@ Shared rules for every phase:
### BDR-125 residuals (after 3.0.0)
Open children of the shipped phases (History 2026-11-12), text verbatim.
- Phase 0 / 0b leftovers (extract-hardening 14a93e3, lib/project-rules.sh):
- [ ] `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 0 / 0b leftovers: done 2026-11-13 (block H1, bugfix/project-rules-residuals 9d5530a, LRN-286): emit via `_rules_out_path_ok` + mkdir/write rc, extract keeps `$tmp` and `$out` on a rewrite failure, physical parent check (`cd -P`, `pwd -P`).
- Phase 4a leftovers:
- [ ] faunosteo: NOT upgraded (V1-2 active in another session's worktree; user: no write to develop or the V1-2 worktree). Patch prepared off-repo from V1-2 587cbb2 (rc 0, 520 lines, open BLK-008/009 unchanged); at integration re-run `memory.sh -C <faunosteo> upgrade` on the branch the user names rather than applying the stale patch. Stale clone `/home/bchanot/faunosteo` (main, 2026-07-01): left alone, user to decide.
- [ ] BLK-038 residuals (gates 2026-10-30, non-blocking): v1 line echoed on stderr unfiltered (CWE-150 LOW, terminal control chars from a foreign registry; same as existing "upgrade lost" path); no fixture asserts the "wrote upgrade status" / "dated status line kept" messages; the real-data replay oracle lives in the session scratchpad (`up4a/realdata.sh`), not re-runnable: port to a `make` target over fixture copies if the upgrade is touched again; `memory.sh journal` leaves two blank lines before a new date heading on files ending with a blank line (commit-changer note).
@@ -0,0 +1,40 @@
# CONTRACT — project-rules-residuals (phase 0 leftovers, block H1)
- date: 2026-11-13 | flow: bugfix | branch: bugfix/project-rules-residuals (off develop)
- status: active
## REQUEST (verbatim — IMMUTABLE)
Phase 0 / 0b leftovers (extract-hardening 14a93e3, lib/project-rules.sh): `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`).
## CLARIFICATIONS
- [gated 2026-11-13, challenge r2] the cat-failure branch keeps BOTH `$tmp` and `$out` (the request's "keep $tmp" alone would drop the section's last copy); callers onboarder.md:144 / scaffolder.md:86-89 read an rc 2 not ending in ` exists` as STOP. Compare with `pwd -P` on both sides (the request's `$PWD` is logical under `-C`). Plan: .claude/tasks/plans/2026-11-13-project-rules-residuals-1300.md § r2.
## ACCEPTANCE CRITERIA
1. Physical parent check present; `$PWD` never used; CDPATH neutralized.
CHECK: cd /home/bchanot/Documents/claude && echo $(grep -c "CDPATH='' cd -P" lib/project-rules.sh)$(grep -c '\$PWD' lib/project-rules.sh)$(grep -c 'must stay under the project' lib/project-rules.sh)
EXPECT: 101
EVIDENCE: MET exit=0 marker-found :: 101
2. emit routes through `_rules_out_path_ok` and checks the write; extract's cat-failure branch keeps both files.
CHECK: cd /home/bchanot/Documents/claude && echo $(grep -c '_rules_out_path_ok' lib/project-rules.sh)$(grep -c 'not written' lib/project-rules.sh)$(grep -c 'rewrite failed' lib/project-rules.sh)
EXPECT: 321
EVIDENCE: MET exit=0 marker-found :: 321
3. Suite green with the new cases (escape refused for extract and emit, `-C <symlink>` accepted, emit write failure named, T9h re-asserted).
CHECK: cd /home/bchanot/Documents/claude && bash lib/tests/project-rules.test.sh 2>&1 | tail -1
EXPECT: FAIL=0
EVIDENCE: MET exit=0 marker-found :: PASS=153 FAIL=0
4. New cases exist.
CHECK: cd /home/bchanot/Documents/claude && grep -c 'must stay under the project\|link-esc\|cannot create' lib/tests/project-rules.test.sh | grep -c '^[3-9]'
EXPECT: 1
EVIDENCE: MET exit=0 marker-found :: 1
5. Callers patched.
CHECK: cd /home/bchanot/Documents/claude && grep -c 'any other rc 2 → STOP' agents/onboarder.md agents/scaffolder.md | grep -c ':1$'
EXPECT: 2
EVIDENCE: MET exit=0 marker-found :: 2
6. shellcheck -x clean on lib/project-rules.sh and lib/tests/project-rules.test.sh; new/changed lines ≤ 80 cols; `_rules_out_path_ok` ≤ 25 logic lines.
CHECK: cd /home/bchanot/Documents/claude && shellcheck -x lib/project-rules.sh lib/tests/project-rules.test.sh && echo SC-OK
EXPECT: SC-OK
EVIDENCE: MET exit=0 marker-found :: SC-OK
7. make test green (manual, bun on PATH).
(manual: run 2026-11-13 on the bugfixer diff, `PATH=$HOME/.bun/bin:$PATH make test` → "SUITE: all GREEN / all suites green", rc=0)
## FILE SCOPE
lib/project-rules.sh, lib/tests/project-rules.test.sh, agents/onboarder.md (:144 one line), agents/scaffolder.md (:86-89 one line). TODO phase 0 lines → STEP 8.
@@ -0,0 +1,26 @@
# PLAN — project-rules-residuals (phase 0 leftovers, block H1) r1
Scope: lib/project-rules.sh (:189-241), lib/tests/project-rules.test.sh.
- `_rules_out_path_ok <out>` (:189): keep the lexical cases; then `parent=$(dirname "$out")`; walk up (`while [ ! -e "$parent" ]; do parent=$(dirname "$parent"); done`, stops at `.`); `phys=$(cd -P "$parent" 2>/dev/null && pwd -P) || return 2`; `case "$phys" in "$PWD"|"$PWD"/*) ;; *) echo "project-rules: out must stay under the project: $out" >&2; return 2;; esac`. `$PWD` is the physical cwd? No: use `here=$(pwd -P)` for both sides. Drop the `shortcut:` comment (shortcut upgraded).
- `project_rules_emit` (:228): replace `[ -e "$2" ] && …` with `_rules_out_path_ok "$2" || return 2`; `mkdir -p "$(dirname "$2")" || { echo "project-rules: cannot create $(dirname "$2")" >&2; return 2; }`; the brace group `> "$2" || { rm -f "$2"; echo "project-rules: $2 not written" >&2; return 2; }`.
- `project_rules_extract` (:217-223): two steps. `_rules_without_section "$src" "$hline" > "$tmp" || { rm -f "$tmp" "$out"; echo "project-rules: $src untouched, $out removed" >&2; return 2; }` then `cat "$tmp" > "$src" || { rm -f "$out"; echo "project-rules: $src may be truncated, full copy kept at $tmp; $out removed" >&2; return 2; }`. Success path unchanged (`rm -f "$tmp"`, "moved" line).
- Tests (follow T7-T9 idiom, sandbox cwd): T10 `.claude/rules` → symlink to a dir outside the sandbox: extract and emit both rc 2 with "must stay under the project"; the link's target untouched. T11 emit onto a dangling symlink → rc 2 "exists". T12 emit with `dirname "$2"` = an existing regular file → rc 2 "cannot create", no output file. T13 extract on a read-only `$src` (chmod 444; skip with a SKIP line when running as root): rc 2, stderr names a path that exists and holds the rewritten text, `$src` byte-identical, `$out` absent; test removes that tmp.
- Limits: `_rules_out_path_ok` stays ≤ 25 logic lines (walk-up loop + case); if not, a helper `_rules_phys_parent <path>` prints the physical first-existing ancestor.
- Disposition: honors BDR-125 phase 0 (extract-hardening 14a93e3 scope), the "shortcut → TODO" rule (the lexical shortcut upgraded, comment dropped).
## r2 (correctness FATAL(2), robustness CONCERNS(3), simplicity CONCERNS(3); all folded) — r2 WINS over r1 where they differ
- `_rules_out_path_ok <out>` FINAL body: lexical cases kept; the `[ -e ] || [ -L ]` "exists" block KEPT (last-component symlink); then
`here=$(pwd -P) && [ -n "$here" ] || { echo "project-rules: cannot resolve cwd" >&2; return 2; }`;
`parent=$(dirname "$out"); while [ ! -d "$parent" ]; do parent=$(dirname "$parent"); done` (a regular-file or dangling-link ancestor is skipped so mkdir keeps naming "cannot create"; ends at `.`);
`phys=$(CDPATH= cd -P -- "./$parent" 2>/dev/null && pwd -P) || { echo "project-rules: cannot resolve $parent" >&2; return 2; }`;
`case "$phys" in "$here"|"$here"/*) ;; *) echo "project-rules: out must stay under the project: $out" >&2; return 2;; esac`. Never `$PWD`. Drop the `shortcut:` comment.
- New helper `_rules_mkdir_parent <out>` = `mkdir -p "$(dirname "$1")" 2>/dev/null || { echo "project-rules: cannot create $(dirname "$1")" >&2; return 2; }`, used by extract (:210-211 replaced) and emit (one copy of the message).
- emit: `_rules_out_path_ok "$2" || return 2` replaces the `-e` test; `_rules_mkdir_parent "$2" || return 2`; the brace group `> "$2" || { rm -f "$2"; echo "project-rules: $2 not written" >&2; return 2; }`.
- extract step 2 (cat failure): REMOVE NOTHING. `echo "project-rules: $src rewrite failed: rewritten text at $tmp, section at $out (restore $src from git, or cat $tmp > $src to complete)" >&2; return 2`. Step 1 (`_rules_without_section` failure) unchanged: rm tmp + out, "$src untouched, $out removed".
- Callers (scope +2): agents/onboarder.md:144 and agents/scaffolder.md:86-89 → "rc 2 whose stderr line ends with ` exists` → keep the file; any other rc 2 → STOP, relay the lib's line".
- Tests (labels = suffixes after the LAST existing T9*/T12* label, never T10-T13): T9 escape: project at `$FIX/p-esc` (cwd), `.claude/rules` → symlink to `$FIX/out-esc`: extract rc 2 + "must stay under the project", emit (T12's skeleton + matching fixture, copied) rc 2 same line, `$FIX/out-esc` empty. T9 positive: emit via `-C "$FIX/link-esc"` (symlink to a sandbox project) rc 0. T12 write failure: emit with `dirname "$2"` = existing regular file → rc 2 + "cannot create", no file. T9h (read-only src) RE-ASSERTED to the new semantics: run with `TMPDIR="$FIX/tmp"`; rc 2; stderr names a path under `$FIX/tmp` that exists and holds the rewritten text; `$out` present; `$src` byte-identical. No new root SKIP (existing chmod tests fail as root today; out of scope, noted).
## r3 (confirmation CONCERNS(3), folded; FINAL, no further challenge)
- `_rules_out_path_ok`: `local out="$1" here parent phys`; `CDPATH='' cd -P -- "./$parent"` (SC1007-clean, repo form lib/gstack-links.sh:68); case pattern `"${here%/}"|"${here%/}"/*`; header comment :189 → "rc 2 when absolute, has a .. part, exists, or resolves outside cwd".
- Step-2 message: `project-rules: $src rewrite failed: rewritten text at $tmp, section at $out. Complete with: cat $tmp > $src; or abort: rm $out when $src is intact` (no git hint).
- onboarder.md S1 rollback (:251-255) gains one clause: a row that failed at the rewrite step leaves `.claude/rules/<name>.md` and a tmp named on stderr: delete both too (scope: still agents/onboarder.md).
- Tests: T9h re-assertion runs with `mkdir -p "$FIX/tmp"` first and ends with `rm -f .claude/rules/beta.md "$FIX"/tmp/tmp.*` (T9x2-x4 unchanged). New emit write-failure case: emit onto `.claude/rules/x.md/` (trailing slash: passes the exists check, open fails ENOTDIR) → rc 2, stderr `not written`, `[ -e .claude/rules/x.md ]` = no (this exercises the `> "$2"` guard; the regular-file-parent case keeps proving "cannot create").
+1
View File
@@ -19,6 +19,7 @@ Format follows [Keep a Changelog](https://keepachangelog.com/) and this project
### Fixed
- `cc <name> [profile]` (lib/cc.sh) raises the session's `oom_score_adj` to 500 before `exec claude` (raise-only, silent if the write fails). sshd's systemd override `OOMScoreAdjust=-1000` was inherited through tmux and dtach by every claude started with `cc`, so the OOM killer never picked a runaway session; `systemd-run --scope` rejects that property, hence the write inside the session (BLK-044). Test: `lib/tests/cc.test.sh` T7-oom.
- `lib/project-rules.sh` (`extract`, `emit`): the out-path check resolves the rule file's first existing parent physically and refuses (rc 2, `must stay under the project`) a path a symlinked component sends outside the project; 3.0.0 checked `..` lexically only. `emit` goes through the same check and reports `cannot create` or `not written` (no partial file) with rc 2 where it printed `emitted`. An `extract` that wrote the rule but fails to rewrite CLAUDE.md keeps the rule file and the rewritten text in a temp file and names both on stderr with the command to complete or abort; it used to delete both. `/onboard` and `/init-project` keep an existing rule on an rc 2 ending in ` exists` and stop on any other rc 2. Tests: `lib/tests/project-rules.test.sh` T9h (re-asserted), T9z3 to T9z13.
## [3.0.0] — 2026-11-12
+4 -2
View File
@@ -141,7 +141,8 @@ bash ~/.claude/lib/project-rules.sh -C <PROJECT_ROOT> emit ~/.claude/templates/p
`build_cmd`, `testing` layout from `test_cmd`); never duplicate the
always-on Build/Test sections.
- rc 1 → no match, nothing written, skip silently.
- rc 2 → the file exists: keep it, never overwrite.
- rc 2 whose stderr line ends with ` exists` → keep the file, never
overwrite; any other rc 2 → STOP, relay the lib's line.
Then `bash ~/.claude/lib/project-rules.sh -C <PROJECT_ROOT> check` must print
`N rule(s) ok`. The `!.claude/rules/` negation comes from the gitflow socle
at /onboard STEP 2.6 (`gitflow init` reconciles it); PHASE 4 stays as is.
@@ -252,7 +253,8 @@ rc ≠ 0 (should not happen after S0) → ROLLBACK: `git -C <PROJECT_ROOT>
reset -q`, `git -C <PROJECT_ROOT> checkout -- CLAUDE.md` (plus
`.gitignore` when S0 appended to it), delete every
`.claude/rules/<name>.md` this run wrote (literal paths, one `rm` per
row), then STOP with the row and the lib's message.
row; a row that failed at the rewrite step also leaves a tmp file named
on stderr: delete it too), then STOP with the row and the lib's message.
S2 — POINTER. If CLAUDE.md has no `## Path-scoped rules` section, append:
```markdown
+3 -1
View File
@@ -87,7 +87,9 @@ bash ~/.claude/lib/project-rules.sh -C <PROJECT_ROOT> emit ~/.claude/templates/p
rc 0 → fill its stubs from the DESIGN (image, compose services, test
layout, pipeline jobs), no placeholders left; rc 1 → nothing matched,
skipped (a plain scaffold yields `docker.md` when Docker is relevant, and
`testing.md` only when the stack scaffolds a test file). Then
`testing.md` only when the stack scaffolds a test file); rc 2 whose stderr
line ends with ` exists` → keep the file, any other rc 2 → STOP, relay the
lib's line. Then
`bash ~/.claude/lib/project-rules.sh -C <PROJECT_ROOT> check` prints
`N rule(s) ok`. The `.gitignore` negation `!.claude/rules/` comes from the
gitflow socle at STEP 5f (`gitflow init` reconciles it before the root
+38 -19
View File
@@ -186,17 +186,33 @@ _rules_globs_all_match() { # <globs-json> → rc 0 valid and every glob matches
done <<< "$globs"
}
_rules_out_path_ok() { # <out> → 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 ;;
_rules_out_path_ok() { # <out> → rc 2 when absolute, has a .. part, exists,
# or resolves outside cwd
local out="$1" here parent phys
case "$out" in
/*) echo "project-rules: out must be relative: $out" >&2; return 2 ;;
..|../*|*/..|*/../*)
echo "project-rules: out must not contain ..: $1" >&2; return 2 ;;
echo "project-rules: out must not contain ..: $out" >&2; return 2 ;;
esac
if [ -e "$1" ] || [ -L "$1" ]; then
echo "project-rules: $1 exists" >&2; return 2
if [ -e "$out" ] || [ -L "$out" ]; then
echo "project-rules: $out exists" >&2; return 2
fi
here=$(pwd -P) && [ -n "$here" ] \
|| { echo "project-rules: cannot resolve cwd" >&2; return 2; }
parent=$(dirname "$out")
while [ ! -d "$parent" ]; do parent=$(dirname "$parent"); done
phys=$(CDPATH='' cd -P -- "./$parent" 2>/dev/null && pwd -P) \
|| { echo "project-rules: cannot resolve $parent" >&2; return 2; }
case "$phys" in
"${here%/}"|"${here%/}"/*) ;;
*) echo "project-rules: out must stay under the project: $out" >&2
return 2 ;;
esac
}
_rules_mkdir_parent() { # <out> → rc 2 when its directory cannot be created
mkdir -p "$(dirname "$1")" 2>/dev/null \
|| { echo "project-rules: cannot create $(dirname "$1")" >&2; return 2; }
}
project_rules_extract() { # <file> <heading> <out.md> <globs-json>
@@ -207,20 +223,22 @@ project_rules_extract() { # <file> <heading> <out.md> <globs-json>
return 2; }
_rules_out_path_ok "$out" || return 2
_rules_globs_all_match "$globs" || return 2
mkdir -p "$(dirname "$out")" \
|| { echo "project-rules: cannot create $(dirname "$out")" >&2; return 2; }
_rules_mkdir_parent "$out" || 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" \
|| { 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"
echo "project-rules: $src not rewritten, $out removed" >&2
return 2
fi
_rules_without_section "$src" "$hline" > "$tmp" \
|| { rm -f "$tmp" "$out"
echo "project-rules: $src untouched, $out removed" >&2
return 2; }
cat "$tmp" > "$src" || {
echo "project-rules: $src rewrite failed: rewritten text at $tmp," \
"section at $out. Complete with: cat $tmp > $src;" \
"or abort: rm $out when $src is intact" >&2
return 2; }
rm -f "$tmp"
echo "project-rules: moved '$hline' to $out"
}
@@ -233,10 +251,11 @@ project_rules_emit() { # <skeleton.md> <out.md> → the rule, matching globs on
|| { echo "project-rules: $1: no valid paths: array" >&2; return 2; }
kept=$(_rules_matching "$globs") \
|| { echo "project-rules: $1: no glob matches, not emitted" >&2; return 1; }
[ -e "$2" ] && { echo "project-rules: $2 exists" >&2; return 2; }
mkdir -p "$(dirname "$2")"
_rules_out_path_ok "$2" || return 2
_rules_mkdir_parent "$2" || return 2
{ printf -- '---\npaths: %s\n---\n' "$kept"
awk 'body { print } NR > 1 && $0 == "---" { body = 1 }' "$1"; } > "$2"
awk 'body { print } NR > 1 && $0 == "---" { body = 1 }' "$1"; } > "$2" \
|| { rm -f "$2"; echo "project-rules: $2 not written" >&2; return 2; }
echo "project-rules: emitted $2 paths: $kept"
}
+38 -2
View File
@@ -165,11 +165,18 @@ TMPDIR=/nonexistent bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md '[
check T9e-no-tmp-rc "$?" 2
check T9f-no-tmp-nothing-written "$([ -e .claude/rules/beta.md ] && echo yes || echo no)" no
check T9g-no-tmp-beta-still-there "$(grep -c '^## Beta' CLAUDE.md)" 1
# a failed rewrite after the truncation keeps the rewritten text and the rule
mkdir -p "$FIX/tmp"; cp CLAUDE.md "$FIX/src.before"
chmod a-w CLAUDE.md
bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md '["src/**"]' 2>/dev/null
err=$(TMPDIR="$FIX/tmp" bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md '["src/**"]' 2>&1 >/dev/null)
check T9h-unwritable-src-rc "$?" 2
check T9i-unwritable-src-rule-removed "$([ -e .claude/rules/beta.md ] && echo yes || echo no)" no
kept=$(printf '%s\n' "$err" | sed -n 's/.*rewritten text at \([^,]*\),.*/\1/p')
check T9h2-unwritable-src-tmp-named "$(case "$kept" in "$FIX"/tmp/*) [ -e "$kept" ] && echo yes || echo no ;; *) echo no ;; esac)" yes
check T9h3-unwritable-src-tmp-rewritten "$(grep -c '^## Beta' "$kept")" 0
check T9i-unwritable-src-rule-kept "$([ -e .claude/rules/beta.md ] && echo yes || echo no)" yes
check T9h4-unwritable-src-untouched "$(cmp -s CLAUDE.md "$FIX/src.before" && echo yes || echo no)" yes
chmod u+w CLAUDE.md
rm -f .claude/rules/beta.md "$FIX"/tmp/tmp.*
check T9j-unwritable-src-beta-still-there "$(grep -c '^## Beta' CLAUDE.md)" 1
# T9k a mid-file H1 ends the section (foreign CLAUDE.md with several parts)
@@ -229,6 +236,35 @@ 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
# T9z3 a symlinked .claude/rules resolving outside the project is refused by
# extract and emit; a symlinked -C dir stays accepted
mkdir -p "$FIX/p-esc/.claude" "$FIX/out-esc" "$FIX/p-esc/src"
ln -s "$FIX/out-esc" "$FIX/p-esc/.claude/rules"
cp CLAUDE.md "$FIX/p-esc/CLAUDE.md"; echo x > "$FIX/p-esc/src/a.c"
echo x > "$FIX/p-esc/Dockerfile"
cd "$FIX/p-esc" || exit 1
err=$(bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md '["src/**"]' 2>&1 >/dev/null)
check T9z3-escape-extract-rc "$?" 2
check T9z4-escape-extract-reason "$(printf '%s\n' "$err" | grep -c 'must stay under the project')" 1
err=$(bash "$PR" emit "$ROOT/templates/project-rules/docker.md" .claude/rules/docker.md 2>&1 >/dev/null)
check T9z5-escape-emit-rc "$?" 2
check T9z6-escape-emit-reason "$(printf '%s\n' "$err" | grep -c 'must stay under the project')" 1
check T9z7-escape-target-empty "$(find "$FIX/out-esc" -mindepth 1 | wc -l | tr -d ' ')" 0
cd "$FIX" || exit 1
mkdir -p "$FIX/p-ok" && echo x > "$FIX/p-ok/Dockerfile" && ln -s "$FIX/p-ok" "$FIX/link-esc"
bash "$PR" -C "$FIX/link-esc" emit "$ROOT/templates/project-rules/docker.md" .claude/rules/docker.md >/dev/null
check T9z8-symlinked-C-emit-rc "$?" 0
cd "$FIX/p-ok" || exit 1
mkdir -p .claude/rules && touch .claude/rules/notadir
err=$(bash "$PR" emit "$ROOT/templates/project-rules/docker.md" .claude/rules/notadir/x.md 2>&1 >/dev/null)
check T9z9-emit-mkdir-fails-rc "$?" 2
check T9z10-emit-mkdir-fails-reason "$(printf '%s\n' "$err" | grep -c 'cannot create')" 1
err=$(bash "$PR" emit "$ROOT/templates/project-rules/docker.md" .claude/rules/x.md/ 2>&1 >/dev/null)
check T9z11-emit-write-fails-rc "$?" 2
check T9z12-emit-write-fails-reason "$(printf '%s\n' "$err" | grep -c 'not written')" 1
check T9z13-emit-write-fails-no-file "$([ -e .claude/rules/x.md ] && echo yes || echo no)" no
cd "$FIX" || exit 1
# T10 the hook appends one jq line, prints nothing, never fails
HOOK="$ROOT/hooks/instructions-loaded.sh"
LOG="$FIX/il.jsonl"