#!/usr/bin/env bash
# Manual real-provider reproduction driver. Run from a checkout with an isolated
# T3 dev server already running (T3CODE_DEV_SEED=0 vp run dev).
# Start fault-gateway.cjs beside this script; it uses existing Claude gateway config.
# Usage: bash driver.sh seed | start | fail THREAD | register THREAD BASH_ID MONITOR_ID | release
set -eu
EVIDENCE_ROOT=$(cd -- "$(dirname -- "$0")" && pwd)
workspace="$EVIDENCE_ROOT/workspace"
cli() { node apps/server/src/bin.ts "$@" --base-dir .t3; }
case "$1" in
seed)
 mkdir -p "$workspace/.claude"
 cat > "$workspace/.claude/settings.local.json" <<'JSON'
{"env":{"ANTHROPIC_BASE_URL":"http://127.0.0.1:19087","CLAUDE_CODE_MAX_RETRIES":"1"},"permissions":{"allow":["Bash(*)","Monitor(*)"]}}
JSON
 echo 'Synthetic background task survival reproduction.' > "$workspace/README.md"
 git init -q "$workspace"
 git -C "$workspace" add .
 git -C "$workspace" -c user.name=Evidence -c user.email=evidence@example.invalid commit -qm 'Synthetic workspace'
 cli project add "$workspace" --title 'Background task survival'
 ;;
start)
 cli thread start "$workspace" --provider claudeAgent --model haiku --title 'Background tasks across gateway failure' 'Start a Bash command in the background that waits until a file named release exists in this project, then prints TASK COMPLETE. Also start a Monitor that checks for release every 15 seconds, emits progress while waiting, and stops when release exists. Register both native task IDs using t3_background_wait, then end your turn immediately, leaving both tasks alive. Do not create release yourself. Report the two IDs.' --json
 ;;
fail)
 touch "$EVIDENCE_ROOT/fault"
 cli thread send "$2" 'Please report whether both background tasks are still running. Do not stop them.' --wait --json
 ;;
register)
 rm -f "$EVIDENCE_ROOT/fault"
 cli thread send "$2" "Call t3_background_wait again with the same task IDs $3 and $4. Report the exact matched and unmatched result without any filesystem paths. Do not stop tasks or create release." --wait --json
 ;;
release) touch "$workspace/release" ;;
esac
