#!/usr/bin/env bash
# verify-site.sh — pre-ship gates for the Franken Research site.
#
# Run from anywhere:  ./scripts/verify-site.sh   (from site/)
#                     bash site/scripts/verify-site.sh
# Prints PASS/FAIL per gate and exits 0 only if every gate passes.
# What each gate checks and why is documented in ../BUILD-GATES.md.
set -u

SITE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# Canonical packets + Rulebook live at the repo root (one level above site/).
# Override with CANON=/path for a detached tree.
CANON="${CANON:-$(cd "$SITE_DIR/.." && pwd)}"
# Hand-written pages every structural gate scans (briefs are globbed on top).
# A listed page that does not exist fails the gates that open it.
# stack/index.html, rigor/index.html and every stack/<slug>.html are generated by
# scripts/make-stack.mjs (Gate K4 proves they match their sources); beyond/ is hand-written.
# study/independent-100/ (index, one page per person, one per deep dive) is generated by
# scripts/make-study.mjs (Gate N proves it matches its sources).
# follow/index.html is hand-written; the feed and OPML it links are generated (Gate M).
SITE_PAGES="index.html method/index.html failure-modes/index.html lessons/index.html
techniques/index.html reproduce/index.html starter-kit/index.html apply/index.html self/index.html
stack/index.html rigor/index.html beyond/index.html updates/index.html follow/index.html"
for _p in "$SITE_DIR"/stack/*.html; do
  case "$_p" in */stack/index.html|*'/stack/*.html') ;; *) SITE_PAGES="$SITE_PAGES stack/${_p##*/}" ;; esac
done
for _p in "$SITE_DIR"/study/independent-100/*.html "$SITE_DIR"/study/independent-100/deep/*.html; do
  [ -f "$_p" ] && SITE_PAGES="$SITE_PAGES ${_p#"$SITE_DIR"/}"
done
# The repository root: stack/, ecosystem/ and starter-kit/ live here (Gate K).
REPO_ROOT="$(cd "$SITE_DIR/.." && pwd)"
export SITE_DIR CANON SITE_PAGES REPO_ROOT

PASS=0
FAIL=0
FAILED_GATES=()

pass() { PASS=$((PASS+1)); echo "PASS  $1"; }
fail() {
  FAIL=$((FAIL+1)); echo "FAIL  $1"; FAILED_GATES+=("$1")
  if [ -n "${2:-}" ]; then echo "      $2"; fi
}

# Extract "<LABEL> <N>" from a gate's output (portable; BSD grep has no -P).
count_of() { printf '%s\n' "$1" | sed -n "s/^$2 \([0-9][0-9]*\).*/\1/p" | head -1; }

echo "Franken Research site gates — $SITE_DIR"
echo "----------------------------------------"

