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.
This commit is contained in:
bchanot committed 2026-11-15 21:59:04 +01:00
1 parent 411d22ff92
commit e0311f4d4f
3 files changed
+130 -5

No files matched your search

+10 -2
View File
@@ -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
+33 -3
View File
@@ -15,7 +15,8 @@
# check [--staged] [<memory-dir>] 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 [<memory-dir>] 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() { # <file> <ID> → rc 0 when a heading carries the ID
}
_mem_next_id() { # <file> <PREFIX> → 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() { # <rev:path> <out file> → rc 1 when absent, rc 2 when
git cat-file blob "$1" > "$2" 2>/dev/null || { : > "$2"; return 2; }
}
_mem_check_next_ids() { # <name> <PREFIX> <HEAD copy> <staged copy> → 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() { # <file> → 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() { # <file> → 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"
+87
View File
@@ -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