feat(rules): extract refuses an absolute, .. or existing out path and checks the mkdir and rule-write rc before rewriting the source

A failed rule write used to remove the section with rc 0. Split S0.3 prose follows the lib's heading case order.
This commit is contained in:
bchanot committed 2026-10-27 20:03:28 +01:00
1 parent 06012ea12d
commit 14a93e3f19
3 files changed
+71 -6

No files matched your search

+4 -2
View File
@@ -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):
`<name>` 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
+21 -4
View File
@@ -182,18 +182,35 @@ _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 ;;
..|../*|*/..|*/../*)
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() { # <file> <heading> <out.md> <globs-json>
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"
+46
View File
@@ -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 <out>, 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"