Skip to content

Commit 633c0eb

Browse files
committed
refactor(status): move autonomous replan projections into bounded module
1 parent 9f32d1a commit 633c0eb

2 files changed

Lines changed: 157 additions & 45 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""Autonomous replan status projections inside the `status` bounded context."""
2+
3+
from __future__ import annotations
4+
5+
import re
6+
from typing import Any
7+
8+
from ..runtime.public_safety import public_safe_compact_text
9+
from ..work_items.autonomous_replan_ack import (
10+
AUTONOMOUS_REPLAN_ACK_MATERIAL_RUN_WINDOW,
11+
autonomous_replan_ack_recorded,
12+
latest_autonomous_replan_ack_for_projection as _latest_autonomous_replan_ack_for_projection,
13+
)
14+
from ..work_items.autonomous_replan_obligation import (
15+
AUTONOMOUS_REPLAN_STALL_THRESHOLD,
16+
autonomous_replan_obligation_from_runs as _autonomous_replan_obligation_from_runs,
17+
autonomous_replan_periodic_review_from_runs as _autonomous_replan_periodic_review_from_runs,
18+
build_autonomous_replan_obligation as _build_autonomous_replan_obligation,
19+
run_history_monitor_wait_already_acknowledged as _run_history_monitor_wait_already_acknowledged,
20+
run_history_stall_signal as _run_history_stall_signal_read_model,
21+
)
22+
from ..work_items.delivery_outcome import (
23+
PROGRESS_DELIVERY_OUTCOMES,
24+
normalize_delivery_outcome,
25+
)
26+
27+
28+
DEAD_MONITOR_REPEAT_THRESHOLD = 6
29+
DEAD_MONITOR_REPEAT_SCHEMA_VERSION = "dead_monitor_repeat_v0"
30+
AUTONOMOUS_REPLAN_SCHEMA_VERSION = "autonomous_replan_obligation_v0"
31+
AUTONOMOUS_REPLAN_PERIODIC_RUN_THRESHOLD = AUTONOMOUS_REPLAN_ACK_MATERIAL_RUN_WINDOW
32+
AUTONOMOUS_RUN_HISTORY_PROGRESS_OUTCOMES = PROGRESS_DELIVERY_OUTCOMES
33+
AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS = {
34+
"quota_slot_spent",
35+
"quota_slot_voided",
36+
"delivery_completion_spend_accounted_v0",
37+
}
38+
AUTONOMOUS_RUN_HISTORY_STALL_PATTERN = re.compile(
39+
r"(?i)(?:monitor|observe|observation|poll|watch|quiet|no[-_ ]?op|no[-_ ]?progress|stalled?|unchanged|dependency|停转|无进展|重复|反复|观察|轮询)"
40+
)
41+
42+
43+
def build_autonomous_replan_obligation(
44+
evidence: list[dict[str, Any]],
45+
*,
46+
agent_todos: dict[str, Any] | None,
47+
) -> dict[str, Any] | None:
48+
return _build_autonomous_replan_obligation(
49+
evidence,
50+
agent_todos=agent_todos,
51+
public_safe_compact_text=public_safe_compact_text,
52+
autonomous_replan_schema_version=AUTONOMOUS_REPLAN_SCHEMA_VERSION,
53+
autonomous_replan_stall_threshold=AUTONOMOUS_REPLAN_STALL_THRESHOLD,
54+
dead_monitor_repeat_threshold=DEAD_MONITOR_REPEAT_THRESHOLD,
55+
dead_monitor_repeat_schema_version=DEAD_MONITOR_REPEAT_SCHEMA_VERSION,
56+
)
57+
58+
59+
def _run_history_stall_signal(run: dict[str, Any]) -> dict[str, Any] | None:
60+
return _run_history_stall_signal_read_model(
61+
run,
62+
autonomous_replan_ack_recorded=autonomous_replan_ack_recorded,
63+
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
64+
progress_outcomes=AUTONOMOUS_RUN_HISTORY_PROGRESS_OUTCOMES,
65+
stall_pattern=AUTONOMOUS_RUN_HISTORY_STALL_PATTERN,
66+
public_safe_compact_text=public_safe_compact_text,
67+
normalize_delivery_outcome=normalize_delivery_outcome,
68+
)
69+
70+
71+
def run_history_monitor_wait_already_acknowledged(
72+
latest_runs: list[dict[str, Any]] | None,
73+
*,
74+
signal_count: int,
75+
) -> bool:
76+
return _run_history_monitor_wait_already_acknowledged(
77+
latest_runs,
78+
signal_count=signal_count,
79+
autonomous_replan_ack_recorded=autonomous_replan_ack_recorded,
80+
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
81+
)
82+
83+
84+
def latest_autonomous_replan_ack_for_projection(
85+
latest_runs: list[dict[str, Any]] | None,
86+
) -> dict[str, Any] | None:
87+
return _latest_autonomous_replan_ack_for_projection(
88+
latest_runs,
89+
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
90+
)
91+
92+
93+
def autonomous_replan_periodic_review_from_runs(
94+
latest_runs: list[dict[str, Any]] | None,
95+
*,
96+
agent_todos: dict[str, Any] | None,
97+
) -> dict[str, Any] | None:
98+
return _autonomous_replan_periodic_review_from_runs(
99+
latest_runs,
100+
agent_todos=agent_todos,
101+
autonomous_replan_ack_recorded=autonomous_replan_ack_recorded,
102+
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
103+
periodic_run_threshold=AUTONOMOUS_REPLAN_PERIODIC_RUN_THRESHOLD,
104+
build_autonomous_replan_obligation=build_autonomous_replan_obligation,
105+
)
106+
107+
108+
def autonomous_replan_obligation_from_runs(
109+
latest_runs: list[dict[str, Any]] | None,
110+
*,
111+
agent_todos: dict[str, Any] | None,
112+
agent_id: str | None = None,
113+
) -> dict[str, Any] | None:
114+
return _autonomous_replan_obligation_from_runs(
115+
latest_runs,
116+
agent_todos=agent_todos,
117+
agent_id=agent_id,
118+
autonomous_replan_ack_recorded=autonomous_replan_ack_recorded,
119+
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
120+
progress_outcomes=AUTONOMOUS_RUN_HISTORY_PROGRESS_OUTCOMES,
121+
stall_pattern=AUTONOMOUS_RUN_HISTORY_STALL_PATTERN,
122+
public_safe_compact_text=public_safe_compact_text,
123+
normalize_delivery_outcome=normalize_delivery_outcome,
124+
build_autonomous_replan_obligation=build_autonomous_replan_obligation,
125+
autonomous_replan_stall_threshold=AUTONOMOUS_REPLAN_STALL_THRESHOLD,
126+
dead_monitor_repeat_threshold=DEAD_MONITOR_REPEAT_THRESHOLD,
127+
dead_monitor_repeat_schema_version=DEAD_MONITOR_REPEAT_SCHEMA_VERSION,
128+
periodic_run_threshold=AUTONOMOUS_REPLAN_PERIODIC_RUN_THRESHOLD,
129+
)

loopx/status.py

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
DELIVERY_OUTCOME_NOT_CONFIGURED,
2626
PROGRESS_DELIVERY_OUTCOMES,
2727
delivery_turn_kind_for_run,
28-
normalize_delivery_outcome,
2928
)
3029
from .doctor import (
3130
PROMOTION_READINESS_CLASSIFICATIONS,
@@ -112,20 +111,13 @@
112111
)
113112
from .control_plane.work_items.autonomous_replan_ack import (
114113
AUTONOMOUS_REPLAN_ACK_MATERIAL_RUN_WINDOW,
115-
autonomous_replan_ack_recorded,
116114
compact_autonomous_replan_ack,
117-
latest_autonomous_replan_ack_for_projection as _latest_autonomous_replan_ack_for_projection_read_model,
118115
)
119116
from .control_plane.work_items.autonomous_replan_obligation import (
120117
AUTONOMOUS_REPLAN_STALL_THRESHOLD as _AUTONOMOUS_REPLAN_STALL_THRESHOLD_READ_MODEL,
121118
AUTONOMOUS_REPLAN_TRIGGER_PATTERNS as _AUTONOMOUS_REPLAN_TRIGGER_PATTERNS_READ_MODEL,
122119
MAX_AUTONOMOUS_REPLAN_TRIGGERS as _MAX_AUTONOMOUS_REPLAN_TRIGGERS_READ_MODEL,
123120
autonomous_replan_obligation_from_state as _autonomous_replan_obligation_from_state_read_model,
124-
autonomous_replan_obligation_from_runs as _autonomous_replan_obligation_from_runs_read_model,
125-
autonomous_replan_periodic_review_from_runs as _autonomous_replan_periodic_review_from_runs_read_model,
126-
build_autonomous_replan_obligation as _build_autonomous_replan_obligation_read_model,
127-
run_history_monitor_wait_already_acknowledged as _run_history_monitor_wait_already_acknowledged_read_model,
128-
run_history_stall_signal as _run_history_stall_signal_read_model,
129121
)
130122
from .control_plane.work_items.backlog_hygiene import (
131123
MAX_BACKLOG_HYGIENE_EVIDENCE_ITEMS as _MAX_BACKLOG_HYGIENE_EVIDENCE_ITEMS_READ_MODEL,
@@ -2118,63 +2110,61 @@ def build_autonomous_replan_obligation(
21182110
*,
21192111
agent_todos: dict[str, Any] | None,
21202112
) -> dict[str, Any] | None:
2121-
return _build_autonomous_replan_obligation_read_model(
2113+
from .control_plane.status.autonomous_replan_projection import (
2114+
build_autonomous_replan_obligation as _build_autonomous_replan_obligation,
2115+
)
2116+
2117+
return _build_autonomous_replan_obligation(
21222118
evidence,
21232119
agent_todos=agent_todos,
2124-
public_safe_compact_text=public_safe_compact_text,
2125-
autonomous_replan_schema_version=AUTONOMOUS_REPLAN_SCHEMA_VERSION,
2126-
autonomous_replan_stall_threshold=AUTONOMOUS_REPLAN_STALL_THRESHOLD,
2127-
dead_monitor_repeat_threshold=DEAD_MONITOR_REPEAT_THRESHOLD,
2128-
dead_monitor_repeat_schema_version=DEAD_MONITOR_REPEAT_SCHEMA_VERSION,
21292120
)
21302121

21312122

21322123
def _run_history_stall_signal(run: dict[str, Any]) -> dict[str, Any] | None:
2133-
return _run_history_stall_signal_read_model(
2134-
run,
2135-
autonomous_replan_ack_recorded=autonomous_replan_ack_recorded,
2136-
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
2137-
progress_outcomes=AUTONOMOUS_RUN_HISTORY_PROGRESS_OUTCOMES,
2138-
stall_pattern=AUTONOMOUS_RUN_HISTORY_STALL_PATTERN,
2139-
public_safe_compact_text=public_safe_compact_text,
2140-
normalize_delivery_outcome=normalize_delivery_outcome,
2124+
from .control_plane.status.autonomous_replan_projection import (
2125+
_run_history_stall_signal as _run_history_stall_signal_projection,
21412126
)
21422127

2128+
return _run_history_stall_signal_projection(run)
2129+
21432130

21442131
def run_history_monitor_wait_already_acknowledged(
21452132
latest_runs: list[dict[str, Any]] | None,
21462133
*,
21472134
signal_count: int,
21482135
) -> bool:
2149-
return _run_history_monitor_wait_already_acknowledged_read_model(
2136+
from .control_plane.status.autonomous_replan_projection import (
2137+
run_history_monitor_wait_already_acknowledged as _run_history_monitor_wait_already_acknowledged,
2138+
)
2139+
2140+
return _run_history_monitor_wait_already_acknowledged(
21502141
latest_runs,
21512142
signal_count=signal_count,
2152-
autonomous_replan_ack_recorded=autonomous_replan_ack_recorded,
2153-
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
21542143
)
21552144

21562145

21572146
def latest_autonomous_replan_ack_for_projection(
21582147
latest_runs: list[dict[str, Any]] | None,
21592148
) -> dict[str, Any] | None:
2160-
return _latest_autonomous_replan_ack_for_projection_read_model(
2161-
latest_runs,
2162-
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
2149+
from .control_plane.status.autonomous_replan_projection import (
2150+
latest_autonomous_replan_ack_for_projection as _latest_autonomous_replan_ack_for_projection,
21632151
)
21642152

2153+
return _latest_autonomous_replan_ack_for_projection(latest_runs)
2154+
21652155

21662156
def autonomous_replan_periodic_review_from_runs(
21672157
latest_runs: list[dict[str, Any]] | None,
21682158
*,
21692159
agent_todos: dict[str, Any] | None,
21702160
) -> dict[str, Any] | None:
2171-
return _autonomous_replan_periodic_review_from_runs_read_model(
2161+
from .control_plane.status.autonomous_replan_projection import (
2162+
autonomous_replan_periodic_review_from_runs as _autonomous_replan_periodic_review_from_runs,
2163+
)
2164+
2165+
return _autonomous_replan_periodic_review_from_runs(
21722166
latest_runs,
21732167
agent_todos=agent_todos,
2174-
autonomous_replan_ack_recorded=autonomous_replan_ack_recorded,
2175-
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
2176-
periodic_run_threshold=AUTONOMOUS_REPLAN_PERIODIC_RUN_THRESHOLD,
2177-
build_autonomous_replan_obligation=build_autonomous_replan_obligation,
21782168
)
21792169

21802170

@@ -2184,21 +2174,14 @@ def autonomous_replan_obligation_from_runs(
21842174
agent_todos: dict[str, Any] | None,
21852175
agent_id: str | None = None,
21862176
) -> dict[str, Any] | None:
2187-
return _autonomous_replan_obligation_from_runs_read_model(
2177+
from .control_plane.status.autonomous_replan_projection import (
2178+
autonomous_replan_obligation_from_runs as _autonomous_replan_obligation_from_runs,
2179+
)
2180+
2181+
return _autonomous_replan_obligation_from_runs(
21882182
latest_runs,
21892183
agent_todos=agent_todos,
21902184
agent_id=agent_id,
2191-
autonomous_replan_ack_recorded=autonomous_replan_ack_recorded,
2192-
neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
2193-
progress_outcomes=AUTONOMOUS_RUN_HISTORY_PROGRESS_OUTCOMES,
2194-
stall_pattern=AUTONOMOUS_RUN_HISTORY_STALL_PATTERN,
2195-
public_safe_compact_text=public_safe_compact_text,
2196-
normalize_delivery_outcome=normalize_delivery_outcome,
2197-
build_autonomous_replan_obligation=build_autonomous_replan_obligation,
2198-
autonomous_replan_stall_threshold=AUTONOMOUS_REPLAN_STALL_THRESHOLD,
2199-
dead_monitor_repeat_threshold=DEAD_MONITOR_REPEAT_THRESHOLD,
2200-
dead_monitor_repeat_schema_version=DEAD_MONITOR_REPEAT_SCHEMA_VERSION,
2201-
periodic_run_threshold=AUTONOMOUS_REPLAN_PERIODIC_RUN_THRESHOLD,
22022185
)
22032186

22042187

0 commit comments

Comments
 (0)