#!/usr/bin/env bash
# PR #34238 "fix(server): Copilot discovers optional T3 tools on demand" (draft, fixes #34233).
# Claim: already-fixed. #34245 (cdd1c6a2af) is on main and closed #34233. Copilot ACP sessions now
# get only core T3 tools plus a search tool, optional tools are listed on demand, and other providers
# keep the full catalog. The script runs #34245's regression tests: they PASS on main and FAIL on the
# commit just before #34245. Requires deps installed (`vp i`) and `gh`. Run from a clean t3code checkout root.
set -euo pipefail

FIX=cdd1c6a2af4e48811f8549caea17e73dedda30bf
MCP_TEST=apps/server/src/mcp/McpHttpServer.test.ts
ACP_TEST=apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts
MCP_CASE="lists optional tools to a deferring client only after tool_search loads them"
ACP_CASE="gives Copilot T3's MCP server over HTTP when it advertises HTTP"

if [ -n "$(git status --porcelain)" ]; then
  echo "Refusing to run: working tree is dirty." >&2
  exit 2
fi
ORIG=$(git symbolic-ref -q --short HEAD || git rev-parse HEAD)
restore() { git reset -q --hard; git checkout -q "$ORIG"; }
trap 'rc=$?; restore; [ "${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)
git fetch -q origin pull/34238/head
PR_HEAD=$(git rev-parse FETCH_HEAD)
git fetch -q origin "$FIX" 2>/dev/null || true

fails=0
check() { if "$@"; then echo "PASS  $DESC"; else echo "FAIL  $DESC"; fails=$((fails + 1)); fi; }
summary() { for log in "$@"; do grep -E "^ *(Test Files|Tests) |✓|×|FAIL " "$log" | sed 's/^/      /' | head -6 || true; done; }
run_tests() {
  vp test run "$MCP_TEST" -t "$MCP_CASE" >/tmp/prove-34238-mcp.log 2>&1 &&
    vp test run "$ACP_TEST" -t "$ACP_CASE" >/tmp/prove-34238-acp.log 2>&1
}

DESC="#34245 merge commit ${FIX:0:10} is an ancestor of main ${MAIN:0:10}"
check git merge-base --is-ancestor "$FIX" "$MAIN"

DESC="PR head ${PR_HEAD:0:10} was cut before #34245 (does not contain ${FIX:0:10})"
check bash -c "! git merge-base --is-ancestor '$FIX' '$PR_HEAD'"

DESC="#34245 is MERGED"
check test "$(gh pr view 34245 --repo pingdotgg/t3code --json state --jq .state)" = MERGED

DESC="issue #34233 (the issue #34238 fixes) is CLOSED as COMPLETED by #34245"
check test "$(gh api graphql -f query='{repository(owner:"pingdotgg",name:"t3code"){issue(number:34233){state stateReason timelineItems(itemTypes:CLOSED_EVENT,last:1){nodes{...on ClosedEvent{closer{...on PullRequest{number}}}}}}}}' \
  --jq '.data.repository.issue | "\(.state) \(.stateReason) \(.timelineItems.nodes[0].closer.number)"')" = "CLOSED COMPLETED 34245"

git checkout -q --detach "$MAIN"
DESC="main: the Copilot ACP flavor opts into deferred MCP tools (deferMcpTools: true)"
check bash -c "git grep -q 'deferMcpTools: true' -- packages/provider-acp-registry/src/server/adapter.ts"

DESC="main: deferring session lists core tools + tool_search, loads optional tools on demand, other clients keep full list; Copilot's HTTP MCP entry carries the deferral header (tests pass)"
check run_tests
summary /tmp/prove-34238-mcp.log /tmp/prove-34238-acp.log

PRE_FIX=$(git rev-parse "$FIX^1")
git checkout -q --detach "$PRE_FIX"
git checkout -q "$FIX" -- "$MCP_TEST" "$ACP_TEST"
DESC="pre-#34245 ${PRE_FIX:0:10}: McpHttpServer has no tool-search layer, so the deferring-client test FAILS"
check bash -c "! vp test run '$MCP_TEST' -t '$MCP_CASE' >/tmp/prove-34238-mcp-pre.log 2>&1"
summary /tmp/prove-34238-mcp-pre.log
DESC="pre-#34245 ${PRE_FIX:0:10}: Copilot's HTTP MCP entry lacks the deferral header, so the transport test FAILS"
check bash -c "! vp test run '$ACP_TEST' -t \"$ACP_CASE\" >/tmp/prove-34238-acp-pre.log 2>&1"
summary /tmp/prove-34238-acp-pre.log
grep -m1 -E "t3-tool-exposure" /tmp/prove-34238-acp-pre.log | sed 's/^/      /' || true
git reset -q --hard

if [ "$fails" -eq 0 ]; then
  echo "VERDICT: CLOSE (already-fixed) - main delivers on-demand T3 tool discovery for Copilot via #34245"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: NOT PROVEN - $fails check(s) failed"
exit 1
