#!/bin/bash
# Usage: drive.sh — runs one isolated opencode serve against fake_model.py and prints the session event timeline.
set -u
B=/tmp/issue-lead-26674-r1/repro; R=$B/run-$(date +%H%M%S); mkdir -p $R/proj $R/xdg/{data,config,cache,state}
python3 $B/fake_model.py 47811 $R/model.log > $R/fake.out 2>&1 & FAKE=$!
cd $R/proj && git init -q . && echo hi > README.md
XDG_DATA_HOME=$R/xdg/data XDG_CONFIG_HOME=$R/xdg/config XDG_CACHE_HOME=$R/xdg/cache XDG_STATE_HOME=$R/xdg/state OPENCODE_CONFIG=$B/opencode.json opencode serve --hostname 127.0.0.1 --port 47812 > $R/serve.log 2>&1 & SERVE=$!
for i in $(seq 1 30); do curl -sf http://127.0.0.1:47812/config > /dev/null && break; sleep 0.5; done
curl -sN http://127.0.0.1:47812/event > $R/events.sse & EV=$!
sleep 1
S=$(curl -s -X POST http://127.0.0.1:47812/session -H 'content-type: application/json' -d '{}' | python3 -c 'import json,sys;print(json.load(sys.stdin)["id"])')
echo "session=$S opencode=$(opencode --version)"
curl -s -X POST http://127.0.0.1:47812/session/$S/prompt_async -H 'content-type: application/json' -d '{"model":{"providerID":"fake","modelID":"fake"},"parts":[{"type":"text","text":"Reply briefly."}]}' -o /dev/null -w 'prompt_async http=%{http_code}\n'
sleep ${WAIT:-30}
kill $EV $SERVE $FAKE
echo "--- fake model requests"; cat $R/model.log
echo "--- opencode events (deltas and plugin noise omitted)"
python3 $B/timeline.py $R/events.sse
echo "run dir: $R"
