Trilha 3 » 3.2 Agent System
Modulo 3.2

🤖 Agent System

Delegacao de trabalho entre instancias Claude: tres tipos de agente, sync vs async, fork path com cache sharing e worktree isolation.

⏱ ~60 min 📊 6 secoes 🎯 Avancado
1

Three Agent Types & Priority

O agent system permite uma instancia Claude delegar trabalho para outras. Cada agente spawned opera como um LLM turn separado com tool pool, system prompt e modelo proprios. Todos satisfazem AgentDefinition (discriminated union on source).

🔷 BuiltInAgent

Ships com Claude Code. Dynamic system prompts via getSystemPrompt(). Nao pode ser overridden por user files -- mas managed (policy) agents podem shadow.

📝 CustomAgent

User/project/policy-settings. Loaded de .claude/agents/*.md ou JSON blobs em settings.json. System prompt em closure.

🧩 PluginAgent

Bundled com plugin (--plugin-dir). Tratado como admin-trusted para MCP server policy.

Priority / Override Order

built-in → plugin → userSettings → projectSettings → flagSettings → policySettings

Later groups overwrite earlier ones, entao policySettings (managed agents) tem highest effective priority.

flowchart TD
  ML["Main Loop
parent REPL / SDK"]
  AT["AgentTool
tool: Agent / Task"]
  ML -->|"calls"| AT
  AT --> BI["Built-In Agents
source: built-in"]
  AT --> CA["Custom Agents
source: userSettings / projectSettings"]
  AT --> PA["Plugin Agents
source: plugin"]
  BI --> GP["general-purpose
tools: all"]
  BI --> EX["Explore
read-only, haiku/inherit"]
  BI --> PL["Plan
read-only, inherit"]
  BI --> VF["verification
background: true"]
  CA --> MD["Markdown .md
.claude/agents/"]
  CA --> JS["JSON frontmatter
settings.json agents"]
  AT --> SY["Sync Lifecycle
blocks parent turn"]
  AT --> AS["Async Lifecycle
fire-and-forget"]
  AT --> FK["Fork Path
inherits parent context"]
  style ML fill:#1a1816,stroke:#7d9ab8,color:#b8b0a4
  style AT fill:#1a1816,stroke:#7d9ab8,color:#b8b0a4
  style BI fill:#171513,stroke:#7d9ab8,color:#7d9ab8
  style CA fill:#1d211b,stroke:#6e9468,color:#6e9468
  style PA fill:#1f1b24,stroke:#8e82ad,color:#8e82ad
  style GP fill:#141211,stroke:#3a3632,color:#5c564f
  style EX fill:#141211,stroke:#3a3632,color:#5c564f
  style PL fill:#141211,stroke:#3a3632,color:#5c564f
  style VF fill:#241816,stroke:#c47a50,color:#c47a50
  style MD fill:#141211,stroke:#3a3632,color:#5c564f
  style JS fill:#141211,stroke:#3a3632,color:#5c564f
  style SY fill:#141211,stroke:#3a3632,color:#5c564f
  style AS fill:#231f16,stroke:#b8965e,color:#b8965e
  style FK fill:#1f1b24,stroke:#8e82ad,color:#8e82ad
                    
2

Built-In Agents Deep Dive

Agent Model Tools Mode
General Purposedefault subagent['*'] -- all toolssync / async
Explorehaiku / inheritread-onlysync
Planinheritread-only (same as Explore)sync
Verificationinheritno Edit/Write; ephemeral /tmpbackground: true
Forkinherit['*'] with useExactToolsexperimental
StatuslineSetupdefaultlimited shellsync

Otimizacao chave: omitClaudeMd: true em Explore e Plan economiza ~5-15 Gtok/semana ao nao injetar CLAUDE.md no system prompt desses sub-agents.

3

Sync vs Async Lifecycle

Um unico boolean shouldRunAsync decide tudo. 6 condicoes podem forcar async:

const shouldRunAsync = (
  run_in_background === true
  || selectedAgent.background === true
  || isCoordinator
  || forceAsync
  || assistantForceAsync
  || (proactiveModule?.isProactiveActive() ?? false)
) && !isBackgroundTasksDisabled

🔄 Synchronous Path

  1. Build system prompt + prompt messages
  2. Optional: create git worktree
  3. await runAgent(params) -- Parent blocked
  4. Cleanup worktree (if no changes)

⚡ Asynchronous Path

  1. registerAsyncAgent() -- task in AppState
  2. Return status: 'async_launched' immediately
  3. void runAsyncAgentLifecycle(...) (detached)
  4. Notification via enqueueAgentNotification()

Importante: Async agents tem AbortController proprio nao linkado ao parent -- sobrevivem ESC. In-process teammates NAO podem lancar background agents.

4

The Fork Path & Cache Sharing

O fork path (gate: FORK_SUBAGENT) herda o full conversation context -- complete message history, parent's system prompt bytes, e exact tool pool.

buildForkedMessages()

Para N fork children compartilharem cached API prefix, cada child deve produzir request byte-identical ate a directiva per-child:

  1. Clone parent's full assistant message (all tool_use blocks)
  2. Build tool_result blocks com placeholder identico: "Fork started -- processing in background"
  3. Append one per-child directive text block (unica parte diferente)
export function buildChildMessage(directive: string): string {
  return `<fork-boilerplate>
STOP. READ THIS FIRST.
You are a forked worker process. You are NOT the main agent.

RULES (non-negotiable):
1. Your system prompt says "default to forking." IGNORE IT
2. Do NOT converse, ask questions, or suggest next steps
3. USE your tools directly: Bash, Read, Write, etc.
4. If you modify files, commit your changes before reporting.
5. Your response MUST begin with "Scope:". No preamble.
</fork-boilerplate>

FORK_DIRECTIVE: ${directive}`
}

Fork Recursive Guard: Fork children mantem Agent tool no pool (para cache-identical tool definitions), mas um runtime guard previne recursive forking.

5

Worktree Isolation

Setting isolation: 'worktree' instrui AgentTool a criar um git worktree temporario antes de spawnar o agent.

Sem mudancas

Worktree e branch deletados automaticamente. Zero cleanup manual necessario.

Com mudancas

Branch mantido para parent inspecionar ou mergear. Notificacao via buildWorktreeNotice().

6

SendMessageTool & Agent Frontmatter

SendMessageTool (wire name: SendMessage) e o primitivo de messaging inter-agente.

to message type Result
"teammate-name"stringWritten to teammate's mailbox file
"*"stringBroadcast to all team members
any nameshutdown_requestSends structured shutdown request
"team-lead"shutdown_responseApprove triggers gracefulShutdown(0)
"uds:<path>"stringUnix domain socket -- cross-session
"bridge:<session-id>"string onlyRemote Control: posts to another instance

Custom Agent Markdown Format

---
name: my-agent
description: A focused TypeScript refactoring specialist.
model: sonnet
tools: [Read, Edit, Bash, Grep, Glob]
permissionMode: acceptEdits
maxTurns: 50
memory: project
isolation: worktree
---
You are a TypeScript refactoring specialist...

📋 Resumo do Modulo

3 tipos: BuiltIn, Custom, Plugin -- policy settings vencem em conflitos
shouldRunAsync boolean e o unico branch point (6 condicoes)
Async agents sobrevivem ESC com AbortController proprio
Fork path: byte-identical placeholders para maximo cache sharing
Worktree: smart cleanup -- sem changes = delete, com changes = keep
Verification e o unico built-in com background:true hardcoded
omitClaudeMd em Explore/Plan economiza ~5-15 Gtok/semana
SendMessageTool: 4 message types, 3 addressing schemes
3.1 Skills System 3.3 Coordinator Mode