diff --git a/lib/memory-commit.sh b/lib/memory-commit.sh index 32c1845..0afdc13 100755 --- a/lib/memory-commit.sh +++ b/lib/memory-commit.sh @@ -49,6 +49,20 @@ _changed_paths() { done } +# Paths staged differently from HEAD AND from the worktree: `git add` below +# overwrites that staged blob, a refusal then resets it. One line per path, +# with the blob id, so the dropped partial stage can be restored by hand. +_partial_stages() { + local p mode oid + while IFS= read -r p; do + read -r mode oid _ < <(git ls-files -s -- "$p") + echo "memory-commit: partial stage dropped on: $p (blob $oid;" \ + "restore: git update-index --cacheinfo $mode,$oid,$p)" + done < <(comm -12 \ + <(git -c core.quotepath=off diff --cached --name-only -- "$@" | sort) \ + <(git -c core.quotepath=off diff --name-only -- "$@" | sort)) +} + # Surgical commit of the scoped paths only. Returns 0 (ok or no-op), 3 (unsafe). # On a real commit, prints the memory-commit short hash to stdout (stderr = diag). commit_memory() { @@ -61,7 +75,7 @@ commit_memory() { echo "memory-commit: detached HEAD or merge/rebase in progress — skip (no commit)" >&2 return 3 fi - local changed + local changed partial mapfile -t changed < <(_changed_paths) if [ "${#changed[@]}" -eq 0 ]; then echo "memory-commit: nothing pending — no-op" >&2 @@ -70,6 +84,7 @@ commit_memory() { # Re-stage working-tree content of the scoped paths over any stale index entry, # then commit ONLY those paths. The pathspec on `git commit` makes it a partial # commit: other staged files (dangling code) are not recorded. + partial="$(_partial_stages "${changed[@]}")" git add -- "${changed[@]}" # Chokepoint (BDR-125 phase 4a): a staged v2 registry may only grow and # never gains a new culprit (duplicate ID, bad heading, open fence). v1 @@ -78,6 +93,7 @@ commit_memory() { if [ -d .claude/memory ] \ && ! bash "$MC_MEMORY_SH" check --staged .claude/memory; then git reset -q -- "${changed[@]}" + [ -z "$partial" ] || printf '%s\n' "$partial" >&2 echo "memory-commit: REFUSED — memory.sh check --staged failed (above);" \ "NOTHING committed, nothing left staged." >&2 return 6 diff --git a/lib/memory.sh b/lib/memory.sh index ad7014c..edae22c 100755 --- a/lib/memory.sh +++ b/lib/memory.sh @@ -22,7 +22,8 @@ # open gets a `Status (…, upgrade)` line only when its v1 # line is a Status/Statut label holding one closed word; # a prose or mixed closure is named and refused (record -# it with `status`, re-run); the oracle (IDs, check, +# it with `status`, re-run); a `## Index` that is not a +# table is refused (rc 2); the oracle (IDs, check, # verdicts, one upgrade line per entry) runs first, else rc 2 # = decision | learning | blocker | eval. MEMORY_DATE overrides today. # Status of an entry: the last candidate wins. A candidate is the heading's @@ -31,11 +32,15 @@ # prose (title included) is never one (the BLK-032..034 false-open). A dated # `**Status (YYYY-MM-DD…)**:` line reads its leading word; other lines rank # words in _MEM_WORDS order, `open` last: "accepted, check OPEN" = accepted. +# A word negated in its clause ("not resolved yet", "never superseded") is +# not one: not|no|never|pas, at most one word before it, no `, ; . : ( ) ->` +# `→` `—` between (mem_has). # Closed = resolved | superseded | deprecated (memory_closed). # Lock: /memory.lock created under noclobber (flock(1) is # absent on macOS), holding " "; released only by its token; # a lock older than 30 s (or seen empty 10 times) is stolen by one waiter -# at a time (memory.lock.steal). Every writer runs on a verified backup +# at a time (memory.lock.steal; dead when it holds a date over 30 s old or +# has stayed empty over 60 s). Every writer runs on a verified backup # of its file, restored on failure or signal. Next ID = max over headings # of any level, fenced or not. Free text reaches awk through ENVIRON, # never `-v` (which expands backslash escapes). @@ -43,6 +48,7 @@ # absolute before any `-C` cd: a relative $0 no longer resolves after it _MEM_LIB_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)" MEM_GREP="${MEM_GREP:-/usr/bin/grep}" +MEM_FIND="${MEM_FIND:-/usr/bin/find}" _MEM_TABLE='decision decisions BDR learning learnings LRN blocker blockers BLK @@ -50,10 +56,22 @@ eval evals EVAL' _MEM_WORDS='resolved superseded deprecated upstream accepted implemented' _MEM_WORDS+=' shipped proposed keep correct deprecate open' _MEM_AWK=' +function mem_negated(pre, n, a) { + gsub(/[,;.:()]|->|→|—/, "\001", pre); sub(/^.*\001/, "", pre) + gsub(/[^a-z0-9]+/, " ", pre); n = split(pre, a, " ") + return (n >= 1 && a[n] ~ /^(not|no|never|pas)$/) || + (n >= 2 && a[n - 1] ~ /^(not|no|never|pas)$/) } +function mem_has(t, w, re, off, ws) { + re = "(^|[^a-z])" w "([^a-z]|$)"; off = 0 + while (match(substr(t, off + 1), re)) { + ws = off + RSTART + if (substr(t, ws, 1) != substr(w, 1, 1)) ws++ + if (!mem_negated(substr(t, 1, ws - 1))) return 1 + off = ws + length(w) - 1 } + return 0 } function mem_word(s, t, i, n, w) { t = tolower(s); n = split(MEMW, w, " ") - for (i = 1; i <= n; i++) - if (t ~ ("(^|[^a-z])" w[i] "([^a-z]|$)")) return w[i] + for (i = 1; i <= n; i++) if (mem_has(t, w[i])) return w[i] return "" } function mem_lead(s, v, i, n, w) { v = s; sub(/^[^:]*\*\*: */, "", v); v = tolower(v) @@ -65,8 +83,9 @@ function mem_lineword(s, w) { return mem_word(s) } function mem_tag(line) { if (!match(line, /\[[^]]*\]( \([0-9-]+\))? *$/)) return "" - if (mem_word(substr(line, RSTART)) == "") return "" - line = substr(line, RSTART); sub(/\].*$/, "]", line); return line } + line = substr(line, RSTART) # before mem_word: its match() moves RSTART + if (mem_word(line) == "") return "" + sub(/\].*$/, "]", line); return line } function mem_cand(line, lab) { if (line ~ /^- \*\*[^*]+\*\*/) { lab = line; sub(/^- \*\*/, "", lab); sub(/\*\*.*/, "", lab) @@ -198,7 +217,13 @@ _mem_stale() { # → rc 0 when stealable _mem_steal() { # → remove a stale lock; one stealer at a time local g="$1.steal" seen if ! (set -C; date +%s > "$g") 2>/dev/null; then - _mem_stale "$(cat "$g" 2>/dev/null) x" 0 && rm -f "$g" # dead stealer + # dead stealer: an old date, or a file left empty for over 60 s + # shortcut: check-then-rm window (a waiter may remove and a new stealer + # recreate between the read and the rm); upgrade = re-check inode/mtime + # just before the rm, content cannot tell two empty files apart + if _mem_stale "$(cat "$g" 2>/dev/null) x" 0 \ + || [ -n "$("$MEM_FIND" "$g" -prune -empty -mmin +1 2>/dev/null)" ] + then rm -f "$g"; fi return 0 fi seen=$(cat "$1" 2>/dev/null) @@ -338,6 +363,15 @@ memory_journal() { # → `- ` under today's heading || { echo "memory: journal line not written" >&2; return 2; } } +_mem_body_heading() { # → rc 2 on an unfenced `## ` line: it + local n # would split the entry (the checks read it as a heading) + n=$(awk '/^```/ { fence = !fence; next } + !fence && /^## / { print NR; exit }' "$1") + [ -z "$n" ] || { + echo "memory: add: body line $n starts with \"## \":" \ + "use \"###\" or indent it" >&2; return 2; } +} + _mem_add_valid() { # <body-file> <status> <supersedes> _mem_one_line title "$2" || return 2 case "$2$4" in *'['*|*']'*|*'|'*) @@ -349,6 +383,7 @@ _mem_add_valid() { # <PREFIX> <title> <body-file> <status> <supersedes> [ -s "$3" ] || { echo "memory: empty or missing body: $3" >&2; return 2; } ! _mem_fence_open "$3" \ || { echo "memory: body leaves a code fence open" >&2; return 2; } + _mem_body_heading "$3" || return 2 if [ "$1" = BLK ] && ! "$MEM_GREP" -q '`' "$3"; then echo "memory: a blocker body quotes ≥1 error in backticks" >&2; return 2 fi @@ -472,17 +507,22 @@ memory_check() { # [--staged] [<memory-dir>] → rc 2 on any culprit return "$rc" } -_mem_blob() { # <rev:path> <out file> → rc 1 when the blob is absent - git cat-file -e "$1" 2>/dev/null && git show "$1" > "$2" +_mem_blob() { # <rev:path> <out file> → rc 1 when absent, rc 2 when + # unreadable; the out file is empty on either + git cat-file -e "$1" 2>/dev/null || { : > "$2"; return 1; } + git cat-file blob "$1" > "$2" 2>/dev/null || { : > "$2"; return 2; } } _mem_check_staged() { # <file> → rc 2 on a new culprit or a lost v2 file - local h s rc=0 + local h s rc=0 hrc src h=$(mktemp) || { echo "memory: no temp file" >&2; return 2; } s=$(mktemp) || { rm -f "$h"; echo "memory: no temp file" >&2; return 2; } - _mem_blob "HEAD:./$1" "$h" || : > "$h" - _mem_blob ":./$1" "$s" || : > "$s" - if [ -s "$h" ] && memory_is_v2 "$h" && ! { [ -s "$s" ] && memory_is_v2 "$s"; } + _mem_blob "HEAD:./$1" "$h"; hrc=$? + _mem_blob ":./$1" "$s"; src=$? + if [ "$hrc" = 2 ] || [ "$src" = 2 ]; then + echo "memory: $1: cannot read the HEAD or the staged blob" >&2; rc=2 + elif [ -s "$h" ] && memory_is_v2 "$h" \ + && ! { [ -s "$s" ] && memory_is_v2 "$s"; } then echo "memory: $1: staged deletion, emptying or loss of format: 2" >&2 rc=2 @@ -660,12 +700,12 @@ _mem_verdict_diff() { # <old> <new> → "ID<TAB>word" per blocker whose open / _mem_v1_word() { # <v1 line> → the one status word of its status/statut # label (e.g. `status: resolved`), nothing for prose or a mixed label - L="$1" MEMW="$_MEM_WORDS" awk 'BEGIN { + L="$1" MEMW="$_MEM_WORDS" awk "$_MEM_AWK"'BEGIN { s = tolower(ENVIRON["L"]); n = split(ENVIRON["MEMW"], w, " ") if (!match(s, /(status|statut)[^a-z:]*:[* ]*/)) exit s = substr(s, RSTART + RLENGTH) for (i = 1; i <= n; i++) - if (s ~ ("(^|[^a-z])" w[i] "([^a-z]|$)")) { c++; word = w[i] } + if (mem_has(s, w[i])) { c++; word = w[i] } if (c == 1) print word }' } @@ -674,8 +714,8 @@ _mem_repair_one() { # <old> <new> <ID> → write the v1 closure when its line v1=$(memory_legacy_status "$1" "$3" '^##* *BLK-[0-9]') word=$(_mem_v1_word "$v1") if ! memory_closed "$word"; then - echo "memory: $3: v1 closed by an unlabelled or mixed line, status" \ - "unknown: ${v1:0:100}" >&2 + echo "memory: $3: v1 closed by an unlabelled, mixed or negated line," \ + "status unknown: ${v1:0:100}" >&2 echo "memory: $3: record it from the project root: memory.sh status" \ "$3 \"<word>, …\" (the leading word decides), then re-run upgrade" >&2 return 0 diff --git a/lib/tests/memory.test.sh b/lib/tests/memory.test.sh index d5b9d92..f4396c7 100755 --- a/lib/tests/memory.test.sh +++ b/lib/tests/memory.test.sh @@ -607,6 +607,77 @@ printf -- '---\ntype: blockers_registry\nentry_prefix: BLK\n---\n\n# B\n\n' \ bash "$MEM" upgrade t16/dt >/dev/null 2>&1 check T16-date-spacing "$(grep -c '^- \*\*Date\*\*' t16/dt/blockers.md)" 1 +# T17 parked minors: dead empty steal file, negated status, body heading, +# blob read errors +# an empty steal file (stealer killed before its date write) is dead after +# 60 s; a fresh one is a live stealer (over-reach guard: `-mmin +0` fails it) +echo $(( $(date +%s) - 60 )) dead.1 > "$lock" +: > "$lock.steal"; touch -t 202001010000 "$lock.steal" +out=$(bash "$MEM" add learning --title steal --body-file plain.md 2>&1); rc=$? +check T17-steal-empty-dead "$rc/$([ -e "$lock.steal" ] && echo kept || echo gone)/$([ -e "$lock" ] && echo held || echo free)" 0/gone/free +echo $(( $(date +%s) - 60 )) dead.1 > "$lock" +: > "$lock.steal" +sum=$(cksum < .claude/memory/learnings.md) +out=$(MEM_LOCK_TRIES=3 bash "$MEM" add learning --title steal \ + --body-file plain.md 2>&1); rc=$? +check T17-steal-empty-fresh "$rc/$([ -e "$lock.steal" ] && echo kept || echo gone)/$(printf '%s\n' "$out" | grep -c 'lock busy')/$(cksum < .claude/memory/learnings.md)" "2/kept/1/$sum" +rm -f "$lock" "$lock.steal" +# a status word negated in its clause is no status word +negword() { # <status text> → the entry's status word, empty when none + printf -- '---\nformat: 2\n---\n\n## BLK-001 — x\n- **Status**: %s\n' "$1" \ + > neg.md + word neg.md BLK-001; } +negcases() { # <label> → the negation cases under the current awk + check "$1-not-resolved" "$(negword 'not resolved yet')" '' + check "$1-open-then-not" "$(negword 'open, not resolved yet')" open + check "$1-never" "$(negword 'never superseded')" '' + check "$1-closed-not-open" "$(negword 'resolved, not open')" resolved + check "$1-no-comma" "$(negword 'no repro, resolved')" resolved + check "$1-not-yet" "$(negword 'not yet resolved')" '' + check "$1-arrow" "$(negword '(JS off -> no dialog), accepted')" accepted + check "$1-two-between" "$(negword 'not reproduced but resolved')" resolved + check "$1-arrow-bare" "$(negword 'not reproducible -> resolved')" resolved + check "$1-semicolon" "$(negword 'not; resolved')" resolved + check "$1-unicode-arrow" "$(negword 'not → resolved')" resolved + check "$1-em-dash" "$(negword 'never — superseded')" superseded + check "$1-plain" "$(negword 'resolved')" resolved; } +negcases T17-negated-status +if [ -x /usr/bin/mawk ]; then + mkdir -p t17/mawk && ln -sf /usr/bin/mawk t17/mawk/awk + OLDPATH=$PATH; PATH="$FIX/t17/mawk:$PATH" + negcases T17-negated-status-mawk + PATH=$OLDPATH +else echo "SKIP mawk absent"; fi +# a v1 line the lexical reader closes but only by a negated word is refused +v1blk d15/negated '## BLK-001 — x' 'Status: not resolved yet' +refused negated BLK-001 T17-upgrade-negated-refused +check T17-upgrade-negated-none "$(grep -c 'upgrade)' d15/negated/blockers.md)" 0 +# a body `## ` line would split the entry: refused; fenced it is text +printf 'first\n## Sub\nsecond\n' > sub.md +sum=$(cksum < .claude/memory/learnings.md) +err=$(bash "$MEM" add learning --title sub --body-file sub.md 2>&1); rc=$? +check T17-add-body-heading-refused "$rc/$(cksum < .claude/memory/learnings.md)/$(printf '%s\n' "$err" | grep -c 'body line 2 starts with "## "')" "2/$sum/1" +printf 'first\n```\n## Sub\n```\nsecond\n' > subf.md +out=$(bash "$MEM" add learning --title subf --body-file subf.md 2>&1); rc=$? +check T17-add-body-heading-fenced "$rc/$(grep -c '^## Sub$' .claude/memory/learnings.md)" 0/1 +# _mem_blob: absent = rc 1 and an empty out file; unreadable = rc 2 +echo stale > blob.out +(. "$MEM"; _mem_blob HEAD:./no/such blob.out); rc=$? +check T17-blob-absent "$rc/$(wc -c < blob.out | tr -d ' ')" 1/0 +mkdir -p t17/ro/.claude/memory t17/shim +reg decisions.md decisions_registry BDR 'Decisions registry (BDR)' +cat .claude/memory/decisions.md > t17/ro/.claude/memory/decisions.md +printf '#!/bin/sh\n[ "$1" = cat-file ] && [ "$2" = blob ] && exit 128\n' \ + > t17/shim/git +printf 'exec %s "$@"\n' "$(command -v git)" >> t17/shim/git +chmod +x t17/shim/git +(cd t17/ro && git init -q . && git config user.email t@t \ + && git config user.name t && git add -A && git commit -qm base) +(cd t17/ro && bash "$MEM" check --staged >/dev/null 2>&1) +check T17-blob-readable-baseline "$?" 0 +err=$(cd t17/ro && PATH="$FIX/t17/shim:$PATH" bash "$MEM" check --staged 2>&1) +check T17-blob-read-error "$?/$(printf '%s\n' "$err" | grep -c 'cannot read the HEAD or the staged blob')" 2/1 + # T12 this repo's registries pass the check check T12-live-check "$(bash "$MEM" -C "$ROOT" check >/dev/null 2>&1; echo $?)" 0 diff --git a/lib/tests/run-deterministic.sh b/lib/tests/run-deterministic.sh index 17bf27e..40c031e 100755 --- a/lib/tests/run-deterministic.sh +++ b/lib/tests/run-deterministic.sh @@ -171,6 +171,25 @@ printf '\n## BDR-002 — added (2026-10-27)\nbody\n' >> "$d/.claude/memory/decis out="$(cd "$d" && bash "$HELPER" commit "chore(memory): BDR-002" 2>/dev/null)"; rc=$? if [ "$rc" -eq 0 ] && [ -n "$out" ]; then ok "append to a v2 registry → committed"; else ko "append refused rc=$rc"; fi +echo "== rc 6 names the partial stage it drops" +d="$(new_repo)" +printf -- '---\nformat: 2\ntype: decisions_registry\nentry_prefix: BDR\n---\n\n## BDR-001 — kept (2026-10-27)\n- **Status**: accepted\n' \ + > "$d/.claude/memory/decisions.md" +git -C "$d" add -A && git -C "$d" commit -qm v2 +sed 's/kept/versionA/' "$d/.claude/memory/decisions.md" > "$d/x.tmp" +cat "$d/x.tmp" > "$d/.claude/memory/decisions.md" +git -C "$d" add .claude/memory/decisions.md +oid="$(git -C "$d" rev-parse :.claude/memory/decisions.md)" +sed 's/versionA/versionB/' "$d/x.tmp" > "$d/.claude/memory/decisions.md" +printf -- '- [ ] task\n' > "$d/.claude/tasks/TODO.md" +git -C "$d" add .claude/tasks/TODO.md +err="$(cd "$d" && bash "$HELPER" commit "chore(memory): partial" 2>&1 >/dev/null)"; rc=$? +staged="$(git -C "$d" diff --cached --name-only)" +want="memory-commit: partial stage dropped on: .claude/memory/decisions.md (blob $oid; restore: git update-index --cacheinfo 100644,$oid,.claude/memory/decisions.md)" +if [ "$rc" -eq 6 ] && [ -z "$staged" ] && [ "$(printf '%s\n' "$err" | grep -cxF "$want")" -eq 1 ]; then ok "rc 6 names a dropped partial stage (blob id), nothing left staged"; else ko "rc=$rc staged=[$staged] err=[$err]"; fi +if [ "$(printf '%s\n' "$err" | grep -c 'dropped on: .*TODO.md')" -eq 0 ]; then ok "a fully staged file is not listed"; else ko "fully staged TODO.md listed as a dropped partial stage"; fi +rm -rf "$d" + echo printf 'RESULT: %d passed, %d failed\n' "$PASS" "$FAIL" [ "$FAIL" -eq 0 ]