#!/usr/bin/env bash
set -euo pipefail
# Run from each commit's checkout after vp i. Use a fresh worktree-local T3 home.
# Set DEMO_WORKSPACE to a disposable git repository, DEVICE_SESSION to the leased
# Pixel 9 session, and DEVICE_HOST to this server's device-reachable host.
# AGENT_PROVIDER and AGENT_MODEL select the locally available agent runtime.
: "${DEMO_WORKSPACE:?}" "${DEVICE_SESSION:?}" "${DEVICE_HOST:?}"
: "${AGENT_PROVIDER:?}" "${AGENT_MODEL:?}" "${SERVER_PORT:?}" "${METRO_PORT:?}"
mkdir -p .t3/userdata
python3 - <<'PY'
import json, os
p='.t3/userdata/settings.json'
s=json.load(open(p)) if os.path.exists(p) else {}
s['linkedEnvironmentsEnabled']=True
s.setdefault('providerInstances',{})[os.environ['AGENT_PROVIDER']]={'driver':os.environ['AGENT_PROVIDER'],'enabled':True}
json.dump(s,open(p,'w'))
PY
# In separate terminals, keep these running while driving the device:
# T3CODE_DEV_SEED=0 vp run dev:server --host 0.0.0.0 --port "$SERVER_PORT"
# (cd apps/mobile && CI=1 EXPO_NO_METRO_LAZY=1 REACT_NATIVE_PACKAGER_HOSTNAME="$DEVICE_HOST" vp run dev:client --port "$METRO_PORT")
node apps/server/src/bin.ts project add "$DEMO_WORKSPACE" --title Demo --base-dir .t3
node apps/server/src/bin.ts thread start "$DEMO_WORKSPACE" \
 'Call t3_environment_link with hint "my vps" and reason "Connect another machine for this demo." Do not run any other actions. Do not mention your model or system details.' \
 --title 'Link a machine' --provider "$AGENT_PROVIDER" --model "$AGENT_MODEL" --base-dir .t3 --json
vp run mobile:install android --session "$DEVICE_SESSION" --metro "http://$DEVICE_HOST:$METRO_PORT"
.agents/skills/test-t3-mobile/scripts/pair-client.sh "$SERVER_PORT" "$PWD/.t3" "http://$DEVICE_HOST:$SERVER_PORT" agent-device --session "$DEVICE_SESSION"
# For the second side, clear the disposable app data first, then reopen its Metro URL
# and pair again, so only one environment is known on each side.
# Open Demo > Link a machine, tap Choose machine… then Another address….
# Expand by dragging the sheet header to its 0.9 detent.
# Record from the expanded sheet, then run identical content gestures:
# agent-device gesture fling up 540 1800 1000 --session "$DEVICE_SESSION" (three times)
# agent-device gesture pan 540 1800 0 -1000 1200 --session "$DEVICE_SESSION" (twice)
# Observe whether Full access, Link, and Cancel become reachable.
