Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ loading the delivery [commands](../src/loopzero/cli.py), which implement the con
with the actual merge tree. Unavailable history blocks inherited coverage.
Ancestor coverage includes outdated unresolved blocking threads. A primary
that requested changes also requires resolved findings at the reviewed head.
For check runs with the same name, numeric run IDs take precedence. Equal
or missing IDs fall back to the parsed start instant, with usable times ahead
of missing or invalid times. Ties keep API order. A retry of the same run
supersedes a failure for waiting purposes only when it starts strictly after
the failure completed. A different run of the same workflow compares creation
instants first, then numeric IDs when those instants are equal.
After its own draft-to-ready write,
`ready --wait` revalidates through the same waiter and original deadline.
Each poll resolves the trusted workflow ref, reusing parsed policy when the
Expand Down
23 changes: 12 additions & 11 deletions src/loopzero/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,29 +1270,30 @@ def find_producer(runs: list[dict]) -> dict | None:
producer = find_producer(_paged(f"repos/{repo}/actions/runs?check_suite_id={suite_id}", "workflow_runs"))
if producer is None or not producer.get("workflow_id"):
return False
created = producer.get("created_at")
created = readiness_timestamp(producer.get("created_at"))
for workflow in workflows:
if (workflow.get("head_sha") != head_sha
or workflow.get("workflow_id") != producer["workflow_id"]
or workflow.get("status") not in _ACTIVE_WORKFLOWS):
continue
if workflow.get("id") == producer.get("id"):
if (workflow.get("run_attempt", 1) > 1 and check.get("completed_at")
and workflow.get("run_started_at")
and workflow["run_started_at"] > check["completed_at"]):
if (workflow.get("run_attempt", 1) > 1
and (completed := readiness_timestamp(check.get("completed_at"))) is not None
and (started := readiness_timestamp(workflow.get("run_started_at"))) is not None
and started > completed):
return True
elif (created and isinstance(producer.get("id"), int)
and workflow.get("created_at") and isinstance(workflow.get("id"), int)
and (workflow["created_at"], workflow["id"]) > (created, producer["id"])):
elif (created is not None and isinstance(producer.get("id"), int)
and (replacement := readiness_timestamp(workflow.get("created_at"))) is not None
and isinstance(workflow.get("id"), int)
and (replacement, workflow["id"]) > (created, producer["id"])):
return True
return False


def _check_run_order(run: dict) -> tuple[int, str]:
def _check_run_order(run: dict) -> tuple[int, bool, ReadinessTimestamp | None]:
run_id = run.get("id")
started_at = run.get("started_at")
return (run_id if isinstance(run_id, int) else -1,
started_at if isinstance(started_at, str) else "")
started_at = readiness_timestamp(run.get("started_at"))
return run_id if isinstance(run_id, int) else -1, started_at is not None, started_at


def _aggregate_signal(states: list[str]) -> str:
Expand Down
51 changes: 32 additions & 19 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,15 +872,24 @@ def test_check_runs_merges_runs_and_statuses(gh: FakeGh) -> None:
assert pages == ["page=1", "page=2"]


