#!/usr/bin/env bash
# Issue #24914: a pending async question shows no indicator while the agent keeps working.
# Claim: already fixed by #28515 (955e2bfbb5). Any pending user_input now sets hasPendingUserInput,
# blocking or not, so the thread leaves Working and shows Input in the sidebar, status pill and mobile list.
# Shows: before #28515 a running thread with a non-blocking question presents as Working with no pending
# input; on main the same thread presents with pending input and is not Working.
# Needs deps installed (`vp i`). Run from the root of a clean t3code checkout.
set -euo pipefail

ok=1
pass() { echo "PASS  $*"; }
fail() { echo "FAIL  $*"; ok=0; }

if [ -n "$(git status --porcelain)" ]; then
  echo "refusing to run on a dirty tree"
  exit 2
fi
ORIG=$(git symbolic-ref -q --short HEAD || git rev-parse HEAD)
TEST=packages/client-runtime/src/state/prove-issue-24914.test.ts
LOG=$(mktemp)
cleanup() {
  rm -f "$TEST" "$LOG"
  git checkout -q -f "$ORIG"
}
trap 'rc=$?; cleanup; [ "${PROOF_DONE:-0}" = 1 ] && exit 0; [ "$rc" -ne 0 ] && exit "$rc"; exit 1' EXIT

git fetch -q origin main
MAIN=$(git rev-parse FETCH_HEAD)
FIX=955e2bfbb5c092c54ad795c8d379fec99702a6b5
BEFORE=$(git rev-parse "$FIX^")
echo "main=$MAIN fix=$FIX before=$BEFORE"

subject=$(git log -1 --format=%s "$FIX")
if git merge-base --is-ancestor "$FIX" "$MAIN" \
  && [ "$subject" = "fix(clients): questions always move a thread out of Working (#28515)" ]; then
  pass "#28515 ($(git rev-parse --short "$FIX")) is on main: $subject"
else
  fail "#28515 is not on main, or $FIX is a different commit ($subject)"
fi

# The three surfaces the issue names read hasPendingUserInput to show Input.
sidebar=$(git show "$MAIN:apps/web/src/components/Sidebar.logic.ts" | grep -c 'hasPendingUserInput' || true)
mobile=$(git show "$MAIN:apps/mobile/src/features/threads/threadListV2.ts" | grep -c 'hasPendingUserInput' || true)
if [ "$sidebar" -gt 0 ] && [ "$mobile" -gt 0 ]; then
  pass "main: the sidebar/status pill (Sidebar.logic.ts) and the mobile list (threadListV2.ts) key Input off hasPendingUserInput"
else
  fail "main: the sidebar or mobile list no longer reads hasPendingUserInput"
fi

write_test() {
  cat > "$TEST" <<'EOF'
// prove-issue-24914: a running Codex-style thread with an unanswered async (non-blocking) question.
import { EnvironmentId, RunId, RuntimeRequestId } from "@t3tools/contracts";
import { expect, it } from "vite-plus/test";
import { presentThreadShell } from "./models.ts";
import { v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
import { isThreadWorking } from "./threadInbox.ts";

it("prove-issue-24914", () => {
  const runId = RunId.make("run-async-question");
  const shell = presentThreadShell(EnvironmentId.make("environment-v2"), {
    ...v2ThreadShell,
    latestRunId: runId,
    activeRunId: runId,
    status: "running" as const,
    pendingRuntimeRequest: {
      id: RuntimeRequestId.make("request-async-question"),
      kind: "user_input" as const,
      createdAt: v2ThreadShell.updatedAt,
      blocking: false,
    },
  });
  const bug = process.env.PROOF_EXPECT === "bug";
  expect(shell.hasPendingUserInput).toBe(!bug);
  expect(isThreadWorking(shell)).toBe(bug);
});
EOF
}

run_at() {
  git checkout -q --detach "$1"
  write_test
  local rc=0
  PROOF_EXPECT="$2" vp test run "$TEST" > "$LOG" 2>&1 || rc=$?
  rm -f "$TEST"
  if [ "$rc" -eq 0 ] && ! grep -Eq 'Tests +1 passed' "$LOG"; then rc=3; fi
  if [ "$rc" -ne 0 ]; then sed 's#'"$HOME"'#~#g' "$LOG" | tail -25; fi
  return "$rc"
}

if run_at "$BEFORE" bug; then
  pass "before #28515 ($(git rev-parse --short "$BEFORE")): a running thread with an async question is Working with hasPendingUserInput=false (no indicator)"
else
  fail "before #28515: the async-question thread did not present as Working without pending input"
fi
if run_at "$MAIN" fixed; then
  pass "main ($(git rev-parse --short "$MAIN")): the same thread has hasPendingUserInput=true and is not Working, so it shows Input"
else
  fail "main: the async-question thread still hides its pending input"
fi

if [ "$ok" = 1 ]; then
  echo "VERDICT: #24914 is fixed on main by #28515"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: not shown"
exit 1
