From 197b899c3f33c506ab1fa23abb30867aa1cda7eb Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 28 Oct 2026 16:01:22 +0100 Subject: [PATCH] fix(memory): upgrade-side v1 range ignores headings inside code fences --- lib/memory.sh | 25 +++++++++++++++++-------- lib/tests/memory.test.sh | 6 ++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/memory.sh b/lib/memory.sh index f9c135c..0d13231 100755 --- a/lib/memory.sh +++ b/lib/memory.sh @@ -580,14 +580,23 @@ _mem_heading_ids() { # → the entry IDs, any heading level _MEM_LEGACY_RE='status|RESOLVED|REVERTED|upstream|resolved|[^a-z]open' memory_legacy_status() { # [] → the v1 lexical status line # (reconcile reads it on a v1 blockers file; `upgrade` proves its verdicts - # against it). The range ends at the next line matching (BRE, - # default `^## BLK-`, reconcile's own). v1: drop every line first: the - # range is inclusive of the NEXT entry's header, which may carry a status. - local end="${3:-^## BLK-}" - sed -n "/^## ${2} /,/$end/p" "$1" \ - | "$MEM_GREP" -v "$end" \ - | "$MEM_GREP" -iE "$_MEM_LEGACY_RE" \ - | tail -1 + # 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. + local end="${3:-}" + if [ -z "$end" ]; then + sed -n "/^## ${2} /,/^## BLK-/p" "$1" \ + | "$MEM_GREP" -v '^## BLK-' \ + | "$MEM_GREP" -iE "$_MEM_LEGACY_RE" \ + | tail -1 + return + fi + RE="$_MEM_LEGACY_RE" awk -v id="$2" -v end="$end" ' + /^```/ { fence = !fence } + !fence && $0 ~ end { on = 0 } + !fence && $0 ~ "^## " id " " { on = 1; next } + on && tolower($0) ~ tolower(ENVIRON["RE"]) { last = $0 } + END { if (last != "") print last }' "$1" } _mem_legacy_open() { # → rc 0 when reconcile's v1 reader diff --git a/lib/tests/memory.test.sh b/lib/tests/memory.test.sh index 434ad63..654d079 100755 --- a/lib/tests/memory.test.sh +++ b/lib/tests/memory.test.sh @@ -422,6 +422,12 @@ v1blk d15/probed '## BLK-003 — three, open' 'Status: open' '' \ bash "$MEM" upgrade d15/probed >/dev/null 2>&1 check T15-probed-no-repair "$(grep -c 'upgrade)' d15/probed/blockers.md)" 0 check T15-probed-open "$(memory_word_of d15/probed/blockers.md BLK-003)" open +# a heading line quoted inside a ``` fence does not end the entry's range +v1blk d15/fenced '## BLK-003 — three' 'Status: resolved' '```' \ + '### BLK-001 — quoted' '```' 'Status: open' +bash "$MEM" upgrade d15/fenced >/dev/null 2>&1 +check T15-fenced-no-repair "$(grep -c 'upgrade)' d15/fenced/blockers.md)" 0 +check T15-fenced-open "$(memory_word_of d15/fenced/blockers.md BLK-003)" open # `## ID—x` (no space) is no v1-visible heading: no silent flip v1blk d15/nospace '## BLK-001—x' 'Status: resolved' '' \ '## BLK-002 — y' 'Status: open'