#!/usr/bin/env bash
# PR #32851 "fix(opencode): background shells count as pending work, and Stop ends them" (Fixes #32829)
# Claim: already-fixed. #34141 (ed52af3a47, "feat(opencode): track native shell jobs across turns") delivers it,
# and the PR's own author closed #32829 as fixed by #34141.
# Shows: ed52af3a47 is on main; #32829's closing comment; main's OpenCode 2 adapter counts running shells as
# pending work, lists them in the roster as `command` tasks and removes them on Stop; main's focused adapter
# tests for exactly that lifecycle (launch -> pending after the turn -> exit/Stop/restart -> cleared) pass.
# Needs deps installed (`vp i`) and an authenticated `gh`. Run from the root of a clean pingdotgg/t3code checkout.
set -euo pipefail

if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
  echo "Refusing to run: the working tree has uncommitted changes." >&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/32851/head
PR_HEAD=$(git rev-parse FETCH_HEAD)
SUPERSEDING=ed52af3a478652c55aeaaf5a64bbf6584dd6a0b4
ADAPTER=packages/provider-opencode/src/server/v2/adapter.ts
TEST=apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
echo "main=$MAIN pr=$PR_HEAD"
fail=0
check() { if eval "$2"; then echo "PASS  $1"; else echo "FAIL  $1"; fail=1; fi; }

check "#34141 ($SUPERSEDING) is an ancestor of main" 'git merge-base --is-ancestor "$SUPERSEDING" "$MAIN"'

closer=$(gh api repos/pingdotgg/t3code/issues/32829/events --jq '[.[] | select(.event=="closed")] | last | .actor.login')
note=$(gh api repos/pingdotgg/t3code/issues/32829/comments --jq '.[] | select(.user.login=="juliusmarminge") | .body')
check "#32829 closed by juliusmarminge (this PR's author) with 'Fixed by .../pulls/34141'" \
  '[ "$closer" = "juliusmarminge" ] && grep -q "Fixed by https://git.cafe/pingdotgg/t3code/pulls/34141" <<<"$note"'

src=$(git show "$MAIN:$ADAPTER")
check "main: owesWork counts a running shell as pending background work" \
  'grep -qF "[...shells.values()].some((shell) => shell.state === state && shell.running)" <<<"$src"'
check "main: the pending-work roster lists running shells as kind \"command\" with their command" \
  'grep -A4 -F "for (const [taskId, shell] of shells) {" <<<"$src" | grep -q "kind: \"command\""'
check "main: Stop (stopBackground) removes the thread's running shells" \
  'grep -A4 -F "const stopBackground = Effect.fnUntraced" <<<"$src" | grep -q "stopShell(id)"'

git checkout -q --detach "$MAIN"
out=$(vp test run "$TEST" -t "tracks native background shells through launch|recovers only owned native shells" 2>&1) || true
echo "$out" | grep -E "Test Files|Tests " || echo "$out" | tail -40
check "main: launch->pending->exit/Stop/restart and owned-shell recovery tests pass (13 cases)" \
  'grep -qE "Tests +13 passed" <<<"$out" && ! grep -qE "[0-9]+ failed" <<<"$out"'

if [ "$fail" -eq 0 ]; then
  echo "VERDICT: CLOSE (already-fixed) - main delivers #32851's behavior via #34141 ($SUPERSEDING)"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: NOT PROVEN - see FAIL lines above"
exit 1
