Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,46 @@ the start of the entry's title.

### Added

- **The built-in `general_purpose` type: `SubAgentPolicy.general_purpose`, on
both backends.** With `general_purpose=True` (False by default; True with
no `spawn_sources` is a `ValueError`: "SubAgentPolicy.general_purpose needs
spawn_sources: with none, nothing could start it."), every source the
policy enables may start `general_purpose`, a type the app does not
configure: an agent's `task` lists it after the app's types ("-
general_purpose: A general-purpose agent with your own tools (but not the
ones that start agents) and your model, for a self-contained task no other
agent fits."), a skill may name it (`agent: general_purpose`), and the user
may start it (`/agent new general_purpose <objective>`). Its run has the
tools, MCP servers, skills and model of the type that started it (the
caller's type for `task` and a skill, the main agent for the user; the
type's own model, never a run's model choice), the framework's system
prompt, a fresh context and the plan's tools, and it starts no agent: no
`task`, none of the parent's tools, and a skill it loads that names an
agent is refused. `task`'s `tools` narrows it against its spawner's tools;
its `model` is refused (it has no model choices). Each backend builds one
for every type that may start it, at initialization (the main agent, and
with `max_spawn_depth` 2 or more every type that does not inherit when
agents may start types, and every type with skills when skills may), with
MCP connections of its own (ADK: an agent and a runner of its own,
connecting at first use; Deep Agents: a graph of its own, whose MCP
sessions open at initialization, so a server that cannot start is a notice
for it too, naming its spawner in `metadata["spawner"]`, and one that never
answers is waited for once more per spawner), built again by a model
switch and released with the others; a main agent with no sub-agents gets
`task` for it alone when agents may start it. Its instances are `general_purpose-<n>`, and the
records keep the type each was made for (`InstanceRecord.based_on`, from
the run's start, `WorkflowEvent.agent_run(based_on=)`; records written
before it read None), so its follow-ups, the user's and after a restart,
run as its first run did (one whose type no longer makes one is refused as
a type that is gone), and its view shows that type's model and context.
While the policy enables it, an app's agent of that name is refused
(`AgentGraphError`: "An agent named 'general_purpose' clashes with the
built-in general_purpose type, which this app's SubAgentPolicy
enables."). `agentic_cli.workflow.agents.spawn` (provisional):
`GENERAL_PURPOSE`, `GENERAL_PURPOSE_PROMPT`, `GENERAL_PURPOSE_DESCRIPTION`,
`general_purpose_config`, `general_purpose_spawners`, `root_type`; the
manager's delegations' `general_purpose` and `config_for(agent_type,
based_on=)`.
- **`/agent new <type> <objective>`: you start a sub-agent, and its view
opens on its first run.** A type whose `spawnable_by` names `"user"`, in
an app whose policy's `spawn_sources` names it too
Expand Down
16 changes: 8 additions & 8 deletions CLAUDE.md

Large diffs are not rendered by default.

31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,8 @@ 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=...)`).
An agent named `general_purpose` raises too while the policy enables the
built-in type of that name (`SubAgentPolicy(general_purpose=True)`).
`model_choices` on the root, one that is no tuple or list of texts, and a choice
named twice raise it too.
`inherit_context=True` on the root, on an agent with sub-agents of its own or
Expand Down Expand Up @@ -1444,7 +1446,8 @@ reviewer = AgentConfig(name="reviewer", prompt="Check what was said.",
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.
main agent alone, and with the policy's `general_purpose` the built-in
general-purpose type is offered last (below).

```python
coordinator = AgentConfig(name="coordinator", prompt="Hand self-contained "
Expand Down Expand Up @@ -1511,6 +1514,32 @@ reviewer = AgentConfig(name="reviewer", prompt="Check what was said.",

Its body is all a fresh type's run reads (an inheriting type reads the
conversation too), so write it as a task that stands on its own.
- **The general-purpose type.** `SubAgentPolicy(spawn_sources=..., general_purpose=True)`
adds a type the app does not configure, `general_purpose`, which every
source the policy enables may start: `task` lists it last ("A
general-purpose agent with your own tools (but not the ones that start
agents) and your model, for a self-contained task no other agent fits."),
a skill may name it (`agent: general_purpose`), and `/agent new
general_purpose <objective>` starts one. Its run has the tools, MCP
servers, skills and model of the type that started it (the main agent's,
for the user's start), the framework's system prompt and a fresh context,
and starts no agent itself; `task`'s `tools` narrows it as any type, and
it has no model choices. One is built for each type that may start it
(the main agent, and with `max_spawn_depth` 2 or more each type that does
not inherit when agents may start types, and each type with skills when
skills may), with MCP connections of its own. Its instances
(`general_purpose-1`) keep the type that started them, so a follow-up runs
as the first run did. An agent of the app's named `general_purpose` is
refused while the policy enables it.

```python
manager = create_workflow_manager_from_settings(
[coordinator, researcher], settings,
subagents=SubAgentPolicy(spawn_sources={"agent", "user"}, general_purpose=True),
)
# The coordinator's call: {"agent_type": "general_purpose", "title": "Tally it",
# "objective": "Count the TODO comments under src/ by file."}
```
- **The parent's tools.** With `subagents=SubAgentPolicy(follow_ups=True)`
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
Expand Down
9 changes: 5 additions & 4 deletions src/agentic_cli/cli/agent_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,21 +583,22 @@ def _runs_of(workflow: Any, conversation: "SessionRef", instance: str) -> list["

def _model(workflow: Any, record: "InstanceRecord") -> Any:
"""The model the instance runs on: its model choice's when it has one, else its
type's."""
type's (a ``general_purpose`` one's, the type that started it: ``based_on``)."""
if record.model is not None:
return getattr(workflow, "choice_models", {}).get((record.agent, record.model))
return getattr(workflow, "agent_models", {}).get(record.agent)
return getattr(workflow, "agent_models", {}).get(record.based_on or record.agent)


def _budget(workflow: Any, record: "InstanceRecord") -> Any:
"""The instance's context budget (its model choice's, when it has one).
"""The instance's context budget (its model choice's, when it has one; a
``general_purpose`` one's, the type that started it: ``based_on``).

None when its manager manages none.
"""
try:
if record.model is not None:
return workflow.context_budget(record.agent, model=record.model)
return workflow.context_budget(record.agent)
return workflow.context_budget(record.based_on or record.agent)
except (KeyError, RuntimeError, AttributeError):
return None

Expand Down
30 changes: 25 additions & 5 deletions src/agentic_cli/tools/skills/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
agent is built: the app still starts, since a skill file can come from a
user's folder.

A skill may name the built-in ``general_purpose`` type (with the policy's
``general_purpose``): its run is made for the loading agent's type, whose tools
and model it has. A general-purpose agent's own loads start no agent (it starts
none: ``spawn.can_have_children``), and its skills are not warned of: its
spawner's build warns of its own.

``SkillAgents`` is one loading agent's: ADK's ``adk/skill_tool.py::SkillLoadTool``
and Deep Agents' ``task_tool.start_skill_run`` call it. Imports no backend.
"""
Expand All @@ -33,7 +39,12 @@
from agentic_cli.logging import Loggers
from agentic_cli.workflow.agents.brief import Brief
from agentic_cli.workflow.agents.delegate import Delegated, StartRequest
from agentic_cli.workflow.agents.spawn import SKILL_SOURCE, may_spawn, spawnable
from agentic_cli.workflow.agents.spawn import (
GENERAL_PURPOSE,
SKILL_SOURCE,
may_spawn,
spawnable,
)
from agentic_cli.workflow.sessions import get_active_run

if TYPE_CHECKING:
Expand Down Expand Up @@ -109,19 +120,25 @@ def starts(self, skill: Any) -> bool:
and SKILL_SOURCE in self._delegations().policy.spawn_sources
)

def _general(self) -> bool:
"""Whether this is a general-purpose agent's skill: it starts no agent."""
return self._agent == GENERAL_PURPOSE and self._delegations().policy.general_purpose

def _reason(self, skill: Any, name: str, depth: int) -> str | None:
"""Why this agent's run at ``depth`` may not start the type ``name`` for ``skill``."""
delegations = self._delegations()
types = delegations.types
config = types.get(name)
if config is None:
return UNKNOWN_TYPE
policy = delegations.policy
config = delegations.config_for(name, based_on=self._agent)
if config is None and not (name == GENERAL_PURPOSE and policy.general_purpose):
return UNKNOWN_TYPE
if name not in spawnable(list(types.values()), policy, SKILL_SOURCE):
return NOT_FOR_SKILLS
caller = types.get(self._agent)
if (
not may_spawn(depth, policy)
config is None # the general-purpose type, from an agent that may not start it
or self._general()
or not may_spawn(depth, policy)
or (caller is not None and caller.inherit_context)
or (depth > 0 and config.inherit_context)
):
Expand All @@ -148,6 +165,8 @@ def refusal(self, skill: Any, depth: int) -> str | None:
def check(self, skills: Sequence[Any]) -> None:
"""Warn, once per skill, of each that names an agent this agent's first run
could not start, or that the app lets no skill start (the key is ignored)."""
if self._general():
return
for skill in skills:
name = skill_agent(skill)
if name is None or (self._agent, skill.name) in self._warned:
Expand Down Expand Up @@ -187,6 +206,7 @@ async def start(
brief=Brief(title=skill.name, objective=skill.instructions),
started_by=skill_started_by(skill.name),
caller=caller, call_id=call_id, state=state, history=history, counted=True,
based_on=self._agent if skill_agent(skill) == GENERAL_PURPOSE else None,
))
if delegated.run is None:
return delegated
Expand Down
68 changes: 58 additions & 10 deletions src/agentic_cli/workflow/adk/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ class GoogleADKWorkflowManager(BaseWorkflowManager):
#: runner of each, by (type, choice): what a run on a choice runs on.
_variant_agents: "Mapping[tuple[str, str], LlmAgent]" = MappingProxyType({})
_variant_runners: "Mapping[tuple[str, str], Runner]" = MappingProxyType({})
#: The built-in general-purpose type built for each type that may start it
#: (``Delegations.general_purpose``), and the runner of each, by that type.
_general_agents: "Mapping[str, LlmAgent]" = MappingProxyType({})
_general_runners: "Mapping[str, Runner]" = MappingProxyType({})

def __init__(
self,
Expand Down Expand Up @@ -415,8 +419,8 @@ async def _agent_tool_names(self, agent: str) -> frozenset[str]:
def _detach_backend(self) -> list[Closer]:
"""The runners and the agents, and the LLM logging plugin.

The conversation's runner and each sub-agent type's, and each of a
type's model choices': a runner closes
The conversation's runner and each sub-agent type's, each of a
type's model choices' and each general-purpose agent's: a runner closes
its agent's toolsets (an MCP server stops with its toolset) and
plugins; the session service it only flushes, so one a model switch
keeps stays open. They close before the session service (the base
Expand All @@ -428,6 +432,8 @@ def _detach_backend(self) -> list[Closer]:
chosen_runners, self._variant_runners = (
self._variant_runners, MappingProxyType({})
)
general_runners, self._general_runners = self._general_runners, MappingProxyType({})
self._general_agents = MappingProxyType({})
self._variant_agents = MappingProxyType({})
self._agents = MappingProxyType({})
self._root_agent = None
Expand All @@ -454,6 +460,15 @@ def _detach_backend(self) -> list[Closer]:
)
for (name, choice), owned in chosen_runners.items()
),
*(
(
f"runner general_purpose for {spawner}",
functools.partial(
self._aclose_owned, owned, f"runner general_purpose for {spawner}"
),
)
for spawner, owned in general_runners.items()
),
]

def _detach_store(self) -> list[Closer]:
Expand Down Expand Up @@ -660,7 +675,10 @@ def _create_agents(self) -> Agent:
itself. Every agent is kept by name (``_agents``): each sub-agent
type gets a runner of its own (``_do_initialize``), and so does each
of its model choices, an agent built once more on that model
(``_variant_agents``, by (type, choice)).
(``_variant_agents``, by (type, choice)), and the built-in
general-purpose type, one for each type that may start it
(``Delegations.general_purpose``: that type's tools, toolsets of its
own and model, no delegation tools; ``_general_agents``, by that type).

Prompt factories are called under this manager's settings context (see
``AgentConfig.get_prompt``), so a prompt that reads settings sees the
Expand All @@ -682,6 +700,7 @@ def _create_agents(self) -> Agent:

agents: dict[str, LlmAgent] = {}
variants: dict[tuple[str, str], LlmAgent] = {}
general: dict[str, LlmAgent] = {}
# What this build's skill checks warned of: an agent built once more
# for each model choice has skills of its own, and warns once.
self._skill_warned: set[tuple[str, str]] = set()
Expand Down Expand Up @@ -711,9 +730,15 @@ def _create_agents(self) -> Agent:
type="coordinator" if delegates else "leaf",
sub_agents=list(config.sub_agents),
)
# The general-purpose agent starts none (``delegates`` stays False).
for spawner, config in self._delegations.general_purpose.items():
general[spawner] = self._build_agent(
config, service_map, root=False, model=self._general_model(spawner)
)

self._agents = agents
self._variant_agents = variants
self._general_agents = general
root_agent = agents[graph.root_name]
logger.info("agents_created", root=root_agent.name, total=len(agents))
return root_agent
Expand Down Expand Up @@ -1088,6 +1113,24 @@ async def _do_initialize(self) -> None:
)
for key, agent in self._variant_agents.items()
}
# And of each general-purpose agent: the chain on its spawner's model
# and numbers, under the name general_purpose.
self._general_runners = {
spawner: NoAfterTurnCompactionRunner(
app=App(
name=child_app(self.app_name),
root_agent=agent,
plugins=self._agent_plugin_chain(
agent, model=self._general_model(spawner),
budget=self._budgets.get(spawner),
),
context_cache_config=build.cache,
events_compaction_config=None,
),
session_service=self._session_service,
)
for spawner, agent in self._general_agents.items()
}
# A call's usage names its agent's own model and context numbers (the
# default model stands in for an agent without one); only the agents
# that reply write the reply, another's text is a sub-agent's work.
Expand Down Expand Up @@ -1707,7 +1750,9 @@ async def _run_agent(self, run: AgentRun) -> RunAnswer:

A run on a model choice (``run.model``) runs on the runner of its type
built on that model (``_variant_runners``), its usage and context the
choice's (``_run_models``).
choice's (``_run_models``). A run of the built-in ``general_purpose``
type (``run.based_on``) runs on the runner built for that type
(``_general_runners``), on that type's model.

In the run's own task (``agents/delegate.py``): the child session
(``child_app``, ``child_session_id``: the conversation's id and the
Expand Down Expand Up @@ -1762,8 +1807,9 @@ async def _run_agent(self, run: AgentRun) -> RunAnswer:
_, budgets = self._run_models(run)
models, kinds = dict(build.models), self._agent_kinds()
chosen = (
self._choice_models[(run.ref.agent, run.model)]
if run.model is not None else None
self._general_model(run.based_on) if run.based_on is not None
else self._choice_models[(run.ref.agent, run.model)] if run.model is not None
else None
)
if chosen is not None:
models[run.ref.agent] = str(chosen.ref)
Expand All @@ -1780,10 +1826,12 @@ async def _run_agent(self, run: AgentRun) -> RunAnswer:
)
aborted = asyncio.Event()
raised: BaseException | None = None
runner = (
self._agent_runners[run.ref.agent] if chosen is None
else self._variant_runners[(run.ref.agent, run.model)]
)
if run.based_on is not None:
runner = self._general_runners[run.based_on]
elif chosen is None:
runner = self._agent_runners[run.ref.agent]
else:
runner = self._variant_runners[(run.ref.agent, run.model)]
key = (conversation, run.ref.run or "")
if chosen is not None:
self._chosen_runs[key] = chosen
Expand Down
Loading
Loading