From 2dae5282f6c39da2467f61bf4d6bb4ee386c8a9f Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 30 Oct 2026 20:23:40 +0100 Subject: [PATCH] fix(memory): upgrade refuses a prose or mixed v1 closure instead of writing resolved The repair copied the v1 reader's lexical verdict, read the verdict diff while rewriting the file, and listed same-ID sub-headings twice. It now writes only a single closed word from a Status label, names and refuses the rest, and asserts one upgrade line per entry. --- lib/memory.sh | 99 ++++++++++++++++++++++++++++++++-------- lib/tests/memory.test.sh | 64 ++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 19 deletions(-) diff --git a/lib/memory.sh b/lib/memory.sh index d0024c2..ad7014c 100755 --- a/lib/memory.sh +++ b/lib/memory.sh @@ -18,9 +18,12 @@ # body lines, `format: 2`, `## Index` removed; a missing # frontmatter comes from the template, ` - ` / ` | date | ` # / `###` headings take the `## ID — title (date)` shape; -# a blocker whose open / closed verdict (reconcile's v1 -# reader) would move gets a `Status (…, upgrade)` line; the -# oracle (IDs, check, verdicts) runs first, else rc 2 +# a blocker the v1 reader closed but the strict one reads +# 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, +# 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 # trailing `[tag]`, or a body line whose bold label names Status, Resolution, @@ -593,7 +596,8 @@ memory_legacy_status() { # [] → the v1 lexical status line # (reconcile reads it on a v1 blockers file; `upgrade` proves its verdicts # against it). Default range: `## ID` to the next `## BLK-`, reconcile's # own. With (ERE) the range is fence-aware: it stops at the next - # heading matching outside a ``` fence, and that header is not read. + # heading matching outside a ``` fence, and that header is not read; + # a same-ID sub-heading (`### ID UPDATE`) continues the range, unread. local end="${3:-}" if [ -z "$end" ]; then sed -n "/^## ${2} /,/^## BLK-/p" "$1" \ @@ -604,6 +608,7 @@ memory_legacy_status() { # [] → the v1 lexical status line fi RE="$_MEM_LEGACY_RE" END_RE="$end" awk -v id="$2" ' /^```/ { fence = !fence; next } + !fence && $0 ~ ("^#+ *" id "( |$)") && on { next } !fence && $0 ~ ENVIRON["END_RE"] { on = 0 } !fence && $0 ~ "^## " id " " { on = 1; next } on && !fence && tolower($0) ~ tolower(ENVIRON["RE"]) { last = $0 } @@ -627,34 +632,78 @@ _mem_index_stated() { # → IDs whose Index status upgrade <(_mem_index_table "$1") <(_mem_index_rows "$1" "$2") } +_mem_explicit_status() { # → rc 0 when the v1 line is a + local v1 win # dated status line written by `memory.sh status`, which + v1=$(memory_legacy_status "$1" "$3" '^##* *BLK-[0-9]') # the strict + win=$(memory_entry_status "$2" "$3") # reader rightly wins + [ "$v1" = "$win" ] || return 1 + printf '%s\n' "$v1" | "$MEM_GREP" -qE '^- \*\*Status \([0-9]' || return 1 + case "$v1" in *", upgrade)"*) return 1 ;; esac +} + _mem_verdict_diff() { # → "IDword" per blocker whose open / - local id was now ix # closed verdict moved (word = the old verdict) - ix=$(_mem_index_stated "$1" BLK) - for id in $(_mem_heading_ids "$2" BLK); do + local id was now ix # closed verdict moved (word = the old verdict, or + ix=$(_mem_index_stated "$1" BLK) # `explicit`: a dated status line) + for id in $(_mem_heading_ids "$2" BLK | awk '!seen[$0]++'); do printf '%s\n' "$ix" | "$MEM_GREP" -qx "$id" && continue # the v1 reader enumerates `## ID` headings only: no verdict to keep "$MEM_GREP" -qE "^## $id( |$)" "$1" || continue _mem_legacy_open "$1" "$id" && was=open || was=resolved memory_closed "$(memory_status_word "$(memory_entry_status "$2" "$id")")" \ && now=resolved || now=open - [ "$was" = "$now" ] || printf '%s\t%s\n' "$id" "$was" + [ "$was" = "$now" ] && continue + [ "$was" = resolved ] && _mem_explicit_status "$1" "$2" "$id" \ + && was=explicit + printf '%s\t%s\n' "$id" "$was" done } +_mem_v1_word() { # → 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 { + 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 (c == 1) print word }' +} + +_mem_repair_one() { # → write the v1 closure when its line + local v1 word # names one closed word, else name the ID and refuse + 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: record it from the project root: memory.sh status" \ + "$3 \", …\" (the leading word decides), then re-run upgrade" >&2 + return 0 + fi + echo "memory: $3: wrote upgrade status $word from v1 line: ${v1:0:100}" >&2 + _mem_insert "$2" "$3" "- **Status ($(_mem_date), upgrade)**: $word" \ + || return 2 +} + _mem_upgrade_repair() { # → no blocker loses its closure - # v1 closed, strict reader open (a dialect it cannot read): a status line - # restores it. v1 open, strict closed is the BLK-032..034 fix (prose words - # read as status by the v1 reader): kept, named - local id word + # v1 closed, strict reader open (a dialect it cannot read): a labelled v1 + # line restores it, prose or a mixed label is named and left to the oracle. + # v1 open, strict closed is the BLK-032..034 fix (prose words read as + # status by the v1 reader): kept, named. The diff is read in full before + # the first insert rewrites the file under it + local id word rows [ "$3" = BLK ] || return 0 + rows=$(_mem_verdict_diff "$1" "$2") + [ -n "$rows" ] || return 0 while IFS=$'\t' read -r id word; do - if [ "$word" = open ]; then - echo "memory: $id: v1 reader open, strict reader closed (kept)" >&2 - continue - fi - _mem_insert "$2" "$id" "- **Status ($(_mem_date), upgrade)**: $word" \ - || return 2 - done < <(_mem_verdict_diff "$1" "$2") + case "$word" in + open) echo "memory: $id: v1 reader open, strict reader closed (kept)" \ + >&2 ;; + explicit) echo "memory: $id: dated status line kept, v1 reader closed" \ + "by prose" >&2 ;; + *) _mem_repair_one "$1" "$2" "$id" || return 2 ;; + esac + done <<< "$rows" } _mem_upgrade_oracle() { # → rc 2 on a lost fact @@ -681,6 +730,18 @@ _mem_upgrade_oracle() { # → rc 2 on a lost fact o=$(_mem_verdict_diff "$1" "$2" | awk -F'\t' '$2 == "resolved" \ { printf "%s ", $1 }') [ -z "$o" ] || { echo "memory: upgrade moved a verdict: $o" >&2; return 2; } + _mem_upgrade_dups "$2" "$3" +} + +_mem_upgrade_dups() { # → rc 2 when an entry holds two + local d # `, upgrade)` status lines + d=$(awk -v p="$2" ' + /^```/ { fence = !fence } + fence { next } + $0 ~ "^## " p "-[0-9]+" { id = $2; next } + /^- \*\*Status \([^)]*, upgrade\)\*\*:/ && ++n[id] == 2 { + printf "%s:dup ", id }' "$1") + [ -z "$d" ] || { echo "memory: upgrade lost: $d" >&2; return 2; } } _mem_upgrade_front() { # → the file; one lacking a frontmatter gets diff --git a/lib/tests/memory.test.sh b/lib/tests/memory.test.sh index 08b62af..d5b9d92 100755 --- a/lib/tests/memory.test.sh +++ b/lib/tests/memory.test.sh @@ -467,6 +467,7 @@ v1blk d15/datestatus '## BLK-001 — one line' \ '## BLK-002 — one line open' 'date: 2026-06-02 · status: open' dialect datestatus '^- \*\*Status ([0-9-]*, upgrade)\*\*: resolved$' check T15-datestatus-one-line "$(grep -c 'upgrade)' d15/datestatus/blockers.md)" 1 +check T15-datestatus-word "$(memory_word_of d15/datestatus/blockers.md BLK-001)" resolved v1blk d15/statut '## BLK-001 — fr' '- **Statut** : resolved' '' \ '## BLK-002 — fr open' '- **Statut** : open' dialect statut '^- \*\*Status ([0-9-]*, upgrade)\*\*: resolved$' @@ -502,6 +503,69 @@ 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" +# v1 closed by prose or a mixed label: refused and named, never guessed +refused() { # → rc 2, file untouched, ID and remedy on stderr + local f="d15/$1/blockers.md" before err rc + before=$(cksum < "$f") + err=$(bash "$MEM" upgrade "d15/$1" 2>&1 >/dev/null); rc=$? + check "$3-rc" "$rc" 2 + check "$3-untouched" "$(cksum < "$f")" "$before" + check "$3-named" "$(printf '%s\n' "$err" | grep -c "memory: $2: v1 closed")" 1 + check "$3-remedy" "$(printf '%s\n' "$err" | grep -c "memory.sh status $2 ")" 1; } +v1blk d15/prose '## BLK-001 — x' 'date: 2026-09-20 · status: open' \ + 'prose: another tab already resolved it' +refused prose BLK-001 T15-prose-refused +v1blk d15/mixdate '## BLK-001 — x' \ + 'date: 2026-10-17 | status: code resolved (abc) · SMTP open' +refused mixdate BLK-001 T15-mixed-date-refused +v1blk d15/mixstatut '## BLK-001 — x' '- **Statut** : resolved (x), open (y)' +refused mixstatut BLK-001 T15-mixed-statut-refused +# boundary: a strict candidate both readers close is no repair +v1blk d15/mixbound '## BLK-001 — x' 'status: code resolved (abc) · SMTP open' +bash "$MEM" upgrade d15/mixbound >/dev/null 2>&1 +check T15-mixed-strict-boundary "$?/$(grep -c 'upgrade)' d15/mixbound/blockers.md)" 0/0 +# a same-ID sub-heading neither doubles the repair nor hides a reopening +v1blk d15/sub '## BLK-008 — x' 'date: 2026-07-20 · status: RESOLVED 2026-07-20' \ + '### BLK-008 UPDATE 2026-07-20 (y)' 'body' +bash "$MEM" upgrade d15/sub >/dev/null 2>&1 +check T15-subhead-one-repair "$?/$(grep -c 'upgrade)' d15/sub/blockers.md)" 0/1 +v1blk d15/subre '## BLK-008 — x' 'date: 2026-07-20 · status: RESOLVED' \ + '### BLK-008 UPDATE 2026-07-20 (y)' 'Status: open, reopened' +bash "$MEM" upgrade d15/subre >/dev/null 2>&1 +check T15-subhead-reopened "$?/$(grep -c 'upgrade)' d15/subre/blockers.md)/$(memory_word_of d15/subre/blockers.md BLK-008)" 0/0/open +# the remedy converges, a "resolved" in its own text included +remedy() { # → the project d15/ with a v1 blockers file + mkdir -p "d15/$1/.claude/memory"; local n="$1"; shift + v1blk "d15/$n/.claude/memory" "$@"; } +remedy e '## BLK-001 — x' 'date: 2026-09-20 · status: open' \ + 'prose: another tab already resolved it' +bash "$MEM" -C d15/e status BLK-001 'upstream, SMTP OVH; code resolved (x)' >/dev/null 2>&1 +E=d15/e/.claude/memory/blockers.md +bash "$MEM" -C d15/e upgrade >/dev/null 2>&1 +check T15-remedy-roundtrip "$?/$(grep -c 'upgrade)' $E)/$(memory_word_of $E BLK-001)" 0/0/upstream +remedy e2 '## BLK-001 — x' 'date: 2026-09-20 · status: open' \ + '### BLK-001 UPDATE 2026-10-01 (y)' 'prose: another tab already resolved it' +bash "$MEM" -C d15/e2 status BLK-001 'upstream, SMTP OVH; code resolved (x)' >/dev/null 2>&1 +E=d15/e2/.claude/memory/blockers.md +bash "$MEM" -C d15/e2 upgrade >/dev/null 2>&1 +check T15-remedy-subhead "$?/$(grep -c 'upgrade)' $E)/$(memory_word_of $E BLK-001)" 0/0/upstream +# a dated line then a later prose closure: the v1 line is the prose, refused +v1blk d15/dated '## BLK-001 — x' 'date: x · status: open' \ + '- **Status (2026-10-30)**: open, waiting' 'later prose: fixed, resolved in abc' +refused dated BLK-001 T15-dated-then-prose-refused +# the v1 range continues past a same-ID sub-heading (fails if it stops there) +v1blk d15/range '## BLK-001 — x' 'prose: fixed, resolved in abc' \ + '### BLK-001 UPDATE 2026-10-01 (y)' 'Status: open, reopened' +bash "$MEM" upgrade d15/range >/dev/null 2>&1 +check T15-range-continues "$?/$(grep -c 'upgrade)' d15/range/blockers.md)" 0/0 +# the oracle refuses two upgrade lines under one entry +v1blk d15/dup '## BLK-001 | 2026-05-18 | dup' 'date: 2026-06-02 · status: RESOLVED' +err=$(bash -c '. "$1"; _mem_upgrade_repair() { local l + l="- **Status (2026-10-30, upgrade)**: resolved" + _mem_insert "$2" BLK-001 "$l"; _mem_insert "$2" BLK-001 "$l"; } + memory_upgrade "$2"' _ "$MEM" d15/dup 2>&1 >/dev/null) +check T15-oracle-dup-refused "$?/$(printf '%s\n' "$err" | grep -c 'BLK-001:dup')" 2/1 + # T16 final-review fixes # lock dir not writable: bounded, named, rc 2 (never a spin) chmod a-w .git