mirror of
https://github.com/shareAI-lab/analysis_claude_code.git
synced 2026-03-22 02:15:42 +08:00
Comprehensive rewrite establishing the harness engineering narrative across the entire repository. README (EN/ZH/JA): added "The Model IS the Agent" manifesto with historical proof (DQN, OpenAI Five, AlphaStar, Tencent Jueyu), "What an Agent Is NOT" critique, harness engineer role definition, "Why Claude Code" as masterclass in harness design, and universe vision. Consistent framing: model = driver, harness = vehicle. docs (36 files, 3 languages): injected one-line "Harness layer" callout after the motto in every session document (s01-s12). agents (13 Python files): added harness framing comment before each module docstring. skills/agent-philosophy.md: full rewrite aligned with harness narrative.
95 lines
3.4 KiB
Markdown
95 lines
3.4 KiB
Markdown
# s04: Subagents
|
|
|
|
`s01 > s02 > s03 > [ s04 ] s05 > s06 | s07 > s08 > s09 > s10 > s11 > s12`
|
|
|
|
> *"Break big tasks down; each subtask gets a clean context"* -- subagents use independent messages[], keeping the main conversation clean.
|
|
>
|
|
> **Harness layer**: Context isolation -- protecting the model's clarity of thought.
|
|
|
|
## Problem
|
|
|
|
As the agent works, its messages array grows. Every file read, every bash output stays in context permanently. "What testing framework does this project use?" might require reading 5 files, but the parent only needs the answer: "pytest."
|
|
|
|
## Solution
|
|
|
|
```
|
|
Parent agent Subagent
|
|
+------------------+ +------------------+
|
|
| messages=[...] | | messages=[] | <-- fresh
|
|
| | dispatch | |
|
|
| tool: task | ----------> | while tool_use: |
|
|
| prompt="..." | | call tools |
|
|
| | summary | append results |
|
|
| result = "..." | <---------- | return last text |
|
|
+------------------+ +------------------+
|
|
|
|
Parent context stays clean. Subagent context is discarded.
|
|
```
|
|
|
|
## How It Works
|
|
|
|
1. The parent gets a `task` tool. The child gets all base tools except `task` (no recursive spawning).
|
|
|
|
```python
|
|
PARENT_TOOLS = CHILD_TOOLS + [
|
|
{"name": "task",
|
|
"description": "Spawn a subagent with fresh context.",
|
|
"input_schema": {
|
|
"type": "object",
|
|
"properties": {"prompt": {"type": "string"}},
|
|
"required": ["prompt"],
|
|
}},
|
|
]
|
|
```
|
|
|
|
2. The subagent starts with `messages=[]` and runs its own loop. Only the final text returns to the parent.
|
|
|
|
```python
|
|
def run_subagent(prompt: str) -> str:
|
|
sub_messages = [{"role": "user", "content": prompt}]
|
|
for _ in range(30): # safety limit
|
|
response = client.messages.create(
|
|
model=MODEL, system=SUBAGENT_SYSTEM,
|
|
messages=sub_messages,
|
|
tools=CHILD_TOOLS, max_tokens=8000,
|
|
)
|
|
sub_messages.append({"role": "assistant",
|
|
"content": response.content})
|
|
if response.stop_reason != "tool_use":
|
|
break
|
|
results = []
|
|
for block in response.content:
|
|
if block.type == "tool_use":
|
|
handler = TOOL_HANDLERS.get(block.name)
|
|
output = handler(**block.input)
|
|
results.append({"type": "tool_result",
|
|
"tool_use_id": block.id,
|
|
"content": str(output)[:50000]})
|
|
sub_messages.append({"role": "user", "content": results})
|
|
return "".join(
|
|
b.text for b in response.content if hasattr(b, "text")
|
|
) or "(no summary)"
|
|
```
|
|
|
|
The child's entire message history (possibly 30+ tool calls) is discarded. The parent receives a one-paragraph summary as a normal `tool_result`.
|
|
|
|
## What Changed From s03
|
|
|
|
| Component | Before (s03) | After (s04) |
|
|
|----------------|------------------|---------------------------|
|
|
| Tools | 5 | 5 (base) + task (parent) |
|
|
| Context | Single shared | Parent + child isolation |
|
|
| Subagent | None | `run_subagent()` function |
|
|
| Return value | N/A | Summary text only |
|
|
|
|
## Try It
|
|
|
|
```sh
|
|
cd learn-claude-code
|
|
python agents/s04_subagent.py
|
|
```
|
|
|
|
1. `Use a subtask to find what testing framework this project uses`
|
|
2. `Delegate: read all .py files and summarize what each one does`
|
|
3. `Use a task to create a new module, then verify it from here`
|