# ============ Gate A: packet + Rulebook byte integrity, method citations ============
echo "== A  packet/rulebook byte integrity =="
A_OK=1
A_DETAIL=""
# Repo layout keeps canonical packets in packets/; a flat canon dir holds them at its top.
CANON_PACKETS="$CANON"; [ -d "$CANON/packets" ] && CANON_PACKETS="$CANON/packets"
for f in "$CANON_PACKETS"/*-assessment.md; do
  base="$(basename "$f")"
  if [ ! -f "$SITE_DIR/packets/$base" ]; then
    A_OK=0; A_DETAIL="${A_DETAIL}missing:$base "
  elif ! cmp -s "$f" "$SITE_DIR/packets/$base"; then
    A_OK=0; A_DETAIL="${A_DETAIL}differs:$base "
  fi
done
for f in "$SITE_DIR"/packets/*.md; do
  base="$(basename "$f")"
  if [ ! -f "$CANON_PACKETS/$base" ]; then
    A_OK=0; A_DETAIL="${A_DETAIL}extra:$base "
  fi
done
if ! cmp -s "$CANON/RULEBOOK.md" "$SITE_DIR/RULEBOOK.md"; then
  A_OK=0; A_DETAIL="${A_DETAIL}RULEBOOK-differs "
fi
if [ $A_OK -eq 1 ]; then
  pass "A packet/rulebook byte-identical ($(ls "$SITE_DIR"/packets/*.md | wc -l | tr -d ' ') packets + RULEBOOK)"
else
  fail "A packet/rulebook byte-identical" "$A_DETAIL"
fi

echo "== A2 method-page claim citations =="
A2_OUT="$(python3 - <<'PYEOF'
import re, html as h, os, sys
site = os.environ['SITE_DIR']
def norm(s):
    s = h.unescape(s)
    s = re.sub(r'`([^`]*)`', r'\1', s)
    s = re.sub(r'\*\*([^*]*)\*\*', r'\1', s)
    s = re.sub(r'\[([^\]]*)\]\([^)]*\)', r'\1', s)
    s = re.sub(r'\s+', ' ', s).strip()
    return s
errs = []
try:
    t = open(site + '/method/index.html').read()
    texts = re.findall(r'<div class="cr-text">(.*?)</div>', t, re.S)
    srcs = re.findall(r'<span class="cr-src">(.*?)</span>', t)
    if not texts:
        errs.append('no .cr-text citations found')
    if len(texts) != len(srcs):
        errs.append('mismatched cr-text (%d) vs cr-src (%d)' % (len(texts), len(srcs)))
    for q, s in zip(texts, srcs):
        m = re.match(r'^([\w.\-]+\.md):(\d+)$', s.strip())
        if not m:
            errs.append('bad citation format: %r' % s); continue
        fn, ln = m.group(1), int(m.group(2))
        pp = site + '/packets/' + fn
        if not os.path.isfile(pp):
            errs.append('cited packet missing: %s' % fn); continue
        lines = open(pp).read().split('\n')
        if not (1 <= ln <= len(lines)):
            errs.append('%s:%d line out of range' % (fn, ln)); continue
        line = lines[ln - 1]
        if not re.match(r'^\|\s*\d+\s*\|', line):
            errs.append('%s:%d is not a claim-table row' % (fn, ln)); continue
        qq = norm(q).strip('"“”')
        if norm(qq) not in norm(line):
            errs.append('quote not in cited line %s:%d: %r' % (fn, ln, qq[:60]))
except Exception as e:
    errs.append('checker error: %s' % e)
print('CITATIONS_OK %d' % len(texts) if not errs else 'CITATIONS_BAD')
for e in errs:
    print('  ' + e)
PYEOF
)"
if echo "$A2_OUT" | head -1 | grep -q CITATIONS_OK; then
  pass "A2 method citations resolve ($(echo "$A2_OUT" | head -1 | grep -o '[0-9]*') claim rows)"
else
  fail "A2 method citations resolve" "$(echo "$A2_OUT" | tail -n +2 | head -4 | tr '\n' ';')"
fi

# ============ Gate B: statistics computed at render time ============
echo "== B  render-time statistics =="
B_OUT="$(python3 - <<'PYEOF'
import re, html as h, os, glob, sys

site = os.environ['SITE_DIR']
errs = []
src = open(site + '/assets/data.js').read()

# --- parse data.js: window.FRANKEN_DATA = { "framing": "...", "repos": [ {...}, ... ] } ---
rows = []
for m in re.finditer(r'\{\n\s*"name":\s*"([^"]+)",(.*?)\n\s*\}', src, re.S):
    body = m.group(0)
    g = lambda k: re.search(r'"' + k + r'":\s*"([^"]*)"', body)
    gi = lambda k: re.search(r'"' + k + r'":\s*(\d+)', body)
    try:
        rows.append({
            'name': m.group(1),
            'nodus': g('nodus').group(1),
            'licenseKey': g('licenseKey').group(1),
            'ciKey': g('ciKey').group(1),
            'trlLow': int(gi('trlLow').group(1)) if gi('trlLow') else None,
            'trlHigh': int(gi('trlHigh').group(1)) if gi('trlHigh') else None,
            'techNA': '"techNA": true' in body,
        })
    except AttributeError:
        pass
if not rows:
    errs.append('could not parse REPOS from data.js')

rings = {'Invest': 0, 'Pilot': 0, 'Explore': 0, 'Monitor': 0}
ci = {}
green_names = []
lic = {'rider': 0, 'mit': 0, 'none': 0}
trl_lo, trl_hi = 9, 2
for r in rows:
    rings[r['nodus']] = rings.get(r['nodus'], 0) + 1
    ci[r['ciKey']] = ci.get(r['ciKey'], 0) + 1
    lic[r['licenseKey']] = lic.get(r['licenseKey'], 0) + 1
    if r['ciKey'] == 'C1':
        green_names.append(r['name'])
    if not r['techNA'] and r['trlLow'] is not None:
        trl_lo = min(trl_lo, r['trlLow'])
        trl_hi = max(trl_hi, r['trlHigh'])
green_names.sort()
STATS = {
    'total': len(rows), 'greenCount': len(green_names),
    'notGreen': len(rows) - len(green_names),
    'riderCount': lic['rider'], 'mitCount': lic['mit'], 'noneCount': lic['none'],
    'noRiderCount': lic['mit'] + lic['none'],
    'ringInvest': rings['Invest'], 'ringPilot': rings['Pilot'],
    'ringExplore': rings['Explore'], 'ringMonitor': rings['Monitor'],
    'trlRange': '%d\u2013%d' % (trl_lo, trl_hi),
    'greenNames': green_names,
    'ciC1': ci.get('C1', 0), 'ciC2': ci.get('C2', 0), 'ciC3': ci.get('C3', 0),
    'ciC4': ci.get('C4', 0), 'ciC5': ci.get('C5', 0), 'ciC6': ci.get('C6', 0),
}

def clean(s):
    s = re.sub(r'<[^>]+>', '', s)
    return re.sub(r'\s+', ' ', h.unescape(s)).strip()

pages = os.environ['SITE_PAGES'].split() + sorted(glob.glob(site + '/briefs/*.html'))

slot_n = 0
for p in pages:
    rel = p if p.startswith(site) else site + '/' + p
    t = open(rel).read()
    for m in re.finditer(r'<([a-z0-9]+)[^>]*data-stat="([A-Za-z0-9]+)"[^>]*>(.*?)</\1>', t, re.S | re.I):
        key, fallback = m.group(2), clean(m.group(3))
        slot_n += 1
        if key not in STATS:
            errs.append('%s: unknown data-stat key %r' % (os.path.basename(p), key)); continue
        exp = STATS[key]
        if key == 'greenNames':
            got = set(re.findall(r'[a-z][a-z0-9_]*', fallback.lower())) - {'and'}
            if got != set(exp):
                errs.append('%s: data-stat greenNames fallback %r != %r' % (os.path.basename(p), sorted(got), exp))
        elif fallback != str(exp):
            errs.append('%s: data-stat %s fallback %r != computed %r' % (os.path.basename(p), key, fallback, str(exp)))

# framesentence fallback must equal FRANKEN_DATA.framing after slot substitution
import json as _json
fm = re.search(r'"framing":\s*"((?:[^"\\]|\\.)*)"', src)
if fm and os.path.isfile(site + '/index.html'):
    framing = _json.loads('"' + fm.group(1) + '"')
    t = open(site + '/index.html').read()
    sm = re.search(r'<span id="framesentence">(.*?)</span>\s*The <span data-stat="ringPilot"', t, re.S)
    if sm:
        inner = sm.group(1)
        inner = re.sub(r'<[a-z0-9]+[^>]*data-stat="([A-Za-z0-9]+)"[^>]*>.*?</[a-z0-9]+>',
                        lambda m: str(STATS.get(m.group(1), '?')), inner, flags=re.S | re.I)
        if clean(inner) != re.sub(r'\s+', ' ', framing).strip():
            errs.append('index.html framesentence fallback != data.js framing')
    else:
        errs.append('index.html: #framesentence not found')

# hardcoded-stat literal scan (bare N/44 etc. outside slots and STAT-annotated synthesis counts)
HARD = [
    (r'\b\d+\s+of\s+44\b', 'N of 44'),
    (r'\b\d+/44\b', 'N/44'),
    (r'\b(three|thirty[\s-]?four|seven|zero)\b.{0,24}\b(pilots?|explorations?|monitors?)\b', 'ring word count'),
    (r'\b(0|3|34|7)\b.{0,12}\b(pilots?|explorations?|monitors?)\b', 'ring numeral count'),
    (r'\ball 44 briefs\b', 'all-44 nav label'),
]
for p in pages:
    rel = p if p.startswith(site) else site + '/' + p
    t = open(rel).read()
    t = re.sub(r'<(script|style)[^>]*>.*?</\1>', '', t, flags=re.S | re.I)
    # blank out data-stat slot contents and STAT-annotated regions (400 chars after annotation)
    t = re.sub(r'<[a-z0-9]+[^>]*data-stat="[A-Za-z0-9]+"[^>]*>.*?</[a-z0-9]+>', ' ', t, flags=re.S | re.I)
    # STAT-annotated synthesis counts: blank the annotation AND the ~400 chars after it (the literal)
    t = re.sub(r'<!--\s*STAT:.*?-->.{0,400}', ' ', t, flags=re.S)
    txt = re.sub(r'<[^>]+>', ' ', t)
    txt = re.sub(r'\s+', ' ', h.unescape(txt))
    for pat, label in HARD:
        for m in re.finditer(pat, txt, re.I):
            errs.append('%s: hardcoded stat (%s): %r' % (
                rel.replace(site + '/', ''), label, txt[max(0, m.start()-30):m.end()+30].strip()))
if slot_n == 0:
    errs.insert(0, 'empty scan set: zero data-stat slots found (a gate that checks nothing is not a pass)')
print('SLOTS %d' % slot_n)
print('STATS_OK' if not errs else 'STATS_BAD')
for e in errs[:12]:
    print('  ' + e)
if len(errs) > 12:
    print('  ... and %d more' % (len(errs) - 12))
PYEOF
)"
B_SLOTS="$(count_of "$B_OUT" SLOTS)"
if echo "$B_OUT" | grep -q '^STATS_OK' && [ "${B_SLOTS:-0}" -gt 0 ]; then
  pass "B stats computed at render time ($B_SLOTS data-stat slots checked)"
else
  fail "B stats computed at render time" "$(echo "$B_OUT" | tail -n +3 | head -4 | tr '\n' ';')"
fi

# ============ Gate C: key terms defined on first use ============
echo "== C  definitions on first use =="
C_OUT="$(python3 - <<'PYEOF'
import re, html as h, os, glob

site = os.environ['SITE_DIR']
errs = []

TERMS = {
    'NODUS':      re.compile(r'NODUS'),
    'TRL':        re.compile(r'\bTRL\b'),
    'CI':         re.compile(r'\bCI\b'),
    'rider':      re.compile(r'\brider\b', re.I),
    'pin':        re.compile(r'\bpin\b|\bpinned\b', re.I),
    'bus factor': re.compile(r'bus factor', re.I),
}
# definitional phrases that must appear in a pre-first-use vocab block
DEF_PHRASE = {
    'NODUS': re.compile(r'four-ring verdict'),
    'TRL': re.compile(r'technology readiness'),
    'CI': re.compile(r'continuous integration'),
    'rider': re.compile(r'withholding all rights|barring the AI labs|license clause withholding|bars OpenAI and Anthropic'),
    'pin': re.compile(r'exact commit|single commit'),
    'bus factor': re.compile(r'how many people'),
}
# inline glosses accepted at first use (front door pattern)
GLOSS = {
    'NODUS': re.compile(r'\([^)]*(?:four|4)[^)]*assessment rings?[^)]*\)'),
    'TRL': re.compile(r'technology-readiness\s*\(TRL\)|technology readiness level'),
    'CI': re.compile(r'CI\s*\(continuous integration\)'),
    'rider': re.compile(r'AI-lab license rider'),
    'pin': re.compile(r'pinned?\s*(?:[A-Z][a-z]+\s+\d{1,2},\s+\d{4}\s*|[A-Z][a-z]+\s+\d{4}\s*)?\((?:the )?(?:single |exact )?assessed commit\)'),
    'bus factor': re.compile(r'bus factor.{0,60}\(one maintainer'),
}

pages = os.environ['SITE_PAGES'].split() + sorted(glob.glob(site + '/briefs/*.html'))

for p in pages:
    rel = p if p.startswith(site) else site + '/' + p
    short = rel.replace(site + '/', '')
    t = open(rel).read()
    t = re.sub(r'<(script|style)[^>]*>.*?</\1>', '', t, flags=re.S | re.I)
    t = re.sub(r'<!--.*?-->', '', t, flags=re.S)
    vm = re.search(r'<div class="vocab"[^>]*>.*?</div>', t, re.S)
    def vis(x):
        x = re.sub(r'<[^>]+>', ' ', x)
        return re.sub(r'\s+', ' ', h.unescape(x)).strip()
    full_txt = vis(t)
    vocab_txt = vis(vm.group(0)) if vm else ''
    vstart = -1
    search_txt = full_txt
    if vocab_txt:
        i = full_txt.find(vocab_txt[:40])
        if i >= 0:
            vstart = i
            search_txt = full_txt[:i] + ' ' * len(vocab_txt) + full_txt[i + len(vocab_txt):]
    for name, rx in TERMS.items():
        m = rx.search(search_txt)
        if not m:
            continue
        ok = False
        # (1) vocab block before first use, containing the definitional phrase
        if vstart >= 0 and vstart < m.start() and DEF_PHRASE[name].search(vocab_txt):
            ok = True
        # (2) inline gloss within 120 chars on either side of first use
        if not ok and GLOSS[name].search(search_txt[max(0, m.start()-120):m.start() + 120]):
            ok = True
        if not ok:
            errs.append('%s: %r used before definition: %r' % (
                short, name, search_txt[max(0, m.start()-40):m.start()+60].strip()))

print('DEFS_OK' if not errs else 'DEFS_BAD')
for e in errs[:12]:
    print('  ' + e)
if len(errs) > 12:
    print('  ... and %d more' % (len(errs) - 12))
PYEOF
)"
if echo "$C_OUT" | grep -q '^DEFS_OK'; then
  pass "C key terms defined on first use"
else
  fail "C key terms defined on first use" "$(echo "$C_OUT" | tail -n +2 | head -4 | tr '\n' ';')"
fi

# ============ Gate D: brief <-> packet integrity ============
echo "== D  brief/packet pairing =="
D_OUT="$(python3 - <<'PYEOF'
import re, os, glob
site = os.environ['SITE_DIR']
errs = []
briefs = sorted(glob.glob(site + '/briefs/*.html'))
for b in briefs:
    name = os.path.basename(b)[:-5]
    t = open(b).read()
    pkt = '../packets/%s-assessment.md' % name
    n = t.count('href="%s"' % pkt)
    if n != 1:
        errs.append('%s: packet link %r appears %d times (want 1)' % (name, pkt, n))
    # brief must name its packet file in the footer sources area
    if ('%s-assessment.md' % name) not in t:
        errs.append('%s: packet filename not cited in brief body' % name)
packets = sorted(glob.glob(site + '/packets/*-assessment.md'))
pnames = set(os.path.basename(p)[:-len('-assessment.md')] for p in packets)
bnames = set(os.path.basename(b)[:-5] for b in briefs)
for p in sorted(pnames - bnames):
    errs.append('packet without brief: %s' % p)
for b in sorted(bnames - pnames):
    errs.append('brief without packet: %s' % b)
# data.js names must map 1:1 onto both
src = open(site + '/assets/data.js').read()
dnames = set(re.findall(r'\{\s*"name":\s*"([^"]+)"', src))
if dnames != bnames:
    errs.append('data.js names != brief names: %r' % sorted(dnames ^ bnames)[:6])
print('PAIR_OK' if not errs else 'PAIR_BAD')
for e in errs[:10]:
    print('  ' + e)
PYEOF
)"
if echo "$D_OUT" | grep -q '^PAIR_OK'; then
  pass "D brief/packet 1:1 pairing ($(ls "$SITE_DIR"/briefs/*.html | wc -l | tr -d ' ') briefs)"
else
  fail "D brief/packet 1:1 pairing" "$(echo "$D_OUT" | tail -n +2 | head -4 | tr '\n' ';')"
fi

# ============ Gate E: internal link graph ============
echo "== E  internal links =="
E_OUT="$(python3 - <<'PYEOF'
import re, os, glob
from urllib.parse import unquote
site = os.environ['SITE_DIR']
errs = []
pages = os.environ['SITE_PAGES'].split() + sorted(glob.glob(site + '/briefs/*.html'))
SKIP_PREFIX = ('http://', 'https://', '//', 'mailto:', 'tel:', 'data:', 'javascript:')
checked = 0
for p in pages:
    rel = p if p.startswith(site) else site + '/' + p
    short = rel.replace(site + '/', '')
    t = open(rel).read()
    t = re.sub(r'<(script|style)[^>]*>.*?</\1>', ' ', t, flags=re.S | re.I)
    ids = set(re.findall(r'id="([^"]+)"', t))
    for attr in ('href', 'src'):
        for m in re.finditer(attr + r'="([^"]*)"', t):
            raw = m.group(1).strip()
            if not raw or raw.startswith(SKIP_PREFIX) or raw.startswith('#'):
                if raw.startswith('#') and raw[1:] not in ids and raw != '#':
                    errs.append('%s: fragment %r has no matching id' % (short, raw))
                continue
            if raw == '#':
                continue  # JS-owned placeholder (verified in gate G)
            path_q, frag = (raw.split('#', 1) + [None])[:2]
            path, query = (path_q.split('?', 1) + [None])[:2]
            # The only query a local link may carry is shell.mjs's content stamp on an asset, ?v=<10 hex>;
            # the stamp's value is checked by gate S. Anything else would be ignored on file:// and on Pages.
            if query is not None and not (re.fullmatch(r'v=[0-9a-f]{10}', query) and re.search(r'(^|/)assets/[^/]', path)):
                errs.append('%s: query on a local link: %r (only assets/*?v=<10 hex> is allowed)' % (short, raw)); continue
            url = unquote(path)
            base = os.path.dirname(rel)
            target = os.path.normpath(os.path.join(base, url))
            if not target.startswith(site):
                errs.append('%s: link escapes site root: %r' % (short, raw)); continue
            if os.path.isdir(target):
                target = os.path.join(target, 'index.html')
            if not os.path.isfile(target):
                errs.append('%s: broken %s %r' % (short, attr, raw)); continue
            checked += 1
            if frag and target.endswith('.html'):
                # index.html#repo=<name> is a map route, not an element id: the map opens the repo of
                # that name, and data.js names equal the brief names (gate D), so the brief must exist.
                route = re.fullmatch(r'repo=([^&]+)', frag)
                if route and target == os.path.join(site, 'index.html'):
                    if not os.path.isfile(os.path.join(site, 'briefs', unquote(route.group(1)) + '.html')):
                        errs.append('%s: map route %r names no brief' % (short, raw))
                    continue
                tt = open(target).read()
                tt = re.sub(r'<(script|style)[^>]*>.*?</\1>', ' ', tt, flags=re.S | re.I)
                if ('id="%s"' % frag) not in tt:
                    errs.append('%s: fragment %r missing in %s' % (short, raw, target.replace(site + '/', '')))
if checked == 0:
    errs.insert(0, 'empty scan set: zero relative links resolved (a gate that checks nothing is not a pass)')
print('LINKS %d ok' % checked)
print('LINKS_OK' if not errs else 'LINKS_BAD')
for e in errs[:12]:
    print('  ' + e)
if len(errs) > 12:
    print('  ... and %d more' % (len(errs) - 12))
PYEOF
)"
E_LINKS="$(count_of "$E_OUT" LINKS)"
if echo "$E_OUT" | grep -q '^LINKS_OK' && [ "${E_LINKS:-0}" -gt 0 ]; then
  pass "E internal links resolve ($E_LINKS links)"
else
  fail "E internal links resolve" "$(echo "$E_OUT" | tail -n +3 | head -4 | tr '\n' ';')"
fi

# ============ Gate F: method-animation artifacts shipped ============
echo "== F  method artifacts =="
F_OK=1; F_DETAIL=""
# every cr-src citation target exists (covered in A2) + every station link on method page
F_OUT="$(python3 - <<'PYEOF'
import re, os
site = os.environ['SITE_DIR']
t = open(site + '/method/index.html').read()
hrefs = set(re.findall(r'href="(\.\./starter-kit/[^"]+)"', t))
errs = []
for h in sorted(hrefs):
    if not os.path.isfile(os.path.normpath(os.path.join(site + '/method', h))):
        errs.append('station link missing: %s' % h)
# every local asset referenced by any page must exist (css/js/img/svg/fonts)
import glob
for p in (os.environ['SITE_PAGES'].split() + sorted(glob.glob(site + '/briefs/*.html'))):
    rel = p if p.startswith(site) else site + '/' + p
    tt = open(rel).read()
    for m in re.finditer(r'(?:src|href)="((?:\.\./|\./)?assets/[^"]+|favicon\.svg|\.\./favicon\.svg)"', tt):
        ref = m.group(1)
        # shell.mjs stamps loaded assets with ?v=<10 hex> (gate S checks the value); resolve the file
        # without it. Any other query or fragment is not stripped, so it fails as a missing file.
        path = re.sub(r'\?v=[0-9a-f]{10}$', '', ref)
        a = os.path.normpath(os.path.join(os.path.dirname(rel), path))
        if not os.path.isfile(a):
            errs.append('%s: asset missing: %s' % (os.path.basename(p), ref))
print('ART_OK' if not errs else 'ART_BAD')
for e in errs[:10]:
    print('  ' + e)
PYEOF
)"
if echo "$F_OUT" | grep -q '^ART_OK'; then
  pass "F method artifacts + local assets exist"
else
  fail "F method artifacts + local assets exist" "$(echo "$F_OUT" | tail -n +2 | head -4 | tr '\n' ';')"
fi

# ============ Gate G: keyboard operability + no-JS/no-WebGL/reduced-motion ============
echo "== G  keyboard + fallbacks =="
G_OUT="$(python3 - <<'PYEOF'
import re, os, glob
site = os.environ['SITE_DIR']
errs = []
# G1: every page has a skip link or the front door's equivalent landmark nav
for p in (os.environ['SITE_PAGES'].split() + sorted(glob.glob(site + '/briefs/*.html'))):
    rel = p if p.startswith(site) else site + '/' + p
    short = rel.replace(site + '/', '')
    t = open(rel).read()
    body = t.split('<body', 1)[1] if '<body' in t else t
    # interactive elements must be natively focusable or have tabindex/key handlers
    for m in re.finditer(r'<(div|span)[^>]*(?:onclick|role="button")[^>]*>', body):
        tag = m.group(0)
        if 'tabindex' not in tag and '<' not in tag:
            errs.append('%s: clickable %s without tabindex' % (short, m.group(1)))
    # details/summary disclosure widgets are keyboard-native; confirm no JS-only disclosure
    # G2: reduced-motion respected somewhere global
# G2: each page's CSS/JS must handle prefers-reduced-motion (or be a static page)
for p in (['index.html', 'method/index.html'] + sorted(glob.glob(site + '/briefs/*.html'))):
    rel = p if p.startswith(site) else site + '/' + p
    short = rel.replace(site + '/', '')
    t = open(rel).read()
    if 'prefers-reduced-motion' not in t:
        errs.append('%s: no prefers-reduced-motion handling' % short)
# G3: front door works without WebGL/JS: framesentence + stats fallbacks present, table view exists
t = open(site + '/index.html').read()
for needle in ['id="framesentence"', 'id="tableview"', 'id="stats"', '<noscript']:
    if needle not in t:
        errs.append('index.html: missing no-JS/no-WebGL fallback marker %s' % needle)
# G4: method page works without JS: claim rows are static HTML, animation is enhancement
t = open(site + '/method/index.html').read()
if t.count('class="claim-row') < 12:
    errs.append('method/index.html: claim rows not statically rendered (<12)')
if 'prefers-reduced-motion' not in t:
    errs.append('method/index.html: no prefers-reduced-motion handling')
print('A11Y_OK' if not errs else 'A11Y_BAD')
for e in errs[:10]:
    print('  ' + e)
PYEOF
)"
if echo "$G_OUT" | grep -q '^A11Y_OK'; then
  pass "G keyboard operability + fallbacks"
else
  fail "G keyboard operability + fallbacks" "$(echo "$G_OUT" | tail -n +2 | head -4 | tr '\n' ';')"
fi

# ============ Gate H: copy-quality slop scan ============
echo "== H  slop scan =="
H_OUT="$(python3 - <<'PYEOF'
import re, html as h, glob, os
site = os.environ['SITE_DIR']
fails, notes = [], []
pages = os.environ['SITE_PAGES'].split() + sorted(glob.glob(site + '/briefs/*.html'))
for p in pages:
    short = p.replace(site + '/', '')
    t = open(p if p.startswith(site) else site + '/' + p).read()
    t = re.sub(r'<(script|style)[^>]*>.*?</\1>', '', t, flags=re.S | re.I)
    t = re.sub(r'<!--.*?-->', '', t, flags=re.S)
    # em-dash: exclude UI/list separators (instance rows, list items, summaries)
    prose_lines = [ln for ln in t.split('\n')
                   if not re.search(r'<(li|summary)[^>]*>|class="repo"|class="count"', ln)]
    prose = h.unescape(re.sub(r'<[^>]+>', ' ', '\n'.join(prose_lines)))
    nd = prose.count('\u2014')
    if nd > 30:
        fails.append('%s: %d non-separator em-dashes (limit 30)' % (short, nd))
    txt = h.unescape(re.sub(r'<[^>]+>', ' ', t))
    for w in ['delve', 'tapestry']:
        for m in re.finditer(r'\b%s\b' % w, txt, re.I):
            fails.append('%s: filler %r: %r' % (short, w, txt[max(0,m.start()-40):m.end()+40].strip()))
    for w in ['unprecedented', 'revolutionary', 'game-changing']:
        for m in re.finditer(w, txt, re.I):
            fails.append('%s: unsupported superlative %r: %r' % (short, w, txt[max(0,m.start()-40):m.end()+40].strip()))
    for w in ['honestly', 'genuinely', 'landscape']:
        for m in re.finditer(r'\b%s\b' % w, txt, re.I):
            notes.append('%s: filler? %r: %r' % (short, w, txt[max(0,m.start()-40):m.end()+40].strip()))
    for w in ['best', 'most widely used', 'world-class', 'cutting-edge', 'state-of-the-art']:
        for m in re.finditer(w, txt, re.I):
            notes.append('%s: superlative? %r: %r' % (short, w, txt[max(0,m.start()-50):m.end()+50].strip()))
print('SLOP_OK' if not fails else 'SLOP_BAD')
for f in fails[:10]:
    print('  FAIL ' + f)
for n in notes[:8]:
    print('  note ' + n)
if len(notes) > 8:
    print('  note ... and %d more review notes' % (len(notes) - 8))
PYEOF
)"
if echo "$H_OUT" | grep -q '^SLOP_OK'; then
  pass "H slop scan"
  echo "$H_OUT" | grep '^  note' | head -8 | sed 's/^/      /'
else
  fail "H slop scan" "$(echo "$H_OUT" | grep '^  FAIL' | head -4 | tr '\n' ';')"
fi

# ============ Gate I: headless render (CDP) ============
# Loads pages via file:// (the site is designed to run straight from the ZIP
# with no server) in headless Chromium, checking for console errors and
# horizontal overflow at desktop and phone widths.
echo "== I  headless render =="
I_TMP="$(mktemp -d)"
cat > "$I_TMP/render.mjs" <<'MEOF'
import { spawn, execSync } from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';

const SITE = process.env.SITE_DIR;
// CHROME_PATH wins; otherwise the first installed Chromium-family browser.
// No browser found is a FAIL, never a skip: an unrun render gate is not a pass.
const CHROME = process.env.CHROME_PATH || [
  '/opt/meta-chromium/chrome',
  '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
  '/Applications/Chromium.app/Contents/MacOS/Chromium',
  '/usr/bin/google-chrome', '/usr/bin/google-chrome-stable',
  '/usr/bin/chromium', '/usr/bin/chromium-browser',
].find((p) => fs.existsSync(p));
if (!CHROME) { console.log('RENDER_BAD'); console.log('  no Chromium found; set CHROME_PATH'); process.exit(1); }
// One generated verdict page (the first by name) stands in for all of them: they share one template.
const SLUG = (fs.existsSync(SITE + '/stack') ? fs.readdirSync(SITE + '/stack') : [])
  .filter((f) => f !== 'index.html' && f.endsWith('.html')).sort()[0];
if (!SLUG) { console.log('RENDER_BAD'); console.log('  no generated stack/<slug>.html to render'); process.exit(1); }
const PAGES = ['/index.html', '/apply/index.html', '/starter-kit/index.html', '/method/index.html', '/briefs/asupersync.html', '/lessons/index.html', '/self/index.html',
  '/stack/index.html', '/stack/' + SLUG, '/rigor/index.html', '/beyond/index.html', '/updates/index.html', '/follow/index.html',
  '/study/independent-100/index.html'];
const VIEWPORTS = [[1440, 900], [390, 844]];

// file:// loading: the site is designed to run straight from the ZIP with no
// server, so the render gate tests exactly that. (An http://127.0.0.1 test
// server is unusable here: Chrome 152's local-network access checks block
// CDP-initiated navigation to loopback.)
// Chrome can take well over 15 s to start on a loaded CI runner. Wait up to 45 s for the
// DevTools port, and relaunch once (fresh profile) before failing. Harness robustness only:
// what the gate asserts about the pages is unchanged.
const DEVTOOLS_WAIT_MS = 45000;
let chrome = null, prof = null, dbgPort = null;
function launch() {
  prof = fs.mkdtempSync(path.join(os.tmpdir(), 'chrome-prof-'));
  chrome = spawn(CHROME, [
    // Software WebGL (SwiftShader) so the gate renders the real 3D path; current
    // Chrome refuses the software fallback unless it is enabled explicitly.
    '--headless=new', '--no-sandbox', '--use-angle=swiftshader', '--enable-unsafe-swiftshader',
    '--allow-file-access-from-files',
    '--remote-debugging-port=0', `--user-data-dir=${prof}`,
    '--no-first-run', '--disable-extensions', 'about:blank',
  ], { stdio: ['ignore', 'pipe', 'pipe'] });
  return new Promise((resolve, reject) => {
    const t = setTimeout(() => reject(new Error(`no devtools port after ${DEVTOOLS_WAIT_MS / 1000}s`)), DEVTOOLS_WAIT_MS);
    chrome.stderr.on('data', d => {
      const m = /DevTools listening on ws:\/\/127\.0\.0\.1:(\d+)/.exec(d.toString());
      if (m) { dbgPort = m[1]; clearTimeout(t); resolve(); }
    });
    chrome.on('exit', () => { clearTimeout(t); reject(new Error('chrome exited')); });
  });
}
for (let attempt = 1; ; attempt++) {
  try { await launch(); break; } catch (e) {
    if (chrome.exitCode === null && chrome.signalCode === null) {
      await new Promise((r) => { chrome.once('exit', r); chrome.kill('SIGKILL'); setTimeout(r, 5000); });
    }
    try { fs.rmSync(prof, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }); } catch {}
    if (attempt >= 2) { console.log('RENDER_BAD'); console.log(`  chrome launch failed twice: ${e.message}`); process.exit(1); }
    console.error(`chrome launch attempt ${attempt} failed (${e.message}); retrying once`);
  }
}

const tabs = JSON.parse(execSync(`curl -s http://127.0.0.1:${dbgPort}/json/list`).toString());
// Desktop Chrome lists built-in extension background pages first; drive the tab.
const tab = tabs.find((t) => t.type === 'page');
if (!tab) { console.log('RENDER_BAD'); console.log('  no page target in ' + JSON.stringify(tabs.map((t) => t.type))); process.exit(1); }
const wsUrl = tab.webSocketDebuggerUrl;
const ws = new WebSocket(wsUrl);
await new Promise((r, rej) => { ws.onopen = r; ws.onerror = rej; });

let id = 0;
const pending = new Map();
let consoleEvents = [];
ws.onmessage = e => {
  const m = JSON.parse(e.data);
  if (m.id && pending.has(m.id)) { pending.get(m.id)(m); pending.delete(m.id); }
  else if (m.method === 'Runtime.consoleAPICalled') consoleEvents.push(m);
  else if (m.method === 'Runtime.exceptionThrown') consoleEvents.push(m);
  else if (m.method === 'Log.entryAdded') consoleEvents.push(m);
};
const send = (method, params = {}) => new Promise(res => {
  const myId = ++id;
  pending.set(myId, res);
  ws.send(JSON.stringify({ id: myId, method, params }));
});
const errors = [];
const sleep = ms => new Promise(r => setTimeout(r, ms));

await send('Runtime.enable');
await send('Log.enable');
await send('Page.enable');

for (const page of PAGES) {
  for (const vp of VIEWPORTS) {
    consoleEvents = [];
    await send('Emulation.setDeviceMetricsOverride', {
      width: vp[0], height: vp[1], deviceScaleFactor: 1, mobile: vp[0] < 500 });
    const targetUrl = `file://${SITE}${page}`;
    const navigation = await send('Page.navigate', { url: targetUrl });
    if (navigation.error || navigation.result?.errorText) {
      errors.push(`${page}: navigation failed: ${JSON.stringify(navigation.error || navigation.result.errorText)}`);
      continue;
    }
    // A fixed delay sampled partially parsed HTML on a hosted runner (PR19,
    // run36609034471). Wait for this document and its styles; do not navigate
    // again or discard console errors. A bad/missing page still fails below.
    const deadline = Date.now() + 20000;
    let ready = false;
    while (Date.now() < deadline) {
      const loaded = await send('Runtime.evaluate', { returnByValue: true, expression: `(() =>
        location.href === ${JSON.stringify(targetUrl)} && document.readyState === 'complete' &&
        [...document.querySelectorAll('link[rel="stylesheet"]')].every(l => {
          try { return !!l.sheet && l.sheet.cssRules.length > 0; } catch { return false; }
        }))()` });
      if (loaded.result?.result?.value === true) { ready = true; break; }
      await sleep(100);
    }
    if (!ready) errors.push(`${page}: document/styles did not finish loading within 20 seconds`);
    await sleep(3000); // preserve the existing post-load timer/animation observation
    const ev = await send('Runtime.evaluate', { returnByValue: true, expression: `(() => ({
      overflowX: document.documentElement.scrollWidth - document.documentElement.clientWidth,
      blank: document.body ? document.body.innerText.trim().length : 0,
      title: document.title,
      // Local assets are loaded as assets/<file>?v=<hash> (gate S). From file:// the query must not stop
      // them loading: every stylesheet from assets/ has readable rules (a sheet that failed to load still
      // exists, but reading its rules throws), and data.js defined FRANKEN_DATA.
      stamped: document.querySelectorAll('link[rel="stylesheet"][href*="assets/"][href*="?v="], script[src*="assets/"][src*="?v="]').length,
      unloadedCss: [...document.querySelectorAll('link[rel="stylesheet"][href*="assets/"]')].filter((l) => {
        try { return !l.sheet || !l.sheet.cssRules.length; } catch (e) { return true; }
      }).map((l) => l.getAttribute('href')),
      dataMissing: !!document.querySelector('script[src*="assets/data.js"]') && typeof window.FRANKEN_DATA !== 'object'
    }))()` });
    const v = ev.result.result.value;
    const label = `${page} @${vp[0]}x${vp[1]}`;
    if (!v || !v.title) { errors.push(`${label}: navigation failed`); continue; }
    if (v.overflowX > 1) errors.push(`${label}: horizontal overflow ${v.overflowX}px`);
    if (v.blank < 200) errors.push(`${label}: page looks blank (${v.blank} chars)`);
    if (!v.stamped) errors.push(`${label}: loads no stamped asset (want assets/*?v=<hash>; run bun run build:shell)`);
    for (const h of v.unloadedCss) errors.push(`${label}: stylesheet did not load from file://: ${h}`);
    if (v.dataMissing) errors.push(`${label}: assets/data.js did not load from file:// (window.FRANKEN_DATA undefined)`);
    for (const e of consoleEvents) {
      const txt = JSON.stringify(e.params || e).slice(0, 300);
      // ignore known-harmless software-WebGL deprecation noise from headless Chromium
      if (/GroupMarkerNotSet|WebGL.*software|SwiftShader/i.test(txt)) continue;
      if (e.method === 'Runtime.consoleAPICalled' && ['error'].includes(e.params.type)) errors.push(`${label}: console.error ${txt}`);
      else if (e.method === 'Runtime.exceptionThrown') errors.push(`${label}: exception ${txt}`);
      else if (e.method === 'Log.entryAdded' && ['error'].includes(e.params.entry.level)) errors.push(`${label}: log.error ${txt}`);
    }
  }
}
ws.close();
// Wait for Chrome to exit before deleting its profile: deleting while it still
// writes raced (ENOTEMPTY) and crashed the gate after a clean render.
await new Promise((r) => { chrome.once('exit', r); chrome.kill(); setTimeout(r, 5000); });
try { fs.rmSync(prof, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }); } catch {}
if (errors.length) { console.log('RENDER_BAD'); errors.slice(0, 12).forEach(e => console.log('  ' + e)); process.exit(1); }
console.log(`RENDER_OK ${PAGES.length}x${VIEWPORTS.length} page-views, zero console errors, zero overflow`);
MEOF
if node "$I_TMP/render.mjs" > "$I_TMP/render.out" 2> "$I_TMP/render.err"; then
  pass "I headless render ($(cat "$I_TMP/render.out" | head -1 | sed 's/RENDER_OK //'))"
else
  fail "I headless render" "$(head -6 "$I_TMP/render.out" "$I_TMP/render.err" | tr '\n' ';' | head -c 600)"
fi
rm -rf "$I_TMP"

# ============ Gate J: no deleted-path references ============
echo "== J  deleted paths =="
J_BAD="$(cd "$SITE_DIR" && grep -rln --include='*.html' --include='*.js' -E 'reader-template|/old/|/drafts/|lorem ipsum|\.bak|TODO\.html' . | grep -v '^\./scripts/' || true)"
if [ -z "$J_BAD" ]; then
  pass "J no deleted-path references"
else
  fail "J no deleted-path references" "$(echo "$J_BAD" | tr '\n' ' ')"
fi

# ============ Gate K: agent stack evidence ============
# K1 verdict files complete and independently reviewed, K2 quoted citations resolve,
# K3 rigor-practices.tsv sources and proofs, K4 generated pages equal a fresh run.
# Rules: ../../stack/METHOD.md. Details: ../BUILD-GATES.md.
echo "== K  agent stack evidence =="
K_OUT="$(python3 - <<'PYEOF'
import re, os, subprocess
root = os.environ['REPO_ROOT']
TIERS = ['Verified', 'CI-observed', 'Maintainer claim', 'External', 'Inference']
GROUPS = ['Model serving', 'Orchestration', 'Tools and environment', 'Memory and retrieval',
          'Eval and safety', 'Training and voice']
VERDICTS = ['Adopt', 'Adopt and wrap', 'Build clean-room', 'Watch']
CONF = ['High', 'Medium', 'Low']
KEYS = ['type', 'title', 'group', 'verdict', 'confidence', 'evidence_date', 'author', 'reviewed_by', 'review_date']
SECTIONS = ['Bottom line', 'Adopt, do not rebuild', 'Copy these practices', 'Build only if',
            'Where FrankenSuite touches this', 'What we cannot say', 'Revisit when']
FACTUAL = SECTIONS[1:5]
QUOTED = FACTUAL + ['What we cannot say']
# <path with extension>:<line>[-<line>] "<quote>"; same shape make-stack.mjs renders.
CITE = re.compile(r'(?<![\w/.:#-])((?:[\w.-]+/)*[\w-][\w.-]*\.[A-Za-z][A-Za-z0-9]*):(\d+)(?:[-\u2013](\d+))?(?!\w)(?:\s+"([^"\n]*)")?')
# References inside an our_proof token: "commit <sha>" / a sha, or a repo path (has a "/" or a known
# extension) with an optional :line. URLs are skipped by the look-behind. make-stack.mjs links the same shapes.
PROOF_REF = re.compile(r'(commit\s+)?\b([0-9a-f]{7,40})\b|(?<![\w/:.-])((?:\.?[\w-][\w.-]*/)+[\w.-]*[\w-]|[\w-][\w.-]*\.(?:md|sh|yml|yaml|tsv|json|mjs|js|py|html|toml|txt))(?::(\d+))?')
TIER_OK = re.compile(r'\[(?:' + '|'.join(re.escape(t) for t in TIERS) + r')(?:[,;][^\]\n]*)?\]')
TIERISH = re.compile(r'(?i)^T\d|verified|observed|claim|external|inference|counted|absence')
DATE = re.compile(r'^\d{4}-\d{2}-\d{2}$')
out = {'K1': [], 'K2': [], 'K3': []}
stats = {'files': 0, 'cites': 0, 'rows': 0}

