Raven-1/tests/test_core_plugin_stack.py
zhanghui 2b3cce70fa
feat(*): upgrade everos to 1.4.1, lift the windows gate, guard the embedding width (#791)
## Summary

EverOS 1.4.0 runs natively on Windows and 1.4.1 pins `openai` below 3,
so the memory plugin's exact pin moves from `everos[multimodal]==1.2.3`
to `==1.4.1`. The lock follows: `pyarrow` 24.0.0 -> 25.0.1 (EverOS's new
floor), `everalgo-boundary` 0.2.0 -> 0.2.1, `everalgo-core` 0.4.0 ->
0.3.0 (EverOS now pins its everalgo layer with `==`), and `msvc-runtime`
on `win32` only. `lancedb` stays at 0.34.0. Every internal everos symbol
the adapter reaches is present in 1.4.1 with the same signature, so the
adapter itself is unchanged. The plugin manifest and package version
move to 1.4.0.

**The Windows gate is gone.** `everos_platform_note` (#737) and its five
call sites, `ServiceState.UNSUPPORTED`, the import.scan refusal (#750),
the wizard's WSL notice and its zh catalogue line are removed;
`_everos_executable` looks for `everos.exe` on `win32`. Spawning,
probing and reusing the server were already portable. Identifying and
stopping a stale server was not: `_cmdline_of` asked `ps`, and the
marker `everos server start` never matched a Windows command line
(`...\Scripts\everos.exe server start`). The command line now comes from
WMI through PowerShell on `win32` and the marker accepts both shapes;
`_listening_port` asks the TCP table through PowerShell there, walking
the launcher's descendants: the socket belongs to the base interpreter
two launchers below `everos.exe`. The server is spawned in a process
group of its own (the Windows counterpart of `start_new_session`, which
also keeps a Ctrl-C at the gateway off it) and stopped with Ctrl-Break,
which uvicorn takes as a shutdown; one that ignores it for ten seconds,
or one no group can be addressed to, gets `TerminateProcess` as before.
And because Windows cannot replace a running executable, the upgrade
helper stops whatever still runs from under the tool environment before
it installs (the server outlives the gateway by design); without that,
`uv` failed on `Scripts\everos.exe` with os error 32. Recorded in
`docs/memory-plugin-architecture.md` section 7.4.

**Two guards, found while verifying against a copy of a real 1.2.x
store:**

- `set_embedding_endpoint` measures the model's vector width before
writing the pin and refuses anything narrower than the 1024-wide memory
index (`REQUIRED_EMBEDDING_DIMENSIONS`), but only where an EverOS
backend will read the pin: the `everos-memory` distribution is installed
and the config names `everos` as the memory backend (an absent key is
the schema default; an explicit `null` or another backend is not gated,
and a knowledge base sizes itself to whatever width the model returns).
The wizard's own check imports the same constant and probe. A
768-dimension model pinned through the settings page had left every
memory store and search answering 500 about a mismatched width, with
nothing on the page saying why. A probe that cannot reach the provider
is not a verdict: the pin is written and a warning logged. The two RPC
writers run the (now network-touching) writer in a thread.
- `ensure_everos_server` reads the running server's `/health` version
and, for a root raven owns, sends a mismatch with the installed everos
through the same precheck / stop / spawn chain a rotated credential
takes (`stale_reason`). Raven used to reuse whatever answered on the
port, so an upgrade left the old server serving until something
unrelated restarted it. A root the user manages is never touched.

**The wire contract is pinned.** Every request body the plugin and the
memory page send (`/add` with a tool exchange, `/flush`, the four
`/search` shapes, the four `/get` kinds) is validated against everos
1.4.1's own request models in `TestRequestBodiesMatchEverosModels`, so a
schema move on the next upgrade fails in the suite rather than as a 422
in a gateway log.

**A six-angle review then hardened the degraded paths** (turn survival
under every EverOS failure, Windows lifecycle, upgrade paths, the
embedding guard, the wire contract, concurrency). The rule: memory
degrades with a notice and never blocks a turn, a start or an upgrade.

- A gateway starts the server again when it finds nothing listening (or
a probe that timed out) and nothing holding the OME lock: only for a
root raven owns, never over a child of its own, at most once per thirty
seconds. The only spawn used to be in `start()`, so a server that went
away left a running gateway without memory until restarted by hand. A
stop that was sent and did not finish is not adopted (uvicorn closes the
port at once and finishes what it had), so the version-mismatch restart
after an upgrade cannot leave the gateway on a dying server.
- A command-line lookup that failed (no `ps`, PowerShell blocked, WMI
wedged, timeout) is distinct from a process that is gone: the stop keeps
waiting, the lock names nothing, and a settings save over a server that
answers but cannot be identified is reported as not applied. A losing
spawn no longer overwrites the live server's pidfile. `stop_pid` counts
wall time and takes a grace; on Windows a backend drains the server it
started at `stop()`.
- The upgrade helper asks again after `Stop-Process` and refuses to run
`uv` while anything from the environment survives (an elevated or
another user's process): `--force` removes the environment before
writing and would have left every file but the one that could not go.
- An embedding pin narrower than the index is measured at backend start
(once per pin per process) and withheld from the spawn: EverOS runs
keyword recall and keeps storing, and the notice names the model and
width. This covers a pin written around the write-time check (before
memory was on, unreachable at save time, edited by hand). The probe no
longer raises on an odd response, times out at ten seconds, honours
`plugins.disabled`; `migrate_roles` runs off the event loop it had put a
provider round-trip onto.
- The memory page uses the `/api/v2` routes (v1 is EverOS's legacy
alias), asks `/health` before a search the way the chat adapter does,
and shows the server's own sentence on a refusal. The recalled profile
is rendered as lines rather than a `namespace(...)` repr with its
evidence fields; a tool-call-only row stores empty content, not
`"None"`; `top_k` stays within 1..100; an empty query asks nothing; a
failed capability probe is not cached. The wizard accepts a model wider
than 1024 (EverOS keeps the first 1024, as the settings page already
allowed) and re-prompts on the host's refusal instead of a traceback.

`docs/memory-plugin-architecture.md` section 7 records the upgrade (7.4)
the way 7.3 recorded 1.2.1 -> 1.2.3.

## Type

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

## Verification

Unit and RPC tests over every touched area:

```
env -u SERPER_API_KEY uv run --frozen --python 3.12 --extra dev pytest -q tests/test_everos_server.py tests/test_everos_backend.py tests/test_cli_onboard_commands.py tests/test_everos_discover.py tests/test_rpc_settings.py tests/test_everos_config.py tests/test_everos_plugin_discovery.py tests/test_cli_plugin_commands.py tests/test_core_plugin_stack.py tests/test_rpc_memory.py tests/test_rpc_import_sync.py tests/test_everos_http_adapter.py tests/test_memory_backend_protocol.py tests/test_memory_backend_contract.py tests/test_plugin_boundary.py tests/test_i18n_boundary.py tests/test_config_update*.py
# 1312 passed, 1 skipped (inotify is Linux-only) on the first commit; 960 passed after the review round; full suite 26214 passed, 2 failed, 120 skipped, the 2 failing on an untouched github/main checkout too (LibreOffice path, background child env)
uv run --frozen --python 3.12 --extra dev ruff check <changed files>   # All checks passed!
uv run --frozen --python 3.12 --extra dev ruff format --check <changed files>   # 20 files already formatted
```

Against a 46 MB copy of a store written by everos 1.2.x (568 episodes,
5528 atomic facts, seven LanceDB tables), never touching the live one:

- `everos server start` on 1.4.1 opens it with no schema complaint;
`/health` reports `version 1.4.1`, `cascade.healthy true`.
- The same four `/api/v2/memory/search` requests on 1.2.3 and 1.4.1
return the same ids in the same order; BM25 scores drift in the third
decimal place.
- `raven agent -m` on this branch spawns everos 1.4.1, a "remember this"
turn lands in `episodes/episode-2026-09-24.md` and `.atomic_facts/`, a
fresh one-shot session recalls it, and a third session recalls episodes
written under 1.2.x months earlier.
- With a 1.2.3 server left on the port, the next `raven agent -m` logs
`runs everos 1.2.3 while this raven installs 1.4.1; restarting`, the old
pid exits and `/health` reports 1.4.1. The 1.2.3 server had opened the
index 1.4.1 wrote with no complaint.
- Windows 11 box, this branch checked out and synced: `raven serve`
spawns `everos.exe` natively, the Memory page shows its four tabs
instead of the platform sentence, a chat turn lands as an episode
(`/add` 200, `/flush` 200, cascade `upserted=1`), and after a memory
role change the next start logs `holds credentials raven has since
changed; restarting`, the old everos pid exits and the new one answers
`/health` 1.4.1.
- The settings page refuses `BAAI/bge-base-en-v1.5` on deepinfra with
`returns 768-dimension vectors and the memory index is 1024 wide` (real
probe), and the pin stays unchanged.
- Adapter import smoke against 1.4.1: all 13 internal symbols present,
signatures unchanged.
- Windows 11 box, second round: with the server up, `uv pip install
--reinstall --no-deps everos==1.4.1` fails on `Scripts\everos.exe` (os
error 32, exit 2); after the helper's sweep (`Stopped what was still
running from the old install (pid ...)`) the same command succeeds and
the relaunched gateway spawns a fresh 1.4.1. A stop from the spawning
console runs the full uvicorn shutdown (`Shutting down` ... `Finished
server process`) in 3 s; a stop from another console falls back and the
process is gone in 2 s. `lock_holder(root).port` reads 18997 (launcher
-> venv python -> base python).
- `tests/test_everos_server.py tests/test_updates_upgrade.py`: 261
passed, 1 skipped; `tests/test_everos_backend.py -k RequestBodies`: 7
passed.
- Review round (`env -u SERPER_API_KEY uv run --frozen --python 3.12
--extra dev pytest` over the everos, config, settings, memory page,
import, onboard, plugin-stack, boundary, cycle-budget and contract
files): 1225 passed, 1 skipped; `test_everos_server.py` 124,
`test_everos_backend.py` 188.
- Windows 11 box, review round: with the gateway serving, the everos
launcher was terminated by hand (`Stop-Process`, nothing on 18997); the
next turn answered without memory (`recall failed ...
state=unresponsive`), and six seconds after it began the gateway logged
`started everos server` / `everos server ready`, the pidfile named the
new pid and `/health` answered 1.4.1. The first attempt did not fire
because a dead port there takes 2-4 s to be refused, longer than the 1 s
probe; the respawn now fires on a timed-out probe as well, guarded by
the lock holder. The memory page's four counts and a search on the Cases
tab went through the v2 routes.

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

## Risk

- Windows installs that used to read "long-term memory is not available
on Windows yet" now spawn EverOS, and a stale server is identified and
replaced there too. The stop there is Ctrl-Break with a ten-second
fallback to `TerminateProcess`. An upgrade on Windows first stops every
process whose executable lives under the tool environment: raven's own
gateway, sub-agents and server, nothing outside that directory.
- An embedding pin narrower than 1024 dimensions is refused at the
settings page and the wizard, with the width in the message, only for an
install whose memory runs on EverOS (plugin installed, backend `everos`
or the default). Knowledge-only installs, memory off, and other backends
keep any width. Existing pins are not touched.
- An owned EverOS server whose version differs from the installed
package is restarted on the next session start. A root the user manages
is never restarted.
- A gateway may now start an EverOS server on its own when its probe
finds nothing listening and nothing holding the lock (owned roots only,
at most every thirty seconds). On Windows a backend stops the server it
started when it shuts down (Ctrl-Break, sixty seconds to drain), so a
settings change or an upgrade there restarts the server rather than
leaving it. An upgrade on Windows refuses to proceed while a process
from the tool environment cannot be stopped, and says which pid.
- An embedding pin narrower than 1024 that reached the config around the
settings check is no longer handed to EverOS: recall runs on keywords
with a notice naming the model, until a 1024-dimension model is pinned.
- Rollback: revert the commit and `uv sync`. `lancedb` did not move, so
an index written by 1.4.1 opens under 1.2.3 (exercised on the copy).

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

## Related Issues

N/A

---------

Co-authored-by: gloryfromca <23442919+gloryfromca@users.noreply.github.com>
Co-authored-by: Claude (claude-fable-5-1) <noreply@anthropic.com>
2026-09-25 19:33:18 +08:00

786 lines
29 KiB
Python

"""CLI plugin-stack helper.
Exercises :func:`build_plugin_registry` and
:func:`maybe_build_memory_backend` against the ``raven_everos`` plugin,
which the dev environment installs from ``plugins-dist/everos-memory`` and
which discovery finds through the ``raven.plugins`` entry-point group.
"""
from __future__ import annotations
import ast
import logging
from pathlib import Path
import pytest
import raven
from raven.config.raven import (
MemoryConfig,
PluginsConfig,
RavenConfig,
)
from raven.config.schema import Config, MediaGenConfig, ToolsConfig, WebToolsConfig
from raven.contracts.memory import MemoryBackend
from raven.core.plugin_stack import (
build_plugin_hooks,
build_plugin_registry,
build_plugin_tools,
everos_plugin_installed,
everos_plugin_missing_note,
maybe_build_memory_backend,
named_plugin_roots,
)
from raven.plugins import PluginRegistry
from tests._everos_presence import everos_plugin_absent, everos_plugin_broken
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _config(
*,
memory_backend: str | None = "everos",
disabled: list[str] | None = None,
plugin_config: dict | None = None,
dirs: list[str] | None = None,
tools: ToolsConfig | None = None,
) -> RavenConfig:
return RavenConfig(
memory=MemoryConfig(backend=memory_backend),
plugins=PluginsConfig(
disabled=list(disabled or []),
config=dict(plugin_config or {}),
dirs=list(dirs or []),
),
base=Config(tools=tools) if tools is not None else Config(),
)
# ---------------------------------------------------------------------------
# build_plugin_registry
# ---------------------------------------------------------------------------
class TestBuildRegistry:
def test_returns_registry_with_everos_activated(self) -> None:
reg = build_plugin_registry(_config())
assert isinstance(reg, PluginRegistry)
assert "everos-memory" in reg.activated_ids()
assert "everos" in reg.memory_backend_names()
def test_disabled_plugin_id_skipped(self) -> None:
reg = build_plugin_registry(
_config(disabled=["everos-memory"]),
)
assert "everos-memory" not in reg.activated_ids()
assert "everos" not in reg.memory_backend_names()
# ---------------------------------------------------------------------------
# maybe_build_memory_backend
# ---------------------------------------------------------------------------
class TestMaybeBuildBackend:
def test_default_config_builds_everos(self, tmp_path: Path) -> None:
backend = maybe_build_memory_backend(tmp_path, _config())
assert backend is not None
assert isinstance(backend, MemoryBackend)
def test_memory_backend_none_returns_none(
self,
tmp_path: Path,
) -> None:
backend = maybe_build_memory_backend(
tmp_path,
_config(memory_backend=None),
)
assert backend is None
def test_unknown_backend_returns_none_no_raise(
self,
tmp_path: Path,
) -> None:
"""A user-config typo / missing plugin must NOT crash boot —
the helper logs + degrades, AgentLoop falls back to legacy."""
backend = maybe_build_memory_backend(
tmp_path,
_config(memory_backend="nonexistent"),
)
assert backend is None
def test_disabled_backend_returns_none(self, tmp_path: Path) -> None:
backend = maybe_build_memory_backend(
tmp_path,
_config(
memory_backend="everos",
disabled=["everos-memory"],
),
)
assert backend is None
def test_a_missing_backend_is_said_out_loud(self, tmp_path: Path, capsys) -> None:
cfg = _config(memory_backend="ghost")
backend = maybe_build_memory_backend(tmp_path, cfg, registry=PluginRegistry())
assert backend is None
err = capsys.readouterr().err
assert "memory.backend" in err and "ghost" in err
def test_the_shipped_default_missing_names_what_to_install(self, tmp_path: Path, capsys) -> None:
"""The commonest way to reach that line is an install without the plugin.
Naming only the backends that ARE installed leaves such a reader with
nothing to do; doctor and the wizard both answer it in one sentence, and
a turn is the surface that actually reaches this code.
"""
from tests._everos_presence import everos_plugin_absent
with everos_plugin_absent():
backend = maybe_build_memory_backend(tmp_path, _config(memory_backend="everos"), registry=PluginRegistry())
assert backend is None
assert "everos-memory" in capsys.readouterr().err
# ---------------------------------------------------------------------------
# Per-plugin config slice resolution
# ---------------------------------------------------------------------------
class TestConfigSliceResolution:
def test_config_by_plugin_id(self, tmp_path: Path) -> None:
backend = maybe_build_memory_backend(
tmp_path,
_config(
plugin_config={
"everos-memory": {"mode": "embedded", "base_url": "http://x"},
}
),
)
# Backend received the config slice keyed by plugin id.
assert backend._config["mode"] == "embedded"
assert backend._config["base_url"] == "http://x"
def test_config_by_backend_name_fallback(
self,
tmp_path: Path,
) -> None:
"""When the user uses the shorter key (backend name), the
helper still finds it. Useful for handwritten configs."""
backend = maybe_build_memory_backend(
tmp_path,
_config(
plugin_config={
"everos": {"mode": "http", "base_url": "http://y"},
}
),
)
assert backend._config["mode"] == "http"
assert backend._config["base_url"] == "http://y"
def test_plugin_id_takes_precedence_over_backend_name(
self,
tmp_path: Path,
) -> None:
"""If both keys are present, the canonical (plugin id) wins."""
backend = maybe_build_memory_backend(
tmp_path,
_config(
plugin_config={
"everos-memory": {"marker": "canonical"},
"everos": {"marker": "fallback"},
}
),
)
assert backend._config["marker"] == "canonical"
def test_no_config_slice_yields_empty_dict(
self,
tmp_path: Path,
) -> None:
backend = maybe_build_memory_backend(tmp_path, _config())
assert backend._config == {}
# ---------------------------------------------------------------------------
# Registry injection — caller can pass a pre-built registry
# ---------------------------------------------------------------------------
class TestRegistryInjection:
def test_caller_supplied_registry_used(self, tmp_path: Path) -> None:
reg = build_plugin_registry(_config())
backend = maybe_build_memory_backend(
tmp_path,
_config(),
registry=reg,
)
# Backend constructed via the explicit registry.
assert backend is not None
def test_default_construction_creates_internal_registry(
self,
tmp_path: Path,
) -> None:
# Sanity: with no registry passed, the helper still works.
backend = maybe_build_memory_backend(tmp_path, _config())
assert backend is not None
# ---------------------------------------------------------------------------
# Workspace plumbing through ServiceLocator
# ---------------------------------------------------------------------------
class TestServiceLocatorPlumbing:
def test_workspace_reaches_backend(self, tmp_path: Path) -> None:
backend = maybe_build_memory_backend(tmp_path, _config())
# EverosBackend stores ctx.services on construction.
assert backend._services.workspace == tmp_path
def test_the_two_identities_do_not_arrive_swapped(self, tmp_path: Path) -> None:
"""The whole point of moving identity into ServiceLocator is that a
mismatch makes every written memory unrecallable with no warning. Until
this assertion existed, swapping the two arguments at the only
production wiring point left the entire suite green."""
config = _config()
config.memory.user_id = "u-distinct"
config.memory.agent_id = "a-distinct"
backend = maybe_build_memory_backend(tmp_path, config)
assert backend._services.user_id == "u-distinct"
assert backend._services.agent_id == "a-distinct"
# ---------------------------------------------------------------------------
# plugins.dirs
# ---------------------------------------------------------------------------
class TestConfiguredDirs:
def test_named_roots_resolve_from_config(self, tmp_path: Path) -> None:
assert named_plugin_roots(_config(dirs=[str(tmp_path)])) == (tmp_path,)
assert named_plugin_roots(_config()) == ()
assert named_plugin_roots(None) == ()
def test_a_plugin_under_a_named_root_activates(self, tmp_path: Path) -> None:
root = tmp_path / "plugins"
(root / "shelf").mkdir(parents=True)
(root / "shelf" / "raven-plugin.toml").write_text(
'[plugin]\nid = "shelf"\nversion = "0.1.0"\n',
encoding="utf-8",
)
assert "shelf" in build_plugin_registry(_config(dirs=[str(root)])).activated_ids()
assert "shelf" not in build_plugin_registry(_config()).activated_ids()
# ---------------------------------------------------------------------------
# One broken plugin
# ---------------------------------------------------------------------------
def _broken_plugin_root(tmp_path: Path, *, backend: str | None = None) -> Path:
"""A named root holding plugin ``wreck``, whose factory module raises on import."""
package = f"_wreck_{tmp_path.name.replace('-', '_')}"
root = tmp_path / "plugins"
plug = root / "wreck"
(plug / package).mkdir(parents=True)
(plug / package / "__init__.py").write_text("raise ImportError('boom')\n", encoding="utf-8")
kind, name = ("memory_backends", backend) if backend else ("tools", "wreck_tool")
(plug / "raven-plugin.toml").write_text(
f'[plugin]\nid = "wreck"\nversion = "0.1.0"\n'
f'[[plugin.contributes.{kind}]]\nname = "{name}"\nfactory = "{package}:make"\n',
encoding="utf-8",
)
return root
class TestOneBrokenPlugin:
def test_the_rest_of_the_registry_survives_and_the_host_hears_it(self, tmp_path: Path) -> None:
heard: list[str] = []
reg = build_plugin_registry(_config(dirs=[str(_broken_plugin_root(tmp_path))]), notify=heard.append)
assert "wreck" not in reg.activated_ids()
assert {"everos-memory", "playbook"} <= set(reg.activated_ids())
assert [f.plugin_id for f in reg.activation_failures()] == ["wreck"]
[message] = heard
assert "'wreck' did not load" in message
assert "boom" in message
assert "plugins.disabled" in message
def test_two_broken_plugins_each_get_a_notice_that_claims_nothing_about_the_other(self, tmp_path: Path) -> None:
root = _broken_plugin_root(tmp_path)
twin = root / "wreck2"
(root / "wreck").rename(twin)
(twin / "raven-plugin.toml").write_text(
(twin / "raven-plugin.toml").read_text(encoding="utf-8").replace('"wreck"', '"wreck2"'), encoding="utf-8"
)
_broken_plugin_root(tmp_path / "second")
heard: list[str] = []
reg = build_plugin_registry(
_config(dirs=[str(root), str(tmp_path / "second" / "plugins")]), notify=heard.append
)
assert [f.plugin_id for f in reg.activation_failures()] == ["wreck", "wreck2"]
assert len(heard) == 2
for message in heard:
assert "loaded normally" not in message
assert "Other plugins continue loading" in message
def test_without_a_notifier_it_reaches_stderr(self, tmp_path: Path, capsys) -> None:
build_plugin_registry(_config(dirs=[str(_broken_plugin_root(tmp_path))]))
assert "'wreck' did not load" in capsys.readouterr().err
def test_a_disabled_broken_plugin_is_not_touched(self, tmp_path: Path) -> None:
heard: list[str] = []
reg = build_plugin_registry(
_config(dirs=[str(_broken_plugin_root(tmp_path))], disabled=["wreck"]), notify=heard.append
)
assert reg.activation_failures() == []
assert heard == []
def test_a_backend_whose_plugin_failed_is_not_called_uninstalled(self, tmp_path: Path) -> None:
cfg = _config(memory_backend="wreckmem", dirs=[str(_broken_plugin_root(tmp_path, backend="wreckmem"))])
heard: list[str] = []
reg = build_plugin_registry(cfg, notify=heard.append)
assert maybe_build_memory_backend(tmp_path, cfg, registry=reg, notify=heard.append) is None
memory_notice = heard[-1]
assert "provided by plugin 'wreck', which did not load: importing" in memory_notice
assert "boom" in memory_notice
assert "no installed plugin provides it" not in memory_notice
def test_build_runtime_hands_the_hosts_notifier_to_the_registry(self, tmp_path: Path, monkeypatch) -> None:
from raven.agent.loop.bundles import HostWiring
from raven.contracts.llm_provider import LLMResponse
from raven.core import plugin_stack, runtime
from raven.providers.base import LLMProvider
root = _broken_plugin_root(tmp_path)
monkeypatch.setattr(
plugin_stack,
"plugin_discovery_sources",
lambda: {
"bundled_dir": tmp_path / "none",
"user_dir": root,
"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)
class _Provider(LLMProvider):
def __init__(self) -> None:
super().__init__(api_key="test")
async def chat(self, messages, **kwargs):
return LLMResponse(content="ok", finish_reason="stop")
def get_default_model(self) -> str:
return "fake/default"
config = Config()
config.agents.defaults.workspace = str(tmp_path / "ws")
heard: list[str] = []
rt = runtime.build_runtime(
config, _config(memory_backend=None), provider=_Provider(), host=HostWiring(notify=heard.append)
)
try:
assert any("'wreck' did not load" in m for m in heard)
finally:
rt.discard()
# ---------------------------------------------------------------------------
# The provider grant
# ---------------------------------------------------------------------------
class _GrantWatch:
"""A registry stand-in that records what locator each factory was handed."""
def __init__(self, seen: list) -> None:
self._seen = seen
def hook_names(self):
return ["h"]
def tool_names(self):
return ["t"]
def hook_plugin_id(self, name):
return "p"
def tool_plugin_id(self, name):
return "p"
def build_hook(self, name, *, config, services):
self._seen.append(("hook", services))
return object()
def build_tool(self, name, *, config, services):
self._seen.append(("tool", services))
return object()
class TestProviderGrant:
def test_the_connections_provider_reaches_hooks_and_tools(self, tmp_path: Path) -> None:
seen: list = []
lent = object()
build_plugin_hooks(tmp_path, _config(), registry=_GrantWatch(seen), provider=lent)
build_plugin_tools(tmp_path, _config(), registry=_GrantWatch(seen), provider=lent)
assert [(kind, services.provider) for kind, services in seen] == [("hook", lent), ("tool", lent)]
def test_no_provider_lends_none(self, tmp_path: Path) -> None:
seen: list = []
build_plugin_hooks(tmp_path, _config(), registry=_GrantWatch(seen))
assert [(kind, services.provider) for kind, services in seen] == [("hook", None)]
class TestHostToolsGrants:
"""The host side of ``media_proxy``, ``web_config`` and ``media_config``.
The plugin side is tested with a hand-built locator; this is the link nothing
else pins, and the one that was broken: every entrance hands the stack its
``RavenConfig``, whose ``tools`` lives under ``base``, so reading ``tools`` off
the extension object lent None to every deployment.
"""
def test_the_hosts_media_proxy_and_web_section_reach_tools(self, tmp_path: Path) -> None:
seen: list = []
tools = ToolsConfig(
media=MediaGenConfig(proxy="http://127.0.0.1:7890"),
web=WebToolsConfig(proxy="socks5://127.0.0.1:1080"),
)
config = _config(tools=tools)
build_plugin_tools(tmp_path, config, registry=_GrantWatch(seen))
[(kind, services)] = seen
assert kind == "tool"
assert services.media_proxy == "http://127.0.0.1:7890"
assert services.web_config is not None
assert services.web_config() is config.base.tools.web
assert services.web_config().proxy == "socks5://127.0.0.1:1080"
assert callable(services.media_config)
def test_a_host_without_a_proxy_lends_none_and_its_web_section(self, tmp_path: Path) -> None:
seen: list = []
config = _config()
build_plugin_tools(tmp_path, config, registry=_GrantWatch(seen))
[(_, services)] = seen
assert services.media_proxy is None
assert services.web_config is not None
assert services.web_config() is config.base.tools.web
def test_a_base_config_is_read_as_itself(self, tmp_path: Path) -> None:
"""A caller holding the base ``Config`` rather than the extension is read
directly; the two lanes name the same section."""
from raven.core.plugin_stack import _host_config
base = Config(tools=ToolsConfig(media=MediaGenConfig(proxy="http://10.0.0.1:3128")))
assert _host_config(base) is base
assert _host_config(_config(tools=base.tools)).tools.media.proxy == "http://10.0.0.1:3128"
# ---------------------------------------------------------------------------
# everos plugin presence
# ---------------------------------------------------------------------------
class TestEverosPresence:
def test_the_dev_environment_carries_the_plugin(self) -> None:
assert everos_plugin_installed() is True
def test_an_uninstalled_distribution_reads_as_absent(self) -> None:
with everos_plugin_absent():
assert everos_plugin_installed() is False
def test_a_plugin_that_is_broken_inside_still_reads_as_installed(self) -> None:
"""The two failures a host must not confuse: gone, and here but broken.
A package whose own imports fail is present -- the guard says so, and the
import the caller then makes raises the plugin's own error instead of
being reported as an absence nobody can act on.
"""
with everos_plugin_broken():
assert everos_plugin_installed() is True
with pytest.raises(ImportError):
from raven_everos.health import capability_available # noqa: F401
def test_no_host_module_imports_the_plugin_at_module_level(self) -> None:
"""Every one of these imports has to stay inside a function.
A module-level one fails at ``import raven.cli``, before any guard can
run, and no amount of degrading downstream can recover from that.
"""
root = Path(raven.__file__).parent
offenders = [
f"{path.relative_to(root)}:{node.lineno}"
for path in sorted(root.rglob("*.py"))
for node in ast.parse(path.read_text(encoding="utf-8")).body
if isinstance(node, (ast.Import, ast.ImportFrom))
for name in ([node.module or ""] if isinstance(node, ast.ImportFrom) else [a.name for a in node.names])
if name.startswith("raven_everos")
]
assert offenders == []
def test_the_note_names_the_distribution_and_the_default(self) -> None:
note = everos_plugin_missing_note()
assert "everos-memory" in note
assert "memory.backend" in note
def test_build_plugin_tools_stamps_the_contributing_plugin(tmp_path):
"""[seam-1] The wake grant's namespace comes from this stamp; a plugin
that could name its own namespace could steal another's partition."""
from types import SimpleNamespace
from raven.core.plugin_stack import build_plugin_tools
class _Tool:
name = "t1"
class _Reg:
def tool_names(self):
return ["t1"]
def tool_plugin_id(self, name):
return "plug-a"
def build_tool(self, name, config, services):
return _Tool()
cfg = SimpleNamespace(
memory=SimpleNamespace(user_id="u", agent_id="a"),
plugins=SimpleNamespace(config={}),
)
tools = build_plugin_tools(tmp_path, cfg, registry=_Reg(), provider=None)
assert len(tools) == 1
assert tools[0].contributed_by == "plug-a"
class _OnboardReg:
"""A registry stand-in contributing one onboard step."""
def __init__(self, step) -> None:
self._step = step
def onboard_names(self):
return ["everos"]
def onboard_plugin_id(self, name):
return None
def activated_ids(self):
return []
def manifest_for(self, plugin_id):
return None
def build_onboard_step(self, name, *, config, services):
if isinstance(self._step, Exception):
raise self._step
return self._step
def test_build_onboard_steps_yields_every_activated_step(tmp_path):
from raven.core.plugin_stack import build_onboard_steps
step = object()
assert build_onboard_steps(tmp_path, _config(), registry=_OnboardReg(step)) == [("everos", step)]
def test_build_onboard_steps_skips_a_raising_factory(tmp_path, caplog):
from raven.core.plugin_stack import build_onboard_steps
with caplog.at_level("WARNING"):
assert build_onboard_steps(tmp_path, _config(), registry=_OnboardReg(RuntimeError("boom"))) == []
assert "boom" in caplog.text
def test_build_onboard_steps_hands_each_plugin_its_own_config_slice(tmp_path):
"""[mrbot] Two plugins, differently named: A owns backend 'a-backend'
(no onboard screen), B owns both backend and onboard 'b-backend'. B's
onboard step must receive plugins.config['B'], never A's slice --
exercises the fix (resolve via the onboard entry's own plugin id)
directly, independent of the cross-owner case activation now refuses."""
import sys as _sys
import types
from raven.core.plugin_stack import build_onboard_steps
from raven.plugins import (
Contributes,
DiscoveredPlugin,
ManifestOrigin,
MemoryBackendContribution,
OnboardContribution,
PluginManifest,
PluginRegistry,
)
mod = types.ModuleType("_test_onboard_slice_owner")
mod.make_backend_a = lambda ctx: "a"
mod.make_backend_b = lambda ctx: "b"
mod.make_onboard_step_b = lambda ctx: ("step-b", ctx.config)
_sys.modules["_test_onboard_slice_owner"] = mod
try:
mf_a = PluginManifest(
id="A",
version="0.1",
contributes=Contributes(
memory_backends=[
MemoryBackendContribution(name="a-backend", factory="_test_onboard_slice_owner:make_backend_a"),
],
),
)
mf_b = PluginManifest(
id="B",
version="0.1",
contributes=Contributes(
memory_backends=[
MemoryBackendContribution(name="b-backend", factory="_test_onboard_slice_owner:make_backend_b"),
],
onboard=[
OnboardContribution(name="b-backend", factory="_test_onboard_slice_owner:make_onboard_step_b"),
],
),
)
reg = PluginRegistry()
reg.activate(
[
DiscoveredPlugin(manifest=mf_a, source=ManifestOrigin.USER, location=None),
DiscoveredPlugin(manifest=mf_b, source=ManifestOrigin.USER, location=None),
]
)
cfg = _config(
plugin_config={
"A": {"marker": "a-slice"},
"B": {"marker": "b-slice"},
}
)
steps = build_onboard_steps(tmp_path, cfg, registry=reg)
assert [name for name, _ in steps] == ["b-backend"]
_step_name, received_config = steps[0][1]
assert received_config == {"marker": "b-slice"}
finally:
_sys.modules.pop("_test_onboard_slice_owner", None)
class TestStartBackendDetached:
"""The resident hosts start the memory backend without waiting on it.
Awaited, the start held every resident boot for the readiness budget --
10s on a machine whose first everos start has to compile its bytecode --
and a session that overran it reported no long-term memory while the child
was still coming up. Detached, the backend's own state machine covers that
window: ``store`` answers False so the loop retries, ``recall`` returns no
hits and schedules a probe.
"""
@pytest.mark.asyncio
async def test_the_caller_is_not_held_until_the_start_finishes(self) -> None:
"""The whole point: control comes back before ``start()`` is done."""
import asyncio
from raven.core.plugin_stack import start_backend_detached
released = asyncio.Event()
finished = asyncio.Event()
class _SlowBackend:
async def start(self) -> None:
await released.wait()
finished.set()
start_backend_detached(_SlowBackend(), logger=logging.getLogger(__name__))
# Still blocked inside start(), and we are already here.
assert not finished.is_set()
released.set()
await asyncio.wait_for(finished.wait(), timeout=2)
@pytest.mark.asyncio
async def test_the_task_is_held_so_it_cannot_be_collected_mid_flight(self) -> None:
"""asyncio keeps only a weak reference; a dropped task is a lost start."""
import asyncio
import gc
from raven.core import plugin_stack
gate = asyncio.Event()
ran = asyncio.Event()
class _Backend:
async def start(self) -> None:
await gate.wait()
ran.set()
plugin_stack.start_backend_detached(_Backend(), logger=logging.getLogger(__name__))
gc.collect()
assert any(t is not None for _b, t in plugin_stack._PENDING_BACKEND_STARTS), "the in-flight start was not held"
gate.set()
await asyncio.wait_for(ran.wait(), timeout=2)
await asyncio.sleep(0)
assert not plugin_stack._PENDING_BACKEND_STARTS, "a finished start was not released"
@pytest.mark.asyncio
async def test_a_start_that_raises_is_logged_and_not_re_raised(self) -> None:
"""The three call sites all swallowed it; the helper owes them the same."""
import asyncio
from raven.core import plugin_stack
class _Failing:
async def start(self) -> None:
raise RuntimeError("no service here")
logged: list[str] = []
class _Logger:
def exception(self, msg: str) -> None:
logged.append(msg)
plugin_stack.start_backend_detached(_Failing(), logger=_Logger())
for _ in range(20):
await asyncio.sleep(0)
if logged:
break
assert logged and "memory backend start failed" in logged[0]
@pytest.mark.asyncio
async def test_no_backend_is_nothing_to_do(self) -> None:
"""``backend`` is None when no memory plugin is wired."""
from raven.core import plugin_stack
plugin_stack.start_backend_detached(None, logger=logging.getLogger(__name__))
assert not plugin_stack._PENDING_BACKEND_STARTS
@pytest.mark.asyncio
async def test_cancel_retires_a_start_still_in_flight(self) -> None:
"""A start left running polls an address for a generation that is gone."""
import asyncio
from raven.core import plugin_stack
entered = asyncio.Event()
cancelled = asyncio.Event()
class _Hanging:
async def start(self) -> None:
entered.set()
try:
await asyncio.sleep(30)
except asyncio.CancelledError:
cancelled.set()
raise
backend = _Hanging()
plugin_stack.start_backend_detached(backend, logger=logging.getLogger(__name__))
await asyncio.wait_for(entered.wait(), timeout=2)
await plugin_stack.cancel_pending_backend_starts(backend)
assert cancelled.is_set(), "cancel returned before the start had left"