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
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,44 @@ the start of the entry's title.

### Added

- **A skill that starts an agent: the frontmatter key `agent`, on both
backends.** A skill's `SKILL.md` may name one of the app's agent types
at the top of its frontmatter (`agent: researcher`). Where the app
lets a skill start an agent (`"skill"` in
`SubAgentPolicy.spawn_sources`; with the default policy it does not,
and the key is ignored: the skill loads as instructions, as any skill
does, and the build warns of it, `skill_agent_ignored`, since other
tools give skills an `agent` key of their own), when an agent loads it
(`load_skill`), a run of that type starts instead of the skill's
instructions being handed back: its task message is the skill's body
under its name (`# Task: <skill>`, `## Objective`), its parent the
loading agent's run, `started_by: skill:<skill>`, counted toward
`max_runs_per_turn` as a `task` call's run is, and the call's result
is the run's envelope with `"skill": "<skill>"` added, shown as a
delegation. The skill gives no tools and no system prompt: the run has
its type's own. The app allows it as for any type started at run time:
the type's `spawnable_by` holds `"skill"` too, and the loading run is
one that may start a type (`max_spawn_depth`: by default the main
agent alone; an inheriting agent starts none, and an inheriting type
is started by the main agent alone). Otherwise the load starts no run
and its result says why: `{"success": false, "error": "The skill
'research' starts the agent 'helper', which this app does not let a
skill start."}` (or `is not one of this app's agent types`, or `cannot
be started here: sub-agents cannot start sub-agents`; or `needs the
skill's instructions as its task, and this skill has none`), and a
skill whose agent its agent's first run could not start is warned of
as the agent is built (`skill_agent_not_startable`); the app still
starts. A skill without the key loads as before.
`agentic_cli.tools.skills` (provisional): `SKILL_AGENT_KEY`,
`skill_agent`, `skill_started_by`, `SkillAgents`;
`make_skill_toolset(..., agents=)` and `make_skill_tools(...,
agents=)` take an agent's `SkillAgents` (without it every skill loads
as ADK loads it), and the manager's delegations' `types` lists every
agent type. On ADK the framework's `load_skill` (`SkillLoadTool`, with
ADK's declaration) takes the place of ADK's in an agent's toolset only
when one of its skills names an agent type and the app lets a skill
start one.

