Skip to content

Commit 365600a

Browse files
committed
refactor(status): move active-state projections into bounded module
1 parent 633c0eb commit 365600a

2 files changed

Lines changed: 158 additions & 52 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""Active-state status projections inside the `status` bounded context."""
2+
3+
from __future__ import annotations
4+
5+
import re
6+
from pathlib import Path
7+
from typing import Any
8+
9+
from ..goals.active_state_event_projection import (
10+
active_state_event_projection_fields as _active_state_event_projection_fields,
11+
state_event_log_candidates as _state_event_log_candidates,
12+
)
13+
from ..goals.active_state_sections import (
14+
active_state_section_entries as _active_state_section_entries,
15+
active_state_sections as _active_state_sections,
16+
)
17+
from ..goals.path_resolution import resolve_goal_local_path
18+
from ..runtime.public_safety import public_safe_compact_text
19+
from ..todos.active_state_todo_parser import parse_active_state_todos
20+
from ..todos.todo_summary import (
21+
MAX_STATUS_TODOS_PER_ROLE,
22+
normalize_todo_text,
23+
)
24+
from ..work_items.autonomous_replan_obligation import (
25+
autonomous_replan_obligation_from_state as _autonomous_replan_obligation_from_state,
26+
)
27+
from ..work_items.backlog_hygiene import (
28+
MAX_BACKLOG_HYGIENE_EVIDENCE_ITEMS,
29+
backlog_hygiene_warning as _backlog_hygiene_warning,
30+
)
31+
from ..work_items.issue_meta_surface import (
32+
parse_issue_meta_surface as _parse_issue_meta_surface,
33+
)
34+
from .autonomous_replan_projection import build_autonomous_replan_obligation
35+
36+
37+
STATE_EVENT_LOG_BASENAME = "events.jsonl"
38+
SECTION_HEADING_PATTERN = re.compile(r"^##+\s+(.+?)\s*$")
39+
BACKLOG_HYGIENE_SECTION_HEADINGS = ("Next Action", "Operating Lessons")
40+
BACKLOG_HYGIENE_BULLET_PATTERN = re.compile(r"^\s*(?:[-*]|\d+[.)])\s+(.+?)\s*$")
41+
BACKLOG_HYGIENE_HINT_PATTERN = re.compile(
42+
r"(?i)(?:\[p[0-4]\]|todo|backlog|follow[- ]?up|queue|audit|regression|smoke|cadence|mirror|monitor|sub-?agent|待办|回归|审计|修复|检查|推进)"
43+
)
44+
AUTONOMOUS_REPLAN_SECTION_HEADINGS = (
45+
"Next Action",
46+
"Operating Lessons",
47+
)
48+
49+
50+
def state_event_log_candidates(goal: dict[str, Any], *, state_path: Path) -> list[Path]:
51+
return _state_event_log_candidates(
52+
goal,
53+
state_path=state_path,
54+
resolve_goal_local_path=resolve_goal_local_path,
55+
event_log_basename=STATE_EVENT_LOG_BASENAME,
56+
)
57+
58+
59+
def active_state_event_projection_fields(
60+
goal: dict[str, Any],
61+
*,
62+
state_path: Path,
63+
preferred_todo_ids: set[str] | None = None,
64+
rollout_events: list[dict[str, Any]] | None = None,
65+
item_limit: int | None = MAX_STATUS_TODOS_PER_ROLE,
66+
) -> dict[str, Any]:
67+
return _active_state_event_projection_fields(
68+
goal,
69+
state_path=state_path,
70+
resolve_goal_local_path=resolve_goal_local_path,
71+
parse_active_state_todos=parse_active_state_todos,
72+
preferred_todo_ids=preferred_todo_ids,
73+
rollout_events=rollout_events,
74+
item_limit=item_limit,
75+
event_log_basename=STATE_EVENT_LOG_BASENAME,
76+
)
77+
78+
79+
def active_state_sections(state_text: str, headings: tuple[str, ...]) -> dict[str, list[str]]:
80+
return _active_state_sections(
81+
state_text,
82+
headings,
83+
section_heading_pattern=SECTION_HEADING_PATTERN,
84+
)
85+
86+
87+
def parse_issue_meta_surface(state_text: str) -> dict[str, Any] | None:
88+
return _parse_issue_meta_surface(
89+
state_text,
90+
section_parser=active_state_sections,
91+
public_safe_compact_text=public_safe_compact_text,
92+
)
93+
94+
95+
def active_state_section_entries(lines: list[str]) -> list[str]:
96+
return _active_state_section_entries(
97+
lines,
98+
bullet_pattern=BACKLOG_HYGIENE_BULLET_PATTERN,
99+
normalize_text=normalize_todo_text,
100+
)
101+
102+
103+
def backlog_hygiene_warning(state_text: str, *, agent_todos: dict[str, Any] | None) -> dict[str, Any] | None:
104+
return _backlog_hygiene_warning(
105+
state_text,
106+
agent_todos=agent_todos,
107+
section_headings=BACKLOG_HYGIENE_SECTION_HEADINGS,
108+
section_parser=active_state_sections,
109+
bullet_pattern=BACKLOG_HYGIENE_BULLET_PATTERN,
110+
hint_pattern=BACKLOG_HYGIENE_HINT_PATTERN,
111+
public_safe_compact_text=public_safe_compact_text,
112+
max_evidence_items=MAX_BACKLOG_HYGIENE_EVIDENCE_ITEMS,
113+
)
114+
115+
116+
def autonomous_replan_obligation(
117+
state_text: str,
118+
*,
119+
agent_todos: dict[str, Any] | None,
120+
) -> dict[str, Any] | None:
121+
return _autonomous_replan_obligation_from_state(
122+
state_text,
123+
agent_todos=agent_todos,
124+
section_headings=AUTONOMOUS_REPLAN_SECTION_HEADINGS,
125+
section_parser=active_state_sections,
126+
section_entries=active_state_section_entries,
127+
public_safe_compact_text=public_safe_compact_text,
128+
build_autonomous_replan_obligation=build_autonomous_replan_obligation,
129+
)

loopx/status.py

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,9 @@
7676
latest_agent_lane_run as _latest_agent_lane_run_read_model,
7777
latest_run_recommended_action_for_projection as _latest_run_recommended_action_for_projection_read_model,
7878
)
79-
from .control_plane.goals.active_state_sections import (
80-
active_state_section_entries as _active_state_section_entries_read_model,
81-
active_state_sections as _active_state_sections_read_model,
82-
)
8379
from .control_plane.goals.active_state_metadata import (
8480
parse_state_frontmatter,
8581
)
86-
from .control_plane.goals.active_state_event_projection import (
87-
active_state_event_projection_fields as _active_state_event_projection_fields_read_model,
88-
state_event_log_candidates as _state_event_log_candidates_read_model,
89-
)
9082
from .control_plane.todos.active_state_todos import (
9183
MONITOR_WRITEBACK_CONTRACT_SCHEMA_VERSION as _MONITOR_WRITEBACK_CONTRACT_SCHEMA_VERSION,
9284
active_state_todo_fields as _active_state_todo_fields_read_model,
@@ -117,11 +109,9 @@
117109
AUTONOMOUS_REPLAN_STALL_THRESHOLD as _AUTONOMOUS_REPLAN_STALL_THRESHOLD_READ_MODEL,
118110
AUTONOMOUS_REPLAN_TRIGGER_PATTERNS as _AUTONOMOUS_REPLAN_TRIGGER_PATTERNS_READ_MODEL,
119111
MAX_AUTONOMOUS_REPLAN_TRIGGERS as _MAX_AUTONOMOUS_REPLAN_TRIGGERS_READ_MODEL,
120-
autonomous_replan_obligation_from_state as _autonomous_replan_obligation_from_state_read_model,
121112
)
122113
from .control_plane.work_items.backlog_hygiene import (
123114
MAX_BACKLOG_HYGIENE_EVIDENCE_ITEMS as _MAX_BACKLOG_HYGIENE_EVIDENCE_ITEMS_READ_MODEL,
124-
backlog_hygiene_warning as _backlog_hygiene_warning_read_model,
125115
)
126116
from .control_plane.goals.dreaming import (
127117
compact_dreaming_lane_badge as _compact_dreaming_lane_badge_read_model,
@@ -236,9 +226,6 @@
236226
from .control_plane.goals.goal_vision import (
237227
compact_goal_vision_packet as _compact_goal_vision_packet_read_model,
238228
)
239-
from .control_plane.work_items.issue_meta_surface import (
240-
parse_issue_meta_surface as _parse_issue_meta_surface_read_model,
241-
)
242229
from .control_plane.work_items.lifecycle import (
243230
goal_lifecycle_fields as _goal_lifecycle_fields_read_model,
244231
ordered_lifecycle_flags as _ordered_lifecycle_flags_read_model,
@@ -279,7 +266,6 @@
279266
compact_todo_group as compact_todo_group,
280267
compact_todo_item as compact_todo_item,
281268
first_open_todo_text,
282-
normalize_todo_text,
283269
open_todo_items,
284270
project_asset_todo_summary,
285271
sync_connected_attention_action_from_todos as _sync_connected_attention_action_from_todos_read_model,
@@ -2024,13 +2010,12 @@ def compact_benchmark_run(run: dict[str, Any]) -> dict[str, Any] | None:
20242010

20252011

20262012
def state_event_log_candidates(goal: dict[str, Any], *, state_path: Path) -> list[Path]:
2027-
return _state_event_log_candidates_read_model(
2028-
goal,
2029-
state_path=state_path,
2030-
resolve_goal_local_path=resolve_goal_local_path,
2031-
event_log_basename=STATE_EVENT_LOG_BASENAME,
2013+
from .control_plane.status.active_state_projection import (
2014+
state_event_log_candidates as _state_event_log_candidates,
20322015
)
20332016

2017+
return _state_event_log_candidates(goal, state_path=state_path)
2018+
20342019

20352020
def active_state_event_projection_fields(
20362021
goal: dict[str, Any],
@@ -2040,70 +2025,62 @@ def active_state_event_projection_fields(
20402025
rollout_events: list[dict[str, Any]] | None = None,
20412026
item_limit: int | None = MAX_STATUS_TODOS_PER_ROLE,
20422027
) -> dict[str, Any]:
2043-
return _active_state_event_projection_fields_read_model(
2028+
from .control_plane.status.active_state_projection import (
2029+
active_state_event_projection_fields as _active_state_event_projection_fields,
2030+
)
2031+
2032+
return _active_state_event_projection_fields(
20442033
goal,
20452034
state_path=state_path,
2046-
resolve_goal_local_path=resolve_goal_local_path,
2047-
parse_active_state_todos=parse_active_state_todos,
20482035
preferred_todo_ids=preferred_todo_ids,
20492036
rollout_events=rollout_events,
20502037
item_limit=item_limit,
2051-
event_log_basename=STATE_EVENT_LOG_BASENAME,
20522038
)
20532039

20542040

20552041
def active_state_sections(state_text: str, headings: tuple[str, ...]) -> dict[str, list[str]]:
2056-
return _active_state_sections_read_model(
2057-
state_text,
2058-
headings,
2059-
section_heading_pattern=SECTION_HEADING_PATTERN,
2042+
from .control_plane.status.active_state_projection import (
2043+
active_state_sections as _active_state_sections,
20602044
)
20612045

2046+
return _active_state_sections(state_text, headings)
2047+
20622048

20632049
def parse_issue_meta_surface(state_text: str) -> dict[str, Any] | None:
2064-
return _parse_issue_meta_surface_read_model(
2065-
state_text,
2066-
section_parser=active_state_sections,
2067-
public_safe_compact_text=public_safe_compact_text,
2050+
from .control_plane.status.active_state_projection import (
2051+
parse_issue_meta_surface as _parse_issue_meta_surface,
20682052
)
20692053

2054+
return _parse_issue_meta_surface(state_text)
2055+
20702056

20712057
def active_state_section_entries(lines: list[str]) -> list[str]:
2072-
return _active_state_section_entries_read_model(
2073-
lines,
2074-
bullet_pattern=BACKLOG_HYGIENE_BULLET_PATTERN,
2075-
normalize_text=normalize_todo_text,
2058+
from .control_plane.status.active_state_projection import (
2059+
active_state_section_entries as _active_state_section_entries,
20762060
)
20772061

2062+
return _active_state_section_entries(lines)
2063+
20782064

20792065
def backlog_hygiene_warning(state_text: str, *, agent_todos: dict[str, Any] | None) -> dict[str, Any] | None:
2080-
return _backlog_hygiene_warning_read_model(
2081-
state_text,
2082-
agent_todos=agent_todos,
2083-
section_headings=BACKLOG_HYGIENE_SECTION_HEADINGS,
2084-
section_parser=active_state_sections,
2085-
bullet_pattern=BACKLOG_HYGIENE_BULLET_PATTERN,
2086-
hint_pattern=BACKLOG_HYGIENE_HINT_PATTERN,
2087-
public_safe_compact_text=public_safe_compact_text,
2088-
max_evidence_items=MAX_BACKLOG_HYGIENE_EVIDENCE_ITEMS,
2066+
from .control_plane.status.active_state_projection import (
2067+
backlog_hygiene_warning as _backlog_hygiene_warning,
20892068
)
20902069

2070+
return _backlog_hygiene_warning(state_text, agent_todos=agent_todos)
2071+
20912072

20922073
def autonomous_replan_obligation(
20932074
state_text: str,
20942075
*,
20952076
agent_todos: dict[str, Any] | None,
20962077
) -> dict[str, Any] | None:
2097-
return _autonomous_replan_obligation_from_state_read_model(
2098-
state_text,
2099-
agent_todos=agent_todos,
2100-
section_headings=AUTONOMOUS_REPLAN_SECTION_HEADINGS,
2101-
section_parser=active_state_sections,
2102-
section_entries=active_state_section_entries,
2103-
public_safe_compact_text=public_safe_compact_text,
2104-
build_autonomous_replan_obligation=build_autonomous_replan_obligation,
2078+
from .control_plane.status.active_state_projection import (
2079+
autonomous_replan_obligation as _autonomous_replan_obligation,
21052080
)
21062081

2082+
return _autonomous_replan_obligation(state_text, agent_todos=agent_todos)
2083+
21072084

21082085
def build_autonomous_replan_obligation(
21092086
evidence: list[dict[str, Any]],

0 commit comments

Comments
 (0)