"""Launch the W0 driver detached under a PROVEN scrubbed environment.

Spawns `python .github/ci/ws272-w0.py --run --hold-released --output <dir>`
with an explicit env that lacks the endpoint identity trio the daemon-stop guard
reads (OWL_SESSION_ID / SPT_AGENT_ID / SPT_ENDPOINT_ID) and its perch companions,
then reads the child's environment back with psutil and refuses to report
success unless the read-back agrees. A launcher that cannot prove its scrub is
the mechanism that produced battery #2 attempt 2 (2026-09-06, todlando). Copied verbatim into doyle's gate worktree; root resolves to THIS worktree.
"""
import os, subprocess, sys, time
from pathlib import Path
import psutil

root = Path(__file__).resolve().parents[1]
out = sys.argv[1] if len(sys.argv) > 1 else ".spt/gate-head-c"
TRIO = ("OWL_SESSION_ID", "SPT_AGENT_ID", "SPT_ENDPOINT_ID")
COMPANIONS = ("SPT_SESSION_NAME", "SPT_ADAPTER", "SPT_HOST_PID", "SPT_INJECT_VERIFY_ECHO")
env = dict(os.environ)
dropped = [k for k in TRIO + COMPANIONS if env.pop(k, None) is not None]
outdir = root / out
outdir.mkdir(parents=True, exist_ok=True)
log = open(outdir / "driver.log", "wb")
flags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS
child = subprocess.Popen(
    [sys.executable, ".github/ci/ws272-w0.py", "--run", "--hold-released", "--output", out],
    cwd=str(root), env=env, stdout=log, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL,
    creationflags=flags, close_fds=True,
)
time.sleep(2)
proc = psutil.Process(child.pid)
seen = proc.environ()
leaked = [k for k in TRIO + COMPANIONS if k in seen]
print(f"driver pid={child.pid} dropped-at-launch={' '.join(dropped) or 'none'}")
print(f"read-back: identity vars in the driver's environment = {leaked or 'NONE'}")
if leaked:
    proc.kill()
    print("LAUNCH REFUSED: scrub did not reach the child; killed it")
    sys.exit(4)
print("LAUNCH PROVEN: driver runs without the endpoint identity")
