From e0311f4d4f37d1a2f646aea562f1f3c5b6f91811 Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 15 Nov 2026 21:59:04 +0100 Subject: [PATCH 1/6] feat(memory): check --staged refuses a new entry that skips or reuses an ID A new ## entry must carry the next free ID, replayed in line order the way add allocates it. The rule runs only on a v2 HEAD with entries, so upgrade, first commits and template imports pass. --- lib/capitalize-commit.md | 12 +++++- lib/memory.sh | 36 +++++++++++++++-- lib/tests/memory.test.sh | 87 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 5 deletions(-) diff --git a/lib/capitalize-commit.md b/lib/capitalize-commit.md index a0377da..59bc2ac 100644 --- a/lib/capitalize-commit.md +++ b/lib/capitalize-commit.md @@ -80,8 +80,16 @@ never a shell argument. committed, nothing left staged. Causes, named on stderr: a line removed inside an existing v2 entry (only appends pass), a NEW - culprit (duplicate ID, heading shape), or a - HEAD or staged blob git cannot read; an open + culprit (duplicate ID, heading shape), a new + entry whose ID is not the next free ID + (stderr names the expected one; a committed + ID is never renamed: fix only the uncommitted + new heading and every uncommitted mention of + its ID, the `superseded by` line `add + --supersedes` wrote, the journal line, or + drop the new entry and that line and re-add + it with memory.sh add), or a HEAD or staged + blob git cannot read; an open code fence counts even when it predates the commit. A path staged apart from its working tree loses that stage: stderr names it with diff --git a/lib/memory.sh b/lib/memory.sh index a12124a..7fbba4e 100755 --- a/lib/memory.sh +++ b/lib/memory.sh @@ -15,7 +15,8 @@ # check [--staged] [] heading shape, unique IDs, no Index, # no open fence in a v2 file (v1 reported, never failed); # --staged: only NEW culprits vs HEAD, no line removed -# inside an entry, no v2 registry deleted or downgraded +# inside an entry, no v2 registry deleted or downgraded, +# a new entry's ID is the next free ID (registry in HEAD) # upgrade [] v1 → v2: Index-only dates and statuses become # body lines, `format: 2`, `## Index` removed; a missing # frontmatter comes from the template, ` - ` / ` | date | ` @@ -161,8 +162,11 @@ _mem_has_id() { # → rc 0 when a heading carries the ID } _mem_next_id() { # → max heading ID + 1, zero-padded - # every heading level, fenced or not: a gap is harmless, a duplicate is - # not (trucks_at_work writes `### BDR-001`; a stray fence hides headings) + # every heading level, fenced or not (trucks_at_work writes `### BDR-001`; + # a stray fence hides headings). check --staged replays this max in line + # order on a v2 HEAD: a new `## ` entry must be this max + 1. Holds when + # memory.sh appends the entries; a hand-inserted ID line above a new entry, + # or an ID line removed above the first entry, can refuse a valid add awk -v p="$2" ' match($0, "^#+ *" p "-[0-9]+") { n = substr($0, RSTART, RLENGTH); sub(/^#+ *[A-Z]+-/, "", n) @@ -533,6 +537,30 @@ _mem_blob() { # → rc 1 when absent, rc 2 when git cat-file blob "$1" > "$2" 2>/dev/null || { : > "$2"; return 2; } } +_mem_check_next_ids() { # → rc 2 + # when an unfenced new `## P-n` entry is not 1 + the max ID seen so far + # (HEAD, then the staged file above it; every level, fenced or not) + awk -v f="$1" -v p="$2" ' + FNR == 1 { fence = 0 } + /^```/ { fence = !fence } + FILENAME == ARGV[1] { + if (!fence && match($0, "^## " p "-[0-9][0-9][0-9]+ — .")) { + match($0, "^## " p "-[0-9]+"); entry[substr($0, 4, RLENGTH - 3)] = 1 + known = 1 } + if (match($0, "^#+ *" p "-[0-9]+")) bump() + next } + !known { exit 0 } + !fence && match($0, "^## " p "-[0-9]+") { + tok = substr($0, 4, RLENGTH - 3); n = tok; sub(/^[A-Z]+-/, "", n) + if (!(tok in entry) && n + 0 != top + 1) { + printf "memory: %s:%d: new entry %s is not the next free ID " \ + "%s-%03d\n", f, FNR, tok, p, top + 1; exit 2 } } + match($0, "^#+ *" p "-[0-9]+") { bump() } + function bump( n) { + n = substr($0, RSTART, RLENGTH); sub(/^#+ *[A-Z]+-/, "", n) + if (n + 0 > top) top = n + 0 }' "$3" "$4" >&2 +} + _mem_check_staged() { # → rc 2 on a new culprit or a lost v2 file local h s rc=0 hrc src h=$(mktemp) || { echo "memory: no temp file" >&2; return 2; } @@ -549,6 +577,8 @@ _mem_check_staged() { # → rc 2 on a new culprit or a lost v2 file elif [ -s "$s" ] && memory_is_v2 "$s"; then memory_check_file "$s" "$(_mem_prefix "$1")" "$h" "$1" || rc=2 _mem_removed_lines "$1" "$h" || rc=2 + [ "$hrc" = 0 ] && memory_is_v2 "$h" && ! _mem_fence_open "$h" \ + && { _mem_check_next_ids "$1" "$(_mem_prefix "$1")" "$h" "$s" || rc=2; } fi rm -f "$h" "$s" return "$rc" diff --git a/lib/tests/memory.test.sh b/lib/tests/memory.test.sh index d8cfc85..778cba0 100755 --- a/lib/tests/memory.test.sh +++ b/lib/tests/memory.test.sh @@ -198,6 +198,93 @@ git rm -q --cached "$B" check T8e-deletion-refused "$(bash "$MEM" check --staged >/dev/null 2>&1; echo $?)" 2 git reset -q && cat b.keep > "$B" +# T8f.. next free ID: new `## ` entries must replay `add` (max + 1) +bash "$MEM" add blocker --title four --body-file body.md >/dev/null +bash "$MEM" add blocker --title five --body-file body.md >/dev/null +git add "$B" +check T8f-two-adds-pass "$(bash "$MEM" check --staged >/dev/null 2>&1; echo $?)" 0 +git reset -q && cat b.keep > "$B" +printf '\n## BLK-005 — skip (2026-10-27)\nx\n' >> "$B" +git add "$B" +err=$(bash "$MEM" check --staged 2>&1 >/dev/null); rc=$? +check T8g-skip-refused-rc "$rc" 2 +check T8g-skip-named "$(printf '%s\n' "$err" | grep -c 'not the next free ID BLK-004')" 1 +git reset -q && cat b.keep > "$B" +printf '\n## BLK-002 — again (2026-10-27)\nx\n' >> "$B" +git add "$B" +err=$(bash "$MEM" check --staged 2>&1 >/dev/null); rc=$? +check T8h-reuse-rc "$rc" 2 +check T8h-reuse-dup "$(printf '%s\n' "$err" | grep -c 'duplicate BLK-002')" 1 +check T8h-reuse-not-seq "$(printf '%s\n' "$err" | grep -c 'next free ID')" 0 +git reset -q && cat b.keep > "$B" +printf -- '- **Friction**: x\n\n### BLK-099 note\n\n```\n## BLK-001 — quoted\n```\n' > body2.md +bash "$MEM" add blocker --title quoting --body-file body2.md >/dev/null +bash "$MEM" add blocker --title after --body-file body.md >/dev/null +git add "$B" +check T8i-body-ids-pass "$(bash "$MEM" check --staged >/dev/null 2>&1; echo $?)" 0 +check T8i-second-is-100 "$(grep -c '^## BLK-100 — after' "$B")" 1 +git reset -q && cat b.keep > "$B" + +# T8j.. each in its own repo: never reg from $FIX (it rewrites the fixture) +seq=$FIX/seq; mkdir -p "$seq/.claude/memory" +( + pass=0; fail=0 + cd "$seq" && git init -q . && git config user.email t@t \ + && git config user.name t || exit 1 + reg blockers.md blockers_registry BLK 'Blockers registry (BLK)' + printf '\n## BLK-001 — a (2026-10-27)\nx\n\n## BLK-003 — c (2026-10-27)\nx\n' \ + >> "$B" + git add -A + bash "$MEM" check --staged >/dev/null 2>&1 + check T8j-first-commit-holes-pass "$?" 0 + git commit -qm holes + printf '\n## BLK-002 — fill (2026-10-27)\nx\n' >> "$B" + git add -A + err=$(bash "$MEM" check --staged 2>&1 >/dev/null); rc=$? + check T8k-gap-fill-rc "$rc" 2 + check T8k-gap-fill-named "$(printf '%s\n' "$err" | grep -c 'not the next free ID BLK-004')" 1 + git reset -q && git checkout -- . + printf '\n- see\n\n### BLK-010 note\n' >> "$B" + git add -A && git commit -qm body + printf '\n## BLK-010 — x (2026-10-27)\nx\n' >> "$B" + git add -A + err=$(bash "$MEM" check --staged 2>&1 >/dev/null); rc=$? + check T8l-body-only-reuse-rc "$rc" 2 + check T8l-body-only-reuse-named "$(printf '%s\n' "$err" | grep -c 'not the next free ID BLK-011')" 1 + # T8m: a v1 HEAD upgraded in place keeps its IDs, nothing is new + mkdir -p "$FIX/upg/.claude/memory" && cd "$FIX/upg" && git init -q . \ + && git config user.email t@t && git config user.name t || exit 1 + printf -- '---\ntype: blockers_registry\nentry_prefix: BLK\n---\n\n# Blockers\n\n### BLK-001 - a\nx\n\n## BLK-002 — b\nx\n' > "$B" + git add -A && git commit -qm v1 + bash "$MEM" upgrade >/dev/null 2>&1 + git add -A + check T8m-upgrade-rewrote "$(grep -c '^## BLK-00[12] — ' "$B")" 2 + bash "$MEM" check --staged >/dev/null 2>&1 + check T8m-upgrade-pass "$?" 0 + # T8n: import onto a committed template with ID holes + mkdir -p "$FIX/tpl/.claude/memory" && cd "$FIX/tpl" && git init -q . \ + && git config user.email t@t && git config user.name t || exit 1 + reg blockers.md blockers_registry BLK 'Blockers registry (BLK)' + git add -A && git commit -qm tpl + printf '\n## BLK-001 — a (2026-10-27)\nx\n\n## BLK-005 — b (2026-10-27)\nx\n' >> "$B" + git add -A + bash "$MEM" check --staged >/dev/null 2>&1 + check T8n-template-import-pass "$?" 0 + # T8o: a HEAD left in an open fence skips the sequence rule + mkdir -p "$FIX/fen/.claude/memory" && cd "$FIX/fen" && git init -q . \ + && git config user.email t@t && git config user.name t || exit 1 + reg blockers.md blockers_registry BLK 'Blockers registry (BLK)' + printf '\n## BLK-001 — a (2026-10-27)\n```\nx\n' >> "$B" + git add -A && git commit -qm fence + printf '```\n\n## BLK-005 — skip (2026-10-27)\nx\n' >> "$B" + git add -A + err=$(bash "$MEM" check --staged 2>&1 >/dev/null) + check T8o-open-fence-head-skips "$(printf '%s\n' "$err" | grep -c 'not the next free ID')" 0 + printf '%s\n' "$pass $fail" > "$FIX/seq.counts" +) +# the subshell counted on its own copy of the counters: fold them back +read -r sp sf < "$FIX/seq.counts"; pass=$((pass+sp)); fail=$((fail+sf)) + # T9 upgrade: Index-only dates and statuses become body lines, the marker # lands, the Index goes, headings untouched; idempotent; refusals keep the file mkdir -p up/.claude/memory && cd up || exit 1 From 074cdde409e5c6d1470bfe9ca59049e4b88a250e Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 15 Nov 2026 21:59:21 +0100 Subject: [PATCH 2/6] chore(audits): log staged next-ID residuals --- .claude/audits/BUGS-FOUND.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.claude/audits/BUGS-FOUND.md b/.claude/audits/BUGS-FOUND.md index 5c3c795..7be512e 100644 --- a/.claude/audits/BUGS-FOUND.md +++ b/.claude/audits/BUGS-FOUND.md @@ -89,6 +89,11 @@ - lib/memory.sh `memory_index` and the digest — what still reads as not-open or absent: an empty or unknown status word on a BLK row counts as not open (game holds 14 such v1 rows, config 3, so flagging them would fire every session); an unclosed ``` fence hides every later entry with no banner line; `index` reads without the lock while `status`/`_mem_restore` truncate then write in place (memory.sh:262,319), so a racing digest can read an empty file; `[ -e ] || [ -L ] || return 0` (:609) reads a stat failure (I/O, LSM denial) as absent. Severity: low. Discovered while: /bugfix index-fail-closed plan challenge and security gate. Not fixed. - hooks/session-start.sh:358-377 `_digest_todo` / `_digest_journal` run awk on TODO.md and journal.md with no regular-file check and no timeout: a FIFO there hangs session start. agents/handover-doc-writer.md:105 reports a non-regular blockers.md as `no-file`. `_memory_digest` (:420) prints no digest at all when `.claude/memory` is not a directory. Severity: low. Discovered while: /bugfix index-fail-closed security gate and verifier. Not fixed. + + +- lib/memory-commit.sh:99 runs `check --staged` on the index, then :118 `git commit -- ` re-reads the working tree, and the memory lock is not held across the two: a registry write landing between them is committed unchecked. Fix direction: commit the checked index (no pathspec once scoped) or hold the lock across check and commit. Severity: low. Discovered while: /feat staged-next-id plan challenge (robustness r2). Not fixed. +- lib/memory.sh `_mem_removed_lines` refuses a `memory.sh upgrade` commit of a v1 file where a dialect heading (`### BLK-n`) sits below the first `## ` entry: its rewrite to `## ` counts as a removed line inside an entry, so mixed-dialect v1 registries cannot be upgraded through memory-commit. Same for /prune-memory's `heading level` fix on a v2 file. Severity: low. Discovered while: /feat staged-next-id executor (T8m fixture). Not fixed. +- lib/memory.sh `_mem_check_next_ids` assumes line order is chronological: a hand-inserted ID line above a new entry, or an ID line removed above the first entry, can refuse an entry `add` wrote correctly (named in the `_mem_next_id` comment). IDs at or above 2^53 lose precision in awk (same limit as `_mem_next_id`). Severity: low. Discovered while: /feat staged-next-id plan challenge. Not fixed. - lib/profile-state.jq `clean` strips LF/CR only: ESC and other control chars pass (no terminal sink today); human mode (:67-75) prints values unsanitized (pre-existing). Severity: low. Discovered while: /bugfix bugs-found-scripts security gate. Not fixed. - lib/tests/profile.test.sh:195 T28-nl-id-no-bare-on cannot fail (pre-fix tail is `=off`); the `clean` on pack keys has no biting assertion; lib/tests/session-digest.test.sh:245 comment still says "no digest". Severity: low. Discovered while: /bugfix bugs-found-scripts verifier. Not fixed. From 9e52f69c8fe069c713739c88d44396b7b44da0ef Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 15 Nov 2026 21:59:32 +0100 Subject: [PATCH 3/6] chore(audits): regroup two bugs-found-scripts items under their header --- .claude/audits/BUGS-FOUND.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.claude/audits/BUGS-FOUND.md b/.claude/audits/BUGS-FOUND.md index 7be512e..d8bb235 100644 --- a/.claude/audits/BUGS-FOUND.md +++ b/.claude/audits/BUGS-FOUND.md @@ -85,6 +85,8 @@ - lib/memory.sh:594-603 `memory_index` — skips a blockers.md that is not a regular file (`[ -f ] || continue`) and does not propagate the awk rc, so `index blocker` exits 0 on a registry it could not read and the session digest prints `- open: none` (SD9b/SD9c pin it). Fix direction: non-regular registry or unparsable `| BLK-` row → non-zero rc, the digest prints `unknown`. Severity: low. Discovered while: /bugfix bugs-found-scripts gates 1+2. FIXED 87aecc1 (BDR-125 gap A3). +- lib/profile-state.jq `clean` strips LF/CR only: ESC and other control chars pass (no terminal sink today); human mode (:67-75) prints values unsanitized (pre-existing). Severity: low. Discovered while: /bugfix bugs-found-scripts security gate. Not fixed. +- lib/tests/profile.test.sh:195 T28-nl-id-no-bare-on cannot fail (pre-fix tail is `=off`); the `clean` on pack keys has no biting assertion; lib/tests/session-digest.test.sh:245 comment still says "no digest". Severity: low. Discovered while: /bugfix bugs-found-scripts verifier. Not fixed. - lib/memory.sh `memory_index` and the digest — what still reads as not-open or absent: an empty or unknown status word on a BLK row counts as not open (game holds 14 such v1 rows, config 3, so flagging them would fire every session); an unclosed ``` fence hides every later entry with no banner line; `index` reads without the lock while `status`/`_mem_restore` truncate then write in place (memory.sh:262,319), so a racing digest can read an empty file; `[ -e ] || [ -L ] || return 0` (:609) reads a stat failure (I/O, LSM denial) as absent. Severity: low. Discovered while: /bugfix index-fail-closed plan challenge and security gate. Not fixed. @@ -94,8 +96,6 @@ - lib/memory-commit.sh:99 runs `check --staged` on the index, then :118 `git commit -- ` re-reads the working tree, and the memory lock is not held across the two: a registry write landing between them is committed unchecked. Fix direction: commit the checked index (no pathspec once scoped) or hold the lock across check and commit. Severity: low. Discovered while: /feat staged-next-id plan challenge (robustness r2). Not fixed. - lib/memory.sh `_mem_removed_lines` refuses a `memory.sh upgrade` commit of a v1 file where a dialect heading (`### BLK-n`) sits below the first `## ` entry: its rewrite to `## ` counts as a removed line inside an entry, so mixed-dialect v1 registries cannot be upgraded through memory-commit. Same for /prune-memory's `heading level` fix on a v2 file. Severity: low. Discovered while: /feat staged-next-id executor (T8m fixture). Not fixed. - lib/memory.sh `_mem_check_next_ids` assumes line order is chronological: a hand-inserted ID line above a new entry, or an ID line removed above the first entry, can refuse an entry `add` wrote correctly (named in the `_mem_next_id` comment). IDs at or above 2^53 lose precision in awk (same limit as `_mem_next_id`). Severity: low. Discovered while: /feat staged-next-id plan challenge. Not fixed. -- lib/profile-state.jq `clean` strips LF/CR only: ESC and other control chars pass (no terminal sink today); human mode (:67-75) prints values unsanitized (pre-existing). Severity: low. Discovered while: /bugfix bugs-found-scripts security gate. Not fixed. -- lib/tests/profile.test.sh:195 T28-nl-id-no-bare-on cannot fail (pre-fix tail is `=off`); the `clean` on pack keys has no biting assertion; lib/tests/session-digest.test.sh:245 comment still says "no digest". Severity: low. Discovered while: /bugfix bugs-found-scripts verifier. Not fixed. - doctor.sh `warn`/`pass`/`fail` print through `echo -e`: jq's `\uXXXX` escapes in the residue diff (:153) come back as raw control bytes (ESC sequences) on the terminal. Source = the user's own tracked settings.json (no trust boundary). Fix direction: `printf '%s\n'` in the shared helpers. Severity: low. Discovered while: /hotfix doctor-skilloverrides-residue security gate. Not fixed. From eb801b762038a6e4dfab22b4085ed02b58b7a425 Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 15 Nov 2026 22:15:49 +0100 Subject: [PATCH 4/6] docs: changelog for the check --staged next-ID rule --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f68073f..2d337d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The BDR-125 context queue is delivered: phases 0, 0b, 4a, 1, 5, 2 and 4b-4d ship - **Skill routing (`CLAUDE.global.md`)**: routing lines a skill's own description already covers move there (bugfix, feat/hotfix split, ship-feature, tour, reconcile, capitalize, close); the section keeps the gstack cases (investigate on explicit ask, gstack ship retired per BDR-105), the pack-off SEO/GEO/W3C line (`profile.sh enable seo|web-validate`), `cso` and media. `/bugfix` triggers gain "error", "500", "stack trace + fix"; `/ship-feature` gains "ship this feature". - **model-router**: the `fable` alias, head of the best tier, resolves to `claude-fable-5-2` (was `claude-fable-5-1`); every best-tier phase, row and skill pin naming `fable` follows. Kit tests updated. - **memory feedback**: precision, trigger precision and the miss share count only rows after the gate-1 fix (merge 2026-11-12T19:53:59Z); their actions wait for 20 post-fix asks (`Post-fix prompt asks: k/20` in FEEDBACK.md, one `NEW: post-fix prompt sample reached` line) and the session reminder keeps prompting until then. The third-pick signal is retired (recall keeps 2 picks). The reader holds one parsed transcript at a time; `labels.tsv` rows and `state.json` are validated on read. +- **memory commit chokepoint**: `lib/memory.sh check --staged`, run by `lib/memory-commit.sh commit` before every memory commit (rc 6, registry unstaged), also refuses a staged v2 registry whose new `## ` entry does not carry the next free ID: the highest ID seen at any heading level, fenced or not, in HEAD and above the entry in the staged file, plus one, the same reading `add` uses. stderr: `memory: :: new entry is not the next free ID -NNN`. A committed ID is never renamed: fix the uncommitted heading and every uncommitted mention of its ID (the `superseded by` line from `add --supersedes`, the journal line), or drop the new entry and re-add it with `memory.sh add`. Skipped when the registry is absent from HEAD, HEAD is v1 (an `upgrade` commit), HEAD has an open code fence, or HEAD has no well-formed `## ` entry. Tests: `lib/tests/memory.test.sh`. ### Removed - `hooks/design-toolchain-reminder.sh`, its `UserPromptSubmit` entry in `settings.json` (`ctx7-reminder` stays the only one) and its suite `lib/tests/design-toolchain-reminder.test.sh`. Measured 2026-10-31: 59 fires, 0 Reads of `rules/web-building.md`. Design routing stays with the `rules/web-building.md` path rule and the `CLAUDE.global.md` pointer. The fire log `~/.claude/logs/design-toolchain-fires.log` is left in place. From 9a8d4ae0eff61840044bbbee285c54e8ac7f180a Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 15 Nov 2026 22:15:58 +0100 Subject: [PATCH 5/6] chore(memory): staged next-ID journal --- .claude/memory/journal.md | 1 + .../2026-11-15-staged-next-id-2030.md | 25 +++ .../plans/2026-11-14-bdr125-gap-audit-2330.md | 2 +- .../plans/2026-11-15-staged-next-id-2030.md | 211 ++++++++++++++++++ 4 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 .claude/tasks/contracts/2026-11-15-staged-next-id-2030.md create mode 100644 .claude/tasks/plans/2026-11-15-staged-next-id-2030.md diff --git a/.claude/memory/journal.md b/.claude/memory/journal.md index 65cbc26..e4cfe23 100644 --- a/.claude/memory/journal.md +++ b/.claude/memory/journal.md @@ -762,3 +762,4 @@ rules: - Docker host-root mount deny shipped 0f73d7e: 28 rows (7 shapes x 4 prefixes), bench-proven echo analogue, verifier CONFORME, security PASS. Legacy rows dead, kept. Wrapper/podman residual added TODO:90. Docs committed. - A2 line width: 4 lines >80 cols re-wrapped (register.ts 591/683/3765, transcripts.ts 133; audit line numbers had drifted), no behavior change, make test green, security PASS. - A3 index fail-closed: memory_index rc 2 on non-regular/unreadable registry with kind, lax skip+stderr without kind (register.ts recall keeps rc 0); digest unknown on unparsable BLK row; SD9b/c re-pinned, SD9f/g/h + T20* added (87aecc1). Residuals BUGS-FOUND 07f9a7f. Next A4. +- A4 staged next-ID: check --staged refuses new ## entry not = max+1 (line-order replay of _mem_next_id, body IDs feed max only); runs on v2 HEAD with entries + no open fence, so upgrade/first commit/template import pass (e0311f4). Challenge r2 caught upgrade false refusal (trucks_at_work 17536ce replay). Residuals BUGS-FOUND 074cdde. Next A5. diff --git a/.claude/tasks/contracts/2026-11-15-staged-next-id-2030.md b/.claude/tasks/contracts/2026-11-15-staged-next-id-2030.md new file mode 100644 index 0000000..e225bf7 --- /dev/null +++ b/.claude/tasks/contracts/2026-11-15-staged-next-id-2030.md @@ -0,0 +1,25 @@ +# CONTRACT — staged-next-id (BDR-125 gap audit A4) +- date: 2026-11-15 | flow: feat | branch: feature/staged-next-id (off develop) +- status: active + +## REQUEST (verbatim — IMMUTABLE) +A4 (BDR-125 gap audit, phase 4a ask): `memory.sh check --staged` must reject a staged registry whose new entry ID is not max+1 of the IDs already committed (today a comment in lib/memory.sh says the gap is harmless and only duplicates are refused). A new entry with a skipped ID (e.g. BLK-052 when max is BLK-050) or a reused one fails the check with a named error; entries written by `memory.sh add` (next-free ID) still pass. Regression tests in lib/tests/memory.test.sh. + +## CLARIFICATIONS +- [internal 2026-11-15, challenge r1] plan .claude/tasks/plans/2026-11-15-staged-next-id-2030.md section r2 wins over the original APPROACH/EDGE CASES/TESTS. Rule replays `_mem_next_id` in line order: a new entry = unfenced `## P-n` heading absent from HEAD's `## ` set, must equal 1 + the max ID seen at any level (fenced or not) in HEAD and above it in the staged file; body lines feed the max, never count as entries, so whatever `add` writes passes. A registry absent from HEAD skips the sequence rule (first commit of a populated registry with holes passes; renumbering is forbidden). A reused ID present in HEAD's `## ` set stays the duplicate rule's job. Only v2 staged files are checked (existing rule). lib/capitalize-commit.md rc 6 cause list gains the new cause. The other 4a validator item (new BLK quotes an error verbatim) is out of scope. +- [internal 2026-11-15, re-challenge r2] plan section r3 amends r2: the sequence rule runs only on a v2 HEAD with no open fence and at least one shape-valid `## ` entry (an `upgrade` commit from v1, a first commit, or an import onto a committed empty template skips it); known entries match by exact ID token; the rc 6 remedy says a committed ID is never renamed. Tests T8m (upgrade passes) and T8n (template import passes) added. +- [internal 2026-11-15, gate 0] criterion 2 CHECK wrapped in `|| true`: `grep -c` exits 1 on zero matches while printing the expected 0; the criterion itself is unchanged. + +## ACCEPTANCE CRITERIA +1. memory suite green, with the new skipped-ID, lower-ID and multi-add cases. + CHECK: cd /home/bchanot/Documents/claude && PATH=$HOME/.bun/bin:$PATH make test suite=lib/tests/memory.test.sh &1 | tail -1 + EXPECT: all suites green + EVIDENCE: MET exit=0 marker-found :: all suites green +2. The "gap is harmless" comment is gone from lib/memory.sh. + CHECK: cd /home/bchanot/Documents/claude && { grep -c 'gap is harmless' lib/memory.sh || true; } + EXPECT: 0 + EVIDENCE: MET exit=0 marker-found :: 0 +3. Full make test green; shellcheck clean on lib/memory.sh; the refusal tests (skipped ID, gap fill, body-only reuse) fail on the pre-change lib while the pass tests pass on both; reused-ID test asserts the duplicate refusal; existing T7k (merge duplicate, staged append) and T8 (single add) still pass (manual). + +## FILE SCOPE +lib/memory.sh (_mem_next_id comment :163-165, _mem_check_staged :536-556, a new helper, usage lines :15-17), lib/tests/memory.test.sh (new T8f.. cases after T8e), lib/capitalize-commit.md (rc 6 cause list :79-91). diff --git a/.claude/tasks/plans/2026-11-14-bdr125-gap-audit-2330.md b/.claude/tasks/plans/2026-11-14-bdr125-gap-audit-2330.md index ae8b4d7..2fb583d 100644 --- a/.claude/tasks/plans/2026-11-14-bdr125-gap-audit-2330.md +++ b/.claude/tasks/plans/2026-11-14-bdr125-gap-audit-2330.md @@ -4,7 +4,7 @@ - [x] A1 DONE merged b03aced (0f73d7e). /bugfix docker host-root mount deny (phase 2, TODO:89): plan .claude/tasks/plans/2026-11-14-docker-mount-deny-1400.md r2. - [x] A2 DONE merged 2315a0b (21ef47f). /hotfix line width regressions: mods/memory/hooks/register.ts:976 (86 col, breaks 4b c10 / 4c c9 oracles), lib/memory-feedback/transcripts.ts:133 (follow-ups c7), register.test.ts:71 optional. - [x] A3 DONE 87aecc1 (docs 038670e, residuals BUGS-FOUND 07f9a7f). /bugfix memory_index rc 0 on unreadable/non-regular blockers.md → digest `- open: none` (4c digest defect, BUGS-FOUND:87): non-zero rc → `- open: unknown`; SD9b/SD9c re-pinned. -- A4 memory.sh `check --staged`: new entry ID = max+1 (4a ask TODO:170; today comment "gap harmless" :162). +- [x] A4 DONE e0311f4 (tests strengthened in same commit, CHANGELOG eb801b7, residuals BUGS-FOUND 074cdde). memory.sh `check --staged`: new entry ID = max+1 (4a ask TODO:170; today comment "gap harmless" :162). - A5 memory.test.sh T18 hard-fails without bun on PATH → SKIP line like other bun-gated tests. - A6 (user choice) routing guard fail mode (phase 2 shared rule "guards fail closed"; today fail-open). diff --git a/.claude/tasks/plans/2026-11-15-staged-next-id-2030.md b/.claude/tasks/plans/2026-11-15-staged-next-id-2030.md new file mode 100644 index 0000000..c0bc09b --- /dev/null +++ b/.claude/tasks/plans/2026-11-15-staged-next-id-2030.md @@ -0,0 +1,211 @@ +# PLAN — staged-next-id (BDR-125 gap audit A4) + +## CONTEXT +- lib/memory.sh:163 `_mem_next_id `: max over every heading + level (`^#+ *PREFIX-[0-9]+`), fenced or not, + 1. Comment :164 says "a gap + is harmless, a duplicate is not". +- lib/memory.sh:536 `_mem_check_staged `: copies HEAD blob ($h) and + staged blob ($s); on a v2 staged file runs `memory_check_file "$s" PREFIX + "$h" "$1"` (new culprits: shape, duplicate, Index, open fence) and + `_mem_removed_lines`. Caller: lib/memory-commit.sh:99 (rc 6 chokepoint, + BDR-129). No ID-sequence rule today. +- TODO.md:171 (phase 4a ask): `check [--staged]` = validator "ID = max+1 + and unique". + +## APPROACH +1. New helper in lib/memory.sh, next to `_mem_check_staged`: + `_mem_check_next_ids ` → rc 2 + (stderr) on a bad new ID, rc 0 otherwise. + - One awk over both files. ID extraction = the `_mem_next_id` regex + (`match($0, "^#+ *" p "-[0-9]+")`, every level, fenced or not), numeric + value of the digits. + - HEAD: set `old[n]`, `max` = largest n (0 when the HEAD blob is empty or + absent). + - Staged: new = n not in `old`, deduplicated (a duplicate heading is + already the culprit rule's job). + - Sort new numerically (awk has no sort in POSIX: collect into an array + indexed by n and walk n from 1 to the largest new n; IDs are small). + Hmm: walk only from min(new) to max(new) — bounded by the staged file's + largest ID. + - expected = max + 1; for each new n in ascending order: n != expected → + print `memory: : new entry - is not the + next free ID -` and set bad; then expected = + n + 1 when n == expected, else keep expected (report every bad one, + the next good one would also mismatch: simpler = report the first + mismatch only, then stop). Decision: report the first mismatch only, + one line, rc 2. + - The printed ID uses the heading's own digits (keep zero padding as + written) for the bad one, `%03d` for the expected one (what `add` + writes). +2. `_mem_check_staged`: in the v2 branch, after `memory_check_file`, add + `_mem_check_next_ids "$1" "$(_mem_prefix "$1")" "$h" "$s" || rc=2`. + HEAD absent → $h empty → max 0, first new entry must be 001. +3. Comment :164 → "every heading level, fenced or not: check --staged + reads IDs the same way, so a new ID must be this max + 1; a duplicate + is refused too" (keep the trucks_at_work / stray fence reason). +4. Usage lines :15-17: `--staged` adds "new IDs = HEAD max + 1, in order". + +## EDGE CASES +- Several `add` calls staged together: new = max+1..max+k, consecutive → + pass. +- Merge leftover duplicate (T7k): the duplicate ID is in HEAD → not new; + the appended entry is max+1 → pass. +- A new ID already present in HEAD (reuse): not "new" by set membership; + the duplicate culprit rule already refuses it when both are `## ` level. + A new ID below max and absent from HEAD (fills an old gap) → refused. +- v1 staged file → skipped as today (branch only runs for v2). +- HEAD v1, staged v2 (`upgrade`): headings keep their IDs → no new IDs. +- Fenced heading-like line in a new entry body (`## BLK-999` in a code + block): counted, like `_mem_next_id` counts it, so refused unless it is + the next ID. Accepted: such a line would also move `add`'s next ID. +- IDs with 4+ digits: numeric compare. + +## TESTS (lib/tests/memory.test.sh, after T8e, same fixture $B, committed +base restored after each case with `git reset -q && cat b.keep > "$B"`) +- T8f-two-adds-pass: two `memory.sh add blocker`, stage, `check --staged` + rc 0. +- T8g-skip-refused: append by printf a v2 heading with ID max+2, stage → + rc 2, stderr names `is not the next free ID` once. +- T8h-gap-fill-refused: build a HEAD with a gap (commit a file whose IDs + are 001 and 003 via printf), stage a new `## BLK-002 — …` → rc 2, named. +- T8i-new-file-first-id: a registry absent from HEAD, staged v2 with only + BLK-002 → rc 2; with BLK-001 → rc 0. Use its own `-C` subtree or a + separate repo dir under $FIX so $B stays untouched. +- Existing T7k and T8 must stay green. + +## DISPOSITION +Honors BDR-129 (memory.sh single writer, `check --staged` = rc 6 +chokepoint in memory-commit.sh): the rule lives in the same validator, no +new caller. LRN/BLK: none bears on ID allocation. + +## FILES +lib/memory.sh, lib/tests/memory.test.sh. + +## r2 (challenge r1: correctness CONCERNS(1 MAJOR), robustness SOLID (5 minor), simplicity CONCERNS(1 MAJOR)) — SUPERSEDES APPROACH, EDGE CASES and TESTS above +Landed: (a) body lines (`### BLK-n`, fenced `## BLK-n`) were read as new +entries → a valid `add` refused (correctness 1, robustness 2); (b) the +ascending walk was bounded by a file-controlled number (corr 3, rob 1); +(c) a registry absent from HEAD with ID holes would be refused and +renumbering is forbidden (rob 3); (d) no test for the reused-ID case the +request names (simp 1, corr 4); (e) T8h moved the shared fixture's HEAD +(corr 5, rob 5, simp 4); (f) rc 6 guide lacks the new cause (rob 4). + +1. Rule = replay of `_mem_next_id` as it ran when `add` wrote each entry. + New helper `_mem_check_next_ids ` → rc 2 + one stderr line on the first bad entry, rc 0 otherwise. + One awk over the two files, `FILENAME == ARGV[1]` for the HEAD pass + (memory_check_file idiom; an empty HEAD file yields no lines): + - HEAD pass: `top` = max ID over every `^#+ *P-[0-9]+` line, every + level, fenced or not (the `_mem_next_id` reading); `entry[n]` set for + each unfenced `^## P-[0-9]+` heading (the `_mem_culprits` reading: + toggle `fence` on `^```` lines). + - Staged pass, in line order, `top` carried over from HEAD: + a line matching `^## P-[0-9]+` outside a fence whose n is not in + `entry` is a NEW entry: n != top + 1 → print `memory: :: + new entry

