Skip to content
66 changes: 58 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,56 @@ the start of the entry's title.

### Added

- **Agent types started at run time: `AgentConfig.spawnable_by`, free types
and `SubAgentPolicy.spawn_sources`, on both backends.** A type may be
started while the app runs, besides by its parent in the configured tree,
when the app allows it twice: the type's `spawnable_by` (a set of `"agent"`,
`"skill"`, `"user"`; the agent YAML's `spawnable_by: [agent]`; None, the
default, for a type only its parent starts) and the policy's
`spawn_sources` (the same three, `frozenset()` by default: nothing is
started at run time, and an app keeps its `task` as it was, byte for
byte). An agent's `task` call is the `"agent"` source: `task` lists, after
the caller's own sub-agents, the types agents may start, each with its
description (`agentic_cli.workflow.agents.spawn`, provisional: `spawnable`,
`may_spawn`, `can_have_children`; the manager's delegations'
`offered(agent, depth)`), so an agent with no sub-agents of its own gets
`task` when there is a type it may start, and, with `follow_ups`, the
parent's tools and the roster. A run is numbered, recorded and run as a
delegation is: `<type>-<n>`, `started_by: agent:<instance>`, parent the
caller, its envelope the call's result. A type in no `sub_agents` list
that sets `spawnable_by` is a free type (`AgentGraph.free_types`, in config
order): never the root, wherever it is listed, in configs or YAML, and built
as any sub-agent is (ADK: a runner of its own; Deep Agents: a graph of its
own). An inheriting type is offered to the root alone. `validate_agent_graph`
takes the policy (`policy=`, keyword; the managers pass theirs) and raises
`AgentGraphError` for a root that sets `spawnable_by` ("Agent 'lead' is the
root agent and cannot set spawnable_by: only a sub-agent type is started at
run time."; with every unreferenced agent free, "Agent graph has no root:
…"), a source that is not one of the three ("Agent 'helper' has
spawnable_by {'agents'}: each source must be one of 'agent', 'skill',
'user'.") and an empty one; a free type no source the policy enables can
start is a warning (`subagent_type_unreachable`), since an app may enable
sources per install. `SubAgentPolicy` keeps `spawn_sources` as a frozenset
and refuses, with a `ValueError`, a source other than the three
(`SubAgentPolicy.spawn_sources has {'agents'}: each source must be one of
'agent', 'skill', 'user'.`).
- **`SubAgentPolicy.max_spawn_depth`: from how deep a run may start a type at
run time** (1 by default, a positive int): a run at depth `d` (the root is
0) is offered the types agents may start only while `d` is below it, so by
default the root alone starts them, and a type started at depth 1 starts
none; the configured tree keeps the depth it declares. `task` is offered
for each run, on both backends: a type that runs at several depths lists
what its run may start (ADK declares it for each request; Deep Agents
rewrites a sub-agent's for each model call), a run offered nothing has no
`task` (a call of it gets the answer for a tool the agent does not have),
and a call naming a type its run is not offered is refused as an unknown
`agent_type`, no run started. When no run a call starts may start one,
`task`'s description says so after its limits: "Sub-agents cannot start
sub-agents."
- **The main agent's requests list its open sub-agents, when it can continue
them.** With `SubAgentPolicy(follow_ups=True)` a main agent with sub-agents
(it has `message_agent`) is told which of its own sub-agents are open: a
them.** With `SubAgentPolicy(follow_ups=True)` a main agent that can have
sub-agents (its own, or types it may start at run time; it has
`message_agent`) is told which of its own sub-agents are open: a
roster after the memory index, `<agents>`, the line `Your sub-agents in
this conversation (message_agent continues one):`, then one line per
idle, stopped or running instance, newest first, `- <id> (<type>,
Expand Down Expand Up @@ -675,8 +722,9 @@ the start of the entry's title.
session through `self.session`, not as a class attribute with a value.
- **A parent's tools that continue its sub-agents, `message_agent`,
`stop_agent` and `check_agents`, on both backends, with
`SubAgentPolicy(follow_ups=True)`.** Every agent with sub-agents gets them
beside `task`; with the policy at its defaults (`follow_ups` False) an
`SubAgentPolicy(follow_ups=True)`.** Every agent that can have sub-agents
(its own, or types it may start at run time) gets them beside `task`; with
the policy at its defaults (`follow_ups` False) an
agent gets `task` alone. The names are declared by the framework, exempt,
as `task` is (`tools/_core/delegation.py`: `FOLLOW_UP_TOOLS`), so an app's
`register_tool(name="message_agent")` (or either other) raises, an agent
Expand Down Expand Up @@ -1898,16 +1946,18 @@ the start of the entry's title.
rather than replace it.
- **One `task` tool on both backends, which takes a brief.** **Breaking:**
ADK offered one tool per sub-agent (`researcher(request=...)`) and Deep
Agents its own `task(description=, subagent_type=)`; now every agent
with sub-agents is offered the framework's `task`, the same on both
(`workflow/agents/brief.py`): `agent_type`, one of its own sub-agents,
Agents its own `task(description=, subagent_type=)`; now every agent with
sub-agents (or types it may start at run time: see Added: **Agent types
started at run time…**) is offered the framework's `task`, the same on both
(`workflow/agents/brief.py`): `agent_type`, one of the types the calling run
may start (its own sub-agents first),
each listed in the tool's description with what it does and the brief
fields it requires, then the brief, `title` and `objective` (required),
`deliverable`, `constraints`, `known` and `done_when`. The run's first
message is the task message, `# Task: <title>` and a section per filled
field (`## Objective`, `## Deliverable`, `## Constraints`, `## What is
already known`, `## Done when`). A call whose brief misses a field its
type requires, or names a type that is not the caller's, starts no run:
type requires, or names a type its run may not start, starts no run:
its result is `{"success": false, "error": ...}` naming what to fix. A
instance's first run starts with no plan and no tasks (its follow-ups continue its own), and what it saves stays with it (the
status bar's task list is the conversation's alone). On ADK nothing else
Expand Down
16 changes: 8 additions & 8 deletions CLAUDE.md

Large diffs are not rendered by default.

52 changes: 46 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ manager = DeepAgentsWorkflowManager(
```

What it supports:
- **Delegation through `task`**: a coordinator (an agent with `sub_agents`)
- **Delegation through `task`**: a coordinator (an agent with `sub_agents`, or
with types it may start at run time)
gets the framework's `task` tool, as on ADK (see
[Sub-agent context](#sub-agent-context)). Each sub-agent's graph runs
apart from its coordinator's, in a task of its own, from the brief the
Expand Down Expand Up @@ -1290,13 +1291,19 @@ configs = [coordinator, researcher, analyst]
| `brief_required` | tuple[str, ...] | The brief fields a delegation to this agent must fill (default `("objective",)`; `title` and `objective` are always required): any of `deliverable`, `constraints`, `known`, `done_when`. A `task` call that leaves one empty starts no run, and its caller reads which. Not on the root |
| `limits` | AgentLimits \| None | The agent's own per-run limits (`max_model_calls`, `timeout` in seconds, `max_tokens`), laid over the app's `SubAgentPolicy.limits` field by field: each of its runs works under them, and one past a limit ends (see [Sub-agent context](#sub-agent-context)). Not on the root |
| `continuable` | bool | Whether an instance of this agent takes follow-ups after its run (`True`, the default) or is closed when its run ends (`False`, one-shot); see [Sub-agent context](#sub-agent-context). Not `False` on the root |
| `spawnable_by` | frozenset[str] \| None | Who may start this agent at run time besides its parent: a set of `"agent"` (another agent's `task` call), `"skill"` and `"user"`, each also enabled by the app's `SubAgentPolicy.spawn_sources`; `None` (the default): only its parent. One in no `sub_agents` list is a free type, never the root; see [Sub-agent context](#sub-agent-context). Not on the root |

The agent graph is validated before anything is allocated — ahead of model
discovery and service creation. Duplicate names, unknown `sub_agents`
references, self-references, delegation cycles, a sub-agent shared by two
parents, and **more than one root** raise `AgentGraphError` naming the
offending agents. Exactly one agent may be unreferenced: the runner starts from
a single root, so agents under any other root would never run.
a single root, so agents under any other root would never run. An agent that
sets `spawnable_by` and is in no `sub_agents` list is a free type, started at
run time only, and no root however it is listed; the root may not set
`spawnable_by`, and a `spawnable_by` that is empty or names a source other
than `agent`, `skill` and `user` raises too (a free type the app's policy lets
no source start is a warning: `validate_agent_graph(configs, policy=...)`).
`inherit_context=True` on the root, on an agent with sub-agents of its own or
on an agent below another sub-agent, and an `inherit_context` that is not True
or False, raise it too, and, when the agents are built, so does a coordinator
Expand All @@ -1314,7 +1321,8 @@ signatures, `async def` factories, and non-string results raise

A sub-agent is a task its parent hands off and gets back in the same turn, on
both backends. The parent calls the framework's `task` tool, the same on both:
`agent_type`, one of its own sub-agents, and a brief, `title` and `objective`
`agent_type`, one of its own sub-agents (or of the types it may start at run
time, below), and a brief, `title` and `objective`
(required), `deliverable`, `constraints`, `known` (what the parent already
knows that the task needs) and `done_when`. The sub-agent's run starts from its
task message, the brief as sections (`# Task: <title>`, `## Objective`, …); it
Expand Down Expand Up @@ -1417,8 +1425,37 @@ reviewer = AgentConfig(name="reviewer", prompt="Check what was said.",
a type that is not continuable), and new
instances are numbered past the stored ones. Deleting the conversation
deletes them, after the transcripts.
- **Starting agents at run time.** A type may also be started while the app
runs, by an agent's `task` call, when the app allows it twice: the type's
`spawnable_by={"agent"}` and the policy's
`subagents=SubAgentPolicy(spawn_sources={"agent"})` (off by default:
`spawnable_by` is unset and `spawn_sources` empty, so `task` lists the
caller's own sub-agents alone). `task` then lists, after
them, every type agents may start, each with its description, so a main
agent needs no `sub_agents` of its own; a type in no `sub_agents` list is a
free type, never the root. Such a run is a delegation like any other
(`helper-1`, `started_by: agent:root`, its envelope the call's result).
`max_spawn_depth` (1) says from how deep: a run at depth `d` (the root is
0) may start one only while `d` is below it, so by default only the main
agent does, and `task`'s description then says "Sub-agents cannot start
sub-agents."; a run that may start nothing is offered no `task`, and the
configured tree keeps its own depth. An inheriting type is offered to the
main agent alone.

```python
coordinator = AgentConfig(name="coordinator", prompt="Hand self-contained "
"checks to fact_checker.")
fact_checker = AgentConfig(name="fact_checker", prompt="Check one claim.",
description="Checks one claim against sources.",
spawnable_by=frozenset({"agent"}))
manager = create_workflow_manager_from_settings(
[coordinator, fact_checker], settings,
subagents=SubAgentPolicy(spawn_sources={"agent"}),
)
```
- **The parent's tools.** With `subagents=SubAgentPolicy(follow_ups=True)`
every agent with sub-agents gets three more tools beside `task` (with the
every agent that can have sub-agents (its own, or types it may start at run
time) gets three more tools beside `task` (with the
policy's defaults, `task` alone): `message_agent(agent, message,
interrupt=false)` sends one of its sub-agents a message, which an idle
one runs as a follow-up (the envelope comes back as `task`'s does) and a
Expand Down Expand Up @@ -1469,8 +1506,10 @@ reviewer = AgentConfig(name="reviewer", prompt="Check what was said.",
the view first; a closed sub-agent's view has no prompt (`/back` only).
The run's questions and a command's dialogs open one at a time, each
waiting for the other ([Customizing the CLI](#customizing-the-cli)).
- **What the agent graph refuses.** `inherit_context` is for sub-agents only, and
only for the main agent's own sub-agents that have none of their own. A
- **What the agent graph refuses.** `inherit_context` is for sub-agents only,
and only for the main agent's own sub-agents that have none of their own.
`spawnable_by` is for sub-agents only, and names `agent`, `skill` or `user`.
A
coordinator cannot have a tool of its own named `task` (nor, with
`follow_ups`, one named `message_agent`, `stop_agent` or `check_agents`).
Each raises `AgentGraphError` at startup.
Expand Down Expand Up @@ -1608,6 +1647,7 @@ are `AgentConfig`'s, and an unknown one is an error:
| `mcp_servers`, `skills` | As on `AgentConfig`: MCP servers (`name`, `transport`, `command`/`args` or `url`, ...) and skill names |
| `sub_agents`, `inherit_context` | The agents it may delegate to, and what a sub-agent sees: `false` (its task alone, the default) or `true` (its parent's conversation too; see [Sub-agent context](#sub-agent-context)) |
| `brief_required`, `limits`, `continuable` | As on `AgentConfig`: `brief_required: [objective, deliverable]`, `limits: {max_model_calls: 30, timeout: 600, max_tokens: 200000}` (each limit read as written), `continuable: false` (one-shot) |
| `spawnable_by` | Who may start it at run time, a list: `spawnable_by: [agent]` (read as a set; with no `sub_agents` naming it, a free type) |
| `include_state_tools` | `true` by default: the plan and task tools are added |

- **A tool is a name, resolved when the manager is built.** A bare name is
Expand Down
33 changes: 20 additions & 13 deletions src/agentic_cli/workflow/adk/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
from agentic_cli.workflow.adk.tool_result_cap_plugin import ToolResultCapPlugin
from agentic_cli.workflow.agents.delegate import AgentRun, RunAnswer, RunFailed, failure_reason
from agentic_cli.workflow.agents.runs import own_plan_and_tasks
from agentic_cli.workflow.agents.spawn import can_have_children
from agentic_cli.workflow.context import OVERFLOW_ERROR_CODE
from agentic_cli.tools._core.delegation import (
FOLLOW_UP_CLASH_ERROR,
Expand Down Expand Up @@ -488,18 +489,20 @@ def _build_agent(
service_map: dict,
*,
root: bool,
types: "Mapping[str, AgentConfig] | None" = None,
delegates: bool = False,
) -> Agent:
"""Construct one ``LlmAgent`` from a config, on its own.

Its model, generation settings and thinking come from the model
initialization resolved for it, built by the model factory.

No agent has ADK ``sub_agents``, so ADK offers none of them
``transfer_to_agent``. An agent that delegates (``types``: its
sub-agents, by name) gets the framework's ``task`` tool
(``adk/task_tool.py::TaskTool``), whose calls run each sub-agent on
that sub-agent type's own runner (``_run_agent``); with
``transfer_to_agent``. An agent that delegates (``delegates``: it has
sub-agents, or may start types at run time, ``spawn.can_have_children``)
gets the framework's ``task`` tool (``adk/task_tool.py::TaskTool``),
which declares for each request what the run that asks may start, and
whose calls run each sub-agent on that type's own runner
(``_run_agent``); with
``SubAgentPolicy.follow_ups`` it also gets ``message_agent``,
``stop_agent`` and ``check_agents`` (``FollowUpTool``). Only the root's
long-running tools are wrapped: a pending one would pause a
Expand All @@ -517,14 +520,14 @@ def _build_agent(
tools = self._assemble_agent_tools(config, service_map)
if root:
tools = self._wrap_long_running(tools)
if types:
if delegates:
names = _tool_names(tools)
if TASK_TOOL in names:
raise AgentGraphError(
f"Agent {config.name!r} delegates to sub-agents through a tool named "
f"{TASK_TOOL!r}, so its own tools cannot include one; rename that tool."
)
tools = [*tools, TaskTool(config.name, types, lambda: self._delegations)]
tools = [*tools, TaskTool(config.name, lambda: self._delegations, root=root)]
if self.subagent_policy.follow_ups:
clash = next((name for name in FOLLOW_UP_TOOLS if name in names), None)
if clash is not None:
Expand Down Expand Up @@ -570,26 +573,30 @@ def _create_agents(self) -> Agent:
# validates its own.
graph, self._validated_graph = getattr(self, "_validated_graph", None), None
if graph is None:
graph = validate_agent_graph(self._agent_configs, backend=self.backend_type)
graph = validate_agent_graph(
self._agent_configs, backend=self.backend_type, policy=self.subagent_policy
)

agents: dict[str, LlmAgent] = {}
service_map = self._get_service_tool_map()
configs = list(graph.config_map.values())

from agentic_cli.config import SettingsContext

# Prompt factories run inside the manager's settings context.
with SettingsContext(self._settings):
for name in graph.build_order:
config = graph.config_map[name]
types = {sub: graph.config_map[sub] for sub in config.sub_agents}
root = name == graph.root_name
delegates = can_have_children(config, configs, self.subagent_policy, root=root)
agents[name] = self._build_agent(
config, service_map, root=name == graph.root_name, types=types
config, service_map, root=root, delegates=delegates
)
logger.debug(
"agent_created",
name=name,
type="coordinator" if types else "leaf",
sub_agents=list(types),
type="coordinator" if delegates else "leaf",
sub_agents=list(config.sub_agents),
)

self._agents = agents
Expand Down Expand Up @@ -1175,7 +1182,7 @@ def _validate_agent_graph(self) -> AgentGraph | None:
self._required_managers = self._detect_required_managers()
return AgentGraph(config_map={}, build_order=(), root_name=root.name)
self._validated_graph = validate_agent_graph(
self._agent_configs, backend=self.backend_type
self._agent_configs, backend=self.backend_type, policy=self.subagent_policy
)
return self._validated_graph

Expand Down
Loading
Loading