def norm(s):
    return re.sub(r'\s+', ' ', re.sub(r'[*`]', '', s)).strip()

_cache = {}
def lines_of(p):
    if p.startswith('/') or '..' in p.split('/'):
        return None
    full = os.path.join(root, p)
    if not os.path.isfile(full):
        return None
    if p not in _cache:
        _cache[p] = open(full, encoding='utf-8', errors='replace').read().split('\n')
    return _cache[p]

def check_cite(p, a, b, quote, need_quote):
    """Errors for one citation: file, line(s), non-empty, and the quote on those lines."""
    ls = lines_of(p)
    ref = '%s:%s%s' % (p, a, '-%s' % b if b != a else '')
    if ls is None:
        return ['%s: file not in repo' % ref]
    if a < 1 or b < a or b > len(ls):
        return ['%s: line out of range (file has %d lines)' % (ref, len(ls))]
    if not ls[a - 1].strip() or not ls[b - 1].strip():
        return ['%s: cited line is empty' % ref]
    if quote is None:
        return ['%s: citation has no quote' % ref] if need_quote else []
    q = norm(quote)
    if len(q) < 20:
        return ['%s: quote shorter than 20 characters: "%s"' % (ref, q)]
    if q not in norm(' '.join(ls[a - 1:b])):
        return ['%s: quote not on the cited line: "%s"' % (ref, q[:70])]
    return []

