#!/usr/bin/env bash
# Scratch driver for issue 38376: thread on A, long run of work on B, back to A.
set -euo pipefail
cd /home/user/wt/factory-issue-38376-triage/apps/server
D() { node src/zz-repro-driver.ts /home/user/wt/factory-issue-38376-triage/.t3 "$@" 2>&1; }
P="mcp:e78714b8-008d-4c79-b70a-43601ccbdae5"
send() { # thread message
  local msg
  msg=$(python3 -c 'import json,sys; print(json.dumps(sys.argv[1]))' "$2")
  D t3_thread_send "{\"threadId\":\"$1\",\"message\":$msg}" | grep '"runId"' || true
  D t3_thread_wait "{\"threadId\":\"$1\",\"timeoutMs\":240000}" | grep '"status"' || true
}
config() { D t3_thread_configure "{\"threadId\":\"$1\",\"modelSelection\":{\"instanceId\":\"$2\",\"model\":\"claude-haiku-5-5\"}}" | grep '"isError"'; }

T=$(D t3_thread_launch "{\"projectId\":\"$P\",\"title\":\"stale resume repro 2\",\"message\":\"We are tracking a deploy target. The deploy target is the staging cluster eu-west-ALPHA. Do not save anything to memory. Reply only with: target ALPHA.\",\"modelSelection\":{\"instanceId\":\"claude_a\",\"model\":\"claude-haiku-5-5\"}}" | python3 -c 'import sys,json; print(json.load(sys.stdin)["structuredContent"]["threadId"])')
echo "thread $T"
D t3_thread_wait "{\"threadId\":\"$T\",\"timeoutMs\":240000}" | grep '"status"'
config "$T" claude_b
send "$T" "Reply only with: ok."
send "$T" "Change of plan: eu-west-ALPHA was decommissioned. The deploy target is now us-east-BRAVO. Reply only with: target BRAVO."
for topic in herons cormorants albatrosses puffins kestrels; do
  send "$T" "Write about 900 words on the natural history of $topic. Do not mention deploy targets. Do not use tools."
done
config "$T" claude_a
send "$T" "Which cluster do we deploy to? Answer with just the cluster name, from what you already know, without reading anything."
echo "$T" > /tmp/factory-issue-38376-triage/thread2.txt
