Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

70.8 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.

"""Guards the ways a dispatched result silently never arrives; all tests run on CPU against stubs."""

from __future__ import annotations

import json
import re
import sys
import time
from pathlib import Path

import pytest
import yaml


def _shared_setup_1(monkeypatch):
    deleted: list[str] = []
    monkeypatch.setattr(
        launch, "delete_kernel", lambda slug, deadline = None: deleted.append(slug) or True
    )
    return deleted


def _shared_setup_2():
    entry = {
        "slug": "me/unsloth-t4-ci-nabcdef01-1111",
        "sha": "abcdef01",
        "kind": "notebook",
        "legacy": False,
        "age_hours": 0.4,
    }
    return entry


def _shared_setup_3(deleted, monkeypatch):
    monkeypatch.setattr(
        launch, "delete_kernel", lambda slug, deadline = None: deleted.append(slug) or True
    )
    api = _StubApi([], {"me/unsloth-t4-ci-nabcdef01-1111": "COMPLETE"})
    return api


REPO_ROOT = Path(__file__).resolve().parents[2]
CI_DIR = REPO_ROOT / ".github" / "scripts" / "kaggle_t4_ci"
WORKFLOWS = REPO_ROOT / ".github" / "workflows"
NOTEBOOK_WF = WORKFLOWS / "kaggle-t4-notebook-ci.yml"
STUDIO_WF = WORKFLOWS / "kaggle-t4-studio-gpu-ci.yml"
COLLECT_WF = WORKFLOWS / "kaggle-collect.yml"

sys.path.insert(0, str(CI_DIR))

import collect  # noqa: E402
import gate  # noqa: E402
import launch  # noqa: E402
import post_statuses  # noqa: E402


def _wf(path: Path) -> dict:
    return yaml.safe_load(path.read_text(encoding = "utf-8"))


def _steps(workflow: dict) -> list[tuple[str, str, dict]]:
    out = []
    for job_name, job in workflow["jobs"].items():
        for step in job.get("steps", []) or []:
            out.append((job_name, step.get("name", ""), step))
    return out


def test_the_slug_carries_the_commit_and_the_workflow():
    """The slug is the ONLY thing surviving the dispatching runner: without the
    commit and the workflow in it, a finished kernel has nowhere to report."""
    name = launch.slug_name("notebook", "1a2b3c4d5e6f7890")
    parsed = launch.parse_slug(name)
    assert parsed is not None, f"{name} does not parse as one of ours"
    assert parsed["sha"] == "1a2b3c4d5e6f", parsed
    assert parsed["kind"] == "notebook", parsed


def test_the_slug_carries_twelve_hex_characters_and_still_reads_eight():
    """Two commits can share eight characters and the commits API 422s the ambiguous prefix every
    pass, leaving status pending forever; twelve is written, eight is still read for old kernels."""
    assert launch.SLUG_SHA_LEN == 12
    name = launch.slug_name("studio", "a" * 40)
    assert launch.parse_slug(name)["sha"] == "a" * 12
    old = launch.parse_slug("me/unsloth-t4-ci-nabcdef01-1111")
    assert old is not None and old["sha"] == "abcdef01" and old["kind"] == "notebook"
    # A too-short commit falls back to the legacy slug form.
    assert launch.parse_slug(launch.slug_name("notebook", "abcdef01"))["legacy"] is True


def test_in_flight_matches_the_old_and_the_new_slug_forms(tmp_path, monkeypatch):
    """A kernel pushed with the eight-character form must still count as in
    flight for its full commit, and a twelve-character one for the same."""
    full = "abcdef0123456789" + "0" * 24
    for slug_sha in ("abcdef01", "abcdef012345"):
        slug = f"danielhanchen/unsloth-t4-ci-n{slug_sha}-1111"
        api = _StubApi([_StubKernel(slug)], {slug: "RUNNING"})
        monkeypatch.setattr(launch, "_api", lambda api = api: api)
        monkeypatch.setattr(
            sys,
            "argv",
            ["collect", "--outdir", str(tmp_path / slug_sha), "--sha", full, "--kind", "notebook"],
        )
        assert collect.main() == 0
        result = json.loads((tmp_path / slug_sha / "collect_result.json").read_text())
        assert result["in_flight_for_sha"] is True, (slug_sha, result)


def test_the_slug_carries_the_slot_and_reads_slot_one_when_absent():
    """Slot 2 is a deliberate second session beside slot 1, so the duplicate
    check needs "this commit, this kind, THIS slot" and the slug has to carry
    the slot. Absent means slot 1, so every earlier slug still reads."""
    two = launch.slug_name("notebook", "1a2b3c4d5e6f7890", slot = "2")
    parsed = launch.parse_slug(two)
    assert parsed and parsed["slot"] == "2" and parsed["sha"] == "1a2b3c4d5e6f", (two, parsed)
    one = launch.slug_name("notebook", "1a2b3c4d5e6f7890", slot = "1")
    assert launch.parse_slug(one)["slot"] == "1"
    assert launch.parse_slug("me/unsloth-t4-ci-n1a2b3c4d5e6f-abcd")["slot"] == "1"
    assert launch.parse_slug("me/unsloth-t4-ci-nabcdef01-1111")["slot"] == "1"
    assert launch.parse_slug("me/unsloth-t4-ci-nabcdef01-21111")["slot"] == "2"
    assert launch.parse_slug(launch.slug_name("notebook", "1a2b3c4d5e6f", slot = "23"))["slot"] == "1"
    assert launch._slugify(two.replace("-", " ")) == two
    assert "slot" in launch.parse_slug(two)
    assert (
        gate.in_flight_for_commit([f"me/{two} (RUNNING)"], "1a2b3c4d5e6f7890", "notebook", "2")
        == f"me/{two}"
    )
    assert (
        gate.in_flight_for_commit([f"me/{two} (RUNNING)"], "1a2b3c4d5e6f7890", "notebook", "1")
        is None
    )
    assert (
        gate.in_flight_for_commit([f"me/{one} (RUNNING)"], "1a2b3c4d5e6f7890", "notebook", "2")
        is None
    )


def test_the_collector_in_flight_answer_is_per_slot(tmp_path, monkeypatch):
    full = "abcdef0123456789" + "0" * 24
    slug = "danielhanchen/unsloth-t4-ci-nabcdef012345-21111"
    for slot, expected in (("2", True), ("1", False)):
        api = _StubApi([_StubKernel(slug)], {slug: "RUNNING"})
        monkeypatch.setattr(launch, "_api", lambda api = api: api)
        out = tmp_path / f"slot{slot}"
        monkeypatch.setattr(
            sys,
            "argv",
            ["collect", "--outdir", str(out), "--sha", full, "--kind", "notebook", "--slot", slot],
        )
        assert collect.main() == 0
        result = json.loads((out / "collect_result.json").read_text())
        assert result["in_flight_for_sha"] is expected, (slot, result)


def test_two_dispatches_of_one_commit_do_not_collide():
    """A re-run, or slots 1 and 2, must not push to one slug: pushing to an
    existing id starts a second session the status calls never answer for."""
    names = {launch.slug_name("notebook", "deadbeefcafe") for _ in range(200)}
    assert len(names) > 190, f"only {len(names)} distinct slugs in 200 draws"


def test_the_slug_still_round_trips_through_slugify():
    """Kaggle files the kernel under the slugified TITLE, not the metadata id,
    so a name that does not round-trip 403s every later status call."""
    for kind in ("notebook", "studio"):
        name = launch.slug_name(kind, "0123456789ab")
        assert launch._slugify(name.replace("-", " ")) == name, name


def test_the_gate_still_recognises_a_dispatched_kernel_as_ours():
    """Dispatched slugs need OWN_KERNEL_PREFIX, or the gate thinks the account is idle and Kaggle
    refuses."""
    import gate
    for kind in ("notebook", "studio"):
        name = launch.slug_name(kind, "abcdef01")
        assert name.startswith(gate.OWN_KERNEL_PREFIX), (
            f"{name} does not start with the gate's {gate.OWN_KERNEL_PREFIX!r}, so the "
            "gate would count a dispatched kernel as somebody else's or not at all"
        )


@pytest.mark.parametrize(
    "foreign",
    (
        "danielhanchen/unsloth-probe-vision-train-r3-6dd742",
        "someone/my-important-notebook",
        "danielhanchen/unsloth-t4-cixyz",
        "danielhanchen/unsloth-studio-ci-12345678",
        "",
    ),
)
def test_a_kernel_that_is_not_ours_is_invisible(foreign):
    """The collector enumerates an account shared with a human and deletes what it collects, so
    there is deliberately no heuristic; a false positive deletes someone's work."""
    assert launch.parse_slug(foreign) is None, f"{foreign!r} was accepted as one of ours"


def test_a_legacy_slug_is_still_reapable_but_reports_nothing():
    """Kernels pushed before this change outlive it: still ours and reapable, or
    they bill forever, but with no commit to attribute a verdict to."""
    parsed = launch.parse_slug("danielhanchen/unsloth-t4-ci-d7faf2b8")
    assert parsed is not None and parsed["legacy"] is True
    assert parsed["sha"] is None and parsed["kind"] is None
    assert collect.statuses_from([{**parsed, "slug": "x", "verdict": "pass", "reason": "ok"}]) == []


