|
| 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