docs(plan): memory v2 write side — 7 tasks + proven reference lib, suite, fixture

This commit is contained in:
bchanot committed 2026-10-28 10:36:29 +01:00
1 parent cdc0a4778b
commit 90d5f96c95
4 files changed
+1325

No files matched your search

@@ -0,0 +1,49 @@
---
format: 2
type: blockers_registry
entry_prefix: BLK
---
# Status parser fixture: BLK-032..034 as written 2026-10-27 (false-open
# before phase 4a), BLK-008 compound (frozen snapshot), BLK-040..042 shapes.
## BLK-008 — gstack ./setup fails on Ubuntu 26.04 — Playwright chromium unsupported
- **Date**: 2026-06-23
- **Friction**: fresh Ubuntu 26.04, `make install` / `make plugin` → "Failed to install browsers / ERROR: Playwright does not support chromium on ubuntu26.04-x64" → "GStack ./setup failed". Non-fatal in our wrapper (warn only) but gstack's browser (`/browse`, `/qa`, design screenshots) is silently dead once gstack is enabled.
- **Real cause**: Playwright 1.58.2 (pinned in the gstack submodule) registry lists `ubuntu20.04/22.04/24.04` only; 26.04 released later → not in list → `getHostPlatform` errors. Pure OS-newness, not an install bug.
- **Solution**: gated `export PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=ubuntu24.04-x64` (ubuntu >24.04 only) before gstack setup + persisted to `.bashrc` for runtime. Playwright then pulls a Chrome-for-Testing fallback build for ubuntu24.04. Verified on 26.04: `ldd` resolves all libs + real headless render OK.
- **Status**: resolved (commit 211c7d4). Residual: exact rev 1208 launch not in-session-tested (sandbox download hung at extraction); proved via sibling rev 1228 same-platform CfT build. Confirm on next real `make plugin`. Proper upstream fix = gstack bumps Playwright to a version that lists ubuntu26.04. See [[LRN-038]].
- **2026-06-23 UPDATE — Solution REVERTED, status downgraded to UPSTREAM/open** (commit b9c3937): the `PLAYWRIGHT_HOST_PLATFORM_OVERRIDE` solution above does NOT work on 26.04. The fallback build downloads to 100% then HANGS at extraction (chrome binary never appears, no headless-shell download starts; reproduced on real machine + sandbox) → turned a 0.5s fast-fail into an install-blocking hang (user Ctrl+C). Reverted to the fast-fail (non-fatal; gstack OFF by default, browser only for /browse,/qa,screenshots). The earlier "verified ldd + headless render" was an isolated test on a sibling already-extracted build (rev 1228) — it masked the rev-1208 install-path hang. **Real fix = upstream**: gstack bumps Playwright to a version that lists ubuntu26.04. Until then gstack's browser is unavailable on 26.04, install completes cleanly. See [[LRN-038]] correction.
- **2026-06-23 FINAL — RESOLVED** (commit 3b8ffb1): gstack browser now works on Ubuntu 26.04. Two layers fixed: (1) bumped gstack's pinned Playwright 1.58.2 → 1.61 (`bun add playwright@latest` in the submodule; 1.61 ships a native ubuntu26.04 build — chromium rev 1228), automated in the installer (`gstack_bump_playwright_if_unsupported`, idempotent, OS-gated); (2) `GSTACK_CHROMIUM_NO_SANDBOX=1` to work around the AppArmor userns restriction (`sysctl kernel.apparmor_restrict_unprivileged_userns=1`), persisted to `.bashrc` + installer Step 9 (sysctl-gated). Verified end-to-end: `browse goto https://example.com` → "Navigated (200)". Caveat: the Playwright bump is a local submodule edit, reset by `git submodule update`, re-applied by the next install. See [[BDR-029]], [[LRN-040]].
---
## BLK-032 — Main loop pinned on Fable in long sessions: session model Fable 1M + downgrade cap 120k + prompt rule `plan` → inline code on Fable xhigh [resolved]
- **Cause**: `~/.claude/settings.json` `"model": "claude-fable-5-1[1m]"` (every session starts on Fable); model-router never moves main above the session model, moves DOWN only under `downgradeMaxTokens` 120000 (LRN-204 cold read) → a long session (faunosteo R4.x, context > 120k) never reaches `implement`/sonnet; prompt rule `(plan|design|architecture…)` → phase plan = best/xhigh each turn ("plan §B.1.7"). No doctrine ceiling: CLAUDE.md allows inline work "doable in a few tool calls", BDR-115 "pin = default, never ceiling".
- **User policy 2026-10-19**: session model Opus 5.5; sonnet writes, opus decides what to write; Fable only for orchestrator reflection/plans and the important audits (security, functional), other judges opus. → next `/feat`: settings model, `judge` split, ceiling rule in CLAUDE.md, BDR superseding part of BDR-066 / BDR-115. Links [[BDR-066]], [[BDR-107]], [[BDR-115]], [[LRN-204]].
- **Solution (2026-10-23)**: feat A+B (BDR-123) + feat C 786ee13 (feature/router-keyword-cap): keyword prompt rules, `ultrathink` floor, derived `orchestrate` dispatch write effort-only routes (`effortRoute`, `belowRun` pattern) → never lift main above session model; held best-tier run keeps its model (per-axis fall-through); `mainModelSlot` drops `turnFloor`. Fable on main only via skill row, `route` tool, `/route`. Docs d2d590f. Links [[LRN-226]], [[EVAL-048]].
## BLK-033 — Guardrails residuals: probe trusted plan `supabase.url`, redirects free of budget, unread bodies read CLOSED; rules 5 FP / 7 misses [resolved]
- **Friction**: GATE 2 of the a11y split (f586304) left a MEDIUM + 3 LOW on probe.py, opus review left Minors on the rules. Parked in TODO, fixed via `/bugfix` 2026-10-20/21.
- **Cause**: `supabase.url` read from the plan w/o `_http_url` → planted SUPABASE_URL would receive GUARDRAILS_ANON_KEY / JWT2, `file://` read CLOSED; `_SameHost` redirector followed hops w/o charging `session.budget`; `_rows_state` collapsed non-200 / empty / >1 MB / non-JSON into CLOSED (unobserved = CLOSED, breaks LRN-220); plan r2 mapped budget exhaustion on `str(URLError)`, which wraps the reason → never matched. Rules: SENSITIVE_KEY on metadata names (`token_type`, `has_password`), 403 on first map candidate stopped the walk, hydrated scan read whole route file, NOSQL `$where` fired on GraphQL `_bool_exp`; misses: Go backtick Sprintf, `rf`/triple-quoted f-strings, `sql|db|trx.raw`, empty `.select()`, multi-line hidden input, Rust `{}` shell arg, `"sources"` map head.
- **Solution**: 5a98c2c (`_BudgetExhausted(URLError)` + `BUDGET_EXHAUSTED` constant, `_SameHost(session)` charges per hop, `_rows_state` rows|empty|unreadable → SKIPPED, `_map_outcome` full candidate walk, `data:` OPEN w/o decoding, `printable` on remote strings, `_safe_text` in scanner, rule fixes + mutant per language variant) + 633faa2 (checks.md: `IN (…)` placeholder list flagged, judged by hand). Plan r3 after 2 challenge rounds, contract 22 oracles MET, verifier CONFORME, auditor PASS. Suites probe 84, scan 241.
- **Residual** (LOW, parked in TODO): table names not `IDENT.fullmatch`-checked per table (`/` → path walk on the approved host); `printable` lets Unicode bidi/format controls through. Links [[LRN-219]], [[LRN-220]], [[LRN-222]], [[LRN-223]].
## BLK-034 — mods.test.sh red on CLI 2.1.306: whole-output `warning` grep caught the advisory plugin-directory section [resolved]
- **Friction**: `make test suite=lib/tests/mods.test.sh` → `FAIL model-router: validate printed a warning`, pre-existing on develop (git archive baseline), surfaced as feat B criterion 8 (NEED-DECISION, BDR-123 run).
- **Cause**: CLI 2.1.306 `claude plugin validate` prints "Plugin directory checks" section (submission catalogue): `⚠ Found 1 plugin directory warning:` ICON_MISSING, `--json` `directory.findings[].blocking: false`. mods.test.sh:57 grepped whole output for `warning` (also matched hooks notes quoting register.ts identifiers, checkout paths). Prose scraping of a CLI with an exit-code contract: `--strict` → per-file warnings rc 1, directory findings rc only under `--profile directory`.
- **Solution**: 801992a (bugfix/mods-validate-advisory). Helper `validate_strict` = rc of `claude plugin validate --strict`; check_cli + positive control share it; control = scratch manifests `{"name":"warnfix"}` (rc 1) / cleanfix with version+description+author (rc 0) under script-level `mktemp -d`. Plan r3 after 3 lenses + 1 re-challenge (sed range on closing marker killed: CLI prints it only when findings > 0, fail-open to EOF). GATE 0 7/7, verifier ECARTS(2) = out-of-scope routing.json dialog write only, auditor PASS.
- **Residual**: 3 historical contracts carry old scrape as CHECK oracle (`.claude/tasks/contracts/2026-10-08-model-router-w1a-1533.md:22`, `2026-10-08-model-router-floor-1835.md:22`, `2026-10-09-model-router-tiers-1237.md:23`), left unedited; re-run fails on ICON_MISSING, not a model-router regression. Unverified: whether `--strict` escalates `advice[]`. Links [[LRN-225]], [[BDR-123]].
## BLK-040 — accepted with an open check
- **Status**: accepted. Behavioral check OPEN: `x` must ask first.
## BLK-041 — "Use when [triggers]" mid-title bracket
- **Status**: open. `y` pending.
## BLK-042 — no status anywhere
- **Solution**: fail-open guard, `data:` OPEN in prose.
@@ -0,0 +1,478 @@
#!/usr/bin/env bash
# lib/memory.sh — the single writer and reader of the .claude/memory
# registries (BDR-125 phase 4a). Sourceable; every verb takes an optional
# leading `-C <dir>` (a dispatched agent's cwd is the session's, never the
# project's):
# add <kind> --title <t> --body-file <f> [--status <s>] [--supersedes <ID>]
# append one entry, next ID from the headings, under the
# lock; the new ID is the only stdout line
# status <ID> <text> append `- **Status (<date>)**: <text>` to the entry
# journal <line> append `- <line>` under today's `## YYYY-MM-DD`
# index [<kind>] ID | Date | Title | Status, from headings and bodies
# check [--staged] [<memory-dir>] heading shape, unique IDs, no Index in
# a v2 file (v1 reported, never failed); --staged: no line
# removed inside an existing entry of a staged v2 registry
# upgrade [<memory-dir>] v1 → v2: Index-only dates and statuses become
# body lines, `format: 2`, `## Index` removed, oracle first
# <kind> = decision | learning | blocker | eval. MEMORY_DATE overrides today.
# Status of an entry: the last candidate wins. A candidate is the heading's
# trailing `[tag]`, or a body line whose bold label names Status, Resolution,
# Action, UPDATE or FINAL (v1: `Status:`), holding a status word. A word in
# prose is never one (the BLK-032..034 false-open). Words rank in
# _MEM_WORDS order, `open` last: "accepted, check OPEN" reads accepted.
# Lock: <git-common-dir>/memory.lock created under noclobber (flock(1) is
# absent on macOS), a lock older than 30 s is stolen. Registry headings
# are scanned outside ``` fences only.
MEM_GREP="${MEM_GREP:-/usr/bin/grep}"
_MEM_WORDS='resolved superseded deprecated upstream accepted implemented'
_MEM_WORDS+=' shipped proposed keep correct deprecate open'
_MEM_AWK='
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]
return "" }
function mem_tag(line) {
if (!match(line, /\[[^]]*\]( \([0-9-]+\))? *$/)) return ""
return mem_word(substr(line, RSTART)) }
function mem_cand(line, lab) {
if (line ~ /^- \*\*[^*]+\*\*/) {
lab = line; sub(/^- \*\*/, "", lab); sub(/\*\*.*/, "", lab)
return tolower(lab) ~ /status|resolution|action|update|final/ \
&& mem_word(line) != "" }
return line ~ /^(- )?[Ss]tatus:/ && mem_word(line) != "" }
function mem_key(line, key) {
return line == "## " key || index(line, "## " key " — ") == 1 }
'
_mem_date() { printf '%s\n' "${MEMORY_DATE:-$(date +%F)}"; }
_mem_kind() { # <kind> → "<file> <PREFIX>", rc 2 when unknown
case "$1" in
decision) echo "decisions.md BDR" ;;
learning) echo "learnings.md LRN" ;;
blocker) echo "blockers.md BLK" ;;
eval) echo "evals.md EVAL" ;;
*) echo "memory: unknown kind: $1 (decision|learning|blocker|eval)" >&2
return 2 ;;
esac
}
_mem_id_file() { # <ID> → its registry path, rc 2 when the prefix is unknown
case "$1" in
BDR-[0-9]*) echo .claude/memory/decisions.md ;;
LRN-[0-9]*) echo .claude/memory/learnings.md ;;
BLK-[0-9]*) echo .claude/memory/blockers.md ;;
EVAL-[0-9]*) echo .claude/memory/evals.md ;;
*) echo "memory: not an entry ID: $1" >&2; return 2 ;;
esac
}
_mem_is_v2() { # <file> → rc 0 when the frontmatter carries `format: 2`
awk 'NR == 1 && $0 != "---" { exit }
NR > 1 && $0 == "---" { exit }
$0 == "format: 2" { v2 = 1; exit }
END { exit !v2 }' "$1"
}
_mem_has_id() { # <file> <ID> → rc 0 when a heading carries the ID
awk -v k="$2" "$_MEM_AWK"'
/^```/ { fence = !fence; next }
!fence && mem_key($0, k) { found = 1; exit }
END { exit !found }' "$1"
}
_mem_next_id() { # <file> <PREFIX> → max heading ID + 1, zero-padded
awk -v p="$2" '
/^```/ { fence = !fence; next }
!fence && match($0, "^## " p "-[0-9]+") {
n = substr($0, length(p) + 5, RLENGTH - length(p) - 4) + 0
if (n > m) m = n }
END { printf "%s-%03d\n", p, m + 1 }' "$1"
}
memory_entry_status() { # <file> <ID> → winning candidate line, rc 1 if none
awk -v k="$2" -v MEMW="$_MEM_WORDS" "$_MEM_AWK"'
/^```/ { fence = !fence; next }
fence { next }
/^## / { if (on) exit
if (mem_key($0, k)) { on = 1; if (mem_tag($0) != "") last = $0 }
next }
on && mem_cand($0) { last = $0 }
END { if (last == "") exit 1; print last }' "$1"
}
memory_status_word() { # <line> → its status word, else its first 40 chars
awk -v MEMW="$_MEM_WORDS" "$_MEM_AWK"'
{ w = mem_word($0); print (w != "" ? w : substr($0, 1, 40)) }' <<< "$1"
}
_mem_lock_dir() { # → the lock path for the current repository
local d
if d=$(git rev-parse --git-common-dir 2>/dev/null); then
echo "$d/memory.lock"; return
fi
echo "${TMPDIR:-/tmp}/memory-$(pwd | cksum | cut -d' ' -f1).lock"
}
_mem_lock() { # <lock file> → rc 0 held, rc 2 after MEM_LOCK_TRIES waits
# 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 now t
until (set -C; date +%s > "$lock") 2>/dev/null; do
now=$(date +%s); t=$(cat "$lock" 2>/dev/null)
case "$t" in ''|*[!0-9]*) t=$now ;; esac # holder still writing it
if [ $((now - t)) -gt 30 ]; then rm -f "$lock"; continue; fi
tries=$((tries + 1))
[ "$tries" -lt "${MEM_LOCK_TRIES:-50}" ] \
|| { echo "memory: lock busy: $lock" >&2; return 2; }
sleep 0.2
done
}
_mem_unlock() { rm -f "$1"; }
_mem_locked() { # <fn> [args…] → run fn under the lock, its rc
local lock rc
lock=$(_mem_lock_dir)
_mem_lock "$lock" || return 2
"$@"; rc=$?
_mem_unlock "$lock"
return "$rc"
}
_mem_one_line() { # <what> <text> → rc 2 when empty or multi-line
case "$2" in
''|*$'\n'*) echo "memory: $1 must be one non-empty line" >&2; return 2 ;;
esac
}
_mem_insert() { # <file> <key> <line> → line ends the section, rc 1 if absent
local tmp
tmp=$(mktemp) || { echo "memory: no temp file" >&2; return 2; }
awk -v k="$2" -v ins="$3" "$_MEM_AWK"'
function flush( i) { print ins; for (i = 1; i <= n; i++) print buf[i]
n = 0; done = 1 }
/^```/ { fence = !fence }
on && !fence && /^## / { flush(); on = 0 }
!done && !on && !fence && mem_key($0, k) { on = 1; print; next }
on { if ($0 == "" || $0 == "---") { buf[++n] = $0; next }
for (i = 1; i <= n; i++) print buf[i]; n = 0; print; next }
{ print }
END { if (on) flush(); exit !done }' "$1" > "$tmp"
case $? in
0) cat "$tmp" > "$1" || { rm -f "$tmp"; return 2; } ;;
*) rm -f "$tmp"; return 1 ;;
esac
rm -f "$tmp"
}
memory_status() { # <ID> <text>
local file
_mem_one_line "status text" "$2" || return 2
file=$(_mem_id_file "$1") || return 2
_mem_has_id "$file" "$1" \
|| { echo "memory: no entry $1 in $file" >&2; return 2; }
_mem_locked _mem_insert "$file" "$1" "- **Status ($(_mem_date))**: $2" \
|| { echo "memory: $1 status not written" >&2; return 2; }
}
_mem_journal_locked() { # <file> <date> <line>
_mem_insert "$1" "$2" "$3"
case $? in
1) printf '\n## %s\n%s\n' "$2" "$3" >> "$1" ;;
0) return 0 ;;
*) return 2 ;;
esac
}
memory_journal() { # <line> → `- <line>` under today's heading
local file=.claude/memory/journal.md line="$1"
_mem_one_line "journal line" "$line" || return 2
[ -f "$file" ] || { echo "memory: no $file" >&2; return 2; }
case "$line" in '- '*) ;; *) line="- $line" ;; esac
_mem_locked _mem_journal_locked "$file" "$(_mem_date)" "$line" \
|| { echo "memory: journal line not written" >&2; return 2; }
}
_mem_add_valid() { # <PREFIX> <title> <body-file> <status> <supersedes>
_mem_one_line title "$2" || return 2
case "$2$4" in *'['*|*']'*|*'|'*)
echo "memory: title and status take no [ ] |" >&2; return 2 ;;
esac
[ "${#2}" -le 160 ] || { echo "memory: title over 160 chars" >&2; return 2; }
case "$4" in *$'\n'*) _mem_one_line status "$4"; return 2 ;; esac
[ -s "$3" ] || { echo "memory: empty or missing body: $3" >&2; return 2; }
if [ "$1" = BLK ] && ! "$MEM_GREP" -q '`' "$3"; then
echo "memory: a blocker body quotes ≥1 error in backticks" >&2; return 2
fi
case "$5" in ''|"$1"-[0-9]*) ;; *)
echo "memory: --supersedes takes a $1 ID: $5" >&2; return 2 ;;
esac
}
_mem_entry_text() { # <title> <body-file> <status> <supersedes> → stdout
local tag=""
[ -z "$3" ] || tag=" [$3]"
printf '%s%s (%s)\n' "$1" "$tag" "$(_mem_date)"
awk '{ l[NR] = $0 } END { n = NR; while (n > 0 && l[n] == "") n--
for (i = 1; i <= n; i++) print l[i] }' "$2"
[ -z "$4" ] || printf 'Supersedes [[%s]].\n' "$4"
}
_mem_add_locked() { # <file> <PREFIX> <entry-file> <supersedes>
local id bak rc=0
id=$(_mem_next_id "$1" "$2")
bak=$(mktemp) || { echo "memory: no temp file" >&2; return 2; }
cat "$1" > "$bak"
{ printf '\n## %s — ' "$id"; cat "$3"; } >> "$1" || rc=2
if [ "$rc" = 0 ] && [ -n "$4" ]; then
_mem_insert "$1" "$4" "- **Status ($(_mem_date))**: superseded by $id" \
|| rc=2
fi
if [ "$rc" = 0 ] && _mem_is_v2 "$1"; then memory_check_file "$1" || rc=2; fi
if [ "$rc" != 0 ]; then
cat "$bak" > "$1"; echo "memory: add refused, $1 restored" >&2
else echo "$id"; fi
rm -f "$bak"
return "$rc"
}
memory_add() { # <kind> <title> <body-file> <status> <supersedes>
local spec file entry rc
spec=$(_mem_kind "$1") || return 2
file=".claude/memory/${spec% *}"
_mem_add_valid "${spec#* }" "$2" "$3" "$4" "$5" || return 2
[ -f "$file" ] || { echo "memory: no registry: $file" >&2; return 2; }
[ -z "$5" ] || _mem_has_id "$file" "$5" \
|| { echo "memory: no entry $5 in $file" >&2; return 2; }
_mem_is_v2 "$file" || echo "memory: v1 registry (no format: 2): $file" >&2
entry=$(mktemp) || { echo "memory: no temp file" >&2; return 2; }
_mem_entry_text "$2" "$3" "$4" "$5" > "$entry"
_mem_locked _mem_add_locked "$file" "${spec#* }" "$entry" "$5"; rc=$?
rm -f "$entry"
return "$rc"
}
_mem_add_cli() { # <kind> --title t --body-file f [--status s] [--supersedes I]
local kind="${1:-}" title="" body="" st="" sup=""
[ $# -gt 0 ] && shift
while [ $# -gt 0 ]; do
[ $# -ge 2 ] || { echo "memory: add: $1 needs a value" >&2; return 2; }
case "$1" in
--title) title="$2" ;;
--body-file) body="$2" ;;
--status) st="$2" ;;
--supersedes) sup="$2" ;;
*) echo "memory: add: unknown option: $1" >&2; return 2 ;;
esac
shift 2
done
memory_add "$kind" "$title" "$body" "$st" "$sup"
}
_mem_prefix() { # <file> → the frontmatter entry_prefix, rc 2 when none
local p
p=$(awk 'NR > 1 && $0 == "---" { exit }
sub(/^entry_prefix: /, "") { print; exit }' "$1")
[ -n "$p" ] || { echo "memory: $1: no entry_prefix" >&2; return 2; }
echo "$p"
}
memory_check_file() { # <file> → rc 2 with each culprit line named
local p
p=$(_mem_prefix "$1") || return 2
awk -v p="$p" -v f="$1" '
/^```/ { fence = !fence; next }
fence { next }
$0 == "## Index" { print f ":" NR ": ## Index in a v2 registry"; bad = 1 }
!match($0, "^## " p "-[0-9]") { next }
!match($0, "^## " p "-[0-9][0-9][0-9]+ — .") {
print f ":" NR ": heading shape: " $0; bad = 1; next }
seen[$2]++ { print f ":" NR ": duplicate " $2; bad = 1 }
END { exit bad ? 2 : 0 }' "$1" | sed 's/^/memory: /' >&2
return "${PIPESTATUS[0]}"
}
_MEM_REGISTRIES='decisions learnings blockers evals'
memory_check() { # [--staged] [<memory-dir>] → rc 2 on any culprit
local staged="" dir f rc=0
[ "${1:-}" = --staged ] && { staged=1; shift; }
dir="${1:-.claude/memory}"
for f in $_MEM_REGISTRIES; do
f="$dir/$f.md"
[ -f "$f" ] || continue
if ! _mem_is_v2 "$f"; then echo "memory: v1: $f" >&2; continue; fi
memory_check_file "$f" || rc=2
[ -z "$staged" ] || _mem_check_staged "$f" || rc=2
done
return "$rc"
}
_mem_check_staged() { # <file> → rc 2 when a staged hunk removes an entry line
local first
git cat-file -e "HEAD:./$1" 2>/dev/null || return 0
first=$(git show "HEAD:./$1" | awk '/^```/ { fence = !fence; next }
!fence && /^## (BDR|LRN|BLK|EVAL)-[0-9]/ { print NR; exit }')
[ -n "$first" ] || return 0
git diff --cached -U0 -- "$1" | awk -v first="$first" -v f="$1" '
/^@@ / { split($2, a, ","); start = substr(a[1], 2) + 0
cnt = (2 in a) ? a[2] + 0 : 1
if (cnt > 0 && start + cnt - 1 >= first) {
print "memory: " f ": line " start " removed inside an entry" \
" (curation: /prune-memory then /commit-change)"
bad = 1 } }
END { exit bad ? 2 : 0 }' >&2
}
_mem_index_rows() { # <file> <PREFIX> → "ID | date | title | word" rows
awk -v p="$2" -v MEMW="$_MEM_WORDS" "$_MEM_AWK"'
function row() { if (id != "") print "| " id " | " d " | " t " | " s " |" }
/^```/ { fence = !fence; next }
fence { next }
/^## / { row(); id = ""
if (!match($0, "^## " p "-[0-9]+")) next
id = $2; t = $0; sub(/^## [^ ]+( — )?/, "", t); d = ""; s = mem_tag(t)
if (match(t, / \([0-9-]+\) *$/)) {
d = substr(t, RSTART + 2, RLENGTH - 3); t = substr(t, 1, RSTART - 1) }
else if (match(t, / — [0-9][0-9][0-9][0-9]-[0-9-]+ *$/)) {
d = substr(t, RSTART + 5); t = substr(t, 1, RSTART - 1) }
sub(/ \[[^]]*\] *$/, "", t); gsub(/\|/, "/", t); next }
id == "" { next }
d == "" && sub(/^- \*\*Date\*\*: */, "") { d = substr($0, 1, 10) }
mem_cand($0) { s = mem_word($0) }
END { row() }' "$1"
}
memory_index() { # [<kind>] → one table per registry
local kinds="decision learning blocker eval" k spec file
[ -z "${1:-}" ] || kinds="$1"
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
done
}
_mem_index_table() { # <file> → "ID<TAB>date<TAB>last column" per Index row
awk '$0 == "## Index" { on = 1; next }
on && /^## / { exit }
on && /^\| *[A-Z]+-[0-9]+ *\|/ {
n = split($0, c, "|")
for (i = 2; i < n; i++) gsub(/^ +| +$/, "", c[i])
print c[2] "\t" c[3] "\t" c[n - 1] }' "$1"
}
_mem_upgrade_text() { # <rows> <file> <PREFIX> → the v2 text on stdout
awk -v p="$3" -v today="$(_mem_date)" -v MEMW="$_MEM_WORDS" "$_MEM_AWK"'
function need(i, c, w) {
c = col[i]; w = mem_word(c)
if (p == "EVAL" || w == "") return ""
if (p == "LRN") return c ~ /^superseded by/ ? c : ""
if (st[i] == "" || (w ~ /^(superseded|deprecated)$/ && st[i] != w))
return c
return "" }
function flush( i, s) {
if (cur != "" && (s = need(cur)) != "")
print "- **Status (" today ", upgrade)**: " s
for (i = 1; i <= n; i++) print buf[i]
n = 0 }
FNR == 1 { pass++ }
pass == 1 { split($0, r, "\t"); dt[r[1]] = r[2]; col[r[1]] = r[3]; next }
pass == 2 { split($0, r, / \| /); sub(/^\| /, "", r[1])
sub(/ \|$/, "", r[4]); hd[r[1]] = r[2] != ""; st[r[1]] = r[4]
next }
FNR == 1 { print; print "format: 2"; next }
/^```/ { fence = !fence }
$0 == "## Index" { skip = 1; next }
skip { if (/^\|/) { tbl = 1; next }
if (!tbl || $0 == "") { if (tbl) skip = 0; next }
skip = 0 }
!fence && /^## / { flush(); split($0, w, " ")
cur = (w[2] in col) ? w[2] : ""; print
if (cur != "" && !hd[cur] && dt[cur] != "") print "- **Date**: " dt[cur]
next }
cur != "" && ($0 == "" || $0 == "---") { buf[++n] = $0; next }
{ for (i = 1; i <= n; i++) print buf[i]; n = 0; print }
END { flush() }' "$1" <(_mem_index_rows "$2" "$3") "$2"
}
_mem_upgrade_oracle() { # <old> <new> <PREFIX> <rows> → rc 2 on a lost fact
local o n
[ "$("$MEM_GREP" -E "^## $3-[0-9]" "$1")" \
= "$("$MEM_GREP" -E "^## $3-[0-9]" "$2")" ] \
|| { echo "memory: upgrade changed a heading: $1" >&2; return 2; }
memory_check_file "$2" || return 2
o=$(_mem_index_rows "$1" "$3"); n=$(_mem_index_rows "$2" "$3")
awk -F'\t' -v p="$3" -v MEMW="$_MEM_WORDS" "$_MEM_AWK"'
function f(s, k) { split(s, a, / \| /); return a[k] }
FNR == 1 { pass++ }
pass == 1 { dt[$1] = $2; col[$1] = $3; next }
{ id = f($0, 1); sub(/^\| /, "", id) }
pass == 2 { old[id] = f($0, 4); sub(/ \|$/, "", old[id]); next }
!(id in col) { next }
{ d = f($0, 2); s = f($0, 4); sub(/ \|$/, "", s); w = mem_word(col[id])
if (p == "LRN" && col[id] !~ /^superseded by/) w = ""
if (p == "EVAL") w = ""
want = (w ~ /^(superseded|deprecated)$/ || old[id] == "") ? w : old[id]
if (dt[id] != "" && d == "") bad = bad " " id ":date"
if (want != "" && s != want) bad = bad " " id ":" s "!=" want }
END { if (bad != "") { print "memory: upgrade lost:" bad; exit 2 } }' \
"$4" <(printf '%s\n' "$o") <(printf '%s\n' "$n") >&2
}
_mem_upgrade_file() { # <file> → rc 0 upgraded or already v2, rc 2 refused
local p rows new rc=0 first
IFS= read -r first < "$1"
[ "$first" = --- ] \
|| { echo "memory: $1: no frontmatter, not upgraded" >&2; return 2; }
_mem_is_v2 "$1" && { echo "memory: $1 already v2" >&2; return 0; }
p=$(_mem_prefix "$1") || return 2
rows=$(mktemp) || { echo "memory: no temp file" >&2; return 2; }
new=$(mktemp) || { rm -f "$rows"; echo "memory: no temp file" >&2; return 2; }
_mem_index_table "$1" > "$rows"
{ _mem_upgrade_text "$rows" "$1" "$p" > "$new" \
&& _mem_upgrade_oracle "$1" "$new" "$p" "$rows" \
&& cat "$new" > "$1"; } || rc=2
rm -f "$rows" "$new"
[ "$rc" = 0 ] && echo "memory: upgraded $1"
[ "$rc" = 0 ] || echo "memory: $1 left as it was" >&2
return "$rc"
}
memory_upgrade() { # [<memory-dir>] → every template-born registry to v2
local dir="${1:-.claude/memory}" f rc=0
for f in $_MEM_REGISTRIES; do
[ -f "$dir/$f.md" ] || continue
_mem_upgrade_file "$dir/$f.md" || rc=2
done
return "$rc"
}
_memory_main() {
set -uo pipefail
if [ "${1:-}" = "-C" ]; then
cd "${2:-}" 2>/dev/null \
|| { echo "memory: no dir ${2:-}" >&2; return 2; }
shift 2
fi
case "${1:-}:$#" in
add:*) shift; _mem_add_cli "$@" ;;
status:3) memory_status "$2" "$3" ;;
journal:2) memory_journal "$2" ;;
index:[12]) memory_index "${2:-}" ;;
check:*) shift; memory_check "$@" ;;
upgrade:[12]) memory_upgrade "${2:-}" ;;
*) echo "usage: memory.sh [-C dir] add <kind> --title t --body-file f" \
"[--status s] [--supersedes ID] | status <ID> <text> | journal" \
"<line> | index [kind] | check [--staged] [dir] | upgrade [dir]" >&2
return 2 ;;
esac
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then _memory_main "$@"; fi
@@ -0,0 +1,273 @@
#!/usr/bin/env bash
# lib/tests/memory.test.sh — lib/memory.sh (add / status / journal / index /
# check / upgrade) and the shared status parser. Hermetic fixtures under one
# mktemp dir with its own git repo; flip-tests first (LRN-096), then the
# real check on this repo's registries.
set -u
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
MEM="${MEM_UNDER_TEST:-$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); trap '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
mkdir -p .claude/memory
reg() { # <file> <type> <PREFIX> <title> → a v2 registry with no entry
printf -- '---\nformat: 2\ntype: %s\nentry_prefix: %s\n---\n\n# %s\n' \
"$2" "$3" "$4" > ".claude/memory/$1"
}
reg decisions.md decisions_registry BDR 'Decisions registry (BDR)'
reg learnings.md learnings_registry LRN 'Learnings registry (LRN)'
reg blockers.md blockers_registry BLK 'Blockers registry (BLK)'
reg evals.md evals_registry EVAL 'Evals registry (EVAL)'
printf -- '---\ntype: journal\n---\n\n# Journal\n\n## 2026-10-26\n- old\n' \
> .claude/memory/journal.md
B=.claude/memory/blockers.md
printf -- "- **Friction**: \`make\` red\n- **Cause**: x\n\n" > body.md
# T1 add: next ID from headings, canonical heading, ID alone on stdout
out=$(bash "$MEM" add blocker --title 'make red' --body-file body.md \
--status open)
check T1-first-id "$out" BLK-001
check T1b-heading "$(grep -c '^## BLK-001 — make red \[open\] (2026-10-27)$' "$B")" 1
check T1c-body-trimmed "$(tail -1 "$B")" '- **Cause**: x'
check T1d-second-id "$(bash "$MEM" add blocker --title 'two' --body-file body.md)" BLK-002
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
# T2 add refuses, rc 2, registry unchanged
sum=$(cksum < "$B")
refused() { # <name> <add args…> → the add is rc 2
local name="$1"; shift
bash "$MEM" add "$@" >/dev/null 2>&1
check "T2-$name" "$?" 2
}
refused kind unknown --title t --body-file body.md
refused bracket blocker --title 'a[b]' --body-file body.md
refused pipe blocker --title 'a|b' --body-file body.md
refused no-body blocker --title t --body-file missing.md
refused status-bracket blocker --title t --body-file body.md --status 'a]'
refused cross-prefix blocker --title t --body-file body.md --supersedes LRN-001
refused unknown-sup blocker --title t --body-file body.md --supersedes BLK-099
refused no-value blocker --title t --body-file
refused bad-option blocker --title t --body-file body.md --bogus x
printf 'no backtick here\n' > plain.md
bash "$MEM" add blocker --title t --body-file plain.md 2>/dev/null
check T2b-blocker-needs-backtick "$?" 2
check T2c-unchanged "$(cksum < "$B")" "$sum"
bash "$MEM" add learning --title t --body-file plain.md >/dev/null
check T2d-learning-without-backtick "$?" 0
# T3 status: a dated line ends the entry, before the next heading's blank
bash "$MEM" status BLK-001 'resolved abc1234'
check T3-rc "$?" 0
check T3b-placed "$(awk '/^## BLK-001/{on=1} on && /^- \*\*Status/{print; exit}' "$B")" \
'- **Status (2026-10-27)**: resolved abc1234'
check T3c-before-next "$(awk '/Status \(2026-10-27\)/{getline n; print n; exit}' "$B")" ''
bash "$MEM" status BLK-002 'upstream, open'
check T3d-at-eof "$(tail -1 "$B")" '- **Status (2026-10-27)**: upstream, open'
bash "$MEM" status BLK-404 x 2>/dev/null
check T3e-unknown-id "$?" 2
bash "$MEM" status BLK-001 "$(printf 'a\nb')" 2>/dev/null
check T3f-multi-line "$?" 2
# T4 journal: appended under today, heading created once, old date untouched
J=.claude/memory/journal.md
bash "$MEM" journal 'first'
bash "$MEM" journal '- second'
check T4-heading-once "$(grep -c '^## 2026-10-27$' "$J")" 1
check T4b-order "$(tail -2 "$J" | tr '\n' '|')" '- first|- second|'
check T4c-old-kept "$(sed -n '/^## 2026-10-26$/,/^$/p' "$J" | grep -c '^- old$')" 1
bash "$MEM" journal "$(printf 'a\nb')" 2>/dev/null
check T4d-multi-line "$?" 2
# T5 --supersedes: forward link on the new entry, status line on the old one
L=.claude/memory/learnings.md
new=$(bash "$MEM" add learning --title 'better' --body-file plain.md \
--supersedes LRN-001)
check T5-id "$new" LRN-002
check T5b-forward "$(tail -1 "$L")" 'Supersedes [[LRN-001]].'
word() { # <file> <ID> → the entry's status word, through the lib
# shellcheck source=/dev/null
(. "$MEM"; memory_status_word "$(memory_entry_status "$1" "$2")")
}
check T5c-old-status "$(word "$L" LRN-001)" superseded
check T5d-index "$(bash "$MEM" index learning | grep -c '^| LRN-001 | 2026-10-27 | t | superseded |$')" 1
# T6 lock: a held lock refuses, a stale one is stolen, concurrent adds get
# distinct IDs
lock="$(git rev-parse --git-common-dir)/memory.lock"
date +%s > "$lock"
sum=$(cksum < .claude/memory/evals.md)
MEM_LOCK_TRIES=2 bash "$MEM" add eval --title t --body-file plain.md 2>/dev/null
check T6-held-rc "$?" 2
check T6b-held-unchanged "$(cksum < .claude/memory/evals.md)" "$sum"
echo $(( $(date +%s) - 60 )) > "$lock"
check T6c-stale-stolen "$(bash "$MEM" add eval --title t --body-file plain.md)" EVAL-001
check T6d-lock-released "$([ -e "$lock" ] && echo held || echo free)" free
for i in 1 2 3 4 5; do
bash "$MEM" add decision --title "d$i" --body-file plain.md > "out$i" &
done
wait
check T6e-distinct "$(cat out1 out2 out3 out4 out5 | sort -u | wc -l | tr -d ' ')" 5
check T6f-max "$(cat out1 out2 out3 out4 out5 | sort | tail -1)" BDR-005
check T6g-check-green "$(bash "$MEM" check; echo $?)" 0
# T7 check: shape, duplicate, Index in v2 are rc 2; v1 is named, not failed
cp .claude/memory/decisions.md d.bak
printf '\n## BDR-005 — again\nx\n' >> .claude/memory/decisions.md
err=$(bash "$MEM" check 2>&1 >/dev/null); rc=$?
check T7-duplicate-rc "$rc" 2
check T7b-duplicate-named "$(printf '%s\n' "$err" | grep -c 'duplicate BDR-005')" 1
cat d.bak > .claude/memory/decisions.md
printf '\n## BDR-7 — short\nx\n' >> .claude/memory/decisions.md
bash "$MEM" check 2>/dev/null
check T7c-shape-rc "$?" 2
cat d.bak > .claude/memory/decisions.md
printf '\n## Index\n' >> .claude/memory/decisions.md
bash "$MEM" check 2>/dev/null
check T7d-index-in-v2-rc "$?" 2
cat d.bak > .claude/memory/decisions.md
mkdir v1 && printf '# Decisions\n\n## BDR-001 - legacy\n' > v1/decisions.md
err=$(bash "$MEM" check v1 2>&1 >/dev/null); rc=$?
check T7e-v1-rc "$rc" 0
check T7f-v1-named "$(printf '%s\n' "$err" | grep -c '^memory: v1: v1/decisions.md$')" 1
# T8 check --staged: an append passes, a removed entry line is rc 2
git add -A && git commit -qm base
bash "$MEM" add blocker --title three --body-file body.md >/dev/null
git add "$B"
check T8-append-staged "$(bash "$MEM" check --staged; echo $?)" 0
git commit -qm three
sed 's/^## BLK-001 — make red/## BLK-001 — make RED/' "$B" > b.tmp \
&& cat b.tmp > "$B"
git add "$B"
err=$(bash "$MEM" check --staged 2>&1 >/dev/null); rc=$?
check T8b-removed-line-rc "$rc" 2
check T8c-named "$(printf '%s\n' "$err" | grep -c 'removed inside an entry')" 1
git checkout -q HEAD -- "$B"
# 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
cat > .claude/memory/decisions.md <<'EOF'
---
type: decisions_registry
entry_prefix: BDR
---
# Decisions registry (BDR)
## Index
| ID | Date | Title | Status |
|----|------|-------|--------|
| BDR-001 | 2026-01-01 | kept | accepted |
| BDR-002 | 2026-01-02 | stale index | accepted |
| BDR-003 | 2026-01-03 | gone | superseded by BDR-004 |
| BDR-004 | 2026-01-04 | no status | accepted |
---
## BDR-001 — kept (2026-01-01)
- **Status**: accepted
## BDR-002 — stale index
- **Date**: 2026-01-02
- **Status**: shipped
## BDR-003 — gone
- **Status**: accepted
## BDR-004 — no status
body
EOF
cat > .claude/memory/learnings.md <<'EOF'
---
type: learnings_registry
entry_prefix: LRN
---
# Learnings registry (LRN)
## Index
| ID | Date | Pattern | Applies to |
|----|------|---------|------------|
| LRN-001 | 2026-02-01 | old | superseded by LRN-002 |
| LRN-002 | 2026-02-02 | new | any shell |
## LRN-001 — old
x
## LRN-002 — new
y
EOF
D=.claude/memory/decisions.md
heads=$(grep '^## BDR-' "$D")
bash "$MEM" upgrade >/dev/null 2>&1
check T9-rc "$?" 0
check T9b-marker "$(sed -n 2p "$D")" 'format: 2'
check T9c-no-index "$(grep -c '^## Index$\|^| BDR-' "$D")" 0
check T9d-headings-same "$(grep '^## BDR-' "$D")" "$heads"
check T9e-index-out "$(bash "$MEM" index decision | sed -n '5,8p' | tr '\n' '#')" \
'| BDR-001 | 2026-01-01 | kept | accepted |#| BDR-002 | 2026-01-02 | stale index | shipped |#| BDR-003 | 2026-01-03 | gone | superseded |#| BDR-004 | 2026-01-04 | no status | accepted |#'
check T9f-lrn-superseded "$(bash "$MEM" index learning | grep -c '^| LRN-001 | 2026-02-01 | old | superseded |$')" 1
check T9g-lrn-applies-dropped "$(grep -c 'any shell' .claude/memory/learnings.md)" 0
check T9h-check-green "$(bash "$MEM" check; echo $?)" 0
sum=$(cksum < "$D")
bash "$MEM" upgrade >/dev/null 2>&1
check T9i-idempotent "$(cksum < "$D")" "$sum"
cat > .claude/memory/blockers.md <<'EOF'
---
type: blockers_registry
entry_prefix: BLK
---
## Index
| ID | Date | Friction | Status |
|----|------|----------|--------|
| BLK-001 | 2026-03-01 | dup | open |
## BLK-001 — dup
- **Status**: open
## BLK-001 — dup again
EOF
sum=$(cksum < .claude/memory/blockers.md)
bash "$MEM" upgrade >/dev/null 2>&1
check T9j-oracle-refuses "$?" 2
check T9k-refused-untouched "$(cksum < .claude/memory/blockers.md)" "$sum"
printf '# Evals\n\n## EVAL-001 | 2026 | pipe dialect\n' > .claude/memory/evals.md
bash "$MEM" upgrade >/dev/null 2>&1
check T9l-no-frontmatter-rc "$?" 2
check T9m-dialect-untouched "$(sed -n 1p .claude/memory/evals.md)" '# Evals'
cd "$FIX" || exit 1
# T10 parser: header tag and labelled lines only, last wins, open ranks last
cp "$ROOT/lib/tests/fixtures/blockers-v2-status.md" st.md
check T10-header-tag "$(word st.md BLK-032)" resolved
check T10b-prose-open-ignored "$(word st.md BLK-033)" resolved
check T10c-fail-open-ignored "$(word st.md BLK-034)" resolved
check T10d-final-wins "$(word st.md BLK-008)" resolved
check T10e-accepted-before-open "$(word st.md BLK-040)" accepted
check T10f-mid-title-bracket "$(word st.md BLK-041)" open
# shellcheck source=/dev/null
(. "$MEM"; memory_entry_status st.md BLK-042) >/dev/null
check T10g-no-candidate-rc "$?" 1
# T11 -C runs from elsewhere
check T11-dash-C "$(cd / && bash "$MEM" -C "$FIX" index blocker | grep -c '^| BLK-00')" 3
bash "$MEM" -C /nonexistent check 2>/dev/null
check T11b-bad-dir "$?" 2
bash "$MEM" frobnicate 2>/dev/null
check T11c-usage "$?" 2
# T12 this repo's registries pass the check
check T12-live-check "$(bash "$MEM" -C "$ROOT" check >/dev/null 2>&1; echo $?)" 0
echo "PASS=$pass FAIL=$fail"
[ "$fail" -eq 0 ]