def test_dispatch_without_a_commit_is_a_usage_error(monkeypatch, capsys):
    """A dispatch whose slug carries no commit runs, costs quota, and reports to
    nobody. Refused at the only moment it is still free."""
    monkeypatch.setattr(
        sys,
        "argv",
        ["launch.py", "--notebook", "k.ipynb", "--outdir", "out", "--dispatch"],
    )
    with pytest.raises(SystemExit) as excinfo:
        launch.main()
    assert excinfo.value.code != 0
    assert "commit-sha" in capsys.readouterr().err


def test_dispatch_does_not_delete_the_kernel_it_pushed():
    """Dispatch leaves the kernel running via the existing --keep-kernel flag, not a second guard in
    release(), since two guards on one deletion can go wrong and the kernel bills GPU quota."""
    src = (CI_DIR / "launch.py").read_text(encoding = "utf-8")
    assert re.search(r"if args\.dispatch:\s*\n\s*args\.keep_kernel = True", src), (
        "dispatch mode must set keep_kernel, or release() deletes the kernel it "
        "was told to leave running and the collector finds nothing"
    )


def test_dispatch_does_not_report_pass():
    """A dispatch reports dispatched, not pass: nothing has run yet, and pass would read as a result."""
    src = (CI_DIR / "launch.py").read_text(encoding = "utf-8")
    block = src[src.index("if args.dispatch:", src.index('result["slug"] = live[0]')) :]
    block = block[: block.index("return finish()")]
    assert '"dispatched"' in block, block[:400]
    assert 'result["verdict"] = "pass"' not in block


def test_the_dispatch_worst_case_excludes_the_phases_it_never_runs():
    """Otherwise the pre-push guard demands two hours of job deadline for a job that exits in five
    minutes and stands runs down for a window they do not need."""
    full = launch.worst_case_seconds(5400, 1)
    quick = launch.worst_case_seconds(5400, 1, dispatch = True)
    assert quick < full, (quick, full)


class _StubKernel:
    def __init__(
        self,
        ref,
        last_run_time = None,
    ):
        self.ref = ref
        self.last_run_time = last_run_time


class _StubApi:
    CONFIG_NAME_USER = "username"

    def __init__(self, kernels, statuses):
        self.config_values = {"username": "danielhanchen"}
        self._kernels = kernels
        self._statuses = statuses
        self.status_calls: list[str] = []

    def kernels_list(
        self,
        mine = True,
        page = 1,
        page_size = 100,
        sort_by = "dateRun",
    ):
        return self._kernels if page == 1 else []

    def kernels_status(self, ref):
        self.status_calls.append(ref)
        state = self._statuses[ref]
        if isinstance(state, Exception):
            raise state
        return type("S", (), {"status": f"KernelWorkerStatus.{state}"})()


def test_a_running_kernel_within_its_ceiling_is_left_completely_alone(tmp_path):
    """No delete, no status, not even a warning: reporting a kernel doing its
    job puts a red on a commit whose result is still coming."""
    api = _StubApi([], {"me/unsloth-t4-ci-nabcdef01-1111": "RUNNING"})
    deleted: list[str] = []
    entry = {
        "slug": "me/unsloth-t4-ci-nabcdef01-1111",
        "sha": "abcdef01",
        "kind": "notebook",
        "legacy": False,
        "age_hours": 0.5,
    }
    record = collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "pending"
    assert record["deleted"] is False
    assert collect.statuses_from([record]) == [], "a running kernel must post no status"
    assert deleted == []


def test_a_kernel_past_its_ceiling_is_reaped_and_reported(tmp_path, monkeypatch):
    """The collector reaps a kernel past its ceiling and reports it, since nothing else deletes it now."""
    deleted = _shared_setup_1(monkeypatch)
    api = _StubApi([], {"me/unsloth-t4-ci-nabcdef01-1111": "RUNNING"})
    entry = {
        "slug": "me/unsloth-t4-ci-nabcdef01-1111",
        "sha": "abcdef01",
        "kind": "notebook",
        "legacy": False,
        "age_hours": 9.0,
    }
    record = collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "reaped"
    assert deleted == ["me/unsloth-t4-ci-nabcdef01-1111"]
    status = collect.statuses_from([record])[0]
    assert status["state"] == "failure", status


def test_a_kernel_with_no_timestamp_is_never_reaped(tmp_path, monkeypatch):
    """A missing timestamp is not evidence that a kernel is old, and guessing in
    that direction DELETES A RUNNING SESSION."""
    deleted = _shared_setup_1(monkeypatch)
    api = _StubApi([], {"me/unsloth-t4-ci-nabcdef01-1111": "RUNNING"})
    entry = {
        "slug": "me/unsloth-t4-ci-nabcdef01-1111",
        "sha": "abcdef01",
        "kind": "notebook",
        "legacy": False,
        "age_hours": None,
    }
    record = collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "pending"
    assert deleted == []


def test_evidence_is_downloaded_before_the_kernel_is_deleted(tmp_path, monkeypatch):
    """A delete that lands first turns a finished run into a result nobody can
    read. Asserted by ORDER of the real calls, not by reading the source."""
    order: list[str] = []
    monkeypatch.setattr(
        launch, "fetch_evidence", lambda slug, dest, deadline = None: order.append("fetch") or {}
    )
    monkeypatch.setattr(launch, "extract_reports", lambda dest: [{"passed": True}])
    monkeypatch.setattr(
        launch, "delete_kernel", lambda slug, deadline = None: order.append("delete") or True
    )
    api = _StubApi([], {"me/unsloth-t4-ci-nabcdef01-1111": "COMPLETE"})
    entry = _shared_setup_2()
    collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0)
    assert order == ["fetch", "delete"], order


def test_a_kernel_whose_evidence_will_not_download_is_NOT_deleted(tmp_path, monkeypatch):
    """Deleting here destroys the only copy of a finished run's result to
    reclaim a slot the kernel is not using. The next pass retries."""

    def _boom(
        slug,
        dest,
        deadline = None,
    ):
        raise TimeoutError("slow")

    deleted: list[str] = []
    monkeypatch.setattr(launch, "fetch_evidence", _boom)
    api = _shared_setup_3(deleted, monkeypatch)
    entry = _shared_setup_2()
    record = collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0)
    # `pending`, not `infra`: infra posts green and is released, so it would never be retried.
    assert record["verdict"] == "pending"
    assert record["verdict"] not in collect.DELETABLE
    assert collect.statuses_from([record]) == []
    assert deleted == [], "evidence that would not download must not be deleted"


def test_an_unreadable_status_does_nothing_at_all(tmp_path, monkeypatch):
    """Both available actions are destructive: delete and we may kill a running
    session, report and we may fail a run that was fine. Ask again next pass."""
    deleted = _shared_setup_1(monkeypatch)
    api = _StubApi([], {"me/unsloth-t4-ci-nabcdef01-1111": RuntimeError("503 upstream")})
    entry = _shared_setup_2()
    record = collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "pending"
    assert deleted == []
    assert collect.statuses_from([record]) == []


def test_find_ours_skips_everything_it_does_not_recognise():
    """Driven against a listing mixing a human's kernels with ours: a rule fed a
    hand-written list of slugs proves nothing about the filter."""
    listing = [
        _StubKernel("danielhanchen/unsloth-probe-vision-train-r3-6dd742"),
        _StubKernel("danielhanchen/unsloth-t4-ci-nabcdef01-1111"),
        _StubKernel("danielhanchen/my-actual-work"),
        _StubKernel("danielhanchen/unsloth-t4-ci-d7faf2b8"),
    ]
    api = _StubApi(listing, {})
    ours = [k["slug"] for k in collect.find_ours(api)]
    assert ours == [
        "danielhanchen/unsloth-t4-ci-nabcdef01-1111",
        "danielhanchen/unsloth-t4-ci-d7faf2b8",
    ], ours


def test_infra_and_partial_do_not_go_red():
    """`infra` and `partial` learned nothing about the code, and a red the
    author cannot act on is how a required check gets REMOVED."""
    assert collect.VERDICT_STATE["infra"] == "success"
    assert collect.VERDICT_STATE["partial"] == "success"
    assert collect.VERDICT_STATE["fail"] == "failure"
    assert collect.VERDICT_STATE["reaped"] == "failure"


def test_a_failed_payload_reaches_github_as_a_failure():
    """Driven through the real verdict and status functions, since every rule
    above this one is fed a dict written by hand."""
    record = {
        "slug": "me/unsloth-t4-ci-nabcdef01-1111",
        "sha": "abcdef01",
        "kind": "notebook",
        "verdict": None,
        "reason": "",
    }
    verdict, reason = collect.verdict_of([{"passed": False, "payload": "gptoss"}], 1)
    record["verdict"], record["reason"] = verdict, reason
    status = collect.statuses_from([record])[0]
    assert status["state"] == "failure"
    assert status["context"] == "kaggle-t4-notebook"
    assert "gptoss" in status["description"]


def test_the_collector_never_sees_a_github_token():
    """A script holding both a Kaggle and a GitHub credential is one bug away from sending one to
    the other, so the collector emits statuses as data for the workflow to post."""
    src = (CI_DIR / "collect.py").read_text(encoding = "utf-8")
    for forbidden in ("GH_TOKEN", "GITHUB_TOKEN", "api.github.com"):
        assert forbidden not in src, f"collect.py references {forbidden}"