slugs = sorted(f[7:-3] for f in os.listdir(os.path.join(root, 'ecosystem/pickup'))
               if f.startswith('pickup-') and f.endswith('.md'))
if not slugs:
    out['K1'].append('empty scan set: no ecosystem/pickup/pickup-*.md types found')
present = sorted(f[:-3] for f in os.listdir(os.path.join(root, 'stack')) if f.endswith('.md') and f != 'METHOD.md')
for extra in sorted(set(present) - set(slugs)):
    out['K1'].append('stack/%s.md: no matching ecosystem/pickup/pickup-%s.md type' % (extra, extra))

for slug in slugs:
    rel = 'stack/%s.md' % slug
    k1, k2 = [], []
    if not os.path.isfile(os.path.join(root, rel)):
        out['K1'].append('%s: missing' % rel)
        continue
    stats['files'] += 1
    src = open(os.path.join(root, rel), encoding='utf-8').read()
    fm, body = {}, src
    m = re.match(r'---\n(.*?)\n---\n?', src, re.S)
    if not m:
        k1.append('no front matter')
    else:
        for l in m.group(1).split('\n'):
            km = re.match(r'([A-Za-z_]+):\s*(.*)$', l)
            if km:
                fm[km.group(1)] = km.group(2).strip()
        body = src[m.end():]
    for k in KEYS:
        if not fm.get(k):
            k1.append('%s missing' % k)
    if fm.get('type') and fm['type'] != slug:
        k1.append('type %r != file slug' % fm['type'])
    if fm.get('group') and fm['group'] not in GROUPS:
        k1.append('group %r not one of the six' % fm['group'])
    if fm.get('verdict') and fm['verdict'] not in VERDICTS:
        k1.append('verdict %r not in the four-value set' % fm['verdict'])
    if fm.get('confidence') and fm['confidence'] not in CONF:
        k1.append('confidence %r not High/Medium/Low' % fm['confidence'])
    for k in ('evidence_date', 'review_date'):
        if fm.get(k) and not DATE.match(fm[k]):
            k1.append('%s %r is not YYYY-MM-DD' % (k, fm[k]))
    if fm.get('reviewed_by') and fm['reviewed_by'] == fm.get('author'):
        k1.append('reviewed_by equals author (%s)' % fm['author'])
    secs, cur = {}, None
    for l in body.split('\n'):
        hm = re.match(r'## (.+?)\s*$', l)
        if hm:
            cur = hm.group(1)
            secs[cur] = []
        elif cur is not None:
            secs[cur].append(l)
    for s in SECTIONS:
        if s not in secs:
            k1.append('section "## %s" missing' % s)
    if 'What we cannot say' in secs and not '\n'.join(secs['What we cannot say']).strip():
        k1.append('"What we cannot say" is empty')
    bl = '\n'.join(secs.get('Bottom line', [])).strip()
    if 'Bottom line' in secs and not re.sub(r'^[*_\s]+', '', bl).startswith('Inference'):
        k1.append('Bottom line does not start with "Inference"')
    rej = fm.get('rejected_alternative', '')
    if fm.get('verdict') == 'Build clean-room':
        if not rej:
            k1.append('Build clean-room without rejected_alternative')
        elif not CITE.search(rej):
            k1.append('rejected_alternative has no citation')
    # K2: every citation in the file, front matter included.
    cited = []
    def scan(text, need_quote):
        for c in CITE.finditer(text):
            a = int(c.group(2)); b = int(c.group(3) or a)
            cited.append((c.group(1), a, b))
            stats['cites'] += 1
            k2.extend(check_cite(c.group(1), a, b, c.group(4), need_quote))
    scan(rej, True)
    for s, ls in secs.items():
        scan('\n'.join(ls), s in QUOTED)
    # Tier tokens: only the Rulebook's five (quotes and code spans are not scanned).
    stripped = CITE.sub(lambda c: c.group(1), body)
    stripped = re.sub(r'`[^`\n]*`', '', stripped)
    for t in re.finditer(r'\[([^\]\n]+)\](?!\()', stripped):
        head = re.split(r'[,;]', t.group(1))[0].strip()
        if head not in TIERS and TIERISH.search(head):
            k2.append('non-Rulebook tier token [%s]' % t.group(1))
    # Every factual bullet: at least one citation and a Rulebook tier.
    for s in FACTUAL:
        items = []
        for l in secs.get(s, []):
            if re.match(r'\s{0,1}[-*]\s+', l):
                items.append(l)
            elif items and re.match(r'\s+\S', l):
                items[-1] += ' ' + l.strip()
        for it in items:
            if not CITE.search(it):
                k2.append('%s bullet without citation: %s' % (s, it.strip()[:60]))
            if not TIER_OK.search(it):
                k2.append('%s bullet without a Rulebook tier: %s' % (s, it.strip()[:60]))
    # At least one citation into the evidence pack's caveats section, when it has one.
    pack = 'ecosystem/pickup/_evidence/%s.md' % slug
    pl = lines_of(pack)
    if pl:
        hs = [i for i, l in enumerate(pl) if re.match(r'##\s+.*(caveat|note)', l, re.I) and not l.startswith('###')]
        if hs:
            lo = hs[0] + 1
            hi = next((i for i in range(lo, len(pl)) if pl[i].startswith('## ')), len(pl))
            if not any(p == pack and a <= hi and b >= lo for p, a, b in cited):
                k2.append('no citation into %s caveats section (lines %d-%d)' % (pack, lo, hi))
    if k1:
        out['K1'].append('%s: %s' % (rel, '; '.join(k1)))
    for e in k2:
        out['K2'].append('%s: %s' % (rel, e))
