fix(memory): index fails closed on an unreadable registry

memory_index skipped a non-regular blockers.md and dropped the awk rc, so the digest printed a false '- open: none'. With a kind it now returns 2; without, it skips the bad registry with a stderr line. The digest prints unknown on a malformed row.
This commit is contained in:
bchanot committed 2026-11-15 19:35:15 +01:00
1 parent 2315a0b33a
commit 87aecc1212
5 files changed
+144 -13

No files matched your search

+2
View File
@@ -123,6 +123,8 @@ bash "$HOME/.claude/lib/memory.sh" -C "${ROOT:?}" index decision
Only BDRs whose current word is `accepted`, `implemented` or `proposed`
feed §6.1; `superseded` and `deprecated` ones are left out. A decision
whose word is empty is counted as unreadable, not dropped silently.
If `index decision` exits non-zero, write `MEMORY: decisions reader
unavailable` instead of an empty §6.1.
A `ROOT_UNSAFE` print (the first line of the root holds a control
character) or a pre-paste refusal (see above) → `STATUS: BLOCKED` naming
+2 -1
View File
@@ -336,7 +336,8 @@ _digest_blockers() { # <root> <memory.sh>
[[ "$row" == "| BLK-"* ]] || continue
line="${row#| }"; line="${line% |}"
rest="${line#* | }"; rest="${rest#* | }"
[[ "$rest" == *" | "* ]] || continue
[[ "$rest" == *" | "* ]] || {
echo "- open: unknown (memory.sh index failed)"; return 0; }
status="${rest##* | }"; title="${rest% | *}"
case "$status" in
open) open=$((open + 1))
+34 -7
View File
@@ -9,7 +9,9 @@
# status <ID> <text> append `- **Status (<date>)**: <text>` to the entry;
# <text> starts with a status word ("open, reopened by …")
# journal <line> append `- <line>` under today's `## YYYY-MM-DD`
# index [<kind>] ID | Date | Title | Status, from headings and bodies
# index [<kind>] ID | Date | Title | Status, from headings and bodies;
# with a kind, an unreadable registry is rc 2; without, a
# bad registry is skipped with a stderr line, rc 0
# 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
@@ -591,17 +593,42 @@ _mem_index_rows() { # <file> <PREFIX> → "| ID | date | title | word |" rows
END { row() }' "$1"
}
_mem_index_dir() { # → 1 (stderr) when .claude/memory cannot be searched
local d=.claude/memory
if [ -e "$d" ] || [ -L "$d" ]; then
{ [ -d "$d" ] && [ -x "$d" ]; } && return 0
else
[ ! -d .claude ] || [ -x .claude ] && return 0
fi
echo "memory: $d: not a searchable directory" >&2
return 1
}
_mem_index_one() { # <file> <PREFIX> → 0 read or absent, 1 bad (stderr)
local file=$1 rows
[ -e "$file" ] || [ -L "$file" ] || return 0
if [ ! -f "$file" ] || ! rows=$(_mem_index_rows "$file" "$2"); then
echo "memory: $file: not a readable regular file" >&2
return 1
fi
printf '%s\n\n| ID | Date | Title | Status |\n|---|---|---|---|\n' "$file"
[ -n "$rows" ] && printf '%s\n' "$rows"
echo
}
memory_index() { # [<kind>] → one table per registry
local kinds k spec file
local kinds k spec strict=${1:+1}
kinds=${1:-$(awk '{ print $1 }' <<< "$_MEM_TABLE")}
if ! _mem_index_dir; then
[ -n "$strict" ] && return 2
return 0
fi
for k in $kinds; do
spec=$(_mem_kind "$k") || return 2
file=".claude/memory/${spec% *}"
[ -f "$file" ] || continue
printf '%s\n\n| ID | Date | Title | Status |\n|---|---|---|---|\n' "$file"
_mem_index_rows "$file" "${spec#* }"
echo
_mem_index_one ".claude/memory/${spec% *}" "${spec#* }" && continue
[ -n "$strict" ] && return 2
done
return 0
}
_mem_index_table() { # <file> → "ID<TAB>date<TAB>last column" per Index row
+68 -2
View File
@@ -9,7 +9,8 @@ MEM="$ROOT/lib/memory.sh"
pass=0; fail=0
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
FIX=$(mktemp -d) || exit 1; trap 'rm -rf "$FIX"' EXIT
FIX=$(mktemp -d) || exit 1
trap 'chmod 700 "$FIX/ix20b/.claude/memory" 2>/dev/null; rm -rf "$FIX"' EXIT
cd "$FIX" || exit 1
git init -q . && git config user.email t@t && git config user.name t
export MEMORY_DATE=2026-10-27
@@ -37,6 +38,23 @@ check T1d-second-id "$(bash "$MEM" add blocker --title 'two' --body-file body.md
check T1e-check-green "$(bash "$MEM" check; echo $?)" 0
check T1f-index-row "$(bash "$MEM" index blocker | grep -c '^| BLK-001 | 2026-10-27 | make red | open |$')" 1
# T1g index healthy: rc 0 and a byte-identical full table (pre-fix capture)
want_ix=$(printf '%s\n' \
'.claude/memory/decisions.md' '' '| ID | Date | Title | Status |' \
'|---|---|---|---|' '' '.claude/memory/learnings.md' '' \
'| ID | Date | Title | Status |' '|---|---|---|---|' '' \
'.claude/memory/blockers.md' '' '| ID | Date | Title | Status |' \
'|---|---|---|---|' \
'| BLK-001 | 2026-10-27 | make red | open |' \
'| BLK-002 | 2026-10-27 | two | |' '' \
'.claude/memory/evals.md' '' '| ID | Date | Title | Status |' \
'|---|---|---|---|' '')
check T1g-index-bytes "$(bash "$MEM" index)" "$want_ix"
bash "$MEM" index blocker >/dev/null 2>&1
check T1h-index-kind-rc "$?" 0
bash "$MEM" index >/dev/null 2>&1
check T1i-index-all-rc "$?" 0
# T2 add refuses, rc 2, registry unchanged
sum=$(cksum < "$B")
refused() { # <name> <add args…> → the add is rc 2
@@ -758,7 +776,9 @@ done
# commit the stub. Installed here so earlier cases see the repo unceilinged.
# shellcheck source=lib/tests/claude-stub.sh
. "$ROOT/lib/tests/claude-stub.sh" || exit 1
CSW=$(mktemp -d) || exit 1; trap 'rm -rf "$FIX" "$CSW"' EXIT
CSW=$(mktemp -d) || exit 1
trap 'chmod 700 "$FIX/ix20b/.claude/memory" 2>/dev/null
rm -rf "$FIX" "$CSW"' EXIT
claude_stub_install "$CSW"
banner() { (cd "$1" && SESSION_START_OFFLINE=1 bash "$ROOT/hooks/session-start.sh" \
</dev/null 2>/dev/null) | grep -c "${2:-registry v1}"; }
@@ -795,6 +815,52 @@ printf -- '---\nformat: 2\ntype: decisions_registry\nentry_prefix: BDR\n---\n\n#
err=$(bash "$MEM" check 'a=b' 2>&1 >/dev/null </dev/null)
check T19c-assign-dir-read "$(printf '%s\n' "$err" | grep -c '^memory: ./a=b/decisions.md:9: heading shape: ## BDR-001 - bad$')" 1
# T20 index fails closed (BDR-125 A3): own subtree, shared $FIX untouched
cd "$FIX" || exit 1
mkdir -p ix20/.claude/memory
reg20() { # <file> <type> <PREFIX> <entry heading>
printf -- '---\nformat: 2\ntype: %s\nentry_prefix: %s\n---\n\n%s\n' \
"$2" "$3" "$4" > "ix20/.claude/memory/$1"; }
reg20 learnings.md learnings_registry LRN '## LRN-001 — kept (2026-10-27)'
mkdir ix20/.claude/memory/blockers.md
err=$(bash "$MEM" -C ix20 index blocker 2>&1 >/dev/null); rc=$?
check T20-dir-strict-rc "$rc" 2
check T20-dir-strict-names "$(printf '%s\n' "$err" | grep -c 'blockers.md')" 1
out=$(bash "$MEM" -C ix20 index 2>ix20/err); rc=$?
check T20b-dir-lax-rc "$rc" 0
check T20b-dir-lax-warns "$(grep -c 'blockers.md' ix20/err)" 1
check T20b-dir-lax-keeps-lrn "$(printf '%s\n' "$out" | grep -c '^| LRN-001 |')" 1
rmdir ix20/.claude/memory/blockers.md
mkfifo ix20/.claude/memory/blockers.md
timeout 5 bash "$MEM" -C ix20 index blocker >/dev/null 2>&1 </dev/null
check T20c-fifo-strict-rc "$?" 2
rm ix20/.claude/memory/blockers.md
ln -s "$FIX/nowhere" ix20/.claude/memory/blockers.md
bash "$MEM" -C ix20 index blocker >/dev/null 2>&1
check T20d-dangling-strict-rc "$?" 2
rm ix20/.claude/memory/blockers.md
reg20 blockers.md blockers_registry BLK '## BLK-001 — mode (2026-10-27) [open]'
chmod 000 ix20/.claude/memory/blockers.md
if [ -r ix20/.claude/memory/blockers.md ]; then
echo "SKIP T20e-unreadable (chmod 000 ineffective for this user)"
else
out=$(bash "$MEM" -C ix20 index blocker 2>/dev/null); rc=$?
check T20e-unreadable-strict-rc "$rc" 2
check T20e-no-partial-table "$(printf '%s' "$out" | grep -c .)" 0
fi
chmod 600 ix20/.claude/memory/blockers.md
mkdir -p ix20b/.claude/memory
chmod 600 ix20b/.claude/memory
if [ -x ix20b/.claude/memory ]; then
echo "SKIP T20f-unsearchable (chmod 600 ineffective for this user)"
else
bash "$MEM" -C ix20b index blocker >/dev/null 2>&1
check T20f-unsearchable-strict-rc "$?" 2
bash "$MEM" -C ix20b index >/dev/null 2>&1
check T20f-unsearchable-lax-rc "$?" 0
fi
chmod 700 ix20b/.claude/memory
check no-claude-calls "$(claude_stub_calls)" 0
echo "PASS=$pass FAIL=$fail"
[ "$fail" -eq 0 ]
+38 -3
View File
@@ -4,7 +4,8 @@
set -u
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
SS="$ROOT/hooks/session-start.sh"
WORK="$(mktemp -d)" || exit 1; trap 'rm -rf "$WORK"' EXIT
WORK="$(mktemp -d)" || exit 1; SD9H_DIR=
trap '[ -z "$SD9H_DIR" ] || chmod 700 "$SD9H_DIR"; rm -rf "$WORK"' EXIT
export SESSION_START_OFFLINE=1
pass=0; fail=0
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
@@ -205,12 +206,46 @@ printf '#!/usr/bin/env bash\nprintf "| BLK-1 |\\n| BLK-2 | x\\n| BLK-3 | d | t |
out=$(run "$p" - "$S9"); rc=$?
check SD9b-malformed-rc "$rc" 0
check SD9b-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1
check SD9b-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 1
check SD9b-unknown "$(printf '%s\n' "$out" | grep -c -x -- '- open: unknown (memory.sh index failed)')" 1
check SD9b-not-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 0
p=$(mkproj sd9d); mkdir "$p/.claude/memory/blockers.md"
out=$(run "$p" -); rc=$?
check SD9c-dir-rc "$rc" 0
check SD9c-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1
check SD9c-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 1
check SD9c-unknown "$(printf '%s\n' "$out" | grep -c -x -- '- open: unknown (memory.sh index failed)')" 1
check SD9c-not-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 0
# SD9f: a dangling-symlink blockers.md fails closed
p=$(mkproj sd9f); ln -s "$WORK/nowhere" "$p/.claude/memory/blockers.md"
out=$(run "$p" -); rc=$?
check SD9f-rc "$rc" 0
check SD9f-unknown "$(printf '%s\n' "$out" | grep -c -x -- '- open: unknown (memory.sh index failed)')" 1
check SD9f-not-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 0
# SD9g: an unreadable (mode 000) blockers.md fails closed
p=$(mkproj sd9g); blk "$p" 1 open "Hidden"
chmod 000 "$p/.claude/memory/blockers.md"
if [ -r "$p/.claude/memory/blockers.md" ]; then
echo "SKIP SD9g-unreadable (chmod 000 ineffective for this user)"
else
out=$(run "$p" -)
check SD9g-unknown "$(printf '%s\n' "$out" | grep -c -x -- '- open: unknown (memory.sh index failed)')" 1
check SD9g-not-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 0
fi
# SD9h: a non-searchable .claude/memory fails closed, mode restored after
p=$(mkproj sd9h); blk "$p" 1 open "Hidden"
SD9H_DIR="$p/.claude/memory"
chmod 600 "$SD9H_DIR"
if [ -x "$SD9H_DIR" ]; then
echo "SKIP SD9h-unsearchable (chmod 600 ineffective for this user)"
else
out=$(run "$p" -)
chmod 700 "$SD9H_DIR" || true
check SD9h-unknown "$(printf '%s\n' "$out" | grep -c -x -- '- open: unknown (memory.sh index failed)')" 1
check SD9h-not-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 0
fi
chmod 700 "$SD9H_DIR" || true
# SD9d: a successful read with zero open rows says so
p=$(mkproj sd9e); blk "$p" 1 resolved "Done"