- is not the next free ID

-` and exit 2 (first bad entry only). Then, for every line + matching `^#+ *P-[0-9]+` (fenced or not, any level, new entry heading + included), top = max(top, n). + - Consequence: body lines feed `top` but are never entries, so whatever + `add` writes passes by construction; a reused ID absent from HEAD's + `## ` set (body-only in HEAD, or a gap fill) is n <= top → refused; a + reused ID present in HEAD's `## ` set is left to the existing + duplicate culprit. No sort, no walk. +2. `_mem_check_staged`: in the v2 branch, after `memory_check_file`, call + it only when the HEAD blob exists (`hrc` = 0): `[ "$hrc" = 1 ] || + _mem_check_next_ids "$1" "$(_mem_prefix "$1")" "$h" "$s" || rc=2`. + A registry absent from HEAD (first commit of a populated or imported + registry, holes allowed) skips the sequence rule; shape and duplicate + rules still apply. +3. Comment lib/memory.sh:164 → say that check --staged replays this same + reading (new `## ` entry = this max + 1), keep the trucks_at_work / + stray fence reason; drop "a gap is harmless". +4. Usage lines lib/memory.sh:15-17: `--staged` also refuses a new entry + whose ID is not the next free ID (registry already in HEAD). +5. lib/capitalize-commit.md rc 6 cause list (:79-91): add "a new entry + whose ID is not the next free ID (stderr names the expected one: rename + that uncommitted heading, or remove it and re-add with memory.sh add)". + Keep the paragraph's wrapping style. + +TESTS (lib/tests/memory.test.sh, after T8e): +- In the shared fixture ($B, HEAD = BLK-001..003, restore with the + existing `git reset -q && cat b.keep > "$B"`, never commit): + - T8f-two-adds-pass: two `memory.sh add blocker`, stage, rc 0. + - T8g-skip-refused: printf-append `## BLK-005 — skip (2026-10-27)` + + a body line, stage → rc 2, stderr has `not the next free ID BLK-004` + once. + - T8h-reuse-dup: append `## BLK-002 — again (2026-10-27)`, stage → rc + 2, `duplicate BLK-002` once, `not the next free ID` 0 times. + - T8i-body-ids-pass: `memory.sh add blocker` with a body holding a line + `### BLK-099 note` and a fenced block with `## BLK-001 — quoted`; + then a second `add`; stage → rc 0 (second entry is BLK-100). +- In a separate repo under $FIX (git init like t17, `reg` helper or + printf with v2 frontmatter; run `(cd

