#!/usr/bin/env bash
# Smoke test for the sprint lifecycle script: exercises the full happy path,
# both fail-loops, the close refusal (both gates, and the user-authorization
# requirement), and the standard edge cases (bad verdict, skipping a phase,
# closing early, empty title). Exits non-zero on the first unexpected result.
#
# Runs entirely inside a throwaway sandbox directory (mktemp -d), never
# against this repo's own docs/sprints/. Note that just `cd`-ing elsewhere
# before invoking the real script would NOT be enough: sprint_lifecycle.py
# resolves ROOT from Path(__file__).resolve().parent.parent, i.e. from
# where the *script file* lives, not the caller's working directory. So
# this test copies the script (and the sprint template) into the sandbox
# and runs that copy, which makes ROOT resolve inside the sandbox instead.
# This is not a style preference: a version of this file that rm -rf'd
# docs/sprints/ directly against the invoking repo has already destroyed a
# real downstream project's sprint history twice. Do not "simplify" this
# back to operating on whatever repo you happen to be standing in.
set -euo pipefail

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"

SANDBOX="$(mktemp -d "${TMPDIR:-/tmp}/fully-completely-smoke.XXXXXX")"
cleanup() { rm -rf "$SANDBOX"; }
trap cleanup EXIT

mkdir -p "$SANDBOX/scripts" "$SANDBOX/templates"
cp "$REPO_ROOT/scripts/sprint_lifecycle.py" "$SANDBOX/scripts/sprint_lifecycle.py"
if [ -f "$REPO_ROOT/templates/sprint-template.md" ]; then
  cp "$REPO_ROOT/templates/sprint-template.md" "$SANDBOX/templates/sprint-template.md"
fi

cd "$SANDBOX"
SCRIPT="python3 scripts/sprint_lifecycle.py"

fail() { echo "SMOKE TEST FAILED: $1" >&2; exit 1; }

# Sprint 41, Req 4a: run against the REAL repository tree (REPO_ROOT), not
# the sandbox copy -- this is a static scan of this project's own shipped
# scripts/.claude files, not something that needs a throwaway sprint
# lifecycle to exercise. Asserts no code path constructs, defaults,
# templates, or otherwise supplies --user-said/--user-said-file CONTENT;
# see scripts/check_user_said_guard.py's own header for the full method
# and the three violation shapes it's been verified (by negative control)
# to catch.
echo "== Req 4a (sprint 41): the scanner's own detection logic against every known violation shape (QA1 round 1 regression test) =="
if ! USER_SAID_SELFTEST_OUT=$(python3 "$REPO_ROOT/scripts/check_user_said_guard.py" --selftest 2>&1); then
  fail "check_user_said_guard.py --selftest found a detection regression:\n$USER_SAID_SELFTEST_OUT"
fi
echo "$USER_SAID_SELFTEST_OUT" | grep -q "USER_SAID_GUARD_SELFTEST_OK" || fail "check_user_said_guard.py --selftest did not report OK"

echo "== Req 4a (sprint 41): no code path supplies --user-said/--user-said-file content =="
if ! USER_SAID_GUARD_OUT=$(python3 "$REPO_ROOT/scripts/check_user_said_guard.py" 2>&1); then
  fail "check_user_said_guard.py found a violation:\n$USER_SAID_GUARD_OUT"
fi
echo "$USER_SAID_GUARD_OUT" | grep -q "USER_SAID_GUARD_OK" || fail "check_user_said_guard.py did not report OK"

# Sprint 41, Req 4b: the reverse assertion, also against the REAL
# docs/sprints/state/ in REPO_ROOT (this repository's own actual close
# history), not the sandbox -- every sprint this project has ever actually
# closed must carry a non-empty --user-said on record. See
# scripts/check_user_said_history.py's own header for the full method.
echo "== Req 4b (sprint 41): every recorded sprint close carries a non-empty --user-said =="
if ! USER_SAID_HISTORY_OUT=$(python3 "$REPO_ROOT/scripts/check_user_said_history.py" 2>&1); then
  fail "check_user_said_history.py found a violation:\n$USER_SAID_HISTORY_OUT"
fi
echo "$USER_SAID_HISTORY_OUT" | grep -q "USER_SAID_HISTORY_OK" || fail "check_user_said_history.py did not report OK"

# Content hash of every file under docs/sprints/, used to assert a command
# (like `gates`) that claims to be read-only actually didn't write anything.
sprints_hash() {
  find docs/sprints -type f -exec sha256sum {} \; | sort | sha256sum
}

# ship's tree-hash check needs a real git repo to resolve commits against,
# entirely local to the sandbox, never the invoking repo.
git init -q
git config user.email "smoke-test@example.com"
git config user.name "Smoke Test"
git add -A
git commit -q -m "sandbox baseline"

# Sprint 24, Req 2: check_ci_status() shells out to the real `gh` CLI, and
# this sandbox is a throwaway `git init` repo with no GitHub remote at
# all -- confirmed directly that real `gh` fails fast and cleanly against
# exactly that shape ("failed to determine base repo: no git remotes
# found", non-zero exit, no hang, no network attempt), which is why every
# OTHER `ship` call in this file below, unmodified, safely lands on
# CI_STATUS_UNDETERMINABLE and never gates -- this sandbox needing no
# retrofit anywhere else is itself evidence the undeterminable path is
# safe by default. But testing the RED and "success with no real steps"
# paths needs a `gh` that can actually claim a run exists. Faithful to
# this project's own precedent for exactly this shape of problem
# (launcher_test.js's withFakeClaude(): a fake executable installed at
# the front of PATH only for the specific invocation under test, real
# subprocess behaviour underneath, never left in place for any other
# test in this file): a fake `gh`, controlled by $FAKE_GH_MODE, prepended
# to PATH only on the individual command lines that need it below.
FAKE_GH_DIR="$SANDBOX/.fake-gh"
mkdir -p "$FAKE_GH_DIR"
cat > "$FAKE_GH_DIR/gh" <<'FAKEGH'
#!/usr/bin/env bash
case "$1 $2" in
  "run list")
    case "${FAKE_GH_MODE:-}" in
      pending) echo '[{"databaseId":1,"conclusion":null,"status":"in_progress","workflowName":"CI"}]' ;;
      red) echo '[{"databaseId":1,"conclusion":"failure","status":"completed","workflowName":"CI"}]' ;;
      no-steps) echo '[{"databaseId":1,"conclusion":"success","status":"completed","workflowName":"CI"}]' ;;
      green) echo '[{"databaseId":1,"conclusion":"success","status":"completed","workflowName":"CI"}]' ;;
      *) echo "[]" ;;
    esac
    ;;
  "run view")
    case "${FAKE_GH_MODE:-}" in
      no-steps) echo '{"jobs":[{"name":"build","conclusion":"success","steps":[{"name":"Set up job","status":"completed","conclusion":"success"},{"name":"npm ci","status":"completed","conclusion":"skipped"},{"name":"Complete job","status":"completed","conclusion":"success"}]}]}' ;;
      green) echo '{"jobs":[{"name":"build","conclusion":"success","steps":[{"name":"Set up job","status":"completed","conclusion":"success"},{"name":"Run tests","status":"completed","conclusion":"success"}]}]}' ;;
      *) echo '{"jobs":[]}' ;;
    esac
    ;;
  *) exit 1 ;;
esac
FAKEGH
chmod +x "$FAKE_GH_DIR/gh"

# docs/sprints/ doesn't exist yet at this point in the sandbox, so this also
# covers the "no state directory at all" path, not just "zero completed
# sprints with a state dir present".
echo "== gates: zero completed sprints prints a clean no-data message, not zeroes or a traceback =="
$SCRIPT gates > /tmp/gates_out.txt 2>&1 || fail "gates exited non-zero with no sprint data"
grep -q "Nothing to aggregate" /tmp/gates_out.txt || fail "gates zero-data message missing"
grep -qE "Traceback" /tmp/gates_out.txt && fail "gates raised a traceback on zero completed sprints"
# Sprint 7, Req 6: "there is nothing here" is the exact sentence that
# produced every one of the wrong readings this sprint exists to fix —
# this is the only point in this script where zero sprints exist at all,
# so it's the only place cmd_status/cmd_list/cmd_gates's "no data yet"
# branches (as opposed to load_state's "no state file for sprint N",
# checked separately below once sprints exist) can be exercised.
grep -qF "fully-completely-smoke" /tmp/gates_out.txt || fail "gates' no-data message doesn't name the tree it looked in"
grep -q "branch:" /tmp/gates_out.txt || fail "gates' no-data message doesn't name the branch"
rm -f /tmp/gates_out.txt

echo "== status/list: zero sprints also names the tree (sprint 7, Req 6) =="
$SCRIPT status > /tmp/out.txt 2>&1 || fail "status with no id and no sprints exited non-zero"
grep -q "No sprints yet in" /tmp/out.txt || fail "status's no-sprints message doesn't name the tree"
grep -qF "fully-completely-smoke" /tmp/out.txt || fail "status's no-sprints message doesn't actually name this sandbox"

$SCRIPT list > /tmp/out.txt 2>&1 || fail "list with no sprints exited non-zero"
grep -q "No sprints yet in" /tmp/out.txt || fail "list's no-sprints message doesn't name the tree"
grep -qF "fully-completely-smoke" /tmp/out.txt || fail "list's no-sprints message doesn't actually name this sandbox"
rm -f /tmp/out.txt

echo "== tree-naming degrades gracefully when git itself is unavailable, never crashing a read-only command (Req 7) =="
PY3_DIR="$(dirname "$(command -v python3)")"
NOGIT_PATH_OUT=$(PATH="$PY3_DIR" $SCRIPT status 2>&1) || fail "status crashed when git was unavailable on PATH"
echo "$NOGIT_PATH_OUT" | grep -qE "Traceback" && fail "status raised a traceback when git was unavailable on PATH"
echo "$NOGIT_PATH_OUT" | grep -q "branch unknown" || fail "status should degrade to 'branch unknown' when git can't be found, not omit the tree entirely or crash"

# Captures the numeric sprint id `new` just created from its own stdout,
# rather than assuming IDs increment one-per-test. Some tests below create a
# sprint without ever `start`-ing it (the injection regression test), which
# shifts every later hand-counted ID by one; that drift previously caused a
# later test block to accidentally re-`start` (and wipe the history of) an
# unrelated sprint from an earlier block. Reading the real ID back out
# instead of counting by hand makes that class of bug impossible.
new_sprint() {
  local out id
  out=$($SCRIPT new "$@")
  id=$(echo "$out" | grep -oE 'Created sprint [0-9]+' | grep -oE '[0-9]+')
  [ -n "$id" ] || fail "could not parse a sprint id out of 'new' output: $out"
  echo "$id"
}

echo "== happy path with both fail-loops =="
SPRINT_1=$(new_sprint "Smoke test sprint" --epic "CI")
$SCRIPT start "$SPRINT_1" > /dev/null
$SCRIPT qa1 "$SPRINT_1" --verdict FAIL --notes "expected fail" > /dev/null
git commit -q --allow-empty -m "address QA1 feedback for sprint $SPRINT_1"
$SCRIPT qa1 "$SPRINT_1" --verdict PASS --notes "ok" > /dev/null
$SCRIPT dev-done "$SPRINT_1" > /dev/null
AUDITED_COMMIT_1=$(git rev-parse HEAD)

echo "== sprint 15, Req 4: ship --commit HEAD prints the resolved SHA, not the literal ref =="
SHIP_OUT_1=$($SCRIPT ship "$SPRINT_1" --commit HEAD 2>&1)
echo "$SHIP_OUT_1" | grep -qF "shipped (commit ${AUDITED_COMMIT_1})" || \
  fail "ship --commit HEAD should print the resolved SHA ($AUDITED_COMMIT_1) -- got: $SHIP_OUT_1"
echo "$SHIP_OUT_1" | grep -qF "shipped (commit HEAD)" && \
  fail "ship --commit HEAD printed the raw ref 'HEAD' instead of resolving it -- Req 4 regression"

LIVEQA_FAIL_OUT_1=$($SCRIPT liveqa "$SPRINT_1" --deployed-commit "$AUDITED_COMMIT_1" --verdict FAIL --notes "expected fail" 2>&1)
# Sprint 36, Req 4: a LiveQA FAIL/CONDITIONAL used to tell the reader
# "Dev Team: fix, then Pipeman: /sprint-reship" with no QA1 audit step in
# between, even though cmd_reship itself has refused an unaudited commit
# since this same sprint. Must name the audit step now. Found by the
# user, reported directly to Dev Team 1 -- NOT a QA1 catch: QA1 missed it
# across all three gate-1 rounds and its own live-loop audit of 711c5fc.
echo "$LIVEQA_FAIL_OUT_1" | grep -q "QA1 audits the fix on that exact commit" || \
  fail "a LiveQA FAIL's printed next-step message doesn't mention QA1 auditing the fix -- got: $LIVEQA_FAIL_OUT_1"
echo "$LIVEQA_FAIL_OUT_1" | grep -qF "/sprint-qa1 ${SPRINT_1} --verdict" || \
  fail "a LiveQA FAIL's printed next-step message doesn't name the /sprint-qa1 recovery command"
echo "$LIVEQA_FAIL_OUT_1" | grep -q "Dev Team: fix, then Pipeman: /sprint-reship\.$" && \
  fail "a LiveQA FAIL's printed next-step message still describes the pre-Req-3 loop with no audit step"
# Real content, not --allow-empty: an empty commit's tree is identical to
# its parent's, which would coincidentally already match gate 1's own
# audited tree (self-correcting per Req 3) and defeat the refusal test
# just below.
echo "fix for sprint $SPRINT_1" > "sprint${SPRINT_1}-fix.txt"
git add "sprint${SPRINT_1}-fix.txt"
git commit -q -m "fix for sprint $SPRINT_1"
FIX_COMMIT_1=$(git rev-parse HEAD)

echo "== sprint 36, Req 3: reship refuses when no QA1 verdict is on record for the reshipped commit's exact tree =="
RESHIP_REFUSE_1=$($SCRIPT reship "$SPRINT_1" --commit "$FIX_COMMIT_1" 2>&1) && \
  fail "reship succeeded on a commit with no QA1 audit recorded for its tree -- Req 3 regression"
echo "$RESHIP_REFUSE_1" | grep -q "has no QA1 PASS currently on record for it" || \
  fail "reship's unaudited-tree refusal message is missing -- got: $RESHIP_REFUSE_1"
echo "$RESHIP_REFUSE_1" | grep -q "no QA1 verdict is on record for it at all" || \
  fail "reship's refusal should say no verdict at all is on record (not merely 'not PASS') when there truly is none -- got: $RESHIP_REFUSE_1"
echo "$RESHIP_REFUSE_1" | grep -qF "$FIX_COMMIT_1" || fail "reship's refusal doesn't name the commit being reshipped"
echo "$RESHIP_REFUSE_1" | grep -qF "/sprint-qa1 ${SPRINT_1} --verdict" || fail "reship's refusal doesn't name the QA1 recovery command"
python3 -c "
import json
s = json.load(open('docs/sprints/state/sprint-${SPRINT_1}.json'))
assert s['last_shipped_commit'] == '$AUDITED_COMMIT_1', 'a refused reship must not have changed last_shipped_commit'
"

echo "== sprint 36, Req 3: a live-loop QA1 PASS on this exact commit unblocks reship, without ever touching gate 1's own fields =="
GATE1_TREE_1=$(python3 -c "import json; print(json.load(open('docs/sprints/state/sprint-${SPRINT_1}.json'))['qa1_audited_tree_hash'])")
$SCRIPT qa1 "$SPRINT_1" --verdict PASS --notes "live-loop audit of the fix" --commit "$FIX_COMMIT_1" > /dev/null || \
  fail "a live-loop audit PASS with --commit should have succeeded"

RESHIP_OUT_1=$($SCRIPT reship "$SPRINT_1" --commit "$FIX_COMMIT_1" 2>&1) || \
  fail "reship should have succeeded once a live-loop PASS was recorded for this exact tree -- output: $RESHIP_OUT_1"
echo "$RESHIP_OUT_1" | grep -qF "fix reshipped (commit ${FIX_COMMIT_1})" || \
  fail "reship should also print the resolved SHA, same fix as ship -- got: $RESHIP_OUT_1"
echo "$RESHIP_OUT_1" | grep -q "QA1 PASS is on record for this exact tree" || \
  fail "reship's output should say a QA1 PASS is on record for this exact tree"
echo "$RESHIP_OUT_1" | grep -q "not a substitute for LiveQA" || \
  fail "reship's output should still foreclose the LiveQA-substitutes-for-QA1 conflation"
python3 -c "
import json
s = json.load(open('docs/sprints/state/sprint-${SPRINT_1}.json'))
assert s['qa1_audited_tree_hash'] == '$GATE1_TREE_1', 'a live-loop audit must never touch gate 1\'s own audited tree hash'
entries = s.get('live_loop_audit_trees', [])
assert len(entries) == 1, f'expected exactly one live-loop-audit-tree entry: {entries}'
assert entries[0]['commit'] == '$FIX_COMMIT_1', f'wrong commit recorded: {entries[0]}'
assert entries[0]['verdict'] == 'PASS', f'wrong verdict recorded: {entries[0]}'
"
$SCRIPT liveqa "$SPRINT_1" --deployed-commit "$FIX_COMMIT_1" --verdict PASS --notes "ok" > /dev/null

echo "== complete refuses (no override) without a non-empty --user-said, even with both gates PASS =="
$SCRIPT complete "$SPRINT_1" > /tmp/out.txt 2>&1 && fail "complete succeeded with no --user-said at all, despite both gates passing" || true
grep -q -- "--user-said is required" /tmp/out.txt || fail "missing --user-said refusal message missing"

$SCRIPT complete "$SPRINT_1" --user-said "   " > /tmp/out.txt 2>&1 && fail "complete succeeded with a whitespace-only --user-said" || true
grep -q -- "--user-said is required" /tmp/out.txt || fail "whitespace-only --user-said refusal message missing"
rm -f /tmp/out.txt

$SCRIPT complete "$SPRINT_1" --user-said "close sprint 1, both gates look good" > /dev/null
STATUS=$($SCRIPT status "$SPRINT_1")
echo "$STATUS" | grep -q "Phase: complete" || fail "sprint $SPRINT_1 did not reach complete"
echo "$STATUS" | grep -q "QA1 audit result: PASS" || fail "qa1 result not recorded"
echo "$STATUS" | grep -q "LiveQA live result: PASS" || fail "liveqa result not recorded"
$SCRIPT status "$SPRINT_1" --verbose | grep -q "close sprint 1, both gates look good" || fail "the --user-said text was not recorded in the sprint's history"

echo "== completion actually relocates the file and updates its frontmatter, not just the phase =="
DONE_FILE=$(find docs/sprints/3-done -name "sprint-${SPRINT_1}_*.md" 2>/dev/null)
[ -n "$DONE_FILE" ] || fail "sprint $SPRINT_1's file was not moved to docs/sprints/3-done/"
[ ! -e "docs/sprints/2-in-progress/sprint-${SPRINT_1}_smoke-test-sprint.md" ] || fail "sprint $SPRINT_1's file is still in 2-in-progress/"
grep -q '^status: done$' "$DONE_FILE" || fail "sprint $SPRINT_1's file frontmatter status was not updated to done"

echo "== gates: one completed sprint (with a GT fail after a normal ship) is an audited miss, not a rate =="
GATES_HASH_BEFORE=$(sprints_hash)
GATES_OUT=$($SCRIPT gates)
GATES_HASH_AFTER=$(sprints_hash)
[ "$GATES_HASH_BEFORE" = "$GATES_HASH_AFTER" ] || fail "gates modified docs/sprints/ (should be strictly read-only)"
echo "$GATES_OUT" | grep -q "single data point, not a rate" || fail "gates didn't flag a single completed sprint as non-statistical"
echo "$GATES_OUT" | grep -q "Audited miss.*: 1 - sprints: ${SPRINT_1}$" || fail "gates didn't count sprint $SPRINT_1's GT fail (after a normal ship) as an audited miss"
echo "$GATES_OUT" | grep -q "Unaudited-fix miss.*: 0 " || fail "gates should show zero unaudited-fix misses so far"
echo "$GATES_OUT" | grep -q "LiveQA: 1 of 1 - sprints: \[${SPRINT_1}\]" || fail "gates didn't record sprint $SPRINT_1 under LiveQA's non-PASS catch"
echo "$GATES_OUT" | grep -q "QA1: 1 of 1 - sprints: \[${SPRINT_1}\]" || fail "gates should count sprint $SPRINT_1 under QA1's non-PASS catch (it had an initial FAIL round)"

echo "== refusal paths =="
SPRINT_2=$(new_sprint "Edge case sprint")
$SCRIPT start "$SPRINT_2" > /dev/null

$SCRIPT qa1 "$SPRINT_2" --verdict MAYBE > /tmp/out.txt 2>&1 && fail "bad verdict was accepted" || true
grep -q "Verdict must be one of" /tmp/out.txt || fail "bad verdict error message missing"

$SCRIPT ship "$SPRINT_2" --commit x > /tmp/out.txt 2>&1 && fail "shipped before qa1/dev-done" || true
grep -q "Pipeman can't ship yet" /tmp/out.txt || fail "ship-too-early error message missing"

$SCRIPT complete "$SPRINT_2" --user-said "trying to close it early" > /tmp/out.txt 2>&1 && fail "closed before any gate passed" || true
grep -q "not ready to close" /tmp/out.txt || fail "early-complete error message missing"

echo "" > /tmp/blank.txt
$SCRIPT new --title-file /tmp/blank.txt > /tmp/out.txt 2>&1 && fail "empty title was accepted" || true
grep -q "title cannot be empty" /tmp/out.txt || fail "empty-title error message missing"

$SCRIPT status 999 > /tmp/out.txt 2>&1 && fail "nonexistent sprint returned success" || true
grep -q "No state file for sprint 999" /tmp/out.txt || fail "nonexistent-sprint error message missing"
# Sprint 7, Req 6: load_state's absence message must name which tree it
# looked in — this is the exact sentence a wrong-checkout status read
# produced four confidently-wrong answers against before this sprint.
grep -qF "fully-completely-smoke" /tmp/out.txt || fail "load_state's absence message doesn't name the tree it looked in"
grep -q "branch:" /tmp/out.txt || fail "load_state's absence message doesn't name the branch"

echo "== injection regression: malicious text via --title-file must be inert =="
rm -f /tmp/PWNED
printf 'Fix login"; touch /tmp/PWNED; echo "done' > /tmp/evil.txt
$SCRIPT new --title-file /tmp/evil.txt > /dev/null
[ -f /tmp/PWNED ] && fail "injection payload executed, --title-file did not neutralize it"
rm -f /tmp/evil.txt /tmp/PWNED /tmp/out.txt

echo "== two independent sprints running concurrently =="
SPRINT_A=$(new_sprint "Parallel sprint A")
$SCRIPT start "$SPRINT_A" > /dev/null
SPRINT_B=$(new_sprint "Parallel sprint B")
$SCRIPT start "$SPRINT_B" > /dev/null
$SCRIPT qa1 "$SPRINT_A" --verdict PASS --notes ok > /dev/null
$SCRIPT status "$SPRINT_B" | grep -q "Phase: dev_build" || fail "sprint $SPRINT_B state was affected by sprint $SPRINT_A's transition"

echo "== dev-done refuses (no override) if the sprint file changed since QA1's PASS =="
SPRINT_STALE=$(new_sprint "Stale audit sprint")
$SCRIPT start "$SPRINT_STALE" > /dev/null
$SCRIPT qa1 "$SPRINT_STALE" --verdict PASS --notes "looked good" > /dev/null
STALE_FILE=$(find docs/sprints/2-in-progress -name "sprint-${SPRINT_STALE}_*.md")
echo "### Requirements amended after audit" >> "$STALE_FILE"

$SCRIPT dev-done "$SPRINT_STALE" > /tmp/out.txt 2>&1 && fail "dev-done succeeded despite sprint file changing after QA1's PASS" || true
grep -q "has changed since QA1's PASS" /tmp/out.txt || fail "stale-audit refusal message missing"
grep -q "\-\-override" /tmp/out.txt && fail "refusal message must not offer an override"

$SCRIPT qa1 "$SPRINT_STALE" --verdict PASS --notes "re-audited the amendment" > /dev/null
$SCRIPT dev-done "$SPRINT_STALE" > /dev/null || fail "dev-done still refused after a fresh QA1 PASS on the current file"
rm -f /tmp/out.txt

echo "== ship refuses (no override) if the commit's content differs from what QA1 audited =="
SPRINT_DRIFT=$(new_sprint "Commit drift sprint")
$SCRIPT start "$SPRINT_DRIFT" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_DRIFT initial work"
$SCRIPT qa1 "$SPRINT_DRIFT" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_DRIFT" > /dev/null
# a real content change lands after QA1's PASS, unaudited
echo "sneaky change" > sneaky.txt
git add sneaky.txt
git commit -q -m "unaudited change after QA1 PASS"
DRIFTED_COMMIT=$(git rev-parse HEAD)

$SCRIPT ship "$SPRINT_DRIFT" --commit "$DRIFTED_COMMIT" > /tmp/out.txt 2>&1 && fail "ship succeeded on a commit QA1 never audited" || true
grep -q "doesn't match what QA1 audited" /tmp/out.txt || fail "commit-drift refusal message missing"
grep -q "\-\-override" /tmp/out.txt && fail "commit-drift refusal message must not offer an override"

echo "== ship tolerates a content-preserving amend/rebase after a fresh QA1 PASS (tree hash, not commit SHA) =="
$SCRIPT qa1 "$SPRINT_DRIFT" --verdict PASS --notes "re-audited the sneaky change" > /dev/null
$SCRIPT dev-done "$SPRINT_DRIFT" > /dev/null   # a fresh qa1 PASS resets phase, dev-done must be re-run before ship
# simulate Pipeman's documented squash/rebase step: same file content, new SHA
git commit -q --amend -m "sprint $SPRINT_DRIFT work (squashed for history hygiene)"
AMENDED_COMMIT=$(git rev-parse HEAD)
[ "$AMENDED_COMMIT" != "$DRIFTED_COMMIT" ] || fail "test setup broken: amend did not change the commit SHA"
$SCRIPT ship "$SPRINT_DRIFT" --commit "$AMENDED_COMMIT" > /dev/null || fail "ship refused a content-identical commit just because rebase/amend changed its SHA"
rm -f /tmp/out.txt

echo "== dev-done/ship give a distinct 'nothing recorded' message for a pre-upgrade sprint missing the hash fields =="
SPRINT_LEGACY=$(new_sprint "Legacy sprint")
$SCRIPT start "$SPRINT_LEGACY" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_LEGACY work"
$SCRIPT qa1 "$SPRINT_LEGACY" --verdict PASS --notes "looked good" > /dev/null
LEGACY_STATE="docs/sprints/state/sprint-${SPRINT_LEGACY}.json"
# simulate a sprint that PASSed under a version of this script from before
# the hash fields existed, by stripping them out of an otherwise-valid PASS
python3 -c "
import json
p = '$LEGACY_STATE'
s = json.load(open(p))
del s['qa1_audit_file_hash']
del s['qa1_audited_tree_hash']
json.dump(s, open(p, 'w'), indent=2)
"

$SCRIPT dev-done "$SPRINT_LEGACY" > /tmp/out.txt 2>&1 && fail "dev-done succeeded on a sprint with no recorded audit hash" || true
grep -q "no QA1-audited sprint-file hash on record" /tmp/out.txt || fail "legacy-sprint dev-done message missing"
grep -q "has changed since QA1's PASS" /tmp/out.txt && fail "legacy sprint should not be told the file 'changed', nothing was ever recorded to compare against"

$SCRIPT qa1 "$SPRINT_LEGACY" --verdict PASS --notes "re-audited under the upgraded script" > /dev/null
$SCRIPT dev-done "$SPRINT_LEGACY" > /dev/null || fail "dev-done still failed after a fresh QA1 PASS backfilled the hash fields"

