Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

test(audit): replace runtime source-inspection tests with behavior tests (#2173)

Call White9872c6f3bce0
diff --git a/config/platform_registry.py b/config/platform_registry.py
index 83a489c96..035c05016 100644
--- a/config/platform_registry.py
+++ b/config/platform_registry.py
@@ -56,7 +56,6 @@ class PlatformDescriptor:
formatter_class: str
credential_fields: tuple[str, ...]
capabilities: PlatformCapabilities
- runtime_reconcile_fields: tuple[str, ...] = ()
# Structural distinction between real IM transports ("im") and the
# always-on in-process Avibe Workbench ("workbench"), which lives in the
# same registry but is never a configurable IM platform. IM-only code paths
@@ -70,12 +69,10 @@ class PlatformDescriptor:
# therefore asserts that this transport's send id was minted by a platform that
# actually reached a person. A transport that mints its own id locally (as
# ``AvibeBot.send_message`` does) must NOT be "im", or an owed notice can be
- # marked delivered when nobody was told. The default stays for the sake of
- # constructors outside the registry, but every descriptor IN ``PLATFORM_REGISTRY``
- # states its kind, and ``test_every_registry_platform_declares_its_kind_explicitly``
- # fails if a new one inherits it instead — a claim this strong should be made,
- # not defaulted into.
- kind: str = "im"
+ # marked delivered when nobody was told. ``kind`` therefore has no default:
+ # a claim this strong must be made by every constructor, not defaulted into.
+ kind: str
+ runtime_reconcile_fields: tuple[str, ...] = ()
@property
def title_key(self) -> str:
diff --git a/core/scheduled_tasks.py b/core/scheduled_tasks.py
index 42d396c13..f204efb14 100644
--- a/core/scheduled_tasks.py
+++ b/core/scheduled_tasks.py
@@ -455,11 +455,9 @@ LADDER_PLATFORM_KIND_UNREGISTERED = "unregistered"
#: locally does not qualify, however IM-shaped it looks — the workbench is exactly that
#: case (``AvibeBot.send_message`` returns a synthetic ``msg_<hex>`` unconditionally),
#: which is why it has a kind of its own rather than a platform-id exception here.
-#: ``PlatformDescriptor.kind`` DEFAULTS to ``"im"``, so a transport added to the
-#: registry without stating its kind would silently take the permissive rows and no
-#: structural test here would notice: the kind axis would be unchanged, the table would
-#: still be total. ``test_every_registry_platform_declares_its_kind_explicitly``
-#: guards that specific accident at the registry, where the claim is made.
+#: ``PlatformDescriptor.kind`` has no default, so a transport cannot enter the
+#: registry without stating its kind and silently take the permissive rows; the table's
+#: totality alone would not notice, because the kind axis would be unchanged.
LADDER_ACK_SOURCES: Mapping[tuple[str, str], str] = MappingProxyType(
{
# A real IM conversation: the send id came from Slack/Discord/Telegram/
diff --git a/tests/scenarios/harness_failure_recovery/catalog.yaml b/tests/scenarios/harness_failure_recovery/catalog.yaml
index 41052f4ae..19acad5e6 100644
--- a/tests/scenarios/harness_failure_recovery/catalog.yaml
+++ b/tests/scenarios/harness_failure_recovery/catalog.yaml
@@ -286,15 +286,15 @@ scenarios:
status: covered
kind: failure
layer: contract
- backend: codex
- test: tests/test_agent_stop_settlement.py::AgentStopSettlementTests::test_codex_stop_emits_stop_semantics
+ backend: shared
+ test: tests/test_agent_stop_settlement.py::AgentStopSettlementTests::test_every_backend_stop_emits_stop_semantics
- id: HFR-040
name: no backend stop may use the terminal-turn default
status: covered
kind: safety
layer: contract
backend: shared
- test: tests/test_agent_stop_settlement.py::AgentStopSettlementTests::test_no_backend_stop_uses_the_terminal_turn_default
+ test: tests/test_agent_stop_settlement.py::AgentStopSettlementTests::test_every_backend_stop_emits_stop_semantics
- id: HFR-041
name: every Harness run records its terminal result text, not just agent_run
status: covered
@@ -1467,13 +1467,10 @@ scenarios:
# claim is made -- a contract comment on ``PlatformDescriptor.kind`` and one beside
# ``LADDER_ACK_SOURCES`` -- all five IM descriptors state ``kind="im"`` explicitly
# instead of inheriting it, and a fourth subordinate test carries this id:
- # ``test_every_registry_platform_declares_its_kind_explicitly``. It reads the
- # registry's AST, because at RUNTIME a defaulted ``kind`` and an explicit
- # ``kind="im"`` are the same object; it also asserts the parsed ids equal the live
- # registry's keys, so a registry the AST can no longer read fails rather than
- # silently passing over nothing. The dataclass default is deliberately KEPT --
- # ``PlatformDescriptor`` is constructed outside the registry too -- so the
- # enforcement sits at the registry, not on the shared constructor.
+ # ``test_a_platform_descriptor_cannot_inherit_the_im_kind``. The test-audit follow-up
+ # replaced the original registry-AST guard by removing the dataclass default
+ # outright: ``PlatformDescriptor`` now refuses construction without ``kind``, so
+ # the enforcement is the constructor itself and the test pins that refusal.
# It takes no id of its own because PR6's block is full at 099 and 100+ is
# contracted to PR2 by the merged plan (docs/plans/harness-run-reliability.md
# §10.7; landed in #1044, PR2 not started per its status header).
diff --git a/tests/test_agent_stop_settlement.py b/tests/test_agent_stop_settlement.py
index 753d4b9d8..be6cc5b70 100644
--- a/tests/test_agent_stop_settlement.py
+++ b/tests/test_agent_stop_settlement.py
@@ -14,8 +14,7 @@ fourth backend cannot miss.
from __future__ import annotations
-import ast
-import inspect
+import asyncio
import sys
import unittest
from pathlib import Path
@@ -31,17 +30,6 @@ from modules.agents.codex.agent import CodexAgent
from modules.agents.opencode.agent import OpenCodeAgent
-def _emitted_message_type(call: ast.Call) -> str | None:
- """The literal message type an ``emit_agent_message`` call passes, if any."""
-
- if len(call.args) >= 2 and isinstance(call.args[1], ast.Constant):
- return call.args[1].value
- for keyword in call.keywords:
- if keyword.arg == "message_type" and isinstance(keyword.value, ast.Constant):
- return keyword.value.value
- return None
-
-
class _StubTurnRegistry:
def __init__(self) -> None:
self._active_turns: dict[str, str] = {}
@@ -91,64 +79,101 @@ class AgentStopSettlementTests(unittest.IsolatedAsyncioTestCase):
self.assertFalse(semantics.settles_run)
self.assertEqual(semantics.settled_by, SETTLED_BY_STOPPED)
- async def test_codex_stop_emits_stop_semantics(self):
- """HFR-039: the live path, not just the helper — Codex as the worked example."""
- agent = object.__new__(CodexAgent)
- agent._session_mgr = SimpleNamespace(get_thread_id=lambda base_session_id: "thread-1")
- agent._turn_registry = _StubTurnRegistry()
- agent._turn_registry._active_turns["session-1"] = "turn-1"
- agent._transports = {
- "/tmp": SimpleNamespace(is_alive=True, send_request=AsyncMock(return_value={}))
- }
- agent._event_handler = SimpleNamespace(clear_pending=lambda turn_id: SimpleNamespace())
- agent._user_stopped_turn_ids = set()
- agent._remove_ack_reaction = AsyncMock()
- emit = AsyncMock()
- agent.controller = SimpleNamespace(emit_agent_message=emit)
- request = SimpleNamespace(base_session_id="session-1", working_path="/tmp", context=object())
-
- self.assertTrue(await agent.handle_stop(request))
-
- semantics = emit.await_args.kwargs["output"]
- self.assertFalse(semantics.settles_run)
- self.assertEqual(semantics.settled_by, SETTLED_BY_STOPPED)
-
- def test_no_backend_stop_uses_the_terminal_turn_default(self):
- """HFR-040: the guard a fourth backend cannot miss.
+ async def test_every_backend_stop_emits_stop_semantics(self):
+ """HFR-039/HFR-040: each backend's live stop path, not just the helper.
- The defect was copied verbatim into three backends, so pinning one behavior
- test would leave the other two free to regress. Asserting on the emit *in
- every* ``handle_stop`` is what makes the shared policy enforceable: a new
+ The defect was copied verbatim into three backends, so pinning one of them
+ would leave the other two free to regress. Each case drives the real
+ ``handle_stop`` to its silent ``result`` and reads the output it emitted: a
backend that reaches for ``terminal_output_for`` in its stop path fails here
instead of silently reporting stopped runs as successes.
"""
- for agent_cls in (CodexAgent, ClaudeAgent, OpenCodeAgent):
- with self.subTest(agent=agent_cls.__name__):
- tree = ast.parse(inspect.getsource(agent_cls.handle_stop).lstrip())
- emits = [
- node
- for node in ast.walk(tree)
- if isinstance(node, ast.Call)
- and isinstance(node.func, ast.Attribute)
- and node.func.attr == "emit_agent_message"
- # Only the terminal ``result`` carries settlement authority. A
- # stop path may also emit ``notify`` (Claude's "cannot be
- # interrupted" / "interrupt failed" refusals), which settles
- # nothing and returns False without ending the turn.
- and _emitted_message_type(node) == "result"
+ for name, build in (
+ ("codex", _codex_stop_case),
+ ("claude", _claude_stop_case),
+ ("opencode", _opencode_stop_case),
+ ):
+ with self.subTest(backend=name):
+ agent, request, emit = build()
+
+ self.assertTrue(await agent.handle_stop(request))
+
+ results = [
+ call for call in emit.await_args_list if call.args[1] == "result"
]
- self.assertTrue(emits, "a stop must answer with a terminal result")
- for call in emits:
- output = next(
- (kw.value for kw in call.keywords if kw.arg == "output"), None
- )
- self.assertIsNotNone(output, "the stop emit must state its semantics")
- self.assertIsInstance(output, ast.Call)
- self.assertEqual(
- getattr(output.func, "id", None),
- "stop_output_for",
- "a stop's empty result must not claim the run's terminal state",
- )
+ self.assertEqual(len(results), 1, "a stop must answer with one terminal result")
+ semantics = results[0].kwargs["output"]
+ self.assertFalse(semantics.settles_run)
+ self.assertEqual(semantics.settled_by, SETTLED_BY_STOPPED)
+
+
+def _codex_stop_case():
+ agent = object.__new__(CodexAgent)
+ agent._session_mgr = SimpleNamespace(get_thread_id=lambda base_session_id: "thread-1")
+ agent._turn_registry = _StubTurnRegistry()
+ agent._turn_registry._active_turns["session-1"] = "turn-1"
+ agent._transports = {
+ "/tmp": SimpleNamespace(is_alive=True, send_request=AsyncMock(return_value={}))
+ }
+ agent._event_handler = SimpleNamespace(clear_pending=lambda turn_id: SimpleNamespace())
+ agent._user_stopped_turn_ids = set()
+ agent._remove_ack_reaction = AsyncMock()
+ emit = AsyncMock()
+ agent.controller = SimpleNamespace(emit_agent_message=emit)
+ request = SimpleNamespace(base_session_id="session-1", working_path="/tmp", context=object())
+ return agent, request, emit
+
+
+def _claude_stop_case():
+ composite_key = "session-1:/tmp"
+ agent = object.__new__(ClaudeAgent)
+ agent.claude_sessions = {composite_key: SimpleNamespace(interrupt=AsyncMock())}
+ agent._pending_requests = {}
+ agent._suppress_receiver_runtime_release = set()
+ agent._cleanup_runtime_session = AsyncMock()
+ agent._mark_session_idle_if_no_pending_requests = lambda _key: True
+ emit = AsyncMock()
+ agent.controller = SimpleNamespace(emit_agent_message=emit)
+ request = SimpleNamespace(
+ base_session_id="session-1",
+ composite_session_id=composite_key,
+ working_path="/tmp",
+ context=SimpleNamespace(platform_specific={}),
+ output=None,
+ stop_failure_reason=None,
+ )
+ return agent, request, emit
+
+
+def _opencode_stop_case():
+ agent = object.__new__(OpenCodeAgent)
+ lock = asyncio.Lock()
+ agent._session_manager = SimpleNamespace(
+ get_session_lock=lambda _base: lock,
+ get_request_session=lambda _base: None,
+ )
+ agent._user_stopped_sessions = set()
+
+ async def _in_flight():
+ await asyncio.Event().wait()
+
+ task = asyncio.get_running_loop().create_task(_in_flight())
+ agent._active_requests = {"session-1": task}
+
+ async def _abort(_base, task, _request_session, *, cancel_before_abort=False):
+ task.cancel()
+ return True
+
+ agent._abort_active_request = _abort
+ emit = AsyncMock()
+ agent.controller = SimpleNamespace(emit_agent_message=emit)
+ request = SimpleNamespace(
+ base_session_id="session-1",
+ working_path="/tmp",
+ context=SimpleNamespace(platform_specific={}),
+ output=None,
+ )
+ return agent, request, emit
if __name__ == "__main__":
diff --git a/tests/test_harness_failure_visibility.py b/tests/test_harness_failure_visibility.py
index 3d93a1602..69ef68f25 100644
--- a/tests/test_harness_failure_visibility.py
+++ b/tests/test_harness_failure_visibility.py
@@ -5465,86 +5465,40 @@ def test_every_ladder_target_class_declares_its_acknowledgement_source() -> None
)
-def test_every_registry_platform_declares_its_kind_explicitly() -> None:
- """Subordinate to HFR-075/079 — the ack policy's permissive row rests on a default.
-
- The test above proves ``LADDER_ACK_SOURCES`` is TOTAL over the axes; it cannot
- prove the axis value is right, and for ``kind`` that value is the whole trust
- boundary. ``("im", channel|user)`` are the permissive rows — a notice sent there
- acks on the id the send returned — and the premise is that such an id was minted
- by a platform that reached a person. ``PlatformDescriptor.kind`` DEFAULTS to
- ``"im"``, so a transport added to the registry without stating its kind inherits
- the permissive answer, and the totality test above stays green because no new
- kind appeared. If that transport mints its own id the way ``AvibeBot.send_message``
- does, every defect in this class reopens with nothing failing.
-
- Checked in the SOURCE rather than at runtime because at runtime the two cases are
- indistinguishable: a defaulted ``kind`` and an explicit ``kind="im"`` produce the
- same object. The registry is a module-level dict literal of direct constructor
- calls, so the AST answers the question exactly — and the assertion that the
- parsed ids equal ``PLATFORM_REGISTRY``'s own keys is what keeps this honest if
- that ever stops being true: a registry the AST can no longer read fails here
- instead of silently passing over nothing.
- """
-
- import ast
- import dataclasses
- import inspect
+def test_a_platform_descriptor_cannot_inherit_the_im_kind() -> None:
+ """Subordinate to HFR-075/079 — the ack policy's permissive row must be claimed.
+
+ ``("im", channel|user)`` are ``LADDER_ACK_SOURCES``' permissive rows: a notice sent
+ there acks on the id the send returned, on the premise that a platform which
+ reached a person minted it. The totality test above cannot check that premise,
+ because a new transport that silently took ``kind="im"`` adds no new axis value.
+ Breaks if ``PlatformDescriptor.kind`` regains a default, which would let a
+ transport that mints its own id (as ``AvibeBot.send_message`` does) inherit the
+ permissive rows with nothing failing.
+ """
- from config import platform_registry
from config.platform_registry import PLATFORM_REGISTRY, PlatformDescriptor
- # The default is real — this test is not asserting a hypothetical.
- kind_field = next(
- field for field in dataclasses.fields(PlatformDescriptor) if field.name == "kind"
- )
- assert kind_field.default == "im", (
- "if kind is mandatory now, this test is obsolete rather than merely passing"
- )
-
- tree = ast.parse(inspect.getsource(platform_registry))
- registry_literal = None
- for node in ast.walk(tree):
- # The registry is an annotated assignment today; accept both spellings so a
- # dropped annotation does not read as a missing registry.
- targets = (
- [node.target]
- if isinstance(node, ast.AnnAssign)
- else node.targets
- if isinstance(node, ast.Assign)
- else []
- )
- if any(
- isinstance(target, ast.Name) and target.id == "PLATFORM_REGISTRY"
- for target in targets
- ):
- registry_literal = node.value
- assert isinstance(registry_literal, ast.Dict), (
- "PLATFORM_REGISTRY is no longer a dict literal; this guard must be rewritten "
- "rather than deleted — the kind axis is still a trust boundary"
- )
-
- undeclared: list[str] = []
- parsed_ids: list[str] = []
- for key, value in zip(registry_literal.keys, registry_literal.values):
- assert isinstance(key, ast.Constant), "every registry key must be a literal id"
- parsed_ids.append(str(key.value))
- assert isinstance(value, ast.Call) and getattr(value.func, "id", "") == (
- "PlatformDescriptor"
- ), f"{key.value} is not a direct PlatformDescriptor(...) call; rewrite this guard"
- if not any(keyword.arg == "kind" for keyword in value.keywords):
- undeclared.append(str(key.value))
-
- assert set(parsed_ids) == set(PLATFORM_REGISTRY), (
- "the AST did not read the live registry, so it proves nothing: parsed "
- f"{sorted(set(parsed_ids))}, registered {sorted(PLATFORM_REGISTRY)}"
- )
- assert not undeclared, (
- f"{undeclared} inherit PlatformDescriptor.kind's default of 'im', which grants "
- "them LADDER_ACK_SOURCES' permissive rows — a failure notice may be marked "
- "delivered on the id their send returns. State kind= explicitly, and if the "
- "transport mints that id itself, it is not 'im'."
- )
+ workbench = PLATFORM_REGISTRY["avibe"]
+ fields = {
+ name: getattr(workbench, name)
+ for name in (
+ "id",
+ "config_key",
+ "config_module",
+ "config_class",
+ "client_module",
+ "client_class",
+ "formatter_module",
+ "formatter_class",
+ "credential_fields",
+ "capabilities",
+ )
+ }
+ with pytest.raises(TypeError, match="kind"):
+ PlatformDescriptor(**fields)
+ assert workbench.kind == "workbench"
+ assert {PLATFORM_REGISTRY[pid].kind for pid in ("slack", "discord", "telegram", "lark", "wechat")} == {"im"}
def test_a_replayed_notice_does_not_finalize_a_live_unrelated_turn(tmp_path: Path) -> None:
diff --git a/tests/test_macos_session_diagnostics.py b/tests/test_macos_session_diagnostics.py
index 4a2451da2..f64d7daf8 100644
--- a/tests/test_macos_session_diagnostics.py
+++ b/tests/test_macos_session_diagnostics.py
@@ -391,7 +391,11 @@ def test_service_main_owns_monitor_shutdown(monkeypatch) -> None:
monkeypatch.setattr(service_main, "load_config", lambda: loaded_config)
monkeypatch.setattr(service_main, "setup_logging", lambda level: None)
monkeypatch.setattr(service_main, "apply_claude_sdk_patches", lambda: None)
- monkeypatch.setattr(service_main, "prepare_sqlite_state", lambda loaded: report)
+ monkeypatch.setattr(
+ service_main,
+ "prepare_sqlite_state",
+ lambda loaded: calls.append("sqlite.prepare") or report,
+ )
monkeypatch.setattr(
service_main,
"start_macos_session_diagnostics",
@@ -409,6 +413,10 @@ def test_service_main_owns_monitor_shutdown(monkeypatch) -> None:
service_main.main()
+ # The migration runs under the lock and before anything is constructed
+ # against the schema it produces.
+ assert calls.index("lock.acquire") < calls.index("sqlite.prepare")
+ assert calls.index("sqlite.prepare") < calls.index("controller.init")
assert calls.index("diagnostics.start") < calls.index("controller.run")
assert calls.index("controller.run") < calls.index("controller.shutdown:signal 15")
assert calls.index("controller.shutdown:signal 15") < calls.index(
diff --git a/tests/test_opencode_stop_receipt.py b/tests/test_opencode_stop_receipt.py
index c9b69311e..fe4890a84 100644
--- a/tests/test_opencode_stop_receipt.py
+++ b/tests/test_opencode_stop_receipt.py
@@ -25,8 +25,11 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from config.v2_sessions import ActivePollInfo
from core.native_dispatch_phase import mark_prewrite_user_stop
from core.processing_indicator import STOPPED_REACTION_EMOJI, ProcessingIndicatorService
+from modules.agents.base import AgentRequest
from modules.agents.opencode.agent import OpenCodeAgent
from modules.agents.opencode.poll_loop import OpenCodePollLoop
+from modules.agents.opencode.session import OpenCodeSessionManager
+from modules.im.base import MessageContext
def _agent():
@@ -329,17 +332,6 @@ class OpenCodeStopReceiptTests(unittest.TestCase):
instead: the branch must read the intent set and ask for the receipt.
"""
- def test_cancellation_branch_reads_the_intent(self):
- source = inspect.getsource(OpenCodeAgent._process_message)
- cleanup = inspect.getsource(OpenCodeAgent._finish_prestart_cancellation)
- claim = inspect.getsource(OpenCodeAgent._claim_user_stop_receipt)
-
- self.assertIn("_finish_prestart_cancellation", source)
- self.assertIn("_claim_user_stop_receipt", cleanup)
- self.assertIn("consume_user_stop_intent", claim)
- self.assertIn("prewrite_user_stop_requested", claim)
- self.assertIn("STOPPED_REACTION_EMOJI", cleanup)
-
def test_restored_cancellation_branch_reads_the_intent(self):
source = inspect.getsource(OpenCodePollLoop.run_restored_poll_loop)
_, _, after = source.partition("except asyncio.CancelledError:")
@@ -351,5 +343,159 @@ class OpenCodeStopReceiptTests(unittest.TestCase):
self.assertIn("restored_request", branch)
+class _StubOpenCodeServer:
+ def __init__(self, *, block_start: bool = False):
+ self.block_start = block_start
+ self.start_entered = asyncio.Event()
+ self.aborted: list[tuple[str, str]] = []
+
+ async def ensure_running(self):
+ if self.block_start:
+ self.start_entered.set()
+ await asyncio.Event().wait()
+
+ async def list_messages(self, **_kwargs):
+ return []
+
+ async def prompt_async(self, **_kwargs):
+ return None
+
+ async def mark_run_active(self, _session_id):
+ return None
+
+ async def mark_run_inactive(self, _session_id):
+ return None
+
+ async def abort_session(self, session_id, directory):
+ self.aborted.append((session_id, directory))
+
+ def get_default_agent_from_config(self):
+ return None
+
+ def get_agent_reasoning_effort_from_config(self, _agent):
+ return None
+
+
+class _BlockingPollLoop:
+ def __init__(self):
+ self.entered = asyncio.Event()
+
+ async def run_prompt_poll(self, *_args, **_kwargs):
+ self.entered.set()
+ await asyncio.Event().wait()
+
+
+class _StubSessionManager(OpenCodeSessionManager):
+ async def ensure_working_dir(self, _path):
+ return None
+
+ async def get_or_create_session_id(self, _request, _server):
+ return "oc-session"
+
+
+def _process_message_agent(server, poll_loop=None):
+ """An OpenCode agent whose real ``handle_message``/``handle_stop`` pair
+ drives the real ``_process_message`` against a stub server."""
+
+ polls: dict[str, object] = {}
+ sessions = SimpleNamespace(
+ add_active_poll=lambda **kwargs: polls.__setitem__(kwargs["opencode_session_id"], kwargs),
+ remove_active_poll=lambda session_id: polls.pop(session_id, None),
+ get_all_active_polls=lambda: dict(polls),
+ )
+ agent = OpenCodeAgent.__new__(OpenCodeAgent)
+ agent.controller = SimpleNamespace(
+ config=SimpleNamespace(platform="slack", reply_enhancements=False, remote_access=None, language="en"),
+ processing_indicator=SimpleNamespace(snapshot_request=lambda _request: {}),
+ get_opencode_overrides=lambda _context: (None, None, None),
+ emit_agent_message=AsyncMock(),
+ )
+ agent.config = agent.controller.config
+ agent.sessions = sessions
+ agent.opencode_config = SimpleNamespace(error_retry_limit=0)
+ agent._session_manager = _StubSessionManager(None, "opencode")
+ agent._poll_loop = poll_loop or _BlockingPollLoop()
+ agent._steering_states = {}
+ agent._active_requests = {}
+ agent._user_stopped_sessions = set()
+ agent._delete_ack = AsyncMock()
+ agent._remove_ack_reaction = AsyncMock()
+
+ async def _get_server():
+ return server
+
+ agent._get_server = _get_server
+ return agent
+
+
+def _process_message_request():
+ return AgentRequest(
+ context=MessageContext(
+ user_id="u",
+ channel_id="c",
+ platform="slack",
+ platform_specific={"agent_session_id": "ses_test"},
+ ),
+ message="hello",
+ user_message="hello",
+ working_path="/tmp/work",
+ base_session_id="base",
+ composite_session_id="base:/tmp/work",
+ session_key="slack::c",
+ vibe_agent_model="fixture-provider/fixture-model",
+ )
+
+
+class OpenCodeProcessMessageStopReceiptTests(unittest.IsolatedAsyncioTestCase):
+ """A /stop must reach the request coroutine's own cancellation branch.
+
+ ``handle_stop`` only publishes the intent; ``_process_message`` owns the 👀
+ and is the only place that trades it for the ⏹️ receipt, whether the stop
+ lands before the server has started or while the poll loop is running.
+ """
+
+ async def _stop_while(self, agent, server, entered: asyncio.Event):
+ request = _process_message_request()
+ message_task = asyncio.create_task(agent.handle_message(request))
+ await asyncio.wait(
+ {message_task, asyncio.create_task(entered.wait())},
+ timeout=5,
+ return_when=asyncio.FIRST_COMPLETED,
+ )
+ self.assertTrue(entered.is_set(), "the request never reached the stop point")
+
+ self.assertTrue(await agent.handle_stop(_process_message_request()))
+ await message_task
+ return request
+
+ async def test_stop_during_the_poll_loop_leaves_the_stopped_receipt(self):
+ server = _StubOpenCodeServer()
+ poll_loop = _BlockingPollLoop()
+ agent = _process_message_agent(server, poll_loop)
+
+ request = await self._stop_while(agent, server, poll_loop.entered)
+
+ agent._remove_ack_reaction.assert_awaited_once_with(
+ request,
+ terminal_emoji=STOPPED_REACTION_EMOJI,
+ )
+ self.assertEqual(server.aborted, [("oc-session", "/tmp/work")])
+ self.assertEqual(agent.sessions.get_all_active_polls(), {})
+ self.assertEqual(agent._user_stopped_sessions, set())
+
+ async def test_stop_before_the_server_starts_leaves_the_stopped_receipt(self):
+ server = _StubOpenCodeServer(block_start=True)
+ agent = _process_message_agent(server)
+
+ request = await self._stop_while(agent, server, server.start_entered)
+
+ agent._remove_ack_reaction.assert_awaited_once_with(
+ request,
+ terminal_emoji=STOPPED_REACTION_EMOJI,
+ )
+ self.assertEqual(server.aborted, [])
+ self.assertEqual(agent._user_stopped_sessions, set())
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/test_platform_registry.py b/tests/test_platform_registry.py
index 49e2327c8..134b48bfe 100644
--- a/tests/test_platform_registry.py
+++ b/tests/test_platform_registry.py
@@ -64,6 +64,7 @@ def test_registry_addition_drives_platform_validation_and_readiness(monkeypatch)
client_class="Unused",
formatter_module="unused",
formatter_class="Unused",
+ kind="im",
credential_fields=("token",),
capabilities=PlatformCapabilities(
supports_channels=True,
diff --git a/tests/test_projects_service.py b/tests/test_projects_service.py
index 5b29f5c5e..ea26e732b 100644
--- a/tests/test_projects_service.py
+++ b/tests/test_projects_service.py
@@ -405,71 +405,64 @@ def test_project_mutations_follow_instance_operations_and_lower_role_acl(engine,
assert project_access_service.get_project_policy(conn, project["id"]) == policy
-def test_every_project_entry_point_resolves_through_the_visibility_check():
- """Every way into a Project row goes through the check ``list_projects`` uses.
-
- The enumeration is taken from the module, not written down here: each round
- of review found one more entry point that resolved a Project without the
- ACL -- first mutation by id, then create-or-reuse by folder path -- because
- each fix named the paths it knew about. Reading the call graph instead means
- an entry point added later is covered on the day it is added, and one that
- stops applying the check fails here rather than in a review.
-
- "Reaching" is transitive on purpose: ``create_project`` never calls the
- check itself, it calls the resolver that does, which is exactly where the
- check belongs -- at the lookup rather than at each of its callers.
+def _list_entry(conn, project_id, context):
+ listed = projects_service.list_projects(conn, include_archived=True, authorization_context=context)
+ if project_id not in {project["id"] for project in listed}:
+ raise LookupError(project_id)
+
+
+_PROJECT_ENTRY_POINTS = {
+ "list_projects": _list_entry,
+ "get_project": lambda conn, project_id, context: projects_service.get_project(
+ conn, project_id, authorization_context=context
+ ),
+ "get_project_workdir": lambda conn, project_id, context: projects_service.get_project_workdir(
+ conn, project_id, authorization_context=context
+ ),
+ "create_project": lambda conn, project_id, context: projects_service.create_project(
+ conn, projects_service.get_project_workdir(conn, project_id), authorization_context=context
+ ),
+ "update_project": lambda conn, project_id, context: projects_service.update_project(
+ conn, project_id, display_name="Denied", authorization_context=context
+ ),
+ "archive_project": lambda conn, project_id, context: projects_service.archive_project(
+ conn, project_id, authorization_context=context
+ ),
+ "reorder_projects": lambda conn, project_id, context: projects_service.reorder_projects(
+ conn, [project_id], expected_order=[project_id], authorization_context=context
+ ),
+}
+
+
[email protected]("role", ["editor", "viewer"])
[email protected]("entry_point", sorted(_PROJECT_ENTRY_POINTS))
+def test_restricted_project_is_unreachable_through_every_entry_point(engine, tmp_path, role, entry_point):
+ """An excluded lower-role caller cannot reach a restricted Project by any door.
+
+ Breaks if one entry point resolves the Project without the ACL that
+ ``list_projects`` applies -- e.g. ``get_project_workdir`` dropping its
+ ``can_read_project`` check would hand the host cwd of a hidden Project to
+ Skills and the instruction editor. The mutation doors are denied by the
+ instance-role floor today; they stay in the table so a relaxed floor still
+ has to answer to the Project ACL.
"""
- import ast
- import inspect
-
- gates = {"_require_visible_project", "can_read_project", "filter_accessible_projects"}
- tree = ast.parse(inspect.getsource(projects_service))
- functions = {
- node.name: node
- for node in tree.body
- if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
- }
-
- def _calls(node: ast.AST) -> set[str]:
- names: set[str] = set()
- for child in ast.walk(node):
- if not isinstance(child, ast.Call):
- continue
- func = child.func
- if isinstance(func, ast.Name):
- names.add(func.id)
- elif isinstance(func, ast.Attribute):
- names.add(func.attr)
- return names
-
- def _reaches_gate(name: str, seen: frozenset[str] = frozenset()) -> bool:
- if name in seen or name not in functions:
- return False
- called = _calls(functions[name])
- if called & gates:
- return True
- return any(_reaches_gate(callee, seen | {name}) for callee in called)
-
- # Public + takes a Connection: that is precisely the set of functions that
- # return or mutate Project rows on behalf of an HTTP caller. ``make_directory``
- # touches no rows and takes no connection, so it falls out by construction.
- entry_points = {
- name
- for name, node in functions.items()
- if not name.startswith("_")
- and any(arg.arg == "conn" for arg in node.args.args)
- }
- assert entry_points >= {
- "list_projects",
- "get_project",
- "get_project_workdir",
- "create_project",
- "update_project",
- "archive_project",
- }
- ungated = sorted(name for name in entry_points if not _reaches_gate(name))
- assert ungated == [], f"Project entry points that never reach a visibility check: {ungated}"
+ folder = tmp_path / "restricted"
+ folder.mkdir()
+ excluded = _acl_context(role, email="[email protected]")
+ included = _acl_context(role, email="[email protected]")
+ call = _PROJECT_ENTRY_POINTS[entry_point]
+ with engine.begin() as conn:
+ project = projects_service.create_project(conn, str(folder), display_name="Restricted")
+ _restrict_project_to(conn, project["id"], "[email protected]")
+ if entry_point in {"list_projects", "get_project", "get_project_workdir"}:
+ call(conn, project["id"], included)
+ # A reorder naming a hidden id is a stale view to the caller, not a hit.
+ with pytest.raises((LookupError, InstanceAuthorizationError, projects_service.ProjectOrderConflict)):
+ call(conn, project["id"], excluded)
+ payload = projects_service.get_project(conn, project["id"])
+ assert payload["display_name"] == "Restricted"
+ assert payload["archived"] is False
@pytest.mark.parametrize("role", ["member", "owner", "editor", "viewer"])
diff --git a/tests/test_sqlite_state_startup.py b/tests/test_sqlite_state_startup.py
index bd696f07f..d2321c148 100644
--- a/tests/test_sqlite_state_startup.py
+++ b/tests/test_sqlite_state_startup.py
@@ -1,8 +1,5 @@
from __future__ import annotations
-import ast
-import inspect
-import textwrap
from pathlib import Path
from types import SimpleNamespace
@@ -41,34 +38,3 @@ def test_prepare_sqlite_state_uses_config_primary_platform(monkeypatch) -> None:
assert calls == ["slack"]
assert report.imported is True
-
-def test_the_migration_runs_under_the_lock_and_before_the_controller_exists() -> None:
- """The order `main()` still owns, after readiness moved out of it.
-
- The lock is taken first because the migration has to run under it, so the
- lock says only that a process got as far as trying. The migration then runs
- before anything is constructed against the schema it produces.
-
- This test used to end by pinning `mark_service_instance_started()` between
- the controller and `run()`, on the reasoning that building the controller was
- the last thing a new release could break structurally. That reasoning was
- wrong: `run()` starts the checkpoint service, the dispatch server and the IM
- runtime, any of which a new release can fail, and each of which it catches
- and returns from -- so a release that never served was being announced as
- serving. Readiness now belongs to `run()`, and the tests for it are in
- `tests/test_service_readiness.py`. What remains here is the ordering that is
- genuinely `main()`'s, asserted structurally because the failure mode is a
- later edit moving a step for a plausible-sounding reason.
- """
-
- tree = ast.parse(textwrap.dedent(inspect.getsource(service_main.main)))
- lines = {}
- for node in ast.walk(tree):
- if not isinstance(node, ast.Call):
- continue
- name = node.func.id if isinstance(node.func, ast.Name) else getattr(node.func, "attr", None)
- lines.setdefault(name, node.lineno)
-
- assert lines["acquire_service_instance_lock"] < lines["prepare_sqlite_state"]
- assert lines["prepare_sqlite_state"] < lines["Controller"]
- assert lines["Controller"] < lines["run"]
diff --git a/tests/test_ui_show_pages.py b/tests/test_ui_show_pages.py
index 8252a7790..6fff34dcb 100644
--- a/tests/test_ui_show_pages.py
+++ b/tests/test_ui_show_pages.py
@@ -9200,34 +9200,80 @@ def test_recovery_fact_consumption_census_has_no_default_producer():
assert "X-Avibe-Show-Recovery-Poll" not in recovery_source
-def test_runtime_http_transport_census_closes_every_direct_client_path():
- manager_tree = ast.parse(textwrap.dedent(inspect.getsource(ShowRuntimeManager)))
- functions = {
- node.name: node
- for node in manager_tree.body[0].body
- if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
- }
- direct_clients = {
- name
- for name, function in functions.items()
- if any(isinstance(node, ast.Attribute) and node.attr == "AsyncClient" for node in ast.walk(function))
- }
- assert direct_clients == {
- "_healthy",
- "_probe_capabilities_payload",
- "_request_runtime_transport",
- }
- for request_owner in ("request", "request_global"):
- calls = [
- node
- for node in ast.walk(functions[request_owner])
- if isinstance(node, ast.Call)
- and isinstance(node.func, ast.Attribute)
- and node.func.attr == "_request_runtime_transport"
- ]
- assert len(calls) == 1
- for internal_probe in ("_healthy", "_probe_capabilities_payload"):
- assert any(isinstance(node, ast.Try) and node.handlers for node in ast.walk(functions[internal_probe]))
+def test_show_runtime_startup_health_probe_turns_transport_failure_into_health_timeout(monkeypatch, tmp_path):
+ """A refused ``/health`` connection is a retryable probe miss, not a crash.
+
+ Breaks if ``_healthy`` lets a transport error escape: ``ensure()`` would then
+ raise out of startup instead of retrying until the shared deadline and
+ reporting ``runtime_start_health_timeout``. Every other startup test replaces
+ ``_healthy`` wholesale, so only this one runs its real client path.
+ """
+
+ class FakeProcess:
+ def poll(self):
+ return None
+
+ manager = ShowRuntimeManager(
+ command="/bin/runtime-cli",
+ workspace_root=tmp_path / "show",
+ runtime_dir=tmp_path / "runtime",
+ )
+ health_urls = []
+
+ async def refuse(_client, url, **_kwargs):
+ health_urls.append(url)
+ if len(health_urls) == 2:
+ # Jump the loop clock past the startup deadline on the second refusal,
+ # so the retry count does not depend on how fast a CI worker runs.
+ loop = asyncio.get_running_loop()
+ real_time = loop.time
+ loop.time = lambda: real_time() + 3600
+ raise httpx.ConnectError("connection refused", request=httpx.Request("GET", url))
+
+ def fake_stop():
+ manager._process = None
+ manager._base_url = None
+
+ monkeypatch.setattr("core.show_runtime._STARTUP_READY_TIMEOUT_SECONDS", 600.0)
+ monkeypatch.setattr("core.show_runtime._STARTUP_POLL_INTERVAL_SECONDS", 0.001)
+ monkeypatch.setattr("core.show_runtime._resolve_command", lambda command: [command])
+ monkeypatch.setattr("core.show_runtime.subprocess.Popen", lambda *_args, **_kwargs: FakeProcess())
+ monkeypatch.setattr(
+ manager,
+ "_read_startup_url",
+ lambda *, deadline: asyncio.sleep(0, result="http://127.0.0.1:12345"),
+ )
+ monkeypatch.setattr("core.show_runtime.httpx.AsyncClient.get", refuse)
+ monkeypatch.setattr(manager, "stop", fake_stop)
+
+ result = asyncio.run(manager.ensure())
+
+ assert result.available is False
+ assert result.reason == "runtime_start_health_timeout"
+ assert health_urls == ["http://127.0.0.1:12345/health"] * 2
+
+
+def test_show_runtime_capability_probe_treats_transport_failure_as_unknown(monkeypatch, tmp_path):
+ """A refused ``/capabilities`` connection answers "unknown", never raises.
+
+ Breaks if ``_probe_capabilities_payload`` stops catching transport errors:
+ capability negotiation would raise instead of falling back to the retry path
+ that a ``None`` answer selects.
+ """
+
+ manager = ShowRuntimeManager(
+ command="/bin/runtime-cli",
+ workspace_root=tmp_path / "show",
+ runtime_dir=tmp_path / "runtime",
+ )
+
+ async def refuse(_client, url, **_kwargs):
+ raise httpx.ConnectError("connection refused", request=httpx.Request("GET", url))
+
+ monkeypatch.setattr("core.show_runtime.httpx.AsyncClient.get", refuse)
+
+ assert asyncio.run(manager._probe_capabilities_payload("http://127.0.0.1:12345")) is None
+ assert asyncio.run(manager._probe_render_markdown_capability("http://127.0.0.1:12345")) is None
def test_provider_install_entrypoints_converge_on_single_admission_owner():