if stats['files'] and not stats['cites']:
    out['K2'].append('empty scan set: zero citations in stack/*.md')
if not stats['files']:
    out['K2'].append('empty scan set: no verdict files to check')

# K3: stack/rigor-practices.tsv
TSV = 'stack/rigor-practices.tsv'
HEADER = 'id\tpractice\twhat_to_copy\tareas\tevidenced_in\tsource\tsource_quote\tchecklist\tour_status\tour_proof'
if not os.path.isfile(os.path.join(root, TSV)):
    out['K3'].append('%s missing' % TSV)
else:
    tl = [l for l in open(os.path.join(root, TSV), encoding='utf-8').read().split('\n') if l.strip()]
    if not tl or tl[0] != HEADER:
        out['K3'].append('header is not exactly: %s' % HEADER.replace('\t', ' | '))
    kit = set(re.findall(r'^### ([A-Z]\d+) ', open(os.path.join(root, 'starter-kit/CHECKLIST.md')).read(), re.M))
    areas_ok = set(slugs) | {'frankensuite'}
    for i, l in enumerate(tl[1:], 1):
        c = l.split('\t')
        stats['rows'] += 1
        rid = c[0] if c else '?'
        if len(c) != 10:
            out['K3'].append('row %d: %d columns, want 10' % (i, len(c))); continue
        r = dict(zip(HEADER.split('\t'), [x.strip() for x in c]))
        if r['id'] != 'RP-%03d' % i:
            out['K3'].append('row %d: id %r, want RP-%03d' % (i, r['id'], i))
        sm = re.match(r'^([\w./-]+):(\d+)$', r['source'])
        if not sm:
            out['K3'].append('%s: source %r is not path:line' % (rid, r['source']))
        else:
            for e in check_cite(sm.group(1), int(sm.group(2)), int(sm.group(2)), r['source_quote'], True):
                out['K3'].append('%s: %s' % (rid, e))
        bad_areas = [a for a in r['areas'].split(';') if a.strip() not in areas_ok]
        if bad_areas or not r['areas']:
            out['K3'].append('%s: areas not in the 21 slugs or frankensuite: %r' % (rid, bad_areas or r['areas']))
        ck = [x.strip() for x in r['checklist'].split(';') if x.strip()]
        if ck != ['none'] and (not ck or any(x not in kit for x in ck)):
            out['K3'].append('%s: checklist %r has ids not headed "### <id> " in starter-kit/CHECKLIST.md' % (rid, r['checklist']))
        if r['our_status'] not in ('adopted', 'partial', 'candidate', 'not-applicable'):
            out['K3'].append('%s: our_status %r not adopted|partial|candidate|not-applicable' % (rid, r['our_status']))
        if r['our_status'] in ('adopted', 'partial'):
            # our_proof is ';'-separated. A token may carry a label ("done: ...", "missing: ...");
            # missing/todo/gap tokens name what is absent and are not proofs. In every other token,
            # each path reference (with a "/" or a known file extension, optional :line) must exist,
            # and each "commit <sha>" (or a token that is only a sha) must pass git cat-file -e.
            ok, bad = 0, []
            for tok in [t.strip() for t in r['our_proof'].split(';') if t.strip()]:
                lm = re.match(r'^([A-Za-z][A-Za-z ]*):\s+(.*)$', tok)
                if lm:
                    if lm.group(1).strip().lower() in ('missing', 'todo', 'gap', 'not yet'):
                        continue
                    tok = lm.group(2).strip()
                for pm in PROOF_REF.finditer(tok):
                    sha, ref, line = pm.group(2), pm.group(3), pm.group(4)
                    if sha:
                        if not pm.group(1) and tok != sha:
                            continue  # a bare hex word inside prose is not a commit reference
                        rc = subprocess.run(['git', '-C', root, 'cat-file', '-e', sha + '^{commit}'],
                                            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
                        (bad.append(pm.group(0)) if rc else None); ok += (rc == 0)
                    else:
                        ls = lines_of(ref)
                        good = ls is not None and (not line or 1 <= int(line) <= len(ls))
                        if ls is None and os.path.isdir(os.path.join(root, ref)) and not line:
                            good = True
                        (None if good else bad.append(pm.group(0))); ok += good
            if bad or not ok:
                out['K3'].append('%s: %s but proof does not resolve: %r' % (rid, r['our_status'], bad or r['our_proof']))
    if not stats['rows']:
        out['K3'].append('empty scan set: %s has no rows' % TSV)

# K5: every owner/repo in **bold** in an "Adopt, do not rebuild" bullet has a stack/licenses.tsv row,
# and a non-permissive license is named in that bullet's own words (METHOD rule 9).
out['K5'] = []
LTSV = 'stack/licenses.tsv'
LHEAD = 'repo\tspdx\tlicense_class\tchecked\tcommand\tnote'
LCLASSES = ['permissive', 'weak-copyleft', 'strong-copyleft', 'network-copyleft', 'source-available',
            'permissive-with-conditions', 'none', 'unknown']
REPO = re.compile(r'(?<![\w./-])([A-Za-z0-9][\w.-]*/[A-Za-z0-9](?:[\w.-]*[A-Za-z0-9_])?)')
NO_SPDX = {'', '-', 'NOASSERTION', 'NONE', 'OTHER', 'UNKNOWN'}
ALIASES = {'BUSL': ['BSL', 'Business Source'], 'SSPL': ['Server Side Public License'], 'AGPL': ['Affero'],
           'Elastic': ['Elastic License'], 'LGPL': ['Lesser General Public'], 'MPL': ['Mozilla Public']}
NOTE_NAMES = ['SSPL', 'Server-Side Public License', 'BSL', 'BUSL', 'Elastic', 'ELv2', 'AGPL', 'Affero', 'LGPL', 'GPL',
              'MPL', 'EPL', 'Commons Clause', 'OpenRAIL', 'RAIL', 'CC-BY-NC', 'non-commercial', 'noncommercial',
              'Community License', 'Enterprise License', 'Enterprise Edition', 'commercial license', 'proprietary',
              'LTX-2']
# Permissive SPDX ids never "name" a non-permissive license: "MIT" in a bullet says nothing about an AGPL part.
PERMISSIVE_IDS = {'MIT', 'MIT-0', 'Apache-2.0', 'Apache-1.1', 'BSD-2-Clause', 'BSD-3-Clause', 'ISC', '0BSD',
                  'Unlicense', 'Zlib', 'BSL-1.0', 'CC0-1.0', 'PSF-2.0', 'Python-2.0', 'X11', 'UPL-1.0'}
CLASS_WORDS = {
    'source-available': ['source-available', 'source available'],
    'permissive-with-conditions': ['condition', 'conditions'],
    'none': ['no license', 'no licence', 'unlicensed', 'without a license', 'no open-source license', 'no LICENSE file'],
    'unknown': ['license unknown', 'unknown license', 'license is unknown', 'license unclear', 'unclear license',
                'license could not be determined', 'license not determined'],
}
lic = {}
stats['lic'] = 0; stats['adopted'] = 0
if not os.path.isfile(os.path.join(root, LTSV)):
    out['K5'].append('%s missing' % LTSV)
else:
    ll = [l for l in open(os.path.join(root, LTSV), encoding='utf-8').read().split('\n') if l.strip()]
    if not ll or ll[0] != LHEAD:
        out['K5'].append('%s header is not exactly: %s' % (LTSV, LHEAD.replace('\t', ' | ')))
    for i, l in enumerate(ll[1:], 2):
        c = [x.strip() for x in l.split('\t')]
        if len(c) != 6:
            out['K5'].append('%s line %d: %d columns, want 6' % (LTSV, i, len(c))); continue
        r = dict(zip(LHEAD.split('\t'), c))
        stats['lic'] += 1
        if not re.fullmatch(REPO.pattern, r['repo']):
            out['K5'].append('%s line %d: repo %r is not owner/repo' % (LTSV, i, r['repo']))
        if r['license_class'] not in LCLASSES:
            out['K5'].append('%s line %d: license_class %r not in the eight classes' % (LTSV, i, r['license_class']))
        if not DATE.match(r['checked']):
            out['K5'].append('%s line %d: checked %r is not YYYY-MM-DD' % (LTSV, i, r['checked']))
        if r['repo'].lower() in lic:
            out['K5'].append('%s line %d: duplicate row for %s' % (LTSV, i, r['repo']))
        lic[r['repo'].lower()] = r
    if not stats['lic']:
        out['K5'].append('empty scan set: %s has no rows' % LTSV)

def license_names(r):
    """(label, regex) pairs; any one found in the bullet's prose names this row's license."""
    names = []
    word = lambda s: (s, r'(?<![\w])%s(?![\w])' % re.escape(s))
    for comp in re.split(r'\s+(?:AND|OR|WITH)\s+|[()]', r['spdx']):
        comp = comp.strip()
        if not comp or comp.upper() in NO_SPDX or comp in PERMISSIVE_IDS:
            continue
        if comp.startswith('LicenseRef-'):
            # LicenseRef-Llama-3.2-Community -> "Llama ... license" within a few words.
            first = comp[len('LicenseRef-'):].split('-')[0]
            if len(first) >= 3:
                names.append(('%s ... license' % first, r'(?<![\w])%s[\w\s.,/-]{0,40}?licen[cs]e' % re.escape(first)))
            continue
        names.append(word(comp))
        fam = re.match(r'[A-Za-z]+', comp)
        if fam and len(fam.group(0)) >= 3:
            names.append(word(fam.group(0)))
            names.extend(word(a) for a in ALIASES.get(fam.group(0), []))
    names.extend(word(w) for w in CLASS_WORDS.get(r['license_class'], []))
    names.extend(word(n) for n in NOTE_NAMES if re.search(r'(?<![\w-])%s(?![\w])' % re.escape(n), r['note'], re.I))
    return list(dict.fromkeys(names))

for slug in slugs:
    rel = 'stack/%s.md' % slug
    if not os.path.isfile(os.path.join(root, rel)):
        continue
    body = open(os.path.join(root, rel), encoding='utf-8').read()
    sm = re.search(r'^## Adopt, do not rebuild\s*\n(.*?)(?=^## |\Z)', body, re.S | re.M)
    items = []
    for l in (sm.group(1).split('\n') if sm else []):
        if re.match(r'\s{0,1}[-*]\s+', l):
            items.append(l)
        elif items and re.match(r'\s+\S', l):
            items[-1] += ' ' + l.strip()
    for it in items:
        # The author's words: quoted evidence and the adopted repo names themselves do not count.
        prose = CITE.sub(lambda c: c.group(1), it)
        prose = re.sub(r'\*\*(.+?)\*\*', lambda m: REPO.sub('', m.group(1)), prose)
        for b in re.findall(r'\*\*(.+?)\*\*', it):
            for repo in REPO.findall(b):
                stats['adopted'] += 1
                r = lic.get(repo.lower())
                if r is None:
                    out['K5'].append('%s: adopted %s has no %s row' % (rel, repo, LTSV))
                    continue
                if r['license_class'] == 'permissive':
                    continue
                names = license_names(r)
                if not any(re.search(rx, prose, re.I) for _, rx in names):
                    out['K5'].append('%s: adopted %s is %s (%s) but its bullet does not name the license (looked for: %s)'
                                     % (rel, repo, r['license_class'], r['spdx'] or 'no spdx',
                                        ', '.join(n for n, _ in names) or 'nothing to match'))
if stats['files'] and not stats['adopted']:
    out['K5'].append('empty scan set: no owner/repo in bold under "Adopt, do not rebuild" in any verdict')

print('KSTATS files=%d/%d cites=%d rows=%d licenses=%d adopted=%d' % (
    stats['files'], len(slugs), stats['cites'], stats['rows'], stats['lic'], stats['adopted']))
for k in ('K1', 'K2', 'K3', 'K5'):
    for e in out[k]:
        print('%s %s' % (k, e))
print('K_DONE')
PYEOF
)"
K_STATS="$(printf '%s\n' "$K_OUT" | sed -n 's/^KSTATS //p')"
k_report() {  # $1 = sub-gate id, $2 = label; PASS only if the checker finished and printed no findings
  local lines n
  lines="$(printf '%s\n' "$K_OUT" | grep "^$1 " || true)"
  if ! printf '%s\n' "$K_OUT" | grep -q '^K_DONE'; then
    fail "$1 $2" "checker crashed: $(printf '%s\n' "$K_OUT" | tail -3 | tr '\n' ';')"
  elif [ -z "$lines" ]; then
    pass "$1 $2 ($K_STATS)"
  else
    n="$(printf '%s\n' "$lines" | wc -l | tr -d ' ')"
    fail "$1 $2" "$n finding(s):"
    printf '%s\n' "$lines" | head -40 | sed 's/^/        /'
    [ "$n" -gt 40 ] && echo "        ... and $((n - 40)) more"
  fi
}
k_report K1 "verdict files complete and independently reviewed"
k_report K2 "quoted citations resolve"
k_report K3 "rigor practices sources and proofs"

