## Summary
One plugin that failed to activate dropped every plugin.
`PluginRegistry.activate` raised on the first plugin whose factory would
not import (or whose contribution name collided), and
`build_plugin_registry` (`raven/core/plugin_stack.py`) answered by
returning an empty registry. A single broken user plugin therefore
silently removed research-flow, ppt-engine, design-engine, the memory
backend and the bundled playbook tools, with one stderr warning as the
only trace. Reproduced with two mock plugins under a scratch
`RAVEN_HOME`: `aa-broken` (factory raises `ImportError`) beside
`zz-good`; before this change the registry came back with `activated:
[]`, after it with `design-engine, everos-memory, playbook, ppt-engine,
zz-good` activated and `aa-broken` named as failed.
Changes:
- **Per-plugin activation** (`raven/plugins/registry.py`). Each admitted
plugin activates on its own. One that fails is rolled back whole (no
partial contribution in any table, and the `sys.path` entry
`_ensure_importable` appended for it is withdrawn), recorded as a
`PluginActivationFailure` (`activation_failures()`), and the plugins
after it still activate. In a name conflict the plugin activated first
(discovery order, by id) keeps the name and the other is recorded as
failed. A module `__getattr__` that raises something other than
`AttributeError` is now a recorded failure rather than an uncaught
crash.
- **Said to the user** (`plugin_stack.build_plugin_registry`,
`runtime.build_runtime`). Each failure is one notice naming the plugin,
its cause and the `plugins.disabled` escape, through the host's notifier
(`HostWiring.notify`), else stderr - the same channel the
missing-memory-backend notice already uses. When the configured memory
backend belongs to a plugin that failed, the memory notice names that
plugin and its cause instead of "no installed plugin provides it".
- **`raven plugins`** shows a `failed` status and the cause under the
table (the command called `activate` directly and previously crashed on
such a plugin).
- **Agents smoke check** (`raven/cli/agents_commands.py`) matched the
old log line "plugin activation failed", which no longer exists; it now
reads the shared `PLUGIN_FAILURE_MARKER`, and its test prints the notice
the real producer writes, so the two cannot drift.
- **Sub-agents inherit the host's opt-outs**
(`product_render.write_rendered`, `inherit_plugin_opt_outs`). A product
engine inherits `RAVEN_HOME`, so it scans the host's `<home>/plugins`
and the shared interpreter's entry points, but it read
`plugins.disabled` only from its own rendered config; a plugin disabled
on the host came back in every product. Every render now merges the
host's list after the product's own. The product's own engine plugin
never travels (`own_plugins=`, passed by all five launchers and the
scaffold): the PPT agent is `ppt-engine`, and a host turning it off for
its own agent is not a request to run the PPT agent without it. A
contract test reads every launcher's `write_rendered` call and requires
it to name its `*_PLUGIN_ID`.
- `CONTEXT.md` (Plugin Registry) and
`docs-site/docs/building-plugin{,.zh}.md` describe the new behaviour.
Not in this PR: the Web UI plugin list (`ext.list`) is discovery-only
and does not show a failed plugin; adding it is a wire change
(`ExtListResult`, openrpc, generated TS, the page) and is left for a
follow-up. The TUI, gateway and `raven agent` hosts show the notice
through their notifiers; the rpc stack (serve/acp) prints it to stderr,
which is also what the agents smoke check reads.
## Type
- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other
## Verification
**Rebased**: head 7cd000dbc on origin/main 8ee5ae0d8, without conflicts.
`git range-diff` shows every commit replayed unchanged. The branch's own
test files rerun at this head: 395 passed. Numbers elsewhere in this
description are from earlier heads.
Head b4d51aee8, rebased onto origin/main 6551f8259 after #698 and #708
merged. #698 conflicted in `raven/config/product_render.py` and
`tests/test_config_product_render.py`: both PRs added a function at the
same spot (`inherit_host_denials` there, `inherit_plugin_opt_outs` here)
and a call in `write_rendered`. The resolution keeps both functions and
all their tests; `write_rendered` now reads `host_config()` once and
passes it to both. #708 touches none of these files. At this head:
```
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider
25948 passed, 109 skipped
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_config_product_render.py
44 passed
ruff check / ruff format --check / make lint-imports lint-deps lint-types / git diff --check origin/main...HEAD
check_source_language.py origin/main...HEAD / make check-commits / check-large-files
all clean
cd ui-tui && npm run lint:rpc && npm run type-check && npm test
144 files, 2106 passed
```
The earlier `TUI checks` failure (run 35834019105, head 5fc374035) was
`newInstancePicker.test.tsx > quick-selects by the number it printed`.
This PR changes nothing under `ui-tui/`; the test passed 5 of 5 runs
locally at this head and in the full ui-tui run above.
The numbers below are from the earlier heads and are kept for the
record.
```
uv run --frozen --python 3.12 --extra dev ruff check raven evolver agents plugins-dist tests scripts
All checks passed
uv run --frozen --python 3.12 --extra dev ruff format --check raven evolver agents plugins-dist tests scripts
2083 files already formatted
make lint-imports && make lint-deps && make lint-types
Contracts: 10 kept, 0 broken / No dependency issues found / All checks passed
git diff --check origin/main...HEAD
clean
COLORTERM=truecolor make coverage (full suite, head before rebasing onto 7fdf296f4)
25847 passed, 109 skipped
COVERAGE_BASE_REF=origin/main make coverage-diff
Diff coverage: 100.00% (71/71 executable changed lines)
PYTHONPATH=. uv run --frozen --python 3.12 --extra dev python scripts/check_source_language.py origin/main...HEAD
exit 0
make check-commits
pass
git merge-tree --write-tree HEAD origin/main
clean
```
After rebasing onto 7fdf296f4 (its two new commits touch none of these
files), the touched suites at this head:
```
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q tests/test_plugin_*.py tests/test_core_plugin_stack.py tests/test_cli_plugin_commands.py tests/test_cli_agents_commands.py tests/test_cli_tui_commands.py tests/test_config_product_render.py tests/test_agents_*launcher*.py tests/test_everos_plugin_discovery.py tests/test_release_plugin_list.py tests/test_cli_gateway_commands.py
727 passed
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_subagent_vendored_agents.py tests/test_rpc_subagents.py tests/test_subagent_acp.py
452 passed
```
Revert-to-red, one production file restored to origin/main at a time
with the tests kept:
```
raven/plugins/registry.py -> 2 errors (plugin_stack imports PluginActivationFailure)
raven/core/plugin_stack.py -> 6 failed
raven/core/runtime.py -> 1 failed (build_runtime hands the host's notifier to the registry)
raven/config/product_render.py -> 42 failed
raven/cli/agents_commands.py -> 1 failed (smoke reds a handshake whose plugin did not load)
raven/cli/plugin_commands.py -> 1 failed (failed row and escaped cause)
agents/raven-ppt/run.py -> 2 failed (engine exemption, launcher contract)
```
Ten existing tests pinned "activation raises" (`test_plugin_registry`,
`test_plugin_bootstrap`, and the conflict tests for tools, hooks, tool
gates and session observers). They now assert the new contract instead:
the later plugin is recorded with the same reason text, the earlier one
keeps the name, and nothing of the loser is registered. Two stubs in
`test_cli_tui_commands.py` took `build_plugin_registry(cfg)` and now
accept the `notify` keyword.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed
## Risk
- A plugin conflict used to disable all plugins; it now keeps the plugin
that activates first and skips the other with a notice. Two plugins
contributing the same name therefore run with one of them instead of
none.
- A product sub-agent now starts with the host's `plugins.disabled`
merged into its config (its own engine excepted). A plugin that was
disabled on the host but relied on inside a product stops loading there;
the remedy is to enable it on the host.
- `PluginRegistry.activate` no longer raises `PluginConflictError` /
`PluginFactoryImportError`; the only in-tree callers were
`assemble_plugin_registry` and `raven plugins`. The exception classes
stay exported.
- Security: the change only narrows what loads. A failed plugin's
directory leaves sys.path, and the inherited opt-outs can only remove
plugins from a product, never add one.
- Rollback: revert the squash commit.
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
## Related Issues
N/A
---------
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
567 lines
21 KiB
Python
567 lines
21 KiB
Python
"""Plugins cast gates over the tool registry: manifest, registry, stack, throat.
|
|
|
|
The fifth contribution kind (paper: contracts/tool_gate.py). Pinned here: the
|
|
manifest entry (a gate without a usable name is refused at the door), registry
|
|
activation with cross-plugin conflict detection, the lenient stack builder with
|
|
its identity stamp, and the throat semantics -- gates are cast at construction
|
|
in (name, contributed_by) order, a verdict replaces exactly one validated call
|
|
with no retry hint appended, a raising gate refuses its call (fail-closed), and
|
|
a gate's bind failure fails the whole assembly instead of quietly unregistering
|
|
(dropping a gate is failing open). The absence pin -- a registry built with no
|
|
gates is byte-identical to today's -- lives with the execute-boundary tests
|
|
(test_tool_registry_execute.py).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import textwrap
|
|
import types
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from raven.agent import workdir
|
|
from raven.agent.loop import AgentLoop
|
|
from raven.agent.loop.bundles import HostWiring, ToolWiring, TurnPolicy
|
|
from raven.agent.tools.registry import ToolRegistry
|
|
from raven.contracts.llm_provider import LLMResponse
|
|
from raven.contracts.tool import Tool
|
|
from raven.contracts.tool_gate import ToolGate
|
|
from raven.plugins import (
|
|
Contributes,
|
|
DiscoveredPlugin,
|
|
ManifestOrigin,
|
|
PluginManifest,
|
|
PluginRegistry,
|
|
ToolGateContribution,
|
|
)
|
|
from raven.providers.base import LLMProvider
|
|
|
|
_INJECTED: set[str] = set()
|
|
|
|
|
|
def _install_module(name: str, attrs: dict[str, object]) -> None:
|
|
mod = types.ModuleType(name)
|
|
for k, v in attrs.items():
|
|
setattr(mod, k, v)
|
|
sys.modules[name] = mod
|
|
_INJECTED.add(name)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _cleanup_modules():
|
|
yield
|
|
for name in _INJECTED:
|
|
sys.modules.pop(name, None)
|
|
_INJECTED.clear()
|
|
|
|
|
|
def _discovered(plugin_id: str, gates: list[tuple[str, str]]) -> DiscoveredPlugin:
|
|
mf = PluginManifest(
|
|
id=plugin_id,
|
|
version="0.1.0",
|
|
contributes=Contributes(tool_gates=[ToolGateContribution(name=n, factory=f) for n, f in gates]),
|
|
)
|
|
return DiscoveredPlugin(manifest=mf, source=ManifestOrigin.USER, location=None)
|
|
|
|
|
|
class _Gate:
|
|
"""A paper-shaped gate: named, awaitable adjudicate, optional fixed verdict."""
|
|
|
|
def __init__(
|
|
self,
|
|
name: str,
|
|
*,
|
|
contributed_by: str | None = None,
|
|
verdict: str | None = None,
|
|
log: list | None = None,
|
|
) -> None:
|
|
self.name = name
|
|
if contributed_by is not None:
|
|
self.contributed_by = contributed_by
|
|
self._verdict = verdict
|
|
self.log = log if log is not None else []
|
|
|
|
async def adjudicate(self, name, params, *, session_workdir):
|
|
self.log.append((self.name, name, session_workdir))
|
|
return self._verdict
|
|
|
|
|
|
class _Probe(Tool):
|
|
"""A registered tool that records whether it ran."""
|
|
|
|
def __init__(self, name: str = "gate_probe") -> None:
|
|
self._name = name
|
|
self.ran = 0
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return self._name
|
|
|
|
@property
|
|
def description(self) -> str:
|
|
return "records dispatch"
|
|
|
|
@property
|
|
def parameters(self) -> dict:
|
|
return {"type": "object", "properties": {}, "required": []}
|
|
|
|
async def execute(self, **kwargs) -> str:
|
|
self.ran += 1
|
|
return "probe ran"
|
|
|
|
|
|
class TestManifestToolGates:
|
|
def test_parses_a_tool_gates_contribution(self) -> None:
|
|
mf = PluginManifest.from_toml_str(
|
|
textwrap.dedent(
|
|
"""
|
|
[plugin]
|
|
id = "code-flow"
|
|
version = "0.1.0"
|
|
[[plugin.contributes.tool_gates]]
|
|
name = "write_gate"
|
|
factory = "some_flow.gate:make_write_gate"
|
|
"""
|
|
)
|
|
)
|
|
assert [g.name for g in mf.contributes.tool_gates] == ["write_gate"]
|
|
|
|
def test_default_tool_gates_empty(self) -> None:
|
|
assert PluginManifest(id="p", version="0.1.0").contributes.tool_gates == []
|
|
|
|
def test_bad_factory_ref_rejected(self) -> None:
|
|
with pytest.raises(ValidationError):
|
|
ToolGateContribution(name="g", factory="not-a-ref")
|
|
|
|
def test_a_gate_without_a_usable_name_is_a_manifest_error(self) -> None:
|
|
"""The owner ruling: no runtime name fallback -- the door refuses."""
|
|
with pytest.raises(ValidationError, match="name"):
|
|
PluginManifest.from_toml_str(
|
|
textwrap.dedent(
|
|
"""
|
|
[plugin]
|
|
id = "p"
|
|
version = "0.1.0"
|
|
[[plugin.contributes.tool_gates]]
|
|
factory = "m:make"
|
|
"""
|
|
)
|
|
)
|
|
with pytest.raises(ValidationError, match="name"):
|
|
ToolGateContribution(name="", factory="m:make")
|
|
|
|
def test_duplicate_tool_gate_names_rejected(self) -> None:
|
|
with pytest.raises(ValidationError, match="duplicate tool_gate name"):
|
|
PluginManifest(
|
|
id="p",
|
|
version="0.1.0",
|
|
contributes=Contributes(
|
|
tool_gates=[
|
|
ToolGateContribution(name="dup", factory="m:a"),
|
|
ToolGateContribution(name="dup", factory="m:b"),
|
|
]
|
|
),
|
|
)
|
|
|
|
|
|
class TestRegistryToolGates:
|
|
def test_activates_and_builds(self) -> None:
|
|
seen = {}
|
|
|
|
def make(ctx):
|
|
seen["config"] = ctx.config
|
|
return "gate-instance"
|
|
|
|
_install_module("_pg_a", {"make": make})
|
|
reg = PluginRegistry()
|
|
reg.activate([_discovered("alpha", [("write_gate", "_pg_a:make")])])
|
|
assert reg.tool_gate_names() == ["write_gate"]
|
|
assert reg.tool_gate_plugin_id("write_gate") == "alpha"
|
|
from raven.plugins import ServiceLocator
|
|
|
|
built = reg.build_tool_gate(
|
|
"write_gate",
|
|
config={"mode": "strict"},
|
|
services=ServiceLocator(workspace=Path("/w"), user_id="u", agent_id="a"),
|
|
)
|
|
assert built == "gate-instance"
|
|
assert seen["config"] == {"mode": "strict"}
|
|
|
|
def test_cross_plugin_name_conflict_skips_the_later_plugin(self) -> None:
|
|
_install_module("_pg_b", {"make": lambda ctx: None})
|
|
reg = PluginRegistry()
|
|
reg.activate([_discovered("one", [("same", "_pg_b:make")]), _discovered("two", [("same", "_pg_b:make")])])
|
|
assert reg.activated_ids() == ["one"]
|
|
assert reg.tool_gate_plugin_id("same") == "one"
|
|
[failure] = reg.activation_failures()
|
|
assert failure.plugin_id == "two"
|
|
assert "tool_gate 'same' contributed by both 'one' and 'two'" in failure.reason
|
|
|
|
|
|
class TestBuildPluginToolGates:
|
|
def _config(self, plugin_config: dict | None = None):
|
|
from raven.config.raven import PluginsConfig, RavenConfig
|
|
|
|
return RavenConfig(plugins=PluginsConfig(config=dict(plugin_config or {})))
|
|
|
|
def test_builds_gates_with_the_plugins_slice_and_stamps_identity(self, tmp_path: Path) -> None:
|
|
from raven.core.plugin_stack import build_plugin_tool_gates
|
|
|
|
def make(ctx):
|
|
return _Gate(f"gate::{ctx.config.get('flag')}")
|
|
|
|
_install_module("_pg_c", {"make": make})
|
|
reg = PluginRegistry()
|
|
reg.activate([_discovered("myplugin", [("g1", "_pg_c:make")])])
|
|
built = build_plugin_tool_gates(tmp_path, self._config({"myplugin": {"flag": "on"}}), registry=reg)
|
|
assert [g.name for g in built] == ["gate::on"]
|
|
assert built[0].contributed_by == "myplugin"
|
|
|
|
def test_failing_and_declining_factories_are_skipped(self, tmp_path: Path) -> None:
|
|
"""Declining (a factory returning None) is the sanctioned opt-out:
|
|
no policy configured means no gate at all."""
|
|
from raven.core.plugin_stack import build_plugin_tool_gates
|
|
|
|
def boom(ctx):
|
|
raise RuntimeError("nope")
|
|
|
|
_install_module("_pg_d", {"boom": boom, "decline": lambda ctx: None})
|
|
reg = PluginRegistry()
|
|
reg.activate([_discovered("p", [("a", "_pg_d:boom"), ("b", "_pg_d:decline")])])
|
|
assert build_plugin_tool_gates(tmp_path, self._config(), registry=reg) == []
|
|
|
|
@pytest.mark.parametrize("fake", [object(), "gate-as-str"])
|
|
def test_a_gate_that_cannot_carry_the_stamp_still_boards(self, tmp_path: Path, fake) -> None:
|
|
from raven.core.plugin_stack import build_plugin_tool_gates
|
|
|
|
def make(ctx):
|
|
return fake
|
|
|
|
_install_module("_pg_e", {"make": make})
|
|
reg = PluginRegistry()
|
|
reg.activate([_discovered("p", [("g", "_pg_e:make")])])
|
|
built = build_plugin_tool_gates(tmp_path, self._config(), registry=reg)
|
|
assert built == [fake]
|
|
assert not hasattr(built[0], "contributed_by")
|
|
|
|
|
|
class TestRegistryAdjudication:
|
|
def test_gates_are_ordered_by_name_then_contributor(self) -> None:
|
|
b2 = _Gate("b", contributed_by="ymir")
|
|
b1 = _Gate("b", contributed_by="xene")
|
|
a = _Gate("a", contributed_by="zeta")
|
|
reg = ToolRegistry(tool_gates=[b2, b1, a])
|
|
assert [(g.name, g.contributed_by) for g in reg.tool_gates] == [
|
|
("a", "zeta"),
|
|
("b", "xene"),
|
|
("b", "ymir"),
|
|
]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_verdict_replaces_one_call_and_other_calls_run(self) -> None:
|
|
class _Selective(_Gate):
|
|
async def adjudicate(self, name, params, *, session_workdir):
|
|
self.log.append((self.name, name, session_workdir))
|
|
return "the gate's answer" if name == "gate_probe" else None
|
|
|
|
probe, free = _Probe(), _Probe("free_probe")
|
|
reg = ToolRegistry(tool_gates=[_Selective("selective")])
|
|
reg.register(probe)
|
|
reg.register(free)
|
|
|
|
gated = await reg.execute("gate_probe", {})
|
|
assert gated == "the gate's answer"
|
|
assert probe.ran == 0, "a verdict means the call never runs"
|
|
|
|
passed = await reg.execute("free_probe", {})
|
|
assert passed == "probe ran"
|
|
assert free.ran == 1, "the batch-mate is untouched"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_none_waves_the_call_through_to_the_tool(self) -> None:
|
|
gate = _Gate("waves", verdict=None)
|
|
probe = _Probe()
|
|
reg = ToolRegistry(tool_gates=[gate])
|
|
reg.register(probe)
|
|
assert await reg.execute("gate_probe", {}) == "probe ran"
|
|
assert probe.ran == 1
|
|
assert [entry[1] for entry in gate.log] == ["gate_probe"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_the_first_non_none_verdict_wins_in_name_order(self) -> None:
|
|
log: list = []
|
|
first = _Gate("a_first", verdict="a's verdict", log=log)
|
|
second = _Gate("b_second", verdict="b's verdict", log=log)
|
|
reg = ToolRegistry(tool_gates=[second, first])
|
|
reg.register(_Probe())
|
|
assert await reg.execute("gate_probe", {}) == "a's verdict"
|
|
assert [entry[0] for entry in log] == ["a_first"], "the later gate is never consulted"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_verdict_is_returned_verbatim_with_no_retry_hint(self) -> None:
|
|
reg = ToolRegistry(tool_gates=[_Gate("g", verdict="Error: this call needs an approval first")])
|
|
reg.register(_Probe())
|
|
result = await reg.execute("gate_probe", {})
|
|
assert result == "Error: this call needs an approval first"
|
|
assert "try a different approach" not in result
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_raising_gate_refuses_the_call_fail_closed(self) -> None:
|
|
class _Broken(_Gate):
|
|
async def adjudicate(self, name, params, *, session_workdir):
|
|
raise RuntimeError("policy store unreachable")
|
|
|
|
probe = _Probe()
|
|
reg = ToolRegistry(tool_gates=[_Broken("guard")])
|
|
reg.register(probe)
|
|
result = await reg.execute("gate_probe", {})
|
|
assert result == "Error: tool call 'gate_probe' was refused by gate guard: policy store unreachable"
|
|
assert probe.ran == 0, "failing open would make a gate's bugs silent permission grants"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_validation_still_precedes_adjudication(self) -> None:
|
|
class _Strict(_Probe):
|
|
@property
|
|
def parameters(self) -> dict:
|
|
return {"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]}
|
|
|
|
gate = _Gate("g", verdict="never reached")
|
|
reg = ToolRegistry(tool_gates=[gate])
|
|
reg.register(_Strict())
|
|
result = await reg.execute("gate_probe", {})
|
|
assert "Invalid parameters" in result
|
|
assert gate.log == [], "the paper says post-validation, pre-dispatch"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_the_bound_workdir_is_passed_explicitly(self, tmp_path: Path) -> None:
|
|
gate = _Gate("g", verdict=None)
|
|
reg = ToolRegistry(tool_gates=[gate])
|
|
reg.register(_Probe())
|
|
with workdir.bind(tmp_path):
|
|
await reg.execute("gate_probe", {})
|
|
await reg.execute("gate_probe", {})
|
|
assert [entry[2] for entry in gate.log] == [tmp_path, None]
|
|
|
|
|
|
class _Provider(LLMProvider):
|
|
def __init__(self) -> None:
|
|
super().__init__(api_key="test")
|
|
|
|
async def chat(
|
|
self,
|
|
messages,
|
|
tools=None,
|
|
model=None,
|
|
max_tokens=4096,
|
|
temperature=0.7,
|
|
reasoning_effort=None,
|
|
tool_choice=None,
|
|
):
|
|
return LLMResponse(content="ok", finish_reason="stop")
|
|
|
|
def get_default_model(self) -> str:
|
|
return "fake/default"
|
|
|
|
|
|
def _loop(tmp_path: Path, gates: list | None = None) -> AgentLoop:
|
|
(tmp_path / "home").mkdir(exist_ok=True)
|
|
return AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path / "home",
|
|
model="fake/model",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
host=HostWiring(),
|
|
tools=ToolWiring(restrict_to_workspace=True, plugin_tool_gates=gates),
|
|
)
|
|
|
|
|
|
class TestLoopCastsGates:
|
|
def test_cast_at_construction_in_order_and_never_after(self, tmp_path: Path) -> None:
|
|
loop = _loop(tmp_path, gates=[_Gate("b"), _Gate("a")])
|
|
assert [g.name for g in loop.tools.tool_gates] == ["a", "b"]
|
|
with pytest.raises(AttributeError):
|
|
loop.tools.tool_gates = () # type: ignore[misc]
|
|
|
|
def test_a_binding_gate_receives_the_loops_own_handles(self, tmp_path: Path) -> None:
|
|
class _Binding(_Gate):
|
|
bound: list = []
|
|
|
|
def bind_runtime(self, handles) -> None:
|
|
_Binding.bound.append(handles)
|
|
|
|
_Binding.bound = []
|
|
gate = _Binding("g", contributed_by="plug")
|
|
_loop(tmp_path, gates=[gate])
|
|
assert len(_Binding.bound) == 1
|
|
handles = _Binding.bound[0]
|
|
assert callable(handles.direct_ask)
|
|
assert callable(handles.rebind_workdir)
|
|
assert handles.session_dir is not None
|
|
|
|
@pytest.mark.parametrize("boom", [RuntimeError("half-bound"), None])
|
|
def test_a_gate_that_fails_to_bind_fails_the_assembly(self, tmp_path: Path, boom) -> None:
|
|
"""Loud by design, declines included: quietly unregistering a gate
|
|
(the tools' path) would fail open."""
|
|
from raven.plugins.context import BindDeclinedError
|
|
|
|
exc = boom if boom is not None else BindDeclinedError("no grant for me")
|
|
|
|
class _Refusing(_Gate):
|
|
def bind_runtime(self, handles) -> None:
|
|
raise exc
|
|
|
|
with pytest.raises(type(exc)):
|
|
_loop(tmp_path, gates=[_Refusing("g")])
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_direct_ask_answers_none_with_no_asking_transport(self, tmp_path: Path) -> None:
|
|
loop = _loop(tmp_path)
|
|
handles = loop.mint_runtime_handles("plug")
|
|
assert await handles.direct_ask("proceed?", ["Yes", "No"], "conv-1") is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_direct_ask_waits_its_turn_on_the_conversation_and_gives_up_at_the_deadline(
|
|
self, tmp_path: Path
|
|
) -> None:
|
|
"""The grant is a third route to the broker's single pending slot, beside the
|
|
confirm gate and the relayed ``Asker``. Like them it holds the conversation's
|
|
question lock across the round trip -- ``ask_direct`` takes none itself -- so
|
|
a gate asking from inside one node's tool call cannot evict the question a
|
|
sibling node (or a relayed sub-agent) already has pending. A conversation
|
|
still busy at the deadline answers None, the gate's no-channel path, and the
|
|
broker never sees the question."""
|
|
import asyncio
|
|
|
|
from raven.acp_client.asker import question_lock
|
|
|
|
class _Broker:
|
|
default_timeout_s = 600.0
|
|
|
|
def __init__(self) -> None:
|
|
self.asked: list[str] = []
|
|
|
|
async def await_question(self, cid: str, *, prompt: str, choices: list[str], **kwargs) -> str:
|
|
self.asked.append(prompt)
|
|
return "Yes"
|
|
|
|
loop = _loop(tmp_path)
|
|
broker = _Broker()
|
|
loop.tools.get("ask_user").set_broker(broker) # type: ignore[union-attr]
|
|
handles = loop.mint_runtime_handles("plug")
|
|
|
|
lock = question_lock("conv-1")
|
|
await lock.acquire()
|
|
try:
|
|
assert await handles.direct_ask("proceed?", ["Yes", "No"], "conv-1", 0.1) is None
|
|
finally:
|
|
lock.release()
|
|
assert broker.asked == []
|
|
|
|
async def release_soon() -> None:
|
|
await asyncio.sleep(0.02)
|
|
lock.release()
|
|
|
|
await lock.acquire()
|
|
answer, _ = await asyncio.gather(handles.direct_ask("proceed?", ["Yes", "No"], "conv-1", 1.0), release_soon())
|
|
assert answer == "Yes"
|
|
assert broker.asked == ["proceed?"]
|
|
|
|
def test_the_handles_carry_the_loops_usage_recorder(self, tmp_path: Path) -> None:
|
|
"""The plugin side reads ``usage_recorder`` off a hand-built ``RuntimeHandles``;
|
|
this pins that the host mints it, so a deck's generations reach the usage
|
|
ledger rather than going unrecorded with nothing looking different."""
|
|
loop = _loop(tmp_path)
|
|
handles = loop.mint_runtime_handles("plug")
|
|
assert handles.usage_recorder is not None
|
|
assert handles.usage_recorder == loop._record_image_usage
|
|
|
|
def test_rebind_workdir_persists_repoints_and_reads_back(self, tmp_path: Path) -> None:
|
|
from raven.agent.workdir import WorkdirPolicy, WorkdirResolver
|
|
|
|
loop = _loop(tmp_path)
|
|
target = tmp_path / "project"
|
|
target.mkdir()
|
|
handles = loop.mint_runtime_handles("plug")
|
|
|
|
with workdir.bind(tmp_path / "old"):
|
|
got = handles.rebind_workdir("web:abc", target)
|
|
assert got == target.resolve()
|
|
assert workdir.current() == target.resolve(), "the very next call resolves the new root"
|
|
assert workdir.current() is None, "the turn-end reset still runs"
|
|
|
|
assert loop.sessions.get_or_create("web:abc").metadata["workdir"] == str(target.resolve())
|
|
resolver = WorkdirResolver(
|
|
WorkdirPolicy.PER_CHANNEL,
|
|
agent_home=tmp_path / "home",
|
|
session_root=tmp_path / "chanwork",
|
|
sessions=loop.sessions,
|
|
)
|
|
assert resolver.resolve("web:abc", create=False) == target.resolve()
|
|
|
|
def test_rebind_workdir_keeps_the_validation_refusals(self, tmp_path: Path) -> None:
|
|
loop = _loop(tmp_path)
|
|
handles = loop.mint_runtime_handles("plug")
|
|
with pytest.raises(ValueError, match="user_memory"):
|
|
handles.rebind_workdir("web:abc", tmp_path / "home" / "user_memory" / "x")
|
|
|
|
|
|
DEMO_GATE = """
|
|
class DemoGate:
|
|
name = "demo_gate"
|
|
|
|
async def adjudicate(self, name, params, *, session_workdir):
|
|
if name == "gate_probe":
|
|
return "gated by demo"
|
|
return None
|
|
|
|
def make(ctx):
|
|
return DemoGate()
|
|
"""
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_plugin_gate_is_cast_over_the_built_runtimes_registry(tmp_path: Path, monkeypatch) -> None:
|
|
"""End to end: a plugin directory's gate rides discovery -> activation ->
|
|
build -> cast, satisfies the paper's face, and adjudicates a live call."""
|
|
from raven.config.raven import RavenConfig
|
|
from raven.config.schema import Config
|
|
from raven.core import plugin_stack, runtime
|
|
|
|
plug = tmp_path / "plugins" / "demogate"
|
|
plug.mkdir(parents=True)
|
|
plug.joinpath("raven-plugin.toml").write_text(
|
|
'[plugin]\nid = "demogate"\nversion = "1.0"\n'
|
|
"[[plugin.contributes.tool_gates]]\n"
|
|
'name = "demo_gate"\nfactory = "demogate_mod:make"\n'
|
|
)
|
|
plug.joinpath("demogate_mod.py").write_text(DEMO_GATE)
|
|
_INJECTED.add("demogate_mod")
|
|
monkeypatch.setattr(
|
|
plugin_stack,
|
|
"plugin_discovery_sources",
|
|
lambda: {
|
|
"bundled_dir": tmp_path / "none",
|
|
"user_dir": tmp_path / "plugins",
|
|
"project_dir": tmp_path / "none",
|
|
"entry_points_group": None,
|
|
},
|
|
)
|
|
monkeypatch.setattr(runtime.token_wise_stack, "install_from_config", lambda *a, **k: None)
|
|
monkeypatch.setattr(runtime.token_wise_stack, "caching_probe", lambda *a, **k: False)
|
|
|
|
config = Config()
|
|
config.agents.defaults.workspace = str(tmp_path / "ws")
|
|
rt = runtime.build_runtime(config, RavenConfig(), provider=_Provider())
|
|
try:
|
|
gates = rt.loop.tools.tool_gates
|
|
assert [g.name for g in gates] == ["demo_gate"]
|
|
assert gates[0].contributed_by == "demogate"
|
|
assert isinstance(gates[0], ToolGate)
|
|
rt.loop.tools.register(_Probe())
|
|
assert await rt.loop.tools.execute("gate_probe", {}) == "gated by demo"
|
|
finally:
|
|
rt.discard()
|