Back to Subagents
TodoWrite → Subagents
s03 (329 LOC) → s04 (288 LOC)
LOC Delta
-41lines
New Tools
1
task
New Classes
0
New Functions
1
runSubagent
TodoWrite
Plan Before You Act
329 LOC
5 tools: bash, read_file, write_file, edit_file, todo
planningSubagents
Clean Context Per Subtask
288 LOC
5 tools: bash, read_file, write_file, edit_file, task
planningSource Code Diff
s03 (s03_todo_write.ts) -> s04 (s04_subagent.ts)
| 1 | 1 | #!/usr/bin/env node | |
| 2 | 2 | /** | |
| 3 | - | * s03_todo_write.ts - TodoWrite | |
| 3 | + | * s04_subagent.ts - Subagents | |
| 4 | 4 | * | |
| 5 | - | * The model tracks its own progress through a TodoManager. | |
| 6 | - | * A nag reminder pushes it to keep the plan updated. | |
| 5 | + | * Spawn a child agent with fresh messages=[]. | |
| 6 | + | * The child shares the filesystem, but returns only a short summary. | |
| 7 | 7 | */ | |
| 8 | 8 | ||
| 9 | 9 | import { spawnSync } from "node:child_process"; | |
| 10 | 10 | import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; | |
| 11 | 11 | import { resolve } from "node:path"; | |
| 12 | 12 | import process from "node:process"; | |
| 13 | 13 | import { createInterface } from "node:readline/promises"; | |
| 14 | 14 | import type Anthropic from "@anthropic-ai/sdk"; | |
| 15 | 15 | import "dotenv/config"; | |
| 16 | 16 | import { buildSystemPrompt, createAnthropicClient, resolveModel, shellToolDescription } from "./shared"; | |
| 17 | 17 | ||
| 18 | - | type ToolUseName = "bash" | "read_file" | "write_file" | "edit_file" | "todo"; | |
| 18 | + | type BaseToolName = "bash" | "read_file" | "write_file" | "edit_file"; | |
| 19 | + | type ParentToolName = BaseToolName | "task"; | |
| 19 | 20 | ||
| 20 | - | type TodoStatus = "pending" | "in_progress" | "completed"; | |
| 21 | - | ||
| 22 | 21 | type ToolUseBlock = { | |
| 23 | 22 | id: string; | |
| 24 | 23 | type: "tool_use"; | |
| 25 | - | name: ToolUseName; | |
| 24 | + | name: ParentToolName; | |
| 26 | 25 | input: Record<string, unknown>; | |
| 27 | 26 | }; | |
| 28 | 27 | ||
| 29 | 28 | type TextBlock = { | |
| 30 | 29 | type: "text"; | |
| 31 | 30 | text: string; | |
| 32 | 31 | }; | |
| 33 | 32 | ||
| 34 | 33 | type ToolResultBlock = { | |
| 35 | 34 | type: "tool_result"; | |
| 36 | 35 | tool_use_id: string; | |
| 37 | 36 | content: string; | |
| 38 | 37 | }; | |
| 39 | 38 | ||
| 40 | 39 | type MessageContent = string | Array<ToolUseBlock | TextBlock | ToolResultBlock>; | |
| 41 | 40 | ||
| 42 | 41 | type Message = { | |
| 43 | 42 | role: "user" | "assistant"; | |
| 44 | 43 | content: MessageContent; | |
| 45 | 44 | }; | |
| 46 | 45 | ||
| 47 | - | type TodoItem = { | |
| 48 | - | id: string; | |
| 49 | - | text: string; | |
| 50 | - | status: TodoStatus; | |
| 51 | - | }; | |
| 52 | - | ||
| 53 | 46 | const WORKDIR = process.cwd(); | |
| 54 | 47 | const MODEL = resolveModel(); | |
| 55 | 48 | const client = createAnthropicClient(); | |
| 56 | 49 | ||
| 57 | - | const SYSTEM = buildSystemPrompt(`You are a coding agent at ${WORKDIR}. | |
| 58 | - | Use the todo tool to plan multi-step tasks. Mark in_progress before starting, completed when done. | |
| 59 | - | Prefer tools over prose.`); | |
| 50 | + | const SYSTEM = buildSystemPrompt(`You are a coding agent at ${WORKDIR}. Use the task tool to delegate exploration or subtasks.`); | |
| 51 | + | const SUBAGENT_SYSTEM = buildSystemPrompt(`You are a coding subagent at ${WORKDIR}. Complete the given task, then summarize your findings.`); | |
| 60 | 52 | ||
| 61 | - | class TodoManager { | |
| 62 | - | private items: TodoItem[] = []; | |
| 63 | - | ||
| 64 | - | update(items: unknown): string { | |
| 65 | - | if (!Array.isArray(items)) { | |
| 66 | - | throw new Error("items must be an array"); | |
| 67 | - | } | |
| 68 | - | if (items.length > 20) { | |
| 69 | - | throw new Error("Max 20 todos allowed"); | |
| 70 | - | } | |
| 71 | - | ||
| 72 | - | let inProgressCount = 0; | |
| 73 | - | const validated = items.map((item, index) => { | |
| 74 | - | const record = (item ?? {}) as Record<string, unknown>; | |
| 75 | - | const text = String(record.text ?? "").trim(); | |
| 76 | - | const status = String(record.status ?? "pending").toLowerCase() as TodoStatus; | |
| 77 | - | const id = String(record.id ?? index + 1); | |
| 78 | - | ||
| 79 | - | if (!text) throw new Error(`Item ${id}: text required`); | |
| 80 | - | if (!["pending", "in_progress", "completed"].includes(status)) { | |
| 81 | - | throw new Error(`Item ${id}: invalid status '${status}'`); | |
| 82 | - | } | |
| 83 | - | if (status === "in_progress") inProgressCount += 1; | |
| 84 | - | ||
| 85 | - | return { id, text, status }; | |
| 86 | - | }); | |
| 87 | - | ||
| 88 | - | if (inProgressCount > 1) { | |
| 89 | - | throw new Error("Only one task can be in_progress at a time"); | |
| 90 | - | } | |
| 91 | - | ||
| 92 | - | this.items = validated; | |
| 93 | - | return this.render(); | |
| 94 | - | } | |
| 95 | - | ||
| 96 | - | render(): string { | |
| 97 | - | if (this.items.length === 0) return "No todos."; | |
| 98 | - | ||
| 99 | - | const lines = this.items.map((item) => { | |
| 100 | - | const marker = { | |
| 101 | - | pending: "[ ]", | |
| 102 | - | in_progress: "[>]", | |
| 103 | - | completed: "[x]", | |
| 104 | - | }[item.status]; | |
| 105 | - | return `${marker} #${item.id}: ${item.text}`; | |
| 106 | - | }); | |
| 107 | - | ||
| 108 | - | const done = this.items.filter((item) => item.status === "completed").length; | |
| 109 | - | lines.push(`\n(${done}/${this.items.length} completed)`); | |
| 110 | - | return lines.join("\n"); | |
| 111 | - | } | |
| 112 | - | } | |
| 113 | - | ||
| 114 | - | const TODO = new TodoManager(); | |
| 115 | - | ||
| 116 | 53 | function safePath(relativePath: string): string { | |
| 117 | 54 | const filePath = resolve(WORKDIR, relativePath); | |
| 118 | 55 | const normalizedWorkdir = `${WORKDIR}${process.platform === "win32" ? "\\" : "/"}`; | |
| 119 | 56 | if (filePath !== WORKDIR && !filePath.startsWith(normalizedWorkdir)) { | |
| 120 | 57 | throw new Error(`Path escapes workspace: ${relativePath}`); | |
| 121 | 58 | } | |
| 122 | 59 | return filePath; | |
| 123 | 60 | } | |
| 124 | 61 | ||
| 125 | 62 | function runBash(command: string): string { | |
| 126 | 63 | const dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]; | |
| 127 | 64 | if (dangerous.some((item) => command.includes(item))) { | |
| 128 | 65 | return "Error: Dangerous command blocked"; | |
| 129 | 66 | } | |
| 130 | 67 | ||
| 131 | 68 | const shell = process.platform === "win32" ? "cmd.exe" : "/bin/sh"; | |
| 132 | 69 | const args = process.platform === "win32" | |
| 133 | 70 | ? ["/d", "/s", "/c", command] | |
| 134 | 71 | : ["-lc", command]; | |
| 135 | 72 | ||
| 136 | 73 | const result = spawnSync(shell, args, { | |
| 137 | 74 | cwd: WORKDIR, | |
| 138 | 75 | encoding: "utf8", | |
| 139 | 76 | timeout: 120_000, | |
| 140 | 77 | }); | |
| 141 | 78 | ||
| 142 | 79 | if (result.error?.name === "TimeoutError") { | |
| 143 | 80 | return "Error: Timeout (120s)"; | |
| 144 | 81 | } | |
| 145 | 82 | ||
| 146 | 83 | const output = `${result.stdout ?? ""}${result.stderr ?? ""}`.trim(); | |
| 147 | 84 | return output.slice(0, 50_000) || "(no output)"; | |
| 148 | 85 | } | |
| 149 | 86 | ||
| 150 | 87 | function runRead(path: string, limit?: number): string { | |
| 151 | 88 | try { | |
| 152 | 89 | let lines = readFileSync(safePath(path), "utf8").split(/\r?\n/); | |
| 153 | 90 | if (limit && limit < lines.length) { | |
| 154 | 91 | lines = lines.slice(0, limit).concat(`... (${lines.length - limit} more)`); | |
| 155 | 92 | } | |
| 156 | 93 | return lines.join("\n").slice(0, 50_000); | |
| 157 | 94 | } catch (error) { | |
| 158 | 95 | return `Error: ${error instanceof Error ? error.message : String(error)}`; | |
| 159 | 96 | } | |
| 160 | 97 | } | |
| 161 | 98 | ||
| 162 | 99 | function runWrite(path: string, content: string): string { | |
| 163 | 100 | try { | |
| 164 | 101 | const filePath = safePath(path); | |
| 165 | 102 | mkdirSync(resolve(filePath, ".."), { recursive: true }); | |
| 166 | 103 | writeFileSync(filePath, content, "utf8"); | |
| 167 | 104 | return `Wrote ${content.length} bytes`; | |
| 168 | 105 | } catch (error) { | |
| 169 | 106 | return `Error: ${error instanceof Error ? error.message : String(error)}`; | |
| 170 | 107 | } | |
| 171 | 108 | } | |
| 172 | 109 | ||
| 173 | 110 | function runEdit(path: string, oldText: string, newText: string): string { | |
| 174 | 111 | try { | |
| 175 | 112 | const filePath = safePath(path); | |
| 176 | 113 | const content = readFileSync(filePath, "utf8"); | |
| 177 | 114 | if (!content.includes(oldText)) { | |
| 178 | 115 | return `Error: Text not found in ${path}`; | |
| 179 | 116 | } | |
| 180 | 117 | writeFileSync(filePath, content.replace(oldText, newText), "utf8"); | |
| 181 | 118 | return `Edited ${path}`; | |
| 182 | 119 | } catch (error) { | |
| 183 | 120 | return `Error: ${error instanceof Error ? error.message : String(error)}`; | |
| 184 | 121 | } | |
| 185 | 122 | } | |
| 186 | 123 | ||
| 187 | - | const TOOL_HANDLERS: Record<ToolUseName, (input: Record<string, unknown>) => string> = { | |
| 124 | + | const TOOL_HANDLERS: Record<BaseToolName, (input: Record<string, unknown>) => string> = { | |
| 188 | 125 | bash: (input) => runBash(String(input.command ?? "")), | |
| 189 | 126 | read_file: (input) => runRead(String(input.path ?? ""), Number(input.limit ?? 0) || undefined), | |
| 190 | 127 | write_file: (input) => runWrite(String(input.path ?? ""), String(input.content ?? "")), | |
| 191 | 128 | edit_file: (input) => | |
| 192 | 129 | runEdit(String(input.path ?? ""), String(input.old_text ?? ""), String(input.new_text ?? "")), | |
| 193 | - | todo: (input) => TODO.update(input.items), | |
| 194 | 130 | }; | |
| 195 | 131 | ||
| 196 | - | const TOOLS = [ | |
| 132 | + | const CHILD_TOOLS = [ | |
| 197 | 133 | { | |
| 198 | 134 | name: "bash", | |
| 199 | 135 | description: shellToolDescription(), | |
| 200 | 136 | input_schema: { | |
| 201 | 137 | type: "object", | |
| 202 | - | properties: { | |
| 203 | - | command: { type: "string" }, | |
| 204 | - | }, | |
| 138 | + | properties: { command: { type: "string" } }, | |
| 205 | 139 | required: ["command"], | |
| 206 | 140 | }, | |
| 207 | 141 | }, | |
| 208 | 142 | { | |
| 209 | 143 | name: "read_file", | |
| 210 | 144 | description: "Read file contents.", | |
| 211 | 145 | input_schema: { | |
| 212 | 146 | type: "object", | |
| 213 | - | properties: { | |
| 214 | - | path: { type: "string" }, | |
| 215 | - | limit: { type: "integer" }, | |
| 216 | - | }, | |
| 147 | + | properties: { path: { type: "string" }, limit: { type: "integer" } }, | |
| 217 | 148 | required: ["path"], | |
| 218 | 149 | }, | |
| 219 | 150 | }, | |
| 220 | 151 | { | |
| 221 | 152 | name: "write_file", | |
| 222 | 153 | description: "Write content to file.", | |
| 223 | 154 | input_schema: { | |
| 224 | 155 | type: "object", | |
| 225 | - | properties: { | |
| 226 | - | path: { type: "string" }, | |
| 227 | - | content: { type: "string" }, | |
| 228 | - | }, | |
| 156 | + | properties: { path: { type: "string" }, content: { type: "string" } }, | |
| 229 | 157 | required: ["path", "content"], | |
| 230 | 158 | }, | |
| 231 | 159 | }, | |
| 232 | 160 | { | |
| 233 | 161 | name: "edit_file", | |
| 234 | 162 | description: "Replace exact text in file.", | |
| 235 | 163 | input_schema: { | |
| 236 | 164 | type: "object", | |
| 237 | 165 | properties: { | |
| 238 | 166 | path: { type: "string" }, | |
| 239 | 167 | old_text: { type: "string" }, | |
| 240 | 168 | new_text: { type: "string" }, | |
| 241 | 169 | }, | |
| 242 | 170 | required: ["path", "old_text", "new_text"], | |
| 243 | 171 | }, | |
| 244 | 172 | }, | |
| 173 | + | ]; | |
| 174 | + | ||
| 175 | + | const PARENT_TOOLS = [ | |
| 176 | + | ...CHILD_TOOLS, | |
| 245 | 177 | { | |
| 246 | - | name: "todo", | |
| 247 | - | description: "Update task list. Track progress on multi-step tasks.", | |
| 178 | + | name: "task", | |
| 179 | + | description: "Spawn a subagent with fresh context. It shares the filesystem but not conversation history.", | |
| 248 | 180 | input_schema: { | |
| 249 | 181 | type: "object", | |
| 250 | 182 | properties: { | |
| 251 | - | items: { | |
| 252 | - | type: "array", | |
| 253 | - | items: { | |
| 254 | - | type: "object", | |
| 255 | - | properties: { | |
| 256 | - | id: { type: "string" }, | |
| 257 | - | text: { type: "string" }, | |
| 258 | - | status: { | |
| 259 | - | type: "string", | |
| 260 | - | enum: ["pending", "in_progress", "completed"], | |
| 261 | - | }, | |
| 262 | - | }, | |
| 263 | - | required: ["id", "text", "status"], | |
| 264 | - | }, | |
| 265 | - | }, | |
| 183 | + | prompt: { type: "string" }, | |
| 184 | + | description: { type: "string" }, | |
| 266 | 185 | }, | |
| 267 | - | required: ["items"], | |
| 186 | + | required: ["prompt"], | |
| 268 | 187 | }, | |
| 269 | 188 | }, | |
| 270 | 189 | ]; | |
| 271 | 190 | ||
| 272 | 191 | function assistantText(content: Array<ToolUseBlock | TextBlock | ToolResultBlock>) { | |
| 273 | 192 | return content | |
| 274 | 193 | .filter((block): block is TextBlock => block.type === "text") | |
| 275 | 194 | .map((block) => block.text) | |
| 276 | 195 | .join("\n"); | |
| 277 | 196 | } | |
| 278 | 197 | ||
| 279 | - | export async function agentLoop(messages: Message[]) { | |
| 280 | - | let roundsSinceTodo = 0; | |
| 198 | + | async function runSubagent(prompt: string): Promise<string> { | |
| 199 | + | const subMessages: Message[] = [{ role: "user", content: prompt }]; | |
| 200 | + | let response: Anthropic.Messages.Message | null = null; | |
| 281 | 201 | ||
| 202 | + | for (let attempt = 0; attempt < 30; attempt += 1) { | |
| 203 | + | response = await client.messages.create({ | |
| 204 | + | model: MODEL, | |
| 205 | + | system: SUBAGENT_SYSTEM, | |
| 206 | + | messages: subMessages as Anthropic.Messages.MessageParam[], | |
| 207 | + | tools: CHILD_TOOLS as Anthropic.Messages.Tool[], | |
| 208 | + | max_tokens: 8000, | |
| 209 | + | }); | |
| 210 | + | ||
| 211 | + | subMessages.push({ | |
| 212 | + | role: "assistant", | |
| 213 | + | content: response.content as Array<ToolUseBlock | TextBlock>, | |
| 214 | + | }); | |
| 215 | + | ||
| 216 | + | if (response.stop_reason !== "tool_use") { | |
| 217 | + | break; | |
| 218 | + | } | |
| 219 | + | ||
| 220 | + | const results: ToolResultBlock[] = []; | |
| 221 | + | for (const block of response.content) { | |
| 222 | + | if (block.type !== "tool_use" || block.name === "task") continue; | |
| 223 | + | const handler = TOOL_HANDLERS[block.name as BaseToolName]; | |
| 224 | + | const output = handler | |
| 225 | + | ? handler(block.input as Record<string, unknown>) | |
| 226 | + | : `Unknown tool: ${block.name}`; | |
| 227 | + | results.push({ | |
| 228 | + | type: "tool_result", | |
| 229 | + | tool_use_id: block.id, | |
| 230 | + | content: String(output).slice(0, 50_000), | |
| 231 | + | }); | |
| 232 | + | } | |
| 233 | + | ||
| 234 | + | subMessages.push({ role: "user", content: results }); | |
| 235 | + | } | |
| 236 | + | ||
| 237 | + | if (!response) return "(no summary)"; | |
| 238 | + | const texts: string[] = []; | |
| 239 | + | for (const block of response.content) { | |
| 240 | + | if (block.type === "text") texts.push(block.text); | |
| 241 | + | } | |
| 242 | + | return texts.join("") || "(no summary)"; | |
| 243 | + | } | |
| 244 | + | ||
| 245 | + | export async function agentLoop(messages: Message[]) { | |
| 282 | 246 | while (true) { | |
| 283 | 247 | const response = await client.messages.create({ | |
| 284 | 248 | model: MODEL, | |
| 285 | 249 | system: SYSTEM, | |
| 286 | 250 | messages: messages as Anthropic.Messages.MessageParam[], | |
| 287 | - | tools: TOOLS as Anthropic.Messages.Tool[], | |
| 251 | + | tools: PARENT_TOOLS as Anthropic.Messages.Tool[], | |
| 288 | 252 | max_tokens: 8000, | |
| 289 | 253 | }); | |
| 290 | 254 | ||
| 291 | 255 | messages.push({ | |
| 292 | 256 | role: "assistant", | |
| 293 | 257 | content: response.content as Array<ToolUseBlock | TextBlock>, | |
| 294 | 258 | }); | |
| 295 | 259 | ||
| 296 | 260 | if (response.stop_reason !== "tool_use") { | |
| 297 | 261 | return; | |
| 298 | 262 | } | |
| 299 | 263 | ||
| 300 | - | const results: Array<TextBlock | ToolResultBlock> = []; | |
| 301 | - | let usedTodo = false; | |
| 302 | - | ||
| 264 | + | const results: ToolResultBlock[] = []; | |
| 303 | 265 | for (const block of response.content) { | |
| 304 | 266 | if (block.type !== "tool_use") continue; | |
| 305 | 267 | ||
| 306 | - | const handler = TOOL_HANDLERS[block.name as ToolUseName]; | |
| 307 | 268 | let output: string; | |
| 308 | - | ||
| 309 | - | try { | |
| 269 | + | if (block.name === "task") { | |
| 270 | + | const input = block.input as { description?: string; prompt?: string }; | |
| 271 | + | const description = String(input.description ?? "subtask"); | |
| 272 | + | console.log(`> task (${description}): ${String(input.prompt ?? "").slice(0, 80)}`); | |
| 273 | + | output = await runSubagent(String(input.prompt ?? "")); | |
| 274 | + | } else { | |
| 275 | + | const handler = TOOL_HANDLERS[block.name as BaseToolName]; | |
| 310 | 276 | output = handler | |
| 311 | 277 | ? handler(block.input as Record<string, unknown>) | |
| 312 | 278 | : `Unknown tool: ${block.name}`; | |
| 313 | - | } catch (error) { | |
| 314 | - | output = `Error: ${error instanceof Error ? error.message : String(error)}`; | |
| 315 | 279 | } | |
| 316 | 280 | ||
| 317 | - | console.log(`> ${block.name}: ${output.slice(0, 200)}`); | |
| 281 | + | console.log(` ${output.slice(0, 200)}`); | |
| 318 | 282 | results.push({ | |
| 319 | 283 | type: "tool_result", | |
| 320 | 284 | tool_use_id: block.id, | |
| 321 | 285 | content: output, | |
| 322 | 286 | }); | |
| 323 | - | ||
| 324 | - | if (block.name === "todo") { | |
| 325 | - | usedTodo = true; | |
| 326 | - | } | |
| 327 | 287 | } | |
| 328 | 288 | ||
| 329 | - | roundsSinceTodo = usedTodo ? 0 : roundsSinceTodo + 1; | |
| 330 | - | if (roundsSinceTodo >= 3) { | |
| 331 | - | results.unshift({ | |
| 332 | - | type: "text", | |
| 333 | - | text: "<reminder>Update your todos.</reminder>", | |
| 334 | - | }); | |
| 335 | - | } | |
| 336 | - | ||
| 337 | - | messages.push({ | |
| 338 | - | role: "user", | |
| 339 | - | content: results, | |
| 340 | - | }); | |
| 289 | + | messages.push({ role: "user", content: results }); | |
| 341 | 290 | } | |
| 342 | 291 | } | |
| 343 | 292 | ||
| 344 | 293 | async function main() { | |
| 345 | 294 | const rl = createInterface({ | |
| 346 | 295 | input: process.stdin, | |
| 347 | 296 | output: process.stdout, | |
| 348 | 297 | }); | |
| 349 | 298 | ||
| 350 | 299 | const history: Message[] = []; | |
| 351 | 300 | ||
| 352 | 301 | while (true) { | |
| 353 | 302 | let query = ""; | |
| 354 | 303 | try { | |
| 355 | - | query = await rl.question("\x1b[36ms03 >> \x1b[0m"); | |
| 304 | + | query = await rl.question("\x1b[36ms04 >> \x1b[0m"); | |
| 356 | 305 | } catch (error) { | |
| 357 | 306 | if ( | |
| 358 | 307 | error instanceof Error && | |
| 359 | 308 | (("code" in error && error.code === "ERR_USE_AFTER_CLOSE") || error.name === "AbortError") | |
| 360 | 309 | ) { | |
| 361 | 310 | break; | |
| 362 | 311 | } | |
| 363 | 312 | throw error; | |
| 364 | 313 | } | |
| 365 | 314 | if (!query.trim() || ["q", "exit"].includes(query.trim().toLowerCase())) { | |
| 366 | 315 | break; | |
| 367 | 316 | } | |
| 368 | 317 | ||
| 369 | 318 | history.push({ role: "user", content: query }); | |
| 370 | 319 | await agentLoop(history); | |
| 371 | 320 | ||
| 372 | 321 | const last = history[history.length - 1]?.content; | |
| 373 | 322 | if (Array.isArray(last)) { | |
| 374 | 323 | const text = assistantText(last); | |
| 375 | 324 | if (text) console.log(text); | |
| 376 | 325 | } | |
| 377 | 326 | console.log(); | |
| 378 | 327 | } | |
| 379 | 328 | ||
| 380 | 329 | rl.close(); | |
| 381 | 330 | } | |
| 382 | 331 | ||
| 383 | 332 | void main(); |