# repeat the same distinction one step later, for ship's tree-hash field
python3 -c "
import json
p = '$LEGACY_STATE'
s = json.load(open(p))
del s['qa1_audited_tree_hash']
json.dump(s, open(p, 'w'), indent=2)
"
LEGACY_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_LEGACY" --commit "$LEGACY_COMMIT" > /tmp/out.txt 2>&1 && fail "ship succeeded on a sprint with no recorded audited commit" || true
grep -q "no QA1-audited commit on record" /tmp/out.txt || fail "legacy-sprint ship message missing"
grep -q "doesn't match what QA1 audited" /tmp/out.txt && fail "legacy sprint should not be told the commit 'doesn't match', nothing was ever recorded to compare against"
rm -f /tmp/out.txt

echo "== sprint 41, Req 5a: a newly-created sprint file carries the template's Human Prerequisites section, between Dependencies and Team Assignments =="
SPRINT_HP=$(new_sprint "Human prerequisites template sprint")
HP_FILE=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_HP}']['file'])")
grep -q '^### Human Prerequisites$' "$HP_FILE" || fail "new sprint file is missing the '### Human Prerequisites' section header"
# Order matters -- the sprint's own text says it belongs between Dependencies
# and Team Assignments, not merely present somewhere in the file.
DEP_LINE=$(grep -n '^### Dependencies$' "$HP_FILE" | head -1 | cut -d: -f1)
HP_LINE=$(grep -n '^### Human Prerequisites$' "$HP_FILE" | head -1 | cut -d: -f1)
TEAM_LINE=$(grep -n '^### Team Assignments$' "$HP_FILE" | head -1 | cut -d: -f1)
[ -n "$DEP_LINE" ] && [ -n "$HP_LINE" ] && [ -n "$TEAM_LINE" ] || fail "could not locate all three section headers in the new sprint file"
[ "$DEP_LINE" -lt "$HP_LINE" ] && [ "$HP_LINE" -lt "$TEAM_LINE" ] || \
  fail "Human Prerequisites is not positioned between Dependencies and Team Assignments (lines: Dependencies=$DEP_LINE, Human Prerequisites=$HP_LINE, Team Assignments=$TEAM_LINE)"

echo "== a custom template containing literal braces doesn't break sprint creation =="
printf '\n### Example config\n```json\n{ "key": "value" }\n```\n' >> templates/sprint-template.md
$SCRIPT new "Brace test sprint" > /dev/null || fail "sprint creation broke on a template containing literal { }"

echo "== concurrent writes to the same sprint don't corrupt state or lose an update (file locking) =="
SPRINT_RACE=$(new_sprint "Race sprint")
$SCRIPT start "$SPRINT_RACE" > /dev/null
( $SCRIPT qa1 "$SPRINT_RACE" --verdict FAIL --notes "race A" > /dev/null 2>&1 ) &
RACE_PID1=$!
( $SCRIPT qa1 "$SPRINT_RACE" --verdict CONDITIONAL --notes "race B" > /dev/null 2>&1 ) &
RACE_PID2=$!
wait "$RACE_PID1" "$RACE_PID2"
RACE_STATUS=$($SCRIPT status "$SPRINT_RACE" --verbose)
echo "$RACE_STATUS" | grep -q "rounds: 2" || fail "concurrent qa1 writes lost an update, expected audit_rounds: 2"
python3 -c "import json; json.load(open('docs/sprints/state/sprint-${SPRINT_RACE}.json'))" || fail "sprint $SPRINT_RACE state file is corrupted JSON after concurrent writes"

echo "== override refuses without the exact --confirm value, and without a --reason =="
SPRINT_OVR_REFUSAL=$(new_sprint "Override refusal sprint")
$SCRIPT start "$SPRINT_OVR_REFUSAL" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_OVR_REFUSAL work"
$SCRIPT qa1 "$SPRINT_OVR_REFUSAL" --verdict PASS --notes "looked good" > /dev/null

$SCRIPT override "$SPRINT_OVR_REFUSAL" --gate dev-done-hash --reason "test" --confirm YES > /tmp/out.txt 2>&1 && fail "override succeeded with the wrong --confirm value" || true
grep -q "must be exactly the literal word OVERRIDE" /tmp/out.txt || fail "wrong-confirm refusal message missing"

$SCRIPT override "$SPRINT_OVR_REFUSAL" --gate dev-done-hash --confirm OVERRIDE > /tmp/out.txt 2>&1 && fail "override succeeded with an empty --reason" || true
grep -q -- "--reason is required" /tmp/out.txt || fail "empty-reason refusal message missing"
rm -f /tmp/out.txt

echo "== override unsticks a stale sprint-file hash, and is permanently logged with the given reason =="
STALE_FILE_OVR=$(find docs/sprints/2-in-progress -name "sprint-${SPRINT_OVR_REFUSAL}_*.md")
echo "### amendment after audit" >> "$STALE_FILE_OVR"
$SCRIPT dev-done "$SPRINT_OVR_REFUSAL" > /tmp/out.txt 2>&1 && fail "dev-done succeeded despite a stale hash (test setup broken)" || true
grep -q "has changed since QA1's PASS" /tmp/out.txt || fail "expected stale-hash refusal did not occur"

$SCRIPT override "$SPRINT_OVR_REFUSAL" --gate dev-done-hash --reason "reviewed the amendment personally, cosmetic only" --confirm OVERRIDE > /dev/null || fail "override refused despite a valid --confirm and --reason"
$SCRIPT dev-done "$SPRINT_OVR_REFUSAL" > /dev/null || fail "dev-done still refused after a valid override re-stamped the hash"
OVERRIDE_STATUS=$($SCRIPT status "$SPRINT_OVR_REFUSAL" --verbose)
echo "$OVERRIDE_STATUS" | grep -q "human-override" || fail "override was not recorded in the sprint's history"
echo "$OVERRIDE_STATUS" | grep -q "reviewed the amendment personally" || fail "override reason was not recorded in the sprint's history"
rm -f /tmp/out.txt

echo "== dev-done-hash override refuses on the right sprint but the wrong phase, with an accurate message (not 'no PASS') =="
$SCRIPT override "$SPRINT_OVR_REFUSAL" --gate dev-done-hash --reason "trying to re-use this gate after dev-done already succeeded" --confirm OVERRIDE > /tmp/out.txt 2>&1 && fail "dev-done-hash override succeeded on a sprint already past qa1_audit phase" || true
grep -q "no QA1 PASS on record" /tmp/out.txt && fail "wrong-phase refusal must not claim there's no PASS on record, this sprint has one"
grep -q "not qa1_audit" /tmp/out.txt || fail "wrong-phase refusal message missing or not phase-specific"
rm -f /tmp/out.txt

echo "== override on a sprint QA1 never actually passed still refuses (it overrides drift, not a missing PASS) =="
SPRINT_NEVER_AUDITED=$(new_sprint "Never audited sprint")
$SCRIPT start "$SPRINT_NEVER_AUDITED" > /dev/null
$SCRIPT override "$SPRINT_NEVER_AUDITED" --gate dev-done-hash --reason "trying to skip QA1 entirely" --confirm OVERRIDE > /tmp/out.txt 2>&1 && fail "override let a sprint bypass QA1 entirely" || true
grep -q "no QA1 PASS on record" /tmp/out.txt || fail "no-real-PASS refusal message missing"
rm -f /tmp/out.txt

echo "== ship-hash override refuses in the wrong phase (the precondition that keeps it from bypassing QA1) =="
SPRINT_SHIP_WRONG_PHASE=$(new_sprint "Ship override wrong phase sprint")
$SCRIPT start "$SPRINT_SHIP_WRONG_PHASE" > /dev/null
$SCRIPT override "$SPRINT_SHIP_WRONG_PHASE" --gate ship-hash --reason "trying to stamp a ship hash before dev work is even agreed done" --confirm OVERRIDE > /tmp/out.txt 2>&1 && fail "ship-hash override succeeded on a sprint not yet dev_agreed_done" || true
grep -q "not ready to ship" /tmp/out.txt || fail "ship-hash wrong-phase refusal message missing"
rm -f /tmp/out.txt

echo "== override unsticks a commit-content mismatch at ship time, and is permanently logged with the given reason =="
SPRINT_SHIP_OVR=$(new_sprint "Ship override sprint")
$SCRIPT start "$SPRINT_SHIP_OVR" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_SHIP_OVR initial work"
$SCRIPT qa1 "$SPRINT_SHIP_OVR" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_SHIP_OVR" > /dev/null
echo "unaudited" > "sprint${SPRINT_SHIP_OVR}-sneaky.txt"
git add "sprint${SPRINT_SHIP_OVR}-sneaky.txt"
git commit -q -m "unaudited change after PASS"
SHIP_OVERRIDE_COMMIT=$(git rev-parse HEAD)

$SCRIPT ship "$SPRINT_SHIP_OVR" --commit "$SHIP_OVERRIDE_COMMIT" > /tmp/out.txt 2>&1 && fail "ship succeeded despite a content mismatch (test setup broken)" || true
grep -q "doesn't match what QA1 audited" /tmp/out.txt || fail "expected ship-time content-mismatch refusal did not occur"

$SCRIPT override "$SPRINT_SHIP_OVR" --gate ship-hash --reason "reviewed the extra commit personally, safe to ship" --confirm OVERRIDE > /dev/null || fail "ship-hash override refused despite a valid --confirm and --reason"
$SCRIPT ship "$SPRINT_SHIP_OVR" --commit "$SHIP_OVERRIDE_COMMIT" > /dev/null || fail "ship still refused after a valid ship-hash override"
SHIP_OVERRIDE_STATUS=$($SCRIPT status "$SPRINT_SHIP_OVR" --verbose)
echo "$SHIP_OVERRIDE_STATUS" | grep -q "human-override" || fail "ship-hash override was not recorded in the sprint's history"
echo "$SHIP_OVERRIDE_STATUS" | grep -q "reviewed the extra commit personally" || fail "ship-hash override reason was not recorded in the sprint's history"
rm -f /tmp/out.txt

echo "== gates: a GT fail after a reship is an unaudited-fix miss, never folded into the audited bucket =="
SPRINT_UNAUDITED=$(new_sprint "Unaudited fix miss sprint")
$SCRIPT start "$SPRINT_UNAUDITED" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_UNAUDITED work"
$SCRIPT qa1 "$SPRINT_UNAUDITED" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_UNAUDITED" > /dev/null
UNAUDITED_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_UNAUDITED" --commit "$UNAUDITED_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_UNAUDITED" --deployed-commit "$UNAUDITED_COMMIT" --verdict FAIL --notes "first fail, audited miss" > /dev/null
git commit -q --allow-empty -m "fix1 for sprint $SPRINT_UNAUDITED"
FIX1_COMMIT=$(git rev-parse HEAD)
# Sprint 36, Req 3: reship now requires a QA1 verdict on record for the
# exact tree being reshipped -- a live-loop PASS unblocks it (this is
# still, per cmd_gates' own unchanged classification below, an
# unaudited-fix miss: a live-loop record is not a fresh gate-1 audit,
# see cmd_reship's own docstring).
$SCRIPT qa1 "$SPRINT_UNAUDITED" --verdict PASS --notes "live-loop audit" --commit "$FIX1_COMMIT" > /dev/null
$SCRIPT reship "$SPRINT_UNAUDITED" --commit "$FIX1_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_UNAUDITED" --deployed-commit "$FIX1_COMMIT" --verdict FAIL --notes "second fail, unaudited miss" > /dev/null
git commit -q --allow-empty -m "fix2 for sprint $SPRINT_UNAUDITED"
FIX2_COMMIT=$(git rev-parse HEAD)
$SCRIPT qa1 "$SPRINT_UNAUDITED" --verdict PASS --notes "live-loop audit" --commit "$FIX2_COMMIT" > /dev/null
$SCRIPT reship "$SPRINT_UNAUDITED" --commit "$FIX2_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_UNAUDITED" --deployed-commit "$FIX2_COMMIT" --verdict PASS --notes ok > /dev/null
$SCRIPT complete "$SPRINT_UNAUDITED" --user-said "close it, both misses are understood" > /dev/null

echo "== gates: a completed sprint that needed a dev-done-hash override is counted under hash-drift, not miscounted as a gate override =="
SPRINT_GATES_OVR=$(new_sprint "Gates override sprint")
$SCRIPT start "$SPRINT_GATES_OVR" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_GATES_OVR work"
$SCRIPT qa1 "$SPRINT_GATES_OVR" --verdict PASS --notes ok > /dev/null
GATES_OVR_FILE=$(find docs/sprints/2-in-progress -name "sprint-${SPRINT_GATES_OVR}_*.md")
echo "### amended after audit" >> "$GATES_OVR_FILE"
$SCRIPT override "$SPRINT_GATES_OVR" --gate dev-done-hash --reason "reviewed, cosmetic only" --confirm OVERRIDE > /dev/null
$SCRIPT dev-done "$SPRINT_GATES_OVR" > /dev/null
GATES_OVR_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_GATES_OVR" --commit "$GATES_OVR_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_GATES_OVR" --deployed-commit "$GATES_OVR_COMMIT" --verdict PASS --notes ok > /dev/null
$SCRIPT complete "$SPRINT_GATES_OVR" --user-said "close it" > /dev/null

echo "== gates: a GT fail after a ship-hash-overridden ship is NOT an audited miss (content that shipped was never QA1's) =="
SPRINT_SHIP_OVR_MISS=$(new_sprint "Ship override miss sprint")
$SCRIPT start "$SPRINT_SHIP_OVR_MISS" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_SHIP_OVR_MISS initial work"
$SCRIPT qa1 "$SPRINT_SHIP_OVR_MISS" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_SHIP_OVR_MISS" > /dev/null
echo "unaudited content" > "sprint${SPRINT_SHIP_OVR_MISS}-drift.txt"
git add "sprint${SPRINT_SHIP_OVR_MISS}-drift.txt"
git commit -q -m "unaudited change after PASS"
DRIFT_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_SHIP_OVR_MISS" --commit "$DRIFT_COMMIT" > /tmp/out.txt 2>&1 && fail "ship succeeded on drifted content (test setup broken)" || true
$SCRIPT override "$SPRINT_SHIP_OVR_MISS" --gate ship-hash --reason "reviewed the drift personally, safe to ship" --confirm OVERRIDE > /dev/null
$SCRIPT ship "$SPRINT_SHIP_OVR_MISS" --commit "$DRIFT_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_SHIP_OVR_MISS" --deployed-commit "$DRIFT_COMMIT" --verdict FAIL --notes "GT caught what QA1 never actually saw" > /dev/null
git commit -q --allow-empty -m "fix for sprint $SPRINT_SHIP_OVR_MISS"
SHIP_OVR_MISS_FIX_COMMIT=$(git rev-parse HEAD)
$SCRIPT qa1 "$SPRINT_SHIP_OVR_MISS" --verdict PASS --notes "live-loop audit" --commit "$SHIP_OVR_MISS_FIX_COMMIT" > /dev/null
$SCRIPT reship "$SPRINT_SHIP_OVR_MISS" --commit "$SHIP_OVR_MISS_FIX_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_SHIP_OVR_MISS" --deployed-commit "$SHIP_OVR_MISS_FIX_COMMIT" --verdict PASS --notes ok > /dev/null
$SCRIPT complete "$SPRINT_SHIP_OVR_MISS" --user-said "close it" > /dev/null
rm -f /tmp/out.txt

echo "== gates: final aggregate across every completed sprint, still strictly read-only =="
FINAL_HASH_BEFORE=$(sprints_hash)
FINAL_GATES_OUT=$($SCRIPT gates)
FINAL_HASH_AFTER=$(sprints_hash)
[ "$FINAL_HASH_BEFORE" = "$FINAL_HASH_AFTER" ] || fail "gates modified docs/sprints/ on the multi-sprint aggregate (should be strictly read-only)"

echo "$FINAL_GATES_OUT" | grep -q "Gates aggregate over 4 completed sprints" || fail "gates should count exactly 4 completed sprints (sprints in other phases, aborted, or mid-loop must be excluded)"
echo "$FINAL_GATES_OUT" | grep -q "Audited miss.*sprints: ${SPRINT_1}, ${SPRINT_UNAUDITED}\$" || fail "gates' audited-miss bucket should list only sprint $SPRINT_1 and sprint $SPRINT_UNAUDITED's first GT fail — a ship-hash-overridden ship must NOT count as audited"
echo "$FINAL_GATES_OUT" | grep -q "Unaudited-fix miss.*sprints: ${SPRINT_UNAUDITED}\$" || fail "gates' unaudited-fix-miss bucket should list only sprint $SPRINT_UNAUDITED, never sprint $SPRINT_1 or sprint $SPRINT_SHIP_OVR_MISS"
echo "$FINAL_GATES_OUT" | grep -q "UNCLASSIFIED" || fail "gates should flag the ship-hash-overridden sprint's GT fail as unclassified, not silently fold it into audited miss"
echo "$FINAL_GATES_OUT" | grep -q "sprint ${SPRINT_SHIP_OVR_MISS}: .*ship-hash.*was human-overridden" || fail "gates' unclassified note for sprint $SPRINT_SHIP_OVR_MISS should explain why (ship-hash override), not just flag it"
echo "$FINAL_GATES_OUT" | grep -q "dev-done-hash overrides: 1 - sprints: ${SPRINT_GATES_OVR}" || fail "gates should count sprint $SPRINT_GATES_OVR's dev-done-hash override under hash-drift frequency"
echo "$FINAL_GATES_OUT" | grep -q "ship-hash overrides: 1 - sprints: ${SPRINT_SHIP_OVR_MISS}" || fail "gates should count sprint $SPRINT_SHIP_OVR_MISS's ship-hash override under hash-drift frequency"
echo "$FINAL_GATES_OUT" | grep -qE "sprint ${SPRINT_GATES_OVR}: audit_rounds=1, live_test_rounds=1" || fail "gates' round-count distribution for sprint $SPRINT_GATES_OVR is wrong"

echo "== gates: an unparseable verdict format is flagged and excluded, never silently counted as a catch =="
SPRINT_CORRUPT_VERDICT=$(new_sprint "Corrupt verdict sprint")
$SCRIPT start "$SPRINT_CORRUPT_VERDICT" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_CORRUPT_VERDICT work"
$SCRIPT qa1 "$SPRINT_CORRUPT_VERDICT" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_CORRUPT_VERDICT" > /dev/null
CORRUPT_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_CORRUPT_VERDICT" --commit "$CORRUPT_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_CORRUPT_VERDICT" --deployed-commit "$CORRUPT_COMMIT" --verdict PASS --notes ok > /dev/null
$SCRIPT complete "$SPRINT_CORRUPT_VERDICT" --user-said "close it" > /dev/null

# Simulate hand-corrupted state (or a future format change) rather than
# anything sprint_lifecycle.py itself would ever write.
CORRUPT_STATE="docs/sprints/state/sprint-${SPRINT_CORRUPT_VERDICT}.json"
python3 -c "
import json
p = '$CORRUPT_STATE'
s = json.load(open(p))
for h in s['history']:
    if h['event'] == 'audit':
        h['detail'] = 'garbled text with no leading verdict token'
json.dump(s, open(p, 'w'), indent=2)
"

CORRUPT_GATES_OUT=$($SCRIPT gates 2>&1)
echo "$CORRUPT_GATES_OUT" | grep -q "Traceback" && fail "gates crashed on an unparseable verdict format"
echo "$CORRUPT_GATES_OUT" | grep -q "WARNING: sprint ${SPRINT_CORRUPT_VERDICT} has a 'audit' event with an unrecognized verdict format" \
  || fail "gates should warn about the unparseable verdict instead of silently guessing"