# K4: the shipped pages are exactly what make-stack.mjs produces from today's sources.
K4_TMP="$(mktemp -d)"
K4_RUN="$(node "$SITE_DIR/scripts/make-stack.mjs" --out "$K4_TMP" 2>&1)"; K4_RC=$?
K4_DIFF=""
K4_N="$(ls "$K4_TMP"/stack/*.html "$K4_TMP"/rigor/*.html 2>/dev/null | wc -l | tr -d ' ')"
for d in stack rigor; do
  if [ -d "$K4_TMP/$d" ] || [ -d "$SITE_DIR/$d" ]; then
    K4_DIFF="$K4_DIFF$(diff -rq "$SITE_DIR/$d" "$K4_TMP/$d" 2>&1 | sed -e "s#$K4_TMP#<fresh>#g" -e "s#$SITE_DIR#site#g")"
  fi
done
rm -rf "$K4_TMP"
if [ $K4_RC -ne 0 ]; then
  fail "K4 generated pages match their sources" "make-stack.mjs exited $K4_RC: $K4_RUN"
elif [ "${K4_N:-0}" -eq 0 ]; then
  fail "K4 generated pages match their sources" "empty scan set: a fresh run produced no pages"
elif [ -n "$K4_DIFF" ]; then
  fail "K4 generated pages match their sources" "rerun node site/scripts/make-stack.mjs: $(echo "$K4_DIFF" | head -4 | tr '\n' ';')"
else
  pass "K4 generated pages match their sources ($K4_N pages byte-identical to a fresh run)"
fi
k_report K5 "adopted incumbents have a checked license, named when not permissive"

# ============ Gate L: no personal email addresses ============
# Every git-tracked text file in the repo. Allowed: noreply@*, *@users.noreply.github.com, the
# starter kit's documented placeholder you@example.com (starter-kit/README.md, starter-kit/scripts/init.sh),
# and git@github.com (an SSH remote host reference, not a personal address).
echo "== L  no personal email addresses =="
L_OUT="$(python3 - <<'PYEOF'
import re, os, subprocess
root = os.environ['REPO_ROOT']
EMAIL = re.compile(r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}')
def allowed(e):
    e = e.lower()
    return (e.startswith('noreply@') or e.endswith('@users.noreply.github.com')
            or e == 'you@example.com' or e == 'git@github.com')
p = subprocess.run(['git', '-C', root, 'ls-files', '-z'], capture_output=True)
files = [f for f in p.stdout.decode('utf-8', 'replace').split('\0') if f] if p.returncode == 0 else []
hits, scanned = [], 0
for f in files:
    full = os.path.join(root, f)
    if not os.path.isfile(full):
        continue
    b = open(full, 'rb').read()
    if b'\0' in b[:8192]:
        continue  # binary
    scanned += 1
    for i, line in enumerate(b.decode('utf-8', 'replace').split('\n'), 1):
        for e in EMAIL.findall(line):
            if not allowed(e):
                hits.append('%s:%d %s' % (f, i, e))
print('SCANNED %d' % scanned)
if p.returncode != 0:
    print('  git ls-files failed: %s' % p.stderr.decode('utf-8', 'replace').strip()[:200])
elif scanned == 0:
    print('  empty scan set: no tracked text files')
for h in hits:
    print('  ' + h)
PYEOF
)"
L_N="$(count_of "$L_OUT" SCANNED)"
L_BAD="$(printf '%s\n' "$L_OUT" | grep '^  ' || true)"
if [ "${L_N:-0}" -gt 0 ] && [ -z "$L_BAD" ]; then
  pass "L no personal email addresses ($L_N tracked text files)"
else
  fail "L no personal email addresses" "$(printf '%s\n' "$L_BAD" | wc -l | tr -d ' ') finding(s):"
  printf '%s\n' "$L_BAD" | head -40 | sed 's/^/      /'
fi

# ============ Gate W: daily-watch selftest (offline) ============
# watch/watch.mjs --selftest feeds the recorded API responses in watch/fixtures/ through the same
# collect, diff, and dedupe code the daily watch runs against GitHub. No network, no token. Fails
# on a nonzero exit, any failed case, or zero cases run. Details: ../BUILD-GATES.md.
echo "== W  watch selftest =="
W_OUT="$(node "$REPO_ROOT/watch/watch.mjs" --selftest 2>&1)"
W_RC=$?
W_N="$(count_of "$W_OUT" CASES)"
W_F="$(count_of "$W_OUT" FAILED)"
if [ $W_RC -eq 0 ] && [ "${W_N:-0}" -gt 0 ] && [ "${W_F:-x}" = "0" ]; then
  pass "W watch selftest ($W_N cases on recorded fixtures)"
else
  fail "W watch selftest" "exit $W_RC, cases ${W_N:-0}, failed ${W_F:-unknown}"
  printf '%s\n' "$W_OUT" | grep -v '^PASS' | head -40 | sed 's/^/      /'
fi

