#!/usr/bin/env bash
set -euo pipefail
# Portable transcript of the CLI seeding and exact-child termination used for the captures.
# Run from an installed checkout. The dev server must already use this fresh T3 home.
: "${PROOF_HOME:?Set the isolated T3 home}"
: "${PROOF_MODEL:?Set a supported Claude model slug (no inference request is made)}"
: "${PROOF_WORKSPACE:?Set the synthetic workspace path}"
: "${PROOF_FIXTURE_DIR:?Set the directory containing claude-compatible-fixture.cjs}"
mode=${1:?signal or exit7}
mkdir -p "$PROOF_WORKSPACE"
git -C "$PROOF_WORKSPACE" init -q
git -C "$PROOF_WORKSPACE" -c user.name=Proof -c user.email=proof@example.invalid commit --allow-empty -qm 'Synthetic workspace'
node apps/server/src/bin.ts project add "$PROOF_WORKSPACE" --title 'Process exit proof' --base-dir "$PROOF_HOME"
if [ "$mode" = signal ]; then
  prompt='PROOF_SIGNAL: Write a long response.'; title='Signal termination'
else
  prompt='PROOF_EXIT7: Write a long response.'; title='Exit code 7 verification'
fi
node apps/server/src/bin.ts thread start "$PROOF_WORKSPACE" "$prompt" --title "$title" --provider claudeAgent --model "$PROOF_MODEL" --base-dir "$PROOF_HOME" --json
# Open the returned thread in the real web client, wait for partial output, and start recording.
read -r -p 'Press Enter after partial output is visible and recording has started: ' _
python3 - "$PROOF_FIXTURE_DIR/active-child.json" "$PROOF_WORKSPACE" "$mode" <<'VERIFY'
import json,os,signal,sys
p=json.load(open(sys.argv[1]))
assert p['cwd']==os.path.realpath(sys.argv[2])
assert os.readlink('/proc/%s/cwd'%p['pid'])==p['cwd']
assert p['mode']==sys.argv[3]
os.kill(p['pid'],signal.SIGABRT if p['mode']=='signal' else signal.SIGUSR1)
VERIFY
# Inspect the persisted failed turn; stop recording. The exit7 handler calls process.exit(7).