echo "$CORRUPT_GATES_OUT" | grep "^   QA1:" > /tmp/qa1_catch_line.txt
FOUND=$(python3 -c "
import re
line = open('/tmp/qa1_catch_line.txt').read()
print('MATCH' if re.search(r'\b${SPRINT_CORRUPT_VERDICT}\b', line) else 'NOMATCH')
")
[ "$FOUND" = "NOMATCH" ] || fail "gates should not count sprint ${SPRINT_CORRUPT_VERDICT} under QA1's catch rate from an unparseable verdict alone"
rm -f /tmp/qa1_catch_line.txt

echo "== liveqa refuses a --deployed-commit that doesn't match what Pipeman actually shipped =="
SPRINT_GT_CHECK=$(new_sprint "Deployed commit check sprint")
$SCRIPT start "$SPRINT_GT_CHECK" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_GT_CHECK work"
$SCRIPT qa1 "$SPRINT_GT_CHECK" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_GT_CHECK" > /dev/null
GT_SHIPPED_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_GT_CHECK" --commit "$GT_SHIPPED_COMMIT" > /dev/null

# Sprint 24, Req 1: this MUST be a real content change, not
# --allow-empty. An empty commit has byte-identical tree content to its
# parent, and under Req 1's new content-based comparison that is now
# correctly ACCEPTED as a bookkeeping-only difference (see the dedicated
# test for exactly that below) — so an empty commit no longer exercises
# "genuinely different deployment," it would exercise the opposite case
# and this test would start passing for the wrong reason (or rather,
# stop refusing at all, which a naive `|| true` here would silently
# paper over). A real product-code line is what still has to refuse,
# unconditionally, per this Req's own FAIL-level acceptance criterion.
echo "unaudited product change, never shipped for this sprint" > "sprint24-unshipped-product-change.txt"
git add "sprint24-unshipped-product-change.txt"
git commit -q -m "an unrelated later commit, never shipped for this sprint"
UNSHIPPED_COMMIT=$(git rev-parse HEAD)
$SCRIPT liveqa "$SPRINT_GT_CHECK" --deployed-commit "$UNSHIPPED_COMMIT" --verdict PASS --notes "tested the wrong thing" \
  > /tmp/out.txt 2>&1 && fail "liveqa accepted a --deployed-commit that was never shipped for this sprint" || true
grep -q "doesn't match what Pipeman actually shipped" /tmp/out.txt || fail "deployed-commit mismatch refusal message missing"
grep -q "$GT_SHIPPED_COMMIT" /tmp/out.txt || fail "mismatch refusal should name the commit that was actually shipped"
grep -q "$UNSHIPPED_COMMIT" /tmp/out.txt || fail "mismatch refusal should name the commit that was actually tested"
# Sprint 24, Req 1: the message must name which paths actually differ,
# not just that the hashes do.
grep -q "sprint24-unshipped-product-change.txt" /tmp/out.txt || fail "mismatch refusal should name the differing path (Req 1)"
rm -f /tmp/out.txt

echo "== liveqa refuses a --deployed-commit that doesn't resolve to a real commit =="
$SCRIPT liveqa "$SPRINT_GT_CHECK" --deployed-commit not-a-real-commit --verdict PASS --notes ok \
  > /tmp/out.txt 2>&1 && fail "liveqa accepted a --deployed-commit that doesn't resolve" || true
grep -q "does not resolve to a real commit" /tmp/out.txt || fail "unresolvable deployed-commit refusal message missing"
rm -f /tmp/out.txt

echo "== sprint 24, Req 1: liveqa ACCEPTS a --deployed-commit that differs from last_shipped_commit only by bookkeeping content =="
SPRINT_LIVEQA_BOOKKEEPING=$(new_sprint "LiveQA bookkeeping-tolerance sprint")
$SCRIPT start "$SPRINT_LIVEQA_BOOKKEEPING" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_LIVEQA_BOOKKEEPING work"
$SCRIPT qa1 "$SPRINT_LIVEQA_BOOKKEEPING" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_LIVEQA_BOOKKEEPING" > /dev/null
LIVEQA_BOOKKEEPING_SHIPPED=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_LIVEQA_BOOKKEEPING" --commit "$LIVEQA_BOOKKEEPING_SHIPPED" > /dev/null
# Same technique as sprint 13's own ship-side bookkeeping-tolerance test
# above (SPRINT_BOOKKEEPING): a real, unrelated sprint gets registered —
# exactly the shape of what this lifecycle itself writes as bookkeeping,
# landing on top before deployment, reproducing Context's Finding A
# scenario (bookkeeping commit on top of the real shipped commit) rather
# than a synthetic stand-in for it.
new_sprint "Another unrelated bookkeeping sprint" > /dev/null
git add docs/sprints
git commit -q -m "bookkeeping: registered another sprint, landed on top of the deploy"
LIVEQA_BOOKKEEPING_DEPLOYED=$(git rev-parse HEAD)
[ "$LIVEQA_BOOKKEEPING_SHIPPED" != "$LIVEQA_BOOKKEEPING_DEPLOYED" ] || \
  fail "test setup broken: the bookkeeping commit didn't actually create a new SHA"
$SCRIPT liveqa "$SPRINT_LIVEQA_BOOKKEEPING" --deployed-commit "$LIVEQA_BOOKKEEPING_DEPLOYED" --verdict PASS --notes "content matched" \
  > /tmp/out.txt 2>&1 || fail "liveqa refused a --deployed-commit that differs from last_shipped_commit only by bookkeeping content (Req 1 regression) -- output: $(cat /tmp/out.txt)"
grep -q "bookkeeping only" /tmp/out.txt || fail "liveqa's bookkeeping-only acceptance message is missing"
grep -qF "$LIVEQA_BOOKKEEPING_DEPLOYED" /tmp/out.txt || fail "liveqa's bookkeeping-only acceptance doesn't name the deployed commit"
$SCRIPT status "$SPRINT_LIVEQA_BOOKKEEPING" 2>&1 | grep -q "LiveQA live result: PASS" || \
  fail "the bookkeeping-tolerant liveqa verdict wasn't actually recorded"
rm -f /tmp/out.txt

echo "== liveqa succeeds once --deployed-commit actually matches what was shipped =="
$SCRIPT liveqa "$SPRINT_GT_CHECK" --deployed-commit "$GT_SHIPPED_COMMIT" --verdict FAIL --notes "real bug found" > /dev/null || \
  fail "liveqa refused a --deployed-commit that genuinely matched the shipped commit"

echo "== status: no stale-test line right after a fresh verdict against the current ship =="
$SCRIPT status "$SPRINT_GT_CHECK" 2>/dev/null | grep -q "not yet re-tested" && \
  fail "status showed the stale-test line when the recorded verdict is current"

echo "== status: stale-test line appears once a reship lands after the last recorded verdict =="
git commit -q --allow-empty -m "fix for sprint $SPRINT_GT_CHECK"
GT_FIX_COMMIT=$(git rev-parse HEAD)
$SCRIPT qa1 "$SPRINT_GT_CHECK" --verdict PASS --notes "live-loop audit" --commit "$GT_FIX_COMMIT" > /dev/null
$SCRIPT reship "$SPRINT_GT_CHECK" --commit "$GT_FIX_COMMIT" > /dev/null
$SCRIPT status "$SPRINT_GT_CHECK" 2>/dev/null | grep -q "Code has changed since the last recorded LiveQA verdict - not yet re-tested." || \
  fail "status did not show the stale-test line after a reship with no fresh verdict yet"

echo "== status: stale-test line clears once a fresh verdict is recorded against the reshipped commit =="
$SCRIPT liveqa "$SPRINT_GT_CHECK" --deployed-commit "$GT_FIX_COMMIT" --verdict PASS --notes ok > /dev/null
$SCRIPT status "$SPRINT_GT_CHECK" 2>/dev/null | grep -q "not yet re-tested" && \
  fail "status still showed the stale-test line after a fresh verdict against the current ship"

echo "== liveqa refuses distinctly when no ship has ever been recorded for this sprint =="
SPRINT_GT_NOSHIP=$(new_sprint "No ship recorded sprint")
$SCRIPT start "$SPRINT_GT_NOSHIP" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_GT_NOSHIP work"
$SCRIPT qa1 "$SPRINT_GT_NOSHIP" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_GT_NOSHIP" > /dev/null
NOSHIP_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_GT_NOSHIP" --commit "$NOSHIP_COMMIT" > /dev/null
# Simulate a pre-upgrade sprint (or a hand-edited state file) with no
# last_shipped_commit on record, same technique as the existing
# SPRINT_LEGACY scenario above for the other hash fields.
NOSHIP_STATE="docs/sprints/state/sprint-${SPRINT_GT_NOSHIP}.json"
python3 -c "
import json
p = '$NOSHIP_STATE'
s = json.load(open(p))
s['last_shipped_commit'] = None
json.dump(s, open(p, 'w'), indent=2)
"
$SCRIPT liveqa "$SPRINT_GT_NOSHIP" --deployed-commit "$NOSHIP_COMMIT" --verdict PASS --notes ok \
  > /tmp/out.txt 2>&1 && fail "liveqa succeeded with no last_shipped_commit on record" || true
grep -q "has no shipped commit on record" /tmp/out.txt || fail "no-ship-recorded refusal message missing"
grep -q "doesn't match what Pipeman actually shipped" /tmp/out.txt && \
  fail "no-ship-recorded refusal must be a distinct message from the mismatch refusal, not reuse it"
rm -f /tmp/out.txt

echo "== backward compat: the deprecated 'groundtruth' subcommand and the legacy 'groundtruth_live' phase string still work, one transition period after the LiveQA rename =="
SPRINT_LEGACY_NAME=$(new_sprint "Legacy GroundTruth name sprint")
$SCRIPT start "$SPRINT_LEGACY_NAME" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_LEGACY_NAME work"
$SCRIPT qa1 "$SPRINT_LEGACY_NAME" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_LEGACY_NAME" > /dev/null
LEGACY_NAME_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_LEGACY_NAME" --commit "$LEGACY_NAME_COMMIT" > /dev/null

# Simulate an in-flight sprint that reached this phase before the rename,
# under the old phase string, rather than anything this version of the
# script would write going forward (cmd_ship always writes LIVEQA_PHASE now).
LEGACY_NAME_STATE="docs/sprints/state/sprint-${SPRINT_LEGACY_NAME}.json"
python3 -c "
import json
p = '$LEGACY_NAME_STATE'
s = json.load(open(p))
s['phase'] = 'groundtruth_live'
json.dump(s, open(p, 'w'), indent=2)
"
$SCRIPT status "$SPRINT_LEGACY_NAME" 2>/dev/null | grep -q "Phase: groundtruth_live" || \
  fail "test setup broken: legacy phase string wasn't actually written"

$SCRIPT groundtruth "$SPRINT_LEGACY_NAME" --deployed-commit "$LEGACY_NAME_COMMIT" --verdict PASS --notes ok \
  > /tmp/out.txt 2>&1 || fail "the deprecated 'groundtruth' subcommand no longer works against a sprint on the legacy 'groundtruth_live' phase"
grep -q "deprecated alias for 'liveqa'" /tmp/out.txt || fail "the deprecated 'groundtruth' subcommand should note it's a deprecated alias"
grep -q "LiveQA live test PASSED" /tmp/out.txt || fail "the deprecated 'groundtruth' subcommand didn't actually record the verdict"
$SCRIPT status "$SPRINT_LEGACY_NAME" 2>/dev/null | grep -q "Phase: complete_ready" || \
  fail "sprint stuck on the legacy phase string never reached complete_ready via the deprecated subcommand"
rm -f /tmp/out.txt

echo "== backward compat: the NEW 'liveqa' subcommand also works against a sprint still on the legacy 'groundtruth_live' phase =="
SPRINT_NEW_NAME_OLD_PHASE=$(new_sprint "New name legacy phase sprint")
$SCRIPT start "$SPRINT_NEW_NAME_OLD_PHASE" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_NEW_NAME_OLD_PHASE work"
$SCRIPT qa1 "$SPRINT_NEW_NAME_OLD_PHASE" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_NEW_NAME_OLD_PHASE" > /dev/null
NEW_NAME_OLD_PHASE_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_NEW_NAME_OLD_PHASE" --commit "$NEW_NAME_OLD_PHASE_COMMIT" > /dev/null

# Same legacy-phase simulation as the scenario above, but this time paired
# with the NEW subcommand name, closing the other meaningful cell of the
# {old,new name} x {old,new phase} matrix (the two mechanisms are
# independent by construction, but that's a design claim until it's
# actually exercised).
NEW_NAME_OLD_PHASE_STATE="docs/sprints/state/sprint-${SPRINT_NEW_NAME_OLD_PHASE}.json"
python3 -c "
import json
p = '$NEW_NAME_OLD_PHASE_STATE'
s = json.load(open(p))
s['phase'] = 'groundtruth_live'
json.dump(s, open(p, 'w'), indent=2)
"
$SCRIPT status "$SPRINT_NEW_NAME_OLD_PHASE" 2>/dev/null | grep -q "Phase: groundtruth_live" || \
  fail "test setup broken: legacy phase string wasn't actually written"

$SCRIPT liveqa "$SPRINT_NEW_NAME_OLD_PHASE" --deployed-commit "$NEW_NAME_OLD_PHASE_COMMIT" --verdict PASS --notes ok \
  > /tmp/out.txt 2>&1 || fail "the new 'liveqa' subcommand doesn't recognize a sprint still on the legacy 'groundtruth_live' phase"
grep -q "deprecated alias" /tmp/out.txt && fail "the canonical 'liveqa' subcommand should never print the deprecation note"
grep -q "LiveQA live test PASSED" /tmp/out.txt || fail "the new 'liveqa' subcommand didn't actually record the verdict"
$SCRIPT status "$SPRINT_NEW_NAME_OLD_PHASE" 2>/dev/null | grep -q "Phase: complete_ready" || \
  fail "sprint stuck on the legacy phase string never reached complete_ready via the new subcommand"
rm -f /tmp/out.txt

# ---------------------------------------------------------------------------
# Sprint 7: the live-loop audit (Req 1/2/3/9) and the git-repository-cause
# distinction (Req 12). All added at the end of the file, deliberately —
# every earlier "gates" check above counts completed sprints by a hardcoded
# number, and every sprint this section completes happens after the last
# of those checks, so it can't perturb them.
# ---------------------------------------------------------------------------
echo "== live-loop audit: records without touching any gate-read field, verified by a full state diff (Req 1) =="
SPRINT_LL=$(new_sprint "Live loop audit sprint")
$SCRIPT start "$SPRINT_LL" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_LL work"
$SCRIPT qa1 "$SPRINT_LL" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_LL" > /dev/null
LL_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_LL" --commit "$LL_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_LL" --deployed-commit "$LL_COMMIT" --verdict FAIL --notes "needs a live-loop audit" > /dev/null

# Now genuinely sitting at liveqa_live with a gate-1 PASS and both hashes
# populated — QA1's own verification method for Req 1: seed exactly this
# shape, diff the WHOLE state file before and after, don't just read the
# code (reading the diff is explicitly not sufficient per the sprint file).
LL_STATE="docs/sprints/state/sprint-${SPRINT_LL}.json"
cp "$LL_STATE" /tmp/ll_before.json

LL_OUT=$($SCRIPT qa1 "$SPRINT_LL" --verdict PASS --notes "live-loop audit" --commit "$LL_COMMIT" 2>&1)
echo "$LL_OUT" | grep -q "RECORD, not a" || fail "live-loop audit output should say plainly that this is a record, not a gate verdict"
echo "$LL_OUT" | grep -q "does not change the sprint's phase" || fail "live-loop audit output should say plainly it doesn't move the sprint"
# Sprint 36, Req 3/4 (QA1 round 1 finding): a PASS with --commit is now
# load-bearing for /sprint-reship's own gate -- the message must say so,
# not claim (as it used to) that this record has no gating effect at all.
echo "$LL_OUT" | grep -q "exactly what /sprint-reship's own gate checks for" || \
  fail "live-loop audit output (PASS, with --commit) should say this is exactly what reship's gate checks for -- got: $LL_OUT"
echo "$LL_OUT" | grep -q "LiveQA's live-test retest remains what actually gates this code" && \
  fail "live-loop audit output (PASS, with --commit) must not still claim LiveQA's retest is the only thing gating this code -- sprint 36 made this record load-bearing for reship too"

python3 -c "
import json
before = json.load(open('/tmp/ll_before.json'))
after = json.load(open('$LL_STATE'))
protected = ['phase', 'qa1_audit_result', 'audit_rounds', 'qa1_audit_file_hash', 'qa1_audited_tree_hash']
for field in protected:
    if before[field] != after[field]:
        raise SystemExit(f'protected field {field} changed: {before[field]!r} -> {after[field]!r}')
# Sprint 25, Req 2: last_claim now legitimately changes on EVERY
# save_state() call, stamped uniformly regardless of which command wrote
# -- excluded here the same way history already is, for the identical
# reason: it's expected to change, on purpose, every time. Not asserted
# that ts itself differs here: now() has one-second resolution and two
# calls this close together can legitimately land in the same second --
# that the mechanism actually stamps a fresh value is proven separately,
# with an explicit before/after env override, in this file's own
# dedicated sprint-25 Req 2 tests below.
# Sprint 36, Req 3: live_loop_audit_trees legitimately changes too, on
# purpose, exactly like history -- this call passes --commit, so a new
# entry is appended (see _qa1_live_loop_audit's own docstring). Excluded
# from the blanket equality check the same way, with its own explicit
# assertion below instead.
ignored_fields = ('history', 'last_claim', 'live_loop_audit_trees')
before_no_history = {k: v for k, v in before.items() if k not in ignored_fields}
after_no_history = {k: v for k, v in after.items() if k not in ignored_fields}
if before_no_history != after_no_history:
    raise SystemExit(f'a field outside the five named ones changed too: before={before_no_history} after={after_no_history}')
if len(after['history']) != len(before['history']) + 1:
    raise SystemExit(f'expected exactly one new history event, before={len(before[\"history\"])} after={len(after[\"history\"])}')
new_event = after['history'][-1]
if new_event['event'] == 'audit':
    raise SystemExit('live-loop audit must use a name distinct from gate 1\'s \"audit\", or cmd_gates would count it')
if '$LL_COMMIT' not in new_event['detail']:
    raise SystemExit('the resolved --commit should appear in the live-loop audit event detail')
before_trees = before.get('live_loop_audit_trees', [])
after_trees = after.get('live_loop_audit_trees', [])
if len(after_trees) != len(before_trees) + 1:
    raise SystemExit(f'expected exactly one new live_loop_audit_trees entry: before={before_trees} after={after_trees}')
new_tree_entry = after_trees[-1]
if new_tree_entry['commit'] != '$LL_COMMIT':
    raise SystemExit(f'live_loop_audit_trees entry recorded the wrong commit: {new_tree_entry}')
if new_tree_entry['verdict'] != 'PASS':
    raise SystemExit(f'live_loop_audit_trees entry recorded the wrong verdict: {new_tree_entry}')
if not new_tree_entry['tree_hash']:
    raise SystemExit(f'live_loop_audit_trees entry has no tree_hash: {new_tree_entry}')
" || fail "live-loop audit state diff check failed — see message above"
rm -f /tmp/ll_before.json

echo "== live-loop audit: all three verdicts record; none of them change phase or move the sprint (Req 3) =="
for V in PASS CONDITIONAL FAIL; do
  BEFORE_PHASE=$(python3 -c "import json; print(json.load(open('$LL_STATE'))['phase'])")
  $SCRIPT qa1 "$SPRINT_LL" --verdict "$V" --notes "live-loop $V" > /tmp/out.txt 2>&1 || fail "live-loop qa1 with verdict $V should succeed"
  AFTER_PHASE=$(python3 -c "import json; print(json.load(open('$LL_STATE'))['phase'])")
  [ "$BEFORE_PHASE" = "$AFTER_PHASE" ] || fail "live-loop $V verdict changed phase from $BEFORE_PHASE to $AFTER_PHASE"
  [ "$AFTER_PHASE" = "liveqa_live" ] || fail "sprint $SPRINT_LL should still be in liveqa_live after a live-loop $V"
done
rm -f /tmp/out.txt

echo "== live-loop audit: an unresolvable --commit is refused, and nothing is written on refusal (Req 2) =="
cp "$LL_STATE" /tmp/ll_before2.json
$SCRIPT qa1 "$SPRINT_LL" --verdict PASS --notes "bad commit" --commit not-a-real-commit > /tmp/out.txt 2>&1 && fail "live-loop audit accepted an unresolvable --commit" || true
grep -q "does not resolve to a real commit" /tmp/out.txt || fail "live-loop audit's bad-commit refusal message missing"
diff -q /tmp/ll_before2.json "$LL_STATE" > /dev/null || fail "a refused live-loop --commit must not write anything to the state file"
rm -f /tmp/out.txt /tmp/ll_before2.json

echo "== live-loop audit: omitting --commit keeps working exactly as before (Req 2, additive-only argument) =="
$SCRIPT qa1 "$SPRINT_LL" --verdict PASS --notes "no commit given" > /tmp/out.txt 2>&1 || fail "live-loop audit without --commit should still succeed"
grep -q "RECORD, not a" /tmp/out.txt || fail "live-loop audit without --commit should still print the record-not-a-gate message"
rm -f /tmp/out.txt

echo "== qa1: a sprint in a phase neither set applies to still refuses, naming both valid phase sets (Req 4) =="
$SCRIPT qa1 "$SPRINT_1" --verdict PASS --notes "trying to audit an already-closed sprint" > /tmp/out.txt 2>&1 && \
  fail "qa1 succeeded against sprint $SPRINT_1, already in complete phase" || true
grep -q "dev_build" /tmp/out.txt || fail "qa1's refusal for an inapplicable phase should still name the gate-1 phase set"
grep -qi "live" /tmp/out.txt || fail "qa1's refusal for an inapplicable phase should also mention the live-loop phase set, now that two paths exist"
rm -f /tmp/out.txt

echo "== gates: a live-loop audit is never counted as a gate catch, even on a completed sprint (Req 9) =="
GATES_BEFORE_LL=$($SCRIPT gates)
git commit -q --allow-empty -m "fix for sprint $SPRINT_LL after the live loop"
LL_FIX_COMMIT=$(git rev-parse HEAD)
# Sprint 36, Req 3: reship needs a QA1 verdict on record for this exact
# tree -- a fresh live-loop PASS on this specific commit, not relying on
# any of the live-loop verdicts already recorded above for LL_COMMIT.
$SCRIPT qa1 "$SPRINT_LL" --verdict PASS --notes "live-loop audit of the after-live-loop fix" --commit "$LL_FIX_COMMIT" > /dev/null
$SCRIPT reship "$SPRINT_LL" --commit "$LL_FIX_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_LL" --deployed-commit "$LL_FIX_COMMIT" --verdict PASS --notes ok > /dev/null

echo "== sprint 15, Req 1: the live-loop audit branch now also accepts complete_ready -- QA1's own method: seed exactly this shape, diff the WHOLE state file, only history may change =="
[ "$(python3 -c "import json; print(json.load(open('$LL_STATE'))['phase'])")" = "complete_ready" ] || \
  fail "test setup broken: sprint $SPRINT_LL should be at complete_ready after its PASS above, before /sprint-complete runs"
cp "$LL_STATE" /tmp/ll_ready_before.json

READY_OUT=$($SCRIPT qa1 "$SPRINT_LL" --verdict PASS --notes "live-loop audit at complete_ready" 2>&1)
echo "$READY_OUT" | grep -q "RECORD, not a" || fail "complete_ready live-loop audit output should say plainly it's a record, not a gate verdict"
echo "$READY_OUT" | grep -q "Both gates already passed" || fail "complete_ready live-loop audit output should say both gates already passed, not point at another LiveQA retest that isn't coming"

python3 -c "
import json
before = json.load(open('/tmp/ll_ready_before.json'))
after = json.load(open('$LL_STATE'))
protected = ['phase', 'qa1_audit_result', 'audit_rounds', 'qa1_audit_file_hash', 'qa1_audited_tree_hash']
for field in protected:
    if before[field] != after[field]:
        raise SystemExit(f'protected field {field} changed: {before[field]!r} -> {after[field]!r}')
# Sprint 25, Req 2: same exclusion as the first live-loop diff check above
# -- last_claim legitimately changes on every save_state() call now (not
# asserted here that ts itself differs -- now() has one-second resolution
# and this file's own dedicated Req 2 tests prove the stamping directly).
ignored_fields = ('history', 'last_claim')
before_no_history = {k: v for k, v in before.items() if k not in ignored_fields}
after_no_history = {k: v for k, v in after.items() if k not in ignored_fields}
if before_no_history != after_no_history:
    raise SystemExit(f'a field outside the five named ones changed too: before={before_no_history} after={after_no_history}')
if len(after['history']) != len(before['history']) + 1:
    raise SystemExit(f'expected exactly one new history event, before={len(before[\"history\"])} after={len(after[\"history\"])}')
if after['history'][-1]['event'] == 'audit':
    raise SystemExit('a complete_ready live-loop audit must use a name distinct from gate 1\'s \"audit\", or cmd_gates would count it')
" || fail "complete_ready live-loop audit state diff check failed -- see message above"
rm -f /tmp/ll_ready_before.json

$SCRIPT complete "$SPRINT_LL" --user-said "close it, the live-loop audits above are just records" > /dev/null

echo "== sprint 15, Req 1: complete is NOT included -- a live-loop audit against an already-closed sprint is refused, and nothing is written =="
cp "$LL_STATE" /tmp/ll_closed_before.json
$SCRIPT qa1 "$SPRINT_LL" --verdict PASS --notes "trying to audit a closed sprint" > /tmp/out.txt 2>&1 && \
  fail "qa1 succeeded recording a live-loop audit against sprint $SPRINT_LL, already complete" || true
diff -q /tmp/ll_closed_before.json "$LL_STATE" > /dev/null || fail "a refused live-loop audit against a closed sprint must not write anything"
rm -f /tmp/out.txt /tmp/ll_closed_before.json
GATES_AFTER_LL=$($SCRIPT gates)
echo "$GATES_AFTER_LL" | grep "^   QA1:" > /tmp/qa1_line.txt
grep -qE "\b${SPRINT_LL}\b" /tmp/qa1_line.txt && \
  fail "gates counted sprint $SPRINT_LL under QA1's catch rate — its only real gate-1 'audit' event was a single PASS; the live-loop PASS/CONDITIONAL/FAIL entries above must not count"
rm -f /tmp/qa1_line.txt

echo "== Req 12: a PASS with no git repository present says so, and ship blames the missing repo, not a missing QA1 pass =="
NOGIT_SANDBOX="$(mktemp -d "${TMPDIR:-/tmp}/fully-completely-smoke-nogit.XXXXXX")"
mkdir -p "$NOGIT_SANDBOX/scripts" "$NOGIT_SANDBOX/templates"
cp "$REPO_ROOT/scripts/sprint_lifecycle.py" "$NOGIT_SANDBOX/scripts/sprint_lifecycle.py"
if [ -f "$REPO_ROOT/templates/sprint-template.md" ]; then
  cp "$REPO_ROOT/templates/sprint-template.md" "$NOGIT_SANDBOX/templates/sprint-template.md"
fi
NOGIT_SCRIPT="python3 $NOGIT_SANDBOX/scripts/sprint_lifecycle.py"
# Deliberately no `git init` here — this directory is not a git repository
# at all, the exact scenario Req 12 exists for. ROOT resolves from where
# the script FILE lives (Path(__file__).resolve().parent.parent), not cwd,
# so running it from anywhere still points ROOT at $NOGIT_SANDBOX.

NOGIT_ID=$($NOGIT_SCRIPT new "No repo sprint" | grep -oE 'Created sprint [0-9]+' | grep -oE '[0-9]+')
$NOGIT_SCRIPT start "$NOGIT_ID" > /dev/null

NOGIT_QA1_OUT=$($NOGIT_SCRIPT qa1 "$NOGIT_ID" --verdict PASS --notes ok 2>&1)
echo "$NOGIT_QA1_OUT" | grep -q "QA1 audit PASSED" || fail "PASS itself should still succeed with no git repository present"
echo "$NOGIT_QA1_OUT" | grep -q "not a git repository" || fail "PASS with no repository present should say so plainly, not record None in silence"
$NOGIT_SCRIPT dev-done "$NOGIT_ID" > /dev/null || fail "dev-done should still succeed with no git repository present (it doesn't need one)"

$NOGIT_SCRIPT ship "$NOGIT_ID" --commit deadbeef > /tmp/out.txt 2>&1 && fail "ship succeeded with no git repository present" || true
grep -q "not a git repository" /tmp/out.txt || fail "ship's no-repo message should name the missing repository"
grep -q "no QA1-audited commit on record" /tmp/out.txt && fail "ship should blame the missing repository, not a missing QA1 pass — QA1 DID pass"
rm -f /tmp/out.txt

echo "== Req 12: reship and liveqa give the same no-repository message, not the generic 'doesn't resolve' one =="
NOGIT_ID2=$($NOGIT_SCRIPT new "No repo reship sprint" | grep -oE 'Created sprint [0-9]+' | grep -oE '[0-9]+')
$NOGIT_SCRIPT start "$NOGIT_ID2" > /dev/null
NOGIT_STATE2="$NOGIT_SANDBOX/docs/sprints/state/sprint-${NOGIT_ID2}.json"
python3 -c "
import json
p = '$NOGIT_STATE2'
s = json.load(open(p))
s['phase'] = 'liveqa_live'
json.dump(s, open(p, 'w'), indent=2)
"
$NOGIT_SCRIPT reship "$NOGIT_ID2" --commit deadbeef > /tmp/out.txt 2>&1 && fail "reship succeeded with no git repository present" || true
grep -q "not a git repository" /tmp/out.txt || fail "reship's no-repo message should name the missing repository"
rm -f /tmp/out.txt

$NOGIT_SCRIPT liveqa "$NOGIT_ID2" --deployed-commit deadbeef --verdict PASS --notes ok > /tmp/out.txt 2>&1 && fail "liveqa succeeded with no git repository present" || true
grep -q "not a git repository" /tmp/out.txt || fail "liveqa's no-repo message should name the missing repository"
rm -f /tmp/out.txt
rm -rf "$NOGIT_SANDBOX"

echo "== Req 12: behaviour inside a real git repository is unchanged — the existing 'no QA1-audited commit' message still fires there =="
SPRINT_REALREPO_NOQA1=$(new_sprint "Real repo no qa1 sprint")
$SCRIPT start "$SPRINT_REALREPO_NOQA1" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_REALREPO_NOQA1 work"
# Force phase to dev_agreed_done without a QA1 PASS ever landing on record,
# same hand-edit technique as SPRINT_LEGACY above, to reach cmd_ship's "no
# audited tree hash" branch inside a REAL repo, never touching the
# no-repository path at all.
REALREPO_NOQA1_STATE="docs/sprints/state/sprint-${SPRINT_REALREPO_NOQA1}.json"
python3 -c "
import json
p = '$REALREPO_NOQA1_STATE'
s = json.load(open(p))
s['phase'] = 'dev_agreed_done'
json.dump(s, open(p, 'w'), indent=2)
"
REALREPO_NOQA1_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_REALREPO_NOQA1" --commit "$REALREPO_NOQA1_COMMIT" > /tmp/out.txt 2>&1 && \
  fail "ship succeeded with no QA1 audit ever recorded (test setup broken)" || true
grep -q "no QA1-audited commit on record" /tmp/out.txt || fail "ship's real-repo, no-QA1-pass message regressed"
grep -q "not a git repository" /tmp/out.txt && fail "ship should never claim no repository exists when it's running inside a real one"
rm -f /tmp/out.txt

echo "== sprint 13, Req 1 (Finding A): a bookkeeping-only change (docs/sprints/) after QA1's PASS does NOT invalidate the audit =="
SPRINT_BOOKKEEPING=$(new_sprint "Bookkeeping-only sprint")
$SCRIPT start "$SPRINT_BOOKKEEPING" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_BOOKKEEPING initial work"
$SCRIPT qa1 "$SPRINT_BOOKKEEPING" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_BOOKKEEPING" > /dev/null
# Real lifecycle bookkeeping lands after the PASS: an entirely unrelated
# sprint gets created (a registry.json update plus a new file, both
# entirely under docs/sprints/), then committed — exactly the shape of
# what the lifecycle itself writes, and exactly what sprints 6, 8 and 10
# each hit against this same sprint's real audit.
new_sprint "Unrelated bookkeeping sprint" > /dev/null
git add docs/sprints
git commit -q -m "bookkeeping: registered another sprint"
BOOKKEEPING_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_BOOKKEEPING" --commit "$BOOKKEEPING_COMMIT" > /tmp/out.txt 2>&1 || \
  fail "ship refused a commit whose only change was under docs/sprints/ (Req 1 regression) — output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt
# The commit just shipped also carries a genuine docs/sprints/.locks/*.lock
# file — every qa1/dev-done/ship call above acquires and releases one, and
# the lock FILE itself is never deleted (see locked()'s own docstring) —
# so this same assertion already covers the exclusion list's .locks/*
# pattern, not just registry.json/state/*.json/*/*.md. QA1's own first
# version of this fix passed this exact test while excluding a whole
# blanket "docs/sprints/" prefix; a real lock file created during THIS
# test run is what caught that the narrowed list initially missed
# docs/sprints/.locks/* too, before this comment or the fix existed.
git ls-tree -r "$BOOKKEEPING_COMMIT" --name-only | grep -q "^docs/sprints/\.locks/" || \
  fail "test setup broken: expected at least one docs/sprints/.locks/*.lock file in the shipped commit"

echo "== sprint 13, Req 1 (QA1 round 1's own finding): a shipped .gitkeep file is NOT in the exclusion list — an unaudited change to one still refuses =="
SPRINT_GITKEEP=$(new_sprint "Gitkeep regression sprint")
$SCRIPT start "$SPRINT_GITKEEP" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_GITKEEP initial work"
$SCRIPT qa1 "$SPRINT_GITKEEP" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_GITKEEP" > /dev/null
# A phase-folder .gitkeep genuinely ships (install.js's own skeleton,
# confirmed against the real published tarball by QA1) — it is NOT one of
# .npmignore's docs/sprints/-specific exclusions, unlike registry.json,
# state/*.json, and the phase folders' own sprint *.md files. An
# unaudited byte appended to one must still be caught.
mkdir -p docs/sprints/4-blocked
printf '\n' >> docs/sprints/4-blocked/.gitkeep
git add docs/sprints/4-blocked/.gitkeep
git commit -q -m "unaudited change to a SHIPPED .gitkeep file"
GITKEEP_DRIFT_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_GITKEEP" --commit "$GITKEEP_DRIFT_COMMIT" > /tmp/out.txt 2>&1 && \
  fail "ship succeeded on an unaudited change to a real, shipped .gitkeep file (Req 1's exclusion list is still too broad)" || true
grep -q "doesn't match what QA1 audited" /tmp/out.txt || fail "gitkeep-drift refusal message missing"
rm -f /tmp/out.txt

echo "== sprint 13, Req 1: a source change (outside docs/sprints/) after QA1's PASS still refuses, no override — the preserved protection =="
SPRINT_SOURCE_DRIFT=$(new_sprint "Source drift sprint (sprint 13)")
$SCRIPT start "$SPRINT_SOURCE_DRIFT" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_SOURCE_DRIFT initial work"
$SCRIPT qa1 "$SPRINT_SOURCE_DRIFT" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_SOURCE_DRIFT" > /dev/null
echo "unaudited source change" > "sprint13-sneaky-source.txt"
git add "sprint13-sneaky-source.txt"
git commit -q -m "unaudited SOURCE change after QA1 PASS"
SOURCE_DRIFT_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_SOURCE_DRIFT" --commit "$SOURCE_DRIFT_COMMIT" > /tmp/out.txt 2>&1 && \
  fail "ship succeeded on an unaudited SOURCE change — Req 1's relaxation leaked beyond docs/sprints/" || true
grep -q "doesn't match what QA1 audited" /tmp/out.txt || fail "sprint 13 source-drift refusal message missing"
rm -f /tmp/out.txt

echo "== sprint 13, Req 2: verify-publish is a real, wired-up command — refuses cleanly with no shipped commit on record =="
SPRINT_VERIFY_PUBLISH=$(new_sprint "Verify publish precondition sprint")
$SCRIPT start "$SPRINT_VERIFY_PUBLISH" > /dev/null
$SCRIPT verify-publish "$SPRINT_VERIFY_PUBLISH" > /tmp/out.txt 2>&1 && \
  fail "verify-publish succeeded with no shipped commit on record (test setup broken)" || true
grep -q "no shipped commit on record" /tmp/out.txt || fail "verify-publish's no-shipped-commit refusal message missing"
rm -f /tmp/out.txt

echo "== sprint 13, Req 3 (Finding C): status warns on a real cross-tree divergence, stays silent when trees agree, and never gates qa1 =="
SPRINT_WT=$(new_sprint "Worktree divergence sprint")
$SCRIPT start "$SPRINT_WT" > /dev/null
git add -A
git commit -q -m "commit sprint $SPRINT_WT's file so a second worktree can see it"

OTHER_WT="$(mktemp -d "${TMPDIR:-/tmp}/fully-completely-smoke-wt.XXXXXX")"
git worktree add -q -b smoke-wt-branch "$OTHER_WT" > /dev/null

# No divergence yet: the second worktree's copy is byte-identical.
$SCRIPT status "$SPRINT_WT" > /tmp/out.txt 2>&1
grep -q "WARNING" /tmp/out.txt && fail "status warned about worktree divergence when the files are actually identical"
rm -f /tmp/out.txt

# Amend the sprint file in the OTHER worktree only — a real cross-tree
# divergence, the exact scenario that had Dev Team building sprint 11
# against a spec amended on a different branch.
OTHER_WT_SPRINT_FILE=$(find "$OTHER_WT/docs/sprints/2-in-progress" -name "sprint-${SPRINT_WT}_*.md")
echo "### amended only in the other worktree" >> "$OTHER_WT_SPRINT_FILE"

$SCRIPT status "$SPRINT_WT" > /tmp/out.txt 2>&1 || fail "status failed once a second worktree existed with a diverged file"
grep -q "WARNING" /tmp/out.txt || fail "status did not warn about a real cross-tree divergence"
# Match on the mktemp basename, not the full $OTHER_WT path: the warning
# names the RESOLVED path (worktree_divergence_warning() calls .resolve()
# so /tmp vs /private/tmp-style symlink aliases compare equal), which can
# legitimately differ textually from $OTHER_WT's own unresolved form on
# macOS — the basename survives that resolution either way.
grep -q "$(basename "$OTHER_WT")" /tmp/out.txt || fail "divergence warning did not name the diverging worktree"
rm -f /tmp/out.txt

# It must warn, never gate: qa1 must still be able to record a verdict,
# and must surface the same warning rather than silently swallowing it.
git commit -q --allow-empty -m "sprint $SPRINT_WT work"
$SCRIPT qa1 "$SPRINT_WT" --verdict PASS --notes "worktree warning present but must not block" > /tmp/out.txt 2>&1 || \
  fail "qa1 was blocked by a worktree divergence warning — Req 3 says warn, never gate"
grep -q "WARNING" /tmp/out.txt || fail "qa1 did not surface the same divergence warning"
rm -f /tmp/out.txt

git worktree remove --force "$OTHER_WT" > /dev/null 2>&1 || rm -rf "$OTHER_WT"
git branch -D smoke-wt-branch > /dev/null 2>&1 || true

echo "== sprint 14, Req 1: a missing --*-file path fails legibly, naming the path, instead of an unhandled crash =="
# This is resolve_text()'s own bug, fixed once and shared by every
# --*-file argument in the CLI (qa1/liveqa notes, complete's user-said,
# abort's reason, override's reason) — the failure LiveQA actually found
# on a default Windows box, where the historical /tmp/... example path
# doesn't exist and the whole process used to die with a bare Python
# traceback and nothing recorded. Exercised here via --reason-file
# (abort) and --user-said-file (complete), the two call sites that don't
# need a prior PASS/phase to reach.
MISSING_FILE_PATH="does-not-exist-$(date +%s).txt"
[ ! -e "$MISSING_FILE_PATH" ] || fail "test setup broken: $MISSING_FILE_PATH unexpectedly exists"

SPRINT_MISSING_FILE=$(new_sprint "Missing file sprint")
$SCRIPT start "$SPRINT_MISSING_FILE" > /dev/null
# Sprint 33, Req 2: --user-said is now checked before --reason, so a
# valid one must be supplied here to reach the --reason-file read this
# case actually exercises.
$SCRIPT abort "$SPRINT_MISSING_FILE" --user-said "yes, abandon it" --reason-file "$MISSING_FILE_PATH" > /tmp/out.txt 2>&1 && \
  fail "abort succeeded reading a --reason-file that doesn't exist (test setup broken)" || true
grep -qF "Could not read '$MISSING_FILE_PATH'" /tmp/out.txt || fail "abort's missing-reason-file message doesn't name the path"
grep -qi "traceback" /tmp/out.txt && fail "abort's missing-reason-file case raised a raw traceback instead of failing legibly"
rm -f /tmp/out.txt

# complete's own three-condition refusal is checked elsewhere; this only
# needs to confirm resolve_text() itself fails legibly for user-said-file
# specifically, so use a sprint that will refuse for an EARLIER reason
# (no QA1 PASS yet) if the file-read somehow didn't fail first — the file
# read happens before those checks, so this still isolates the right bug.
$SCRIPT complete "$SPRINT_MISSING_FILE" --user-said-file "$MISSING_FILE_PATH" > /tmp/out.txt 2>&1 && \
  fail "complete succeeded reading a --user-said-file that doesn't exist (test setup broken)" || true
grep -qF "Could not read '$MISSING_FILE_PATH'" /tmp/out.txt || fail "complete's missing-user-said-file message doesn't name the path"
grep -qi "traceback" /tmp/out.txt && fail "complete's missing-user-said-file case raised a raw traceback instead of failing legibly"
rm -f /tmp/out.txt

echo "== sprint 14, Req 1: a missing --title-file on 'new' fails legibly too, naming the path (cmd_new's own former duplicate bug) =="
$SCRIPT new --title-file "$MISSING_FILE_PATH" > /tmp/out.txt 2>&1 && \
  fail "new succeeded reading a --title-file that doesn't exist (test setup broken)" || true
grep -qF "Could not read '$MISSING_FILE_PATH'" /tmp/out.txt || fail "new's missing-title-file message doesn't name the path"
grep -qi "traceback" /tmp/out.txt && fail "new's missing-title-file case raised a raw traceback instead of failing legibly"
rm -f /tmp/out.txt

echo "== sprint 24, Req 2: ship refuses over a red CI run for the exact commit being shipped =="
SPRINT_CI=$(new_sprint "CI status sprint")
$SCRIPT start "$SPRINT_CI" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_CI work"
$SCRIPT qa1 "$SPRINT_CI" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_CI" > /dev/null
CI_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=red $SCRIPT ship "$SPRINT_CI" --commit "$CI_COMMIT" \
  > /tmp/out.txt 2>&1 && fail "ship succeeded despite a red CI run for the exact commit being shipped" || true
grep -q "CI is red for the exact commit being shipped" /tmp/out.txt || fail "ship's CI-red refusal message is missing"
grep -qF "$CI_COMMIT" /tmp/out.txt || fail "ship's CI-red refusal doesn't name the commit"
$SCRIPT status "$SPRINT_CI" 2>&1 | grep -q "Phase: dev_agreed_done" || \
  fail "a CI-red ship attempt must not have moved the sprint's phase"
rm -f /tmp/out.txt

echo "== sprint 24, Req 2: ship refuses when CI 'succeeded' but no step actually executed -- not merely that a run existed or finished =="
# This is the specific case the reporter's own two-day-red build was: npm
# ci exiting EUSAGE in 5-7 seconds, lint/test/build never running. A run
# whose own top-level conclusion still reads "success" (this fake
# simulates a workflow that's misconfigured to report success trivially)
# but whose only completed, non-skipped step is "Set up job" must still
# refuse -- a check satisfied by "a run exists" or "a run completed"
# would pass this exact case, which is the defect this Req exists to fix.
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=no-steps $SCRIPT ship "$SPRINT_CI" --commit "$CI_COMMIT" \
  > /tmp/out.txt 2>&1 && fail "ship succeeded on a run that reported success but never executed a real step" || true
grep -q "no real step actually executed" /tmp/out.txt || fail "ship's no-real-steps-executed refusal message is missing"
rm -f /tmp/out.txt

echo "== sprint 24, Req 2: an undeterminable CI status (no runs found) warns but does NOT gate -- the explicit decision this Req requires =="
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=none $SCRIPT ship "$SPRINT_CI" --commit "$CI_COMMIT" \
  > /tmp/out.txt 2>&1 || fail "ship refused when CI status was genuinely undeterminable -- a project with no CI, or CI this tool can't see, must not become unshippable by accident (Req 2)"
grep -q "WARNING: could not determine CI status" /tmp/out.txt || fail "ship's undeterminable-CI-status warning is missing"
grep -q "no CI runs found" /tmp/out.txt || fail "ship's undeterminable warning doesn't explain why"
$SCRIPT status "$SPRINT_CI" 2>&1 | grep -qE "Phase: (liveqa_live|groundtruth_live)" || \
  fail "ship should have proceeded (undeterminable does not gate) and moved phase to the LiveQA phase"
rm -f /tmp/out.txt

echo "== sprint 24, Req 2: a genuinely green CI run (real steps executed) ships cleanly =="
SPRINT_CI_GREEN=$(new_sprint "CI green sprint")
$SCRIPT start "$SPRINT_CI_GREEN" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_CI_GREEN work"
$SCRIPT qa1 "$SPRINT_CI_GREEN" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_CI_GREEN" > /dev/null
CI_GREEN_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_CI_GREEN" --commit "$CI_GREEN_COMMIT" \
  > /tmp/out.txt 2>&1 || fail "ship refused despite a genuinely green CI run with real steps executed -- output: $(cat /tmp/out.txt)"
grep -q "CI check:.*completed successfully with real steps executed" /tmp/out.txt || \
  fail "ship's green-CI confirmation message is missing"
rm -f /tmp/out.txt

echo "== sprint 24, Req 2: a run still in progress is undeterminable (does not gate), not red, and says so distinctly =="
SPRINT_CI_PENDING=$(new_sprint "CI pending sprint")
$SCRIPT start "$SPRINT_CI_PENDING" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_CI_PENDING work"
$SCRIPT qa1 "$SPRINT_CI_PENDING" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_CI_PENDING" > /dev/null
CI_PENDING_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=pending $SCRIPT ship "$SPRINT_CI_PENDING" --commit "$CI_PENDING_COMMIT" \
  > /tmp/out.txt 2>&1 || fail "ship refused on a run still in progress -- this tool has no wait/poll mechanism, an in-progress run is undeterminable, not red"
grep -q "WARNING: could not determine CI status" /tmp/out.txt || fail "ship's pending-run warning is missing"
grep -q "have not finished yet" /tmp/out.txt || fail "ship's pending-run warning doesn't say the run hasn't finished, distinct from no CI at all"
rm -f /tmp/out.txt

echo "== sprint 24, Req 4 (added mid-flight): reship refuses over a red CI run for the exact commit, same as ship =="
SPRINT_RESHIP_CI=$(new_sprint "Reship CI status sprint")
$SCRIPT start "$SPRINT_RESHIP_CI" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_RESHIP_CI work"
$SCRIPT qa1 "$SPRINT_RESHIP_CI" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_RESHIP_CI" > /dev/null
RESHIP_CI_SHIPPED=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_RESHIP_CI" --commit "$RESHIP_CI_SHIPPED" > /dev/null
# A reship needs the sprint mid the LiveQA fix loop, not complete_ready --
# record a FAIL first, same as every other reship setup in this file.
$SCRIPT liveqa "$SPRINT_RESHIP_CI" --deployed-commit "$RESHIP_CI_SHIPPED" --verdict FAIL --notes "found a bug" > /dev/null
git commit -q --allow-empty -m "fix for sprint $SPRINT_RESHIP_CI"
RESHIP_CI_FIX=$(git rev-parse HEAD)
# Sprint 36, Req 3: reship's new audit-tree gate runs BEFORE the CI check
# below -- give this commit a live-loop PASS first so these tests still
# reach and exercise the CI-status behavior they're actually testing.
$SCRIPT qa1 "$SPRINT_RESHIP_CI" --verdict PASS --notes "live-loop audit" --commit "$RESHIP_CI_FIX" > /dev/null
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=red $SCRIPT reship "$SPRINT_RESHIP_CI" --commit "$RESHIP_CI_FIX" \
  > /tmp/out.txt 2>&1 && fail "reship succeeded despite a red CI run for the exact commit being reshipped (Req 4 regression)" || true
grep -q "CI is red for the exact commit being reshipped" /tmp/out.txt || fail "reship's CI-red refusal message is missing"
grep -qF "$RESHIP_CI_FIX" /tmp/out.txt || fail "reship's CI-red refusal doesn't name the commit"
# Checked against the actual last_shipped_commit field, not a blanket
# grep of --verbose output: sprint 36, Req 3's own live-loop audit above
# legitimately mentions $RESHIP_CI_FIX in its own history event detail
# (it's the commit that audit covers), so a plain substring search across
# the whole verbose output would false-positive on that, unrelated to
# whether the red-CI reship itself recorded anything.
python3 -c "
import json
s = json.load(open('docs/sprints/state/sprint-${SPRINT_RESHIP_CI}.json'))
assert s['last_shipped_commit'] != '$RESHIP_CI_FIX', 'a CI-red reship attempt must not have recorded the fix commit as last_shipped_commit'
"
rm -f /tmp/out.txt

echo "== sprint 24, Req 4: reship refuses when CI 'succeeded' but no real step executed, same specificity as ship =="
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=no-steps $SCRIPT reship "$SPRINT_RESHIP_CI" --commit "$RESHIP_CI_FIX" \
  > /tmp/out.txt 2>&1 && fail "reship succeeded on a run that reported success but never executed a real step" || true
grep -q "no real step actually executed" /tmp/out.txt || fail "reship's no-real-steps-executed refusal message is missing"
rm -f /tmp/out.txt

echo "== sprint 24, Req 4: an undeterminable CI status warns but does not gate a reship either -- the same decision, not decided twice =="
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=none $SCRIPT reship "$SPRINT_RESHIP_CI" --commit "$RESHIP_CI_FIX" \
  > /tmp/out.txt 2>&1 || fail "reship refused when CI status was genuinely undeterminable -- Req 4 requires the identical decision cmd_ship makes"
grep -q "WARNING: could not determine CI status" /tmp/out.txt || fail "reship's undeterminable-CI-status warning is missing"
grep -q "fix reshipped" /tmp/out.txt || fail "reship should have proceeded (undeterminable does not gate) -- its own success output is missing"
$SCRIPT status "$SPRINT_RESHIP_CI" --verbose 2>&1 | grep -qF "$RESHIP_CI_FIX" || \
  fail "reship should have recorded the fix commit as last_shipped_commit (undeterminable does not gate)"
rm -f /tmp/out.txt

echo "== sprint 24, Req 3: origin-ahead-of-record warns in status and liveqa, never gates, and doesn't misread as a bypassed push rule =="
SPRINT_ORIGIN=$(new_sprint "Origin-ahead sprint")
$SCRIPT start "$SPRINT_ORIGIN" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_ORIGIN work"
$SCRIPT qa1 "$SPRINT_ORIGIN" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_ORIGIN" > /dev/null
ORIGIN_SHIPPED_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_ORIGIN" --commit "$ORIGIN_SHIPPED_COMMIT" > /dev/null

# Simulate a second, real ship (often a headless Pipeman's own twin, per
# Finding C) landing on origin before this one's own bookkeeping caught
# up: a bare remote standing in for "origin", set up AFTER the commit
# this sprint already shipped, then advanced past it.
ORIGIN_BARE="$SANDBOX/.origin-bare.git"
git init -q --bare "$ORIGIN_BARE"
git remote add origin "$ORIGIN_BARE"
git push -q -u origin HEAD:main
echo "a second, real ship landing on origin" > "sprint24-second-ship.txt"
git add "sprint24-second-ship.txt"
git commit -q -m "a second, real ship (e.g. a headless Pipeman's own twin) landing on origin"
git push -q origin HEAD:main
# Deliberately do NOT run /sprint-ship for this second commit against
# THIS sprint's own record — that's the whole scenario: origin moved,
# last_shipped_commit did not, because cmd_ship's own state write for
# this second push hasn't happened (or belongs to a different sprint
# entirely; either way, this sprint's own record is now behind origin).

STATUS_ORIGIN_OUT=$($SCRIPT status "$SPRINT_ORIGIN" 2>&1)
echo "$STATUS_ORIGIN_OUT" | grep -q "carries 1 commit beyond this sprint's own recorded last_shipped_commit" || \
  fail "status did not surface the origin-ahead drift (Req 3) -- output: $STATUS_ORIGIN_OUT"
# The message legitimately CONTAINS the word "bypassed" -- as part of an
# explicit denial ("does NOT mean someone bypassed..."), the same
# foreclose-the-conflation wording pipeman.md itself already uses
# elsewhere. The actual test is that the denial is there, explicit and
# unambiguous, not that the word never appears.
echo "$STATUS_ORIGIN_OUT" | grep -q "does NOT mean someone bypassed" || \
  fail "status's origin-ahead warning should explicitly foreclose the bypassed-push-rule misreading, not just avoid repeating it"

# Warns, never gates: liveqa against the ORIGINAL shipped commit must
# still succeed cleanly despite origin now being ahead of the record.
LIVEQA_ORIGIN_OUT=$($SCRIPT liveqa "$SPRINT_ORIGIN" --deployed-commit "$ORIGIN_SHIPPED_COMMIT" --verdict PASS --notes ok 2>&1) || \
  fail "liveqa refused (or otherwise failed) solely because origin is ahead of the record -- Req 3 must never gate on this -- output: $LIVEQA_ORIGIN_OUT"
echo "$LIVEQA_ORIGIN_OUT" | grep -q "carries 1 commit beyond this sprint's own recorded last_shipped_commit" || \
  fail "liveqa did not surface the origin-ahead drift while reading last_shipped_commit to make a decision (Req 3)"

echo "== sprint 24, Req 3: origin-ahead-of-record warning is silent when there's nothing to report =="
SPRINT_ORIGIN_CLEAN=$(new_sprint "Origin clean sprint")
$SCRIPT start "$SPRINT_ORIGIN_CLEAN" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_ORIGIN_CLEAN work"
$SCRIPT qa1 "$SPRINT_ORIGIN_CLEAN" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_ORIGIN_CLEAN" > /dev/null
ORIGIN_CLEAN_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_ORIGIN_CLEAN" --commit "$ORIGIN_CLEAN_COMMIT" > /dev/null
git push -q origin HEAD:main
$SCRIPT status "$SPRINT_ORIGIN_CLEAN" 2>&1 | grep -q "carries.*commit.*beyond" && \
  fail "status warned about origin drift when origin and the record actually agree (false positive)"

echo "== sprint 25, Req 2: a write command's session identity, when the environment provides one, is recorded and surfaced by status (read-only) =="
SPRINT_CLAIM=$(new_sprint "Session claim sprint")
$SCRIPT start "$SPRINT_CLAIM" > /dev/null
CLAUDE_CODE_SESSION_ID="test-session-abc123" CLAUDE_CODE_AGENT="test-agent-xyz" \
  $SCRIPT qa1 "$SPRINT_CLAIM" --verdict PASS --notes ok > /dev/null
CLAIM_STATUS_OUT=$($SCRIPT status "$SPRINT_CLAIM" 2>&1)
echo "$CLAIM_STATUS_OUT" | grep -q "Last touched by: test-agent-xyz, session test-session-abc123" || \
  fail "status did not surface the recorded claim's exact agent/session (Req 2) -- output: $CLAIM_STATUS_OUT"

echo "== sprint 25, Req 2: an honest 'no session identity available' when the environment provides none, not a silently missing line =="
env -u CLAUDE_CODE_SESSION_ID -u CLAUDE_CODE_AGENT $SCRIPT dev-done "$SPRINT_CLAIM" > /dev/null
NOSESSION_STATUS_OUT=$($SCRIPT status "$SPRINT_CLAIM" 2>&1)
echo "$NOSESSION_STATUS_OUT" | grep -q "Last touched by: (unknown" || \
  fail "status did not honestly report a missing session identity -- output: $NOSESSION_STATUS_OUT"

echo "== sprint 25, Req 2: status itself never writes a claim -- it stays read-only =="
STATUS_HASH_BEFORE=$(sha256sum "docs/sprints/state/sprint-${SPRINT_CLAIM}.json" | cut -d' ' -f1)
$SCRIPT status "$SPRINT_CLAIM" > /dev/null 2>&1
STATUS_HASH_AFTER=$(sha256sum "docs/sprints/state/sprint-${SPRINT_CLAIM}.json" | cut -d' ' -f1)
[ "$STATUS_HASH_BEFORE" = "$STATUS_HASH_AFTER" ] || fail "status must never modify the sprint's own state file (Req 2's own explicit instruction)"

echo "== sprint 25, Req 4: rename updates registry, frontmatter and filename together, and preserves the original title =="
SPRINT_RENAME=$(new_sprint "Original working title")
$SCRIPT start "$SPRINT_RENAME" > /dev/null
RENAME_FILE_BEFORE=$($SCRIPT status "$SPRINT_RENAME" > /dev/null 2>&1; find docs/sprints/2-in-progress -name "sprint-${SPRINT_RENAME}_*.md")
[ -n "$RENAME_FILE_BEFORE" ] || fail "test setup broken: could not find sprint $SPRINT_RENAME's file before renaming"

$SCRIPT rename "$SPRINT_RENAME" --title "A narrower, more accurate title" > /tmp/out.txt 2>&1 || \
  fail "rename failed -- output: $(cat /tmp/out.txt)"
grep -q 'renamed: "Original working title" -> "A narrower, more accurate title"' /tmp/out.txt || \
  fail "rename's own confirmation message is missing or wrong"
rm -f /tmp/out.txt

RENAME_FILE_AFTER=$(find docs/sprints/2-in-progress -name "sprint-${SPRINT_RENAME}_*.md")
[ "$RENAME_FILE_AFTER" != "$RENAME_FILE_BEFORE" ] || fail "the filename did not actually change"
[ -f "$RENAME_FILE_AFTER" ] || fail "the new filename does not exist on disk"
[ ! -f "$RENAME_FILE_BEFORE" ] || fail "the old filename still exists on disk -- rename should move, not copy"
echo "$RENAME_FILE_AFTER" | grep -q "narrower-more-accurate-title" || fail "the new filename doesn't reflect the new title"

grep -q 'title: "A narrower, more accurate title"' "$RENAME_FILE_AFTER" || fail "the frontmatter's title: line was not updated"
grep -q 'original_title: "Original working title"' "$RENAME_FILE_AFTER" || fail "the frontmatter's original_title: line is missing or wrong"

REGISTRY_TITLE=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_RENAME}']['title'])")
[ "$REGISTRY_TITLE" = "A narrower, more accurate title" ] || fail "registry title was not updated to the new title"
REGISTRY_ORIGINAL=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_RENAME}']['original_title'])")
[ "$REGISTRY_ORIGINAL" = "Original working title" ] || fail "registry did not preserve the original title"

