Skip to content

Commit 4e597d7

Browse files
authored
refactor(control-plane): move goal latest run into quota bounded context (#2933)
1 parent f387b7b commit 4e597d7

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

loopx/control_plane/quota/recent_runs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
NON_WORK_RUN_CLASSIFICATIONS = {"state_refreshed"}
1111

1212

13+
def goal_latest_run(goal: dict[str, Any]) -> dict[str, Any]:
14+
"""Return the latest run object for one goal, or an empty mapping."""
15+
16+
latest_runs = (
17+
goal.get("latest_runs")
18+
if isinstance(goal.get("latest_runs"), list)
19+
else []
20+
)
21+
if latest_runs and isinstance(latest_runs[0], dict):
22+
return latest_runs[0]
23+
return {}
24+
25+
1326
def _run_is_unchanged_monitor_observation(run: dict[str, Any]) -> bool:
1427
if str(run.get("classification") or "") != QUOTA_MONITOR_POLL_CLASSIFICATION:
1528
return False

loopx/quota.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
)
7979
from .control_plane.quota.recent_runs import (
8080
build_monitor_debt_arbitration as _build_monitor_debt_arbitration,
81+
goal_latest_run as _goal_latest_run,
8182
goal_latest_runs as _goal_latest_runs,
8283
recent_external_monitor_observation_unchanged as _recent_external_monitor_observation_unchanged,
8384
)
@@ -507,13 +508,6 @@ def quota_status(
507508
return payload
508509

509510

510-
def _latest_run(goal: dict[str, Any]) -> dict[str, Any]:
511-
latest_runs = goal.get("latest_runs") if isinstance(goal.get("latest_runs"), list) else []
512-
if latest_runs and isinstance(latest_runs[0], dict):
513-
return latest_runs[0]
514-
return {}
515-
516-
517511
def _quota_sort_key(item: dict[str, Any]) -> tuple[int, float, int, str]:
518512
quota = item.get("quota") if isinstance(item.get("quota"), dict) else {}
519513
state = str(quota.get("state") or "waiting")
@@ -910,7 +904,7 @@ def build_quota_plan(status_payload: dict[str, Any], *, mode: str = "status") ->
910904
if isinstance(attention.get("project_asset"), dict)
911905
else {}
912906
)
913-
latest = _latest_run(goal)
907+
latest = _goal_latest_run(goal)
914908
waiting_on = attention.get("waiting_on") or "none"
915909
lifecycle_phase = attention.get("lifecycle_phase") or goal.get("lifecycle_phase")
916910
lifecycle_flags = attention.get("lifecycle_flags") or goal.get("lifecycle_flags")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from __future__ import annotations
2+
3+
from loopx.control_plane.quota.recent_runs import goal_latest_run
4+
5+
6+
def test_goal_latest_run_returns_first_compact_run() -> None:
7+
assert goal_latest_run({}) == {}
8+
assert goal_latest_run({"latest_runs": []}) == {}
9+
assert goal_latest_run(
10+
{
11+
"latest_runs": [
12+
{"generated_at": "2026-08-08T00:00:00+00:00"},
13+
{"generated_at": "2026-08-07T00:00:00+00:00"},
14+
]
15+
}
16+
) == {"generated_at": "2026-08-08T00:00:00+00:00"}

0 commit comments

Comments
 (0)