@pytest.mark.parametrize(
    "path,kind,context",
    ((NOTEBOOK_WF, "notebook", "kaggle-t4-notebook"), (STUDIO_WF, "studio", "kaggle-studio-gpu")),
    ids = ("notebook", "studio"),
)
def test_the_gpu_job_dispatches_rather_than_waits(path, kind, context):
    """The measured win, asserted from the workflow rather than from a comment."""
    body = path.read_text(encoding = "utf-8")
    launch_calls = re.findall(r"kaggle_t4_ci/launch\.py \\\n(?:.*\n)*?(?=\n)", body)
    assert launch_calls, f"{path.name} does not invoke launch.py"
    for call in launch_calls:
        assert "--dispatch" in call, f"{path.name} still waits for its kernel:\n{call}"
        assert "--commit-sha" in call, f"{path.name} dispatches without a commit:\n{call}"
        assert f"--kind {kind}" in call, f"{path.name} dispatches without its kind:\n{call}"


@pytest.mark.parametrize(
    "path,context",
    ((NOTEBOOK_WF, "kaggle-t4-notebook"), (STUDIO_WF, "kaggle-studio-gpu")),
    ids = ("notebook", "studio"),
)
def test_the_dispatching_job_can_post_the_status_that_replaces_it(path, context):
    """The job succeeds by dispatching, so the verdict has to travel some other
    way. Without `statuses: write` the collection runs and reaches nobody."""
    wf = _wf(path)
    gpu_jobs = [j for name, j in wf["jobs"].items() if name != "gate"]
    assert gpu_jobs, path.name
    for job in gpu_jobs:
        perms = job.get("permissions") or {}
        assert perms.get("statuses") == "write", (
            f"{path.name}'s GPU job cannot post commit statuses, so the verdict "
            "produced by its collection step is discarded"
        )


@pytest.mark.parametrize("path", (NOTEBOOK_WF, STUDIO_WF), ids = ("notebook", "studio"))
def test_the_gpu_job_collects_before_it_dispatches(path):
    """Order matters twice: collecting frees the session slot this job wants,
    and it answers whether this commit is already running."""
    body = path.read_text(encoding = "utf-8")
    assert "kaggle_t4_ci/collect.py" in body, f"{path.name} never collects"
    assert body.index("kaggle_t4_ci/collect.py") < body.index(
        "--dispatch"
    ), f"{path.name} dispatches before it collects"


@pytest.mark.parametrize("path", (NOTEBOOK_WF, STUDIO_WF), ids = ("notebook", "studio"))
def test_a_commit_already_in_flight_is_not_dispatched_again(path):
    """Two sessions for one result is quota spent twice, and under Kaggle's
    2-session cap the second may take the slot the first needs."""
    for _job, name, step in _steps(_wf(path)):
        if "--dispatch" in (step.get("run") or ""):
            assert "in_flight != 'true'" in (step.get("if") or ""), (
                f"{path.name}'s dispatch step is not gated on the in-flight check: "
                f"{step.get('if')!r}"
            )
            return
    pytest.fail(f"{path.name} has no dispatch step")


def test_a_scheduled_collector_exists_and_reaps():
    """The other route only fires when somebody pushes, so a quiet weekend with
    a wedged kernel is a weekend of unwatched billing."""
    wf = _wf(COLLECT_WF)
    on = wf.get(True) or wf.get("on")
    assert "schedule" in on, "the collector does not run on a schedule, so a quiet repo never reaps"
    assert on["schedule"], on


def test_the_collector_covers_both_accounts():
    """The gate's weighted draw decides where a session is SPENT; it must not
    decide where results are read from."""
    wf = _wf(COLLECT_WF)
    job = wf["jobs"]["collect"]
    names = {e["secret_name"] for e in job["strategy"]["matrix"]["include"]}
    import gate

    assert names == set(gate.DEFAULT_ACCOUNT_ENVS), names


def test_the_collector_holds_one_token_per_job():
    """As in the GPU workflows: a step holding both could authenticate as
    either, and delete kernels for an account it is not reporting for."""
    for _job, name, step in _steps(_wf(COLLECT_WF)):
        env = step.get("env") or {}
        tokens = sorted(k for k in env if k.startswith("KAGGLE_API_TOKEN"))
        if tokens:
            assert tokens == ["KAGGLE_API_TOKEN"], f"{name} sees {tokens}"


def test_the_collector_serialises_per_account():
    """Two collectors on one account race: the loser deletes what the winner
    already deleted and reports the 404 as a failure."""
    wf = _wf(COLLECT_WF)
    conc = wf.get("concurrency")
    assert conc, "the collector has no concurrency group"
    assert conc.get("cancel-in-progress") is False, (
        "a pass midway through downloading evidence must finish; cancelling it "
        "throws away work the next pass has to redo"
    )


def test_the_status_contexts_are_stable_strings():
    """Branch protection is configured against these names, and renaming one
    silently stops requiring it: a context that never reports is not failing."""
    assert collect.STATUS_CONTEXTS == {
        "notebook": "kaggle-t4-notebook",
        "studio": "kaggle-studio-gpu",
    }
    for path, context in ((NOTEBOOK_WF, "kaggle-t4-notebook"), (STUDIO_WF, "kaggle-studio-gpu")):
        assert context in path.read_text(encoding = "utf-8"), (
            f"{path.name} never mentions {context}, so its pending status is posted "
            "under a name nothing else uses"
        )


def test_a_dispatch_posts_a_pending_status():
    """Without it a required context that has never reported blocks the pull
    request with no explanation, indistinguishable from an unconfigured check."""
    for path, context in ((NOTEBOOK_WF, "kaggle-t4-notebook"), (STUDIO_WF, "kaggle-studio-gpu")):
        body = path.read_text(encoding = "utf-8")
        assert re.search(
            r"state=pending -f context=" + re.escape(context), body
        ), f"{path.name} never posts a pending {context} status"


def test_the_reporters_wait_for_an_EXECUTED_NOTEBOOK_not_just_a_directory():
    """Wait on an executed notebook, not hashFiles of a directory, which passes once
    launch_result.json exists."""
    for path in (NOTEBOOK_WF, STUDIO_WF):
        for _job, name, step in _steps(_wf(path)):
            body = step.get("run") or ""
            if "report.py" not in body:
                continue
            condition = step.get("if") or ""
            assert "_output.ipynb" in condition, (
                f"{path.name}: step {name!r} reports on the presence of the evidence "
                f"DIRECTORY, which a dispatching run creates and does not fill: "
                f"if={condition!r}"
            )


def _fake_gh(
    monkeypatch,
    resolve_to = None,
    post_ok = True,
    lookup_error = "",
):
    """Stand in for `gh`, recording every call. `resolve_to` is what
    `repos/../commits/<sha>` answers, None means gone in GitHub's own words, and
    `lookup_error` is what a failed lookup says."""
    calls: list[list[str]] = []

    def _gh(args):
        calls.append(list(args))
        if "/commits/" in args[1]:
            if lookup_error:
                return (1, "", lookup_error)
            if resolve_to:
                return (0, resolve_to, "")
            return (1, "", "gh: No commit found for SHA: 2ecb19df (HTTP 422)")
        return (0 if post_ok else 1, "", "")

    monkeypatch.setattr(post_statuses, "_gh", _gh)
    return calls


_STATUS = {
    "sha": "2ecb19df",
    "state": "success",
    "context": "kaggle-t4-notebook",
    "description": "pass: all 5 payload(s) passed",
    "target_url": "https://example/run/1",
    "slug": "me/unsloth-t4-ci-n2ecb19df-1111",
    "slugs": ["me/unsloth-t4-ci-n2ecb19df-1111"],
}


def test_an_abbreviated_sha_is_EXPANDED_before_a_status_is_posted(monkeypatch):
    """The statuses API rejects an abbreviated SHA with 422, so the poster must expand it first."""
    full = "2ecb19df" + "a" * 32
    calls = _fake_gh(monkeypatch, resolve_to = full)
    outcome = post_statuses.post_all([dict(_STATUS)], "unslothai/unsloth")
    assert outcome["ok"] == [_STATUS["slug"]], outcome
    posts = [c for c in calls if "/statuses/" in c[1]]
    assert posts == [posts[0]] and posts[0][1] == f"repos/unslothai/unsloth/statuses/{full}"
    assert not any(c[1].endswith("/statuses/2ecb19df") for c in calls)
    # Every field is its own argument: nothing is assembled into a shell line.
    assert "-f" in posts[0] and f"description={_STATUS['description']}" in posts[0]


def test_a_commit_that_no_longer_exists_is_reported_not_silently_dropped(monkeypatch, capsys):
    """A force-push can remove the commit a running kernel was dispatched for; posting is then
    impossible, so it must say so and release the kernel rather than retry forever."""
    calls = _fake_gh(monkeypatch, resolve_to = None)
    outcome = post_statuses.post_all([dict(_STATUS)], "unslothai/unsloth")
    assert outcome["unresolved"] == [_STATUS["slug"]] and outcome["failed"] == []
    assert not any("/statuses/" in c[1] for c in calls), "posted to a commit that is gone"
    assert "Could not resolve a collected commit" in capsys.readouterr().out


def test_every_workflow_posts_through_the_shared_poster_and_none_keeps_a_shell_loop():
    for path in (NOTEBOOK_WF, STUDIO_WF, COLLECT_WF):
        body = path.read_text(encoding = "utf-8")
        assert "kaggle_t4_ci/post_statuses.py" in body, f"{path.name} does not use the poster"
        assert "statuses.txt" not in body, f"{path.name} still carries the tab-delimited loop"