STATE_TITLE=$(python3 -c "import json; print(json.load(open('docs/sprints/state/sprint-${SPRINT_RENAME}.json'))['title'])")
[ "$STATE_TITLE" = "A narrower, more accurate title" ] || fail "state.json's own title was not updated"

echo "== sprint 25, Req 4: renaming a second time still preserves the TRUE original title, not just the pre-rename one =="
$SCRIPT rename "$SPRINT_RENAME" --title "Yet another title" > /dev/null
SECOND_RENAME_ORIGINAL=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_RENAME}']['original_title'])")
[ "$SECOND_RENAME_ORIGINAL" = "Original working title" ] || \
  fail "a second rename overwrote original_title with the intermediate name instead of preserving the true original"

echo "== sprint 25, Req 4: rename refuses an empty title, and refuses renaming to the same title =="
$SCRIPT rename "$SPRINT_RENAME" --title "" > /tmp/out.txt 2>&1 && fail "rename accepted an empty title" || true
grep -q "cannot be empty" /tmp/out.txt || fail "empty-title refusal message missing"
rm -f /tmp/out.txt
$SCRIPT rename "$SPRINT_RENAME" --title "Yet another title" > /tmp/out.txt 2>&1 && fail "rename accepted renaming to the exact current title" || true
grep -q "already titled" /tmp/out.txt || fail "same-title refusal message missing"
rm -f /tmp/out.txt

echo "== sprint 25, Req 4: rename does NOT touch phase, verdicts, or history =="
$SCRIPT qa1 "$SPRINT_RENAME" --verdict PASS --notes "before another rename" > /dev/null
PHASE_BEFORE=$($SCRIPT status "$SPRINT_RENAME" 2>&1 | grep "^Phase:")
HISTORY_LEN_BEFORE=$(python3 -c "import json; print(len(json.load(open('docs/sprints/state/sprint-${SPRINT_RENAME}.json'))['history']))")
$SCRIPT rename "$SPRINT_RENAME" --title "Renamed once more, post-PASS" > /dev/null
PHASE_AFTER=$($SCRIPT status "$SPRINT_RENAME" 2>&1 | grep "^Phase:")
[ "$PHASE_BEFORE" = "$PHASE_AFTER" ] || fail "rename changed the sprint's phase -- must not (Req 4)"
HISTORY_LEN_AFTER=$(python3 -c "import json; print(len(json.load(open('docs/sprints/state/sprint-${SPRINT_RENAME}.json'))['history']))")
[ "$HISTORY_LEN_BEFORE" = "$HISTORY_LEN_AFTER" ] || fail "rename appended to history -- must not (Req 4)"
QA1_RESULT_AFTER=$($SCRIPT status "$SPRINT_RENAME" 2>&1 | grep "^QA1 audit result:")
echo "$QA1_RESULT_AFTER" | grep -q "PASS" || fail "rename changed the recorded QA1 verdict -- must not (Req 4)"

echo "== sprint 25, Req 4: THE HASH QUESTION, TESTED NOT ASSUMED -- a rename after a QA1 PASS DOES require a fresh QA1 look, and this is the intended, correct behaviour =="
# A fresh, dedicated sprint, isolated from the phase/history test above
# (whose own rename of $SPRINT_RENAME already made ITS file stale relative
# to ITS own QA1 PASS by design -- that's the same mechanism this test
# checks directly and deliberately, not a bug to route around here).
SPRINT_RENAME_HASH=$(new_sprint "Hash gate rename sprint")
$SCRIPT start "$SPRINT_RENAME_HASH" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_RENAME_HASH work"
$SCRIPT qa1 "$SPRINT_RENAME_HASH" --verdict PASS --notes ok > /dev/null
$SCRIPT rename "$SPRINT_RENAME_HASH" --title "Renamed between QA1 PASS and dev-done" > /dev/null
$SCRIPT dev-done "$SPRINT_RENAME_HASH" > /tmp/out.txt 2>&1 && \
  fail "dev-done succeeded despite the sprint file changing (the rename itself) after QA1's PASS -- the hash gate should have caught this, per Req 4's own tested decision" || true
grep -q "requirements may have been amended after the audit" /tmp/out.txt || \
  fail "dev-done's refusal after a post-PASS rename doesn't give the expected reason -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt
# And the documented recovery path works: a fresh QA1 look on the CURRENT
# (renamed) file, then dev-done succeeds.
$SCRIPT qa1 "$SPRINT_RENAME_HASH" --verdict PASS --notes "re-audited the renamed file" > /dev/null
$SCRIPT dev-done "$SPRINT_RENAME_HASH" > /tmp/out.txt 2>&1 || \
  fail "dev-done still refused after a fresh QA1 PASS on the current (renamed) file -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt

echo "== sprint 25, Req 4: renaming BEFORE any QA1 PASS has nothing to invalidate =="
SPRINT_RENAME_EARLY=$(new_sprint "Pre-audit rename sprint")
$SCRIPT start "$SPRINT_RENAME_EARLY" > /dev/null
$SCRIPT rename "$SPRINT_RENAME_EARLY" --title "Renamed before QA1 ever looked" > /tmp/out.txt 2>&1 || \
  fail "rename before any audit failed -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt
git commit -q --allow-empty -m "sprint $SPRINT_RENAME_EARLY work"
$SCRIPT qa1 "$SPRINT_RENAME_EARLY" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_RENAME_EARLY" > /tmp/out.txt 2>&1 || \
  fail "dev-done refused for a sprint renamed before QA1 ever audited it -- nothing should be invalidated here -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt

