#!/usr/bin/env bash
# bench/architect_sweeps.sh
#
# Run the five honesty/anchor sweep-invariant greps against the
# design/contracts (the honesty-bound contract surface; design/models
# is the narrative tier, out of honesty-sweep scope); the spine is
# design/INDEX.md. Output: matched lines, prefixed by sweep name.
# Exit 0 if all five sweeps are clean, 1 if any anchor was found,
# 2 if the spine (design/INDEX.md) is not found. Sweeps 1-4 are the
# history-anchor invariants; Sweep 5 is the docs-honesty-lint
# Wunschdenken / post-mortem invariant.
#
# Companion to skills/audit/agents/ailang-architect.md "What you check
# / design/ history-anchor regrowth". The rationale of each sweep
# regex (Sweeps 1–4: history anchors; Sweep 5: honesty / Wunschdenken)
# lives in the design-md-consolidation milestone commits (2026-05-10).
#
# A non-zero exit is advisory, not authoritative: the architect agent
# reviews each match and decides whether it is a legitimate quote
# (e.g. a block-quote citation of a commit body) or fresh drift.

set -u
INDEX="design/INDEX.md"
DESIGN_GLOB="design/contracts"  # contracts only — design/models is the narrative tier; "as of milestone N" context is legitimate there
HITS=0

if [[ ! -f "$INDEX" ]]; then
    echo "architect_sweeps: $INDEX not found (run from repo root)" >&2
    exit 2
fi

run_sweep() {
    local name="$1"
    local pattern="$2"
    local matches
    matches=$(grep -rnE "$pattern" $DESIGN_GLOB || true)
    if [[ -n "$matches" ]]; then
        echo "=== Sweep: $name ==="
        echo "$matches"
        echo
        HITS=1
    fi
}

run_sweep "1 (history anchors)" \
    'Iter [0-9]+[a-z]?(\.[0-9]+)?|Family [0-9]+|^[^/]*2026-[0-9]{2}-[0-9]{2}|\*\*Status: |pre-[0-9]+[a-z]?|[0-9]+[a-z]? sketch|21\.g'

run_sweep "2 (REVERTED + migration)" \
    'REVERTED|preserved for the audit trail|Migration plan|[Oo]riginally framed|original rationale|empirically-grounded version|A future (iter|iteration|milestone|Prelude) may|A future iter that|a (later|future) iter(ation)? \(deferred\)|Concrete design deferred'

run_sweep "3 (schema SoT)" \
    '^\s*(struct |enum |pub (struct|enum|fn))|whenever the two disagree|ast\.rs is the source of truth'

run_sweep "4 (workflow + stale cross-references)" \
    'agents/README\.md|ailang-docwriter|Recently lifted|see the JOURNAL queue|later the same day|Sharpened later|closure conversion in JOURNAL|see the per-iter journal|in a per-iter journal|docs/roadmap\.md'

run_sweep "5 (honesty: wunschdenken + non-citation post-mortem)" \
    'on the path to|may land in a future|ships in a future iter|[Ww]ill back `Show|[Ww]ill eventually (be|support|back)|[Ii]f/?when [a-z]+ (arrives|lands)|[Aa]n earlier draft of this|previously all diagnostics were|[Ww]hat did change in 20|were retired 20[0-9]{2}-|retired in iter [a-z]|originally listed|\bwas deferred\b|foundation for [a-z]'

if [[ $HITS -eq 0 ]]; then
    echo "All five sweeps clean."
fi
exit $HITS
