#!/usr/bin/env bash
# Manual real-provider proof driver. Run in each evidence checkout after vp i.
# Supply GOOGLE_GEMINI_BASE_URL for the real API hub; no provider responses are mocked.
# Start: T3CODE_DEV_SEED=0 vp run dev
# Browser: pair using the printed URL; Settings > Providers > Add provider > Antigravity.
# Use instance ID antigravity, Gemini API key, and any non-empty key.
# Browser viewport: 1280x800, light theme.
set -eu
phase=$1
workspace=$2
thread=${3:-}
case "$phase" in
 seed)
  mkdir -p "$workspace"
  printf 'alpha\nbeta\ngamma\n' > "$workspace/sample.txt"
  printf 'This is a synthetic subagent lifecycle test. Follow the user exactly. Use your native invoke_subagent and send_message tools, not MCP delegation. Do not inspect other directories.\n' > "$workspace/AGENTS.md"
  node apps/server/src/bin.ts project add --base-dir .t3 --title 'Subagent recovery proof' "$workspace"
  ;;
 launch)
  node apps/server/src/bin.ts thread start --base-dir .t3 --provider antigravity --model gemini-4-neon --title 'Subagent restart proof' "$workspace" 'Use invoke_subagent to start exactly two background self subagents. Give each this task: run sleep 120, then count the lines in sample.txt and report the count. Do not wait for them. Once both are launched, tell me their conversationIds and return immediately. Use native Antigravity tools, not MCP delegation.' --json
  ;;
 restart)
  # Run only once both child cards show running. Capture the server PID from its runtime file.
  node -e 'const fs=require("fs");const r=JSON.parse(fs.readFileSync(".t3/userdata/server-runtime.json"));process.kill(r.pid,"SIGTERM")'
  # After shutdown reconciliation, touch the entrypoint to wake the dev watch launcher.
  read -r -p 'After shutdown reconciliation completes, press Enter: ' _
  touch apps/server/src/bin.ts
  ;;
 revive)
  node apps/server/src/bin.ts thread send --base-dir .t3 "$thread" 'Revive both existing children now using native send_message addressed to each child conversationId. Tell each to run sleep 120 again, then count sample.txt and report again. Do not launch new children. After sending both messages, use manage_subagents list and report their status without waiting for completion.' --json
  ;;
 inspect)
  sqlite3 -readonly .t3/userdata/statev2.sqlite 'select status,run_id from orchestration_v2_projection_subagents;'
  ;;
esac