echo "== sprint 25, Req 4: rename refuses cleanly for an unknown sprint id =="
$SCRIPT rename 999999 --title "Doesn't matter" > /tmp/out.txt 2>&1 && fail "rename succeeded for a nonexistent sprint id" || true
grep -q "not found in registry" /tmp/out.txt || fail "unknown-sprint-id refusal message missing"
rm -f /tmp/out.txt

echo "== sprint 27, Req 1: sprint_lifecycle.py itself performs zero git commit/add calls (static, mechanical, FAIL-level per QA1's own criterion) =="
grep -qE '"git",\s*"(commit|add)"|'"'"'git'"'"',\s*'"'"'(commit|add)'"'"'' "$REPO_ROOT/scripts/sprint_lifecycle.py" && \
  fail "sprint_lifecycle.py contains a git commit/add call -- this property must stay at zero (Req 1)"

echo "== sprint 27, Req 1: a writing command prints the completion notice, naming exactly what it wrote =="
SPRINT_NOTICE=$(new_sprint "Completion notice sprint")
$SCRIPT start "$SPRINT_NOTICE" > /tmp/out.txt 2>&1 || fail "start failed -- output: $(cat /tmp/out.txt)"
grep -q "^Wrote: " /tmp/out.txt || fail "start did not print the completion notice (Req 1)"
grep -q "sprint-${SPRINT_NOTICE}_" /tmp/out.txt || fail "the notice doesn't name the sprint file it moved/wrote"
grep -q "docs/sprints/registry.json" /tmp/out.txt || fail "the notice doesn't name registry.json"
grep -q "docs/sprints/state/sprint-${SPRINT_NOTICE}.json" /tmp/out.txt || fail "the notice doesn't name the new state file"
grep -qi "not committed" /tmp/out.txt || fail "the notice doesn't say these are uncommitted"
# The notice must read as a receipt, not an alarm -- read cold, per QA1's
# own acceptance criterion.
grep -qi "warning\|drift\|error" /tmp/out.txt && \
  fail "the completion notice reads like a problem report rather than a statement of what just happened (Req 1's own tone requirement)"
rm -f /tmp/out.txt
# And the notice is honest -- these files really are uncommitted right now.
git status --porcelain "docs/sprints/state/sprint-${SPRINT_NOTICE}.json" | grep -q "^??" || \
  fail "test setup broken: the state file the notice named should genuinely be untracked"

echo "== sprint 27, Req 1: a read-only command prints no completion notice =="
$SCRIPT status "$SPRINT_NOTICE" > /tmp/out.txt 2>&1 || fail "status failed"
grep -q "^Wrote: " /tmp/out.txt && fail "status (read-only) printed a completion notice -- it must never write anything (Req 1)"
rm -f /tmp/out.txt
$SCRIPT list > /tmp/out.txt 2>&1 || fail "list failed"
grep -q "^Wrote: " /tmp/out.txt && fail "list (read-only) printed a completion notice"
rm -f /tmp/out.txt
$SCRIPT gates > /tmp/out.txt 2>&1 || fail "gates failed"
grep -q "^Wrote: " /tmp/out.txt && fail "gates (read-only) printed a completion notice"
rm -f /tmp/out.txt

echo "== sprint 27, Req 1: the notice still fires (an honest receipt for what DID land) even when the command later refuses =="
$SCRIPT qa1 "$SPRINT_NOTICE" --verdict BOGUS --notes "invalid verdict, should refuse after nothing further is written" \
  > /tmp/out.txt 2>&1 && fail "qa1 accepted an invalid verdict (test setup broken)" || true
grep -q "^Wrote: " /tmp/out.txt && fail "a refused command with NOTHING written must not print a notice -- nothing landed on disk"
rm -f /tmp/out.txt

echo "== sprint 27, Req 3: cmd_liveqa RECORDS a sprint file amended since QA1's PASS -- it does not refuse =="
SPRINT_DRIFT_LQ=$(new_sprint "LiveQA sprint-file drift sprint")
$SCRIPT start "$SPRINT_DRIFT_LQ" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_DRIFT_LQ work"
$SCRIPT qa1 "$SPRINT_DRIFT_LQ" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_DRIFT_LQ" > /dev/null
DRIFT_LQ_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_DRIFT_LQ" --commit "$DRIFT_LQ_COMMIT" > /dev/null

# Amend the sprint file itself -- exactly Finding B's own scenario: a
# Master Controller (or anyone) editing the file during the live-test
# window, no lifecycle command involved.
DRIFT_LQ_FILE=$(find docs/sprints/2-in-progress -name "sprint-${SPRINT_DRIFT_LQ}_*.md")
[ -n "$DRIFT_LQ_FILE" ] || fail "test setup broken: could not find sprint $SPRINT_DRIFT_LQ's file"
printf '\n**Amended mid-flight, discovered an unsatisfiable acceptance criterion.**\n' >> "$DRIFT_LQ_FILE"

LQ_DRIFT_OUT=$($SCRIPT liveqa "$SPRINT_DRIFT_LQ" --deployed-commit "$DRIFT_LQ_COMMIT" --verdict PASS --notes "live test itself was fine" 2>&1)
LQ_DRIFT_STATUS=$?
[ "$LQ_DRIFT_STATUS" -eq 0 ] || fail "liveqa REFUSED over a sprint file amended since QA1's PASS -- Req 3 is explicit this must never refuse (FAIL-level) -- output: $LQ_DRIFT_OUT"
echo "$LQ_DRIFT_OUT" | grep -q "has changed since QA1's PASS" || \
  fail "liveqa's own verdict output doesn't surface the sprint-file drift (Req 3) -- output: $LQ_DRIFT_OUT"
# Not a die()-style refusal -- no "ERROR:" prefix (die()'s own, checked
# elsewhere throughout this file) and the verdict itself still printed as
# a real PASS below, not just a non-zero-exit check in isolation.
echo "$LQ_DRIFT_OUT" | grep -q "^ERROR:" && \
  fail "liveqa's drift notice came through die()'s own refusal path -- output: $LQ_DRIFT_OUT"
echo "$LQ_DRIFT_OUT" | grep -q "PASSED" || \
  fail "liveqa's own verdict output doesn't confirm the PASS went through -- output: $LQ_DRIFT_OUT"
# Recorded in the durable history, not only printed once and lost.
python3 -c "
import json
state = json.load(open('docs/sprints/state/sprint-${SPRINT_DRIFT_LQ}.json'))
events = [h['event'] for h in state['history']]
assert 'sprint_file_drift_since_audit' in events, f'drift event missing from history: {events}'
assert state['groundtruth_result'] == 'PASS', f'the live-test verdict itself was not recorded: {state[\"groundtruth_result\"]!r}'
assert state['phase'] == 'complete_ready', f'a PASS verdict should still move the sprint to complete_ready, drift or not: {state[\"phase\"]!r}'
"
$SCRIPT status "$SPRINT_DRIFT_LQ" 2>&1 | grep -q "LiveQA live result: PASS" || \
  fail "the live-test PASS itself must still be recorded and visible, drift or not"

echo "== sprint 27, Req 3: a sprint file UNCHANGED since QA1's PASS gets no drift note =="
SPRINT_NODRIFT_LQ=$(new_sprint "LiveQA no-drift sprint")
$SCRIPT start "$SPRINT_NODRIFT_LQ" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_NODRIFT_LQ work"
$SCRIPT qa1 "$SPRINT_NODRIFT_LQ" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_NODRIFT_LQ" > /dev/null
NODRIFT_LQ_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_NODRIFT_LQ" --commit "$NODRIFT_LQ_COMMIT" > /dev/null
NODRIFT_OUT=$($SCRIPT liveqa "$SPRINT_NODRIFT_LQ" --deployed-commit "$NODRIFT_LQ_COMMIT" --verdict PASS --notes ok 2>&1)
echo "$NODRIFT_OUT" | grep -q "has changed since QA1's PASS" && \
  fail "liveqa reported sprint-file drift when the file genuinely never changed (false positive)"
python3 -c "
import json
state = json.load(open('docs/sprints/state/sprint-${SPRINT_NODRIFT_LQ}.json'))
events = [h['event'] for h in state['history']]
assert 'sprint_file_drift_since_audit' not in events, f'a drift event was recorded when nothing drifted: {events}'
"

MAIN_BRANCH=$(git rev-parse --abbrev-ref HEAD)

echo "== sprint 28, Req 1: no workflows configured at all still ships as the benign undeterminable case (baseline, unaffected by the split) =="
SPRINT_CI_NOWF=$(new_sprint "CI no-workflows sprint")
$SCRIPT start "$SPRINT_CI_NOWF" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_CI_NOWF work, deliberately no .github/workflows/"
$SCRIPT qa1 "$SPRINT_CI_NOWF" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_CI_NOWF" > /dev/null
CI_NOWF_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=none $SCRIPT ship "$SPRINT_CI_NOWF" --commit "$CI_NOWF_COMMIT" \
  > /tmp/out.txt 2>&1 || fail "ship refused with no workflows configured at all -- must stay the benign undeterminable case (sprint 24's own rule)"
grep -q "no CI is configured" /tmp/out.txt || fail "ship's no-workflows-at-all warning doesn't say CI genuinely isn't configured"
rm -f /tmp/out.txt

echo "== sprint 28, Req 1: workflows configured but no CI run found for the commit is graded RED (refuses), NOT the benign undeterminable case =="
SPRINT_CI_SPLIT=$(new_sprint "CI split sprint")
$SCRIPT start "$SPRINT_CI_SPLIT" > /dev/null
mkdir -p .github/workflows
echo "name: ci" > .github/workflows/ci.yml
git add .github/workflows
git commit -q -m "sprint $SPRINT_CI_SPLIT: a workflow file exists, no run will ever exist for this fake commit"
$SCRIPT qa1 "$SPRINT_CI_SPLIT" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_CI_SPLIT" > /dev/null
CI_SPLIT_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=none $SCRIPT ship "$SPRINT_CI_SPLIT" --commit "$CI_SPLIT_COMMIT" \
  > /tmp/out.txt 2>&1 && fail "ship succeeded when workflows are configured but no CI run was found for the commit -- must refuse, not silently proceed" || true
grep -q "workflows are configured" /tmp/out.txt || fail "ship's workflows-configured-no-run refusal doesn't name the case"
grep -q "indistinguishable from a pipeline broken" /tmp/out.txt || fail "ship's workflows-configured-no-run refusal doesn't explain why this isn't benign"
$SCRIPT status "$SPRINT_CI_SPLIT" 2>&1 | grep -q "Phase: dev_agreed_done" || \
  fail "a workflows-configured-no-run ship attempt must not have moved the sprint's phase"
rm -f /tmp/out.txt

echo "== sprint 28, Req 1: the same sprint still ships cleanly once a run genuinely exists and is green -- not a permanent lockout =="
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_CI_SPLIT" --commit "$CI_SPLIT_COMMIT" \
  > /tmp/out.txt 2>&1 || fail "ship refused a workflows-configured commit with a genuinely green run -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt

echo "== sprint 28, Req 1: reship gets the identical workflows-configured-no-run treatment as ship (same question, same answer) =="
SPRINT_RESHIP_SPLIT=$(new_sprint "Reship CI split sprint")
$SCRIPT start "$SPRINT_RESHIP_SPLIT" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_RESHIP_SPLIT work"
$SCRIPT qa1 "$SPRINT_RESHIP_SPLIT" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_RESHIP_SPLIT" > /dev/null
RESHIP_SPLIT_SHIPPED=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_RESHIP_SPLIT" --commit "$RESHIP_SPLIT_SHIPPED" > /dev/null
$SCRIPT liveqa "$SPRINT_RESHIP_SPLIT" --deployed-commit "$RESHIP_SPLIT_SHIPPED" --verdict FAIL --notes "found a bug" > /dev/null
git commit -q --allow-empty -m "fix for sprint $SPRINT_RESHIP_SPLIT, workflows are already configured by now"
RESHIP_SPLIT_FIX=$(git rev-parse HEAD)
$SCRIPT qa1 "$SPRINT_RESHIP_SPLIT" --verdict PASS --notes "live-loop audit" --commit "$RESHIP_SPLIT_FIX" > /dev/null
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=none $SCRIPT reship "$SPRINT_RESHIP_SPLIT" --commit "$RESHIP_SPLIT_FIX" \
  > /tmp/out.txt 2>&1 && fail "reship succeeded when workflows are configured but no CI run was found for the commit (Req 1 regression)" || true
grep -q "workflows are configured" /tmp/out.txt || fail "reship's workflows-configured-no-run refusal doesn't name the case"
rm -f /tmp/out.txt

echo "== sprint 28, Req 2: ship refuses when --commit is not reachable from HEAD, before ever recording it as last_shipped_commit =="
SPRINT_UNREACH=$(new_sprint "Unreachable commit sprint")
$SCRIPT start "$SPRINT_UNREACH" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_UNREACH work"
$SCRIPT qa1 "$SPRINT_UNREACH" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_UNREACH" > /dev/null
UNREACH_MAIN_HEAD=$(git rev-parse HEAD)
# A real, valid commit object that is nonetheless not an ancestor of HEAD:
# branch off, add an empty commit (same tree content as its parent, so the
# ship gate's own tree-hash check still matches the audited tree and lets
# execution reach the reachability check being tested here), then return to
# the main branch WITHOUT that commit ever merging in. Left on its own
# branch rather than deleted -- reachable from HEAD is what's being tested,
# not reachable from anywhere at all, and a throwaway sandbox needs no
# cleanup beyond the final rm -rf.
git checkout -q -b unreachable-throwaway
git commit -q --allow-empty -m "a commit that will never be on $MAIN_BRANCH"
UNREACHABLE_COMMIT=$(git rev-parse HEAD)
git checkout -q "$MAIN_BRANCH"
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_UNREACH" --commit "$UNREACHABLE_COMMIT" \
  > /tmp/out.txt 2>&1 && fail "ship succeeded for a commit that is not reachable from HEAD" || true
grep -q "is not reachable from HEAD" /tmp/out.txt || fail "ship's unreachable-commit refusal message is missing"
$SCRIPT status "$SPRINT_UNREACH" --verbose 2>&1 | grep -qF "$UNREACHABLE_COMMIT" && \
  fail "an unreachable commit must never be recorded as last_shipped_commit"
$SCRIPT status "$SPRINT_UNREACH" 2>&1 | grep -q "Phase: dev_agreed_done" || \
  fail "a ship refused for unreachability must not have moved the sprint's phase"
rm -f /tmp/out.txt

echo "== sprint 28, Req 2: the same sprint ships fine once given a genuinely reachable commit -- not a permanent lockout =="
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_UNREACH" --commit "$UNREACH_MAIN_HEAD" \
  > /tmp/out.txt 2>&1 || fail "ship refused a genuinely reachable commit -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt

echo "== sprint 28, Req 3: repoint-shipped-commit re-points last_shipped_commit to a patch-id-equivalent (relocated) commit, and records the equivalence =="
SPRINT_REPOINT=$(new_sprint "Repoint sprint")
$SCRIPT start "$SPRINT_REPOINT" > /dev/null
echo "repoint sprint content" > "repoint-${SPRINT_REPOINT}.txt"
git add "repoint-${SPRINT_REPOINT}.txt"
git commit -q -m "sprint $SPRINT_REPOINT work"
$SCRIPT qa1 "$SPRINT_REPOINT" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_REPOINT" > /dev/null
REPOINT_ORIGINAL=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_REPOINT" --commit "$REPOINT_ORIGINAL" > /dev/null

# Simulate the Context Finding B shape: a rebase relocates the exact same
# patch onto a different base (unrelated work landing underneath it) --
# cherry-pick, which by construction preserves patch content while
# changing the commit's parent (and therefore its own SHA and tree).
git checkout -q -b repoint-relocated "${REPOINT_ORIGINAL}~1"
git commit -q --allow-empty -m "unrelated work underneath, simulating the moved base"
git cherry-pick "$REPOINT_ORIGINAL" > /dev/null
REPOINT_RELOCATED=$(git rev-parse HEAD)
git checkout -q "$MAIN_BRANCH"

$SCRIPT repoint-shipped-commit "$SPRINT_REPOINT" --commit "$REPOINT_RELOCATED" \
  > /tmp/out.txt 2>&1 || fail "repoint-shipped-commit refused a genuinely patch-id-equivalent relocated commit -- output: $(cat /tmp/out.txt)"
grep -q "re-pointed" /tmp/out.txt || fail "repoint's success message is missing"
grep -qF "$REPOINT_RELOCATED" /tmp/out.txt || fail "repoint's success message doesn't name the new commit"
$SCRIPT status "$SPRINT_REPOINT" --verbose 2>&1 | grep -qF "$REPOINT_RELOCATED" || \
  fail "repoint should have updated last_shipped_commit to the relocated commit"
python3 -c "
import json
state = json.load(open('docs/sprints/state/sprint-${SPRINT_REPOINT}.json'))
events = [h['event'] for h in state['history']]
assert 'repointed_shipped_commit' in events, f'no repointed_shipped_commit event recorded: {events}'
last = [h for h in state['history'] if h['event'] == 'repointed_shipped_commit'][-1]
assert '$REPOINT_ORIGINAL' in last['detail'], f'repoint history entry does not name the old commit: {last[\"detail\"]}'
assert '$REPOINT_RELOCATED' in last['detail'], f'repoint history entry does not name the new commit: {last[\"detail\"]}'
assert 'patch_id' in last['detail'], f'repoint history entry does not record the patch-id: {last[\"detail\"]}'
"
rm -f /tmp/out.txt

echo "== sprint 28, Req 3: repoint-shipped-commit refuses a commit whose patch-id does NOT match -- FAIL, no override =="
echo "totally different content" > "repoint-unrelated-${SPRINT_REPOINT}.txt"
git add "repoint-unrelated-${SPRINT_REPOINT}.txt"
git commit -q -m "unrelated commit, not the same patch at all"
REPOINT_UNRELATED=$(git rev-parse HEAD)
$SCRIPT repoint-shipped-commit "$SPRINT_REPOINT" --commit "$REPOINT_UNRELATED" \
  > /tmp/out.txt 2>&1 && fail "repoint-shipped-commit succeeded on a commit whose patch-id does not match -- must refuse, no override" || true
grep -q "is NOT the same patch as" /tmp/out.txt || fail "repoint's patch-id-mismatch refusal message is missing"
$SCRIPT status "$SPRINT_REPOINT" --verbose 2>&1 | grep -qF "$REPOINT_UNRELATED" && \
  fail "a patch-id-mismatched commit must never be recorded as last_shipped_commit"
$SCRIPT status "$SPRINT_REPOINT" --verbose 2>&1 | grep -qF "$REPOINT_RELOCATED" || \
  fail "last_shipped_commit should still be the relocated commit after a refused repoint attempt"
rm -f /tmp/out.txt

echo "== sprint 28, Req 3: repoint-shipped-commit works on an already-COMPLETE sprint, not just mid-liveqa -- the whole reason it exists (Context Finding B) =="
$SCRIPT liveqa "$SPRINT_REPOINT" --deployed-commit "$REPOINT_RELOCATED" --verdict PASS --notes ok > /dev/null
printf 'closing repoint sprint for the test\n' > /tmp/user_said_repoint.txt
$SCRIPT complete "$SPRINT_REPOINT" --user-said-file /tmp/user_said_repoint.txt > /dev/null
$SCRIPT status "$SPRINT_REPOINT" 2>&1 | grep -q "Phase: complete" || fail "test setup: sprint should be complete before this check"
$SCRIPT reship "$SPRINT_REPOINT" --commit "$REPOINT_RELOCATED" > /tmp/out.txt 2>&1 && \
  fail "test setup check: reship should refuse on a complete sprint, it succeeded instead" || true
git checkout -q -b repoint-relocated-2 "${REPOINT_ORIGINAL}~1"
git commit -q --allow-empty -m "different unrelated work underneath, second relocation"
git cherry-pick "$REPOINT_ORIGINAL" > /dev/null
REPOINT_RELOCATED_2=$(git rev-parse HEAD)
git checkout -q "$MAIN_BRANCH"
$SCRIPT repoint-shipped-commit "$SPRINT_REPOINT" --commit "$REPOINT_RELOCATED_2" \
  > /tmp/out.txt 2>&1 || fail "repoint-shipped-commit refused on a complete sprint -- this is exactly the dead end it exists to fix. output: $(cat /tmp/out.txt)"
$SCRIPT status "$SPRINT_REPOINT" --verbose 2>&1 | grep -qF "$REPOINT_RELOCATED_2" || \
  fail "repoint should have updated last_shipped_commit even on a complete sprint"
rm -f /tmp/out.txt /tmp/user_said_repoint.txt

echo "== sprint 28, Req 4: the ship gate's own tree comparison is unaffected -- patch-id appears nowhere in git_tree_hash_excluding's call graph =="
if grep -n "patch.id\|patch_id" scripts/sprint_lifecycle.py | grep -qi "git_tree_hash_excluding\|cmd_ship\b"; then
  fail "patch-id logic appears to have leaked into the ship gate's tree comparison (Req 4 regression) -- see the matching grep line above"
fi

echo "== sprint 28, Req 3: repoint-shipped-commit refuses cleanly when the sprint has no last_shipped_commit to re-point =="
SPRINT_NOREPOINT=$(new_sprint "No repoint target sprint")
$SCRIPT start "$SPRINT_NOREPOINT" > /dev/null
$SCRIPT repoint-shipped-commit "$SPRINT_NOREPOINT" --commit "$MAIN_BRANCH" \
  > /tmp/out.txt 2>&1 && fail "repoint-shipped-commit succeeded on a sprint with no last_shipped_commit on record" || true
grep -q "nothing to re-point" /tmp/out.txt || fail "repoint's no-last-shipped-commit refusal message is missing"
rm -f /tmp/out.txt

echo "== sprint 29, Req 1/2: a sprint closed only in another worktree is surfaced from here (state + registry), on status (both forms) and list =="
SPRINT_STATE_WT=$(new_sprint "State divergence sprint")
$SCRIPT start "$SPRINT_STATE_WT" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_STATE_WT work"
$SCRIPT qa1 "$SPRINT_STATE_WT" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_STATE_WT" > /dev/null
STATE_WT_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_STATE_WT" --commit "$STATE_WT_COMMIT" > /dev/null
# A new worktree checks out committed history only, never another
# worktree's uncommitted files -- sprint_lifecycle.py's own writes never
# get here any other way, same reason sprint 13's own worktree test
# commits before branching (line ~980, above).
git add -A
git commit -q -m "commit sprint $SPRINT_STATE_WT's shipped state so a second worktree starts from it"

STATE_WT_DIR="$(mktemp -d "${TMPDIR:-/tmp}/fully-completely-smoke-state-wt.XXXXXX")"
git worktree add -q -b smoke-state-wt-branch "$STATE_WT_DIR" > /dev/null
STATE_WT_SCRIPT="python3 $STATE_WT_DIR/scripts/sprint_lifecycle.py"

# Close it fully, but ONLY inside the other worktree -- nothing here ever
# commits or pushes that close, exactly the stranding Finding A describes
# ("/sprint-complete correctly writes where it runs; the close commit
# never reaches main"). sprint_lifecycle.py never touches git itself, so
# these writes sit as plain uncommitted files in $STATE_WT_DIR the whole
# time -- state_divergence_warning() reads worktree files directly, not
# through git, which is exactly why it can see this at all.
$STATE_WT_SCRIPT liveqa "$SPRINT_STATE_WT" --deployed-commit "$STATE_WT_COMMIT" --verdict PASS --notes ok > /dev/null
printf 'closing for the divergence test\n' > /tmp/state_wt_user_said.txt
$STATE_WT_SCRIPT complete "$SPRINT_STATE_WT" --user-said-file /tmp/state_wt_user_said.txt > /dev/null

# This tree's own phase must be completely unaffected by a close that
# happened only in the other worktree.
$SCRIPT status "$SPRINT_STATE_WT" 2>&1 | grep -qE "Phase: (liveqa_live|groundtruth_live)" || \
  fail "test setup: this tree's own phase should be unaffected by a close in the other worktree"

STATUS_STATE_OUT=$($SCRIPT status "$SPRINT_STATE_WT" 2>&1)
# Deliberately NOT asserting a bare `grep -q "WARNING"` here on its own:
# QA1 round 1 found that assertion still passes even with
# state_divergence_warning() fully dead, because worktree_divergence_warning()
# (sprint 13's own sprint-FILE check) happens to ALSO fire on this exact
# incident, since /sprint-complete changes the sprint file's frontmatter
# too -- see state_divergence_warning()'s own docstring. Asserting the
# PHASE and the worktree PATH below is what actually pins the new code,
# since sprint 13's own warning never mentions either.
echo "$STATUS_STATE_OUT" | grep -q "phase 'complete'" || \
  fail "status <id>'s state-divergence warning doesn't name the other tree's phase -- output: $STATUS_STATE_OUT"
echo "$STATUS_STATE_OUT" | grep -qF "$(basename "$STATE_WT_DIR")" || \
  fail "status <id>'s state-divergence warning doesn't name the diverging worktree's path"

$SCRIPT list 2>&1 | grep -E "^ *${SPRINT_STATE_WT} " -A1 | grep -q "WARNING" || \
  fail "list did not surface the stranded close next to the sprint it applies to (Req 2)"
$SCRIPT status 2>&1 | grep -F "Sprint ${SPRINT_STATE_WT}:" -A1 | grep -q "WARNING" || \
  fail "status with no id did not surface the stranded close (Req 2)"

git worktree remove --force "$STATE_WT_DIR" > /dev/null 2>&1 || rm -rf "$STATE_WT_DIR"
rm -f /tmp/state_wt_user_said.txt

echo "== sprint 29, Req 1/2 (QA1 round 1 finding): the worktree-created-BEFORE-start order -- this tree never has a state file at all for the sprint's entire life =="
# CLAUDE.md's own documented order for Dev Team 2: create the sprint,
# then /sprint-worktree "before Dev Team 2 starts building", THEN start
# there. cmd_new writes a registry entry and no state file; cmd_start
# writes the state file wherever it runs -- so under this order, THIS
# tree never has a local state file for this sprint at all, the exact
# path state_divergence_warning()'s original early return silently
# swallowed (QA1 round 1: reproduced end to end, isolated to
# scripts/sprint_lifecycle.py's own early `if not this_state_path.exists():
# return None`, before the registry comparison ever ran).
SPRINT_WT_FIRST=$(new_sprint "Worktree-created-first sprint")
git add -A
git commit -q -m "commit sprint $SPRINT_WT_FIRST's registry entry (no state file yet -- not started here)"

WT_FIRST_DIR="$(mktemp -d "${TMPDIR:-/tmp}/fully-completely-smoke-wt-first.XXXXXX")"
git worktree add -q -b smoke-wt-first-branch "$WT_FIRST_DIR" > /dev/null
WT_FIRST_SCRIPT="python3 $WT_FIRST_DIR/scripts/sprint_lifecycle.py"

