#!/usr/bin/env bash
# Real-app repro for #48296: real OpenCode naming (slow model) + slow real git checkout. No injected code.
set -euo pipefail
checkout="$1"; model="${2:-openai/gpt-5.5-pro}"; per_file="${3:-3.6}"
scratch="/tmp/factory-issue-48296-triage/run-$(date +%s)"
mkdir -p "$scratch/state/userdata"
export GIT_CONFIG_NOSYSTEM=1 GIT_TRACE2_EVENT="$scratch/git.jsonl" T3CODE_DEV_SEED=0
repo="$scratch/repo"
git init -q -b main "$repo"
git -C "$repo" config checkout.workers 1
printf '*.fixture filter=repro\n' > "$repo/.gitattributes"
for i in {1..8}; do printf 'Synthetic fixture %s\n' "$i" > "$repo/$i.fixture"; done
git -C "$repo" add .
git -C "$repo" -c user.name=Fixture -c user.email=fixture@example.invalid commit -qm fixture
cat > "$scratch/smudge.py" <<PY
import hashlib, sys, time
data = sys.stdin.buffer.read()
deadline = time.process_time() + $per_file
while time.process_time() < deadline:
    hashlib.pbkdf2_hmac("sha256", data, b"fixture", 100_000)
sys.stdout.buffer.write(data)
PY
git -C "$repo" config filter.repro.smudge "python3 '$scratch/smudge.py'"
git -C "$repo" config filter.repro.clean cat
git -C "$repo" config filter.repro.required true
cat > "$scratch/state/userdata/settings.json" <<JSON
{
  "defaultModelSelection": {"instanceId":"devReplay","model":"showcase"},
  "textGenerationModelSelection": {"instanceId":"opencode","model":"$model"},
  "providerInstances": {
    "codex": {"driver":"codex","enabled":false},
    "claudeAgent": {"driver":"claudeAgent","enabled":false},
    "cursor": {"driver":"cursor","enabled":false},
    "muse": {"driver":"muse","enabled":false},
    "pi": {"driver":"pi","enabled":false},
    "antigravity": {"driver":"antigravity","enabled":false},
    "opencode": {"driver":"opencode","enabled":true}
  }
}
JSON
port=$(node -e 'const s=require("node:net").createServer();s.listen(0,"127.0.0.1",()=>{console.log(s.address().port);s.close()})')
cli() { node "$checkout/apps/server/src/bin.ts" "$@" --base-dir "$scratch/state"; }
node "$checkout/apps/server/src/bin.ts" serve --host 127.0.0.1 --port "$port" --dev-url "http://127.0.0.1:$port" \
  --base-dir "$scratch/state" > "$scratch/server.log" 2>&1 &
server=$!
trap 'kill $server 2>/dev/null; wait $server 2>/dev/null; echo "scratch $scratch"' EXIT
for ((i=0;i<120;i++)); do curl -fs "http://127.0.0.1:$port/.well-known/t3/environment" >/dev/null && break; sleep 0.5; done
cli project add "$repo" >/dev/null
start=$SECONDS
launch=$(cli thread start "$repo" "Inspect the fixture files and summarize them" --worktree main --wait --json)
thread=$(node -pe 'JSON.parse(process.argv[1]).threadId' "$launch")
status=$(cli thread show "$thread" --messages 0 --json)
worktree=$(node -pe 'JSON.parse(process.argv[1]).thread.worktreePath' "$status")
echo "elapsed $((SECONDS-start))s branch $(git -C "$worktree" branch --show-current)"
node --input-type=module -e '
  import { DatabaseSync } from "node:sqlite";
  const db = new DatabaseSync(process.argv[1], { readOnly: true });
  for (const {payload_json} of db.prepare("SELECT payload_json FROM orchestration_v2_projection_turn_items WHERE thread_id = ? AND type = ?").all(process.argv[2], "system_notice")) {
    const {title,message} = JSON.parse(payload_json); console.log("notice:", title, "—", message);
  }' "$scratch/state/userdata/statev2.sqlite" "$thread"
grep -o '"event":"exit"[^}]*"t_abs":[0-9.]*' "$scratch/git.jsonl" | head -0 || true
node -e '
  const lines=require("fs").readFileSync(process.argv[1],"utf8").trim().split("\n").map(JSON.parse);
  const starts=new Map(lines.filter(l=>l.event==="start").map(l=>[l.sid,l]));
  for (const l of lines) if (l.event==="exit") { const s=starts.get(l.sid); if (s && s.argv.includes("worktree")) console.log("git", s.argv.slice(1,4).join(" "), "code", l.code, l.t_abs.toFixed(1)+"s"); }
' "$scratch/git.jsonl"
grep -i "branch\|textgen\|opencode" "$scratch/server.log" | tail -5 || true