@pytest.mark.parametrize(
    "path", (NOTEBOOK_WF, STUDIO_WF, COLLECT_WF), ids = ("notebook", "studio", "collect")
)
def test_collection_never_deletes_and_the_release_step_comes_after_posting(path):
    """A verdict that does not reach GitHub must leave its kernel up for the
    next pass: collect --no-delete, post, then --delete-collected releases only
    what was delivered."""
    body = path.read_text(encoding = "utf-8")
    steps = [(name, step) for _job, name, step in _steps(_wf(path))]
    collect_steps = [
        s
        for _n, s in steps
        if "kaggle_t4_ci/collect.py" in (s.get("run") or "")
        and "--delete-collected" not in s["run"]
    ]
    assert collect_steps, f"{path.name} never collects"
    for step in collect_steps:
        assert (
            "--no-delete" in step["run"]
        ), f"{path.name} collects with deletion on:\n{step['run']}"
    names = [n for n, _s in steps]
    post = next(i for i, (n, s) in enumerate(steps) if "post_statuses.py" in (s.get("run") or ""))
    release = next(
        i for i, (n, s) in enumerate(steps) if "--delete-collected" in (s.get("run") or "")
    )
    assert (
        release > post
    ), f"{path.name} releases kernels before posting: {names[release]!r} before {names[post]!r}"
    assert "--posted kaggle_collected/posted.json" in steps[release][1]["run"]


def test_a_kernel_whose_status_did_not_post_is_KEPT_for_the_next_pass(tmp_path, monkeypatch):
    deleted = _shared_setup_1(monkeypatch)
    result = tmp_path / "collect_result.json"
    result.write_text(
        json.dumps(
            {
                "kernels": [
                    {"slug": "me/ok", "verdict": "pass", "deleted": False},
                    {"slug": "me/refused", "verdict": "fail", "deleted": False},
                    {"slug": "me/legacy", "verdict": "reaped", "deleted": False},
                    {"slug": "me/running", "verdict": "pending", "deleted": False},
                ],
                "statuses": [],
            }
        )
    )
    posted = tmp_path / "posted.json"
    posted.write_text(json.dumps({"ok": ["me/ok"], "failed": ["me/refused"], "unresolved": []}))
    assert collect.delete_collected(result, posted) == 0
    assert deleted == ["me/ok", "me/legacy"], deleted
    outcome = json.loads((tmp_path / "delete_result.json").read_text())
    assert outcome["kept"] == ["me/refused"]


def test_no_delivery_record_at_all_keeps_every_kernel_that_had_something_to_post(
    tmp_path, monkeypatch
):
    """The poster never ran, so deleting would lose every verdict of the pass."""
    deleted = _shared_setup_1(monkeypatch)
    result = tmp_path / "collect_result.json"
    result.write_text(
        json.dumps(
            {
                "kernels": [
                    {"slug": "me/a", "verdict": "pass", "deleted": False},
                    {"slug": "me/nothing-to-post", "verdict": "infra", "deleted": False},
                ],
                "statuses": [{"slug": "me/a", "slugs": ["me/a"]}],
            }
        )
    )
    collect.delete_collected(result, tmp_path / "posted.json")
    assert deleted == ["me/nothing-to-post"], deleted


def test_a_refused_post_is_red_and_recorded(monkeypatch, tmp_path):
    _fake_gh(monkeypatch, resolve_to = "2ecb19df" + "b" * 32, post_ok = False)
    outcome = post_statuses.post_all([dict(_STATUS)], "unslothai/unsloth")
    assert outcome["failed"] == [_STATUS["slug"]]
    result = tmp_path / "r.json"
    result.write_text(json.dumps({"statuses": [dict(_STATUS)]}))
    monkeypatch.setattr(
        sys,
        "argv",
        [
            "post_statuses",
            "--result",
            str(result),
            "--out",
            str(tmp_path / "p.json"),
            "--repo",
            "o/r",
        ],
    )
    assert post_statuses.main() == 1
    assert json.loads((tmp_path / "p.json").read_text())["failed"] == [_STATUS["slug"]]


def test_the_poster_signs_only_records_it_recognises(monkeypatch, capsys):
    calls = _fake_gh(monkeypatch, resolve_to = "2ecb19df" + "c" * 32)
    bad_state = dict(_STATUS, state = "green")
    bad_context = dict(_STATUS, context = "ci/somebody-else")
    outcome = post_statuses.post_all([bad_state, bad_context], "o/r")
    assert outcome["invalid"] == [_STATUS["slug"], _STATUS["slug"]]
    assert calls == [], "a malformed record reached gh"


def test_two_kernels_for_one_commit_and_context_post_ONE_status_and_a_failure_wins():
    """Slots 1 and 2 on one sha are two kernels under one context, and two
    statuses would race, the last posted becoming the visible verdict."""
    records = [
        {
            "slug": "me/one",
            "sha": "abcdef01",
            "kind": "notebook",
            "verdict": "pass",
            "reason": "all passed",
        },
        {
            "slug": "me/two",
            "sha": "abcdef01",
            "kind": "notebook",
            "verdict": "fail",
            "reason": "1 of 5 failed",
        },
        {
            "slug": "me/other",
            "sha": "abcdef02",
            "kind": "notebook",
            "verdict": "pass",
            "reason": "all passed",
        },
    ]
    statuses = collect.statuses_from(records)
    assert len(statuses) == 2
    joint = next(s for s in statuses if s["sha"] == "abcdef01")
    assert joint["state"] == "failure" and sorted(joint["slugs"]) == ["me/one", "me/two"]
    statuses = collect.statuses_from(list(reversed(records)))
    assert next(s for s in statuses if s["sha"] == "abcdef01")["state"] == "failure"


def test_a_description_is_one_line():
    record = {
        "slug": "me/k",
        "sha": "abcdef01",
        "kind": "notebook",
        "verdict": "fail",
        "reason": "a\nb\tc",
    }
    assert collect.statuses_from([record])[0]["description"] == "fail: a b c"


def _terminal_entry():
    return {
        "slug": "me/unsloth-t4-ci-nabcdef01-1111",
        "sha": "abcdef01",
        "kind": "notebook",
        "legacy": False,
        "age_hours": 0.4,
    }