# Everything -- start through complete -- happens ONLY in the worktree.
# This tree (main) never runs /sprint-start for this sprint at all.
$WT_FIRST_SCRIPT start "$SPRINT_WT_FIRST" > /dev/null
(cd "$WT_FIRST_DIR" && git commit -q --allow-empty -m "sprint $SPRINT_WT_FIRST work")
$WT_FIRST_SCRIPT qa1 "$SPRINT_WT_FIRST" --verdict PASS --notes ok > /dev/null
$WT_FIRST_SCRIPT dev-done "$SPRINT_WT_FIRST" > /dev/null
WT_FIRST_COMMIT=$(cd "$WT_FIRST_DIR" && git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $WT_FIRST_SCRIPT ship "$SPRINT_WT_FIRST" --commit "$WT_FIRST_COMMIT" > /dev/null
$WT_FIRST_SCRIPT liveqa "$SPRINT_WT_FIRST" --deployed-commit "$WT_FIRST_COMMIT" --verdict PASS --notes ok > /dev/null
printf 'closing for the worktree-first test\n' > /tmp/wt_first_user_said.txt
$WT_FIRST_SCRIPT complete "$SPRINT_WT_FIRST" --user-said-file /tmp/wt_first_user_said.txt > /dev/null

# Confirm the test's own premise: main genuinely has no state file for
# this sprint, ever, not just "hasn't been read yet".
[ -f "docs/sprints/state/sprint-${SPRINT_WT_FIRST}.json" ] && \
  fail "test setup: this tree should have NO state file for this sprint (it was never /sprint-start'ed here)"

$SCRIPT list 2>&1 | grep -E "^ *${SPRINT_WT_FIRST} " -A1 | grep -q "WARNING" || \
  fail "list did not surface a sprint closed in a worktree that was never started here (QA1 round 1 finding)"
$SCRIPT status 2>&1 | grep -F "Sprint ${SPRINT_WT_FIRST}:" -A1 | grep -q "WARNING" || \
  fail "status with no id did not surface a sprint closed in a worktree that was never started here (QA1 round 1 finding)"

STATUS_WT_FIRST_OUT=$($SCRIPT status "$SPRINT_WT_FIRST" 2>&1) && \
  fail "status <id> should refuse (no local state file exists here at all) -- it succeeded instead"
echo "$STATUS_WT_FIRST_OUT" | grep -qi "run /sprint-start" && \
  fail "status <id>'s refusal still tells the reader to start a sprint that's already been closed elsewhere -- the exact misleading message QA1 flagged"
echo "$STATUS_WT_FIRST_OUT" | grep -q "exists in 1 other worktree" || \
  fail "status <id>'s refusal doesn't name that the sprint exists elsewhere -- output: $STATUS_WT_FIRST_OUT"
echo "$STATUS_WT_FIRST_OUT" | grep -q "phase 'complete'" || \
  fail "status <id>'s refusal doesn't name the other tree's phase -- output: $STATUS_WT_FIRST_OUT"

git worktree remove --force "$WT_FIRST_DIR" > /dev/null 2>&1 || rm -rf "$WT_FIRST_DIR"
rm -f /tmp/wt_first_user_said.txt

echo "== sprint 29, Req 1/2: identical state across trees produces no warning, on status (both forms) and list =="
SPRINT_STATE_CLEAN=$(new_sprint "State agreement sprint")
$SCRIPT start "$SPRINT_STATE_CLEAN" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_STATE_CLEAN work"
$SCRIPT qa1 "$SPRINT_STATE_CLEAN" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_STATE_CLEAN" > /dev/null
git add -A
git commit -q -m "commit sprint $SPRINT_STATE_CLEAN's state so a second worktree starts from an identical copy"

STATE_CLEAN_WT_DIR="$(mktemp -d "${TMPDIR:-/tmp}/fully-completely-smoke-state-clean-wt.XXXXXX")"
git worktree add -q -b smoke-state-clean-wt-branch "$STATE_CLEAN_WT_DIR" > /dev/null

$SCRIPT status "$SPRINT_STATE_CLEAN" 2>&1 | grep -q "WARNING" && \
  fail "status <id> warned about state divergence when the other worktree's state is genuinely identical"
$SCRIPT list 2>&1 | grep -E "^ *${SPRINT_STATE_CLEAN} " -A1 | grep -q "WARNING" && \
  fail "list warned about state divergence when the other worktree's state is genuinely identical"
$SCRIPT status 2>&1 | grep -F "Sprint ${SPRINT_STATE_CLEAN}:" -A1 | grep -q "WARNING" && \
  fail "status with no id warned about state divergence when the other worktree's state is genuinely identical"

git worktree remove --force "$STATE_CLEAN_WT_DIR" > /dev/null 2>&1 || rm -rf "$STATE_CLEAN_WT_DIR"

echo "== sprint 29, Req 3: a branch tip moving off last_shipped_commit during liveqa_live is detected and the landed commit is named, including a docs-only push =="
SPRINT_TIPMOVE=$(new_sprint "Tip move sprint")
$SCRIPT start "$SPRINT_TIPMOVE" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_TIPMOVE work"
$SCRIPT qa1 "$SPRINT_TIPMOVE" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_TIPMOVE" > /dev/null
TIPMOVE_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_TIPMOVE" --commit "$TIPMOVE_COMMIT" > /dev/null
git push -q origin HEAD:main

# A push touching ONLY docs/sprints/ -- inside sprint 13's own
# SHIP_HASH_EXCLUDE_PATTERNS, so no content-based comparison anywhere
# could ever see this land. This is Finding B's own motivating incident
# (commit 4d67199, touching only docs/sprints/1-todo/... and
# registry.json), reproduced directly rather than assumed equivalent.
echo "placeholder" > "docs/sprints/1-todo/tipmove-placeholder-${SPRINT_TIPMOVE}.md"
git add "docs/sprints/1-todo/tipmove-placeholder-${SPRINT_TIPMOVE}.md"
git commit -q -m "docs-only bookkeeping landing during sprint ${SPRINT_TIPMOVE}'s live gate"
TIPMOVE_DOCS_COMMIT=$(git rev-parse HEAD)
git push -q origin HEAD:main

LIVEQA_TIPMOVE_OUT=$($SCRIPT liveqa "$SPRINT_TIPMOVE" --deployed-commit "$TIPMOVE_COMMIT" --verdict PASS --notes ok 2>&1) || \
  fail "liveqa refused (or failed) solely because the branch tip moved during the live gate -- Req 3 must never gate on this -- output: $LIVEQA_TIPMOVE_OUT"
echo "$LIVEQA_TIPMOVE_OUT" | grep -q "carries 1 commit beyond this sprint's own recorded last_shipped_commit" || \
  fail "liveqa did not detect the branch tip moving off last_shipped_commit during the live gate (Req 3) -- output: $LIVEQA_TIPMOVE_OUT"
echo "$LIVEQA_TIPMOVE_OUT" | grep -qF "${TIPMOVE_DOCS_COMMIT:0:7}" || \
  fail "liveqa's tip-move warning doesn't name the actual commit that landed (Req 3: 'say which commits landed since') -- output: $LIVEQA_TIPMOVE_OUT"
rm -f "docs/sprints/1-todo/tipmove-placeholder-${SPRINT_TIPMOVE}.md"

echo "== sprint 33, Req 2: abort refuses with no --user-said, before any state or file mutation, and names /sprint-block as the alternative =="
SPRINT_ABORT=$(new_sprint "Abort gate sprint")
$SCRIPT start "$SPRINT_ABORT" > /dev/null
$SCRIPT abort "$SPRINT_ABORT" --reason "no real content exists" \
  > /tmp/out.txt 2>&1 && fail "abort succeeded with no --user-said -- Req 2 must refuse, no override" || true
grep -q "\-\-user-said is required and must be non-empty" /tmp/out.txt || fail "abort's missing-user-said refusal message is missing"
grep -qF "/sprint-block ${SPRINT_ABORT} --reason" /tmp/out.txt || fail "abort's refusal doesn't name /sprint-block as the alternative (Req 5)"
$SCRIPT status "$SPRINT_ABORT" 2>&1 | grep -q "Phase: dev_build" || fail "a refused abort must not have mutated state"
ABORT_REFUSED_FILE=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_ABORT}']['file'])")
echo "$ABORT_REFUSED_FILE" | grep -q "2-in-progress/" || fail "a refused abort must not have moved the sprint file -- got: $ABORT_REFUSED_FILE"
[ -f "$ABORT_REFUSED_FILE" ] || fail "the registry's recorded file path doesn't exist on disk after a refused abort: $ABORT_REFUSED_FILE"
rm -f /tmp/out.txt

echo "== sprint 33, Req 3: abort refuses with --user-said but no --reason -- '(none given)' can no longer be produced =="
$SCRIPT abort "$SPRINT_ABORT" --user-said "yes, abandon it" \
  > /tmp/out.txt 2>&1 && fail "abort succeeded with no --reason -- Req 3 must refuse" || true
grep -q "\-\-reason is required and must be non-empty" /tmp/out.txt || fail "abort's missing-reason refusal message is missing"
rm -f /tmp/out.txt

echo "== sprint 33, Req 1: a successful abort records the REAL actor, never the hardcoded 'human' =="
CLAUDE_CODE_AGENT="dev-team-1" $SCRIPT abort "$SPRINT_ABORT" --user-said "yes, abandon it" --reason "no real content exists" \
  > /tmp/out.txt 2>&1 || fail "abort with both arguments present should have succeeded -- output: $(cat /tmp/out.txt)"
grep -q "none given" /tmp/out.txt && fail "'(none given)' must no longer be producible (Req 3)"
python3 -c "
import json
state = json.load(open('docs/sprints/state/sprint-${SPRINT_ABORT}.json'))
last = state['history'][-1]
assert last['event'] == 'aborted', f'expected an aborted event, got {last}'
assert last['actor'] == 'dev-team-1', f'actor must be the real CLAUDE_CODE_AGENT value, not \"human\": {last}'
"
$SCRIPT status "$SPRINT_ABORT" 2>&1 | grep -q "Phase: dev_build" && fail "abort should have moved the sprint out of dev_build"
rm -f /tmp/out.txt

echo "== sprint 33, Req 4 (QA1 round 1 finding): block on a sprint that was NEVER /sprint-start'ed refuses cleanly, with nothing moved and no analysis discarded =="
SPRINT_NEVER_STARTED=$(new_sprint "Never started sprint")
$SCRIPT block "$SPRINT_NEVER_STARTED" --reason "the content this sprint needs does not exist yet" \
  > /tmp/out.txt 2>&1 && fail "block succeeded on a sprint with no state file -- QA1 round 1 must stay fixed" || true
grep -q "has no state file" /tmp/out.txt || fail "block's never-started refusal message is missing"
grep -q "Nothing has been moved" /tmp/out.txt || fail "block's never-started refusal doesn't say nothing moved"
NEVER_STARTED_FILE=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_NEVER_STARTED}']['file'])")
echo "$NEVER_STARTED_FILE" | grep -q "1-todo/" || fail "a refused block on a never-started sprint must leave the file in 1-todo/ -- got: $NEVER_STARTED_FILE"
python3 -c "
import json
r = json.load(open('docs/sprints/registry.json'))
assert r['sprints']['${SPRINT_NEVER_STARTED}']['status'] == 'todo', 'registry status must be untouched (todo)'
"
[ -f "docs/sprints/state/sprint-${SPRINT_NEVER_STARTED}.json" ] && fail "a refused block must not have created a state file"
rm -f /tmp/out.txt

echo "== sprint 33, Req 4: block refuses with no --reason, and requires no --user-said (non-destructive) =="
SPRINT_BLOCK=$(new_sprint "Block sprint")
$SCRIPT start "$SPRINT_BLOCK" > /dev/null
$SCRIPT block "$SPRINT_BLOCK" \
  > /tmp/out.txt 2>&1 && fail "block succeeded with no --reason -- must refuse" || true
grep -q "\-\-reason is required and must be non-empty" /tmp/out.txt || fail "block's missing-reason refusal message is missing"
rm -f /tmp/out.txt

echo "== sprint 33, Req 4: block returns the sprint to the planner -- id preserved, never moved to 5-abandoned, analysis retrievable, real actor recorded =="
CLAUDE_CODE_AGENT="qa1" $SCRIPT block "$SPRINT_BLOCK" --reason "cards.json content does not exist yet, cannot build without it" \
  > /tmp/out.txt 2>&1 || fail "block with a real reason should have succeeded -- output: $(cat /tmp/out.txt)"
grep -q "returned to the planner" /tmp/out.txt || fail "block's success message is missing"
grep -q "cards.json content does not exist" /tmp/out.txt || fail "block's success output doesn't echo the analysis"

BLOCK_FILE=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_BLOCK}']['file'])")
echo "$BLOCK_FILE" | grep -q "4-blocked/" || fail "block did not move the file to docs/sprints/4-blocked/ -- got: $BLOCK_FILE"
echo "$BLOCK_FILE" | grep -q "5-abandoned" && fail "block must never move anything to 5-abandoned"
[ -f "$BLOCK_FILE" ] || fail "the registry's recorded file path doesn't actually exist on disk: $BLOCK_FILE"

python3 -c "
import json
reg = json.load(open('docs/sprints/registry.json'))
entry = reg['sprints']['${SPRINT_BLOCK}']
assert entry['status'] == 'blocked', f'registry status must be blocked, not: {entry[\"status\"]}'
assert entry['status'] != 'abandoned'
state = json.load(open('docs/sprints/state/sprint-${SPRINT_BLOCK}.json'))
assert state['phase'] == 'blocked', f'phase must be blocked: {state}'
assert state['id'] == ${SPRINT_BLOCK}, 'sprint id must be preserved'
last = state['history'][-1]
assert last['event'] == 'blocked', f'expected a blocked event: {last}'
assert last['actor'] == 'qa1', f'actor must be the real CLAUDE_CODE_AGENT value: {last}'
assert 'cards.json content does not exist' in last['detail'], f'analysis must be recorded in history: {last}'
"

BLOCK_STATUS_OUT=$($SCRIPT status "$SPRINT_BLOCK" --verbose 2>&1)
echo "$BLOCK_STATUS_OUT" | grep -q "cards.json content does not exist" || \
  fail "the stated analysis must be retrievable from /sprint-status --verbose alone (Req 4's own LiveQA criterion) -- output: $BLOCK_STATUS_OUT"
rm -f /tmp/out.txt

echo "== sprint 33, Req 6: re-filing a blocked sprint is /sprint-start again, with no special handling =="
BLOCK_STARTED_BEFORE=$(cat "docs/sprints/state/sprint-${SPRINT_BLOCK}.json")
$SCRIPT start "$SPRINT_BLOCK" > /tmp/out.txt 2>&1 || fail "re-starting a blocked sprint should succeed like any other start -- output: $(cat /tmp/out.txt)"
python3 -c "
import json
reg = json.load(open('docs/sprints/registry.json'))
entry = reg['sprints']['${SPRINT_BLOCK}']
assert entry['status'] == 'in_progress', f'restarted sprint should be in_progress: {entry[\"status\"]}'
assert '2-in-progress/' in entry['file'], f'restarted sprint file should be back in 2-in-progress/: {entry[\"file\"]}'
state = json.load(open('docs/sprints/state/sprint-${SPRINT_BLOCK}.json'))
assert state['phase'] == 'dev_build', f'restarted sprint should be back in dev_build: {state[\"phase\"]}'
"
rm -f /tmp/out.txt

echo "== sprint 36, Req 1a: re-filing a blocked sprint preserves history (with a new restart event appended, not a replacement), audit_rounds/live_test_rounds/started, and resets every gate-result field =="
python3 -c "
import json
before = json.loads('''$BLOCK_STARTED_BEFORE''')
after = json.load(open('docs/sprints/state/sprint-${SPRINT_BLOCK}.json'))
assert len(after['history']) == len(before['history']) + 1, f'expected history to be preserved plus exactly one new event: before={len(before[\"history\"])} after={len(after[\"history\"])}'
assert after['history'][:-1] == before['history'], 'the restart must APPEND to history, never replace or reorder the prior events (the blocked analysis must survive)'
assert after['history'][-1]['event'] == 'sprint_restarted', f'expected a sprint_restarted event, got {after[\"history\"][-1]}'
assert after['audit_rounds'] == before['audit_rounds'], 'audit_rounds must be kept across a re-file from blocked'
assert after['live_test_rounds'] == before['live_test_rounds'], 'live_test_rounds must be kept across a re-file from blocked'
assert after['started'] == before['started'], 'the ORIGINAL started timestamp must be kept, not reset to the restart time'
for field in ('qa1_audit_result', 'qa1_audit_file_hash', 'qa1_audited_tree_hash', 'last_shipped_commit', 'groundtruth_result'):
    assert after[field] is None, f'{field} must be reset to None on a re-file from blocked, got {after[field]!r}'
assert after.get('live_loop_audit_trees') == [], f'live_loop_audit_trees must be reset to empty on a re-file from blocked, got {after.get(\"live_loop_audit_trees\")}'
assert after['phase'] == 'dev_build'
"

echo "== sprint 39, Req 1: the audited-file hash ignores a status: rewrite, but still changes on a body or title edit =="
SPRINT_HASH_DIRECT=$(new_sprint "Hash exclusion sprint")
$SCRIPT start "$SPRINT_HASH_DIRECT" > /dev/null
HASH_DIRECT_FILE=$(find docs/sprints/2-in-progress -name "sprint-${SPRINT_HASH_DIRECT}_*.md")
python3 -c "
import sys
sys.path.insert(0, 'scripts')
from sprint_lifecycle import audited_file_hash, update_frontmatter_status, file_hash
from pathlib import Path

p = Path('$HASH_DIRECT_FILE')
before = audited_file_hash(p)
before_whole = file_hash(p)

# A pure lifecycle bookkeeping write -- exactly what start/block/complete/
# abort do -- must not change the new-scheme hash, even though it DOES
# change the raw file bytes (so the OLD whole-file scheme WOULD see it).
update_frontmatter_status(p, 'blocked')
after_status_only = audited_file_hash(p)
after_status_only_whole = file_hash(p)
assert after_status_only == before, 'a pure status: rewrite must not change the audited-file hash (Req 1)'
assert after_status_only_whole != before_whole, 'test setup sanity check failed: the status rewrite must actually change the raw bytes, or this test proves nothing'
update_frontmatter_status(p, 'in_progress')  # restore

# A body edit still changes the hash.
with open(p, 'a') as f:
    f.write('\nA genuinely new body line.\n')
after_body = audited_file_hash(p)
assert after_body != before, 'a body edit must still change the audited-file hash'
"
# A title: edit still changes the hash -- Req 1 deliberately does NOT
# exclude title/original_title (a rename must keep forcing a fresh audit).
python3 -c "
import sys, re
sys.path.insert(0, 'scripts')
from sprint_lifecycle import audited_file_hash
from pathlib import Path
p = Path('$HASH_DIRECT_FILE')
before = audited_file_hash(p)
text = p.read_text()
text2 = re.sub(r'(?m)^title:.*\$', 'title: \"A changed title\"', text, count=1)
assert text2 != text, 'test setup: title: line must actually exist and be replaceable'
p.write_text(text2)
after = audited_file_hash(p)
assert after != before, 'a title: edit must still change the audited-file hash -- Req 1 deliberately does not exclude title'
"

echo "== sprint 39, Req 1a: an old (pre-39), whole-file-scheme hash on record compares correctly for a genuinely unchanged file =="
SPRINT_OLD_SCHEME_OK=$(new_sprint "Old scheme unchanged sprint")
$SCRIPT start "$SPRINT_OLD_SCHEME_OK" > /dev/null
$SCRIPT qa1 "$SPRINT_OLD_SCHEME_OK" --verdict PASS --notes "will be downgraded to simulate a pre-39 record" > /dev/null
OLD_OK_STATE="docs/sprints/state/sprint-${SPRINT_OLD_SCHEME_OK}.json"
OLD_OK_FILE=$(find docs/sprints/2-in-progress -name "sprint-${SPRINT_OLD_SCHEME_OK}_*.md")
python3 -c "
import sys, json
sys.path.insert(0, 'scripts')
from sprint_lifecycle import file_hash
from pathlib import Path
p = '$OLD_OK_STATE'
s = json.load(open(p))
del s['qa1_audit_hash_scheme']
s['qa1_audit_file_hash'] = file_hash(Path('$OLD_OK_FILE'))
json.dump(s, open(p, 'w'), indent=2)
"
$SCRIPT dev-done "$SPRINT_OLD_SCHEME_OK" > /tmp/out.txt 2>&1 || \
  fail "dev-done wrongly refused a genuinely unchanged file under a simulated pre-39 hash -- Req 1a regression -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt

echo "== sprint 39, Req 1a: an old (pre-39), whole-file-scheme hash on record still catches a real edit =="
SPRINT_OLD_SCHEME_EDIT=$(new_sprint "Old scheme edited sprint")
$SCRIPT start "$SPRINT_OLD_SCHEME_EDIT" > /dev/null
$SCRIPT qa1 "$SPRINT_OLD_SCHEME_EDIT" --verdict PASS --notes "will be downgraded to simulate a pre-39 record" > /dev/null
OLD_EDIT_STATE="docs/sprints/state/sprint-${SPRINT_OLD_SCHEME_EDIT}.json"
OLD_EDIT_FILE=$(find docs/sprints/2-in-progress -name "sprint-${SPRINT_OLD_SCHEME_EDIT}_*.md")
python3 -c "
import sys, json
sys.path.insert(0, 'scripts')
from sprint_lifecycle import file_hash
from pathlib import Path
p = '$OLD_EDIT_STATE'
s = json.load(open(p))
del s['qa1_audit_hash_scheme']
s['qa1_audit_file_hash'] = file_hash(Path('$OLD_EDIT_FILE'))
json.dump(s, open(p, 'w'), indent=2)
"
echo "### A real edit after the simulated pre-39 audit" >> "$OLD_EDIT_FILE"
$SCRIPT dev-done "$SPRINT_OLD_SCHEME_EDIT" > /tmp/out.txt 2>&1 && \
  fail "dev-done wrongly passed an EDITED file under a simulated pre-39 hash -- Req 1a regression: a silent pass-through" || true
grep -q "has changed since QA1's PASS" /tmp/out.txt || fail "old-scheme edit-detection refusal message missing -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt

echo "== sprint 39, Req 2a/2b: a sprint blocked AFTER a QA1 PASS, with its file unchanged, returns to its exact pre-block phase with every gate field kept =="
SPRINT_KEEP=$(new_sprint "Gate-preserving restart sprint")
$SCRIPT start "$SPRINT_KEEP" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_KEEP work"
$SCRIPT qa1 "$SPRINT_KEEP" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_KEEP" > /dev/null
KEEP_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_KEEP" --commit "$KEEP_COMMIT" > /dev/null
# Now sitting in liveqa_live -- an undecided question comes up (a
# workshop-shaped scenario) and Dev Team blocks it, nothing about the
# code itself in question.
KEEP_STATE_BEFORE=$(cat "docs/sprints/state/sprint-${SPRINT_KEEP}.json")
$SCRIPT block "$SPRINT_KEEP" --reason "waiting on a decision, nothing about the shipped code needs to change" > /dev/null

KEEP_START_OUT=$($SCRIPT start "$SPRINT_KEEP" 2>&1)
echo "$KEEP_START_OUT" | grep -qi "Gates KEPT" || \
  fail "re-filing an unchanged, PASSed, blocked sprint should say gates were KEPT -- output: $KEEP_START_OUT"
echo "$KEEP_START_OUT" | grep -qF "liveqa_live" || \
  fail "the restart output should name the restored pre-block phase -- output: $KEEP_START_OUT"

python3 -c "
import json
before = json.loads('''$KEEP_STATE_BEFORE''')
after = json.load(open('docs/sprints/state/sprint-${SPRINT_KEEP}.json'))
assert after['phase'] == 'liveqa_live', f\"phase must be restored to its pre-block phase: {after['phase']}\"
assert after['qa1_audit_result'] == before['qa1_audit_result'] == 'PASS'
assert after['qa1_audit_file_hash'] == before['qa1_audit_file_hash']
assert after['qa1_audit_hash_scheme'] == before['qa1_audit_hash_scheme']
assert after['qa1_audited_tree_hash'] == before['qa1_audited_tree_hash']
assert after['last_shipped_commit'] == before['last_shipped_commit']
assert after['groundtruth_result'] == before['groundtruth_result']
assert after['live_loop_audit_trees'] == before['live_loop_audit_trees']
assert after['audit_rounds'] == before['audit_rounds']
assert after['live_test_rounds'] == before['live_test_rounds']
assert after['pre_block_phase'] is None, 'pre_block_phase should be cleared once restored'
assert len(after['history']) == len(before['history']) + 2, 'exactly two new events (blocked, sprint_restarted), nothing replaced'
assert after['history'][-1]['event'] == 'sprint_restarted'
assert 'KEPT' in after['history'][-1]['detail'], f\"restart event should say gates were kept: {after['history'][-1]}\"
"

echo "== sprint 39, Req 2a (QA1 round 1 finding): blocking an already-blocked sprint must not overwrite its real pre-block phase with 'blocked' itself =="
SPRINT_DOUBLE_BLOCK=$(new_sprint "Double block sprint")
$SCRIPT start "$SPRINT_DOUBLE_BLOCK" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_DOUBLE_BLOCK work"
$SCRIPT qa1 "$SPRINT_DOUBLE_BLOCK" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_DOUBLE_BLOCK" > /dev/null
DOUBLE_BLOCK_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_DOUBLE_BLOCK" --commit "$DOUBLE_BLOCK_COMMIT" > /dev/null
$SCRIPT block "$SPRINT_DOUBLE_BLOCK" --reason "first block, waiting on a decision" > /dev/null
# Block it a SECOND time, before ever re-filing -- the exact QA1 repro:
# the second block used to overwrite pre_block_phase with "blocked"
# itself.
$SCRIPT block "$SPRINT_DOUBLE_BLOCK" --reason "still waiting, blocking again" > /dev/null
python3 -c "
import json
s = json.load(open('docs/sprints/state/sprint-${SPRINT_DOUBLE_BLOCK}.json'))
assert s['pre_block_phase'] == 'liveqa_live', f\"a second block must not overwrite the real pre-block phase with 'blocked' itself: {s['pre_block_phase']}\"
"
DOUBLE_BLOCK_START_OUT=$($SCRIPT start "$SPRINT_DOUBLE_BLOCK" 2>&1)
echo "$DOUBLE_BLOCK_START_OUT" | grep -qi "Gates KEPT" || \
  fail "re-filing a twice-blocked, unchanged, PASSed sprint should still say gates were KEPT -- output: $DOUBLE_BLOCK_START_OUT"
DOUBLE_BLOCK_ENTRY_STATUS=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_DOUBLE_BLOCK}']['status'])")
python3 -c "
import json
s = json.load(open('docs/sprints/state/sprint-${SPRINT_DOUBLE_BLOCK}.json'))
assert s['phase'] == 'liveqa_live', f\"a twice-blocked sprint must come back to its real pre-block phase, not 'blocked': {s['phase']}\"
assert s['phase'] != 'blocked', 'the exact wedge QA1 found -- state stuck at blocked while the registry/file already moved'
"
[ "$DOUBLE_BLOCK_ENTRY_STATUS" = "in_progress" ] || \
  fail "registry status must agree with the restored phase, not disagree (the exact wedge QA1 found) -- got: $DOUBLE_BLOCK_ENTRY_STATUS"

echo "== sprint 39, Req 2d: code committed while blocked is still refused at ship -- restoring the phase does not bypass the tree-content checks =="
SPRINT_KEEP_SHIP=$(new_sprint "Gate-preserving restart before ship sprint")
$SCRIPT start "$SPRINT_KEEP_SHIP" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_KEEP_SHIP work"
$SCRIPT qa1 "$SPRINT_KEEP_SHIP" --verdict PASS --notes "looked good" > /dev/null
$SCRIPT dev-done "$SPRINT_KEEP_SHIP" > /dev/null
# Blocked BEFORE ever shipping -- a real "waiting on a decision before
# Pipeman pushes" scenario.
$SCRIPT block "$SPRINT_KEEP_SHIP" --reason "waiting on a go/no-go decision before shipping" > /dev/null
KEEP_SHIP_OUT=$($SCRIPT start "$SPRINT_KEEP_SHIP" 2>&1)
echo "$KEEP_SHIP_OUT" | grep -qi "Gates KEPT" || \
  fail "re-filing an unchanged, dev_agreed_done, blocked sprint should say gates were KEPT -- output: $KEEP_SHIP_OUT"
python3 -c "
import json
after = json.load(open('docs/sprints/state/sprint-${SPRINT_KEEP_SHIP}.json'))
assert after['phase'] == 'dev_agreed_done', f\"phase must be restored to dev_agreed_done: {after['phase']}\"
"
# Now an unaudited commit lands (simulating a change made while it sat
# blocked) -- ship must still refuse it, exactly as it would have before
# this sprint, because qa1_audited_tree_hash was never touched by the
# gate-preserving restart.
echo "sneaky change before first ship" > "sneaky-ship-${SPRINT_KEEP_SHIP}.txt"
git add "sneaky-ship-${SPRINT_KEEP_SHIP}.txt"
git commit -q -m "unaudited change landing after the gate-preserving restart of sprint $SPRINT_KEEP_SHIP"
SNEAKY_SHIP_COMMIT=$(git rev-parse HEAD)
$SCRIPT ship "$SPRINT_KEEP_SHIP" --commit "$SNEAKY_SHIP_COMMIT" > /tmp/out.txt 2>&1 && \
  fail "ship succeeded on a commit that was never audited -- Req 2d regression: gate-preserving restart must not let unaudited code through" || true
