diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 4db76120..649d98c0 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -148,6 +148,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 diff --git a/src/loopzero/github.py b/src/loopzero/github.py index 80d1b365..b7e74d27 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -1314,29 +1314,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: diff --git a/tests/test_github.py b/tests/test_github.py index 13690a02..40fe08b1 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -473,6 +473,7 @@ def test_post_review_counts_only_lf_as_patch_line_break(gh: FakeGh, separator: s findings = ( Finding("important", path, 2, "In diff", ""), Finding("critical", path, 3, "Outside diff", ""), + Finding("suggestion", path, 3, "Off-diff suggestion", ""), ) github.post_review(REPO, 7, HEAD, review(findings=findings)) @@ -481,6 +482,7 @@ def test_post_review_counts_only_lf_as_patch_line_break(gh: FakeGh, separator: s comments = comments_posted(gh) assert [(c["path"], c["line"]) for c in comments] == [(path, 2), (path, 1)] assert "Reviewer location src/a.py:3 is not in the diff" in comments[1]["body"] + assert "Off-diff suggestion" in review_payloads(gh)[0]["body"] @pytest.mark.parametrize(("files", "severity", "anchor"), [ @@ -840,15 +842,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 @@ -980,13 +991,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"), @@ -1060,13 +1074,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 ())