From aab9e4d9a0f5659ffb46eba91e6c86e295dd47d6 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 28 Oct 2026 18:28:54 +0100 Subject: [PATCH] fix(memory): bounded lock loop, entry-ID validation, refuse a non-table Index, check refuses nothing-to-check, Date label spacing --- lib/memory.sh | 26 +++++++++++++++++++++++-- lib/tests/memory.test.sh | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/lib/memory.sh b/lib/memory.sh index 8940506..d0024c2 100755 --- a/lib/memory.sh +++ b/lib/memory.sh @@ -105,6 +105,8 @@ _mem_kind() { # → ".md ", rc 2 when unknown _mem_id_file() { # → its registry path, rc 2 when the prefix is unknown local file + [[ "$1" =~ ^[A-Z]+-[0-9][0-9][0-9]+$ ]] \ + || { echo "memory: not an entry ID: $1" >&2; return 2; } file=$(_mem_table 2 3 "${1%%-[0-9]*}") \ || { echo "memory: not an entry ID: $1" >&2; return 2; } echo ".claude/memory/$file.md" @@ -205,10 +207,12 @@ _mem_lock() { # → rc 0 held, rc 2 after MEM_LOCK_TRIES # noclobber = open(O_EXCL) by bash itself: mkdir is not atomic under # uutils coreutils 0.10 (two concurrent mkdir both succeed) local lock="$1" tries=0 empty=0 t + [ -w "$(dirname "$lock")" ] \ + || { echo "memory: cannot create lock: $lock" >&2; return 2; } until (set -C; echo "$(date +%s) $2" > "$lock") 2>/dev/null; do t=$(cat "$lock" 2>/dev/null) if [ -z "$t" ]; then empty=$((empty + 1)); else empty=0; fi - if _mem_stale "$t" "$empty"; then _mem_steal "$lock"; continue; fi + if _mem_stale "$t" "$empty"; then _mem_steal "$lock"; fi tries=$((tries + 1)) [ "$tries" -lt "${MEM_LOCK_TRIES:-50}" ] \ || { echo "memory: lock busy: $lock" >&2; return 2; } @@ -447,6 +451,10 @@ memory_check() { # [--staged] [] → rc 2 on any culprit local staged="" dir f rc=0 [ "${1:-}" = --staged ] && { staged=1; shift; } dir="${1:-.claude/memory}" + [ $# -le 1 ] && [ "${dir#-}" = "$dir" ] || { echo "usage: memory.sh" \ + "check [--staged] [dir]" >&2; return 2; } + [ -n "$staged" ] || [ -d "$dir" ] \ + || { echo "memory: no memory dir: $dir" >&2; return 2; } case "$staged:$dir" in 1:/*|1:..|1:../*|1:*/../*|1:*/..) echo "memory: check --staged takes a repo-relative dir: $dir" >&2 return 2 ;; @@ -517,7 +525,7 @@ _mem_index_rows() { # → "| ID | date | title | word |" rows sub(/^ — /, "", d) } sub(/ \[[^]]*\] *$/, "", t); gsub(/\|/, "/", t); next } id == "" { next } - d == "" && sub(/^- \*\*Date\*\*: */, "") { d = substr($0, 1, 10) } + d == "" && sub(/^- \*\*Date\*\* *: */, "") { d = substr($0, 1, 10) } (w = mem_cand($0)) != "" { s = w } END { row() }' "$1" } @@ -721,8 +729,22 @@ _mem_upgrade_build() { # → the v2 text, blocker return "$rc" } +_mem_index_only_table() { # → rc 1 when ## Index holds a non-table + awk '$0 == "## Index" { on = 1; next } # line outside a comment + !on { next } + cm { if (/-->/) cm = 0; next } + /^/) cm = 1; next } + /^\|/ { tbl = 1; next } + /^#/ || /^---$/ || (tbl && /^$/) { exit } + /^$/ { next } + { bad = 1; exit } + END { exit bad }' "$1" +} + _mem_upgrade_ready() { # → rc 0 upgradable, 1 already v2, 2 refused memory_is_v2 "$1" && { echo "memory: $1 already v2" >&2; return 1; } + _mem_index_only_table "$1" || { echo "memory: $1: ## Index is not a" \ + "table, not upgraded" >&2; return 2; } _mem_prefix "$1" > /dev/null } diff --git a/lib/tests/memory.test.sh b/lib/tests/memory.test.sh index 81bd378..08b62af 100755 --- a/lib/tests/memory.test.sh +++ b/lib/tests/memory.test.sh @@ -502,6 +502,47 @@ check T15-tamper-rc "$?" 2 check T15-tamper-culprit "$(printf '%s\n' "$err" | grep -c 'moved a verdict: BLK-001')" 1 check T15-tamper-untouched "$(cksum < d15/tamper/blockers.md)" "$sum" +# T16 final-review fixes +# lock dir not writable: bounded, named, rc 2 (never a spin) +chmod a-w .git +out=$(timeout 10 bash "$MEM" journal x 2>&1); rc=$? +chmod u+w .git +check T16-lock-rc "$rc" 2 +check T16-lock-named "$(printf '%s\n' "$out" | grep -c 'memory: cannot create lock: ')" 1 +# a bare prefix is not an entry ID +sum=$(cksum < "$B") +bash "$MEM" status BLK "resolved abc" >/dev/null 2>&1 +check T16-bare-prefix-rc "$?" 2 +check T16-bare-prefix-untouched "$(cksum < "$B")" "$sum" +# a bullet Index is refused, byte-identical +mkdir -p t16/bul +printf -- '---\ntype: blockers_registry\nentry_prefix: BLK\n---\n\n# B\n\n' \ + > t16/bul/blockers.md +printf '## Index\n\n- BLK-001 — x (resolved 2026-08-03)\n\n---\n\n' \ + >> t16/bul/blockers.md +printf '## BLK-001 — x\n- **Status**: resolved\n' >> t16/bul/blockers.md +sum=$(cksum < t16/bul/blockers.md) +err=$(bash "$MEM" upgrade t16/bul 2>&1 >/dev/null); rc=$? +check T16-bullet-index-rc "$rc" 2 +check T16-bullet-index-named "$(printf '%s\n' "$err" | grep -c 'blockers.md: ## Index is not a table, not upgraded')" 1 +check T16-bullet-index-untouched "$(cksum < t16/bul/blockers.md)" "$sum" +# check refuses nothing-to-check +bash "$MEM" check --stagd >/dev/null 2>&1 +check T16-check-flag "$?" 2 +err=$(bash "$MEM" check /no/such/dir 2>&1); rc=$? +check T16-check-nodir-rc "$rc" 2 +check T16-check-nodir-named "$(printf '%s\n' "$err" | grep -c 'memory: no memory dir: /no/such/dir')" 1 +# a `- **Date** :` body gets no second Date line +mkdir -p t16/dt +printf -- '---\ntype: blockers_registry\nentry_prefix: BLK\n---\n\n# B\n\n' \ + > t16/dt/blockers.md +{ printf '## Index\n\n| ID | Date | Title | Status |\n|---|---|---|---|\n' + printf '| BLK-001 | 2026-05-01 | x | open |\n\n---\n\n' + printf '## BLK-001 — x\n- **Date** : 2026-05-01\n- **Status**: open\n' +} >> t16/dt/blockers.md +bash "$MEM" upgrade t16/dt >/dev/null 2>&1 +check T16-date-spacing "$(grep -c '^- \*\*Date\*\*' t16/dt/blockers.md)" 1 + # T12 this repo's registries pass the check check T12-live-check "$(bash "$MEM" -C "$ROOT" check >/dev/null 2>&1; echo $?)" 0