def test_an_incomplete_download_judges_nothing_and_keeps_the_kernel(tmp_path, monkeypatch):
    """fetch_evidence flags a spent budget instead of raising. Judging the short
    set reads a run that lost half its notebooks as whatever survived, and
    deleting makes that permanent."""
    deleted: list[str] = []
    monkeypatch.setattr(
        launch,
        "fetch_evidence",
        lambda slug, dest, deadline = None: {"notebooks": ["a"], "truncated": True},
    )
    monkeypatch.setattr(launch, "extract_reports", lambda dest: [{"passed": True}])
    api = _shared_setup_3(deleted, monkeypatch)
    record = collect.collect_one(api, _terminal_entry(), tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "pending", record
    assert deleted == []
    assert collect.statuses_from([record]) == []


def test_a_kernel_another_collector_finished_first_posts_nothing(tmp_path, monkeypatch):
    """Three unserialised workflows collect on one account, and the loser of
    that race must not post an `infra` over the winner's real verdict."""

    def _gone(
        slug,
        dest,
        deadline = None,
    ):
        raise RuntimeError(
            "404 Client Error: Not Found for url: https://www.kaggle.com/api/v1/kernels/output"
        )

    deleted: list[str] = []
    monkeypatch.setattr(launch, "fetch_evidence", _gone)
    api = _shared_setup_3(deleted, monkeypatch)
    record = collect.collect_one(api, _terminal_entry(), tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "gone", record
    assert deleted == [] and collect.statuses_from([record]) == []


def test_an_unreadable_report_is_infra_not_a_crash(tmp_path, monkeypatch):
    """Raising here writes no result and wedges every later pass. Nothing was
    learned, so `infra`, and the kernel is released."""

    def _boom(dest):
        raise AttributeError("'list' object has no attribute 'get'")

    monkeypatch.setattr(
        launch,
        "fetch_evidence",
        lambda slug, dest, deadline = None: {"notebooks": ["a"], "truncated": False},
    )
    monkeypatch.setattr(launch, "extract_reports", _boom)
    monkeypatch.setattr(launch, "delete_kernel", lambda slug, deadline = None: True)
    api = _StubApi([], {"me/unsloth-t4-ci-nabcdef01-1111": "COMPLETE"})
    record = collect.collect_one(api, _terminal_entry(), tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "infra" and "could not be read" in record["reason"]
    assert record["deleted"] is True


def test_a_malformed_report_line_is_skipped_by_the_extractor(tmp_path):
    (tmp_path / "kernel.log").write_text(
        'T4_SMOKE_REPORT []\nT4_SMOKE_REPORT "text"\nT4_SMOKE_REPORT {"label": "ok", "passed": true}\n',
        encoding = "utf-8",
    )
    assert launch.extract_reports(tmp_path) == [{"label": "ok", "passed": True}]


def test_the_expected_report_count_travels_inside_the_kernel(tmp_path, monkeypatch):
    """The scheduled collector has no workflow output to read the payload count from, so the count
    travels in the driver's sentinel record; a default of one would pass a run that lost four."""

    def _fetch(
        slug,
        dest,
        deadline = None,
    ):
        dest.mkdir(parents = True, exist_ok = True)
        (dest / "kernel.log").write_text(
            'KAGGLE_T4_CI_DRIVER_EXPECT {"reports": 5}\nT4_SMOKE_REPORT {"label": "a", "passed": true}\n',
            encoding = "utf-8",
        )
        return {"notebooks": [], "log": "kernel.log", "truncated": False}

    monkeypatch.setattr(launch, "fetch_evidence", _fetch)
    monkeypatch.setattr(launch, "delete_kernel", lambda slug, deadline = None: True)
    api = _StubApi([], {"me/unsloth-t4-ci-nabcdef01-1111": "COMPLETE"})
    record = collect.collect_one(api, _terminal_entry(), tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["expected"] == 5
    assert record["verdict"] == "partial", record
    (tmp_path / "plain").mkdir()
    assert collect.expected_reports(tmp_path / "plain", 3) == 3


def test_the_built_driver_records_its_own_expected_report_count():
    sys.path.insert(0, str(CI_DIR))
    import build_kernel

    payloads = {
        "t4_a.ipynb": {"cells": []},
        "t4_b.ipynb": {"cells": []},
        "studio_install.ipynb": {"cells": []},
    }

    def _source(nb):
        return "".join("".join(cell.get("source", [])) for cell in nb["cells"])

    driver = _source(build_kernel.build_driver(payloads, 60, cpu_lane = "studio_install.ipynb"))
    assert 'KAGGLE_T4_CI_DRIVER_EXPECT " + json.dumps({"reports": 2})' in driver
    plain = _source(build_kernel.build_driver({"t4_a.ipynb": {"cells": []}}, 60))
    assert 'json.dumps({"reports": 1})' in plain


def test_the_scheduled_collector_no_longer_guesses_the_payload_count():
    body = COLLECT_WF.read_text(encoding = "utf-8")
    assert (
        "--expect" not in body
    ), "the scheduled collector still applies one flat --expect to every kernel"


def test_a_kernel_far_past_the_ceiling_is_still_seen(monkeypatch):
    """A pass delayed six hours by an outage must still find a wedged kernel, since ageing it out of
    the listing would leave it billing with its commit pending forever."""
    from datetime import datetime, timedelta

    now = datetime(2026, 9, 6, 12, 0, 0)
    old = _StubKernel("me/unsloth-t4-ci-nabcdef01-1111", last_run_time = now - timedelta(hours = 30))
    api = _StubApi([old], {})
    found = collect.find_ours(api, now = now, max_age_hours = 3.0)
    assert [f["slug"] for f in found] == ["me/unsloth-t4-ci-nabcdef01-1111"]
    assert found[0]["age_hours"] == pytest.approx(30.0)


class _PagedApi(_StubApi):
    """kernels_list answering from a list of pages."""

    def __init__(self, pages, statuses):
        super().__init__([], statuses)
        self._pages = pages
        self.pages_asked: list[int] = []

    def kernels_list(
        self,
        mine = True,
        page = 1,
        page_size = 100,
        sort_by = "dateRun",
    ):
        self.pages_asked.append(page)
        return self._pages[page - 1] if page <= len(self._pages) else []


def test_a_kernel_of_ours_below_five_hundred_newer_records_is_still_reached():
    """The account is shared and human kernels are never deleted, so a fixed
    page cap would leave a kernel of ours below it unreachable forever. The walk
    stops only at a page entirely past the horizon with none of ours on it."""
    from datetime import datetime, timedelta

    now = datetime(2026, 9, 6, 12, 0, 0)
    recent = now - timedelta(hours = 1)
    human = [
        [_StubKernel(f"me/human-notebook-{p}-{i}", last_run_time = recent) for i in range(100)]
        for p in range(6)
    ]
    ours = _StubKernel(
        "me/unsloth-t4-ci-nabcdef012345-1111", last_run_time = now - timedelta(hours = 5)
    )
    pages = human + [[ours] + [_StubKernel(f"me/old-{i}", last_run_time = recent) for i in range(99)]]
    api = _PagedApi(pages, {})
    found = collect.find_ours(api, now = now)
    assert [f["slug"] for f in found] == [ours.ref]
    assert 7 in api.pages_asked


def test_the_listing_walk_ends_at_a_page_past_the_horizon_with_none_of_ours():
    from datetime import datetime, timedelta

    now = datetime(2026, 9, 6, 12, 0, 0)
    ancient = now - timedelta(hours = collect.LISTING_HORIZON_HOURS + 1)
    pages = [
        [_StubKernel(f"me/human-{i}", last_run_time = now - timedelta(hours = 1)) for i in range(100)],
        [_StubKernel(f"me/ancient-{i}", last_run_time = ancient) for i in range(100)],
        [_StubKernel("me/unsloth-t4-ci-nabcdef012345-2222", last_run_time = ancient)],
    ]
    api = _PagedApi(pages, {})
    assert collect.find_ours(api, now = now) == []
    assert api.pages_asked == [1, 2], "the walk did not stop at the first page past the horizon"
    pages[1][50] = _StubKernel("me/unsloth-t4-ci-nabcdef012345-3333", last_run_time = ancient)
    api = _PagedApi(pages, {})
    found = collect.find_ours(api, now = now)
    assert {f["slug"] for f in found} == {
        "me/unsloth-t4-ci-nabcdef012345-3333",
        "me/unsloth-t4-ci-nabcdef012345-2222",
    }


def test_the_listing_walk_stops_at_the_pass_deadline(monkeypatch):
    from datetime import datetime, timedelta

    now = datetime(2026, 9, 6, 12, 0, 0)
    pages = [
        [
            _StubKernel(f"me/human-{p}-{i}", last_run_time = now - timedelta(hours = 1))
            for i in range(100)
        ]
        for p in range(5)
    ]
    api = _PagedApi(pages, {})
    clock = [1000.0]
    monkeypatch.setattr(collect.time, "time", lambda: clock[0])
    orig = api.kernels_list

    def slow(*a, **k):
        clock[0] += 400.0
        return orig(*a, **k)

    api.kernels_list = slow
    collect.find_ours(api, now = now, deadline = 1500.0)
    assert api.pages_asked == [1, 2], api.pages_asked
    source = (CI_DIR / "collect.py").read_text(encoding = "utf-8")
    assert "find_ours(api, max_age_hours = args.max_age_hours, deadline = deadline)" in source


def test_a_reaped_kernel_is_not_reported_deleted_before_it_is(tmp_path, monkeypatch):
    """The delete happens in a later step that can be refused, and this reason
    is posted as a durable status before it runs, so it must not claim it."""
    from datetime import datetime, timedelta

    slug = "me/unsloth-t4-ci-nabcdef012345-1111"
    api = _StubApi([], {slug: "RUNNING"})
    entry = {
        "slug": slug,
        "sha": "abcdef012345",
        "kind": "notebook",
        "legacy": False,
        "age_hours": 5.0,
    }
    record = collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0, delete = False)
    assert record["verdict"] == "reaped"
    assert "released for deletion" in record["reason"] and "was deleted" not in record["reason"]
    monkeypatch.setattr(launch, "delete_kernel", lambda slug, deadline = None: True)
    record = collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0, delete = True)
    assert "was deleted" in record["reason"]
    monkeypatch.setattr(launch, "delete_kernel", lambda slug, deadline = None: False)
    record = collect.collect_one(api, entry, tmp_path, expect = 1, max_age_hours = 3.0, delete = True)
    assert "was deleted" not in record["reason"] and record["deleted"] is False


def test_a_malformed_neighbour_notebook_does_not_hide_a_failing_report(tmp_path, monkeypatch):
    """A malformed neighbour notebook must not make the reader raise, since that judged the kernel
    infra and posted success; the failing report must survive."""
    slug = _ENTRY["slug"]
    dest = tmp_path / slug.rsplit("/", 1)[-1]
    dest.mkdir()
    failing = launch.RESULT_PREFIX + json.dumps({"label": "control", "passed": False, "model": "m"})
    (dest / f"a{launch.OUTPUT_SUFFIX}").write_text(
        json.dumps({"cells": [{"outputs": [{"text": failing + "\n"}]}]}), encoding = "utf-8"
    )
    (dest / f"b{launch.OUTPUT_SUFFIX}").write_text("[]", encoding = "utf-8")
    (dest / f"c{launch.OUTPUT_SUFFIX}").write_text(
        json.dumps(
            {
                "cells": [
                    "not a cell",
                    {"outputs": ["not an output", {"text": 7}, {"text": ["x", 1]}]},
                ]
            }
        ),
        encoding = "utf-8",
    )
    assert launch.extract_reports(dest) == [{"label": "control", "passed": False, "model": "m"}]
    monkeypatch.setattr(launch, "fetch_evidence", lambda slug, dest, deadline = None: {})
    monkeypatch.setattr(launch, "delete_kernel", lambda slug, deadline = None: True)
    api = _StubApi([], {slug: "COMPLETE"})
    record = collect.collect_one(api, dict(_ENTRY), tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "fail", record
    assert "report_error" not in record


def test_the_scheduled_collector_fails_loudly_when_it_cannot_authenticate(
    tmp_path, monkeypatch, capsys
):
    """Its token is the repository's own, so a green pass collecting nothing
    hides an expired credential while kernels bill and commits stay pending."""

    def _no(*a, **k):
        raise OSError("Could not find kaggle.json")

    monkeypatch.setattr(launch, "_api", _no)
    monkeypatch.setenv("KAGGLE_API_TOKEN", "a-token-kaggle-refused")
    monkeypatch.setattr(sys, "argv", ["collect", "--outdir", str(tmp_path), "--require-auth"])
    assert collect.main() == 1
    assert "Kaggle authentication failed" in capsys.readouterr().out
    monkeypatch.setattr(sys, "argv", ["collect", "--outdir", str(tmp_path)])
    assert collect.main() == 0, "a fork job with the secret withheld is still a skip"


def test_an_unconfigured_collector_account_warns_and_passes(tmp_path, monkeypatch, capsys):
    """An empty Kaggle token is an absent account: warn and pass, but a token that is refused stays red."""

    def _no(*a, **k):
        raise OSError("Could not find kaggle.json")

    monkeypatch.setattr(launch, "_api", _no)
    monkeypatch.delenv("KAGGLE_API_TOKEN", raising = False)
    monkeypatch.setattr(sys, "argv", ["collect", "--outdir", str(tmp_path), "--require-auth"])
    assert collect.main() == 0
    out = capsys.readouterr().out
    assert "::warning title=Kaggle account not configured" in out
    assert "::error" not in out
    monkeypatch.setenv("KAGGLE_API_TOKEN", "")
    assert collect.main() == 0
    monkeypatch.setenv("KAGGLE_API_TOKEN", "present")
    assert collect.main() == 1
    body = COLLECT_WF.read_text(encoding = "utf-8")
    assert "EMPTY token" in body


def test_the_scheduled_workflow_asks_for_that():
    body = COLLECT_WF.read_text(encoding = "utf-8")
    assert "--require-auth" in body


def test_the_collector_installs_the_client_the_gpu_workflows_install():
    """1.7.4.5 cannot read KAGGLE_API_TOKEN at all (#9535), so a scheduled pass
    on it authenticated nothing and, before --require-auth, said so to nobody."""
    pins = {}
    for path in (NOTEBOOK_WF, STUDIO_WF, COLLECT_WF):
        found = re.findall(
            r"pip install [^\n]*'kaggle==([0-9][^']*)'", path.read_text(encoding = "utf-8")
        )
        assert found, f"{path.name} pins no kaggle client"
        pins[path.name] = set(found)
    assert (
        len(set().union(*pins.values())) == 1
    ), f"the workflows disagree on the kaggle client: {pins}"


def test_every_kaggle_client_install_also_installs_six():
    """kaggle 2.2.x imports six in kaggle/models/upload_file.py without declaring
    it. python-dateutil < 4 brought it in; 4.0 dropped it, and every collector
    pass then died on ModuleNotFoundError before it reached the token."""
    for path in (NOTEBOOK_WF, STUDIO_WF, COLLECT_WF):
        lines = re.findall(r"pip install [^\n]*'kaggle==[^\n]*", path.read_text(encoding = "utf-8"))
        assert lines, f"{path.name} pins no kaggle client"
        for line in lines:
            assert re.search(r"'six(?:[=<>!~][^']*)?'", line), f"{path.name}: {line}"


def test_a_client_that_cannot_import_is_not_reported_as_a_bad_token(tmp_path, monkeypatch, capsys):
    """An ImportError means the token was never tried; blaming it sends the
    reader to rotate a secret that is fine while the install stays broken."""

    def _no(*a, **k):
        raise ModuleNotFoundError("No module named 'six'", name = "six")

    monkeypatch.setattr(launch, "_api", _no)
    monkeypatch.setenv("KAGGLE_API_TOKEN", "a-token-that-is-fine")
    monkeypatch.setattr(sys, "argv", ["collect", "--outdir", str(tmp_path), "--require-auth"])
    assert collect.main() == 1
    out = capsys.readouterr().out
    assert "Kaggle client import failed" in out
    assert "No module named 'six'" in out
    assert "Kaggle authentication failed" not in out
    assert "a-token-that-is-fine" not in out


def test_dispatch_refuses_a_ref_that_is_not_a_commit(monkeypatch, capsys):
    """slug_name falls back to the legacy unattributable form for anything not hex, so dispatching a
    branch would push a kernel that runs, bills and reports to nobody."""
    monkeypatch.setattr(
        sys,
        "argv",
        [
            "launch",
            "--notebook",
            "k.ipynb",
            "--outdir",
            "out",
            "--dispatch",
            "--kind",
            "studio",
            "--commit-sha",
            "main",
        ],
    )
    with pytest.raises(SystemExit) as exc:
        launch.main()
    assert exc.value.code == 2
    assert "hex commit id" in capsys.readouterr().err


def test_the_studio_workflow_resolves_its_ref_to_a_commit_before_dispatching():
    """workflow_dispatch accepts any `unsloth_ref`; the notebook workflow
    already resolved it, the Studio one passed it straight through."""
    for path in (NOTEBOOK_WF, STUDIO_WF):
        wf = _wf(path)
        gate_ref = next(s for s in wf["jobs"]["gate"]["steps"] if s.get("id") == "ref")
        assert (
            "git ls-remote" in gate_ref["run"] and "[0-9a-f]{40}" in gate_ref["run"]
        ), f"{path.name}: the gate does not resolve refs"
        step = next(s for _j, n, s in _steps(wf) if n == "Resolve the ref under test")
        run = step["run"]
        # Resolved once by the gate: re-resolving a branch later can name a different commit.
        assert step["env"]["GATE_SHA"] == "${{ needs.gate.outputs.head_sha }}"
        assert (
            'RESOLVED="$GATE_SHA"' in run and "$(git ls-remote" not in run
        ), f"{path.name}: the GPU job resolves the ref a second time"
        assert wf["jobs"]["gate"]["outputs"]["head_sha"] == "${{ steps.ref.outputs.head_sha }}"
        assert (
            "exit 1" in run or "stand_down=true" in run
        ), f"{path.name} dispatches on an unresolved ref"
        # Only fetching the object proves the repo has the SHA.
        assert (
            "git fetch --quiet --depth=1 https://github.com/unslothai/unsloth" in run
        ), f"{path.name} dispatches a full SHA without checking the repository serves it"


@pytest.mark.parametrize("path", (NOTEBOOK_WF, STUDIO_WF), ids = ("notebook", "studio"))
def test_pending_is_posted_only_for_a_kernel_that_was_actually_dispatched(path):
    """launch.py exits 0 on every stand-down, and a pending status for a kernel
    that does not exist is one no collector can ever replace."""
    step = next(
        s for _j, n, s in _steps(_wf(path)) if n == "Mark the dispatch pending on this commit"
    )
    assert step.get("if") == "steps.launch.outputs.verdict == 'dispatched'", step.get("if")


def test_a_lookup_that_could_not_be_made_keeps_the_kernel(monkeypatch, capsys):
    """Only GitHub saying the commit is missing releases a kernel; a 5xx or dropped connection says
    nothing, and reading it as gone would delete the only copy of the result."""
    calls = _fake_gh(monkeypatch, lookup_error = "gh: HTTP 503 Service Unavailable")
    outcome = post_statuses.post_all([dict(_STATUS)], "unslothai/unsloth")
    assert outcome["failed"] == [_STATUS["slug"]]
    assert outcome["unresolved"] == []
    assert not any("/statuses/" in c[1] for c in calls)
    assert "Commit lookup failed" in capsys.readouterr().out


def test_resolve_sha_separates_missing_from_unknown(monkeypatch):
    """Measured: a commit that is not there answers HTTP 422 "No commit found
    for SHA", not a 404. Anything else is an unknown, never a missing."""
    answers = {}

    def _gh(args):
        return answers[args[1]]

    monkeypatch.setattr(post_statuses, "_gh", _gh)
    answers["repos/o/r/commits/aa"] = (0, "a" * 40, "")
    answers["repos/o/r/commits/bb"] = (1, "", "gh: No commit found for SHA: bb (HTTP 422)")
    answers["repos/o/r/commits/cc"] = (1, "", "gh: HTTP 502 Bad Gateway")
    answers["repos/o/r/commits/dd"] = (1, "", "")
    # 404 can also mean an unreadable repo, and 422 an ambiguous abbreviation.
    answers["repos/o/r/commits/ee"] = (1, "", "gh: Not Found (HTTP 404)")
    answers["repos/o/r/commits/ff"] = (1, "", "gh: Validation Failed (HTTP 422)")
    assert post_statuses.resolve_sha("o/r", "aa") == ("ok", "a" * 40)
    assert post_statuses.resolve_sha("o/r", "bb") == ("missing", None)
    for sha in ("cc", "dd", "ee", "ff"):
        assert post_statuses.resolve_sha("o/r", sha) == ("error", None), sha


_ENTRY = {
    "slug": "me/unsloth-t4-ci-nabcdef01-1111",
    "sha": "abcdef01",
    "kind": "notebook",
    "legacy": False,
    "age_hours": 0.4,
}


def test_an_unrecognised_kernel_state_is_kept_and_not_judged(tmp_path, monkeypatch):
    """Kaggle's enum can grow, and a non-terminal state has no evidence: judging
    one posts a green `infra` for a run still to come, then deletes it."""
    touched: list[str] = []
    monkeypatch.setattr(
        launch, "fetch_evidence", lambda slug, dest, deadline = None: touched.append(slug) or {}
    )
    monkeypatch.setattr(
        launch, "delete_kernel", lambda slug, deadline = None: touched.append("delete") or True
    )
    api = _StubApi([], {_ENTRY["slug"]: "NEW_SCRIPT"})
    record = collect.collect_one(api, dict(_ENTRY), tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "pending"
    assert record["verdict"] not in collect.DELETABLE
    assert collect.statuses_from([record]) == []
    assert touched == [], "an unjudged kernel must be neither downloaded nor deleted"


def test_a_downloaded_file_that_is_not_a_notebook_does_not_wedge_the_collector(
    tmp_path, monkeypatch
):
    """An empty JSON list is valid JSON but not a notebook; the expected-count reader must not raise
    on it, since a raising collector writes no result and stalls every pass."""
    slug = _ENTRY["slug"]
    dest = tmp_path / slug.rsplit("/", 1)[-1]
    dest.mkdir()
    (dest / f"x{launch.OUTPUT_SUFFIX}").write_text("[]", encoding = "utf-8")
    (dest / f"y{launch.OUTPUT_SUFFIX}").write_text(
        json.dumps({"cells": ["not a cell", {"outputs": ["not an output", {"text": 7}]}]}),
        encoding = "utf-8",
    )
    monkeypatch.setattr(launch, "fetch_evidence", lambda slug, dest, deadline = None: {})
    monkeypatch.setattr(launch, "delete_kernel", lambda slug, deadline = None: True)
    api = _StubApi([], {slug: "COMPLETE"})
    assert collect.expected_reports(dest, 4) == 4
    record = collect.collect_one(api, dict(_ENTRY), tmp_path, expect = 4, max_age_hours = 3.0)
    assert record["verdict"] == "infra", record
    assert record["expected"] is None


def test_the_evidence_download_is_clamped_to_the_pass_deadline(tmp_path, monkeypatch):
    """A kernel started just inside the pass budget must not get a fresh five-minute download
    budget, or the collector overruns the job before the release step."""
    seen: list[float] = []

    def _fetch(
        slug,
        dest,
        deadline = None,
    ):
        seen.append(deadline)
        return {}

    monkeypatch.setattr(launch, "fetch_evidence", _fetch)
    monkeypatch.setattr(launch, "extract_reports", lambda dest: [{"passed": True}])
    monkeypatch.setattr(launch, "delete_kernel", lambda slug, deadline = None: True)
    api = _StubApi([], {_ENTRY["slug"]: "COMPLETE"})
    pass_deadline = time.time() + 5.0
    collect.collect_one(
        api, dict(_ENTRY), tmp_path, expect = 1, max_age_hours = 3.0, deadline = pass_deadline
    )
    assert seen and seen[0] <= pass_deadline
    source = (CI_DIR / "collect.py").read_text(encoding = "utf-8")
    body = source[source.index("deadline = time.time() + BUDGET_SEC") :]
    assert "deadline = deadline," in body[: body.index("statuses_from")]


@pytest.mark.parametrize("path", (NOTEBOOK_WF, STUDIO_WF), ids = ("notebook", "studio"))
def test_collection_runs_before_the_recheck_so_the_recheck_is_last_before_the_push(path):
    """Collection can spend up to BUDGET_SEC downloading, and between the
    recheck and the push that gap is the stale window the recheck exists to
    close: the other GPU workflow can take the last session in it."""
    steps = [(n, s) for _j, n, s in _steps(_wf(path))]
    names = [n for n, _s in steps]
    collect_i = names.index("Collect finished Kaggle runs")
    recheck_i = names.index("Recheck the Kaggle account")
    launch_i = names.index("Dispatch to Kaggle")
    assert collect_i < recheck_i < launch_i, names[collect_i : launch_i + 1]
    assert "steps.recheck" not in (steps[collect_i][1].get("if") or "")
    between = [n for n in names[recheck_i + 1 : launch_i]]
    assert not any("collect.py" in (s.get("run") or "") for n, s in steps if n in between), between
    assert "steps.recheck.outputs.should_run == 'true'" in steps[launch_i][1]["if"]


@pytest.mark.parametrize("path", (NOTEBOOK_WF, STUDIO_WF), ids = ("notebook", "studio"))
def test_the_unpack_step_reads_the_tree_a_collected_kernel_lands_in(path):
    """A collected kernel lands in kaggle_collected/<slug>, so the unpack step must read that tree
    as well as kaggle_evidence or it never fires for collected screenshots."""
    step = next(s for _j, n, s in _steps(_wf(path)) if n == "Unpack the Playwright evidence")
    assert "kaggle_collected/**/*_output.ipynb" in step["if"]
    assert "kaggle_evidence/**/*_output.ipynb" in step["if"]
    run = step["run"]
    assert "kaggle_collected/*/" in run and "kaggle_evidence/*/" in run
    # One kernel per call: chunks are numbered per bundle.
    assert '--evidence "$dir"' in run and 'studio_evidence/$(basename "$dir")' in run


def test_the_scheduled_collector_unpacks_and_uploads_what_it_collected():
    """It collects most Studio kernels, so it is the pass that has to put the
    screenshots back and keep them somewhere."""
    steps = [(n, s) for _j, n, s in _steps(_wf(COLLECT_WF))]
    names = [n for n, _s in steps]
    unpack = steps[names.index("Unpack the Playwright evidence")][1]
    assert "kaggle_collected/*/" in unpack["run"] and "collect_evidence.py" in unpack["run"]
    upload = steps[names.index("Upload evidence")][1]
    assert upload["uses"].startswith("actions/upload-artifact@")
    assert "kaggle_collected/**" in upload["with"]["path"]
    assert "studio_evidence/**" in upload["with"]["path"]
    assert upload.get("continue-on-error") is True, "an artifact outage is not a collection failure"
    assert names.index("Upload evidence") > names.index("Post the commit statuses")


def test_a_listed_notebook_without_a_download_url_marks_the_evidence_incomplete(
    tmp_path, monkeypatch
):
    """Skipping it silently left `truncated` false, so the short set was judged,
    posted and its kernel deleted: a failing payload lost for good."""
    listing = {
        "files": [{"fileName": f"a{launch.OUTPUT_SUFFIX}"}],
        "log": "",
        "truncated": False,
    }
    monkeypatch.setattr(launch, "list_outputs", lambda slug, timeout = None, deadline = None: listing)
    evidence = launch.fetch_evidence("me/k", tmp_path / "k", deadline = time.time() + 60)
    assert evidence["notebooks"] == []
    assert evidence["truncated"] is True


def test_old_and_new_slug_forms_for_one_commit_post_one_status(monkeypatch):
    """An 8 and a 12 character slug can name one commit in one pass, and only
    the commits API can say so: the collector keeps them apart and the POSTER
    merges what resolves alike, failure winning, both slugs named."""
    full = "abcdef012345" + "0" * 28
    calls: list[list[str]] = []

    def _gh(args):
        calls.append(list(args))
        if "/commits/" in args[1]:
            return (0, full, "")
        return (0, "", "")

    monkeypatch.setattr(post_statuses, "_gh", _gh)
    records = [
        {
            "slug": "me/unsloth-t4-ci-nabcdef012345-2222",
            "sha": "abcdef012345",
            "kind": "notebook",
            "verdict": "fail",
            "reason": "leg red",
        },
        {
            "slug": "me/unsloth-t4-ci-nabcdef01-1111",
            "sha": "abcdef01",
            "kind": "notebook",
            "verdict": "pass",
            "reason": "ok",
        },
    ]
    statuses = collect.statuses_from(records)
    assert len(statuses) == 2, "the collector cannot know these are one commit"
    for order in (statuses, statuses[::-1]):
        calls.clear()
        outcome = post_statuses.post_all([dict(s) for s in order], "unslothai/unsloth")
        posts = [c for c in calls if "/statuses/" in c[1]]
        assert len(posts) == 1, posts
        assert "state=failure" in " ".join(posts[0])
        assert sorted(outcome["ok"]) == sorted(r["slug"] for r in records)


def test_two_commits_that_merely_share_eight_characters_post_two_statuses():
    """The reason the slug grew to twelve characters: merged, one commit loses
    its verdict and has its kernel deleted as delivered."""
    records = [
        {
            "slug": "me/unsloth-t4-ci-nabcdef012345-1111",
            "sha": "abcdef012345",
            "kind": "notebook",
            "verdict": "pass",
            "reason": "ok",
        },
        {
            "slug": "me/unsloth-t4-ci-nabcdef01ffff-2222",
            "sha": "abcdef01ffff",
            "kind": "notebook",
            "verdict": "fail",
            "reason": "leg red",
        },
    ]
    statuses = collect.statuses_from(records)
    assert len(statuses) == 2, statuses
    assert {s["sha"]: s["state"] for s in statuses} == {
        "abcdef012345": "success",
        "abcdef01ffff": "failure",
    }
    assert all(len(s["slugs"]) == 1 for s in statuses)


def test_the_release_phase_stops_at_its_budget_and_keeps_the_rest(tmp_path, monkeypatch):
    """delete_kernel allows three 180-second attempts per kernel, so an unbounded release could
    outlive the job; what is left over is released by the next pass."""
    clock = [1000.0]
    monkeypatch.setattr(collect.time, "time", lambda: clock[0])

    def _slow_delete(slug, deadline = None):
        clock[0] += collect.RELEASE_BUDGET_SEC  # one delete eats the whole budget
        return True

    monkeypatch.setattr(launch, "delete_kernel", _slow_delete)
    kernels = [
        {"slug": f"me/unsloth-t4-ci-nabcdef01234{i}-1111", "verdict": "pass"} for i in range(3)
    ]
    result = tmp_path / "collect_result.json"
    result.write_text(json.dumps({"kernels": kernels, "statuses": []}), encoding = "utf-8")
    posted = tmp_path / "posted.json"
    posted.write_text(json.dumps({"ok": [], "failed": []}), encoding = "utf-8")
    assert collect.delete_collected(result, posted) == 0
    outcome = json.loads((tmp_path / "delete_result.json").read_text())
    assert len(outcome["deleted"]) == 1 and len(outcome["kept"]) == 2, outcome


def test_the_scheduled_collector_timeout_covers_collection_and_release():
    job = _wf(COLLECT_WF)["jobs"]["collect"]
    floor = (collect.BUDGET_SEC + collect.RELEASE_BUDGET_SEC) / 60 + 3
    assert job["timeout-minutes"] >= floor, (job["timeout-minutes"], floor)


def test_nothing_tells_the_reader_to_require_a_sampled_context():
    """The gate samples and the workflows path-filter, so a skipped commit never gets a status and a
    required context that never arrives blocks the merge."""
    for path in (NOTEBOOK_WF, STUDIO_WF, COLLECT_WF, CI_DIR / "launch.py", CI_DIR / "collect.py"):
        assert "branch protection must require" not in path.read_text(encoding = "utf-8"), path


def test_a_delete_is_clamped_to_the_deadline_it_is_handed(monkeypatch):
    """The deadline must bound each attempt, not just the call: two deletes of three 180-second
    attempts outlive a 600-second budget, so every attempt is clamped to what is left."""
    clock = [1000.0]
    timeouts: list[float] = []

    class _Proc:
        returncode = 1
        stdout = ""
        stderr = "503"

    def _run(
        cmd,
        capture_output = True,
        text = True,
        timeout = None,
    ):
        timeouts.append(timeout)
        clock[0] += timeout  # the call uses its whole allowance
        return _Proc()

    monkeypatch.setattr(launch.subprocess, "run", _run)
    monkeypatch.setattr(launch.time, "time", lambda: clock[0])
    monkeypatch.setattr(launch.time, "sleep", lambda s: clock.__setitem__(0, clock[0] + s))
    assert launch.delete_kernel("me/k", deadline = 1000.0 + 200.0) is False
    assert timeouts and timeouts[0] <= 200.0
    assert sum(timeouts) <= 200.0, timeouts
    assert len(timeouts) < launch.DELETE_ATTEMPTS, "an attempt started past the deadline"


def test_the_release_hands_every_delete_its_deadline(tmp_path, monkeypatch):
    seen: list = []
    monkeypatch.setattr(
        launch, "delete_kernel", lambda slug, deadline = None: seen.append(deadline) or True
    )
    result = tmp_path / "collect_result.json"
    result.write_text(
        json.dumps(
            {
                "kernels": [{"slug": "me/unsloth-t4-ci-nabcdef012345-1111", "verdict": "pass"}],
                "statuses": [],
            }
        ),
        encoding = "utf-8",
    )
    posted = tmp_path / "posted.json"
    posted.write_text(json.dumps({"ok": [], "failed": []}), encoding = "utf-8")
    collect.delete_collected(result, posted)
    assert seen and seen[0] is not None and seen[0] <= time.time() + collect.RELEASE_BUDGET_SEC


def test_the_scheduled_collector_uploads_once_and_reports_real_deletions():
    steps = [(n, s) for _j, n, s in _steps(_wf(COLLECT_WF))]
    uploads = [
        s for _n, s in steps if str(s.get("uses", "")).startswith("actions/upload-artifact@")
    ]
    assert len(uploads) == 1, "two uploads: the second one fails the job on an artifact outage"
    assert uploads[0].get("continue-on-error") is True
    assert "kaggle_collected/**" in uploads[0]["with"]["path"]
    summary = next(s for n, s in steps if n == "Summarise")
    assert (
        "delete_result.json" in summary["run"]
    ), "the summary reports the collect step's always-false deleted flag"


def test_a_kernel_whose_status_record_was_rejected_is_kept(tmp_path, monkeypatch):
    """The poster refuses a malformed record and posts nothing for it. Keeping
    only the refused posts released that kernel with no status delivered."""
    deleted = _shared_setup_1(monkeypatch)
    slug = "me/unsloth-t4-ci-nabcdef012345-1111"
    result = tmp_path / "collect_result.json"
    result.write_text(
        json.dumps({"kernels": [{"slug": slug, "verdict": "pass"}], "statuses": []}),
        encoding = "utf-8",
    )
    posted = tmp_path / "posted.json"
    posted.write_text(json.dumps({"ok": [], "failed": [], "invalid": [slug]}), encoding = "utf-8")
    collect.delete_collected(result, posted)
    assert deleted == []
    outcome = json.loads((tmp_path / "delete_result.json").read_text())
    assert outcome["kept"] == [slug]


def test_the_pass_budget_starts_before_the_kernel_listing():
    """Five slow listing pages under the socket timeout are minutes. A budget
    started after them is that much later than the job timeout was sized for."""
    source = (CI_DIR / "collect.py").read_text(encoding = "utf-8")
    body = source[source.index("def main(") :]
    assert body.index("deadline = time.time() + BUDGET_SEC") < body.index("ours = find_ours(")


def test_two_commits_that_share_eight_characters_resolve_apart_and_post_twice(monkeypatch):
    """The transition case: an old 8 character slug for commit A and a 12
    character one for a commit B starting with the same eight. Two commits."""
    a_full = "abcdef01" + "a" * 32
    b_full = "abcdef01ffff" + "b" * 28
    calls: list[list[str]] = []

    def _gh(args):
        calls.append(list(args))
        if "/commits/" in args[1]:
            sha = args[1].rsplit("/", 1)[-1]
            return (0, a_full if sha == "abcdef01" else b_full, "")
        return (0, "", "")

    monkeypatch.setattr(post_statuses, "_gh", _gh)
    statuses = collect.statuses_from(
        [
            {
                "slug": "me/unsloth-t4-ci-nabcdef01-1111",
                "sha": "abcdef01",
                "kind": "notebook",
                "verdict": "pass",
                "reason": "ok",
            },
            {
                "slug": "me/unsloth-t4-ci-nabcdef01ffff-2222",
                "sha": "abcdef01ffff",
                "kind": "notebook",
                "verdict": "fail",
                "reason": "red",
            },
        ]
    )
    outcome = post_statuses.post_all(statuses, "unslothai/unsloth")
    posts = [c for c in calls if "/statuses/" in c[1]]
    assert len(posts) == 2
    assert {c[1].rsplit("/", 1)[-1] for c in posts} == {a_full, b_full}
    assert len(outcome["ok"]) == 2


def test_a_notebook_kernel_without_its_expected_count_is_never_a_pass(tmp_path, monkeypatch):
    """A notebook kernel without its expected-count record could have lost four legs yet reported
    one, which a default of one reads as a pass; a clean set must be infra, failures still fail."""
    monkeypatch.setattr(launch, "fetch_evidence", lambda slug, dest, deadline = None: {})
    monkeypatch.setattr(launch, "delete_kernel", lambda slug, deadline = None: True)
    api = _StubApi([], {_ENTRY["slug"]: "COMPLETE"})
    monkeypatch.setattr(launch, "extract_reports", lambda dest: [{"passed": True}])
    record = collect.collect_one(api, dict(_ENTRY), tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "infra" and record["expected"] is None, record
    monkeypatch.setattr(launch, "extract_reports", lambda dest: [{"passed": False, "payload": "x"}])
    record = collect.collect_one(api, dict(_ENTRY), tmp_path, expect = 1, max_age_hours = 3.0)
    assert record["verdict"] == "fail"
    monkeypatch.setattr(launch, "extract_reports", lambda dest: [{"passed": True}])
    studio = dict(_ENTRY, kind = "studio", slug = "me/unsloth-t4-ci-sabcdef01-1111")
    api = _StubApi([], {studio["slug"]: "COMPLETE"})
    assert (
        collect.collect_one(api, studio, tmp_path, expect = 1, max_age_hours = 3.0)["verdict"] == "pass"
    )


def test_every_gate_budget_covers_the_reaper_window():
    """A gate budget shorter than the reaper window lets a wedged kernel bill into the reserve."""
    collect_wf = _wf(COLLECT_WF)
    job = collect_wf["jobs"]["collect"]
    on = collect_wf.get("on") or collect_wf.get(True) or {}  # PyYAML reads a bare `on:` as True
    crons = [s["cron"] for s in on.get("schedule") or []]
    assert crons, "the collector is not scheduled"
    minutes = sorted(int(m) for m in crons[0].split()[0].split(","))
    interval_min = max(b - a for a, b in zip(minutes, minutes[1:])) if len(minutes) > 1 else 60
    source = COLLECT_WF.read_text(encoding = "utf-8")
    m = re.search(r"--max-age-hours '\$\{\{ inputs\.max_age_hours \|\| '(\d+)' \}\}'", source)
    assert m, "the collector's default age ceiling is not readable from the workflow"
    max_age = float(m.group(1))
    assert max_age == collect.DEFAULT_MAX_AGE_HOURS
    window = max_age + (interval_min + job["timeout-minutes"]) / 60
    for path in (NOTEBOOK_WF, STUDIO_WF):
        budgets = {
            int(b) for b in re.findall(r"--budget-hours (\d+)", path.read_text(encoding = "utf-8"))
        }
        assert len(budgets) == 1, (path.name, budgets)
        assert (
            budgets.pop() >= window
        ), f"{path.name}: a wedged kernel can bill {window:.2f}h before it is reaped"