grep -q "doesn't match what QA1 audited" /tmp/out.txt || \
  fail "ship's refusal for the unaudited commit is missing or reworded -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt

echo "== sprint 39, Req 2d: code committed while blocked is still refused at reship -- restoring the phase does not bypass the tree-content checks =="
echo "sneaky change while blocked" > "sneaky-${SPRINT_KEEP}.txt"
git add "sneaky-${SPRINT_KEEP}.txt"
git commit -q -m "unaudited change landing after the gate-preserving restart of sprint $SPRINT_KEEP"
SNEAKY_COMMIT=$(git rev-parse HEAD)
$SCRIPT reship "$SPRINT_KEEP" --commit "$SNEAKY_COMMIT" > /tmp/out.txt 2>&1 && \
  fail "reship succeeded on a commit that was never audited -- Req 2d regression: gate-preserving restart must not let unaudited code through" || true
grep -q "has no QA1 PASS currently on record for it" /tmp/out.txt || \
  fail "reship's refusal for the unaudited commit is missing or reworded -- output: $(cat /tmp/out.txt)"
rm -f /tmp/out.txt

echo "== sprint 39, Req 2c: a sprint blocked BEFORE any QA1 PASS resets gates, and says why =="
SPRINT_RESET_NOPASS=$(new_sprint "No-PASS block reset sprint")
$SCRIPT start "$SPRINT_RESET_NOPASS" > /dev/null
$SCRIPT block "$SPRINT_RESET_NOPASS" --reason "requirements aren't real yet" > /dev/null
RESET_NOPASS_OUT=$($SCRIPT start "$SPRINT_RESET_NOPASS" 2>&1)
echo "$RESET_NOPASS_OUT" | grep -qi "Gates RESET" || fail "a never-PASSed blocked sprint should say gates were RESET -- output: $RESET_NOPASS_OUT"
echo "$RESET_NOPASS_OUT" | grep -q "no QA1 PASS is on record" || fail "the reset reason should name the missing PASS -- output: $RESET_NOPASS_OUT"

echo "== sprint 39, Req 2c: a sprint blocked AFTER a QA1 PASS, but edited while blocked, resets gates and says the file changed =="
SPRINT_RESET_EDITED=$(new_sprint "Edited-while-blocked reset sprint")
$SCRIPT start "$SPRINT_RESET_EDITED" > /dev/null
$SCRIPT qa1 "$SPRINT_RESET_EDITED" --verdict PASS --notes ok > /dev/null
$SCRIPT block "$SPRINT_RESET_EDITED" --reason "a real requirements question came up" > /dev/null
RESET_EDITED_FILE=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_RESET_EDITED}']['file'])")
echo "### Master Controller's repair, changing the requirement" >> "$RESET_EDITED_FILE"
RESET_EDITED_OUT=$($SCRIPT start "$SPRINT_RESET_EDITED" 2>&1)
echo "$RESET_EDITED_OUT" | grep -qi "Gates RESET" || fail "an edited-while-blocked sprint should say gates were RESET -- output: $RESET_EDITED_OUT"
echo "$RESET_EDITED_OUT" | grep -q "the sprint file has changed since QA1's PASS" || \
  fail "the reset reason should name the file change -- output: $RESET_EDITED_OUT"
python3 -c "
import json
after = json.load(open('docs/sprints/state/sprint-${SPRINT_RESET_EDITED}.json'))
assert after['phase'] == 'dev_build'
assert after['qa1_audit_result'] is None
"

echo "== sprint 39, Req 2c: a sprint blocked by a version before Req 2a (no pre_block_phase on record) resets gates and says so =="
SPRINT_RESET_UNKNOWN=$(new_sprint "Unknown pre-block phase reset sprint")
$SCRIPT start "$SPRINT_RESET_UNKNOWN" > /dev/null
$SCRIPT qa1 "$SPRINT_RESET_UNKNOWN" --verdict PASS --notes ok > /dev/null
$SCRIPT block "$SPRINT_RESET_UNKNOWN" --reason "simulating a pre-Req-2a block" > /dev/null
python3 -c "
import json
p = 'docs/sprints/state/sprint-${SPRINT_RESET_UNKNOWN}.json'
s = json.load(open(p))
del s['pre_block_phase']
json.dump(s, open(p, 'w'), indent=2)
"
RESET_UNKNOWN_OUT=$($SCRIPT start "$SPRINT_RESET_UNKNOWN" 2>&1)
echo "$RESET_UNKNOWN_OUT" | grep -qi "Gates RESET" || fail "an unknown-pre-block-phase sprint should say gates were RESET -- output: $RESET_UNKNOWN_OUT"
echo "$RESET_UNKNOWN_OUT" | grep -q "no pre-block phase is on record" || \
  fail "the reset reason should name the missing pre-block phase -- output: $RESET_UNKNOWN_OUT"

echo "== sprint 36, Req 1: /sprint-start refuses a sprint already in dev_build (not never-started, not blocked), with nothing changed =="
BLOCK_FILE_BEFORE_REFUSAL=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_BLOCK}']['file'])")
BLOCK_STATE_BEFORE_REFUSAL=$(cat "docs/sprints/state/sprint-${SPRINT_BLOCK}.json")
$SCRIPT start "$SPRINT_BLOCK" > /tmp/out.txt 2>&1 && fail "start succeeded a second time on a sprint already in dev_build -- Req 1 regression" || true
grep -q "has already started and is at phase 'dev_build'" /tmp/out.txt || fail "start's already-started refusal doesn't name the current phase -- got: $(cat /tmp/out.txt)"
grep -q "Nothing has been changed" /tmp/out.txt || fail "start's already-started refusal doesn't say nothing changed"
grep -q -- "--override" /tmp/out.txt && fail "start's already-started refusal must not offer an override"
BLOCK_FILE_AFTER_REFUSAL=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_BLOCK}']['file'])")
[ "$BLOCK_FILE_BEFORE_REFUSAL" = "$BLOCK_FILE_AFTER_REFUSAL" ] || fail "a refused start moved the sprint's registered file"
[ "$(cat "docs/sprints/state/sprint-${SPRINT_BLOCK}.json")" = "$BLOCK_STATE_BEFORE_REFUSAL" ] || fail "a refused start modified the state file"
rm -f /tmp/out.txt

echo "== sprint 36, Req 1: /sprint-start refuses an already-COMPLETE sprint -- the exact ShowOffTest incident (a mis-issued start erasing a closed sprint's record), reproduced and confirmed fixed =="
COMPLETE_STATE_BEFORE=$(cat "docs/sprints/state/sprint-${SPRINT_1}.json")
COMPLETE_FILE_BEFORE=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_1}']['file'])")
$SCRIPT start "$SPRINT_1" > /tmp/out.txt 2>&1 && fail "start succeeded on an already-complete sprint -- the exact record-erasure incident this Req exists to fix" || true
grep -q "has already started and is at phase 'complete'" /tmp/out.txt || fail "start's refusal on a complete sprint doesn't name the phase -- got: $(cat /tmp/out.txt)"
[ "$(cat "docs/sprints/state/sprint-${SPRINT_1}.json")" = "$COMPLETE_STATE_BEFORE" ] || fail "a refused start erased or altered sprint 1's closed state -- regression"
COMPLETE_FILE_AFTER=$(python3 -c "import json; print(json.load(open('docs/sprints/registry.json'))['sprints']['${SPRINT_1}']['file'])")
[ "$COMPLETE_FILE_BEFORE" = "$COMPLETE_FILE_AFTER" ] || fail "a refused start moved sprint 1's file out of 3-done/"
echo "$COMPLETE_FILE_AFTER" | grep -q "3-done/" || fail "sprint 1's file should still be sitting in docs/sprints/3-done/"
rm -f /tmp/out.txt

echo "== sprint 36, Req 1: /sprint-start refuses a sprint mid-LiveQA-loop (liveqa_live), a third distinct in-flight phase =="
SPRINT_START_GUARD=$(new_sprint "Start guard sprint")
$SCRIPT start "$SPRINT_START_GUARD" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_START_GUARD work"
$SCRIPT qa1 "$SPRINT_START_GUARD" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_START_GUARD" > /dev/null
START_GUARD_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_START_GUARD" --commit "$START_GUARD_COMMIT" > /dev/null
$SCRIPT start "$SPRINT_START_GUARD" > /tmp/out.txt 2>&1 && fail "start succeeded on a sprint mid the LiveQA fix loop -- Req 1 regression" || true
grep -q "has already started and is at phase 'liveqa_live'" /tmp/out.txt || fail "start's refusal on a liveqa_live sprint doesn't name the phase -- got: $(cat /tmp/out.txt)"
$SCRIPT status "$SPRINT_START_GUARD" | grep -q "Phase: liveqa_live" || fail "a refused start should not have moved sprint $SPRINT_START_GUARD off liveqa_live"
rm -f /tmp/out.txt

echo "== sprint 36, Req 1 (QA1 round 1 finding): /sprint-start refuses a sprint ABORTED BEFORE IT EVER STARTED -- no state file exists for it, exactly like a never-started sprint, but the registry's own status is 'abandoned', not 'todo' =="
SPRINT_ABORTED_PRESTART=$(new_sprint "Aborted before start sprint")
printf 'yes, abandon it, never built\n' > /tmp/abort_prestart_said.txt
$SCRIPT abort "$SPRINT_ABORTED_PRESTART" --user-said-file /tmp/abort_prestart_said.txt --reason "content for this one never materialized" > /dev/null
[ ! -f "docs/sprints/state/sprint-${SPRINT_ABORTED_PRESTART}.json" ] || fail "test setup broken: an abort of a never-started sprint should not create a state file"
python3 -c "
import json
reg = json.load(open('docs/sprints/registry.json'))
assert reg['sprints']['${SPRINT_ABORTED_PRESTART}']['status'] == 'abandoned', 'test setup broken: registry status should be abandoned'
"
$SCRIPT start "$SPRINT_ABORTED_PRESTART" > /tmp/out.txt 2>&1 && \
  fail "start succeeded on a sprint aborted before it ever started -- this revives a burned sprint id with one ordinary command, the exact incident QA1 demonstrated against sprint 37" || true
grep -q "its registry status is 'abandoned', not 'todo'" /tmp/out.txt || \
  fail "start's refusal on a pre-start-aborted sprint doesn't name the cause -- got: $(cat /tmp/out.txt)"
grep -q "Nothing has been changed" /tmp/out.txt || fail "start's pre-start-abort refusal doesn't say nothing changed"
[ ! -f "docs/sprints/state/sprint-${SPRINT_ABORTED_PRESTART}.json" ] || \
  fail "a refused start must not have fabricated a state file for an aborted sprint"
python3 -c "
import json
reg = json.load(open('docs/sprints/registry.json'))
entry = reg['sprints']['${SPRINT_ABORTED_PRESTART}']
assert entry['status'] == 'abandoned', f'a refused start must not have changed the registry status: {entry[\"status\"]}'
assert '5-abandoned/' in entry['file'], f'a refused start must not have moved the file out of 5-abandoned/: {entry[\"file\"]}'
"
rm -f /tmp/out.txt /tmp/abort_prestart_said.txt

echo "== sprint 36, Req 1 (QA1 round 2 finding): /sprint-start refuses ANY registry status other than 'todo' when no state file exists, not only 'abandoned' -- the code now matches its own docstring's stronger guarantee =="
SPRINT_CORRUPTED_STATUS=$(new_sprint "Corrupted status sprint")
# Simulate a hand-damaged registry entry: some status other than 'todo'
# or 'abandoned', with no state file ever created for it (this shape
# should be structurally unreachable through the normal commands, which
# is exactly why it needs its own explicit guard rather than trusting
# "abandoned" to be the only non-todo status a state-file-less sprint
# could ever have).
python3 -c "
import json
reg = json.load(open('docs/sprints/registry.json'))
reg['sprints']['${SPRINT_CORRUPTED_STATUS}']['status'] = 'done'
json.dump(reg, open('docs/sprints/registry.json', 'w'), indent=2)
"
[ ! -f "docs/sprints/state/sprint-${SPRINT_CORRUPTED_STATUS}.json" ] || fail "test setup broken: this sprint should have no state file"
$SCRIPT start "$SPRINT_CORRUPTED_STATUS" > /tmp/out.txt 2>&1 && \
  fail "start succeeded on a sprint with no state file and a corrupted registry status ('done') -- Req 1 must refuse anything other than 'todo', not only 'abandoned'" || true
grep -q "its registry status is 'done', not 'todo'" /tmp/out.txt || \
  fail "start's refusal doesn't name the corrupted status -- got: $(cat /tmp/out.txt)"
[ ! -f "docs/sprints/state/sprint-${SPRINT_CORRUPTED_STATUS}.json" ] || \
  fail "a refused start must not have fabricated a state file"
rm -f /tmp/out.txt

echo "== sprint 36, Req 1b: a genuinely never-started sprint's fresh state is unaffected by the phase guard -- diffed against the known schema, not just 'it worked' =="
SPRINT_NEVER_STARTED_2=$(new_sprint "Fresh start shape sprint")
$SCRIPT start "$SPRINT_NEVER_STARTED_2" > /dev/null
python3 -c "
import json
state = json.load(open('docs/sprints/state/sprint-${SPRINT_NEVER_STARTED_2}.json'))
expected_keys = {'id', 'title', 'phase', 'qa1_audit_result', 'qa1_audit_file_hash',
                  'qa1_audit_hash_scheme', 'qa1_audited_tree_hash', 'last_shipped_commit',
                  'groundtruth_result', 'live_loop_audit_trees', 'pre_block_phase',
                  'audit_rounds', 'live_test_rounds', 'started',
                  'completed', 'history', 'last_claim'}
assert set(state.keys()) == expected_keys, f'fresh-start schema drifted: {sorted(state.keys())}'
assert state['phase'] == 'dev_build'
assert state['qa1_audit_result'] is None
assert state['live_loop_audit_trees'] == []
assert state['audit_rounds'] == 0
assert state['live_test_rounds'] == 0
assert state['completed'] is None
assert state['qa1_audit_hash_scheme'] is None
assert state['pre_block_phase'] is None
assert len(state['history']) == 1 and state['history'][0]['event'] == 'sprint_started'
"

echo "== sprint 36, Req 2: /sprint-block refuses a COMPLETE sprint, with nothing changed -- the other half of the two-command record-erasure path =="
COMPLETE_STATE_BEFORE_BLOCK=$(cat "docs/sprints/state/sprint-${SPRINT_1}.json")
$SCRIPT block "$SPRINT_1" --reason "trying to block an already-closed sprint" > /tmp/out.txt 2>&1 && \
  fail "block succeeded on an already-complete sprint -- Req 2 regression" || true
grep -q "is 'complete' and cannot be blocked" /tmp/out.txt || fail "block's refusal on a complete sprint is missing -- got: $(cat /tmp/out.txt)"
grep -q "Nothing has been changed" /tmp/out.txt || fail "block's complete-sprint refusal doesn't say nothing changed"
[ "$(cat "docs/sprints/state/sprint-${SPRINT_1}.json")" = "$COMPLETE_STATE_BEFORE_BLOCK" ] || fail "a refused block modified sprint 1's closed state"
$SCRIPT status "$SPRINT_1" | grep -q "Phase: complete" || fail "sprint $SPRINT_1 should still read as complete after the refused block"
rm -f /tmp/out.txt

echo "== sprint 36, Req 2: /sprint-block refuses an ABORTED sprint too, with nothing changed =="
ABORTED_STATE_BEFORE_BLOCK=$(cat "docs/sprints/state/sprint-${SPRINT_ABORT}.json")
$SCRIPT block "$SPRINT_ABORT" --reason "trying to block an already-aborted sprint" > /tmp/out.txt 2>&1 && \
  fail "block succeeded on an already-aborted sprint -- Req 2 regression" || true
grep -q "is 'aborted' and cannot be blocked" /tmp/out.txt || fail "block's refusal on an aborted sprint is missing -- got: $(cat /tmp/out.txt)"
[ "$(cat "docs/sprints/state/sprint-${SPRINT_ABORT}.json")" = "$ABORTED_STATE_BEFORE_BLOCK" ] || fail "a refused block modified sprint $SPRINT_ABORT's aborted state"
rm -f /tmp/out.txt

echo "== sprint 36, Req 2: /sprint-block still works normally on every other phase (dev_build), unaffected by the new guard =="
SPRINT_BLOCK_STILL_WORKS=$(new_sprint "Block still works sprint")
$SCRIPT start "$SPRINT_BLOCK_STILL_WORKS" > /dev/null
$SCRIPT block "$SPRINT_BLOCK_STILL_WORKS" --reason "still buildable-checking that dev_build is unaffected" > /tmp/out.txt 2>&1 || \
  fail "block on a dev_build sprint should still succeed -- output: $(cat /tmp/out.txt)"
grep -q "returned to the planner" /tmp/out.txt || fail "block on dev_build should still succeed as before Req 2"
rm -f /tmp/out.txt

echo "== sprint 36, Req 3a: a live-loop FAIL recorded on the SAME tree AFTER its own PASS revokes it -- latest verdict for that tree wins, refuse =="
SPRINT_TREE_ORDER=$(new_sprint "Tree order sprint")
$SCRIPT start "$SPRINT_TREE_ORDER" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_TREE_ORDER work"
$SCRIPT qa1 "$SPRINT_TREE_ORDER" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_TREE_ORDER" > /dev/null
TREE_ORDER_SHIPPED=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_TREE_ORDER" --commit "$TREE_ORDER_SHIPPED" > /dev/null
$SCRIPT liveqa "$SPRINT_TREE_ORDER" --deployed-commit "$TREE_ORDER_SHIPPED" --verdict FAIL --notes "found a bug" > /dev/null
echo "tree order fix" > "sprint${SPRINT_TREE_ORDER}-fix.txt"
git add "sprint${SPRINT_TREE_ORDER}-fix.txt"
git commit -q -m "fix for sprint $SPRINT_TREE_ORDER"
TREE_ORDER_FIX=$(git rev-parse HEAD)
# A PASS, then a LATER FAIL, both against this exact tree.
$SCRIPT qa1 "$SPRINT_TREE_ORDER" --verdict PASS --notes "first look, looked fine" --commit "$TREE_ORDER_FIX" > /dev/null
$SCRIPT qa1 "$SPRINT_TREE_ORDER" --verdict FAIL --notes "second look, found a real problem" --commit "$TREE_ORDER_FIX" > /dev/null
$SCRIPT reship "$SPRINT_TREE_ORDER" --commit "$TREE_ORDER_FIX" > /tmp/out.txt 2>&1 && \
  fail "reship succeeded even though the LATEST QA1 verdict for this exact tree is FAIL, not PASS -- Req 3a regression" || true
grep -q "the latest QA1 verdict on record for it is FAIL, not PASS" /tmp/out.txt || \
  fail "reship's refusal doesn't explain that the latest verdict for this tree is FAIL -- got: $(cat /tmp/out.txt)"
# LiveQA round 1 FINDING: this is exactly its case (b) -- a PASS was on
# record for this tree, then superseded by a FAIL. The message must say
# so plainly, never the old, false "has never been through QA1's audit
# successfully" (which is untrue here -- it WAS audited, and it WAS
# PASSed, just not most recently).
grep -q "a PASS was recorded earlier for this exact tree and has since been superseded" /tmp/out.txt || \
  fail "reship's refusal doesn't say a PASS for this tree was superseded -- got: $(cat /tmp/out.txt)"
grep -q "has never been through QA1's audit successfully" /tmp/out.txt && \
  fail "reship's refusal must not claim this tree was never audited -- LiveQA's round-1 finding, it WAS audited and DID pass, just not most recently"
rm -f /tmp/out.txt

echo "== sprint 36, Req 3a: a live-loop FAIL on a DIFFERENT tree never revokes a PASS already on record for the tree actually being reshipped =="
SPRINT_TREE_ORDER_2=$(new_sprint "Tree order sprint 2")
$SCRIPT start "$SPRINT_TREE_ORDER_2" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_TREE_ORDER_2 work"
$SCRIPT qa1 "$SPRINT_TREE_ORDER_2" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_TREE_ORDER_2" > /dev/null
TREE_ORDER_2_SHIPPED=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_TREE_ORDER_2" --commit "$TREE_ORDER_2_SHIPPED" > /dev/null
$SCRIPT liveqa "$SPRINT_TREE_ORDER_2" --deployed-commit "$TREE_ORDER_2_SHIPPED" --verdict FAIL --notes "found a bug" > /dev/null
echo "good fix" > "sprint${SPRINT_TREE_ORDER_2}-good.txt"
git add "sprint${SPRINT_TREE_ORDER_2}-good.txt"
git commit -q -m "good fix for sprint $SPRINT_TREE_ORDER_2"
TREE_ORDER_2_GOOD_FIX=$(git rev-parse HEAD)
$SCRIPT qa1 "$SPRINT_TREE_ORDER_2" --verdict PASS --notes "this one's fine" --commit "$TREE_ORDER_2_GOOD_FIX" > /dev/null
# A DIFFERENT tree, audited FAIL afterward -- must not touch the good fix's own standing PASS.
echo "bad fix, different tree, never reshipped" > "sprint${SPRINT_TREE_ORDER_2}-bad.txt"
git add "sprint${SPRINT_TREE_ORDER_2}-bad.txt"
git commit -q -m "a different, bad fix for sprint $SPRINT_TREE_ORDER_2"
TREE_ORDER_2_BAD_FIX=$(git rev-parse HEAD)
$SCRIPT qa1 "$SPRINT_TREE_ORDER_2" --verdict FAIL --notes "this one has a real problem" --commit "$TREE_ORDER_2_BAD_FIX" > /dev/null
$SCRIPT reship "$SPRINT_TREE_ORDER_2" --commit "$TREE_ORDER_2_GOOD_FIX" > /tmp/out.txt 2>&1 || \
  fail "reship refused the good fix's own tree just because a DIFFERENT tree was later FAILed -- Req 3a regression -- output: $(cat /tmp/out.txt)"
grep -q "fix reshipped" /tmp/out.txt || fail "reship of the good fix should have succeeded"
rm -f /tmp/out.txt

echo "== sprint 36, Req 3a (LiveQA round 1 finding, case a): gate 1 PASSes a tree, a LATER live-loop FAIL on that exact same tree supersedes it -- refuse, with accurate wording, no self-contradictory 'Gate 1's tree ... does not match' when the two hashes are identical =="
SPRINT_GATE1_SUPERSEDED=$(new_sprint "Gate1 superseded sprint")
$SCRIPT start "$SPRINT_GATE1_SUPERSEDED" > /dev/null
echo "gate1 content" > "sprint${SPRINT_GATE1_SUPERSEDED}-c1.txt"
git add "sprint${SPRINT_GATE1_SUPERSEDED}-c1.txt"
git commit -q -m "sprint $SPRINT_GATE1_SUPERSEDED work"
$SCRIPT qa1 "$SPRINT_GATE1_SUPERSEDED" --verdict PASS --notes "gate-1 pass" > /dev/null
$SCRIPT dev-done "$SPRINT_GATE1_SUPERSEDED" > /dev/null
GATE1_SUPERSEDED_SHIPPED=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_GATE1_SUPERSEDED" --commit "$GATE1_SUPERSEDED_SHIPPED" > /dev/null
$SCRIPT liveqa "$SPRINT_GATE1_SUPERSEDED" --deployed-commit "$GATE1_SUPERSEDED_SHIPPED" --verdict FAIL --notes "found a bug" > /dev/null
# A live-loop audit against the EXACT SAME commit gate 1 already PASSed --
# reproduces LiveQA's own repro directly (a real QA1 second look at the
# already-shipped commit finding a real problem on reflection).
$SCRIPT qa1 "$SPRINT_GATE1_SUPERSEDED" --verdict FAIL --notes "second look, found a real problem" --commit "$GATE1_SUPERSEDED_SHIPPED" > /dev/null
$SCRIPT reship "$SPRINT_GATE1_SUPERSEDED" --commit "$GATE1_SUPERSEDED_SHIPPED" > /tmp/out.txt 2>&1 && \
  fail "reship succeeded even though a live-loop FAIL superseded gate 1's own PASS on this exact tree -- Req 3a regression" || true
grep -q "a PASS was recorded earlier for this exact tree and has since been superseded" /tmp/out.txt || \
  fail "reship's refusal doesn't say gate 1's own PASS for this tree was superseded -- got: $(cat /tmp/out.txt)"
grep -q "has never been through QA1's audit successfully" /tmp/out.txt && \
  fail "reship's refusal must not claim this tree was never audited -- it WAS, by gate 1 itself"
grep -qE "Gate 1's currently PASSed tree is [0-9a-f]+, which does not match" /tmp/out.txt && \
  fail "reship's refusal must not print the same tree hash twice and call it a mismatch -- LiveQA's exact round-1 repro"
rm -f /tmp/out.txt

echo "== sprint 34, Req 1/2/4: closing a sprint from inside a Dev Team 2 worktree prints an unmissable statement that it hasn't reached main, naming the branch and Pipeman by name -- and main's own view stays stranded (the sprint 32 incident, reproduced directly) =="
SPRINT_WT_CLOSE=$(new_sprint "Worktree close sprint")
$SCRIPT start "$SPRINT_WT_CLOSE" > /dev/null
git add -A
git commit -q -m "commit sprint $SPRINT_WT_CLOSE's state so a worktree can see it"

WT_CLOSE_DIR="$(mktemp -d "${TMPDIR:-/tmp}/fully-completely-smoke-wt-close.XXXXXX")"
git worktree add -q -b "smoke-wt-close-branch-${SPRINT_WT_CLOSE}" "$WT_CLOSE_DIR" > /dev/null
WT_CLOSE_SCRIPT="python3 $WT_CLOSE_DIR/scripts/sprint_lifecycle.py"

