#!/usr/bin/env bash
set -euo pipefail
# Run from each app checkout after vp i, against its own fresh T3 home.
# Supply a working OpenCode model privately; never publish its identifier.
: "${T3_HOME:?isolated app home}" "${DEMO_DIR:?synthetic git project}" "${OPENCODE_MODEL:?working model}"
# Start the app in a separate terminal: T3CODE_DEV_SEED=0 vp run dev
# Configure the isolated home's settings with OpenCode enabled.
mkdir -p "$DEMO_DIR"
git -C "$DEMO_DIR" init
node apps/server/src/bin.ts project add --base-dir "$T3_HOME" --title 'Fork recovery demo' "$DEMO_DIR"
node apps/server/src/bin.ts thread start --base-dir "$T3_HOME" --provider opencode --model "$OPENCODE_MODEL" --title 'BLUEBIRD source' --wait --json "$DEMO_DIR" 'Remember the codeword BLUEBIRD. Reply only: Remembered BLUEBIRD. Do not use tools.'
: "${SOURCE_THREAD:?use the returned threadId}"
node apps/server/src/bin.ts thread send --base-dir "$T3_HOME" --wait --json "$SOURCE_THREAD" 'Second turn: reply READY only; keep the codeword in mind. Do not use tools.'
NATIVE_ID=$(sqlite3 -readonly "$T3_HOME/userdata/statev2.sqlite" "SELECT json_extract(payload_json,'\$.nativeThreadRef.nativeId') FROM orchestration_v2_projection_provider_threads WHERE thread_id='$SOURCE_THREAD' AND json_extract(payload_json,'\$.nativeThreadRef.nativeId') IS NOT NULL LIMIT 1")
: "${OPENCODE_URL:?URL of this app's spawned OpenCode server}"
curl --fail -s -X DELETE -G --data-urlencode "directory=$DEMO_DIR" "$OPENCODE_URL/session/$NATIVE_ID"
# Confirm GET returns 404, while the T3 conversation remains readable.
curl -s -w '\n%{http_code}\n' -G --data-urlencode "directory=$DEMO_DIR" "$OPENCODE_URL/session/$NATIVE_ID"
# Browser interaction, identical on both commits, recorded at 1280x800, light theme:
# 1. Open SOURCE_THREAD, click the first reply's "Fork from this response".
# 2. Send: What is the codeword? Reply only with the codeword. Do not use tools.
# 3. Wait for the run to finish, then send:
#    Send two: repeat the codeword only. Do not use tools.
# 4. Wait for the run to finish, then send:
#    Send three: what codeword do you remember? Answer briefly. Do not use tools.
# 5. Capture the completed conversation at 1280x1200, light theme.
#    Expand the fallback notice on head; dismiss the error toast on base.
# Control: repeat the two source turns in a new thread, omit DELETE, then fork
# the first reply and send the same codeword question.
# Public capture redaction: hide only the model picker and machine label with:
# [aria-label*="<private model display name>"], [aria-label*="Run context:"]
# { visibility: hidden !important; }
# The conversation, failures and fallback notice are unmodified.