# ============ Gate W2: discovery selftest (offline) ============
# watch/discover.mjs --selftest runs the recorded search, contents, commits, and readme responses in
# watch/fixtures/discover-*.json through the same transport, exclusion, scoring, and rollup-issue code
# the weekly sweep runs against GitHub. No network, no token. Fails on a nonzero exit, any failed
# case, or zero cases run. Details: ../BUILD-GATES.md.
echo "== W2 discovery selftest =="
W2_OUT="$(node "$REPO_ROOT/watch/discover.mjs" --selftest 2>&1)"
W2_RC=$?
W2_N="$(count_of "$W2_OUT" CASES)"
W2_F="$(count_of "$W2_OUT" FAILED)"
if [ $W2_RC -eq 0 ] && [ "${W2_N:-0}" -gt 0 ] && [ "${W2_F:-x}" = "0" ]; then
  pass "W2 discovery selftest ($W2_N cases on recorded fixtures)"
else
  fail "W2 discovery selftest" "exit $W2_RC, cases ${W2_N:-0}, failed ${W2_F:-unknown}"
  printf '%s\n' "$W2_OUT" | grep -v '^PASS' | head -40 | sed 's/^/      /'
fi

# ============ Gate W3: freshness conformance harness (offline) ============
# watch/freshness/harness/run.mjs --check-report runs every case in watch/freshness/cases/ against the
# clauses of watch/freshness/SPEC.md and compares watch/freshness/REPORT.md with a fresh render. One of
# those cases, HAR-H6-mutants, runs the mutation runner (harness/mutate.mjs) on every mutant in
# harness/mutants.json; W3 reads its verdict and counts from the harness output rather than running the
# mutants a second time. ops/schedule.mjs checks ops/schedule.tsv against the workflows, this script,
# and the scripts that write files; ops/write-job.mjs checks that the watch job runs only on main, keeps
# no token in .git/config or in reach of install, build and gate steps, and syncs the dashboard only after
# the gates and the push; make-live.mjs --check re-renders the live:card region of every brief. No
# network, no token. Fails on a nonzero exit of any of the four, any failed case, an uncovered MUST
# clause, zero cases, a stale REPORT.md, a missing HAR-H6-mutants result, zero mutants or one that
# survives, and a missing make-live.mjs. Details: ../BUILD-GATES.md.
echo "== W3 freshness harness =="
W3_OK=1; W3_DETAIL=""
W3_OUT="$(node "$REPO_ROOT/watch/freshness/harness/run.mjs" --check-report 2>&1)"; W3_RC=$?
W3_N="$(count_of "$W3_OUT" CASES)"
W3_F="$(count_of "$W3_OUT" FAILED)"
W3_U="$(count_of "$W3_OUT" UNCOVERED_MUST)"
W3_S="$(printf '%s\n' "$W3_OUT" | sed -n 's/^SECONDS \([0-9.]*\)$/\1/p')"
if [ $W3_RC -ne 0 ] || [ "${W3_N:-0}" -eq 0 ] || [ "${W3_F:-x}" != "0" ] || [ "${W3_U:-x}" != "0" ] || ! printf '%s\n' "$W3_OUT" | grep -q '^REPORT_OK'; then
  W3_OK=0; W3_DETAIL="harness exit $W3_RC, cases ${W3_N:-0}, failed ${W3_F:-unknown}, uncovered MUST ${W3_U:-unknown}; "
fi
W3M_LINE="$(printf '%s\n' "$W3_OUT" | grep '^{"id":"HAR-H6-mutants",' | head -1)"
W3_MK="$(printf '%s\n' "$W3M_LINE" | sed -n 's/.*"mutation\.killed":\([0-9][0-9]*\).*/\1/p')"
W3_MT="$(printf '%s\n' "$W3M_LINE" | sed -n 's/.*"mutation\.total":\([0-9][0-9]*\).*/\1/p')"
if ! printf '%s\n' "$W3M_LINE" | grep -q '"verdict":"PASS"' || [ "${W3_MT:-0}" -eq 0 ] || [ "${W3_MK:-x}" != "${W3_MT:-y}" ]; then
  W3_OK=0; W3_DETAIL="${W3_DETAIL}mutation (case HAR-H6-mutants): killed ${W3_MK:-none} of ${W3_MT:-none}; "
fi
W3S_OUT="$(node "$REPO_ROOT/ops/schedule.mjs" 2>&1)"; W3S_RC=$?
W3_SR="$(printf '%s\n' "$W3S_OUT" | sed -n 's/^SCHEDULE_OK rows=\([0-9][0-9]*\).*/\1/p')"
if [ $W3S_RC -ne 0 ] || [ "${W3_SR:-0}" -eq 0 ]; then
  W3_OK=0; W3_DETAIL="${W3_DETAIL}schedule check exit $W3S_RC; "
fi
W3J_OUT="$(node "$REPO_ROOT/ops/write-job.mjs" 2>&1)"; W3J_RC=$?
if [ $W3J_RC -ne 0 ] || ! printf '%s\n' "$W3J_OUT" | grep -q '^WRITE_JOB_OK'; then
  W3_OK=0; W3_DETAIL="${W3_DETAIL}write-job check exit $W3J_RC; "
fi
W3P_OUT="$(node --test "$REPO_ROOT/scripts/test-ci-evidence-packet.mjs" 2>&1)"; W3P_RC=$?
if [ $W3P_RC -ne 0 ]; then
  W3_OK=0; W3_DETAIL="${W3_DETAIL}CI evidence packet tests exit $W3P_RC; "
fi
W3L_OUT=""
if [ -f "$SITE_DIR/scripts/make-live.mjs" ]; then
  W3L_OUT="$(node "$SITE_DIR/scripts/make-live.mjs" --check 2>&1)"; W3L_RC=$?
  W3_LB="$(printf '%s\n' "$W3L_OUT" | sed -n 's/^LIVE_OK briefs=\([0-9][0-9]*\).*/\1/p')"
  if [ $W3L_RC -ne 0 ] || [ "${W3_LB:-0}" -eq 0 ]; then
    W3_OK=0; W3_DETAIL="${W3_DETAIL}make-live.mjs --check exit $W3L_RC; "
  fi
else
  W3_OK=0; W3_DETAIL="${W3_DETAIL}site/scripts/make-live.mjs is missing; "
fi
if [ $W3_OK -eq 1 ]; then
  pass "W3 freshness harness ($W3_N cases in ${W3_S:-?} s, REPORT.md fresh, $W3_MK of $W3_MT mutants killed, $W3_SR schedule rows, write job guarded, $W3_LB brief cards)"
else
  fail "W3 freshness harness" "$W3_DETAIL"
  printf '%s\n' "$W3_OUT" "$W3S_OUT" "$W3J_OUT" "$W3P_OUT" "$W3L_OUT" |
    grep -E '"verdict":"FAIL"|^REPORT_STALE|^HARNESS ERROR|^NO CASES|^SCHEDULE_BAD|^WRITE_JOB_(BAD|ERROR)|^LIVE_BAD|^  ' |
    head -40 | sed 's/^/      /'
fi

# ============ Gate M: feed and OPML fresh and well-formed ============
# site/feed.xml and site/follow/franken-suite.opml are generated by scripts/make-feed.mjs from
# CHANGELOG.md, updates/, watch/census/ and packets/. A fresh run into a temp dir must match the
# committed files byte for byte, and both must pass a strict XML parse plus the Atom/OPML structure
# check (make-feed.mjs --check), and xmllint --noout when xmllint is installed. Details: ../BUILD-GATES.md.
echo "== M  feed + OPML =="
M_OK=1; M_DETAIL=""
M_TMP="$(mktemp -d)"
M_RUN="$(node "$SITE_DIR/scripts/make-feed.mjs" --out "$M_TMP" 2>&1)"; M_RC=$?
if [ $M_RC -ne 0 ]; then
  M_OK=0; M_DETAIL="make-feed.mjs exited $M_RC: $(printf '%s\n' "$M_RUN" | head -3 | tr '\n' ';') "
else
  for f in feed.xml follow/franken-suite.opml; do
    if [ ! -f "$SITE_DIR/$f" ]; then
      M_OK=0; M_DETAIL="${M_DETAIL}site/$f missing; "
    elif ! cmp -s "$SITE_DIR/$f" "$M_TMP/$f"; then
      M_OK=0; M_DETAIL="${M_DETAIL}site/$f differs from a fresh run (run: bun run build:feed); "
    fi
  done
fi
rm -rf "$M_TMP"
M_CHECK="$(node "$SITE_DIR/scripts/make-feed.mjs" --check "$SITE_DIR/feed.xml" "$SITE_DIR/follow/franken-suite.opml" 2>&1)"; M_CRC=$?
if [ $M_CRC -ne 0 ] || ! printf '%s\n' "$M_CHECK" | grep -q '^FEED_OK' || ! printf '%s\n' "$M_CHECK" | grep -q '^OPML_OK'; then
  M_OK=0; M_DETAIL="${M_DETAIL}check exited $M_CRC: $(printf '%s\n' "$M_CHECK" | grep -v '_OK' | head -4 | tr '\n' ';') "
fi
M_PARSERS="node parser"
if command -v xmllint >/dev/null 2>&1; then
  M_PARSERS="node parser + xmllint"
  if ! M_XL="$(xmllint --noout "$SITE_DIR/feed.xml" "$SITE_DIR/follow/franken-suite.opml" 2>&1)"; then
    M_OK=0; M_DETAIL="${M_DETAIL}xmllint: $(printf '%s\n' "$M_XL" | head -3 | tr '\n' ';') "
  fi
fi
M_N="$(printf '%s\n' "$M_CHECK" | sed -n 's/^FEED_OK entries=\([0-9][0-9]*\).*/\1/p')"
M_F="$(printf '%s\n' "$M_CHECK" | sed -n 's/^OPML_OK feeds=\([0-9][0-9]*\).*/\1/p')"
if [ $M_OK -eq 1 ] && [ "${M_N:-0}" -gt 0 ] && [ "${M_F:-0}" -gt 0 ]; then
  pass "M feed + OPML fresh and well-formed ($M_N entries, $M_F OPML feeds; $M_PARSERS)"
else
  fail "M feed + OPML fresh and well-formed" "$M_DETAIL"
fi

# ============ Gate S: shared shell (head links, site nav, footer) ============
# scripts/shell.mjs owns four marked regions: shell:head on every page, shell:nav and shell:footer on every
# page except the home page, and shell:dir on the home page only. --check re-renders each region and fails on a missing
# or hand-edited one, on a missing or stale ?v=<content hash> on a script or stylesheet loaded from
# assets/, on a page list that disagrees with sitemap.xml, or on a prefilled issue link that names a
# field the issue forms lack. Fix drift (including after any edit to a file in assets/) with
# `bun run build:shell`. Details: ../BUILD-GATES.md.
echo "== S  shared shell =="
S_OUT="$(node "$SITE_DIR/scripts/shell.mjs" --check --site "$SITE_DIR" 2>&1)"; S_RC=$?
S_N="$(printf '%s\n' "$S_OUT" | sed -n 's/^SHELL_OK pages=\([0-9][0-9]*\) regions=\([0-9][0-9]*\) .*stamps=\([0-9][0-9]*\).*/\1 pages, \2 regions, \3 asset stamps/p')"
if [ $S_RC -eq 0 ] && [ -n "$S_N" ]; then
  pass "S shared shell matches its render on every page ($S_N)"
else
  fail "S shared shell" "exit $S_RC: $(printf '%s\n' "$S_OUT" | grep -v '^SHELL_' | head -4 | tr '\n' ';')"
fi

