Raven-1/tests/test_rpc_bootstrap.py
Dizhan Xue 182dacac00
fix(rpc): stop the owned skill watcher during teardown (#910)
## Summary

An RPC stack that built its own engine left the skill watcher running
after teardown. Stop that watcher before draining the owned stores, and
continue the remaining cleanup if stopping it raises.

A stack mounted on a host-owned engine leaves the host's watcher
running. The regression uses a real local watcher and checks repeated
teardown and failure isolation.

## Type

- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

- [ ] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed

Native Windows, Python 3.12. PYTHONUTF8=1; the existing environment was
selected through UV_PROJECT_ENVIRONMENT, and PYTHONPATH pointed at this
checkout.

```text
uv run --no-sync pytest -o addopts= tests/test_rpc_bootstrap.py -k "skill_watcher or failed_watcher" -q --tb=short --show-capture=no
```

Result: 3 passed in the targeted selection. The broader RPC/serve
selection previously produced 136 passed, 2 failed and 26 skipped on
Windows. Both failures reproduced on the unmodified base: owner-only
POSIX mode bits and the supervisor SIGTERM cleanup test. The
relevant-tests box is left unchecked because that broader selection is
not all green.

Whole-branch gates:

```powershell
$changed = @(git diff --name-only origin/main...HEAD -- '*.py')
$production = @($changed | Where-Object { $_ -like 'raven/*' })
uv run --no-sync ruff check @changed
uv run --no-sync ruff format --check @changed
uv run --no-sync ty check @production
uv run --no-sync lint-imports
uv run --no-sync pre-commit run --from-ref origin/main --to-ref HEAD
uv run --no-sync python scripts/check_commit_messages.py origin/main..HEAD
uv run --no-sync python scripts/check_source_language.py origin/main...HEAD
uv run --no-sync python scripts/check_large_files.py origin/main...HEAD
git diff --check origin/main...HEAD
```

All listed gates passed. The whole-branch review covered callers,
ownership, guard boundaries, failure paths, regression strength,
architecture and compatibility. No screenshot change applies; directly
affected code documentation is updated where needed. The full repository
suite and Linux/macOS execution were not run locally; the local Docker
Linux engine was unavailable.

## Risk

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

Cleanup applies only to the engine owned by this RPC stack. Mounted
stacks retain host ownership. Stop failures are logged without
preventing the remaining resources from closing. Revert the squash
commit to roll back.

## Related Issues

#490
2026-10-12 22:55:13 +08:00

686 lines
26 KiB
Python

"""``build_rpc_stack`` over a host-owned agent loop (the gateway hosting the page).
The default path builds its own engine and owns its whole lifecycle. The
mounted path is handed the gateway's loop and must leave everything
process-scoped -- the subagent submit, cron, the memory backend -- to the
host, or one engine ends up double-wired by two spines. What it must still
take are the page-facing hooks: while the page is mounted, it is the surface
that renders a question or a progress stream.
"""
from __future__ import annotations
import asyncio
import inspect
import json
from pathlib import Path
from types import SimpleNamespace
import pytest
from raven.rpc import bootstrap
from raven.rpc.subscriptions import COALESCE_WINDOW_S
class _FakeCron:
def __init__(self) -> None:
self.on_job = None
self.started = False
self.stopped = False
self.last_startup_drops: list = []
async def start(self) -> None:
self.started = True
def stop(self) -> None:
self.stopped = True
class _FakeSubagents:
def __init__(self, order: list | None = None) -> None:
self.submit = None
self.delivery_sink = None
# Shared with the loop, so teardown order is assertable across both.
self.order = order if order is not None else []
def set_submit(self, fn) -> None:
self.submit = fn
def set_delivery_sink(self, fn) -> None:
self.delivery_sink = fn
async def cancel_all(self, *, reason: str = "") -> None:
# Reached by an owning stack's teardown, which logs and swallows what
# this raises -- so without it a real assertion failure in such a test
# is reported behind an AttributeError traceback that is not the bug.
self.order.append("cancel_all")
class _FakeAskUser:
"""The one tool build_rpc_stack hands its broker to."""
def __init__(self) -> None:
self.broker = None
def set_broker(self, broker) -> None:
self.broker = broker
class _FakeLoop:
"""The AgentLoop surface build_rpc_stack touches, each hook recorded."""
def __init__(self, cron: _FakeCron | None = None) -> None:
self.tools: dict = {"ask_user": _FakeAskUser()}
self.order: list[str] = []
self.subagents = _FakeSubagents(self.order)
self.mcp_closed = 0
self.cron_service = cron
self.backend = None
self.dag_sink = None
self.mcp_sink = None
self.prewarms = 0
# Whether the MCP event sink was already bound when the prewarm started.
# The URL an OAuth server parks on rides that sink and nothing else.
self.sink_at_prewarm: object = "never called"
def set_dag_progress_sink(self, sink) -> None:
self.dag_sink = sink
def set_mcp_event_sink(self, sink) -> None:
self.mcp_sink = sink
def prewarm_mcp(self) -> None:
self.prewarms += 1
self.sink_at_prewarm = self.mcp_sink
async def close_mcp(self) -> None:
self.mcp_closed += 1
self.order.append("close_mcp")
async def _sink(_frame: dict) -> None:
pass
class _RecordingBackend:
"""Records the teardown order the owning stack puts it through."""
def __init__(self, order: list[str]) -> None:
self._order = order
async def start(self) -> None:
pass
async def stop(self) -> None:
self._order.append("stop")
async def recall(self, query, *, user_id=None, agent_id=None, top_k):
return []
async def store(self, session_id, messages, *, metadata=None):
return True
async def feedback(self, signals):
pass
async def test_the_owning_stack_drains_queued_writes_before_stopping_the_backend(monkeypatch) -> None:
"""serve and ACP own turns too, and dispatch now returns before the write
lands. Stopping the backend without draining closes the HTTP client the
queued writes still need, so this path lost its last turns silently while
the agent, TUI and gateway hosts did not."""
order: list[str] = []
loop = _FakeLoop(_FakeCron())
loop.backend = _RecordingBackend(order)
async def _drain(*_a, **_kw) -> None:
order.append("drain")
loop.drain_backend_stores = _drain
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
stack = await bootstrap.build_rpc_stack(_sink, agent_loop=None)
assert stack.agent_loop is loop
await stack.teardown()
assert order == ["drain", "stop"], order
async def test_the_stack_sizes_its_turn_pools_from_the_config(monkeypatch) -> None:
"""serve, the page and every ACP worker build through here and used to get
the spine's built-in 1/1 whatever the config said, so a second conversation
or a second task on a pooled sub-agent queued behind the first."""
loop = _FakeLoop(_FakeCron())
import raven.rpc.spine as spine_module
seen: dict = {}
real = spine_module.build_rpc_spine
def spy(*args, **kwargs):
seen.update(kwargs)
return real(*args, **kwargs)
# Imported inside the factory, so the seam is the spine module's own name.
monkeypatch.setattr(spine_module, "build_rpc_spine", spy)
monkeypatch.setattr(bootstrap, "_turn_pools", lambda: (7, 3))
stack = await bootstrap.build_rpc_stack(_sink, agent_loop=loop)
try:
assert (seen["user_pool"], seen["system_pool"]) == (7, 3)
finally:
await stack.teardown()
def test_the_turn_pools_come_from_the_gateway_section_and_zero_is_allowed(tmp_path, monkeypatch) -> None:
path = tmp_path / "config.json"
monkeypatch.setattr("raven.config.loader.get_config_path", lambda: path)
assert bootstrap._turn_pools() == (4, 2), "no file: the schema defaults"
path.write_text(json.dumps({"gateway": {"userPool": 0, "systemPool": 0}}))
assert bootstrap._turn_pools() == (0, 0)
async def test_a_shared_loop_is_used_not_rebuilt(monkeypatch) -> None:
def _boom(**_):
raise AssertionError("a mounted stack must not build a second engine")
monkeypatch.setattr(bootstrap, "build_agent_loop", _boom)
cron = _FakeCron()
loop = _FakeLoop(cron)
stack = await bootstrap.build_rpc_stack(_sink, agent_loop=loop)
try:
assert stack.agent_loop is loop
assert stack.build_error is None
# Host lifecycle untouched: the subagent submit stays the gateway's
# (its hubs can route any channel's delivery; this stack's hub only
# knows the page's), and cron was neither rewired nor started. The
# stack's own spine is exposed instead, for the host to route the
# relays that belong to page sessions onto.
assert loop.subagents.submit is None
assert stack.turn_scheduler is not None
assert cron.on_job is None
assert cron.started is False
# Page-facing hooks applied, so the host mounting this stack after its
# own wiring hands the question and progress surfaces to the page.
# The page's broker is exposed so a host with a question surface of
# its own can build a RoutingQuestionBroker over both.
assert stack.question_broker is loop.tools["ask_user"].broker is not None
assert loop.dag_sink is not None
assert loop.mcp_sink is not None
assert loop.subagents.delivery_sink is not None
# The engine is the host's and the host's ``run()`` already connected
# it; a second prewarm here would re-walk every server it attached.
assert loop.prewarms == 0
finally:
await stack.teardown()
# Teardown stops only what this stack built; cron is the host's to stop.
assert cron.stopped is False
async def test_the_snapshot_backfill_is_scheduled_except_on_the_acp_channel(monkeypatch) -> None:
"""An acp row's statefulness comes from its capability snapshot, and this
hook is the only writer that needs no human -- unscheduled, a fresh install
reads every acp agent stateless and the instance picker hides them until
someone opens a settings page and clicks Test. On the acp channel the stack
is itself a subagent child, where verifying would cascade (each child
verifying its own row launches another child).
"""
from raven.agent.subagent import probe
scheduled: list = []
monkeypatch.setattr(probe, "schedule_snapshot_verification", lambda manager: scheduled.append(manager) or None)
loop = _FakeLoop(_FakeCron())
stack = await bootstrap.build_rpc_stack(_sink, agent_loop=loop)
try:
assert scheduled == [loop.subagents]
finally:
await stack.teardown()
acp_loop = _FakeLoop(_FakeCron())
acp_stack = await bootstrap.build_rpc_stack(_sink, agent_loop=acp_loop, channel="acp")
try:
assert scheduled == [loop.subagents]
finally:
await acp_stack.teardown()
async def test_the_default_path_still_owns_the_whole_lifecycle(monkeypatch) -> None:
from raven import browser as browser_module
class _NoBrowser:
async def close(self) -> None:
pass
monkeypatch.setattr(browser_module, "get_browser", lambda: _NoBrowser())
cron = _FakeCron()
loop = _FakeLoop(cron)
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
stack = await bootstrap.build_rpc_stack(_sink)
assert stack.agent_loop is loop
assert loop.subagents.submit is not None
assert cron.started is True
assert cron.on_job is not None
await stack.teardown()
assert cron.stopped is True
async def test_the_owning_path_prewarms_mcp_instead_of_charging_the_first_turn(monkeypatch) -> None:
"""This stack never starts ``run()``, and ``run()`` is where the one-time MCP
connect lived -- so the cost landed on whatever turn arrived first (measured:
4.52s of handshake before the turn body began). The prewarm has to be ordered
after the event sink is bound, or an OAuth server's authorization URL is
minted with nobody to publish it to.
"""
from raven import browser as browser_module
class _NoBrowser:
async def close(self) -> None:
pass
monkeypatch.setattr(browser_module, "get_browser", lambda: _NoBrowser())
loop = _FakeLoop(_FakeCron())
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
stack = await bootstrap.build_rpc_stack(_sink)
try:
assert loop.prewarms == 1
assert loop.sink_at_prewarm is loop.mcp_sink
assert loop.sink_at_prewarm is not None
finally:
await stack.teardown()
async def test_the_served_page_hears_reminders_dropped_at_startup(monkeypatch) -> None:
"""``start()`` drops past-due one-shot reminders; whoever starts cron owns
telling someone. This path is the served page's, and it runs before any
client has subscribed -- so the assertion is that the notice survives that
gap and reaches the first subscription, not merely that a call was made.
"""
from raven import browser as browser_module
from raven.proactive_engine.schedulers.cron.types import CronStartupDrop
class _NoBrowser:
async def close(self) -> None:
pass
class _DroppingCron(_FakeCron):
async def start(self) -> None:
await super().start()
self.last_startup_drops = [CronStartupDrop(name="pills", message="take the pills", at_ms=1749024000000)]
monkeypatch.setattr(browser_module, "get_browser", lambda: _NoBrowser())
cron = _DroppingCron()
loop = _FakeLoop(cron)
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
frames: list[dict] = []
async def _record(frame: dict) -> None:
frames.append(frame)
stack = await bootstrap.build_rpc_stack(_record)
try:
assert [f for f in frames if f.get("method") == "event"] == []
await stack.emitter.register("tui:default")
await asyncio.sleep(COALESCE_WINDOW_S * 3)
finally:
await stack.teardown()
events = [f["params"]["event"] for f in frames if f.get("method") == "event"]
assert [e["type"] for e in events] == ["cron.missed"]
assert events[0]["payload"] == {
"count": 1,
"items": [
{
"name": "pills",
"scheduled_at": "2025-06-04T08:00:00+00:00",
"message": "take the pills",
}
],
}
def test_the_approval_broker_is_conversation_scoped() -> None:
"""A protected-command overlay interrupts one conversation, so it belongs to
the surface that sent that turn -- not to every socket sharing the page's
``/rpc``, which since the TUI relay includes other terminals. The broker is
not exposed on ``RpcStack``, so pin the assembly source; the routing itself
is covered in tests/test_approval_broker.py.
"""
import inspect
src = inspect.getsource(bootstrap.build_rpc_stack)
assert "ApprovalBroker(send_frame=conversation_scoped(send_frame))" in src
def test_the_confirm_broker_is_conversation_scoped() -> None:
"""Same reason, and it is the field that made it possible: a destructive
command's yes/no now names the conversation it was raised in, so it can reach
that surface instead of every terminal attached to the gateway. Pinned at the
assembly for the same reason as the approval broker above; the routing and
the no-conversation fallback are covered in tests/test_rpc_confirm.py.
"""
import inspect
src = inspect.getsource(bootstrap.build_rpc_stack)
assert "ConfirmBroker(send_frame=conversation_scoped(send_frame))" in src
async def test_the_channel_reaches_both_collaborators_or_nothing_is_delivered(monkeypatch) -> None:
"""One name, two consumers, and getting one of them wrong loses every turn.
``build_rpc_spine`` registers its delivery outlet under the channel name, and
``register_turn_methods`` stamps it on every turn ``turn.send`` submits as
``source.channel``. ``spine/turn.py`` says outright that the default channel
MUST match the channel the outlet was registered under or the reply is
dropped -- so a ``channel`` argument that reached only one of them would be
worse than none: the turn runs, produces output, and delivers it to a channel
with no outlet, with nothing anywhere reporting a problem.
"""
from raven.rpc import methods as methods_module
from raven.rpc import spine as spine_module
seen: dict[str, object] = {}
real_spine = spine_module.build_rpc_spine
def _spy_spine(agent_loop, emitter, *, channel="tui", **kwargs):
seen["outlet_channel"] = channel
return real_spine(agent_loop, emitter, channel=channel, **kwargs)
# Patched on the umbrella and not on ``methods.turn``: the umbrella imported
# the function into its own namespace at import time, so a patch on the
# defining module is never consulted -- and the test would pass while
# asserting nothing.
real_register = methods_module.register_turn_methods
def _spy_register(dispatcher, *, default_channel="tui", **kwargs):
seen["turn_channel"] = default_channel
return real_register(dispatcher, default_channel=default_channel, **kwargs)
monkeypatch.setattr(spine_module, "build_rpc_spine", _spy_spine)
monkeypatch.setattr(methods_module, "register_turn_methods", _spy_register)
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: _FakeLoop())
stack = await bootstrap.build_rpc_stack(_sink, agent_loop=_FakeLoop(), channel="acp")
try:
assert seen["outlet_channel"] == "acp"
assert seen["turn_channel"] == "acp", "an outlet on 'acp' fed by turns stamped 'tui' delivers nothing"
finally:
await stack.teardown()
async def test_the_channel_defaults_to_the_one_both_sides_already_used() -> None:
"""Every existing caller passes no channel, so the default has to be the
value the two sides independently defaulted to before it was a parameter."""
from raven.rpc.methods.turn import register_turn_methods
from raven.rpc.spine import build_rpc_spine
assert inspect.signature(build_rpc_spine).parameters["channel"].default == "tui"
assert inspect.signature(register_turn_methods).parameters["default_channel"].default == "tui"
assert inspect.signature(bootstrap.build_rpc_stack).parameters["channel"].default == "tui"
async def test_the_owning_teardown_cancels_subagents_before_the_spine_seals(monkeypatch) -> None:
"""Sealing the scheduler is the first thing the spine's teardown does, and a
sub-agent that finishes after it announces its result into a submit that
refuses new turns -- the only route that result has back. Cancelled first,
the run ends as the stop it is instead of as a result nobody received."""
from raven.rpc import spine as spine_module
loop = _FakeLoop(_FakeCron())
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
real_build = spine_module.build_rpc_spine
def _recording_build(*args, **kwargs):
scheduler, hub, ids, teardown = real_build(*args, **kwargs)
async def _teardown() -> None:
loop.order.append("turn_teardown")
await teardown()
return scheduler, hub, ids, _teardown
monkeypatch.setattr(spine_module, "build_rpc_spine", _recording_build)
stack = await bootstrap.build_rpc_stack(_sink)
await stack.teardown()
assert loop.order.index("cancel_all") < loop.order.index("turn_teardown")
async def test_a_cancel_that_fails_does_not_keep_the_spine_from_sealing(monkeypatch) -> None:
"""The cancel now runs ahead of the turn teardown, so a failure in it would
be a failure in front of the spine's own teardown -- logged and stepped
over, the way the rest of this teardown treats its steps."""
from raven.rpc import spine as spine_module
loop = _FakeLoop(_FakeCron())
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
async def _failing_cancel(*, reason: str = "") -> None:
raise RuntimeError("cancel blew up")
loop.subagents.cancel_all = _failing_cancel
real_build = spine_module.build_rpc_spine
def _recording_build(*args, **kwargs):
scheduler, hub, ids, teardown = real_build(*args, **kwargs)
async def _teardown() -> None:
loop.order.append("turn_teardown")
await teardown()
return scheduler, hub, ids, _teardown
monkeypatch.setattr(spine_module, "build_rpc_spine", _recording_build)
stack = await bootstrap.build_rpc_stack(_sink)
await stack.teardown()
assert "turn_teardown" in loop.order
def test_the_served_shutdown_stops_subagents_before_it_stops_the_backend() -> None:
"""Closing the memory adapter while a sub-agent run is still going fails
that run's next write for a reason the service had no part in."""
src = (Path(__file__).resolve().parents[1] / "raven" / "rpc" / "bootstrap.py").read_text(encoding="utf-8")
cancel = src.index("await agent_loop.subagents.cancel_all(reason=")
stop = src.index("await agent_loop.backend.stop()")
assert cancel < stop
async def test_owning_teardown_closes_the_mcp_it_opened_at_assembly(monkeypatch) -> None:
"""Assembly now opens MCP transports (and stdio children, and the sandbox)
whether or not a turn is ever served, so a stack stopped before its first
turn has resources to release that it never used to have.
"""
from raven import browser as browser_module
class _NoBrowser:
async def close(self) -> None:
pass
monkeypatch.setattr(browser_module, "get_browser", lambda: _NoBrowser())
loop = _FakeLoop(_FakeCron())
# A real backend too, so the one assertion below pins where MCP sits among
# everything else the engine owns rather than only that it is closed at all.
loop.backend = _RecordingBackend(loop.order)
async def _drain(*_a, **_kw) -> None:
loop.order.append("drain")
loop.drain_backend_stores = _drain
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
stack = await bootstrap.build_rpc_stack(_sink)
assert loop.prewarms == 1
assert loop.mcp_closed == 0
await stack.teardown()
assert loop.mcp_closed == 1
# Sub-agents first: a run still going can hand the backend another write, and
# it runs its tools through the executor close_mcp() tears down. MCP last of
# the engine's own resources, so the executor outlives everything using it.
assert loop.order == ["cancel_all", "drain", "stop", "close_mcp"]
async def test_a_mounted_stack_does_not_close_its_host_s_mcp(monkeypatch) -> None:
cron = _FakeCron()
loop = _FakeLoop(cron)
stack = await bootstrap.build_rpc_stack(_sink, agent_loop=loop)
await stack.teardown()
assert loop.mcp_closed == 0
@pytest.mark.parametrize("owns_loop", [True, False])
async def test_teardown_stops_only_the_owned_skill_watcher(tmp_path, monkeypatch, owns_loop) -> None:
from raven.memory_engine.skill_local.watcher import SkillFileWatcher
watcher = SkillFileWatcher([tmp_path], lambda _: None, lambda _: None)
loop = _FakeLoop()
loop.context = SimpleNamespace(skills=SimpleNamespace(stop_file_watcher=watcher.stop))
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
assert watcher.start()
thread = watcher._thread
assert thread is not None and thread.is_alive()
try:
stack = await bootstrap.build_rpc_stack(_sink, agent_loop=None if owns_loop else loop)
await stack.teardown()
assert thread.is_alive() is not owns_loop
await stack.teardown()
assert thread.is_alive() is not owns_loop
finally:
watcher.stop()
async def test_a_failed_watcher_stop_does_not_strand_the_owned_stores(monkeypatch) -> None:
loop = _FakeLoop()
def fail():
loop.order.append("watcher")
raise RuntimeError("watcher stop failed")
async def drain():
loop.order.append("drain")
loop.context = SimpleNamespace(skills=SimpleNamespace(stop_file_watcher=fail))
loop.backend = _RecordingBackend(loop.order)
loop.drain_backend_stores = drain
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: loop)
stack = await bootstrap.build_rpc_stack(_sink)
await stack.teardown()
assert loop.order == ["cancel_all", "watcher", "drain", "stop", "close_mcp"]
async def test_the_owning_path_builds_the_engine_for_the_channel_it_serves(monkeypatch) -> None:
"""The engine's cron is partitioned to the channel it was built for. Built
for the default while serving acp, it refused every wake the on-call agent
armed -- "channel 'acp' is outside this runner's partition" -- and the job
sat unclaimed (measured 2026-09-03/04, seven wakes, none fired)."""
from raven import browser as browser_module
class _NoBrowser:
async def close(self) -> None:
pass
monkeypatch.setattr(browser_module, "get_browser", lambda: _NoBrowser())
asked: list[dict] = []
def _factory(**kwargs):
asked.append(kwargs)
return _FakeLoop(_FakeCron())
monkeypatch.setattr(bootstrap, "build_agent_loop", _factory)
stack = await bootstrap.build_rpc_stack(_sink, channel="acp")
await stack.teardown()
assert asked == [{"channel": "acp"}]
def test_the_loop_factory_hands_the_channel_to_the_engine(monkeypatch) -> None:
from raven.core import engine_stack
seen: dict = {}
class _Runtime:
loop = object()
def _build_engine(**kwargs):
seen.update(kwargs)
return _Runtime()
monkeypatch.setattr(engine_stack, "build_engine", _build_engine)
assert bootstrap.build_agent_loop(channel="acp") is _Runtime.loop
assert seen["channel"] == "acp"
bootstrap.build_agent_loop()
assert seen["channel"] == "tui", "the default surface is unchanged"
async def test_an_acp_stack_runs_wakes_on_the_session_and_a_tui_stack_keeps_the_reminder(monkeypatch) -> None:
"""The reminder callback runs a job on ``cron:<job_id>`` and relies on the tui
fan-out to show the reply; an ACP client has no fan-out and watches one
session. The stack it builds must wire the callback that runs the wake on
that session (measured 2026-09-04: two wakes and a closed campaign, not one
frame delivered)."""
from raven import browser as browser_module
class _NoBrowser:
async def close(self) -> None:
pass
monkeypatch.setattr(browser_module, "get_browser", lambda: _NoBrowser())
acp_cron = _FakeCron()
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: _FakeLoop(acp_cron))
stack = await bootstrap.build_rpc_stack(_sink, channel="acp")
await stack.teardown()
assert getattr(acp_cron.on_job, "runs_on_session", False) is True
tui_cron = _FakeCron()
monkeypatch.setattr(bootstrap, "build_agent_loop", lambda **_: _FakeLoop(tui_cron))
stack = await bootstrap.build_rpc_stack(_sink, channel="tui")
await stack.teardown()
assert tui_cron.on_job is not None
assert getattr(tui_cron.on_job, "runs_on_session", False) is False, "the served page keeps its reminders"
async def test_a_second_assembly_keeps_the_subscriptions_the_live_one_holds() -> None:
"""A stack rebuilt under a live socket is handed the emitter it replaces.
``raven serve`` can assemble late: a first run comes up with no loop and
builds one when the page writes a model. The socket does not drop for that,
and the page re-subscribes only when it does -- so a replacement stack with
an emitter of its own would emit into one nothing is reading, and every
open stream would go quiet with no error anywhere.
"""
frames: list[dict] = []
async def _record(frame: dict) -> None:
frames.append(frame)
first = await bootstrap.build_rpc_stack(_record, agent_loop=_FakeLoop(_FakeCron()))
await first.emitter.register("tui:default")
second = await bootstrap.build_rpc_stack(
_record,
agent_loop=_FakeLoop(_FakeCron()),
emitter=first.emitter,
)
try:
await second.emitter.emit("tui:default", {"type": "message.complete", "payload": {}})
await asyncio.sleep(COALESCE_WINDOW_S * 3)
finally:
await second.teardown()
await first.teardown()
assert [f["params"]["event"]["type"] for f in frames if f.get("method") == "event"] == ["message.complete"]