#!/bin/sh
# RED-first repro — REQ-HAZARD-LAUNCH-CLI-HANG (spt-core wave input, 2026-07-25).
#
# CLASS: an spt-hosted harness endpoint hangs INDEFINITELY at bringup when the harness CLI
# RESOLVES but STALLS at startup (field: `ccs.cmd` present, `volta run ccs` blocks because its
# delegate left PATH). The launch shim is the broker PTY leader — its child wait is UNBOUNDED
# (correct for a healthy session), so a stalling child = wedged endpoint, force-stop required.
#
# A truly-ABSENT cli is already handled (resolve -> None -> clean exit 2). This repro fakes the
# PRESENT-but-STALLING case with a hanging .cmd and shows `claude-spt launch` never returns.
#
# PROPOSED FIX OWNER: spt-core daemon (it owns the bringup-bind wait + the launch child lifecycle).
# A bringup-bind TIMEOUT: if a spawned endpoint doesn't bind within N seconds, tear down the child
# + surface a clean bringup failure. Design cautions (doyle 2026-07-25): (1) custody-verified
# teardown — an unprovable/slow-but-live bind must NOT be killed (no second racer over reconcile);
# (2) N must clear slow-but-healthy cold starts (CC first boot) — configurable floor + a loud
# diagnostic naming the child and elapsed.
set -e
BIN="${1:-claude-spt}"   # path to the claude-spt binary (default: on PATH)
TD="$(mktemp -d)"
trap 'rm -rf "$TD"' EXIT
# A CLI that resolves (the file exists) but never returns — the stall.
printf '@echo off\r\nping -n 60 127.0.0.1 >nul\r\n' > "$TD/hangcli.cmd"   # windows
printf '#!/bin/sh\nsleep 60\n' > "$TD/hangcli"; chmod +x "$TD/hangcli"     # posix
echo "Launching with a resolved-but-stalling CLI; a bounded run should NOT sit for the child's 60s..."
START=$(date +%s)
# Expect: HANG (killed by the outer timeout at 10s => exit 124) until the core-side bringup timeout lands.
PATH="$TD:$PATH" timeout 10 "$BIN" launch --cli hangcli --id sptc-hang-repro || true
END=$(date +%s)
echo "elapsed=$((END-START))s  (>=10 => HANG reproduced; the shim waited on the stalling child)"
