Skip to content

Commit fdb3589

Browse files
authored
Merge pull request #7825 from keoyyo85/refactor/chat-action-kind-owner
refactor(chat): read the action-kind vocabulary from the store that owns it
2 parents 73c5daa + 1d31f0c commit fdb3589

2 files changed

Lines changed: 96 additions & 17 deletions

File tree

‎loopx/chat_actions.py‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
from .control_plane.coordination.new_goal_storage_transport import initialize_goal_storage_target
1414
from .chat import apply_todo_review_preview, build_todo_review_preview
1515
from .chat_action_normalization import ChatActionNormalizationMixin
16-
from .chat_action_store import ActionConflictError, ChatActionStore
16+
from .chat_action_store import (
17+
ACTION_KINDS as SUPPORTED_ACTION_KINDS,
18+
ActionConflictError,
19+
ChatActionStore,
20+
)
1721
from .chat_goal_lifecycle_actions import ChatGoalLifecycleActionMixin
1822
from .chat_monitor_actions import ChatMonitorActionMixin
1923
from .chat_store import ChatSessionStore
@@ -40,22 +44,7 @@
4044

4145

4246
CHAT_ACTION_RESPONSE_SCHEMA_VERSION = "loopx_chat_action_response_v1"
43-
SUPPORTED_ACTION_KINDS = {
44-
"goal.acceptance.check",
45-
"todo.create",
46-
"todo.update",
47-
"run.correct",
48-
"goal.create",
49-
"goal.update",
50-
"goal.lifecycle",
51-
"agent.bind",
52-
"heartbeat.bind",
53-
"monitor.create",
54-
"monitor.update",
55-
"gate.resolve",
56-
"operation.execute",
57-
"team.plan",
58-
}
47+
5948
# Runtime Endpoint ids and durable Goal agent ids are chosen independently, so
6049
# a family token collapses both onto the host that produced them: Endpoint
6150
# `codex` has to resolve to a registered `codex-main-control`. Every host that
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""The Chat action-kind vocabulary has one owner, so a new kind cannot land twice.
2+
3+
``loopx/chat_action_store.py`` decides which action kinds a proposal may be stored
4+
for, and ``loopx/chat_server.py`` publishes that same set to hosts.
5+
``loopx/chat_actions.py`` kept a byte-identical copy under a different name and
6+
gated dispatch on it, so a kind added to one side stayed invisible to the other
7+
and a proposal could be accepted by the service while the store rejected it.
8+
"""
9+
10+
from __future__ import annotations
11+
12+
import ast
13+
from pathlib import Path
14+
15+
import pytest
16+
17+
from loopx import chat_action_store, chat_actions
18+
19+
REPO_ROOT = Path(__file__).resolve().parents[2]
20+
OWNER_PATH = "loopx/chat_action_store.py"
21+
OWNER_NAME = "ACTION_KINDS"
22+
KIND_VALUE = frozenset(
23+
{
24+
"agent.bind",
25+
"gate.resolve",
26+
"goal.acceptance.check",
27+
"goal.create",
28+
"goal.lifecycle",
29+
"goal.update",
30+
"heartbeat.bind",
31+
"monitor.create",
32+
"monitor.update",
33+
"operation.execute",
34+
"run.correct",
35+
"team.plan",
36+
"todo.create",
37+
"todo.update",
38+
}
39+
)
40+
41+
42+
def set_literal(node: ast.AST) -> frozenset[str] | None:
43+
"""Return the string members of a set literal, including a frozenset call."""
44+
if isinstance(node, ast.Call) and getattr(node.func, "id", "") == "frozenset":
45+
if not node.args:
46+
return None
47+
node = node.args[0]
48+
if not isinstance(node, ast.Set):
49+
return None
50+
members: set[str] = set()
51+
for element in node.elts:
52+
if not isinstance(element, ast.Constant) or not isinstance(element.value, str):
53+
return None
54+
members.add(element.value)
55+
return frozenset(members)
56+
57+
58+
def modules_holding_the_vocabulary() -> list[str]:
59+
offenders: list[str] = []
60+
for path in sorted(REPO_ROOT.glob("loopx/**/*.py")):
61+
relative = path.relative_to(REPO_ROOT).as_posix()
62+
try:
63+
tree = ast.parse(path.read_text(encoding="utf-8"))
64+
except (SyntaxError, UnicodeDecodeError): # pragma: no cover - guarded tree
65+
continue
66+
for statement in tree.body:
67+
if not isinstance(statement, ast.Assign):
68+
continue
69+
vocabulary = set_literal(statement.value)
70+
if vocabulary == KIND_VALUE:
71+
offenders.append(relative)
72+
break
73+
return offenders
74+
75+
76+
def test_the_store_owns_the_declared_action_kind_vocabulary() -> None:
77+
assert frozenset(chat_action_store.ACTION_KINDS) == KIND_VALUE
78+
79+
80+
def test_the_action_service_applies_the_owner_object() -> None:
81+
assert chat_actions.SUPPORTED_ACTION_KINDS is chat_action_store.ACTION_KINDS
82+
83+
84+
def test_no_module_outside_the_owner_holds_its_own_copy() -> None:
85+
assert modules_holding_the_vocabulary() == [OWNER_PATH]
86+
87+
88+
@pytest.mark.parametrize("action_kind", sorted(KIND_VALUE))
89+
def test_every_declared_kind_is_dispatchable(action_kind: str) -> None:
90+
assert action_kind in chat_actions.SUPPORTED_ACTION_KINDS

0 commit comments

Comments
 (0)