&& bash "$MEM" check + --staged)`): + - T8j-first-commit-holes-pass: blockers.md absent from HEAD, staged with + BLK-001 and BLK-003 → rc 0. + - commit it; T8k-gap-fill-refused: append `## BLK-002 — fill`, stage → + rc 2, `not the next free ID BLK-004`. + - T8l-body-only-reuse-refused: HEAD entry body holds `### BLK-010 + note` (commit), stage a new `## BLK-010 — x` → rc 2 (expected + BLK-011). +Refusal cases (T8g, T8k, T8l) must fail on the pre-change lib; pass cases +pass on both. +FILES (final): lib/memory.sh, lib/tests/memory.test.sh, +lib/capitalize-commit.md. + +## r3 (re-challenge: correctness CONCERNS(1 MAJOR), robustness CONCERNS(1 MAJOR), same finding) — amends r2 +Landed: r2 refused a real `memory.sh upgrade` commit (v1 HEAD with +`### BDR-n` headings rewritten to `## BDR-n`; replayed on trucks_at_work +17536ce: passes today, r2 → rc 2) and its remedy steered toward +renumbering. Minors folded below. + +1. Gate (in `_mem_check_staged`, v2 branch): run `_mem_check_next_ids` + only when the HEAD blob exists (hrc 0) AND `memory_is_v2 "$h"` AND + `! _mem_fence_open "$h"`. A v1 HEAD (upgrade commit), an absent HEAD + (first commit) or a HEAD with an open fence (the open-fence culprit + already speaks) skips the sequence rule; shape and duplicate rules still + apply. Inside the awk: no shape-valid `## ` entry in HEAD (empty or + template-only registry) → exit 0 (import onto a committed template with + ID holes passes). +2. Known entry = exact ID token. HEAD pass: `entry[id]` for each unfenced + heading matching the shape `^## P-[0-9][0-9][0-9]+ — .` (the + `_mem_culprits` shape), id = the `P-digits` token. Staged pass: an + unfenced line matching `^## P-[0-9]+` whose `P-digits` token is not in + `entry` is NEW (catches re-padded `BLK-0002` and a reuse of a HEAD + heading that had a bad shape: n <= top → refused). +3. `FNR == 1 { fence = 0 }` in the awk so the staged pass never inherits + the HEAD pass's fence state. +4. Comment replacing lib/memory.sh:164 states the reading and its two + assumptions in a few lines: check --staged replays this max in line + order on a v2 HEAD, so a new `## ` entry must be this max + 1; holds + when entries are appended by memory.sh (a hand-inserted ID line above + a new entry, or an ID line removed above the first entry, can refuse a + valid add). +5. lib/capitalize-commit.md rc 6 cause text: "a new entry whose ID is not + the next free ID (stderr names the expected one). A committed ID is + never renamed: fix only the uncommitted new heading and every + uncommitted mention of its ID (the `superseded by` line `add + --supersedes` wrote, the journal line), or drop the new entry and that + line and re-add it with memory.sh add." +6. Tests, amended: + - Separate repos under $FIX: create files with printf or `(cd && + reg …)` inside the subshell only, never `reg` from $FIX (it would + overwrite the shared fixture). After T8k: `git reset -q && git + checkout -- .` in that repo before T8l's commit. + - New T8m-upgrade-pass: own repo, HEAD v1 blockers.md with `### BLK-001 + — a` and `### BLK-002 — b` (plus whatever v1 shape `memory.sh + upgrade` accepts; read `_mem_upgrade_headings` / existing T9 `up/` + fixture at memory.test.sh:203 for a valid v1 input), run `memory.sh + upgrade` there, stage, `check --staged` rc 0. + - New T8n-template-import-pass: own repo, HEAD = v2 blockers.md with + frontmatter and no entry, staged adds `## BLK-001 — a (2026-10-27)` + and `## BLK-005 — b (2026-10-27)` → rc 0. + - T8j (absent from HEAD, holes) stays a pass. + Refusal cases that must fail on the pre-change lib: T8g, T8k, T8l. +7. Out of scope, logged to BUGS-FOUND at close: memory-commit.sh checks + the index (:99) but `git commit -- ` (:118) re-reads the + working tree, unlocked: a write between the two is committed + unchecked (pre-existing). +FILES unchanged: lib/memory.sh, lib/tests/memory.test.sh, +lib/capitalize-commit.md. From 6b3395d2472ed552173a18ce99a0e92679d2b1b3 Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 15 Nov 2026 23:54:32 +0100 Subject: [PATCH 6/6] chore(memory): LRN validator replays allocator --- .claude/memory/learnings.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.claude/memory/learnings.md b/.claude/memory/learnings.md index 0536468..65231ea 100644 --- a/.claude/memory/learnings.md +++ b/.claude/memory/learnings.md @@ -2108,3 +2108,9 @@ Rule: when editing a doctrine file under structure locks, grep the test's lock s - **Pattern**: `timeout N cmd` (no --foreground) puts cmd in own process group = background of the tty. Interactive CLI (claude, anything Ink/readline) inheriting tty stdin sets terminal modes → SIGTTOU to whole group → cmd AND timeout stopped (state T), alarm never fires, hang forever. Only in a terminal; non-tty runs (Claude sessions, CI) pass, so tests stay green while user's `make test` hangs. - **Context**: lib/tests/mods.test.sh `bounded()` around `claude plugin validate --strict`, user report 2026-11-14 (4h30 in T). Reproduced under util-linux `script -qec` pty: before T from 2 s, after 52 s pass. - **Apply**: any CLI under timeout/perl group runner in tests or hooks → `' /dev/null`, not only headless runs. + +## LRN-292 — Validator replaying an allocator: same reading, same dialect gate, replay real commits (2026-11-15) +- **Pattern**: validator that checks what an allocator produced must read inputs exactly as allocator reads them, and run only where allocator's assumptions hold. Else it refuses valid output. +- **Context**: A4 `check --staged` next-ID rule (e0311f4). r1 counted body ID lines as entries → valid `add` refused. r2 ran on v1 HEAD → `upgrade` commit refused (`### BDR-n` → `## BDR-n` read as new); replay of trucks_at_work 17536ce: rc 0 today, rc 2 under r2, remedy would have steered to renumbering. +- **Apply**: replay allocator in same order (line order, any level, fenced or not); gate on dialect it assumes (v2 HEAD, ≥1 shape-valid entry, no open fence); before trusting, replay rule on real historical commits of each workflow (add, upgrade, merge, import). Tests must bite: mutate each gate clause, test must go red. +- **Reference**: lib/memory.sh `_mem_check_next_ids`, `_mem_next_id`; lib/tests/memory.test.sh T8f-T8o