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
22 changes: 21 additions & 1 deletion heart/checks/ci_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,28 @@ def normalize_runs(payload: Any, branch: str = "main") -> list[dict[str, Any]]:
"url": run.get("url") or run.get("html_url") or "",
"event": run.get("event") or "",
"headBranch": head_branch or "",
"id": str(run.get("databaseId") or run.get("id") or ""),
}
)
return runs


def observer_run_id(name: str) -> str:
"""The id of the Actions run doing this observation, when it observes itself.

Heart publishes from a workflow in its own repository, so a tick there
lists that very run — still ``in_progress`` — as the newest run of its
workflow. Read as evidence it is a permanent self-made "CI in progress"
that no repair can clear. Only the observer's own run is set aside; every
other in-progress run still reads as in progress.
"""
run_id = os.environ.get("GITHUB_RUN_ID", "")
repo = os.environ.get("GITHUB_REPOSITORY", "").rsplit("/", 1)[-1]
if run_id and repo and repo.lower() == (name or "").lower():
return run_id
return ""


def latest_per_workflow(runs: list[dict[str, Any]]) -> dict[str, dict[str, Any]]:
"""Collapse ``gh run list`` output to the newest run of each workflow on main.

Expand Down Expand Up @@ -336,7 +353,10 @@ def build_sidecar(
fetched itself from one an agent handed it.
"""
required = required_for(group, config_path)
latest = latest_per_workflow(normalize_runs(runs))
own_run = observer_run_id(name)
observed = [r for r in normalize_runs(runs)
if not (own_run and r.get("id") == own_run)]
latest = latest_per_workflow(observed)
workflows = {wf: _wf_entry(run, head_sha) for wf, run in latest.items()}

if error:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_ci_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,43 @@ def test_stale_drop_point_keeps_the_original_error(self, tmp_path, monkeypatch):
assert sidecar["status"] == "unavailable"
assert "gh: command not found" in sidecar["error"]
assert "stale" in sidecar["error"]


# --- the observer's own run (Heart publishing from its own repository) -----

def _own_runs():
previous = dict(_run("Heart Health", "success", created="2026-06-29T00:00:00Z"), databaseId=1)
own = dict(_run("Heart Health", "", status="in_progress", created="2026-06-29T01:00:00Z"),
databaseId=2)
return [previous, own]


def test_observer_skips_its_own_in_progress_run(tmp_path, monkeypatch):
monkeypatch.setenv("GITHUB_RUN_ID", "2")
monkeypatch.setenv("GITHUB_REPOSITORY", "SomeOrg/PyAutoHeart")
side = ci.build_sidecar("PyAutoHeart", "organs", _own_runs(), HEAD, "T",
config_path=_cfg(tmp_path))
assert side["workflows"]["Heart Health"]["status"] == "completed"
assert side["workflows"]["Heart Health"]["conclusion"] == "success"
assert side["conclusion"] == "success"


def test_another_repos_in_progress_run_still_reads_in_progress(tmp_path, monkeypatch):
monkeypatch.setenv("GITHUB_RUN_ID", "2")
monkeypatch.setenv("GITHUB_REPOSITORY", "SomeOrg/PyAutoHeart")
side = ci.build_sidecar("PyAutoFit", "organs", _own_runs(), HEAD, "T",
config_path=_cfg(tmp_path))
assert side["workflows"]["Heart Health"]["status"] == "in_progress"


def test_outside_actions_every_run_is_evidence(tmp_path, monkeypatch):
monkeypatch.delenv("GITHUB_RUN_ID", raising=False)
monkeypatch.delenv("GITHUB_REPOSITORY", raising=False)
side = ci.build_sidecar("PyAutoHeart", "organs", _own_runs(), HEAD, "T",
config_path=_cfg(tmp_path))
assert side["workflows"]["Heart Health"]["status"] == "in_progress"


def test_normalize_runs_keeps_the_run_id():
assert ci.normalize_runs([dict(_run("Tests", "success"), databaseId=7)])[0]["id"] == "7"
assert ci.normalize_runs({"workflow_runs": [dict(_rest_run("Tests", "success"), id=9)]})[0]["id"] == "9"
Loading