@pytest.mark.parametrize(("runs", "expected"), [
([check_run("skipped", id=10), check_run("success", id=11)], {"checks": "success"}),
([check_run(None, id=20, status="in_progress"), check_run("success", id=19)], {"checks": "pending"}),
([check_run("success", id=30), check_run("failure", name="lint", id=31)],
{"checks": "success", "lint": "failure"}),
([check_run("failure", id=40, started_at="2026-09-18T01:00:00Z"),
check_run("success", id=40, started_at="2026-09-18T02:00:00Z")], {"checks": "success"}),
], ids=["stale-skipped", "newer-pending", "distinct-names", "timestamp-tie"])
def test_check_runs_ordering(gh: FakeGh, runs: list[dict], expected: dict) -> None:
@pytest.mark.parametrize("rows,expected", [
([(10, "checks", None, "skipped"), (11, "checks", None, "success")], {"checks": "success"}),
([(20, "checks", "01:00:00Z", None), (19, "checks", "02:00:00Z", "success")], {"checks": "pending"}),
([(30, "checks", None, "success"), (31, "lint", None, "failure")], {"checks": "success", "lint": "failure"}),
([(40, "checks", "01:00:00Z", "failure"), (40, "checks", "02:00:00Z", "success")], {"checks": "success"}),
([(7, "checks", "12:00:00Z", "success"), (7, "checks", "12:00:00.250Z", "failure")], {"checks": "failure"}),
([(None, "checks", "12:00:00Z", "success"), (None, "checks", "09:00:01-03:00", "failure")], {"checks": "failure"}),
([(7, "checks", "12:00:00Z", "failure"), (7, "checks", "14:00:00+02:00", "success")], {"checks": "failure"}),
([(7, "checks", "12:00:00Z", "failure"), (7, "checks", "23:00:00+00:99", "success")], {"checks": "failure"}),
([(None, "checks", None, "failure"), (None, "checks", "23:00:00", "success")], {"checks": "failure"}),
], ids=[
"stale-skipped", "id-before-time", "distinct-names", "timestamp-tie", "same-id-fraction",
"missing-ids-offset", "equal-instants", "invalid-loses-to-valid", "unusable-times-tie",
])
def test_check_runs_ordering(gh: FakeGh, rows: list[tuple], expected: dict) -> None:
runs = [check_run(conclusion, name=name, status="completed" if conclusion else "in_progress",
started_at=stamp and f"2026-09-23T{stamp}", **({"id": number} if number is not None else {}))
for number, name, stamp, conclusion in rows]
arm_check_pages(gh, {"check_runs": runs})
assert github._check_signals(REPO, HEAD)[0] == expected

Expand Down Expand Up @@ -1023,13 +1032,16 @@ def test_readiness_missing_and_failed_checks(gh: FakeGh, failure, wait, grace) -
@pytest.mark.parametrize("check,producer,replacement,statuses,waitable", [
*(pytest.param(
failed_check(failure, id=100, started_at="2026-09-23T21:53:00Z", completed_at="2026-09-23T21:54:41Z"),
producing_run(), replacement_run() | changes, [], not changes, id=case,
) for case, failure, changes in (
("failed", "failure", {}), ("cancelled", "cancelled", {}),
("different-workflow", "failure", {"workflow_id": 8}),
("different-head", "failure", {"head_sha": "b" * 40}),
("completed", "failure", {"status": "completed"}),
("older", "failure", {"created_at": "2026-09-23T21:52:00Z"}),
producing_run(), replacement_run() | changes, [], waitable, id=case,
) for case, failure, changes, waitable in (
("failed", "failure", {}, True), ("cancelled", "cancelled", {}, True),
("different-workflow", "failure", {"workflow_id": 8}, False),
("different-head", "failure", {"head_sha": "b" * 40}, False),
("completed", "failure", {"status": "completed"}, False),
("older", "failure", {"created_at": "2026-09-23T21:52:00Z"}, False),
("offset-tie", "failure", {"created_at": "2026-09-23T22:53:00+01:00", "id": 0}, False),
("newer-fraction", "failure", {"created_at": "2026-09-23T21:53:00.250Z"}, True),
("invalid-creation", "failure", {"created_at": "invalid"}, False),
)),
pytest.param(failed_check(id=100), producing_run(created_at=replacement_run()["created_at"]),
replacement_run(), [], True, id="same-second-newer-run"),
Expand Down Expand Up @@ -1103,13 +1115,14 @@ def test_readiness_actions_api_error_does_not_defer_failure(gh: FakeGh) -> None:


@pytest.mark.parametrize("started,waitable", [
("2026-09-23T21:55:00Z", True), ("2026-09-23T21:54:00Z", False),
("21:55:00Z", True), ("21:54:00Z", False), ("21:54:41.250Z", True),
("22:54:41+01:00", False), ("23:54:40.999+02:00", False), ("invalid", False), (None, False),
])
def test_readiness_rerun_attempt_requires_start_after_old_failure(
gh: FakeGh, started: str, waitable: bool
gh: FakeGh, started: str | None, waitable: bool
) -> None:
arm_workflow_check(gh, failed_check(id=100, completed_at="2026-09-23T21:54:41Z"))
arm_workflow_runs(gh, producing_run(status="in_progress", run_attempt=2, run_started_at=started))
arm_workflow_runs(gh, producing_run(status="in_progress", run_attempt=2, run_started_at=started and f"2026-09-23T{started}"))
result = readiness(workflow_wait=True)
assert result.waitable_failures == (result.reasons if waitable else ())

Expand Down
Loading