# Subagents

The Bub app includes a built-in `subagent` tool that delegates a subtask to a background child run.

Subagents are ideal for tasks that produce large amounts of intermediate output — **broad codebase searches, reading long documents, or independent code reviews**. Intermediate tool calls stay isolated inside the subagent's session, and only the final conclusion from its last step returns to the parent session.

## Delegate a task {#delegate}

In `bub chat`, you can ask the model to delegate directly to a subagent:

```text
Use the subagent tool to review the authentication flow:
1. Use a temporary session and allow only fs.read and bash;
2. Do not modify files;
3. Return the review findings with the relevant file paths.
```

When the parent calls `subagent`, the child starts in the background and immediately returns a `task_id`. The child runs its own tool loop and delivers only its final answer back to the parent.

Because a subagent sees only its `prompt` by default (plus prior conversation history when `session="fork"`), the delegated `prompt` should be self-contained: state the goal, expected return format, and constraints.

You can also add delegation rules to `AGENTS.md` or a skill (e.g., "delegate broad searches or code reviews to a read-only subagent first") so the parent delegates automatically.

## Session modes {#sessions}

The `session` parameter controls the subagent's initial context and whether its history is kept afterwards:

| `session` value | Behavior | Best for |
| --- | --- | --- |
| `temp` (default) | Starts a blank `temp/<task_id>` session; discarded when finished | **Most common.** One-off research, retrieval, or reviews where only the final answer matters |
| `fork` (alias `inherit`) | Copies the current conversation snapshot at the moment of the call; discarded when finished | Subtasks that depend on recent context without the parent re-explaining it |
| Custom session ID string | Uses that fixed session ID and keeps its history | Multi-round tasks you want to continue later via `subagent.send` or inspect afterwards |

`fork` copies the conversation snapshot at the time of the call: the child does not see later parent actions, and the child's entries are not merged back into the parent's Tape.

## Receive results and manage tasks {#background}

Subagents always run asynchronously in the background. The parent can start multiple subagents in parallel while continuing its own work:

```text
Start three subagents, one per service, to check the logs for errors.
Keep working on the migration; summarize their findings when they are done.
```

When a subagent finishes, its result arrives as a notification message before the parent's next Step:

```text
<subagent-notification task_id="sub-1a2b3c4d" status="completed">
No errors in the payments service logs since the last deploy.
</subagent-notification>
```

A Turn does not finish while subagents it started are still running: when the parent has no other tool calls to make, it automatically waits for the next subagent to finish and continues reasoning with the result. If the parent Turn ends early due to an error, cancellation, or `max_steps`, remaining subagents deliver their results on the session's next Turn.

The parent can also manage background tasks explicitly:

| Tool | Purpose |
| --- | --- |
| `subagent.wait` | Wait for and return subagent results up to `timeout_seconds` (default 60; `0` checks status without blocking). Defaults to all unread tasks in the session. Tasks read via `subagent.wait` do not send a duplicate notification |
| `subagent.send` | Send a follow-up instruction to a running task (read before its next Step), or resume a finished task that used a custom named session |
| `subagent.cancel` | Cancel a running subagent task |

At most `BUB_SUBAGENT_MAX_CONCURRENCY` subagents (default 4) run concurrently in one process; extra tasks queue. Limit runtime per call with `timeout_seconds` or globally with `BUB_SUBAGENT_TIMEOUT_SECONDS`. Exiting Bub or running `,quit` cancels all active subagents.

## Define subagent profiles {#profiles}

If you regularly delegate to recurring roles (such as a code reviewer or log analyzer), save their model, allowed tools, and system prompt as a reusable **profile**. Just create a Markdown file in `.agents/agents/` in your workspace (or `~/.agents/agents/` globally) — YAML frontmatter sets default parameters, and the Markdown body is appended to the subagent's system prompt:

```markdown title=".agents/agents/reviewer.md"
---
description: Reviews code changes for bugs and reports findings without editing files
model: openai:gpt-5-mini
tools: [fs.read, bash, tape.search]
skills: []
timeout_seconds: 600
---

You review code. Read the relevant files, report concrete findings with file
paths and line numbers, and never modify files.
```

| Field | Meaning |
| --- | --- |
| `description` | **Required**. Tells the parent when to select this profile |
| `name` | Optional; defaults to the filename without `.md` (e.g. `reviewer`) |
| `model`, `reasoning_effort` | Default model and reasoning effort for the subagent |
| `tools`, `skills` | Allowed tools and skills (YAML list or comma-separated string) |
| `timeout_seconds` | Timeout in seconds |

Discovered profiles are listed in the parent's system prompt automatically. The parent activates one by passing `agent="reviewer"` to `subagent`; explicit call arguments override profile defaults. Workspace profiles override global profiles of the same name.

## State sharing and boundaries {#boundaries}

A subagent runs on the same `BubAgent` instance as its parent with the following boundaries:

| Resource | Boundary | Notes |
| --- | --- | --- |
| **Agent instance** | Shared | Shares plugin hooks, model credentials, and `BubAgent` settings |
| **Execution environment** | Shared | Shares workspace directory, local filesystem, Shell processes, and network |
| **Model** | Configurable | Defaults to profile or parent model; override via `model` |
| **Tools and skills** | Subset of parent | Defaults to the parent run's allowed tools and skills; limit further with `allowed_tools` and `allowed_skills`. Subagents cannot call `subagent` |
| **TurnState** | Shallow copy | Copies the parent's `TurnState` with an isolated steering inbox key so parent steering messages stay with the parent |
| **Session history** | Per `session` mode | `temp` and `fork` are discarded on exit; custom named sessions are kept |

See the [`subagent` tool reference](/docs/reference/tools/#subagent) for parameter details and [subagent events](/docs/reference/tools/#subagent-events) for lifecycle events.

### Security limits {#security-limits}

Like the parent, a subagent runs in your local environment with your user permissions. Keep these boundaries in mind:

1. **No filesystem isolation**: `session` isolates only **conversation history and model context**. File edits, shell commands, and network requests made by a subagent affect the local system directly and are not rolled back on exit.
2. **`allowed_tools` is not a security boundary**: in chat, the model generates `subagent` arguments, so `allowed_tools` only guides model behavior. To enforce hard blocks on dangerous commands, use [tool interception hooks](/docs/app/plugins/#control-a-model-or-tool-call); for OS-level isolation, configure a separate [execution environment](/docs/app/tools/#environment).