Files
claude/.claude/tasks/plans/2026-11-13-memory-sh-residuals-1400.md
T

9.1 KiB

PLAN — memory-sh-residuals (phase 4a leftovers + BUGS-FOUND memory.sh items, block H2) r1

Scope: lib/memory.sh, lib/tests/memory.test.sh, lib/reconcile.sh (:117 one call).

  • (a) memory_legacy_status <file> <ID> [<end>] [<re>]: 4th positional replaces ${MEM_LEGACY_RE:-…}; RE="${4:-$_MEM_LEGACY_RE}". reconcile.sh:117 passes "$RECONCILE_BDR_RE" as $4 and drops the env assignment. No other setter exists (grep). Comment "callers may widen the selector" updated.
  • (b) one range reader: the default (no ) path becomes the awk path with END_RE='^##* *BLK-[0-9]' (any heading level of another BLK ends the range; a same-ID ### sub-heading continues it, unread, as today in the form). Both forms also stop at ANY level-2 heading that is not the entry itself (!fence && /^## / && on && $0 !~ "^## " id " " → on=0): a trailing ## Index or section is never read (BUGS-FOUND :38). The sed/grep path and MEM_GREP use in this function go away; reconcile.sh:97 keeps calling without (behaviour: a ### BLK- entry no longer leaks into the ## entry above, BUGS-FOUND :22).
  • (c) stderr echoes of a v1 line (_mem_repair_one :726-731, and any other ${v1:0:100} site) go through _mem_printable <text> = printf '%s' "${1//[![:print:]]/?}" (locale-safe: run under the script's existing locale; tabs become ? too, accepted). One helper, ≤ 5 lines.
  • (d) _mem_journal_locked: on rc 1, prepend \n only when the file is non-empty and its last line is not blank: [ -s "$1" ] && [ -n "$(tail -c1 "$1")" ] || sep='' is wrong (a trailing newline is normal); use tail -n1 "$1" non-empty → sep=$'\n', else sep=''; printf '%s## %s\n%s\n' "$sep" "$2" "$3" >> "$1".
  • (e) _mem_upgrade_text flush: when st[cur] == "" and the entry body holds a v1 status label line (_mem_v1_word of memory_legacy_status <old> <ID> <end for the prefix> gives a word), treat that word as st so mem_want keeps the body and prints "kept body , dropped Index " (BDR-129: Index never wins over a body label). Implementation: the rows file passed as ARGV[1] gains a 4th column = v1 body word per ID (computed in the caller loop over _mem_heading_ids), read as v1w[r[1]] = r[4]; flush uses st[cur] != "" ? st[cur] : v1w[cur]. If memory.sh's existing structure makes this > 25 logic lines, split a helper _mem_v1_words <old> <PREFIX> printing ID<TAB>word.
  • (f) lib/tests/memory.test.sh: new cases (labels after the last existing T-label): ### BLK- after ## BLK- on a v1 file → the ## entry stays open (legacy status not leaked); v1 decisions file with a trailing ## Index after the last BDR → memory_legacy_status … '^##* *BDR-[0-9]' returns nothing from the Index; MEM_LEGACY_RE=open exported in the test env does NOT change the result; control chars (\x1b[31m) in a v1 line → stderr shows ? not the raw byte; journal on a file ending with a blank line → exactly one blank line before the new heading; upgrade fixture: Index cell resolved, body **Statut** : open → body kept, stderr "kept body open, dropped Index resolved", no (…, upgrade) line written; upgrade fixture whose v1 line is Status: resolved with strict reader open → stderr "wrote upgrade status resolved" and the dated line present; fixture with a dated - **Status (2026-…)**: line → stderr "dated status line kept".
  • Not done, justified: the real-data replay oracle (up4a/realdata.sh, session scratchpad) is gone; the fixture suite above replaces it (TODO line removal says so).
  • Disposition: honors BDR-129 (one status parser, Index never a status source), BDR-125 phase 4a; make test runs memory.test.sh (bun needed for T18 only).

r2 (correctness FATAL(4), robustness CONCERNS(3), simplicity FATAL(3); all folded) — r2 WINS over r1

  • (a) signature memory_legacy_status <file> <ID> [<re>]: <end> DERIVED inside = "^##* *${2%%-*}-[0-9]"; 3rd positional = label selector, default $_MEM_LEGACY_RE; new _MEM_LEGACY_BDR_RE="$_MEM_LEGACY_RE|statut|superseded|deprecated" hosted in memory.sh (reconcile.sh:55 reuses it). Callers: memory.sh :669/:685/:722 drop the literal end; reconcile.sh:97 → memory_legacy_status "$1" "$2", :117-118 → memory_legacy_status "$1" "$2" "$_MEM_LEGACY_BDR_RE"; both MEM_GREP="$RECONCILE_GREP" prefixes deleted (dead). No env read.
  • (b) ONE awk reader (sed path gone), fence-aware (named change: a fenced ## BLK- no longer ends a range; v1 registries on disk hold no fence, 0 diffs measured by the robustness lens). Range ends at the next ^##* *<PREFIX>-[0-9] heading of another ID (a same-ID ### sub-heading continues, unread) OR at $0 == "## Index" (BUGS-FOUND :38) — NOT at every ## heading (an in-entry ## Update section must stay readable by the lexical reader so the oracle still sees its disagreement with the strict one). Rule placed after the same-ID next rule.
  • (c) in place, no helper: _mem_repair_one after word=$(_mem_v1_word "$v1"): v1=${v1//[![:print:]]/?}; awk side :612-613 gsub(/[[:cntrl:]]/, "?", col[cur]). Accepted and stated in a comment: under a C locale UTF-8 bytes print as ?. Remaining raw sinks (reconcile.sh:141 stdout, check "heading shape") listed in the TODO removal note, not fixed here.
  • (d) _mem_journal_locked: sep=$'\n'; [ -s "$1" ] || sep=''; [ -z "$(tail -c1 "$1")" ] || sep=$'\n\n' (no final newline → terminate + blank); [ -z "$(tail -n1 "$1")" ] && sep='' (already blank-ended). Then printf '%s## %s\n%s\n' "$sep" "$2" "$3" >> "$1".
  • (e) ONE effective status, three consumers. New _mem_v1_words <old> <PREFIX> → ID<TAB>word|mixed for every ID with an Index cell whose strict word is "" : word = _mem_v1_word of memory_legacy_status <old> <ID> <re> (re = _MEM_LEGACY_BDR_RE for BDR, default for BLK; LRN/EVAL: none), label accepted only anchored (^(- )?(\*\*)?[Ss]tatu[st] or after · / | ), mixed when a label line exists but _mem_v1_word gives nothing. Fed as an extra ARGV input to _mem_upgrade_text, _mem_index_stated and _mem_upgrade_oracle: effective st = strict != "" ? strict : v1w. Flush: strict "" + v1w word → WRITE - **Status (<today>, upgrade)**: <v1w> (the body label made readable) and, when the Index word differs, stderr "kept body , dropped Index "; mixed → not Index-stated, falls to the existing name-and-refuse path. Index-stated = IDs where mem_want(p, col, effective st) != "". Oracle want = mem_word(mem_want(p, col, effective st)), else effective st.
  • (f) tests (memory.test.sh): T15-probed gains an openset check (### leak); new v1 decisions fixture with a trailing ## Index → nothing read from it; control chars via refused() fixture → ? on stderr; T14l extended to three journal endings (none, \n, \n\n → exactly one blank line before the heading); upgrade fixtures: BLK Index open + body - **Statut** : resolved → rc 0, strict word resolved, stderr "kept body resolved, dropped Index open"; BLK Index resolved + body - **Statut** : resolved → stays resolved; BDR Index accepted + body **Statut** : proposed → rc 0, strict proposed; BLK Index open + mixed - **Statut** : resolved (x), open (y) → refused, named (as T15-mixed-statut-refused); "wrote upgrade status" / "dated status line kept" asserted by capturing stderr of T15-statut / T15-datestatus / T15-remedy-roundtrip (not new fixtures; the r1 Status: resolved wording is a strict candidate and cannot fire). T15-reconcile-grep repointed at reconcile_blk_open (its enumerator still greps). Fixtures use - **Statut** : / date: … · status: dialects.
  • CHANGELOG (doc sync): Removed line for the MEM_LEGACY_RE env override advertised in 3.0.0; Fixed line for the rest.

r3 (confirmation CONCERNS(2), folded; FINAL, no further challenge)

  • (e) flush ORDER kept: s = mem_want(p, col, est) first (est = strict != "" ? strict : v1w, where mixed counts as ""); non-empty s → write s (superseded/deprecated Index still wins, as today). Only when s == "" AND strict == "" AND v1w is a word → write - **Status (<today>, upgrade)**: <v1w> (+ stderr "kept body , dropped Index " when the Index word differs). mixed = "" for flush and oracle (today's Index path); it is EXCLUDED from Index-stated only, so a BLK mixed case reaches the verdict-diff refusal and a BDR mixed case keeps the Index word with rc 0.
  • _mem_v1_words "$1" "$2" reads the ORIGINAL file for all three consumers (build included; $src has rewritten ### headings).
  • memory_legacy_status routes $1 through _mem_path_safe (awk operand-assignment class, T19).
  • Every flush print of col[cur] applies gsub(/[[:cntrl:]]/, "?", …) (mawk-safe).
  • Named behaviour changes of (b) for reconcile's v1 BLK path: fence-aware; a same-ID ### BLK-x UPDATE … RESOLVED heading line is no longer itself a status candidate (0 on disk). BUGS-FOUND :38 marked PARTIALLY fixed: a trailing non-Index ## section is still read (kept so ## Update stays readable).
  • Extra fixtures: BDR Index superseded by BDR-004 + body **Statut** : accepted → rc 0, strict superseded; BDR mixed label + Index accepted → rc 0, strict accepted; BLK unknown word - **Status**: closed + Index resolved → strict resolved kept.