主循环所有会话/子 agent 共用restored-src/src/query.tsquery() / 内部的 queryLoop一轮轮调模型、解析 tool use、执行工具、把结果写回消息、直到结束。 这是整个产品里 “agent 行为”的公共引擎。* 代码文件核心就是定义了一个queryLoop子Agent类型子 agent 类型分三块内置代码写死、自定义你自己加、以及不能当subagent_type选的隐式 fork。按能力划分只读型Explore、Plan、verification全能力general-purpose、worker、自定义默认专用型statusline-setup、claude-code-guide特殊fork隐式explore 子agent:Explore 没有单独的「Explore 引擎」核心是一份类型定义 若干针对Explore的特殊优化实际执行走通用的 Subagent 管线。执行流程主 agent: Agent({ subagent_type: Explore, prompt })↓AgentTool.tsx → 查找 EXPLORE_AGENT 定义↓runAgent.ts → 组装 prompt/工具/上下文 → query() 循环↓Glob / Grep / Read / Bash → 通用工具执行搜索↓AgentTool.tsx → 返回报告one-shot无 resume 提示对话流程用户输入↓主 agentquery 循环↓主 agent 自己决定├── 直接用 Read / Grep / Bash … 处理└── 调用 Agent({ subagent_type: Explore, prompt: ... }) → 这时才创建子 agent没有「用户一说话就后台 spawn Explore」这类机制。子 agent 的创建是一次 tool call和调用Read一样由模型在当轮推理里决定是否发起。对话框里的需求先进主 agent子 agent 是主 agent 认为需要委派时通过Agent工具显式创建的不是用户发消息就自动触发。何时委派用户消息↓query 主循环src/query.ts↓模型读 system prompt Agent 工具描述↓模型决定直接 Read/Grep还是 tool_call → Agent(...)↓若调了 Agent → AgentTool.tsx → runAgent.ts 才真正创建子 agent决策点 模型的 tool choice不在某段 TypeScript 的if/else里。AgentTool.tsx的call()只做权限校验、类型解析、spawn 执行不负责「该不该委派」。总结Explore 的业务逻辑主要在exploreAgent.tsprompt 工具/模型配置执行在runAgent.ts的通用子 agent 循环里runAgent.ts和agentToolUtils.ts里有少量 Explore/Plan 专属的上下文与工具过滤优化。若要改 Explore 行为通常从exploreAgent.ts的getExploreSystemPrompt()和disallowedTools入手。子Agent与Tool的区别执行模型区别普通 Tool子 AgentAgent工具本质单次能力调用派生一个会自己多轮推理、多工具协作的小 agent例子Read、Grep、Bash、EditAgent({ subagent_type: Explore, ... })普通 Tool:主 agent → tool_call(Read) → 读文件 → 返回结果 → 结束子 Agent:主 agent → tool_call(Agent) → runAgent() → query() 循环→ 子 agent 自己调 Read/Grep/Bash…多轮→ 汇总报告 → 返回主 agent-----------------------------------------------------------------------------------------普通 tool一次输入、一次输出。子 agent独立 mini 对话内部可反复调很多 tool。是否有自己的「大脑」普通 Tool子 AgentLLM 推理无纯代码执行有完整query()循环System prompt无有如 Explore 的只读 specialist prompt自主决策无自己决定下一步用哪个 toolRead不会「思考」Explore 会先 Glob 再 Grep 再 Read路径由子 agent 自己定。上下文普通 Tool子 Agent对话历史不参与有独立消息链fork 除外会继承主上下文可见用户对话仅本次参数默认看不到需在prompt里 briefing上下文成本一次 tool result子 agent 多轮 tool 输出主 agent 通常只收最终摘要工具集普通 Tool子 Agent能力固定单一能力可调多种 tool限制无按类型限制如 Explore 禁 Write/Edit子 agent 是「带工具包的 agent」不是单一能力点类型与配置普通 Tool子 Agent定义位置src/tools/*Tool/built-in/*.ts、.claude/agents/*.md可配置性固定subagent_type、model、background、worktree 等实例无状态每次 spawn 新agentId调用方式对主 agent两者对主 agent 都是 tool call但语义不同// 普通 tool — 原子操作 Read({ file_path: src/foo.ts }) // 子 agent — 委派任务 Agent({ subagent_type: Explore, description: 搜认证模块, prompt: 在 src/auth/ 找 session 处理逻辑报告文件路径和调用链 })耗时与并行:普通 Tool子 Agent延迟通常毫秒秒级常需多轮 API秒分钟并行可多个 tool 并行可一次 spawn 多个子 agent 并行后台一般同步完成支持run_in_background/ fork权限与身份普通 Tool子 Agent身份追踪无agentContextagentType: subagent权限主 agent 权限上下文可单独permissionMode、工具白/黑名单续跑不支持异步子 agent 可用SendMessageresumeExplore/Plan 等 one-shot 除外何时用哪个与代码 prompt 一致场景更合适已知文件路径读一下Read搜一个 class/函数名Grep/Glob2–3 个文件内查找ReadGrep大范围探索、多轮搜索子 agentExplore复杂多步研究/实现子 agentgeneral-purpose或 fork改一行代码Edit改完要独立验证子 agentverification架构关系图主 Agent (query 循环)├── 普通 Tools原子│ ├── Read / Write / Edit│ ├── Grep / Glob / Bash│ └── AskUserQuestion / ...│└── Agent Tool元工具 → 子 agent└── runAgent() → 新的 query 循环└── 子 agent 再调普通 Tools总结普通 tool 是主 agent 的「手脚」单次动作子 agent 是主 agent 雇的「临时工」有自己的判断和多轮工具使用通过Agent这个元工具接入结果以 tool result 形式回到主 agent。三层 Prompt 对比类型给谁看作用普通 Tool子 Agent① 主 agent System prompt主 agent定义主会话身份、规范共享同一份共享同一份fork 可继承② Tool description主 agent教主 agent 如何调用这个工具✅ 有✅Agent工具也有③ 子 agent 专属 System prompt子 agent 自己的 query 循环定义子 agent 身份、行为、约束❌ 没有✅ 有FileEditTool/prompt.ts、FileReadTool/prompt.ts属于 ② Tool description不是 ③ 子 agent System prompt。ps: cursor 也有实现相应的子agent功能