import io

s = io.open(".github/ci/ws272-w1.py", encoding="utf-8").read()

old_doc = s[: s.index('"""', s.index('"""') + 3) + 3]
new_doc = '''#!/usr/bin/env python3
"""W2 local gate driver (WEBSERVE #246 + #147 + #17). Run only after the gater releases the hold.

Same shape as ws272-w1.py — the IR-76 three-arm pre-flight, the lane lock, the env
scrub, the cold-pool bootstrap — with ONE deliberate difference: the nextest leg is
TARGETED, not a workspace sweep (gater ruling 2026-09-07 02:40Z). W2 shares the W1
pool (176 GB) on a box measuring ~28 GB free against CI's 32 GiB floor, and a full
sweep there fills the disk MID-SUITE — the failure that voided a W1 leg: five disk
reds, no exit file, and a window nobody could read. The full sweep runs once, on the
golden head, where it belongs.

The filter is `kind(lib)` over the four crates W2 touches plus W2's own test bins by
name. `kind(lib)` is the saving: it takes every unit cell in those crates and NONE of
the several hundred integration binaries under crates/spt/tests, each of which spawns
daemons. A binary named here that does not exist yet matches nothing and reds nothing.

--preflight-only makes no build claim and runs no cargo. --self-test exercises
all three IR-76 decisions without network access. --run requires the explicit
hold-release acknowledgement and repeats the live meter before each cargo leg.
Every leg retains its raw output and actual subprocess exit code beside it.

SAME-LANE POOL SERIALIZATION IS THE INVARIANT: one driver at a time per
worktree, whatever its output dir. Every --run holds the lane lock
<worktree>/.spt/driver.lock (beside the target/ pool it guards) for its whole
life; a second driver in the same worktree is REFUSED with POOL_HELD naming the
holder pid. An output-dir lock would not do: two drivers with different output
dirs still share the worktree, target/ and the default docs port, which is the
shape that bit. Mechanism this guards (W0, 2026-09-06): stopping a LOCAL ssh
does not stop its remote `bash -lc` (no tty, no SIGHUP) — an orphan chain
re-ran this driver beside the intended one, two suites interleaved one
nextest.raw (two Summary lines), and the gater voided the window.

The battery runs under a SCRUBBED environment: the perch identity variables an
agent's shell carries (SPT_ENDPOINT_ID and its companions) are dropped from
every leg's child and named once as ENV_SCRUBBED. Mechanism (W0, 2026-09-06):
a driver launched from a live perch inherited SPT_ENDPOINT_ID, and every test
that stops a daemon was refused by the product's own endpoint guard
(DAEMON_STOP_REFUSED) — two reds that were the launcher's, not the tree's.
"""'''
s = s.replace(old_doc, new_doc, 1)

assert s.count("ws272-w1") == 5, s.count("ws272-w1")
s = s.replace("ws272-w1", "ws272-w2")

old_legs = '''        ("nextest", ["cargo", "nextest", "run", "--no-fail-fast", "-p", "spt-store",
                     "-p", "spt-runtime", "-p", "spt-net", "-p", "spt-daemon", "-p", "spt"], True),'''
assert s.count(old_legs) == 1
new_legs = '''        # TARGETED (see the module docstring): every unit cell in the four
        # crates W2 touches, plus W2's own test bins by name. NOT a workspace
        # sweep — this pool and this disk cannot carry one without filling
        # mid-suite, and the full sweep at the golden head covers what this skips.
        ("nextest", ["cargo", "nextest", "run", "--no-fail-fast", "-E", NEXTEST_FILTER], True),'''
s = s.replace(old_legs, new_legs, 1)

anchor = "BATTERY_ENV = None  # set by --run before the first leg"
assert s.count(anchor) == 1
filt = '''# The W2 nextest population, as ONE expression so the gate reads it as one fact.
# kind(lib) = the unit cells compiled into each crate's own lib target; the
# integration binaries under crates/spt/tests are named individually because W2
# adds its own and every other one is out of this lane's scope.
W2_CRATES = ("spt-proto", "spt-store", "spt-daemon", "spt")
W2_TEST_BINS = ("webserve_attachment_e2e",)
NEXTEST_FILTER = " + ".join(
    [f"(kind(lib) & package({name}))" for name in W2_CRATES]
    + [f"binary({name})" for name in W2_TEST_BINS]
)

''' + anchor
s = s.replace(anchor, filt, 1)

io.open(".github/ci/ws272-w2.py", "w", encoding="utf-8", newline="").write(s)
print("driver written")
