"""Gate finding #1 (doyle 2026-09-06 23:19Z): in a COLD pool the driver refuses at
the first floor check (prebuilt xtask absent → WinError 2 → DISK_FLOOR:REFUSE),
so a fresh checkout can never run it once. Fix: a bootstrap reading before the
meter exists — direct free-space read against the same floor, named loudly.
Apply only on the gater's word; batched with any further driver findings.
"""
from pathlib import Path
import os
os.chdir(r"C:\Users\decid\Documents\projects\spt-core\.worktrees\ws272-w0")
p = Path(".github/ci/ws272-w0.py")
s = p.read_bytes().decode("utf-8").replace("\r\n", "\n")

def swap(old, new):
    global s
    assert s.count(old) == 1, old[:60]
    s = s.replace(old, new)

swap('''ACTIVE_STATES = ("in_progress", "queued", "waiting", "pending", "requested")
''',
'''ACTIVE_STATES = ("in_progress", "queued", "waiting", "pending", "requested")
# The same floor xtask disk-floor enforces (32 GiB); the bootstrap reading
# below must never be more lenient than the meter it stands in for.
FLOOR_BYTES = 32 << 30
''')

swap('''def disk_floor(root, output, name, xtask):
    # Use a prebuilt xtask: compiling the meter before its first reading would
    # make the very first build unguarded. --xtask permits a bootstrap binary.
    try:
        result = subprocess.run(
            [str(xtask), "disk-floor", "--path", str(root), "--label", "ws272-w0"],
            cwd=root, capture_output=True, text=True,
        )
        raw, code = result.stdout + result.stderr, result.returncode
    except OSError as error:
        raw, code = f"Cannot run prebuilt xtask {xtask}: {error}\\n", 127
    admitted, verdict = floor_verdict(raw, code)''',
'''def bootstrap_floor_reading(root, xtask):
    """A COLD pool has no prebuilt meter yet: read free space directly against
    the same floor and say so, so the first run of a fresh checkout is guarded
    rather than refused. The claim leg builds xtask; every later floor check
    uses the real meter."""
    import shutil
    free = shutil.disk_usage(root).free
    return (
        f"DISK_FLOOR:BOOTSTRAP: prebuilt xtask absent ({xtask}); direct free-space "
        f"read free_bytes={free} floor_bytes={FLOOR_BYTES} label=ws272-w0\\n"
    )


def disk_floor(root, output, name, xtask):
    # Use a prebuilt xtask: compiling the meter before its first reading would
    # make the very first build unguarded. --xtask permits a bootstrap binary;
    # a cold pool (no meter at all) takes one direct reading, named as such.
    if not Path(xtask).exists():
        raw, code = bootstrap_floor_reading(root, xtask), 0
    else:
        try:
            result = subprocess.run(
                [str(xtask), "disk-floor", "--path", str(root), "--label", "ws272-w0"],
                cwd=root, capture_output=True, text=True,
            )
            raw, code = result.stdout + result.stderr, result.returncode
        except OSError as error:
            raw, code = f"Cannot run prebuilt xtask {xtask}: {error}\\n", 127
    admitted, verdict = floor_verdict(raw, code)''')

swap('''    print("PASS env scrub: perch identity dropped and named, everything else kept, clean env untouched")
''',
'''    print("PASS env scrub: perch identity dropped and named, everything else kept, clean env untouched")
    with tempfile.TemporaryDirectory() as tmp:
        raw = bootstrap_floor_reading(Path(tmp), Path(tmp) / "no-such-xtask")
        admitted, verdict = floor_verdict(raw, 0)
        if "DISK_FLOOR:BOOTSTRAP" not in raw or "no-such-xtask" not in raw or verdict.split(":")[1] not in ("PASS", "REFUSE"):
            raise AssertionError(f"cold-pool bootstrap: raw={raw!r} verdict={verdict!r}")
        print(f"PASS cold-pool bootstrap: direct reading named the missing meter and rendered {verdict.split(':')[1]}")
''')

p.write_bytes(s.replace("\n", "\r\n").encode("utf-8"))
print("driver cold-pool bootstrap patched")