- **`AgentConfig.model_choices`: the models a delegation may run a type on
instead of its own, on both backends.** References, bare model ids or
roles (`model_choices=("fast",)`; the agent YAML's `model_choices:
Expand Down
14 changes: 8 additions & 6 deletions CLAUDE.md

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ configs = [coordinator, researcher, analyst]
| `model` | str \| None | A model reference (`anthropic:claude-sonnet-5-5`), a bare model id, or a role (`fast`); without one, the `default` role (see [Models and providers](#models-and-providers)) |
| `model_settings` | ModelSettings \| None | The agent's thinking, `max_tokens`, sampling settings and `extra` (see [Thinking](#thinking)) |
| `mcp_servers` | list[MCPServerConfig] | MCP servers whose tools the agent gets (ADK and Deep Agents) |
| `skills` | list[str] | Agent Skills: a skill folder's absolute path, or a name under `skills_dirs` (ADK and Deep Agents; never looked up in the working directory) |
| `skills` | list[str] | Agent Skills: a skill folder's absolute path, or a name under `skills_dirs` (ADK and Deep Agents; never looked up in the working directory). A skill whose frontmatter names an agent type (`agent: researcher`) starts a run of it when loaded, where the app lets a skill start an agent (otherwise the key is ignored); see [Sub-agent context](#sub-agent-context) |
| `inherit_context` | bool | What a sub-agent sees when its parent delegates to it: its task alone (`False`, the default) or its parent's conversation too (`True`); see [Sub-agent context](#sub-agent-context) (ADK and Deep Agents) |
| `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 |
Expand Down Expand Up @@ -1477,6 +1477,40 @@ reviewer = AgentConfig(name="reviewer", prompt="Check what was said.",
# The coordinator's call: {"agent_type": "researcher", "title": "Look it up",
# "objective": "...", "tools": ["web_search"], "model": "fast"}
```
- **Skills that start an agent.** A skill may name an agent type at the
top of its `SKILL.md` frontmatter, `agent: researcher`; with the type's
`spawnable_by={"skill"}` and `SubAgentPolicy(spawn_sources={"skill"})`,
an agent that loads the skill starts a run of that type instead of
reading the skill's instructions. Without `"skill"` in `spawn_sources`
(the default) the key is ignored: the skill loads as instructions, as
any skill does, and the build warns of it (`skill_agent_ignored`). The
run's task message is the skill's body under its name (`# Task:
research`, `## Objective`), its parent the loading agent's run
(`started_by: skill:research`), and it counts toward
`max_runs_per_turn`; the `load_skill` call's result is the run's
envelope with `"skill": "research"` added. The skill gives no tools and
no system prompt: the run has its type's. A load is refused, no run
started, when the type is not one of the app's, when the type does not
let a skill start it, for a skill with no instructions (nothing to be
the task), or from a run that may start no type (`max_spawn_depth`; an
inheriting agent starts none, and an inheriting type is started by the
main agent alone): `{"success": false, "error": "The skill 'research'
starts the agent 'researcher', which this app does not let a skill
start."}`. Such a skill is also warned of as its agent is built
(`skill_agent_not_startable`), and the app still starts.

```markdown
---
name: todo-audit
description: List the project's open TODO comments.
agent: auditor
---
Find every TODO comment under src/, group them by file, and report them
as a list with each one's line number.
```

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 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: 7 additions & 2 deletions src/agentic_cli/tools/_core/delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@
STOP_AGENT_TOOL = "stop_agent"
CHECK_AGENTS_TOOL = "check_agents"
FOLLOW_UP_TOOLS = (MESSAGE_AGENT_TOOL, STOP_AGENT_TOOL, CHECK_AGENTS_TOOL)
#: The skill tool whose call starts a run when its skill names an agent type
#: (``tools/skills/agents.py``); registered with the other skill tools
#: (``tools/skills/permissions.py``).
LOAD_SKILL_TOOL = "load_skill"
#: The tools whose result is a run's envelope, which the event processors
#: show as a delegation (``agents/runs.py::show_as_delegation``).
ENVELOPE_TOOLS = (TASK_TOOL, MESSAGE_AGENT_TOOL)
#: show as a delegation (``agents/runs.py::show_as_delegation``). A
#: ``load_skill`` result that is no envelope is shown as any result is.
ENVELOPE_TOOLS = (TASK_TOOL, MESSAGE_AGENT_TOOL, LOAD_SKILL_TOOL)
FOLLOW_UP_CLASH_ERROR = (
"Agent {agent!r} continues its sub-agents through tools named message_agent, "
"stop_agent and check_agents, so its own tools cannot include {tool!r}; rename that tool."
Expand Down
19 changes: 18 additions & 1 deletion src/agentic_cli/tools/skills/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@
Importing this package registers the skill tool names with the permission
registry, so both backends bind their skill tools to the same definitions
(the ADK PermissionPlugin and the Deep Agents gate both allow them).

A skill whose frontmatter names an agent type starts a run of it when loaded
(``agents``).
"""

from typing import Any

from agentic_cli.tools.skills.agents import (
SKILL_AGENT_KEY,
SkillAgents,
skill_agent,
skill_started_by,
)
from agentic_cli.tools.skills.permissions import register_skill_tool_permissions
from agentic_cli.tools.skills.store import SkillStore
from agentic_cli.tools.skills.toolset import make_skill_toolset
Expand All @@ -21,7 +30,15 @@
register_skill_tool_permissions()

# make_skill_tools is left out: a star import would load LangChain.
__all__ = ["SkillStore", "make_skill_toolset", "register_skill_tool_permissions"]
__all__ = [
"SKILL_AGENT_KEY",
"SkillAgents",
"SkillStore",
"make_skill_toolset",
"register_skill_tool_permissions",
"skill_agent",
"skill_started_by",
]


def __getattr__(name: str) -> Any:
Expand Down
196 changes: 196 additions & 0 deletions src/agentic_cli/tools/skills/agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
"""A skill that starts an agent.

A skill's ``SKILL.md`` may name one of the app's agent types at the top of its
frontmatter (``agent: researcher``). When an agent loads such a skill, a run of
that type starts, instead of the skill's instructions being handed back: its
task message is the skill's body under its name, its parent the loading
agent's run, ``started_by`` ``skill:<name>``, and the load's result the run's
envelope with the skill's name added.

The key takes effect only where the app lets a skill start an agent
(``"skill"`` in ``SubAgentPolicy.spawn_sources``); otherwise it is ignored, the
skill loads as instructions, as any skill does, and the build warns of it
(``skill_agent_ignored``: other tools, Claude Code's among them, give skills
an ``agent`` key of their own).

A load starts a run only where any run-time start is allowed: the type is one
of the app's, the type's ``spawnable_by`` and the policy's ``spawn_sources``
both hold ``"skill"``, and the loading run may start a type at its depth
(``max_spawn_depth``). Otherwise the load is refused and starts nothing, and a
skill whose agent its agent's first run could not start is warned of as the
agent is built: the app still starts, since a skill file can come from a
user's folder.

``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.
"""

from __future__ import annotations

from collections.abc import Callable, Mapping, Sequence
from typing import TYPE_CHECKING, Any

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.sessions import get_active_run

if TYPE_CHECKING:
from agentic_cli.workflow.agents.delegate import Delegations

logger = Loggers.tools()

#: The frontmatter key (top level) that names the agent type a skill starts.
SKILL_AGENT_KEY = "agent"

#: What a refused load says; ``reason`` is one of the four below.
SKILL_AGENT_ERROR = "The skill '{skill}' starts the agent '{agent}', which {reason}."
UNKNOWN_TYPE = "is not one of this app's agent types"
NOT_FOR_SKILLS = "this app does not let a skill start"
NOT_HERE = "cannot be started here: sub-agents cannot start sub-agents"
NO_TASK = "needs the skill's instructions as its task, and this skill has none"
#: The build's warning for a key that is ignored, its text.
IGNORED = (
"The skill '{skill}' names the agent '{agent}', but this app lets no skill "
"start an agent: it loads as instructions."
)


def skill_started_by(name: str) -> str:
"""``started_by`` of a run the skill ``name`` starts."""
return f"skill:{name}"


def skill_agent(skill: Any) -> str | None:
"""The agent type ``skill`` names, or None.

A value that is not text is taken as written, so it names no type and its
load is refused, never silently loaded as instructions.
"""
value = getattr(skill.frontmatter, SKILL_AGENT_KEY, None)
if value is None:
return None
return value if isinstance(value, str) else str(value)


class SkillAgents:
"""What one agent's skills may start, and the start.

Args:
agent: The loading agent's type name.
delegations: The manager's ``Delegations``, read when asked (it is
replaced with the workflow).
root: Whether the agent is the root (its first run is at depth 0,
any other agent's at 1).
warned: The (agent, skill) pairs already warned of, shared by the
builds of one agent (a type and its model choices): each is
logged once. None: this object's own.
"""

def __init__(
self,
agent: str,
delegations: Callable[[], Delegations],
*,
root: bool,
warned: set[tuple[str, str]] | None = None,
) -> None:
self._agent = agent
self._delegations = delegations
self._root = root
self._warned = warned if warned is not None else set()

def starts(self, skill: Any) -> bool:
"""Whether loading ``skill`` is a start: it names an agent type and the app
lets a skill start one (``"skill"`` in ``spawn_sources``)."""
return (
skill_agent(skill) is not None
and SKILL_SOURCE in self._delegations().policy.spawn_sources
)

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
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)
or (caller is not None and caller.inherit_context)
or (depth > 0 and config.inherit_context)
):
return NOT_HERE
if not skill.instructions.strip():
return NO_TASK
return None

def refusal(self, skill: Any, depth: int) -> str | None:
"""The error a start of ``skill`` from a run at ``depth`` gets, or None to start it.

A skill that names no agent is no start (None). One whose key the app
ignores (``starts`` is false) is refused too: a load treats it as
instructions before asking, and a start that comes anyway starts nothing.
"""
name = skill_agent(skill)
if name is None:
return None
reason = self._reason(skill, name, depth) if self.starts(skill) else NOT_FOR_SKILLS
if reason is None:
return None
return SKILL_AGENT_ERROR.format(skill=skill.name, agent=name, reason=reason)

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)."""
for skill in skills:
name = skill_agent(skill)
if name is None or (self._agent, skill.name) in self._warned:
continue
if not self.starts(skill):
self._warned.add((self._agent, skill.name))
logger.warning(
"skill_agent_ignored",
agent=self._agent, skill=skill.name, agent_type=name,
message=IGNORED.format(skill=skill.name, agent=name),
)
continue
reason = self._reason(skill, name, 0 if self._root else 1)
if reason is None:
continue
self._warned.add((self._agent, skill.name))
logger.warning(
"skill_agent_not_startable",
agent=self._agent, skill=skill.name, agent_type=name, reason=reason,
)

async def start(
self,
skill: Any,
*,
call_id: str | None,
state: Mapping[str, Any],
history: Callable[[], Any] | None,
) -> Delegated:
"""Load ``skill``, which ``starts``: start its agent's run, or refuse and start none."""
caller = get_active_run()
refused = self.refusal(skill, caller.depth if caller is not None else 0)
if refused is not None:
return Delegated(result={"success": False, "error": refused}, state={}, run=None)
delegated = await self._delegations().start(StartRequest(
agent_type=skill_agent(skill) or "",
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,
))
if delegated.run is None:
return delegated
return Delegated(
result={**delegated.result, "skill": skill.name},
state=delegated.state, run=delegated.run,
)
Loading
Loading