From a75ff13140e16a605dda45e1131435ce35557dc7 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 04:18:48 +0100 Subject: [PATCH 1/2] Compare readiness run ordering as UTC instants Keep numeric check-run IDs primary and preserve API order when instants tie. Compare workflow retry and replacement timestamps with the same strict parser used by the freshness change. Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 6 ++++ src/loopzero/github.py | 36 +++++++++++++------ tests/test_github.py | 80 +++++++++++++++++++++--------------------- 3 files changed, 71 insertions(+), 51 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c474e6e7..1ef54a77 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -112,6 +112,12 @@ The [CLI](../src/loopzero/cli.py) implements the contract's 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. This is best-effort observation: an immediate green read may precede diff --git a/src/loopzero/github.py b/src/loopzero/github.py index 45fb8ce2..64b8fb10 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -1133,6 +1133,19 @@ def _review_threads(repo: str, number: int, *, include_resolved: bool = False) - raise _not_found(number, exc) from exc +_READINESS_TIMESTAMP = re.compile( + r"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?" + r"(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])" +) + + +def readiness_timestamp(value: object) -> datetime | None: + if isinstance(value, str) and _READINESS_TIMESTAMP.fullmatch(value): + with suppress(OverflowError, ValueError): + return datetime.fromisoformat(value).astimezone(UTC) + return None + + def _check_signals( repo: str, head_sha: str, @@ -1235,29 +1248,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 {"queued", "in_progress", "waiting", "pending", "requested"}): 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, datetime | 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 cbdc0588..48b968c8 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -893,27 +893,30 @@ def test_check_runs_merges_runs_and_statuses(gh: FakeGh) -> None: assert pages == ["page=1", "page=2"] -@pytest.mark.parametrize(("runs", "expected"), [ - ([ - {"id": 10, "name": "checks", "status": "completed", "conclusion": "skipped"}, - {"id": 11, "name": "checks", "status": "completed", "conclusion": "success"}, - ], {"checks": "success"}), - ([ - {"id": 20, "name": "checks", "status": "in_progress", "conclusion": None}, - {"id": 19, "name": "checks", "status": "completed", "conclusion": "success"}, - ], {"checks": "pending"}), - ([ - {"id": 30, "name": "checks", "status": "completed", "conclusion": "success"}, - {"id": 31, "name": "lint", "status": "completed", "conclusion": "failure"}, - ], {"checks": "success", "lint": "failure"}), - ([ - {"id": 40, "name": "checks", "started_at": "2026-09-18T01:00:00Z", - "status": "completed", "conclusion": "failure"}, - {"id": 40, "name": "checks", "started_at": "2026-09-18T02:00:00Z", - "status": "completed", "conclusion": "success"}, - ], {"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", [ + pytest.param([(10, "checks", None, "skipped"), (11, "checks", None, "success")], + {"checks": "success"}, id="stale-skipped"), + pytest.param([(20, "checks", "01:00:00Z", None), (19, "checks", "02:00:00Z", "success")], + {"checks": "pending"}, id="id-before-time"), + pytest.param([(30, "checks", None, "success"), (31, "lint", None, "failure")], + {"checks": "success", "lint": "failure"}, id="distinct-names"), + pytest.param([(40, "checks", "01:00:00Z", "failure"), (40, "checks", "02:00:00Z", "success")], + {"checks": "success"}, id="timestamp-tie"), + pytest.param([(7, "checks", "12:00:00Z", "success"), (7, "checks", "12:00:00.250Z", "failure")], + {"checks": "failure"}, id="same-id-fraction"), + pytest.param([(None, "checks", "12:00:00Z", "success"), (None, "checks", "09:00:01-03:00", "failure")], + {"checks": "failure"}, id="missing-ids-offset"), + pytest.param([(7, "checks", "12:00:00Z", "failure"), (7, "checks", "14:00:00+02:00", "success")], + {"checks": "failure"}, id="equal-instants"), + pytest.param([(7, "checks", "12:00:00Z", "failure"), (7, "checks", "23:00:00+00:99", "success")], + {"checks": "failure"}, id="invalid-loses-to-valid"), + pytest.param([(None, "checks", None, "failure"), (None, "checks", "23:00:00", "success")], + {"checks": "failure"}, id="unusable-times-tie"), +]) +def test_check_runs_ordering(gh: FakeGh, rows: list[tuple], expected: dict) -> None: + runs = [dict({"id": number} if number is not None else {}, name=name, conclusion=conclusion, + started_at=stamp and f"2026-09-23T{stamp}", status="completed" if conclusion else "in_progress") + for number, name, stamp, conclusion in rows] gh.respond(f"api repos/{REPO}/commits/{HEAD}/check-runs", {"check_runs": runs}) gh.respond(f"api repos/{REPO}/commits/{HEAD}/statuses", []) assert github._check_signals(REPO, HEAD)[0] == expected @@ -1059,31 +1062,27 @@ def test_readiness_missing_and_failed_checks(gh: FakeGh, failure, wait, grace) - assert r.waitable_failures == ((f"required check 'checks' is {failure}",) if wait and grace else ()) -@pytest.mark.parametrize("failure,changes", [ - pytest.param("failure", {}, id="failed"), pytest.param("cancelled", {}, id="cancelled"), - pytest.param("failure", {"workflow_id": 8}, id="different-workflow"), - pytest.param("failure", {"head_sha": "b" * 40}, id="different-head"), - pytest.param("failure", {"status": "completed"}, id="completed"), - pytest.param("failure", {"created_at": "2026-09-23T21:52:00Z"}, id="older"), +@pytest.mark.parametrize("failure,changes,waitable", [ + ("failure", {}, True), ("cancelled", {}, True), + ("failure", {"workflow_id": 8}, False), ("failure", {"head_sha": "b" * 40}, False), + ("failure", {"status": "completed"}, False), + ("failure", {"created_at": "2026-09-23T21:52:00Z"}, False), + ("failure", {"created_at": "2026-09-23T21:53:00Z"}, True), + ("failure", {"created_at": "2026-09-23T22:53:00+01:00", "id": 0}, False), + ("failure", {"created_at": "2026-09-23T21:53:00.250Z"}, True), + ("failure", {"created_at": "invalid"}, False), ]) -def test_readiness_waits_only_for_newer_active_producing_workflow(gh, failure, changes): +def test_readiness_waits_only_for_newer_active_producing_workflow(gh, failure, changes, waitable): arm_workflow_check(gh, failed_check( failure, id=100, started_at="2026-09-23T21:53:00Z", completed_at="2026-09-23T21:54:41Z", )) - arm_workflow_runs(gh, producing_run(), {**replacement_run(), **changes}) + arm_workflow_runs(gh, producing_run(), replacement_run() | changes) result = readiness(workflow_wait=True) assert result.reasons == (f"required check 'checks' is {failure}",) - assert result.waitable_failures == (() if changes else result.reasons) + assert result.waitable_failures == (result.reasons if waitable else ()) assert not result.ready -def test_readiness_newer_run_in_same_second_waits(gh: FakeGh) -> None: - arm_workflow_check(gh, failed_check(id=100)) - arm_workflow_runs(gh, producing_run(created_at="2026-09-23T21:54:21Z"), replacement_run()) - result = readiness(workflow_wait=True) - assert result.waitable_failures == result.reasons - - def test_readiness_keeps_failing_commit_status_blocker(gh: FakeGh) -> None: arm_workflow_check(gh, failed_check(id=100), [{"context": "checks", "state": "failure"}]) arm_workflow_runs(gh, producing_run(), replacement_run()) @@ -1166,13 +1165,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 ()) From 255b885b6b5e65d5cf97e8c97a44d7601c29b2c4 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:32:41 +0100 Subject: [PATCH 2/2] Keep off-diff suggestions out of inline review comments Co-Authored-By: Codex --- tests/test_github.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_github.py b/tests/test_github.py index 13690a02..05ba69e1 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"), [