# The entire rest of the lifecycle, driven from inside the worktree --
# exactly the sprint 32 shape (real gates, real authorization, closed
# from inside the worktree).
(cd "$WT_CLOSE_DIR" && git commit -q --allow-empty -m "sprint $SPRINT_WT_CLOSE work")
$WT_CLOSE_SCRIPT qa1 "$SPRINT_WT_CLOSE" --verdict PASS --notes ok > /dev/null
$WT_CLOSE_SCRIPT dev-done "$SPRINT_WT_CLOSE" > /dev/null
WT_CLOSE_COMMIT=$(cd "$WT_CLOSE_DIR" && git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $WT_CLOSE_SCRIPT ship "$SPRINT_WT_CLOSE" --commit "$WT_CLOSE_COMMIT" > /dev/null
$WT_CLOSE_SCRIPT liveqa "$SPRINT_WT_CLOSE" --deployed-commit "$WT_CLOSE_COMMIT" --verdict PASS --notes ok > /dev/null
printf 'yes, close it\n' > /tmp/wt_close_user_said.txt
WT_CLOSE_OUT=$($WT_CLOSE_SCRIPT complete "$SPRINT_WT_CLOSE" --user-said-file /tmp/wt_close_user_said.txt 2>&1)
echo "$WT_CLOSE_OUT" | grep -q "Sprint ${SPRINT_WT_CLOSE} closed" || \
  fail "the close itself should have succeeded (a strand warning is a statement, not a gate) -- output: $WT_CLOSE_OUT"
echo "$WT_CLOSE_OUT" | grep -q "HAS NOT REACHED main" || \
  fail "closing from inside a worktree must print the unmissable strand statement (Req 1/4) -- output: $WT_CLOSE_OUT"
echo "$WT_CLOSE_OUT" | grep -qF "smoke-wt-close-branch-${SPRINT_WT_CLOSE}" || \
  fail "the strand statement doesn't name the actual branch"
echo "$WT_CLOSE_OUT" | grep -q "PIPEMAN" || \
  fail "the strand statement doesn't name Pipeman explicitly (Req 2)"
echo "$WT_CLOSE_OUT" | grep -q "must NOT push or merge it yourself" || \
  fail "the strand statement doesn't say Dev Team 2 must not push/merge it itself (Req 2)"

# Main's own view: this sprint must still read as NOT complete -- the
# whole point being reproduced.
$SCRIPT status "$SPRINT_WT_CLOSE" 2>&1 | grep -q "Phase: complete$" && \
  fail "main's own view should NOT show this sprint as complete -- it was closed only in the worktree"

git worktree remove --force "$WT_CLOSE_DIR" > /dev/null 2>&1 || rm -rf "$WT_CLOSE_DIR"
rm -f /tmp/wt_close_user_said.txt

echo "== sprint 34, Req 1: closing from the primary checkout (no worktrees, or none diverging) prints no strand statement =="
SPRINT_MAIN_CLOSE=$(new_sprint "Main close sprint")
$SCRIPT start "$SPRINT_MAIN_CLOSE" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_MAIN_CLOSE work"
$SCRIPT qa1 "$SPRINT_MAIN_CLOSE" --verdict PASS --notes ok > /dev/null
$SCRIPT dev-done "$SPRINT_MAIN_CLOSE" > /dev/null
MAIN_CLOSE_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_MAIN_CLOSE" --commit "$MAIN_CLOSE_COMMIT" > /dev/null
$SCRIPT liveqa "$SPRINT_MAIN_CLOSE" --deployed-commit "$MAIN_CLOSE_COMMIT" --verdict PASS --notes ok > /dev/null
printf 'yes, close it\n' > /tmp/main_close_user_said.txt
MAIN_CLOSE_OUT=$($SCRIPT complete "$SPRINT_MAIN_CLOSE" --user-said-file /tmp/main_close_user_said.txt 2>&1)
echo "$MAIN_CLOSE_OUT" | grep -q "HAS NOT REACHED main" && \
  fail "closing from the primary checkout must never print the strand statement (false positive) -- output: $MAIN_CLOSE_OUT"
rm -f /tmp/main_close_user_said.txt

echo "== sprint 38, Req 2 (Finding F2): the tree description distinguishes a real, never-committed repo from every other case, on a real git init with no commits =="
BRANCH_SANDBOX="$(mktemp -d "${TMPDIR:-/tmp}/fully-completely-smoke-branch.XXXXXX")"
mkdir -p "$BRANCH_SANDBOX/scripts" "$BRANCH_SANDBOX/templates"
cp "$REPO_ROOT/scripts/sprint_lifecycle.py" "$BRANCH_SANDBOX/scripts/sprint_lifecycle.py"
if [ -f "$REPO_ROOT/templates/sprint-template.md" ]; then
  cp "$REPO_ROOT/templates/sprint-template.md" "$BRANCH_SANDBOX/templates/sprint-template.md"
fi
BRANCH_SCRIPT="python3 $BRANCH_SANDBOX/scripts/sprint_lifecycle.py"

# Case (c): not a git repository at all -- before `git init` ever runs.
NOREPO_OUT=$(cd "$BRANCH_SANDBOX" && $BRANCH_SCRIPT list 2>&1)
echo "$NOREPO_OUT" | grep -qF "(not a git repository)" || \
  fail "tree_description should say plainly this isn't a git repository before git init -- got: $NOREPO_OUT"
echo "$NOREPO_OUT" | grep -q "branch unknown" && \
  fail "a genuinely non-repository directory must not be reported as merely 'branch unknown' -- Req 2 requires the specific case"

# Case (b): a real, unborn branch -- git init has run, but there is not
# yet a single commit. This is Finding F2's own exact repro: the OLD
# implementation printed "(branch unknown)" here, identical to the
# not-a-repository case above, which is exactly the conflation Req 2
# exists to fix.
(cd "$BRANCH_SANDBOX" && git init -q && git config user.email smoke@example.com && git config user.name "Smoke Test")
UNBORN_OUT=$(cd "$BRANCH_SANDBOX" && $BRANCH_SCRIPT list 2>&1)
echo "$UNBORN_OUT" | grep -qE "\(branch: [^,]+, no commits yet\)" || \
  fail "tree_description should name the branch AND say it has no commits yet on a real unborn branch -- got: $UNBORN_OUT"
echo "$UNBORN_OUT" | grep -q "not a git repository" && \
  fail "an unborn branch must not still be reported as 'not a git repository' -- it is one, just without commits"
echo "$UNBORN_OUT" | grep -q "branch unknown" && \
  fail "an unborn branch must not fall back to 'branch unknown' -- its name IS determinable via git symbolic-ref"

# Case (a): a named branch with at least one commit -- unchanged wording
# from before this sprint.
(cd "$BRANCH_SANDBOX" && echo x > f.txt && git add f.txt && git commit -q -m "first commit")
NAMED_OUT=$(cd "$BRANCH_SANDBOX" && $BRANCH_SCRIPT list 2>&1)
echo "$NAMED_OUT" | grep -qE '\(branch: [^,)]+\)\.' || \
  fail "tree_description should print the plain, unchanged (branch: <name>) form once a commit exists -- got: $NAMED_OUT"
echo "$NAMED_OUT" | grep -q "no commits yet" && \
  fail "a branch with a real commit must not still say 'no commits yet'"

# Case (d): detached HEAD -- a real, determinable-elsewhere state that
# must still fall through to the same undeterminable wording as before,
# never a fabricated branch name.
(cd "$BRANCH_SANDBOX" && echo y > g.txt && git add g.txt && git commit -q -m "second commit")
FIRST_COMMIT=$(cd "$BRANCH_SANDBOX" && git rev-parse HEAD~1)
(cd "$BRANCH_SANDBOX" && git checkout -q "$FIRST_COMMIT")
DETACHED_OUT=$(cd "$BRANCH_SANDBOX" && $BRANCH_SCRIPT list 2>&1)
echo "$DETACHED_OUT" | grep -q "branch unknown" || \
  fail "a detached HEAD should fall through to the existing 'branch unknown' wording -- got: $DETACHED_OUT"
echo "$DETACHED_OUT" | grep -qE '\(branch: [^,)]+\)\.' && \
  fail "a detached HEAD must never be reported as a real named branch"

echo "== sprint 38, Req 2 (QA1 round 1 finding): a real repository git refuses for an UNRELATED reason (dubious ownership) is never reported as 'not a git repository' =="
# git's own test hook for its safe.directory ownership check
# (GIT_TEST_ASSUME_DIFFERENT_OWNER=1) makes `git rev-parse
# --is-inside-work-tree` fail with "fatal: detected dubious ownership in
# repository at ..." against a real, ordinary repository -- exactly what
# happens on Windows whenever a checkout is owned by a different OS user
# (elevated creation, a VM shared folder), one of this sprint's own two
# workshop platforms.
DUBIOUS_OUT=$(cd "$BRANCH_SANDBOX" && GIT_TEST_ASSUME_DIFFERENT_OWNER=1 $BRANCH_SCRIPT list 2>&1)
echo "$DUBIOUS_OUT" | grep -q "not a git repository" && \
  fail "a real repository refused for dubious ownership must NOT be reported as 'not a git repository' -- got: $DUBIOUS_OUT"
echo "$DUBIOUS_OUT" | grep -q "branch unknown" || \
  fail "a dubious-ownership refusal should fall through to the undeterminable 'branch unknown' wording -- got: $DUBIOUS_OUT"

rm -rf "$BRANCH_SANDBOX"

echo "== sprint 40, Req 1a/1d: a correction on a COMPLETE sprint appends exactly one history event and changes nothing else -- diffed against the full state dict, phase included =="
SPRINT_CORRECT=$(new_sprint "Correction sprint")
$SCRIPT start "$SPRINT_CORRECT" > /dev/null
git commit -q --allow-empty -m "sprint $SPRINT_CORRECT work"
CLAUDE_CODE_AGENT="qa1" $SCRIPT qa1 "$SPRINT_CORRECT" --verdict PASS --notes "looked good" > /dev/null
CLAUDE_CODE_AGENT="dev-team-1" $SCRIPT dev-done "$SPRINT_CORRECT" > /dev/null
CORRECT_COMMIT=$(git rev-parse HEAD)
PATH="$FAKE_GH_DIR:$PATH" FAKE_GH_MODE=green $SCRIPT ship "$SPRINT_CORRECT" --commit "$CORRECT_COMMIT" > /dev/null
CLAUDE_CODE_AGENT="liveqa" $SCRIPT liveqa "$SPRINT_CORRECT" --deployed-commit "$CORRECT_COMMIT" --verdict PASS --notes "verified live, on branch release" > /dev/null
printf 'close it' > /tmp/correct_user_said.txt
CLAUDE_CODE_AGENT="dev-team-1" $SCRIPT complete "$SPRINT_CORRECT" --user-said-file /tmp/correct_user_said.txt > /dev/null
rm -f /tmp/correct_user_said.txt

CORRECT_STATE_BEFORE=$(cat "docs/sprints/state/sprint-${SPRINT_CORRECT}.json")
LIVEQA_EVENT_INDEX=$(python3 -c "
import json
s = json.loads('''$CORRECT_STATE_BEFORE''')
for i, h in enumerate(s['history']):
    if h['event'] == 'live_test' and h['actor'] == 'liveqa':
        print(i)
        break
")
[ -n "$LIVEQA_EVENT_INDEX" ] || fail "could not find the liveqa verdict event to correct -- history: $CORRECT_STATE_BEFORE"

CORRECT_OUT=$(CLAUDE_CODE_AGENT="liveqa" $SCRIPT correct "$SPRINT_CORRECT" --event-index "$LIVEQA_EVENT_INDEX" --correction "the branch name in my notes was wrong -- it was main, not release" 2>&1)
echo "$CORRECT_OUT" | grep -q "correction recorded against event #${LIVEQA_EVENT_INDEX}" || \
  fail "correct's success message is missing or wrong -- output: $CORRECT_OUT"
echo "$CORRECT_OUT" | grep -qi "not a new verdict" || fail "correct's own output doesn't say this isn't a new verdict (Req 1e)"

python3 -c "
import json
before = json.loads('''$CORRECT_STATE_BEFORE''')
after = json.load(open('docs/sprints/state/sprint-${SPRINT_CORRECT}.json'))
# Nothing but history (and the always-stamped last_claim, sprint 25's own
# established per-write timestamp) may differ.
before_copy = dict(before); after_copy = dict(after)
del before_copy['history']; del after_copy['history']
del before_copy['last_claim']; del after_copy['last_claim']
assert before_copy == after_copy, f'a correction changed something other than history/last_claim -- before={before_copy} after={after_copy}'
assert len(after['history']) == len(before['history']) + 1, 'a correction must append EXACTLY one event'
assert after['history'][-1]['event'] == 'correction'
assert after['history'][-1]['actor'] == 'liveqa'
assert after['phase'] == 'complete', 'phase must be unchanged -- still complete'
assert after['groundtruth_result'] == 'PASS', 'the verdict itself must be unchanged'
"

echo "== sprint 40, Req 1d: the correction is visible against its target event in --verbose, and the summary shows a correction exists =="
CORRECT_STATUS=$($SCRIPT status "$SPRINT_CORRECT")
echo "$CORRECT_STATUS" | grep -q "Corrections on record: 1" || \
  fail "the non-verbose summary must show a correction exists -- output: $CORRECT_STATUS"
CORRECT_VERBOSE=$($SCRIPT status "$SPRINT_CORRECT" --verbose)
echo "$CORRECT_VERBOSE" | grep -q "\[${LIVEQA_EVENT_INDEX}\].*live_test" || \
  fail "verbose history should show the target event at its index -- output: $CORRECT_VERBOSE"
echo "$CORRECT_VERBOSE" | grep -q "CORRECTION.*liveqa.*branch name in my notes was wrong" || \
  fail "verbose history should show the correction, attached to its target -- output: $CORRECT_VERBOSE"
# It must appear immediately after the target line, not merely somewhere
# in the output -- Req 1d's own "attached to, or immediately after".
python3 -c "
lines = '''$CORRECT_VERBOSE'''.splitlines()
target_i = next(i for i, l in enumerate(lines) if l.strip().startswith('[${LIVEQA_EVENT_INDEX}]'))
assert 'CORRECTION' in lines[target_i + 1], f'the correction must appear on the line immediately after its target: {lines[target_i:target_i+2]}'
"

echo "== sprint 40, Req 1c: refuses a correction from a DIFFERENT actor than the one who recorded the target event, naming both =="
CORRECT_WRONG_ACTOR_OUT=$(CLAUDE_CODE_AGENT="dev-team-1" $SCRIPT correct "$SPRINT_CORRECT" --event-index "$LIVEQA_EVENT_INDEX" --correction "trying to correct liveqa's own note" 2>&1) && \
  fail "a different role's correction attempt should have been refused -- output: $CORRECT_WRONG_ACTOR_OUT" || true
echo "$CORRECT_WRONG_ACTOR_OUT" | grep -q "was recorded by 'liveqa', not 'dev-team-1'" || \
  fail "the actor-mismatch refusal doesn't name both actors -- output: $CORRECT_WRONG_ACTOR_OUT"

echo "== sprint 40, Req 1c: refuses a correction with no CLAUDE_CODE_AGENT set (unattributable) =="
CORRECT_UNKNOWN_OUT=$(env -u CLAUDE_CODE_AGENT $SCRIPT correct "$SPRINT_CORRECT" --event-index "$LIVEQA_EVENT_INDEX" --correction "no agent set" 2>&1) && \
  fail "an unattributable correction should have been refused -- output: $CORRECT_UNKNOWN_OUT" || true
echo "$CORRECT_UNKNOWN_OUT" | grep -q "CLAUDE_CODE_AGENT is not set" || \
  fail "the unknown-actor refusal message is missing -- output: $CORRECT_UNKNOWN_OUT"

echo "== sprint 40, Req 1b: refuses when the target event index does not exist =="
CORRECT_MISSING_OUT=$(CLAUDE_CODE_AGENT="liveqa" $SCRIPT correct "$SPRINT_CORRECT" --event-index 999 --correction "out of range" 2>&1) && \
  fail "an out-of-range event index should have been refused -- output: $CORRECT_MISSING_OUT" || true
echo "$CORRECT_MISSING_OUT" | grep -q "has no history event at index 999" || \
  fail "the missing-event refusal message is missing -- output: $CORRECT_MISSING_OUT"

echo "== sprint 40, Req 1b: refuses an empty correction =="
CORRECT_EMPTY_OUT=$(CLAUDE_CODE_AGENT="liveqa" $SCRIPT correct "$SPRINT_CORRECT" --event-index "$LIVEQA_EVENT_INDEX" --correction "" 2>&1) && \
  fail "an empty correction should have been refused -- output: $CORRECT_EMPTY_OUT" || true
echo "$CORRECT_EMPTY_OUT" | grep -q -- "--correction is required and must be non-empty" || \
  fail "the empty-correction refusal message is missing -- output: $CORRECT_EMPTY_OUT"

echo "== sprint 40, Req 1a: none of the refused correction attempts above actually appended anything =="
FINAL_CORRECT_STATE=$(cat "docs/sprints/state/sprint-${SPRINT_CORRECT}.json")
python3 -c "
import json
after_one_correction = json.loads('''$CORRECT_STATE_BEFORE''')
final = json.loads('''$FINAL_CORRECT_STATE''')
# CORRECT_STATE_BEFORE was captured before the ONE successful correction,
# so the real event count now should be exactly +1 relative to it, not
# +1 plus however many refused attempts ran afterward.
assert len(final['history']) == len(after_one_correction['history']) + 1, \
    f\"a refused correction attempt must never append anything -- expected {len(after_one_correction['history']) + 1} events, got {len(final['history'])}\"
"

echo "== sprint 46, Req 3: a command that writes state while HEAD is detached prints a NOTE naming it, and still succeeds -- a warning, not a gate =="
SPRINT_DETACHED=$(new_sprint "Detached HEAD sprint")
DETACHED_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# `--detach` with NO target ref -- stays at the exact current commit,
# only changes whether HEAD is a branch pointer or a raw commit. This
# sandbox has been accumulating uncommitted sprint bookkeeping since its
# own baseline commit for the whole rest of this file (by design --
# sprint_lifecycle.py never commits anything itself), so checking out any
# OTHER specific commit here would conflict with that real, expected
# dirt; detaching in place needs to discard nothing.
git checkout -q --detach
# Plain, not `cmd && fail ... || true` -- this command is expected to
# SUCCEED (set -e's own default behaviour is the exit-code assertion
# here: if the NOTE below ever changed the exit code, this line would
# abort the whole suite instead of reaching the checks that follow).
DETACHED_OUT=$($SCRIPT start "$SPRINT_DETACHED" 2>&1)
echo "$DETACHED_OUT" | grep -q "NOTE: HEAD is currently detached" || \
  fail "expected the detached-HEAD NOTE to fire while state was written under a detached HEAD -- output: $DETACHED_OUT"
git checkout -q "$DETACHED_BRANCH"
DETACHED_PHASE=$(python3 -c "import json; print(json.load(open('docs/sprints/state/sprint-${SPRINT_DETACHED}.json'))['phase'])")
[ "$DETACHED_PHASE" = "dev_build" ] || \
  fail "expected phase dev_build after start even with the detached-HEAD NOTE firing -- got '$DETACHED_PHASE'"

echo "== sprint 46, Req 3: the identical command, HEAD NOT detached, prints no such NOTE =="
SPRINT_NOT_DETACHED=$(new_sprint "Not detached sprint")
NOT_DETACHED_OUT=$($SCRIPT start "$SPRINT_NOT_DETACHED" 2>&1)
echo "$NOT_DETACHED_OUT" | grep -q "NOTE: HEAD is currently detached" && \
  fail "the detached-HEAD NOTE must not fire when HEAD is on a real branch -- output: $NOT_DETACHED_OUT" || true

echo "== sprint 46, Req 3: a genuinely READ-ONLY command (status) never fires the NOTE even while detached, since it writes nothing =="
git checkout -q --detach
READONLY_DETACHED_OUT=$($SCRIPT status "$SPRINT_DETACHED" 2>&1)
echo "$READONLY_DETACHED_OUT" | grep -q "NOTE: HEAD is currently detached" && \
  fail "a read-only command must never fire the detached-HEAD NOTE -- it writes nothing, so there is nothing to warn about -- output: $READONLY_DETACHED_OUT" || true
git checkout -q "$DETACHED_BRANCH"

echo "== sprint 47, Req 5: verify-release-content.sh's own three behaviours, against a throwaway sandbox git repo (never this repo's own real history) =="

# A dedicated nested repo, separate from $SANDBOX's own sprint-lifecycle
# state -- this script needs a real commit with real tracked files to diff
# a "published tarball" against, and planting that inside this repo's own
# actual git history would be exactly the kind of destructive-sandbox
# mistake this file's own header (above) warns against.
VRC_REPO="$SANDBOX/.vrc-repo"
mkdir -p "$VRC_REPO/sub"
(
  cd "$VRC_REPO"
  git init -q
  git config user.email "vrc-test@example.com"
  git config user.name "VRC Test"
  echo "hello from file one" > file-one.txt
  echo "hello from file two" > sub/file-two.txt
  git add -A
  git commit -q -m "vrc baseline"
)
VRC_COMMIT="$(cd "$VRC_REPO" && git rev-parse HEAD)"

# A fake `npm`, controlled entirely by env vars set per invocation below --
# same shape as this file's own $FAKE_GH_DIR precedent (withFakeClaude /
# fake gh): a real executable prepended to PATH only for the specific
# command line under test, never left in place for anything else in this
# file. Keeps this suite offline and deterministic; the real script has
# already been verified live against the real npm registry (sprint 47
# build notes) -- this is the permanent regression coverage for its own
# logic, not a substitute for that live verification.
FAKE_NPM_DIR="$SANDBOX/.fake-npm"
mkdir -p "$FAKE_NPM_DIR"
cat > "$FAKE_NPM_DIR/npm" <<'FAKENPM'
#!/usr/bin/env bash
case "$1" in
  view)
    field="$3"
    case "$field" in
      gitHead)
        if [ -n "${FAKE_NPM_GITHEAD:-}" ]; then echo "$FAKE_NPM_GITHEAD"; fi
        # absent -> print nothing, matching real npm's own behaviour for a
        # version published with no gitHead recorded (sprint 46's 0.2.22)
        ;;
      dist.shasum)
        echo "${FAKE_NPM_SHASUM:?FAKE_NPM_SHASUM must be set by the test}"
        ;;
      *)
        exit 1
        ;;
    esac
    ;;
  pack)
    : "${FAKE_NPM_TARBALL:?FAKE_NPM_TARBALL must be set by the test}"
    cp "$FAKE_NPM_TARBALL" "./$(basename "$FAKE_NPM_TARBALL")"
    echo "$(basename "$FAKE_NPM_TARBALL")"
    ;;
  *)
    exit 1
    ;;
esac
FAKENPM
chmod +x "$FAKE_NPM_DIR/npm"

# A "published tarball" whose package/ directory mirrors the commit's own
# tracked files exactly -- the matching case.
VRC_MATCH_STAGE="$SANDBOX/.vrc-match-stage"
mkdir -p "$VRC_MATCH_STAGE/package/sub"
cp "$VRC_REPO/file-one.txt" "$VRC_MATCH_STAGE/package/"
cp "$VRC_REPO/sub/file-two.txt" "$VRC_MATCH_STAGE/package/sub/"
VRC_MATCH_TARBALL="$SANDBOX/.vrc-match.tgz"
tar -czf "$VRC_MATCH_TARBALL" -C "$VRC_MATCH_STAGE" package
VRC_MATCH_SHASUM="$(shasum "$VRC_MATCH_TARBALL" | awk '{print $1}')"

echo "== Req 5: a matching release passes, gitHead present and matching =="
VRC_OUT_MATCH=$(
  PATH="$FAKE_NPM_DIR:$PATH" \
  VERIFY_RELEASE_CONTENT_REPO_ROOT="$VRC_REPO" \
  FAKE_NPM_TARBALL="$VRC_MATCH_TARBALL" \
  FAKE_NPM_SHASUM="$VRC_MATCH_SHASUM" \
  FAKE_NPM_GITHEAD="$VRC_COMMIT" \
  bash "$REPO_ROOT/scripts/verify-release-content.sh" fake-pkg 1.0.0 "$VRC_COMMIT" 2>&1
) || fail "expected the matching-content run to PASS -- output: $VRC_OUT_MATCH"
echo "$VRC_OUT_MATCH" | grep -q "RELEASE CONTENT VERIFICATION PASSED" || \
  fail "matching run did not report PASSED -- output: $VRC_OUT_MATCH"
echo "$VRC_OUT_MATCH" | grep -q "present and matches: $VRC_COMMIT" || \
  fail "matching run's gitHead line did not report a match -- output: $VRC_OUT_MATCH"
echo "$VRC_OUT_MATCH" | grep -q "^MATCHED: 2$" || \
  fail "matching run should report both files matched -- output: $VRC_OUT_MATCH"
echo "$VRC_OUT_MATCH" | grep -q "^MISMATCHED: 0$" || \
  fail "matching run should report zero mismatches -- output: $VRC_OUT_MATCH"

echo "== Req 5 / Req 1a: a missing gitHead is reported plainly but does NOT fail the content check =="
VRC_OUT_NOHEAD=$(
  PATH="$FAKE_NPM_DIR:$PATH" \
  VERIFY_RELEASE_CONTENT_REPO_ROOT="$VRC_REPO" \
  FAKE_NPM_TARBALL="$VRC_MATCH_TARBALL" \
  FAKE_NPM_SHASUM="$VRC_MATCH_SHASUM" \
  bash "$REPO_ROOT/scripts/verify-release-content.sh" fake-pkg 1.0.0 "$VRC_COMMIT" 2>&1
) || fail "a missing gitHead alone must never fail this script -- output: $VRC_OUT_NOHEAD"
echo "$VRC_OUT_NOHEAD" | grep -q "ABSENT" || \
  fail "expected the ABSENT gitHead line -- output: $VRC_OUT_NOHEAD"
echo "$VRC_OUT_NOHEAD" | grep -q "RELEASE CONTENT VERIFICATION PASSED" || \
  fail "content comparison must still pass with gitHead absent -- output: $VRC_OUT_NOHEAD"

# A planted mismatch: one file's content in the "published tarball" now
# differs from what the commit actually has at that path -- the exact
# shape sprint 47's own build-time negative control exercised live
# against a real registry (fully-completely 0.2.23 vs. a deliberately
# wrong commit), now made a permanent regression test.
VRC_MISMATCH_STAGE="$SANDBOX/.vrc-mismatch-stage"
mkdir -p "$VRC_MISMATCH_STAGE/package/sub"
cp "$VRC_REPO/file-one.txt" "$VRC_MISMATCH_STAGE/package/"
echo "this content was never committed" > "$VRC_MISMATCH_STAGE/package/sub/file-two.txt"
VRC_MISMATCH_TARBALL="$SANDBOX/.vrc-mismatch.tgz"
tar -czf "$VRC_MISMATCH_TARBALL" -C "$VRC_MISMATCH_STAGE" package
VRC_MISMATCH_SHASUM="$(shasum "$VRC_MISMATCH_TARBALL" | awk '{print $1}')"

echo "== Req 5: a planted content mismatch fails, real (non-piped) exit code 1, names the mismatched file =="
set +e
VRC_OUT_MISMATCH=$(
  PATH="$FAKE_NPM_DIR:$PATH" \
  VERIFY_RELEASE_CONTENT_REPO_ROOT="$VRC_REPO" \
  FAKE_NPM_TARBALL="$VRC_MISMATCH_TARBALL" \
  FAKE_NPM_SHASUM="$VRC_MISMATCH_SHASUM" \
  FAKE_NPM_GITHEAD="$VRC_COMMIT" \
  bash "$REPO_ROOT/scripts/verify-release-content.sh" fake-pkg 1.0.0 "$VRC_COMMIT" 2>&1
)
VRC_MISMATCH_EXIT=$?
set -e
[ "$VRC_MISMATCH_EXIT" -eq 1 ] || \
  fail "expected exit code 1 on a real content mismatch, got $VRC_MISMATCH_EXIT -- output: $VRC_OUT_MISMATCH"
echo "$VRC_OUT_MISMATCH" | grep -q "RELEASE CONTENT VERIFICATION FAILED" || \
  fail "mismatch run did not report FAILED -- output: $VRC_OUT_MISMATCH"
echo "$VRC_OUT_MISMATCH" | grep -q "MISMATCHED: sub/file-two.txt" || \
  fail "mismatch run did not name the actual mismatched file -- output: $VRC_OUT_MISMATCH"
echo "$VRC_OUT_MISMATCH" | grep -q "^MISMATCHED: 1$" || \
  fail "mismatch run should report exactly one mismatch -- output: $VRC_OUT_MISMATCH"

# Req 5's other named test: no shipped file references a rule by a sprint
# number whose content no longer matches. This is NOT automated as a
# content-diff check here -- there is no mechanical way to know, from a
# sprint number alone, what content it "should" still match without
# re-deriving the same judgment call Req 2b's own sweep already made by
# hand (is this a navigational pointer or a historical attribution?).
# What IS automated, permanently, below: no shipped file names the one
# rule this sprint knows went stale (sprint 46's worktree-publish
# technique) by its old sprint-number form ever again -- a regression
# test for the exact defect found, not a general staleness prover.
echo "== sprint 47, Req 5: no shipped file regresses to the old, now-stale 'sprint 46' pointer form for the publish-isolation rule =="
VRC_STALE_GREP=$(grep -rn "worktree-publish rule, sprint 46" \
  --include="*.js" --include="*.py" --include="*.md" \
  "$REPO_ROOT/.claude" "$REPO_ROOT/scripts" "$REPO_ROOT/CLAUDE.md" 2>/dev/null || true)
[ -z "$VRC_STALE_GREP" ] || \
  fail "found a regression to the old stale 'worktree-publish rule, sprint 46' pointer -- $VRC_STALE_GREP"

echo "ALL SMOKE TESTS PASSED"