# ============ Gate V: verdict strip on every brief ============
# scripts/brief-strip.mjs owns two marked regions on every brief: brief:style in <head> and brief:strip
# under the title (ring, TRL, CI class, license, the brief's own bottom line, a Why link, a link to the
# prefilled correction form, and one sentence of FrankenSuite context). --check re-renders both from
# assets/data.js and the map's palette and CI words in assets/app.src.js, and fails on a missing or
# hand-edited region, on a correction link whose repository or page is not this brief, on a strip or a ring widget or TRL
# gauge whose ring or TRL disagrees with data.js, and on a "What would change the verdict" list collapsed
# into one item or carrying its "Until then" sentence as an item. Fix drift with
# `node site/scripts/brief-strip.mjs`. Details: ../BUILD-GATES.md.
echo "== V  verdict strip =="
V_OUT="$(node "$SITE_DIR/scripts/brief-strip.mjs" --check --site "$SITE_DIR" 2>&1)"; V_RC=$?
V_N="$(printf '%s\n' "$V_OUT" | sed -n 's/^STRIP_OK briefs=\([0-9][0-9]*\).*/\1/p')"
if [ $V_RC -eq 0 ] && [ "${V_N:-0}" -gt 0 ]; then
  pass "V verdict strip matches its render and data.js on every brief ($V_N briefs)"
else
  fail "V verdict strip" "exit $V_RC: $(printf '%s\n' "$V_OUT" | grep -v '^STRIP_' | head -4 | tr '\n' ';')"
fi

# ============ Gate N: Independent 100 study pages ============
# study/independent-100/ holds people.jsonl, deep/*.md and README.md; scripts/make-study.mjs renders them into
# site/study/independent-100/ and a marked block of sitemap.xml. `make-study.mjs --check` fails on N1 drift (a page
# or the sitemap block differs from a fresh render, or a page no source makes), N2 count, N3 schema (people.jsonl
# keys and order, https URLs, labels, scores; deep-dive front matter and required sections; no "Internal notes";
# the correction-form option every page prefills) and N4 scan (local paths, emails, phone numbers, images, X API
# field names, internal ticket ids, gendered pronouns on pseudonymous records, and the literal text undefined, null or
# NaN on a rendered page), and N5 evidence and links (rows in study/independent-100/evidence/ behind every cited X post,
# X API label and GitHub license; every URL in a pseudonymous record on route a, b, c or d of the study README; no
# links.site or links.blog there; no withheld or handle-named site anywhere in the study). This block also counts the files
# itself, independently of the script: page files on disk must equal the people records, plus the index, plus one
# per deep dive. Fix drift with `node site/scripts/make-study.mjs`. Details: ../BUILD-GATES.md.
echo "== N  Independent 100 study =="
N_OUT="$(node "$SITE_DIR/scripts/make-study.mjs" --check 2>&1)"; N_RC=$?
N_PEOPLE=0
[ -f "$REPO_ROOT/study/independent-100/people.jsonl" ] && N_PEOPLE="$(grep -c . "$REPO_ROOT/study/independent-100/people.jsonl")"
N_DEEP="$(find "$REPO_ROOT/study/independent-100/deep" -maxdepth 1 -name '*.md' | wc -l | tr -d ' ')"
N_PAGES="$(find "$SITE_DIR/study/independent-100" -name '*.html' | wc -l | tr -d ' ')"
N_WANT=$(( N_PEOPLE + 1 + N_DEEP ))
if [ $N_RC -ne 0 ] || ! printf '%s\n' "$N_OUT" | grep -q '^STUDY_OK'; then
  fail "N Independent 100 study" "make-study.mjs --check exit $N_RC:"
  printf '%s\n' "$N_OUT" | grep '^  ' | head -40 | sed 's/^/      /'
elif [ "$N_PEOPLE" -ne 99 ] || [ "$N_PAGES" -ne "$N_WANT" ]; then
  fail "N Independent 100 study" "count: $N_PEOPLE people records (want 99), $N_DEEP deep dives, $N_PAGES page files (want $N_WANT)"
else
  pass "N Independent 100 study matches its sources ($N_PAGES pages: $N_PEOPLE people, index, $N_DEEP deep dives; schema and scan clean)"
fi

# ============ Gate T: llms.txt fresh, linked pages present ============
# site/llms.txt (the llms.txt convention: H1, blockquote, sections of "- [text](url): description") is generated by
# scripts/make-llms.mjs from data.js, the shell page list, the pages' titles and descriptions, the self-assessment's
# License row, the feed, the OPML, RULEBOOK.md and the packets. `make-llms.mjs --check` writes nothing and fails on
# T1 drift (the committed file differs from a fresh render, or the render fails), T2 links (a link off this site and
# its source repository, one with no file under site/, or an HTML page sitemap.xml does not list) and T3 shape.
# This block also resolves every site link in the committed file itself, without the script. Fix drift with
# `bun run build:llms`. Details: ../BUILD-GATES.md.
echo "== T  llms.txt =="
T_OUT="$(node "$SITE_DIR/scripts/make-llms.mjs" --check --site "$SITE_DIR" 2>&1)"; T_RC=$?
T_N="$(printf '%s\n' "$T_OUT" | sed -n 's/^LLMS_OK links=\([0-9][0-9]*\) sections=\([0-9][0-9]*\).*/\1 links, \2 sections/p')"
T_MISSING=""; T_LINKS=0
if [ -f "$SITE_DIR/llms.txt" ]; then
  T_URLS="$(sed -n 's|.*](\(https://fr\.zeststream\.ai/[^)]*\)).*|\1|p' "$SITE_DIR/llms.txt")"
  for T_URL in $T_URLS; do
    T_LINKS=$((T_LINKS+1))
    T_P="${T_URL#https://fr.zeststream.ai/}"
    case "$T_P" in ''|*/) T_F="${T_P}index.html" ;; *.*) T_F="$T_P" ;; *) T_F="$T_P.html" ;; esac
    [ -f "$SITE_DIR/$T_F" ] || T_MISSING="${T_MISSING}${T_URL} (no site/$T_F); "
  done
fi
if [ $T_RC -ne 0 ] || [ -z "$T_N" ]; then
  fail "T llms.txt" "make-llms.mjs --check exit $T_RC:"
  printf '%s\n' "$T_OUT" | grep '^  ' | head -20 | sed 's/^/      /'
elif [ -n "$T_MISSING" ] || [ "$T_LINKS" -eq 0 ]; then
  fail "T llms.txt" "$T_LINKS site links; missing: ${T_MISSING:-none (zero links is an empty scan set)}"
else
  pass "T llms.txt matches a fresh render and every link resolves ($T_N; $T_LINKS site links resolved by the gate)"
fi

# ============ U: installed kit and maintained decision records ============
echo "== U  kit and decision regressions =="
if (cd "$REPO_ROOT" && python3 starter-kit/tests/test_gates.py && python3 scripts/test_decisions.py && python3 scripts/test_review_decisions.py && python3 scripts/test_learning_example.py && python3 scripts/test_learning.py && python3 -B ops/test_research_agent.py && python3 scripts/build-learning.py --check && python3 scripts/check-decisions.py docs/evidence/fr-evolution/decisions.json && diff -qr starter-kit site/starter-kit); then
  pass "U kit regression controls, decision identities and shipped copies"
else
  fail "U kit regression controls, decision identities and shipped copies"
fi

# ============ Gate GP: golden-packet regression (bead fr-itk) ============
# scripts/golden-packets.py re-derives a small banked set of golden
# packets from their pinned inputs and fails on any unexplained delta.
# Unlike receipt-verify, the golden cannot be silently re-pinned: a
# one-character packet edit fails until the reviewed emit ceremony.
echo "== GP golden-packet regression =="
GP_OK=1; GP_DETAIL=""
GP_T_OUT="$(python3 "$REPO_ROOT/scripts/test_golden_packets.py" 2>&1)"; GP_T_RC=$?
if [ $GP_T_RC -ne 0 ]; then
  GP_OK=0; GP_DETAIL="golden-packet tests exit $GP_T_RC; "
fi
GP_OUT="$(python3 "$REPO_ROOT/scripts/golden-packets.py" --root "$CANON" verify 2>&1)"; GP_RC=$?
if [ $GP_RC -ne 0 ]; then
  GP_OK=0; GP_DETAIL="${GP_DETAIL}golden-packets exit $GP_RC; "
fi
if [ $GP_OK -eq 1 ]; then
  pass "GP golden-packet regression re-derives banked derivations"
else
  fail "GP golden-packet regression" "$GP_DETAIL"
  printf '%s\n' "$GP_OUT" | head -20 | sed 's/^/      /'
fi

# ============ Gate FX: self-verification fixtures (bead fr-tww) ============
# scripts/fixtures.py runs the three golden-fixture surfaces inside a
# 300s budget; gates U and GP already cover two of them above, so this
# gate runs the surface they miss (nightly candidate shape, whose
# committed candidates must re-execute to their recorded
# execution.json hashes) plus the runner's own tests. Agents cite
# `python3 scripts/fixtures.py` output in bead closures; see
# docs/fixtures.md.
echo "== FX self-verification fixtures =="
FX_OK=1; FX_DETAIL=""
FX_T_OUT="$(python3 "$REPO_ROOT/scripts/test_candidate_shape.py" 2>&1)"; FX_T_RC=$?
if [ $FX_T_RC -ne 0 ]; then
  FX_OK=0; FX_DETAIL="candidate-shape tests exit $FX_T_RC; "
fi
FX_R_OUT="$(python3 "$REPO_ROOT/scripts/test_fixtures.py" 2>&1)"; FX_R_RC=$?
if [ $FX_R_RC -ne 0 ]; then
  FX_OK=0; FX_DETAIL="${FX_DETAIL}fixtures-runner tests exit $FX_R_RC; "
fi
FX_OUT="$(python3 "$REPO_ROOT/scripts/candidate-shape.py" --root "$CANON" verify 2>&1)"; FX_RC=$?
if [ $FX_RC -ne 0 ]; then
  FX_OK=0; FX_DETAIL="${FX_DETAIL}candidate-shape exit $FX_RC; "
fi
if [ $FX_OK -eq 1 ]; then
  pass "FX candidate-shape fixtures re-derive recorded nightly evidence"
else
  fail "FX self-verification fixtures" "$FX_DETAIL"
  printf '%s\n' "$FX_OUT" | head -20 | sed 's/^/      /'
fi

# ============ Gate CV: citation verifier (bead fr-brn) ============
# scripts/citation-verifier.py re-derives every checkable number a packet cites
# (packet/RULEBOOK/claim-set hashes, claim counts, numbering, status ratios,
# labelled pins) from the pinned in-repo sources and fails on any mismatch.
# Its unit tests include planted false citations, which must be flagged.
echo "== CV citation verifier =="
CV_OK=1; CV_DETAIL=""
CV_T_OUT="$(python3 "$REPO_ROOT/scripts/test_citation_verifier.py" 2>&1)"; CV_T_RC=$?
if [ $CV_T_RC -ne 0 ]; then
  CV_OK=0; CV_DETAIL="citation-verifier tests exit $CV_T_RC; "
fi
CV_OUT="$(python3 "$REPO_ROOT/scripts/citation-verifier.py" --root "$CANON" verify 2>&1)"; CV_RC=$?
CV_N="$(printf '%s\n' "$CV_OUT" | sed -n 's/^verify: \([0-9][0-9]*\)\/\([0-9][0-9]*\) packets pass.*/\1\/\2/p')"
if [ $CV_RC -ne 0 ] || [ -z "$CV_N" ]; then
  CV_OK=0; CV_DETAIL="${CV_DETAIL}citation-verifier exit $CV_RC; "
fi
if [ $CV_OK -eq 1 ]; then
  pass "CV citation verifier re-derives cited numbers ($CV_N packets)"
else
  fail "CV citation verifier" "$CV_DETAIL"
  printf '%s\n' "$CV_OUT" | grep 'verdict=FAIL' | head -20 | sed 's/^/      /'
fi

# ============ summary ============
echo "----------------------------------------"
echo "gates passed: $PASS   failed: $FAIL"
if [ $FAIL -gt 0 ]; then
  echo "failed gates: ${FAILED_GATES[*]}"
  exit 1
fi
echo "ALL GATES PASS"
