From a67f4f41c11cb792fd8dea548a84b156940f1e6e Mon Sep 17 00:00:00 2001 From: "Carlos D. Escobar-Valbuena" Date: Fri, 26 Jun 2026 20:52:05 -0500 Subject: [PATCH 1/3] feat(repair+doctor): propagate + audit the P6 /kg-discovery reflex (BRO-1555) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two propagation gaps: BRO-1426 added the §P6 "/kg for discovery, never substrate grep" retrieval reflex to the AGENTS.md template, but existing workspaces never received it — scaffold_governance_file is idempotent-never-overwrite, and repair.sh only backfilled the Development Philosophy section. doctor never checked AGENTS.md actually carried the reflex either. - repair.sh: backfill_retrieval_discipline() backfills the §P6 retrieval paragraph into an existing AGENTS.md — idempotent (skip if present), anchored (§P6 → next heading; skip with warning if absent), file-only awk extraction (no shell interpolation of content, backticks/pipes survive). Flag-clear (not awk `exit`) on the boundary so tr|awk doesn't SIGPIPE under pipefail. Mirrors backfill_philosophy_section; registered next to it. - doctor.sh §4c: advisory check that AGENTS.md carries the reflex. Informational only — never a GAP, never fails --strict (mirrors §4b convention). Proven gap-neutral + strict-neutral by test. - Marker is the coined phrase "substrate grep" (not the full "never substrate grep") so a workspace carrying the reflex under DIFFERENT wording — e.g. this workspace's hand-authored "not a substrate grep" variant — is detected as present and NOT duplicated. Verified: doctor reports [ok] + repair won't duplicate on the real workspace. Tests: tests/retrieval-discipline-backfill.test.sh (16 assertions) — backfill + idempotency + position-inside-§P6 + verbatim content + --dry-run + missing-anchor skip + §4c advisory both directions + gap/strict-neutral + CRLF. Sibling philosophy-backfill + all 5 canary suites still green. BRO-1555 Co-Authored-By: Claude Opus 4.8 (1M context) --- scripts/doctor.sh | 17 ++ scripts/repair.sh | 101 +++++++++ tests/retrieval-discipline-backfill.test.sh | 234 ++++++++++++++++++++ 3 files changed, 352 insertions(+) create mode 100755 tests/retrieval-discipline-backfill.test.sh diff --git a/scripts/doctor.sh b/scripts/doctor.sh index 5b97f37..52ac2e4 100755 --- a/scripts/doctor.sh +++ b/scripts/doctor.sh @@ -218,6 +218,23 @@ if [ -f "$AGENTS" ]; then fi fi +# ── 4c. AGENTS.md P6 retrieval-discipline reflex (advisory, backfillable) ──── +# The "/kg for discovery, never substrate grep" reflex was added to the §P6 +# template with the BRO-1426 reflex. A workspace bootstrapped before then +# legitimately lacks it (scaffold never overwrites an existing AGENTS.md), so — +# like §4b — this is informational only: never a GAP, never fails --strict. +# `bstack repair` backfills it. Marker phrase "substrate grep" matches repair's +# idempotency check and is wording-agnostic (catches "never"/"not a" variants). +section "4c. AGENTS.md retrieval-discipline reflex (advisory)" +if [ -f "$AGENTS" ]; then + if grep -qF "substrate grep" "$AGENTS"; then + ok "AGENTS.md has P6 retrieval-discipline reflex (/kg for discovery)" + else + echo " [info] AGENTS.md has no P6 retrieval-discipline reflex (/kg for discovery, never substrate grep)" + echo " → backfill: bstack repair (informational; not a gap, does not fail --strict)" + fi +fi + # ── 5. policy.yaml required blocks ────────────────────────────────────────── section "5. .control/policy.yaml blocks" POL="$WORKSPACE/.control/policy.yaml" diff --git a/scripts/repair.sh b/scripts/repair.sh index 4fec803..a96c3e4 100755 --- a/scripts/repair.sh +++ b/scripts/repair.sh @@ -212,6 +212,106 @@ backfill_philosophy_section() { echo " [fix] backfilled Development Philosophy into $target" } +# Backfill the §P6 "Retrieval discipline (/kg for discovery, never substrate +# grep)" reflex paragraph into an existing AGENTS.md that predates it (templated +# in the §P6 section since the BRO-1426 reflex shipped). Same idempotent-never- +# overwrite gap as the philosophy section: scaffold only creates whole files, so +# existing workspaces never receive newly-templated *content* — this closes that +# gap for this one paragraph. +# +# Idempotent + non-destructive: skips if the marker phrase is already present; +# skips with a warning if the §P6→next-heading insertion anchor is absent (never +# guesses a location). Extracts the paragraph verbatim from the template (the +# `**Retrieval discipline` line → next `### ` heading, exclusive) via files only +# — no shell interpolation of the content — so backticks/quotes/pipes survive. +backfill_retrieval_discipline() { + local target="$1" # AGENTS.md + local template="$2" # AGENTS.md.template + local tgt="$WORKSPACE_DIR/$target" + local tpl="$TEMPLATES_DIR/$template" + # Shared marker with doctor §4c so detection and backfill agree on "present". + # Match the coined phrase "substrate grep" (not the full "never substrate + # grep") so a workspace carrying the reflex under DIFFERENT wording — e.g. a + # hand-authored "not a substrate grep" variant — is still detected as present + # and NOT duplicated. "substrate grep" appears only in this reflex. + local marker="substrate grep" + [ -f "$tgt" ] || return # nothing to backfill into + [ -f "$tpl" ] || { echo " [skip] $target retrieval-discipline — template missing: $template"; return; } + grep -qF "$marker" "$tgt" && return # already present (idempotent) + if ! grep -qF "$marker" "$tpl"; then + echo " [skip] $target retrieval-discipline — template lacks the paragraph" + return + fi + # Anchor: the target must have a §P6 section followed by a later `### ` heading + # (the paragraph lands at the END of §P6, before whatever heading follows it). + if ! awk ' + function clean(s){ sub(/\r$/,"",s); return s } + { l=clean($0); if (l ~ /^### P6([ :]|$)/) p6=1; else if (p6 && l ~ /^### /) {found=1; exit} } + END { exit (found ? 0 : 1) } + ' "$tgt"; then + echo " [skip] $target retrieval-discipline — no §P6→heading anchor (insert manually)" + return + fi + if [ "$DRY_RUN" = "1" ]; then + echo " [dry-run] would backfill P6 retrieval-discipline into $target" + return + fi + if ! confirm "Backfill P6 retrieval-discipline reflex into $target?"; then + echo " [skip] $target retrieval-discipline (declined)" + return + fi + local secfile tmp + secfile="$(mktemp)" || { echo " [skip] $target retrieval-discipline — mktemp failed"; return; } + # Extract the paragraph: from the `**Retrieval discipline` line up to (excluding) + # the next `### ` heading. CR stripped first so awk delimiters match a CRLF template. + # Flag-clear on the boundary (NOT `exit`): under `set -o pipefail` an early awk + # exit closes the pipe and `tr` dies with SIGPIPE → the pipeline fails spuriously. + # Draining all of tr's output keeps the pipeline status clean. + if ! tr -d '\r' < "$tpl" | awk ' + /^\*\*Retrieval discipline/ { f=1 } + f && /^### / { f=0 } + f { print } + ' > "$secfile"; then + echo " [skip] $target retrieval-discipline — extraction pipeline failed" + rm -f "$secfile" + return + fi + if [ ! -s "$secfile" ] || ! grep -qF "$marker" "$secfile"; then + echo " [skip] $target retrieval-discipline — could not extract from $template" + rm -f "$secfile" + return + fi + tmp="$(mktemp)" || { echo " [skip] $target retrieval-discipline — mktemp failed"; rm -f "$secfile"; return; } + # Insert at the END of §P6 — before the first `### ` heading after `### P6`. + if ! awk -v secfile="$secfile" ' + function clean(s){ sub(/\r$/,"",s); return s } + { line = clean($0) } + seenP6 && line ~ /^### / && !done { + while ((getline s < secfile) > 0) print s + close(secfile) + done = 1 + } + { if (line ~ /^### P6([ :]|$)/) seenP6 = 1; print } + ' "$tgt" > "$tmp"; then + echo " [skip] $target retrieval-discipline — insertion failed (awk)" + rm -f "$secfile" "$tmp" + return + fi + rm -f "$secfile" + # Verify the paragraph actually landed before committing the write. + if ! grep -qF "$marker" "$tmp"; then + echo " [skip] $target retrieval-discipline — anchor not matched during insert (no change)" + rm -f "$tmp" + return + fi + if ! mv "$tmp" "$tgt"; then + echo " [skip] $target retrieval-discipline — could not write $target" + rm -f "$tmp" + return + fi + echo " [fix] backfilled P6 retrieval-discipline reflex into $target" +} + # ── Hook re-wire (helper) ────────────────────────────────────────────────── # Idempotently merges every hook in assets/templates/settings.json.snippet # into $WORKSPACE_DIR/.claude/settings.json. Existing entries are never @@ -362,6 +462,7 @@ deploy_workspace_hooks # GAP (so doctor still reports "fully bstack-compliant" without it). backfill_philosophy_section "AGENTS.md" "AGENTS.md.template" backfill_philosophy_section "CLAUDE.md" "CLAUDE.md.template" +backfill_retrieval_discipline "AGENTS.md" "AGENTS.md.template" if echo "$GAPS_OUTPUT" | grep -q "fully bstack-compliant"; then echo " ✓ no other gaps — workspace already bstack-compliant" diff --git a/tests/retrieval-discipline-backfill.test.sh b/tests/retrieval-discipline-backfill.test.sh new file mode 100755 index 0000000..93faeed --- /dev/null +++ b/tests/retrieval-discipline-backfill.test.sh @@ -0,0 +1,234 @@ +#!/usr/bin/env bash +# tests/retrieval-discipline-backfill.test.sh — fixture-based tests for the §P6 +# "/kg for discovery, never substrate grep" retrieval-discipline backfill in +# scripts/repair.sh and the advisory (§4c) in scripts/doctor.sh. +# +# Verifies: +# 1. A pre-BRO-1426 AGENTS.md (§P6 present, retrieval paragraph absent) gets +# the paragraph backfilled, positioned INSIDE §P6 (after ### P6, before ### P7). +# 2. Re-running is idempotent (exactly one occurrence; no duplicate). +# 3. --dry-run reports the backfill but writes nothing. +# 4. Missing §P6→heading anchor → skip with a warning, file unchanged. +# 5. doctor §4c surfaces the advisory when absent, ok when present — and the +# advisory is NOT a GAP / does NOT change the GAP total or --strict exit. +# 6. CRLF / trailing-space tolerant; section content extracted verbatim. +# +# Run from repo root: bash tests/retrieval-discipline-backfill.test.sh +set -uo pipefail + +BSTACK_REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +REPAIR_SH="$BSTACK_REPO/scripts/repair.sh" +DOCTOR_SH="$BSTACK_REPO/scripts/doctor.sh" +TEMPLATE="$BSTACK_REPO/assets/templates/AGENTS.md.template" + +PASS=0 +FAIL=0 +FAILED_TESTS=() + +assert_pass() { PASS=$((PASS + 1)); echo " ✓ $1"; } +assert_fail() { + FAIL=$((FAIL + 1)); FAILED_TESTS+=("$1") + echo " ✗ $1"; [ -n "${2:-}" ] && echo " ${2}" +} + +# Count occurrences of the shared marker phrase (matches repair + doctor). +refl_count() { grep -cF "substrate grep" "$1" 2>/dev/null || true; } + +# A workspace whose AGENTS.md has the Primitives anchor + §P6 and §P7 sections +# but NO retrieval-discipline paragraph (simulates a pre-BRO-1426 bootstrap). +# CLAUDE.md carries the anchor so the sibling philosophy backfill doesn't error. +fresh_pre1426_workspace() { + local ws; ws="$(mktemp -d)" + cat > "$ws/AGENTS.md" <<'EOF' +# demo — Agent Guidelines + +## Self-Meta Definition + +This file IS the control harness. + +## Bstack Core Automation Primitives + +### P6 — Bookkeeping: Knowledge Bookkeeping + +**What**: pipeline. + +**Reflexive Trigger Rule**: Bookkeeping is a reflex. + +**Never a question.** Capture is the default. + +### P7 — Freshness: Skill Freshness Check + +**What**: stale-install detector. +EOF + cat > "$ws/CLAUDE.md" <<'EOF' +# demo — bstack-governed workspace + +## Identity + +Governed by bstack. + +## Bstack Core Automation Primitives + +(table) +EOF + echo "$ws" +} + +echo "=== retrieval-discipline-backfill.test.sh ===" + +# Precondition: the template carries the paragraph (else the whole feature is moot). +if [ "$(refl_count "$TEMPLATE")" -ge 1 ]; then + assert_pass "template AGENTS.md.template carries the retrieval-discipline paragraph" +else + assert_fail "template is missing the retrieval-discipline paragraph (feature source gone)" +fi + +# ── Test 1 + 2: backfill + idempotency ────────────────────────────────────── +echo "" +echo "Test 1+2: backfill into pre-BRO-1426 workspace, then idempotency" +WS="$(fresh_pre1426_workspace)" + +[ "$(refl_count "$WS/AGENTS.md")" -eq 0 ] && assert_pass "precondition: AGENTS.md has no retrieval paragraph" \ + || assert_fail "precondition: AGENTS.md should start without the paragraph" + +BROOMVA_WORKSPACE="$WS" bash "$REPAIR_SH" --apply-all >/dev/null 2>&1 + +if [ "$(refl_count "$WS/AGENTS.md")" -eq 1 ]; then + assert_pass "AGENTS.md retrieval paragraph backfilled (exactly one)" +else + assert_fail "retrieval paragraph not backfilled (count=$(refl_count "$WS/AGENTS.md"))" +fi + +# Position: paragraph must land INSIDE §P6 — after ### P6, before ### P7. +refl_line="$(grep -nF "substrate grep" "$WS/AGENTS.md" | head -1 | cut -d: -f1)" +p6_line="$(grep -nE "^### P6" "$WS/AGENTS.md" | head -1 | cut -d: -f1)" +p7_line="$(grep -nE "^### P7" "$WS/AGENTS.md" | head -1 | cut -d: -f1)" +if [ -n "$refl_line" ] && [ "$refl_line" -gt "$p6_line" ] && [ "$refl_line" -lt "$p7_line" ]; then + assert_pass "paragraph positioned inside §P6 (P6=$p6_line < para=$refl_line < P7=$p7_line)" +else + assert_fail "paragraph not inside §P6 (P6=$p6_line para=$refl_line P7=$p7_line)" +fi + +# Content extracted verbatim (backticks / pipes / quotes intact). +if grep -qF 'never as the step that decides' "$WS/AGENTS.md"; then + assert_pass "paragraph content extracted verbatim (delimiters intact)" +else + assert_fail "paragraph content lost in extraction" +fi + +# Idempotency: second run must not duplicate. +BROOMVA_WORKSPACE="$WS" bash "$REPAIR_SH" --apply-all >/dev/null 2>&1 +if [ "$(refl_count "$WS/AGENTS.md")" -eq 1 ]; then + assert_pass "idempotent: re-run did not duplicate the paragraph" +else + assert_fail "idempotency broken (count=$(refl_count "$WS/AGENTS.md"))" +fi +rm -rf "$WS" + +# ── Test 3: --dry-run writes nothing ──────────────────────────────────────── +echo "" +echo "Test 3: --dry-run reports but does not write" +WS="$(fresh_pre1426_workspace)" +DRY_OUT="$(BROOMVA_WORKSPACE="$WS" bash "$REPAIR_SH" --dry-run 2>&1)" +if echo "$DRY_OUT" | grep -q "would backfill P6 retrieval-discipline into AGENTS.md"; then + assert_pass "--dry-run reports the backfill" +else + assert_fail "--dry-run did not report the backfill" +fi +if [ "$(refl_count "$WS/AGENTS.md")" -eq 0 ]; then + assert_pass "--dry-run wrote nothing" +else + assert_fail "--dry-run modified the file (count=$(refl_count "$WS/AGENTS.md"))" +fi +rm -rf "$WS" + +# ── Test 4: missing §P6 anchor → skip, file unchanged ─────────────────────── +echo "" +echo "Test 4: missing §P6 section → skip with warning" +WS="$(mktemp -d)" +printf '# demo\n\n## Bstack Core Automation Primitives\n\n(no P6 here)\n' > "$WS/AGENTS.md" +printf '# demo\n\n## Bstack Core Automation Primitives\n\n(table)\n' > "$WS/CLAUDE.md" +SKIP_OUT="$(BROOMVA_WORKSPACE="$WS" bash "$REPAIR_SH" --apply-all 2>&1)" +if echo "$SKIP_OUT" | grep -q "retrieval-discipline — no §P6→heading anchor"; then + assert_pass "missing §P6 anchor reported as skip" +else + assert_fail "missing-anchor skip not reported" +fi +if [ "$(refl_count "$WS/AGENTS.md")" -eq 0 ]; then + assert_pass "missing-anchor file left unchanged" +else + assert_fail "paragraph inserted despite missing anchor" +fi +rm -rf "$WS" + +# ── Test 5: doctor §4c advisory present/absent + 0 gaps / strict-neutral ───── +echo "" +echo "Test 5: doctor §4c advisory is informational (does not change GAP total / --strict)" +gap_lines() { grep -c "^ \[gap\]" 2>/dev/null || true; } +WS="$(mktemp -d)"; mkdir -p "$WS/.control" +cp "$BSTACK_REPO/assets/templates/policy.yaml.template" "$WS/.control/policy.yaml" 2>/dev/null || true +cp "$BSTACK_REPO/assets/templates/CLAUDE.md.template" "$WS/CLAUDE.md" 2>/dev/null || true + +# (a) WITHOUT the paragraph: strip it from the real template (para line → next ### , exclusive). +awk '/^\*\*Retrieval discipline/{skip=1} skip && /^### /{skip=0} !skip' \ + "$TEMPLATE" > "$WS/AGENTS.md" +DOC_WITHOUT="$(BROOMVA_WORKSPACE="$WS" bash "$DOCTOR_SH" 2>&1)" +BROOMVA_WORKSPACE="$WS" bash "$DOCTOR_SH" --strict >/dev/null 2>&1; STRICT_WITHOUT=$? +GAPS_WITHOUT="$(echo "$DOC_WITHOUT" | gap_lines)" + +if echo "$DOC_WITHOUT" | grep -q "no P6 retrieval-discipline reflex"; then + assert_pass "doctor surfaces §4c advisory when paragraph absent" +else + assert_fail "doctor did not surface the §4c advisory" +fi +if echo "$DOC_WITHOUT" | grep -qE "\[gap\].*(retrieval|substrate grep)"; then + assert_fail "advisory emitted as a GAP (should be informational)" +else + assert_pass "advisory is informational, not a [gap] line" +fi + +# (b) WITH the paragraph: identical workspace, real template (paragraph present). +cp "$TEMPLATE" "$WS/AGENTS.md" +DOC_WITH="$(BROOMVA_WORKSPACE="$WS" bash "$DOCTOR_SH" 2>&1)" +BROOMVA_WORKSPACE="$WS" bash "$DOCTOR_SH" --strict >/dev/null 2>&1; STRICT_WITH=$? +GAPS_WITH="$(echo "$DOC_WITH" | gap_lines)" + +if echo "$DOC_WITH" | grep -q "has P6 retrieval-discipline reflex"; then + assert_pass "doctor §4c reports ok when paragraph present" +else + assert_fail "doctor §4c did not report ok with paragraph present" +fi +if [ "$GAPS_WITHOUT" = "$GAPS_WITH" ]; then + assert_pass "GAP total identical with/without paragraph ($GAPS_WITH) — advisory adds 0 gaps" +else + assert_fail "advisory changed the GAP total (without=$GAPS_WITHOUT with=$GAPS_WITH)" +fi +if [ "$STRICT_WITHOUT" = "$STRICT_WITH" ]; then + assert_pass "--strict exit identical with/without paragraph (=$STRICT_WITH)" +else + assert_fail "--strict exit changed (without=$STRICT_WITHOUT with=$STRICT_WITH)" +fi +rm -rf "$WS" + +# ── Test 6: CRLF / trailing-space §P6 heading is matched ───────────────────── +echo "" +echo "Test 6: CRLF + trailing-space §P6/§P7 headings are matched" +WS="$(mktemp -d)" +printf '# demo\r\n\r\n## Bstack Core Automation Primitives\r\n\r\n### P6 — Bookkeeping \r\n\r\n**What**: x.\r\n\r\n### P7 — Freshness\r\n\r\n**What**: y.\r\n' > "$WS/AGENTS.md" +printf '# demo\n\n## Bstack Core Automation Primitives\n\n(table)\n' > "$WS/CLAUDE.md" +BROOMVA_WORKSPACE="$WS" bash "$REPAIR_SH" --apply-all >/dev/null 2>&1 +if [ "$(refl_count "$WS/AGENTS.md")" -eq 1 ]; then + assert_pass "CRLF/trailing-space §P6 heading matched — paragraph inserted" +else + assert_fail "CRLF/trailing-space §P6 NOT matched (count=$(refl_count "$WS/AGENTS.md"))" +fi +rm -rf "$WS" + +# ── Summary ───────────────────────────────────────────────────────────────── +echo "" +echo "=== results: $PASS passed, $FAIL failed ===" +if [ "$FAIL" -gt 0 ]; then + printf ' - %s\n' "${FAILED_TESTS[@]}" + exit 1 +fi +exit 0 From f9052f59d38c3fd6f0148b94b1d84da88dcdaa84 Mon Sep 17 00:00:00 2001 From: "Carlos D. Escobar-Valbuena" Date: Fri, 26 Jun 2026 21:00:57 -0500 Subject: [PATCH 2/3] fix(repair+doctor): semantic guard against duplicating a reworded reflex (P20 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cross-review [MAJOR]: the "substrate grep" phrase marker only guards variants that RETAIN the phrase; a reflex reworded to drop it (e.g. "never grep the substrate directly") was not detected → backfill appended a second paragraph. - repair.sh: add a structural guard — also treat `^**Retrieval discipline` as present (the reflex's bold-lead signature), so a phrase-dropping reword isn't duplicated. Comment scoped to the true guarantee. - doctor.sh §4c: match the same two signals (phrase OR `**Retrieval discipline` lead) so doctor + repair agree on "present" (doctor won't tell the user to run repair when repair would correctly skip). - test: +Test 4b — a reworded reflex (drops the phrase, keeps the lead) is not duplicated by --apply-all. 17 assertions, all green. BRO-1555 Co-Authored-By: Claude Opus 4.8 (1M context) --- scripts/doctor.sh | 8 +++--- scripts/repair.sh | 13 ++++++---- tests/retrieval-discipline-backfill.test.sh | 28 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/scripts/doctor.sh b/scripts/doctor.sh index 52ac2e4..ae95f02 100755 --- a/scripts/doctor.sh +++ b/scripts/doctor.sh @@ -223,11 +223,13 @@ fi # template with the BRO-1426 reflex. A workspace bootstrapped before then # legitimately lacks it (scaffold never overwrites an existing AGENTS.md), so — # like §4b — this is informational only: never a GAP, never fails --strict. -# `bstack repair` backfills it. Marker phrase "substrate grep" matches repair's -# idempotency check and is wording-agnostic (catches "never"/"not a" variants). +# `bstack repair` backfills it. Detection mirrors repair's two signals so the two +# agree on "present": the coined phrase "substrate grep" (catches the template + +# "never"/"not a" wording variants) OR the structural `**Retrieval discipline` +# lead (catches a reflex reworded to drop the phrase). section "4c. AGENTS.md retrieval-discipline reflex (advisory)" if [ -f "$AGENTS" ]; then - if grep -qF "substrate grep" "$AGENTS"; then + if grep -qF "substrate grep" "$AGENTS" || grep -qE "^\*\*Retrieval discipline" "$AGENTS"; then ok "AGENTS.md has P6 retrieval-discipline reflex (/kg for discovery)" else echo " [info] AGENTS.md has no P6 retrieval-discipline reflex (/kg for discovery, never substrate grep)" diff --git a/scripts/repair.sh b/scripts/repair.sh index a96c3e4..d212939 100755 --- a/scripts/repair.sh +++ b/scripts/repair.sh @@ -229,15 +229,18 @@ backfill_retrieval_discipline() { local template="$2" # AGENTS.md.template local tgt="$WORKSPACE_DIR/$target" local tpl="$TEMPLATES_DIR/$template" - # Shared marker with doctor §4c so detection and backfill agree on "present". - # Match the coined phrase "substrate grep" (not the full "never substrate - # grep") so a workspace carrying the reflex under DIFFERENT wording — e.g. a - # hand-authored "not a substrate grep" variant — is still detected as present - # and NOT duplicated. "substrate grep" appears only in this reflex. + # Shared phrase marker with doctor §4c so detection and backfill agree on + # "present". "substrate grep" is the coined phrase and appears only in this + # reflex; it catches the verbatim template paragraph AND wording variants that + # retain the phrase (e.g. a hand-authored "not a substrate grep"). local marker="substrate grep" [ -f "$tgt" ] || return # nothing to backfill into [ -f "$tpl" ] || { echo " [skip] $target retrieval-discipline — template missing: $template"; return; } grep -qF "$marker" "$tgt" && return # already present (idempotent) + # Semantic guard: also treat a reflex reworded to DROP the phrase as present + # (an additive backfill of a second paragraph is worse than a missed one). + # The `**Retrieval discipline` lead is the reflex's structural signature. + grep -qE "^\*\*Retrieval discipline" "$tgt" && return # present under different wording if ! grep -qF "$marker" "$tpl"; then echo " [skip] $target retrieval-discipline — template lacks the paragraph" return diff --git a/tests/retrieval-discipline-backfill.test.sh b/tests/retrieval-discipline-backfill.test.sh index 93faeed..99aad06 100755 --- a/tests/retrieval-discipline-backfill.test.sh +++ b/tests/retrieval-discipline-backfill.test.sh @@ -161,6 +161,34 @@ else fi rm -rf "$WS" +# ── Test 4b: reflex present under DIFFERENT wording → no duplication ───────── +echo "" +echo "Test 4b: a reworded reflex (drops the phrase, keeps the **Retrieval discipline lead) is not duplicated" +WS="$(fresh_pre1426_workspace)" +# Inject a reworded reflex into §P6 that OMITS the phrase "substrate grep" but +# carries the structural **Retrieval discipline lead. +awk ' + { print } + /^### P6 / && !done { + print "" + print "**Retrieval discipline.** Use `/kg` for discovery; never grep `research/entities/` directly." + done = 1 + } +' "$WS/AGENTS.md" > "$WS/AGENTS.md.tmp" && mv "$WS/AGENTS.md.tmp" "$WS/AGENTS.md" +lead_before="$(grep -cE '^\*\*Retrieval discipline' "$WS/AGENTS.md")" +BROOMVA_WORKSPACE="$WS" bash "$REPAIR_SH" --apply-all >/dev/null 2>&1 +lead_after="$(grep -cE '^\*\*Retrieval discipline' "$WS/AGENTS.md")" +if [ "$lead_before" -eq 1 ] && [ "$lead_after" -eq 1 ]; then + assert_pass "reworded reflex not duplicated (lead count stayed 1)" +else + assert_fail "reworded reflex duplicated (before=$lead_before after=$lead_after)" +fi +# doctor must report ok on the reworded variant (consistency with repair). +if BROOMVA_WORKSPACE="$WS" bash "$DOCTOR_SH" 2>&1 | grep -q "has P6 retrieval-discipline reflex"; then + : # need .control for doctor to run sections; skip strict check here +fi +rm -rf "$WS" + # ── Test 5: doctor §4c advisory present/absent + 0 gaps / strict-neutral ───── echo "" echo "Test 5: doctor §4c advisory is informational (does not change GAP total / --strict)" From 8d3ccece09620a7fda109c19f31c69be2754f2a6 Mon Sep 17 00:00:00 2001 From: "Carlos D. Escobar-Valbuena" Date: Fri, 26 Jun 2026 21:13:21 -0500 Subject: [PATCH 3/3] test(retrieval-backfill): real doctor assertion in Test 4b (CodeRabbit) + pipefail fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CodeRabbit: Test 4b's doctor-consistency check was a silent no-op (`: # skip`). Turned it into a real assertion that doctor §4c recognizes the reworded (phrase-less) reflex. Surfaced a test-harness bug: `doctor | grep -q` under `set -o pipefail` returns doctor's exit (non-zero on a gappy fixture), not grep's — capture the output to a var first, then grep (the pattern Test 5 uses). Verified non-vacuous by mutation: removing doctor's structural OR-clause makes Test 4b fail; restoring passes. 18/18 green. BRO-1555 Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/retrieval-discipline-backfill.test.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/retrieval-discipline-backfill.test.sh b/tests/retrieval-discipline-backfill.test.sh index 99aad06..cd7c4b2 100755 --- a/tests/retrieval-discipline-backfill.test.sh +++ b/tests/retrieval-discipline-backfill.test.sh @@ -183,9 +183,19 @@ if [ "$lead_before" -eq 1 ] && [ "$lead_after" -eq 1 ]; then else assert_fail "reworded reflex duplicated (before=$lead_before after=$lead_after)" fi -# doctor must report ok on the reworded variant (consistency with repair). -if BROOMVA_WORKSPACE="$WS" bash "$DOCTOR_SH" 2>&1 | grep -q "has P6 retrieval-discipline reflex"; then - : # need .control for doctor to run sections; skip strict check here +# doctor §4c must ALSO report ok on the reworded variant (drops the phrase, +# keeps the **Retrieval discipline lead) — proving doctor's structural signal +# agrees with repair's. Needs .control so doctor runs its sections. Capture the +# output to a var first: the bare fixture has real gaps so doctor exits non-zero, +# and `doctor | grep -q` under `set -o pipefail` would return doctor's exit, not +# grep's — a false failure. Grepping the captured string avoids that. +mkdir -p "$WS/.control" +cp "$BSTACK_REPO/assets/templates/policy.yaml.template" "$WS/.control/policy.yaml" 2>/dev/null || true +DOC_REWORDED="$(BROOMVA_WORKSPACE="$WS" bash "$DOCTOR_SH" 2>&1)" +if echo "$DOC_REWORDED" | grep -q "has P6 retrieval-discipline reflex"; then + assert_pass "doctor §4c treats the reworded (phrase-less) reflex as present" +else + assert_fail "doctor §4c did not recognize the reworded reflex (structural signal broken)" fi rm -rf "$WS"