Skip to content

Commit 1ebba9c

Browse files
committed
refactor(control-plane): extract quota registry and pause read models
1 parent 6bafafd commit 1ebba9c

3 files changed

Lines changed: 43 additions & 38 deletions

File tree

loopx/control_plane/quota/goal_boundary.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import json
34
import shlex
45
from collections.abc import Callable, Mapping
56
from pathlib import Path
@@ -63,6 +64,29 @@ def quota_execution_profile_summary(value: Any) -> dict[str, Any] | None:
6364
return compact or None
6465

6566

67+
def registry_goal_by_id(
68+
status_payload: Mapping[str, Any],
69+
) -> dict[str, dict[str, Any]]:
70+
"""Return registry goals indexed by goal id from a status payload."""
71+
72+
registry_value = status_payload.get("registry")
73+
if not registry_value:
74+
return {}
75+
registry_path = Path(str(registry_value)).expanduser()
76+
try:
77+
payload = json.loads(registry_path.read_text(encoding="utf-8"))
78+
except (OSError, json.JSONDecodeError):
79+
return {}
80+
goals = payload.get("goals") if isinstance(payload, dict) else None
81+
if not isinstance(goals, list):
82+
return {}
83+
return {
84+
str(goal.get("id") or ""): goal
85+
for goal in goals
86+
if isinstance(goal, dict) and goal.get("id")
87+
}
88+
89+
6690
def quota_execution_profile_boundary_summary(value: Any) -> dict[str, Any] | None:
6791
summary = quota_execution_profile_summary(value)
6892
if not summary:

loopx/control_plane/quota/states.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import Any
4+
35

46
QUOTA_STATE_ORDER = (
57
"blocked_health",
@@ -10,3 +12,18 @@
1012
"throttled",
1113
"paused",
1214
)
15+
16+
17+
def quota_item_is_paused(item: dict[str, Any]) -> bool:
18+
"""Return True when a plan item carries a Goal-level hard pause."""
19+
20+
raw_quota = item.get("quota")
21+
quota = raw_quota if isinstance(raw_quota, dict) else {}
22+
if str(quota.get("state") or "") == "paused":
23+
return True
24+
compute = quota.get("compute")
25+
return (
26+
isinstance(compute, (int, float))
27+
and not isinstance(compute, bool)
28+
and compute <= 0
29+
)

loopx/quota.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import json
43
from collections.abc import Callable, Mapping
54
from dataclasses import dataclass
65
from datetime import datetime, timedelta, timezone
@@ -71,7 +70,7 @@
7170
refine_quota_recommended_action,
7271
resolve_quota_run_decision,
7372
)
74-
from .control_plane.quota.goal_boundary import effective_available_capabilities as _effective_available_capabilities, goal_boundary as _goal_boundary, quota_execution_profile_summary as _quota_execution_profile_summary
73+
from .control_plane.quota.goal_boundary import effective_available_capabilities as _effective_available_capabilities, goal_boundary as _goal_boundary, quota_execution_profile_summary as _quota_execution_profile_summary, registry_goal_by_id as _registry_goal_by_id
7574
from .control_plane.quota.monitor_poll import (
7675
QUOTA_MONITOR_POLL_CLASSIFICATION as QUOTA_MONITOR_POLL_CLASSIFICATION,
7776
build_quota_monitor_poll_event as build_quota_monitor_poll_event,
@@ -116,7 +115,7 @@
116115
from .control_plane.quota.spend_sources import (
117116
DEFAULT_SLOT_SPEND_SOURCE,
118117
)
119-
from .control_plane.quota.states import QUOTA_STATE_ORDER
118+
from .control_plane.quota.states import QUOTA_STATE_ORDER, quota_item_is_paused as _quota_item_is_paused
120119
from .control_plane.quota.policy_constants import (
121120
AUTONOMOUS_CANDIDATE_CONTEXT_FIELDS,
122121
DEFAULT_COMPUTE_QUOTA,
@@ -1103,25 +1102,6 @@ def _reward_lesson_projection_warning(
11031102
}
11041103

11051104

1106-
def _registry_goal_by_id(status_payload: dict[str, Any]) -> dict[str, dict[str, Any]]:
1107-
registry_value = status_payload.get("registry")
1108-
if not registry_value:
1109-
return {}
1110-
registry_path = Path(str(registry_value)).expanduser()
1111-
try:
1112-
payload = json.loads(registry_path.read_text(encoding="utf-8"))
1113-
except (OSError, json.JSONDecodeError):
1114-
return {}
1115-
goals = payload.get("goals") if isinstance(payload, dict) else None
1116-
if not isinstance(goals, list):
1117-
return {}
1118-
return {
1119-
str(goal.get("id") or ""): goal
1120-
for goal in goals
1121-
if isinstance(goal, dict) and goal.get("id")
1122-
}
1123-
1124-
11251105
def _recovery_delivery_allowed(quota: dict[str, Any], *, plan_ok: bool) -> bool:
11261106
return (
11271107
bool(plan_ok)
@@ -2434,22 +2414,6 @@ def _build_quota_should_run_payload(
24342414
QUOTA_PAUSED_MODE = "quota_paused"
24352415

24362416

2437-
def _quota_item_is_paused(item: dict[str, Any]) -> bool:
2438-
"""Return True when a plan item carries a Goal-level hard pause.
2439-
2440-
A paused Goal (`quota.compute<=0`) is a typed terminal decision: it is
2441-
evaluated before the selector builds any capability, workspace, replan,
2442-
monitor, or inbox candidate, so no lane can emit a contradicting execution
2443-
signal underneath the pause.
2444-
"""
2445-
2446-
quota = item.get("quota") if isinstance(item.get("quota"), dict) else {}
2447-
if str(quota.get("state") or "") == "paused":
2448-
return True
2449-
compute = quota.get("compute")
2450-
return isinstance(compute, (int, float)) and not isinstance(compute, bool) and compute <= 0
2451-
2452-
24532417
def _build_quota_paused_should_run_payload(
24542418
status_payload: dict[str, Any],
24552419
*,

0 commit comments

Comments
 (0)