## Summary
`raven.config` and `raven.agent` imported each other. config validated sub-agent
declarations against tables living in `raven.agent.subagent`; the agent package
reads config for its shapes and paths. Both sides deferred their import into a
function body to keep module init off the cycle, which is what kept it invisible
to everything except a graph walk.
The validation needs the **vocabulary**, not the behaviour: which names are
legal, never what those names run. Only membership was ever read at the two call
sites -- `command`, `description`, `kind` and `readyTimeoutMs` were not. So the
names move down to `raven/config/agent_names.py` and the tables stay where they
are:
- **config owns what is legal** -- `BUILTIN_AGENT_NAMES`, `LEGACY_AGENT_ALIASES`,
`THIRD_PARTY_PRESET_NAMES`, `canonical_agent_name`, `is_builtin_agent_name`.
Pure data and pure string functions, no IO.
- **`raven.agent.subagent` owns what it does** -- the preset table with its
commands, transports and timeouts is untouched.
`builtin_agents` re-exports the moved names, so its eight consumers (`registry`,
`rpc.methods.subagents`, `manager`, `spawn_tool`,
`context_engine.segments.identity`) are not touched at all.
### Why the check did not move up instead
Moving the validation later, rather than moving the data down, would also break
the cycle and would cost nothing in cohesion. It was rejected because the check
is deliberately at parse time. It runs as a `@model_validator(mode="after")` on
all three sub-agent transports, on both paths that build a model from a dict
(`load_config` reading the file, and `update_subagents` validating before an
atomic write). Its own docstring names what it catches: a hand-edited
`preset: "hermes"` on an unrelated entry, which would otherwise both dodge the
reserved-name guard and hide the real Hermes preset from the Presets group.
An invalid row would become constructible, and every later reader would treat it
as valid.
### The cost, and the pin
The split spells one name set in two places. A preset added to
`THIRD_PARTY_SUBAGENT_PRESETS` without its key added to the vocabulary would ship
and then be refused by the schema meant to accept it, with nothing reporting the
mismatch. `tests/test_subagent_name_vocabulary.py` pins both sets equal in both
directions, and does the same for the seed table against the reserved names.
## Type
- [ ] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [x] Refactor
- [ ] Other
## Verification
```
$ uv run --frozen --python 3.12 lint-imports
Contracts: 9 kept, 0 broken.
$ uv run --frozen --python 3.12 pytest \
tests/test_subagent_name_vocabulary.py tests/test_subagent_builtin_agents.py \
tests/test_update_subagents.py tests/test_subagent_registry.py \
tests/test_subagent_acp.py tests/test_rpc_subagents.py tests/test_kernel_closure.py -q
350 passed in 13.91s
$ uv run --frozen --python 3.12 --extra dev ruff check <the five changed files>
All checks passed!
$ uv run --frozen --python 3.12 --extra dev ruff format --check <the five changed files>
5 files already formatted
$ make check-commits COMMIT_RANGE=origin/main..HEAD
exit 0
```
Both the contract run and the test run above were repeated after rebasing onto
the current main, since this one touches production code.
Every call site of every moved symbol was enumerated before the edit
(`BUILTIN_AGENT_NAMES`, `canonical_agent_name`, `is_builtin_agent_name`,
`GENERIC_AGENT`, `LEGACY_AGENT_ALIASES`, `THIRD_PARTY_SUBAGENT_PRESETS`) and the
re-export is what keeps them all valid.
Effect on the dependency graph, measured before and after:
```
config -> agent edges 2 -> 0
packages in an import cycle 22 -> 19 (out of 40)
mutually importing pairs 14 -> 13
```
The three packages that leave the cycle were only ever pulled in through this
one pair.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed
No user-facing surface changes.
## Risk
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
No behaviour change: the same names, the same validation, the same failure
messages. The reserved-name guard and the preset provenance check both still run
at parse time on all three transports. Backward compatibility is why
`builtin_agents` re-exports rather than relocating its public surface, and why
`LEGACY_AGENT_ALIASES` moves intact -- stored records under the old lowercase
name still resolve.
Rollback is reverting the one commit. The risk this leaves behind is the
two-spellings drift, which is why the equality pin ships in the same commit
rather than as a follow-up.
## Related Issues
N/A
140 lines
4.9 KiB
Python
140 lines
4.9 KiB
Python
"""Factories for the tool face code-flow contributes (raven-plugin.toml names them).
|
|
|
|
Every factory reads the flow slice's ``tools`` section and declines (returns
|
|
None) when that section leaves the face off or the slice does not parse, so a
|
|
product that wants the host's own tools simply says so and gets them.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING
|
|
|
|
from loguru import logger
|
|
from pydantic import ValidationError
|
|
|
|
from code_flow.config import FlowConfig, ToolsConfig
|
|
from code_flow.tools.exec import SPILL_SUBDIR, CodeExecTool, CodeExecutor
|
|
from code_flow.tools.filesystem import EditFileTool, ListDirTool, ReadFileTool, WriteFileTool
|
|
from code_flow.tools.read_state import owner_for
|
|
from code_flow.tools.search import GlobTool
|
|
from code_flow.tools.todo import STORES, TodoTool
|
|
|
|
if TYPE_CHECKING:
|
|
from raven.plugins.context import PluginContext
|
|
|
|
|
|
def _face(ctx: "PluginContext", seat: str) -> ToolsConfig | None:
|
|
"""The tool face's own section of the flow slice, or None to decline."""
|
|
try:
|
|
flow = FlowConfig.from_slice(dict(ctx.config or {}))
|
|
except ValidationError as exc:
|
|
logger.warning("code-flow: config slice is malformed; declining {}: {}", seat, exc)
|
|
return None
|
|
if not flow.tools.enabled:
|
|
return None
|
|
return flow.tools
|
|
|
|
|
|
def _fence(ctx: "PluginContext", cfg: ToolsConfig) -> tuple[Path, ...]:
|
|
"""The roots these tools may reach, mirroring what the host grants its own.
|
|
|
|
The host computes ``allowed_dirs = (workspace,) if restrict_to_workspace``
|
|
(raven/agent/loop/wiring.py) and an empty tuple means the fence is OFF --
|
|
which is what these replacements used to pass, whatever the product asked
|
|
for. The section carries the answer because a plugin factory cannot read
|
|
the host field; the launcher renders it in.
|
|
"""
|
|
return (Path(ctx.services.workspace),) if cfg.restrict_to_workspace else ()
|
|
|
|
|
|
def make_read_file(ctx: "PluginContext") -> ReadFileTool | None:
|
|
cfg = _face(ctx, "read_file")
|
|
if not cfg:
|
|
return None
|
|
return ReadFileTool(workspace=ctx.services.workspace, allowed_dirs=_fence(ctx, cfg), read_owner=owner_for(ctx))
|
|
|
|
|
|
def make_write_file(ctx: "PluginContext") -> WriteFileTool | None:
|
|
cfg = _face(ctx, "write_file")
|
|
if not cfg:
|
|
return None
|
|
return WriteFileTool(
|
|
workspace=ctx.services.workspace,
|
|
allowed_dirs=_fence(ctx, cfg),
|
|
syntax_note=cfg.python_syntax_note,
|
|
read_owner=owner_for(ctx),
|
|
)
|
|
|
|
|
|
def make_edit_file(ctx: "PluginContext") -> EditFileTool | None:
|
|
cfg = _face(ctx, "edit_file")
|
|
if not cfg:
|
|
return None
|
|
return EditFileTool(
|
|
workspace=ctx.services.workspace,
|
|
allowed_dirs=_fence(ctx, cfg),
|
|
syntax_note=cfg.python_syntax_note,
|
|
read_owner=owner_for(ctx),
|
|
require_read=cfg.require_read_before_edit,
|
|
)
|
|
|
|
|
|
def make_list_dir(ctx: "PluginContext") -> ListDirTool | None:
|
|
cfg = _face(ctx, "list_dir")
|
|
if not cfg:
|
|
return None
|
|
return ListDirTool(workspace=ctx.services.workspace, allowed_dirs=_fence(ctx, cfg))
|
|
|
|
|
|
def make_glob(ctx: "PluginContext") -> GlobTool | None:
|
|
cfg = _face(ctx, "glob")
|
|
if not cfg:
|
|
return None
|
|
return GlobTool(workspace=ctx.services.workspace, allowed_dirs=_fence(ctx, cfg))
|
|
|
|
|
|
def make_exec(ctx: "PluginContext") -> CodeExecTool | None:
|
|
"""The shell, with the clamped ceiling, partial output on timeout and the
|
|
30,000-character budget with spill. Served only where the host's own exec
|
|
would run on the host too (``tools.sandbox.backend == "none"``): the
|
|
replacement runs on the host, and a configured sandbox must not be
|
|
bypassed by a same-name tool -- so under any other backend this declines
|
|
and the host's exec keeps the shell."""
|
|
cfg = _face(ctx, "exec")
|
|
if not cfg:
|
|
return None
|
|
if cfg.exec.sandbox_backend != "none":
|
|
logger.info(
|
|
"code-flow: tools.sandbox.backend is {!r}; the host's exec keeps the shell", cfg.exec.sandbox_backend
|
|
)
|
|
return None
|
|
home = Path(ctx.services.workspace)
|
|
executor = CodeExecutor(max_timeout=cfg.exec.max_timeout, spill_dir=home / SPILL_SUBDIR)
|
|
return CodeExecTool(
|
|
timeout=cfg.exec.timeout,
|
|
working_dir=str(home),
|
|
restrict_to_workspace=cfg.restrict_to_workspace,
|
|
path_append=cfg.exec.path_append,
|
|
executor=executor,
|
|
extra_allowed_dirs=(home,),
|
|
max_timeout=cfg.exec.max_timeout,
|
|
)
|
|
|
|
|
|
def make_todo(ctx: "PluginContext") -> TodoTool | None:
|
|
"""The checklist, on the store the flow hook binds per session: one store
|
|
per Agent home, so the hook factory and this one meet on the same object."""
|
|
return TodoTool(STORES.for_home(ctx.services.workspace)) if _face(ctx, "todo") else None
|
|
|
|
|
|
__all__ = [
|
|
"make_edit_file",
|
|
"make_exec",
|
|
"make_glob",
|
|
"make_list_dir",
|
|
"make_read_file",
|
|
"make_todo",
|
|
"make_write_file",
|
|
]
|