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
8 changes: 7 additions & 1 deletion src/forge_loop/cli_status_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def _cmd_status(self, args: SimpleNamespace) -> int:
last_failure: dict[str, Any] | None = None
last_5_events: list[dict[str, str]] = []
active_workers_by_issue: dict[int, dict[str, Any]] = {}
terminal_worker_issues: set[int] = set()
worker_terminal_kinds = {
"worker_done",
"worker_failed",
Expand Down Expand Up @@ -108,6 +109,7 @@ def _cmd_status(self, args: SimpleNamespace) -> int:
"log_path": e.get("log_path"),
}
elif kind in worker_terminal_kinds:
terminal_worker_issues.add(issue)
active_workers_by_issue.pop(issue, None)
elif issue in active_workers_by_issue:
active_workers_by_issue[issue]["last_event_ts"] = ts
Expand All @@ -123,7 +125,11 @@ def _cmd_status(self, args: SimpleNamespace) -> int:
active_workers = list(active_workers_by_issue.values())
if not active_workers and state_blob.get("state") == "running":
for entry in state_blob.get("dispatched") or []:
if isinstance(entry, dict) and isinstance(entry.get("issue"), int):
if (
isinstance(entry, dict)
and isinstance(entry.get("issue"), int)
and entry["issue"] not in terminal_worker_issues
):
active_workers.append(
{
"issue": entry["issue"],
Expand Down
38 changes: 38 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,44 @@ def test_status_json_falls_back_to_dispatched_state_when_worker_events_missing(
assert blob["runner_stale"] is True


def test_status_json_does_not_resurrect_terminal_worker_from_dispatched_state(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
fake_cfg = SimpleNamespace(
pid_file=tmp_path / "pid",
state_dir=tmp_path,
stop_file=tmp_path / "stop",
state_file=tmp_path / "state.json",
events_file=tmp_path / "events.jsonl",
github_repo="o/r",
labels=SimpleNamespace(ready="loop:ready"),
)
fake_cfg.state_file.write_text(
json.dumps({"state": "running", "tick": 4, "dispatched": [{"issue": 9, "title": "x"}]})
)
fake_cfg.events_file.write_text(
"\n".join(
[
json.dumps({"ts": "2026-05-30T10:00:00Z", "kind": "worker_start", "issue": 9}),
json.dumps({"ts": "2026-05-30T10:01:00Z", "kind": "worker_done", "issue": 9}),
]
)
+ "\n"
)
monkeypatch.setattr(cli, "load", lambda: fake_cfg)
monkeypatch.setattr(
cli.subprocess,
"run",
lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("gh")),
)

rc = cli._cmd_status(SimpleNamespace(json=True))

assert rc == 0
blob = json.loads(capsys.readouterr().out)
assert blob["active_workers"] == []


def test_events_raw_falls_back_to_local_ops_without_github_config(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
Expand Down
Loading