返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

Claude Code 分层多Agent架构篇

[复制链接]
链载Ai 显示全部楼层 发表于 2 小时前 |阅读模式 打印 上一主题 下一主题
本文档基于Claude Code源代码的深度逆向工程分析,详细还原了其分层多Agent架构的完整技术实现。通过分析混淆代码和运行时行为,我们深入揭示了Task工具如何实现SubAgent的创建、生命周期管理、并发执行协调以及安全隔离机制,为理解现代AI编程助手的核心架构提供了详尽的技术洞察。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;letter-spacing: 0.1em;color: rgb(63, 63, 63);">
https://github.com/shareAI-lab/analysis_claude_code


ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;display: table;padding: 0px 0.2em;color: rgb(255, 255, 255);background: rgb(0, 152, 116);">一、架构概述

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;padding-left: 8px;color: rgb(63, 63, 63);">1.1 整体架构设计

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;letter-spacing: 0.1em;color: rgb(63, 63, 63);">Claude Code采用了一种创新的分层多Agent架构,通过主Agent和SubAgent的协作来处理复杂任务:

否是用户请求主AgentnO函数是否调用Task工具直接处理工具调用Task工具p_2对象创建SubAgentI2A函数Agent生命周期管理并发执行协调UH1函数结果合成器KN5函数返回合成结果返回处理结果

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;padding-left: 8px;color: rgb(63, 63, 63);">1.2 核心技术特点

    ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;color: rgb(63, 63, 63);" class="list-paddingleft-1">
  1. ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;text-indent: -1em;display: block;margin: 0.2em 8px;color: rgb(63, 63, 63);">
    1.ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: inherit;color: rgb(0, 152, 116);">完全隔离的执行环境:每个SubAgent在独立上下文中运行
  2. ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;text-indent: -1em;display: block;margin: 0.2em 8px;color: rgb(63, 63, 63);">
    2.ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: inherit;color: rgb(0, 152, 116);">智能并发调度:支持多Agent并发执行,动态负载均衡
  3. 3.安全权限控制:细粒度的工具权限管理和资源限制
  4. 4.高效结果合成:智能的多Agent结果聚合和冲突解决
  5. 5.弹性错误处理:多层错误隔离和恢复机制

二、Task工具Agent实例化机制

2.1 Task工具核心定义

Task工具是Claude Code多Agent架构的入口点,其核心实现如下:

// Task工具常量定义 (improved-claude-code-5.mjs:25993)
cX ="Task"

// Task工具输入Schema (improved-claude-code-5.mjs:62321-62324)
CN5= n.object({
description: n.string().describe("A short (3-5 word) description of the task"),
prompt: n.string().describe("The task for the agent to perform")
})

// Task工具完整对象结构 (improved-claude-code-5.mjs:62435-62569)
p_2 = {
// 动态描述生成
asyncprompt({ tools: A }) {
returnawaitu_2(A) // 调用描述生成函数
},

name: cX, // "Task"

asyncdescription() {
return"Launch a new task"
},

inputSchema:CN5,

// 核心执行函数
async*call({ prompt: A }, context, J, F) {
// 实际的Agent启动和管理逻辑
// 详见后续分析
},

// 工具特性定义
isReadOnly() {returntrue},
isConcurrencySafe() {returntrue},
isEnabled() {returntrue},
userFacingName() {return"Task"},

// 权限检查
asynccheckPermissions(A) {
return{behavior:"allow",updatedInput: A }
}
}

2.2 动态描述生成机制

Task工具的描述是动态生成的,包含了当前可用工具的列表:

// 工具描述生成器 (improved-claude-code-5.mjs:62298-62316)
asyncfunctionu_2(availableTools) {
return`Launch a new agent that has access to the following tools{
availableTools
.filter((tool) => tool.name !== cX) // 排除Task工具本身,防止递归
.map((tool) => tool.name)
.join(", ")
}. When you are searching for a keyword or file and are not confident that you will find the right match in the first few tries, use the Agent tool to perform the search for you.

When to use the Agent tool:
- If you are searching for a keyword like "config" or "logger", or for questions like "which file does X?", the Agent tool is strongly recommended

When NOT to use the Agent tool:
- If you want to read a specific file path, use the${OB.name}or${g$.name}tool instead of the Agent tool, to find the match more quickly
- If you are searching for a specific class definition like "class Foo", use the${g$.name}tool instead, to find the match more quickly
- If you are searching for code within a specific file or set of 2-3 files, use the${OB.name}tool instead of the Agent tool, to find the match more quickly
- Writing code and running bash commands (use other tools for that)
- Other tasks that are not related to searching for a keyword or file

Usage notes:
1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses
2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.
3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.
4. The agent's outputs should generally be trusted
5. Clearly tell the agent whether you expect it to write code or just to do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent`
}

2.3 SubAgent创建流程

SubAgent的创建由I2A函数负责,该函数实现了完整的Agent实例化流程:

// SubAgent启动函数 (improved-claude-code-5.mjs:62353-62433)
asyncfunction*I2A(taskPrompt, agentIndex, parentContext, globalConfig, options = {}) {
const{
abortController: D,
options: {
debug: Y,
verbose: W,
isNonInteractiveSession: J
},
getToolPermissionContext: F,
readFileState: X,
setInProgressToolUseIDs: V,
tools: C
} = parentContext;

const{
isSynthesis: K =false,
systemPrompt: E,
model: N
} = options;

// 生成唯一的Agent ID
constagentId =VN5();

// 创建初始消息
constinitialMessages = [K2({content: taskPrompt })];

// 获取配置信息
const[modelConfig, resourceConfig, selectedModel] =awaitPromise.all([
qW(), // getModelConfiguration
RE(), // getResourceConfiguration
N ??J7() // getDefaultModel
]);

// 生成Agent系统提示
constagentSystemPrompt =await(
E ??ma0(selectedModel,Array.from(parentContext.getToolPermissionContext().additionalWorkingDirectories))
);

// 执行Agent主循环
letmessageHistory = [];
lettoolUseCount =0;
letexitPlanInput =undefined;

forawait(letagentResponseofnO( // 主Agent循环函数
initialMessages,
agentSystemPrompt,
modelConfig,
resourceConfig,
globalConfig,
{
abortController: D,
options: {
isNonInteractiveSession: J ??false,
tools: C, // 继承工具集合(但会被过滤)
commands: [],
debug: Y,
verbose: W,
mainLoopModel: selectedModel,
maxThinkingTokens:s$(initialMessages), // 计算思考token限制
MCPClients: [],
mcpResources: {}
},
getToolPermissionContext: F,
readFileState: X,
getQueuedCommands) =>[],
removeQueuedCommands) =>{},
setInProgressToolUseIDs: V,
agentId: agentId
}
)) {
// 过滤和处理Agent响应
if(agentResponse.type!=="assistant"&&
agentResponse.type!=="user"&&
agentResponse.type!=="progress")continue;

messageHistory.push(agentResponse);

// 处理工具使用统计和特殊情况
if(agentResponse.type==="assistant"|| agentResponse.type==="user") {
constnormalizedMessages =AQ(messageHistory);

for(letmessageGroupofAQ([agentResponse])) {
for(letcontentofmessageGroup.message.content) {
if(content.type!=="tool_use"&& content.type!=="tool_result")continue;

if(content.type==="tool_use") {
toolUseCount++;

// 检查退出计划模式
if(content.name==="exit_plan_mode"&& content.input) {
letvalidation = hO.inputSchema.safeParse(content.input);
if(validation.success) {
exitPlanInput = {plan: validation.data.plan};
}
}
}

// 生成进度事件
yield{
type:"progress",
toolUseID: K ?`synthesis_${globalConfig.message.id}`:`agent_${agentIndex}_${globalConfig.message.id}`,
data: {
message: messageGroup,
normalizedMessages: normalizedMessages,
type:"agent_progress"
}
};
}
}
}
}

// 处理最终结果
constlastMessage =UD(messageHistory); // 获取最后一条消息

if(lastMessage &&oK1(lastMessage))thrownewNG; // 检查中断
if(lastMessage?.type!=="assistant") {
thrownewError(K ?"Synthesis: Last message was not an assistant message":
`Agent${agentIndex +1}: Last message was not an assistant message`);
}

// 计算token使用量
consttotalTokens = (lastMessage.message.usage.cache_creation_input_tokens??0) +
(lastMessage.message.usage.cache_read_input_tokens??0) +
lastMessage.message.usage.input_tokens+
lastMessage.message.usage.output_tokens;

// 提取文本内容
consttextContent = lastMessage.message.content.filter(content=>content.type==="text");

// 保存对话历史
awaitCZ0([...initialMessages, ...messageHistory]);

// 返回最终结果
yield{
type:"result",
data: {
agentIndex: agentIndex,
content: textContent,
toolUseCount: toolUseCount,
tokens: totalTokens,
usage: lastMessage.message.usage,
exitPlanModeInput: exitPlanInput
}
};
}

三、SubAgent执行上下文分析

3.1 上下文隔离机制

每个SubAgent都在完全隔离的执行上下文中运行,确保安全性和稳定性:

// SubAgent上下文创建(基于代码分析推断)
classSubAgentContext{
constructor(parentContext, agentId) {
this.agentId= agentId;
this.parentContext= parentContext;

// 隔离的工具集合
this.tools=this.filterToolsForSubAgent(parentContext.tools);

// 继承的权限上下文
this.getToolPermissionContext= parentContext.getToolPermissionContext;

// 文件状态访问器
this.readFileState= parentContext.readFileState;

// 资源限制
this.resourceLimits= {
maxExecutionTime:300000, // 5分钟
maxToolCalls:50,
maxTokens:100000
};

// 独立的中断控制器
this.abortController=newAbortController();

// 独立的工具使用状态管理
this.setInProgressToolUseIDs=newSet();
}

// 过滤SubAgent可用的工具
filterToolsForSubAgent(allTools) {
// SubAgent被禁用的工具列表
constblockedTools = ['Task']; // 防止递归调用

returnallTools.filter(tool=>!blockedTools.includes(tool.name));
}
}

3.2 工具权限继承和限制

SubAgent继承了主Agent的基本权限,但受到额外的限制:

// 工具权限过滤器(基于代码分析推断)
classToolPermissionFilter{
constructor() {
this.allowedTools= [
'Bash','Glob','Grep','LS','exit_plan_mode',
'Read','Edit','MultiEdit','Write',
'NotebookRead','NotebookEdit','WebFetch',
'TodoRead','TodoWrite','WebSearch'
];

this.restrictedOperations= {
'Write': {maxFileSize:'5MB',requiresValidation:true},
'Edit': {maxChangesPerCall:10,requiresBackup:true},
'Bash': {timeoutSeconds:120,forbiddenCommands: ['rm -rf','sudo'] },
'WebFetch': {allowedDomains: ['docs.anthropic.com','github.com'] }
};
}

validateToolAccess(toolName, parameters, agentContext) {
// 检查工具是否在允许列表中
if(!this.allowedTools.includes(toolName)) {
thrownewError(`Tool${toolName}not allowed for SubAgent`);
}

// 检查特定工具的限制
constrestrictions =this.restrictedOperations[toolName];
if(restrictions) {
this.applyToolRestrictions(toolName, parameters, restrictions);
}

returntrue;
}
}

3.3 资源分配的独立性

每个SubAgent都有独立的资源分配和监控:

// 资源监控器(基于代码分析推断)
classSubAgentResourceMonitor{
constructor(agentId, limits) {
this.agentId= agentId;
this.limits= limits;
this.usage= {
startTimeate.now(),
tokenCount:0,
toolCallCount:0,
fileOperations:0,
networkRequests:0
};
}

recordTokenUsage(tokens) {
this.usage.tokenCount+= tokens;
if(this.usage.tokenCount>this.limits.maxTokens) {
thrownewError(`Token limit exceeded for agent${this.agentId}`);
}
}

recordToolCall(toolName) {
this.usage.toolCallCount++;
if(this.usage.toolCallCount>this.limits.maxToolCalls) {
thrownewError(`Tool call limit exceeded for agent${this.agentId}`);
}
}

checkTimeLimit() {
constelapsed =Date.now() -this.usage.startTime;
if(elapsed >this.limits.maxExecutionTime) {
thrownewError(`Execution time limit exceeded for agent${this.agentId}`);
}
}
}

四、并发执行协调机制

4.1 并发执行策略

Task工具支持两种执行模式:单Agent模式和多Agent并发模式。执行模式由parallelTasksCount配置决定:

// Task工具的并发执行逻辑 (improved-claude-code-5.mjs:62474-62526)
async*call({ prompt: A }, context, J, F) {
conststartTime =Date.now();
constconfig =ZA(); // 获取配置
constexecutionContext = {
abortController: context.abortController,
options: context.options,
getToolPermissionContext: context.getToolPermissionContext,
readFileState: context.readFileState,
setInProgressToolUseIDs: context.setInProgressToolUseIDs,
tools: context.options.tools.filter((tool) =>tool.name!== cX) // 排除Task工具本身
};

if(config.parallelTasksCount>1) {
// 多Agent并发执行模式
yield*this.executeParallelAgents(A, executionContext, config, F, J);
}else{
// 单Agent执行模式
yield*this.executeSingleAgent(A, executionContext, F, J);
}
}

// 并发执行多个Agents
async*executeParallelAgents(taskPrompt, context, config, F, J) {
lettotalToolUseCount =0;
lettotalTokens =0;

// 创建多个相同的Agent任务
constagentTasks =Array(config.parallelTasksCount)
.fill(`${taskPrompt}\n\nProvide a thorough and complete analysis.`)
.map((prompt, index) =>I2A(prompt, index, context, F, J));

constagentResults = [];

// 并发执行所有Agent任务(最大并发数:10)
forawait(letresultofUH1(agentTasks,10)) {
if(result.type==="progress") {
yieldresult;
}elseif(result.type==="result") {
agentResults.push(result.data);
totalToolUseCount += result.data.toolUseCount;
totalTokens += result.data.tokens;
}
}

// 检查是否被中断
if(context.abortController.signal.aborted)thrownewNG;

// 使用合成器合并结果
constsynthesisPrompt =KN5(taskPrompt, agentResults);
constsynthesisAgent =I2A(synthesisPrompt,0, context, F, J, {isSynthesis:true});

letsynthesisResult =null;
forawait(letresultofsynthesisAgent) {
if(result.type==="progress") {
totalToolUseCount++;
yieldresult;
}elseif(result.type==="result") {
synthesisResult = result.data;
totalTokens += synthesisResult.tokens;
}
}

if(!synthesisResult)thrownewError("Synthesis agent did not return a result");

// 检查退出计划模式
constexitPlanInput = agentResults.find(r=>r.exitPlanModeInput)?.exitPlanModeInput;

yield{
type:"result",
data: {
content: synthesisResult.content,
totalDurationMsate.now() - startTime,
totalTokens: totalTokens,
totalToolUseCount: totalToolUseCount,
usage: synthesisResult.usage,
wasInterrupted: context.abortController.signal.aborted,
exitPlanModeInput: exitPlanInput
}
};
}

4.2 并发调度器实现

UH1函数是核心的并发调度器,实现了异步生成器的并发执行:

// 并发执行调度器 (improved-claude-code-5.mjs:45024-45057)
asyncfunction*UH1(generators, maxConcurrency =Infinity) {
// 包装生成器,添加Promise追踪
constwrapGenerator= (generator) => {
constpromise = generator.next().then(({ done, value }) =>({
done,
value,
generator,
promise
}));
returnpromise;
};

constremainingGenerators = [...generators];
constactivePromises =newSet();

// 启动初始的并发任务
while(activePromises.size< maxConcurrency && remainingGenerators.length>0) {
constgenerator = remainingGenerators.shift();
activePromises.add(wrapGenerator(generator));
}

// 并发执行循环
while(activePromises.size>0) {
// 等待任何一个生成器产生结果
const{ done, value, generator, promise } =awaitPromise.race(activePromises);

// 移除已完成的Promise
activePromises.delete(promise);

if(!done) {
// 生成器还有更多数据,继续执行
activePromises.add(wrapGenerator(generator));
if(value !==undefined)yieldvalue;
}elseif(remainingGenerators.length>0) {
// 当前生成器完成,启动新的生成器
constnextGenerator = remainingGenerators.shift();
activePromises.add(wrapGenerator(nextGenerator));
}
}
}

4.3 Agent间通信和同步

Agents之间的通信通过结构化的消息系统实现:

// Agent通信消息类型
constAgentMessageTypes= {
PROGRESS:"progress",
RESULT:"result",
ERROR:"error",
STATUS_UPDATE:"status_update"
};

// Agent进度消息结构
interfaceAgentProgressMessage{
type:"progress";
toolUseID: string;
data: {
message: any;
normalizedMessages: any[];
type:"agent_progress";
};
}

// Agent结果消息结构
interfaceAgentResultMessage{
type:"result";
data: {
agentIndex: number;
content: any[];
toolUseCount: number;
tokens: number;
usage: any;
exitPlanModeInput?: any;
};
}

五、Agent生命周期管理

5.1 Agent创建和初始化

每个Agent都有明确的生命周期阶段:

// Agent生命周期状态枚举
constAgentLifecycleStates= {
INITIALIZING:'initializing',
RUNNING:'running',
WAITING:'waiting',
COMPLETED:'completed',
FAILED:'failed',
ABORTED:'aborted'
};

// Agent实例管理器(基于代码分析推断)
classAgentInstanceManager{
constructor() {
this.activeAgents=newMap();
this.completedAgents=newMap();
this.agentCounter=0;
}

createAgent(taskDescription, taskPrompt, parentContext) {
constagentId =this.generateAgentId();
constagentInstance = {
id: agentId,
index:this.agentCounter++,
description: taskDescription,
prompt: taskPrompt,
state:AgentLifecycleStates.INITIALIZING,
startTimeate.now(),
context:this.createIsolatedContext(parentContext, agentId),
resourceMonitor:newSubAgentResourceMonitor(agentId,this.getDefaultLimits()),
messageHistory: [],
results:null,
error:null
};

this.activeAgents.set(agentId, agentInstance);
returnagentInstance;
}

generateAgentId() {
return`agent_${Date.now()}_${Math.random().toString(36).substr(2,9)}`;
}

getDefaultLimits() {
return{
maxExecutionTime:300000, // 5分钟
maxTokens:100000,
maxToolCalls:50,
maxFileOperations:100
};
}
}

5.2 资源管理和清理机制

Agent执行完成后需要进行资源清理:

// 资源清理管理器(基于代码分析推断)
classAgentResourceCleaner{
constructor() {
this.cleanupTasks=newMap();
this.tempFiles=newSet();
this.activeConnections=newSet();
}

registerCleanupTask(agentId, cleanupFn) {
if(!this.cleanupTasks.has(agentId)) {
this.cleanupTasks.set(agentId, []);
}
this.cleanupTasks.get(agentId).push(cleanupFn);
}

asynccleanupAgent(agentId) {
consttasks =this.cleanupTasks.get(agentId) || [];

// 执行所有清理任务
constcleanupPromises = tasks.map(async(cleanupFn) => {
try{
awaitcleanupFn();
}catch(error) {
console.error(`Cleanup task failed for agent${agentId}:`, error);
}
});

awaitPromise.all(cleanupPromises);

// 移除清理任务记录
this.cleanupTasks.delete(agentId);

// 清理临时文件
awaitthis.cleanupTempFiles(agentId);

// 关闭网络连接
awaitthis.closeConnections(agentId);
}

asynccleanupTempFiles(agentId) {
// 清理Agent创建的临时文件
constagentTempFiles =Array.from(this.tempFiles)
.filter(file=>file.includes(agentId));

for(constfileofagentTempFiles) {
try{
if(x1().existsSync(file)) {
x1().unlinkSync(file);
}
this.tempFiles.delete(file);
}catch(error) {
console.error(`Failed to delete temp file${file}:`, error);
}
}
}
}

5.3 超时控制和错误恢复

Agent执行过程中的超时和错误处理:

// Agent超时控制器(基于代码分析推断)
classAgentTimeoutController{
constructor(agentId, timeoutMs =300000) { // 默认5分钟
this.agentId= agentId;
this.timeoutMs= timeoutMs;
this.abortController=newAbortController();
this.timeoutId=null;
this.startTime=Date.now();
}

start() {
this.timeoutId=setTimeout(() =>{
console.warn(`Agent${this.agentId}timed out after${this.timeoutMs}ms`);
this.abort('timeout');
},this.timeoutMs);

returnthis.abortController.signal;
}

abort(reason ='manual') {
if(this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId=null;
}

this.abortController.abort();

console.log(`Agent${this.agentId}aborted due to{reason}`);
}

getElapsedTime() {
returnDate.now() -this.startTime;
}

getRemainingTime() {
returnMath.max(0,this.timeoutMs-this.getElapsedTime());
}
}

// Agent错误恢复机制(基于代码分析推断)
classAgentErrorRecovery{
constructor() {
this.maxRetries=3;
this.backoffMultiplier=2;
this.baseDelayMs=1000;
}

asyncexecuteWithRetry(agentFn, agentId, attempt =1) {
try{
returnawaitagentFn();
}catch(error) {
if(attempt >=this.maxRetries) {
thrownewError(`Agent${agentId}failed after${this.maxRetries}attempts{error.message}`);
}

constdelay =this.baseDelayMs*Math.pow(this.backoffMultiplier, attempt -1);
console.warn(`Agent${agentId}attempt${attempt}failed, retrying in${delay}ms{error.message}`);

awaitthis.sleep(delay);
returnthis.executeWithRetry(agentFn, agentId, attempt +1);
}
}

sleep(ms) {
returnnewPromise(resolve=>setTimeout(resolve, ms));
}
}

六、工具白名单和权限控制

6.1 SubAgent工具白名单

SubAgent只能访问预定义的安全工具集合:

// SubAgent可用工具列表(基于代码分析)
constSUBAGENT_ALLOWED_TOOLS= [
// 文件操作工具
'Read', // 文件读取
'Write', // 文件写入
'Edit', // 文件编辑
'MultiEdit', // 批量文件编辑
'LS', // 目录列表

// 搜索工具
'Glob', // 文件模式匹配
'Grep', // 内容搜索

// 系统交互工具
'Bash', // 命令执行(受限)

// Notebook工具
'NotebookRead',// Notebook读取
'NotebookEdit',// Notebook编辑

// 网络工具
'WebFetch', // 网页内容获取(受限域名)
'WebSearch', // 网络搜索

// 任务管理工具
'TodoRead', // 任务列表读取
'TodoWrite', // 任务列表写入

// 计划模式工具
'exit_plan_mode'// 退出计划模式
];

// 被禁用的工具(不可用于SubAgent)
constSUBAGENT_BLOCKED_TOOLS= [
'Task', // 防止递归调用
// 可能还有其他敏感工具
];

// 工具过滤函数(improved-claude-code-5.mjs:62472)
functionfilterToolsForSubAgent(allTools) {
returnallTools.filter((tool) =>tool.name!== cX); // cX = "Task"
}

6.2 工具权限验证器

每个工具调用都经过严格的权限验证:

// 工具权限验证系统(基于代码分析推断)
classToolPermissionValidator{
constructor() {
this.permissionMatrix=this.buildPermissionMatrix();
this.securityPolicies=this.loadSecurityPolicies();
}

buildPermissionMatrix() {
return{
'Read': {
allowedExtensions: ['.js','.ts','.json','.md','.txt','.yaml','.yml','.py'],
maxFileSize:10*1024*1024, // 10MB
forbiddenPaths: ['/etc/passwd','/etc/shadow','~/.ssh','~/.aws'],
maxConcurrent:5
},

'Write': {
maxFileSize:5*1024*1024, // 5MB
forbiddenPaths: ['/etc','/usr','/bin','/sbin'],
requiresBackup:true,
maxFilesPerOperation:10
},

'Edit': {
maxChangesPerCall:10,
forbiddenPatterns: ['eval(','exec(','__import__','subprocess.'],
requiresValidation:true,
backupRequired:true
},

'Bash': {
timeoutSeconds:120,
forbiddenCommands: [
'rm -rf','dd if=','mkfs','fdisk','chmod 777',
'sudo','su','passwd','chown','mount'
],
allowedCommands: [
'ls','cat','grep','find','echo','pwd','whoami',
'ps','top','df','du','date','uname'
],
maxOutputSize:1024*1024, // 1MB
sandboxed:true
},

'WebFetch': {
allowedDomains: [
'docs.anthropic.com',
'github.com',
'raw.githubusercontent.com',
'api.github.com'
],
maxResponseSize:5*1024*1024, // 5MB
timeoutSeconds:30,
cacheDuration:900, // 15分钟
maxRequestsPerMinute:10
},

'WebSearch': {
maxResults:10,
allowedRegions: ['US'],
timeoutSeconds:15,
maxQueriesPerMinute:5
}
};
}

asyncvalidateToolCall(toolName, parameters, agentContext) {
// 1. 检查工具是否在白名单中
if(!SUBAGENT_ALLOWED_TOOLS.includes(toolName)) {
thrownewPermissionError(`Tool${toolName}not allowed for SubAgent`);
}

// 2. 检查工具特定权限
constpermissions =this.permissionMatrix[toolName];
if(permissions) {
awaitthis.enforceToolPermissions(toolName, parameters, permissions, agentContext);
}

// 3. 检查全局安全策略
awaitthis.enforceSecurityPolicies(toolName, parameters, agentContext);

// 4. 记录工具使用
this.logToolUsage(toolName, parameters, agentContext);

returntrue;
}

asyncenforceToolPermissions(toolName, parameters, permissions, agentContext) {
switch(toolName) {
case'Read':
awaitthis.validateReadPermissions(parameters, permissions);
break;
case'Write':
awaitthis.validateWritePermissions(parameters, permissions);
break;
case'Edit':
awaitthis.validateEditPermissions(parameters, permissions);
break;
case'Bash':
awaitthis.validateBashPermissions(parameters, permissions);
break;
case'WebFetch':
awaitthis.validateWebFetchPermissions(parameters, permissions);
break;
default:
// 使用默认验证逻辑
break;
}
}

asyncvalidateBashPermissions(parameters, permissions) {
constcommand = parameters.command.toLowerCase();

// 检查禁止的命令
for(constforbiddenofpermissions.forbiddenCommands) {
if(command.includes(forbidden.toLowerCase())) {
thrownewPermissionError(`Forbidden command{forbidden}`);
}
}

// 检查命令长度
if(command.length>1000) {
thrownewPermissionError('Command too long');
}

// 检查危险字符
constdangerousChars = ['|','&',';','`','$','(',')'];
for(constcharofdangerousChars) {
if(command.includes(char)) {
console.warn(`Potentially dangerous character in command{char}`);
}
}
}

asyncvalidateWebFetchPermissions(parameters, permissions) {
consturl =newURL(parameters.url);

// 检查域名白名单
constisAllowed = permissions.allowedDomains.some(domain=>
url.hostname=== domain || url.hostname.endsWith('.'+ domain)
);

if(!isAllowed) {
thrownewPermissionError(`Domain not allowed{url.hostname}`);
}

// 检查协议
if(url.protocol!=='https:'&& url.protocol!=='http:') {
thrownewPermissionError(`Protocol not allowed{url.protocol}`);
}
}
}

// 权限错误类
classPermissionErrorextendsError{
constructor(message, code ='PERMISSION_DENIED') {
super(message);
this.name='PermissionError';
this.code= code;
}
}

6.3 递归调用防护机制

防止SubAgent递归调用Task工具的多层保护:

// 递归调用防护系统(基于代码分析推断)
classRecursionGuard{
constructor() {
this.callStack=newMap(); // agentId -> call depth
this.maxDepth=3;
this.maxAgentsPerLevel=5;
}

checkRecursionLimit(agentId, toolName) {
// 严格禁止Task工具的递归调用
if(toolName ==='Task') {
thrownewRecursionError('Task tool cannot be called from SubAgent');
}

// 检查调用深度
constcurrentDepth =this.callStack.get(agentId) ||0;
if(currentDepth >=this.maxDepth) {
thrownewRecursionError(`Maximum recursion depth exceeded{currentDepth}`);
}

returntrue;
}

enterCall(agentId) {
constcurrentDepth =this.callStack.get(agentId) ||0;
this.callStack.set(agentId, currentDepth +1);
}

exitCall(agentId) {
constcurrentDepth =this.callStack.get(agentId) ||0;
if(currentDepth >0) {
this.callStack.set(agentId, currentDepth -1);
}
}
}

classRecursionErrorextendsError{
constructor(message) {
super(message);
this.name='RecursionError';
}
}

七、结果合成和报告机制

7.1 多Agent结果收集逻辑

多个Agent的执行结果通过专门的收集器统一管理:

// 多Agent结果收集器(基于代码分析)
classMultiAgentResultCollector{
constructor() {
this.results=newMap(); // agentIndex -> result
this.metadata= {
totalTokens:0,
totalToolCalls:0,
totalExecutionTime:0,
errorCount:0
};
}

addResult(agentIndex, result) {
this.results.set(agentIndex, result);

// 更新统计信息
this.metadata.totalTokens+= result.tokens||0;
this.metadata.totalToolCalls+= result.toolUseCount||0;

if(result.error) {
this.metadata.errorCount++;
}
}

getAllResults() {
// 按Agent索引排序返回结果
constsortedResults =Array.from(this.results.entries())
.sort(([indexA], [indexB]) =>indexA - indexB)
.map(([index, result]) =>({agentIndex: index, ...result }));

returnsortedResults;
}

getSuccessfulResults() {
returnthis.getAllResults().filter(result=>!result.error);
}

hasErrors() {
returnthis.metadata.errorCount>0;
}
}

7.2 结果格式化和合并

KN5函数负责将多个Agent的结果合并成统一的格式:

// 多Agent结果合成器 (improved-claude-code-5.mjs:62326-62351)
functionKN5(originalTask, agentResults) {
// 按Agent索引排序结果
constsortedResults = agentResults.sort((a, b) =>a.agentIndex- b.agentIndex);

// 提取每个Agent的文本内容
constagentResponses = sortedResults.map((result, index) =>{
consttextContent = result.content
.filter((content) =>content.type==="text")
.map((content) =>content.text)
.join("\n\n");

return`== AGENT${index +1}RESPONSE ==
${textContent}`;
}).join("\n\n");

// 生成合成提示
constsynthesisPrompt =`Original task{originalTask}

I've assigned multiple agents to tackle this task. Each agent has analyzed the problem and provided their findings.

${agentResponses}

Based on all the information provided by these agents, synthesize a comprehensive and cohesive response that:
1. Combines the key insights from all agents
2. Resolves any contradictions between agent findings
3. Presents a unified solution that addresses the original task
4. Includes all important details and code examples from the individual responses
5. Is well-structured and complete

Your synthesis should be thorough but focused on the original task.`;

returnsynthesisPrompt;
}

7.3 智能总结生成机制

合成Agent使用专门的提示来生成智能总结:

// 智能总结生成器(基于代码分析推断)
classIntelligentSynthesizer{
constructor() {
this.synthesisStrategies= {
'code_analysis':this.synthesizeCodeAnalysis,
'problem_solving':this.synthesizeProblemSolving,
'research':this.synthesizeResearch,
'implementation':this.synthesizeImplementation
};
}

asyncgenerateSynthesis(originalTask, agentResults, taskType ='general') {
// 预处理结果
constprocessedResults =this.preprocessResults(agentResults);

// 检测任务类型
constdetectedType =this.detectTaskType(originalTask, processedResults);
conststrategy =this.synthesisStrategies[detectedType] ||this.synthesizeGeneral;

// 生成合成内容
constsynthesis =awaitstrategy.call(this, originalTask, processedResults);

return{
originalTask,
taskType: detectedType,
agentCount: agentResults.length,
synthesis,
metadata:this.extractMetadata(processedResults)
};
}

preprocessResults(agentResults) {
returnagentResults.map(result=>({
agentIndex: result.agentIndex,
content:this.extractTextContent(result.content),
toolsUsed:this.extractToolsUsed(result),
codeBlocks:this.extractCodeBlocks(result.content),
findings:this.extractFindings(result.content),
errors:this.extractErrors(result)
}));
}

synthesizeCodeAnalysis(originalTask, processedResults) {
constallCodeBlocks = processedResults.flatMap(r=>r.codeBlocks);
constallFindings = processedResults.flatMap(r=>r.findings);

return{
summary:this.generateCodeAnalysisSummary(allFindings),
codeExamples:this.deduplicateCodeBlocks(allCodeBlocks),
recommendations:this.generateCodeRecommendations(allFindings),
technicalDetails:this.mergeTechnicalDetails(processedResults)
};
}

synthesizeProblemSolving(originalTask, processedResults) {
constsolutions = processedResults.map(r=>r.findings);
constbestSolution =this.rankSolutions(solutions)[0];

return{
problem: originalTask,
recommendedSolution: bestSolution,
alternativeSolutions: solutions.slice(1),
implementationSteps:this.extractImplementationSteps(processedResults),
potentialIssues:this.identifyPotentialIssues(processedResults)
};
}

extractCodeBlocks(content) {
constcodeBlockRegex =/```[\s\S]*?```/g;
returncontent.map(c=>c.text||'').join('\n').match(codeBlockRegex) || [];
}

deduplicateCodeBlocks(codeBlocks) {
constseen =newSet();
returncodeBlocks.filter(block=>{
constnormalized = block.replace(/\s+/g,' ').trim();
if(seen.has(normalized))returnfalse;
seen.add(normalized);
returntrue;
});
}
}

7.4 结果一致性保证

确保多Agent结果的一致性和准确性:

// 结果一致性验证器(基于代码分析推断)
classResultConsistencyValidator{
constructor() {
this.consistencyChecks= [
this.checkFactualConsistency,
this.checkCodeConsistency,
this.checkRecommendationConsistency,
this.checkTimelineConsistency
];
}

asyncvalidateConsistency(agentResults) {
constinconsistencies = [];

for(constcheckofthis.consistencyChecks) {
try{
constissues =awaitcheck.call(this, agentResults);
inconsistencies.push(...issues);
}catch(error) {
console.error('Consistency check failed:', error);
}
}

return{
isConsistent: inconsistencies.length===0,
inconsistencies,
confidence:this.calculateConfidence(agentResults, inconsistencies)
};
}

checkFactualConsistency(agentResults) {
constfacts =this.extractFacts(agentResults);
constcontradictions = [];

// 检查事实性陈述的一致性
for(leti =0; i < facts.length; i++) {
for(letj = i +1; j < facts.length; j++) {
if(this.areContradictory(facts[i], facts[j])) {
contradictions.push({
type:'factual_contradiction',
fact1: facts[i],
fact2: facts[j],
severity:'high'
});
}
}
}

returncontradictions;
}

checkCodeConsistency(agentResults) {
constcodeBlocks = agentResults.flatMap(r=>this.extractCodeBlocks(r.content));
constinconsistencies = [];

// 检查代码示例的一致性
constfunctionNames =this.extractFunctionNames(codeBlocks);
constvariableNames =this.extractVariableNames(codeBlocks);

// 检查命名一致性
if(this.hasNamingInconsistencies(functionNames)) {
inconsistencies.push({
type:'naming_inconsistency',
category:'functions',
details: functionNames
});
}

returninconsistencies;
}

calculateConfidence(agentResults, inconsistencies) {
constbaseConfidence =0.8;
constpenaltyPerInconsistency =0.1;
constagreementBonus =this.calculateAgreementBonus(agentResults);

returnMath.max(0.1,Math.min(1.0,
baseConfidence - (inconsistencies.length* penaltyPerInconsistency) + agreementBonus
));
}
}

八、主Agent循环机制深度分析

8.1 nO函数核心实现

nO函数是整个Agent系统的核心,实现了完整的对话循环:

// 主Agent循环函数 (improved-claude-code-5.mjs:46187-46302)
asyncfunction*nO(messages, systemPrompt, modelConfig, resourceConfig, globalConfig, context, compactionState, fallbackModel, options) {
yield{type:"stream_request_start"};

letcurrentMessages = messages;
letcurrentContext = context;

// 检查是否需要上下文压缩
const{
messages: processedMessages,
wasCompacted
} =awaitwU2(messages, context); // 上下文压缩函数

if(wasCompacted) {
// 记录压缩事件
E1("tengu_auto_compact_succeeded", {
originalMessageCount: messages.length,
compactedMessageCount: processedMessages.length
});

// 更新压缩状态
if(!compactionState?.compacted) {
compactionState = {
compacted:true,
turnId:bW5(), // 生成turn ID
turnCounter:0
};
}

currentMessages = processedMessages;
}

letassistantMessages = [];
letcurrentModel = context.options.mainLoopModel;
letshouldRetry =true;

try{
while(shouldRetry) {
shouldRetry =false;

try{
// 调用语言模型
forawait(letresponseofwu( // 语言模型调用函数
Ie1(currentMessages, modelConfig), // 格式化消息
Qe1(systemPrompt, resourceConfig), // 格式化系统提示
context.options.maxThinkingTokens, // token限制
context.options.tools, // 可用工具
context.abortController.signal, // 中断信号
{
getToolPermissionContext: context.getToolPermissionContext,
model: currentModel,
prependCLISysprompt:true,
toolChoice:undefined,
isNonInteractiveSession: context.options.isNonInteractiveSession,
fallbackModel: fallbackModel
}
)) {
yieldresponse;

if(response.type==="assistant") {
assistantMessages.push(response);
}
}
}catch(error) {
// 模型fallback处理
if(errorinstanceofwH1 && fallbackModel) {
currentModel = fallbackModel;
shouldRetry =true;
assistantMessages.length=0;
context.options.mainLoopModel= fallbackModel;

// 记录fallback事件
E1("tengu_model_fallback_triggered", {
original_model: error.originalModel,
fallback_model: fallbackModel,
entrypoint:"cli"
});

yieldL11(`Model fallback triggered: switching from${error.originalModel}to${error.fallbackModel}`,"info");
continue;
}
throwerror;
}
}
}catch(error) {
// 错误处理逻辑
b1(errorinstanceofError? error :newError(String(error)));

consterrorMessage = errorinstanceofError? error.message:String(error);
E1("tengu_query_error", {
assistantMessages: assistantMessages.length,
toolUses: assistantMessages.flatMap(msg=>
msg.message.content.filter(content=>content.type==="tool_use")
).length
});

// 为每个工具调用生成错误响应
lethasErrorResponse =false;
for(constmessageofassistantMessages) {
consttoolUses = message.message.content.filter(content=>content.type==="tool_use");
for(consttoolUseoftoolUses) {
yieldK2({ // 创建用户消息
content: [{
type:"tool_result",
content: errorMessage,
is_error:true,
tool_use_id: toolUse.id
}],
toolUseResult: errorMessage
});
hasErrorResponse =true;
}
}

if(!hasErrorResponse) {
yieldSt1({toolUse:false,hardcodedMessage:undefined});
}
return;
}

if(!assistantMessages.length)return;

// 提取工具调用
consttoolUses = assistantMessages.flatMap(msg=>
msg.message.content.filter(content=>content.type==="tool_use")
);

if(!toolUses.length)return;

// 执行工具调用
consttoolResults = [];
letpreventContinuation =false;

forawait(letresultofhW5(toolUses, assistantMessages, globalConfig, context)) { // 工具执行协调器
yieldresult;

if(result && result.type==="system"&& result.preventContinuation) {
preventContinuation =true;
}

toolResults.push(...JW([result]).filter(msg=>msg.type==="user"));
}

// 检查中断
if(context.abortController.signal.aborted) {
yieldSt1({toolUse:true,hardcodedMessage:undefined});
return;
}

if(preventContinuation)return;

// 排序工具结果
constsortedResults = toolResults.sort((a, b) =>{
constindexA = toolUses.findIndex(tool=>tool.id=== (a.type==="user"&& a.message.content[0].id));
constindexB = toolUses.findIndex(tool=>tool.id=== (b.type==="user"&& b.message.content[0].id));
returnindexA - indexB;
});

// 更新压缩状态计数器
if(compactionState?.compacted) {
compactionState.turnCounter++;
E1("tengu_post_autocompact_turn", {
turnId: compactionState.turnId,
turnCounter: compactionState.turnCounter
});
}

// 处理排队的命令
constqueuedCommands = [...context.getQueuedCommands()];
forawait(letcommandofx11(null, context,null, queuedCommands)) {
yieldcommand;
toolResults.push(command);
}
context.removeQueuedCommands(queuedCommands);

// Opus 4限制检查和fallback
constupdatedContext =HP() ? { // 检查是否达到Opus 4限制
...context,
options: {
...context.options,
mainLoopModel:wX() // 获取fallback模型
}
} : context;

if(HP() &&wX() !== context.options.mainLoopModel) {
E1("tengu_fallback_system_msg", {
mainLoopModel: context.options.mainLoopModel,
fallbackModel:wX()
});
yieldL11(`Claude Opus 4 limit reached, now using${H_(wX())}`,"warning");
}

// 递归调用,继续对话循环
yield*nO(
[...currentMessages, ...assistantMessages, ...sortedResults],
systemPrompt,
modelConfig,
resourceConfig,
globalConfig,
updatedContext,
compactionState,
fallbackModel,
options
);
}

8.2 工具执行协调器hW5

hW5函数负责协调多个工具的执行,实现并发和串行执行的智能调度:

// 工具执行协调器 (improved-claude-code-5.mjs:46304-46312)
asyncfunction*hW5(toolUses, assistantMessages, globalConfig, context) {
// 将工具调用按并发安全性分组
for(let{ isConcurrencySafe, blocks }ofmW5(toolUses, context)) {
if(isConcurrencySafe) {
// 并发执行安全工具
yield*uW5(blocks, assistantMessages, globalConfig, context);
}else{
// 串行执行非安全工具
yield*dW5(blocks, assistantMessages, globalConfig, context);
}
}
}

// 工具分组函数 (improved-claude-code-5.mjs:46314-46326)
functionmW5(toolUses, context) {
returntoolUses.reduce((groups, toolUse) =>{
consttool = context.options.tools.find(t=>t.name=== toolUse.name);
constvalidation = tool?.inputSchema.safeParse(toolUse.input);
constisSafe = validation?.success?Boolean(tool?.isConcurrencySafe(validation.data)) :false;

if(isSafe && groups[groups.length-1]?.isConcurrencySafe) {
// 添加到当前安全组
groups[groups.length-1].blocks.push(toolUse);
}else{
// 创建新组
groups.push({
isConcurrencySafe: isSafe,
blocks: [toolUse]
});
}

returngroups;
}, []);
}

// 串行执行 (improved-claude-code-5.mjs:46328-46330)
asyncfunction*dW5(toolUses, assistantMessages, globalConfig, context) {
for(consttoolUseoftoolUses) {
yield*MH1( // 单个工具执行
toolUse,
assistantMessages.find(msg=>
msg.message.content.some(content=>
content.type==="tool_use"&& content.id=== toolUse.id
)
),
globalConfig,
context
);
}
}

// 并发执行 (improved-claude-code-5.mjs:46332-46334)
asyncfunction*uW5(toolUses, assistantMessages, globalConfig, context) {
yield*UH1( // 并发执行调度器
toolUses.map(toolUse=>MH1(
toolUse,
assistantMessages.find(msg=>
msg.message.content.some(content=>
content.type==="tool_use"&& content.id=== toolUse.id
)
),
globalConfig,
context
)),
gW5 // 最大并发数:10
);
}

8.3 单个工具执行MH1

MH1函数负责执行单个工具调用,包含完整的验证、权限检查和结果处理:

// 单个工具执行函数 (improved-claude-code-5.mjs:46340-46388)
asyncfunction*MH1(toolUse, assistantMessage, globalConfig, context) {
consttoolName = toolUse.name;
consttool = context.options.tools.find(t=>t.name=== toolName);

// 标记工具正在使用
context.setInProgressToolUseIDs(current=>newSet([...current, toolUse.id]));

if(!tool) {
// 工具不存在错误
E1("tengu_tool_use_error", {
error:`No such tool available:${toolName}`,
toolName: toolName,
toolUseID: toolUse.id,
isMcp:false
});

yieldK2({
content: [{
type:"tool_result",
content:`Error: No such tool available:${toolName}`,
is_error:true,
tool_use_id: toolUse.id
}],
toolUseResult:`Error: No such tool available:${toolName}`
});

Oe1(context, toolUse.id); // 清理工具使用状态
return;
}

constinputParameters = toolUse.input;

try{
// 检查中断信号
if(context.abortController.signal.aborted) {
E1("tengu_tool_use_cancelled", {
toolName: tool.name,
toolUseID: toolUse.id,
isMcp: tool.isMcp??false
});

constcancelledResult =kw2(toolUse.id); // 创建取消结果
yieldK2({
content: [cancelledResult],
toolUseResult:Ju// 取消消息常量
});

Oe1(context, toolUse.id);
return;
}

// 执行工具
forawait(letresultofpW5(tool, toolUse.id, inputParameters, context, globalConfig, assistantMessage)) {
yieldresult;
}
}catch(error) {
// 工具执行错误处理
b1(errorinstanceofError? error :newError(String(error)));

yieldK2({
content: [{
type:"tool_result",
content:"Error calling tool",
is_error:true,
tool_use_id: toolUse.id
}],
toolUseResult:"Error calling tool"
});
}

Oe1(context, toolUse.id); // 清理工具使用状态
}

九、实际混淆代码实现还原

9.1 核心函数映射表

基于深度代码分析,以下是关键混淆函数的完整映射:

// 混淆代码映射表(完整版)
constOBFUSCATED_FUNCTION_MAPPING= {
// === Agent核心函数 ===
'nO':'executeMainAgentLoop', // 主Agent循环
'I2A':'launchSubAgent', // SubAgent启动器
'u_2':'generateTaskDescription', // Task工具描述生成器
'KN5':'synthesizeMultipleAgentResults',// 多Agent结果合成
'hW5':'coordinateToolExecution', // 工具执行协调器
'MH1':'executeToolWithValidation', // 单个工具执行引擎
'pW5':'executeToolCall', // 工具调用执行器
'UH1':'concurrentExecutor', // 并发执行调度器

// === 系统提示和配置函数 ===
'ga0':'getMainSystemPrompt', // 主系统提示
'ma0':'generateAgentSystemPrompt', // Agent系统提示生成器
'wU2':'compressConversationContext', // 上下文压缩器
'AU2':'generateCompressionPrompt', // 压缩提示生成器
'qW':'getModelConfiguration', // 模型配置获取
'RE':'getResourceConfiguration', // 资源配置获取
'J7':'getDefaultModel', // 默认模型获取
's$':'calculateThinkingTokenLimit', // 思考token限制计算

// === 消息处理函数 ===
'K2':'createUserMessage', // 用户消息创建器
'wu':'callLanguageModel', // 语言模型调用
'Ie1':'formatMessagesForModel', // 消息格式化
'Qe1':'formatSystemPrompt', // 系统提示格式化
'JW':'normalizeMessages', // 消息标准化
'AQ':'processMessageArray', // 消息数组处理
'UD':'getLastMessage', // 获取最后消息

// === 工具相关常量和对象 ===
'cX':'"Task"', // Task工具名称
'p_2':'TaskToolObject', // Task工具对象
'CN5':'TaskToolInputSchema', // Task工具输入Schema
'OB':'ReadToolObject', // Read工具对象
'g$':'GrepToolObject', // Grep工具对象
'IJ1':'"WebFetch"', // WebFetch工具名称

// === ID和状态管理 ===
'VN5':'generateUniqueAgentId', // Agent ID生成器
'bW5':'generateTurnId', // Turn ID生成器
'ZA':'getGlobalConfiguration', // 全局配置获取
'Oe1':'clearToolUseState', // 清理工具使用状态
'kw2':'createCancelledResult', // 创建取消结果

// === 错误处理和事件 ===
'E1':'recordTelemetryEvent', // 记录遥测事件
'b1':'logError', // 错误日志
'L11':'createInfoMessage', // 创建信息消息
'St1':'createSystemMessage', // 创建系统消息
'NG':'AbortError', // 中断错误类
'wH1':'ModelFallbackError', // 模型fallback错误
'Uz':'UserCancellationError', // 用户取消错误

// === 工具分组和执行 ===
'mW5':'groupToolsByCompatibility', // 按兼容性分组工具
'dW5':'executeToolsSequentially', // 串行执行工具
'uW5':'executeToolsConcurrently', // 并发执行工具
'gW5':'MAX_CONCURRENT_TOOLS', // 最大并发工具数(值:10)

// === 上下文和压缩 ===
'CZ0':'saveConversationHistory', // 保存对话历史
'HP':'isOpus4LimitReached', // 检查Opus 4限制
'wX':'getFallbackModel', // 获取fallback模型
'H_':'getModelDisplayName', // 获取模型显示名称

// === 工具结果处理 ===
'yw2':'createProgressMessage', // 创建进度消息
'bK':'recordToolMetrics', // 记录工具指标
'oK1':'isInterruptedMessage', // 检查中断消息
'x11':'processQueuedCommands', // 处理排队命令

// === 配置和限制 ===
'Q2A':'MAX_PARALLEL_AGENTS', // 最大并行Agent数(值:3)
'k11':'MAX_CONTEXT_LENGTH', // 最大上下文长度(值:40000)
'Uu':'MIN_COMPRESSION_THRESHOLD', // 最小压缩阈值(值:1000)

// === UI和React组件(如果存在) ===
'_p':'MainAgentComponent', // 主Agent React组件
'c3':'ContextProvider', // 上下文提供者
'Py2':'StatusDisplayComponent', // 状态显示组件
'$w1':'ToolResultComponent', // 工具结果组件
'Hp':'ErrorDisplayComponent', // 错误显示组件
};

9.2 还原的核心数据结构

// 还原的核心数据结构

// Agent执行上下文结构
interfaceAgentExecutionContext{
abortController:AbortController;
options: {
debug: boolean;
verbose: boolean;
isNonInteractiveSession: boolean;
tools:Tool[];
commands:Command[];
mainLoopModel: string;
maxThinkingTokens: number;
mcpClients: any[];
mcpResources:Record<string, any>;
};
getToolPermissionContext) =>ToolPermissionContext;
readFileState:FileStateReader;
getQueuedCommands) =>Command[];
removeQueuedCommandscommands: Command[]) =>void;
setInProgressToolUseIDs:React.Dispatch<React.SetStateAction<Set<string>>>;
agentId?: string;
}

// 工具执行结果结构
interfaceToolExecutionResult{
type:"result"|"progress"|"error";
toolUseID?: string;
parentToolUseID?: string;
data?: any;
error?: string;
}

// Agent结果数据结构
interfaceAgentResultData{
agentIndex: number;
content:TextContent[];
toolUseCount: number;
tokens: number;
usage:TokenUsage;
exitPlanModeInput?: {
plan: string;
};
}

// Task工具最终结果结构
interfaceTaskToolResult{
content:TextContent[];
totalDurationMs: number;
totalTokens: number;
totalToolUseCount: number;
usage:TokenUsage;
wasInterrupted: boolean;
exitPlanModeInput?: {
plan: string;
};
}

// 压缩状态结构
interfaceCompactionState{
compacted: boolean;
turnId: string;
turnCounter: number;
}

// 工具分组结构
interfaceToolGroup{
isConcurrencySafe: boolean;
blocks:ToolUse[];
}

// 全局配置结构
interfaceGlobalConfiguration{
parallelTasksCount: number;
maxConcurrentTools: number;
compressionThreshold: number;
defaultTimeoutMs: number;
fallbackModel: string;
debugMode: boolean;
}

9.3 完整的Task工具对象还原

// 完整还原的Task工具对象 (基于improved-claude-code-5.mjs:62435-62569)
constTaskToolObject= {
// 动态描述生成
asyncprompt({ tools }) {
returnawaitgenerateTaskDescription(tools);
},

name:"Task",

asyncdescription() {
return"Launch a new task";
},

inputSchema: zod.object({
description: zod.string().describe("A short (3-5 word) description of the task"),
prompt: zod.string().describe("The task for the agent to perform")
}),

// 核心执行函数
async*call({ prompt }, context, globalConfig, parentMessage) {
conststartTime =Date.now();
constconfig =getGlobalConfiguration();
constexecutionContext = {
abortController: context.abortController,
options: {
debug: context.options.debug,
verbose: context.options.verbose,
isNonInteractiveSession: context.options.isNonInteractiveSession??false
},
getToolPermissionContext: context.getToolPermissionContext,
readFileState: context.readFileState,
setInProgressToolUseIDs: context.setInProgressToolUseIDs,
tools: context.options.tools.filter(tool=>tool.name!=="Task") // 排除自身
};

if(config.parallelTasksCount>1) {
// 多Agent并发执行模式
lettotalToolUseCount =0;
lettotalTokens =0;

// 创建多个Agent任务
constagentTasks =Array(config.parallelTasksCount)
.fill(`${prompt}\n\nProvide a thorough and complete analysis.`)
.map((taskPrompt, index) =>launchSubAgent(taskPrompt, index, executionContext, parentMessage, globalConfig));

constagentResults = [];

// 并发执行Agents
forawait(letresultofconcurrentExecutor(agentTasks,10)) {
if(result.type==="progress") {
yieldresult;
}elseif(result.type==="result") {
agentResults.push(result.data);
totalToolUseCount += result.data.toolUseCount;
totalTokens += result.data.tokens;
}
}

if(context.abortController.signal.aborted)thrownewAbortError();

// 结果合成
constsynthesisPrompt =synthesizeMultipleAgentResults(prompt, agentResults);
constsynthesisAgent =launchSubAgent(synthesisPrompt,0, executionContext, parentMessage, globalConfig, {
isSynthesis:true
});

letsynthesisResult =null;
forawait(letresultofsynthesisAgent) {
if(result.type==="progress") {
totalToolUseCount++;
yieldresult;
}elseif(result.type==="result") {
synthesisResult = result.data;
totalTokens += synthesisResult.tokens;
}
}

if(!synthesisResult)thrownewError("Synthesis agent did not return a result");

constexitPlanInput = agentResults.find(r=>r.exitPlanModeInput)?.exitPlanModeInput;

yield{
type:"result",
data: {
content: synthesisResult.content,
totalDurationMsate.now() - startTime,
totalTokens: totalTokens,
totalToolUseCount: totalToolUseCount,
usage: synthesisResult.usage,
wasInterrupted: context.abortController.signal.aborted,
exitPlanModeInput: exitPlanInput
}
};
}else{
// 单Agent执行模式
constagentExecution =launchSubAgent(prompt,0, executionContext, parentMessage, globalConfig);
lettoolUseCount =0;
letagentResult =null;

forawait(letresultofagentExecution) {
if(result.type==="progress") {
yieldresult;
}elseif(result.type==="result") {
agentResult = result.data;
toolUseCount = agentResult.toolUseCount;
}
}

if(context.abortController.signal.aborted)thrownewAbortError();
if(!agentResult)thrownewError("Agent did not return a result");

yield{
type:"result",
data: {
content: agentResult.content,
totalDurationMsate.now() - startTime,
totalTokens: agentResult.tokens,
totalToolUseCount: toolUseCount,
usage: agentResult.usage,
wasInterrupted: context.abortController.signal.aborted,
exitPlanModeInput: agentResult.exitPlanModeInput
}
};
}
},

// 工具特性定义
isReadOnly() {
returntrue;
},

isConcurrencySafe() {
returntrue;
},

isEnabled() {
returntrue;
},

userFacingName() {
return"Task";
},

// 权限检查
asynccheckPermissions(input) {
return{
behavior:"allow",
updatedInput: input
};
},

// 工具结果映射
mapToolResultToToolResultBlockParam(result, toolUseId) {
if(result.exitPlanModeInput) {
return{
tool_use_id: toolUseId,
type:"tool_result",
content: [{
type:"text",
text:`The agent created a new plan that was approved by the user. Please go ahead and start implementing this plan and use the todo tool if applicable. We are no longer in plan mode and you do not need to use the exit_plan_mode tool.\n\nUser-approved plan:${result.exitPlanModeInput.plan}`
}]
};
}

return{
tool_use_id: toolUseId,
type:"tool_result",
content: result.content
};
},

// UI渲染(如果需要)
renderToolResultMessage(result, toolUseId, context) {
// 渲染逻辑实现
return{
type:"tool_result_display",
data: result,
toolUseId: toolUseId
};
}
};

十、架构优势与技术创新

10.1 分层多Agent架构的技术优势

  1. 1.完全隔离的执行环境
  • • 每个SubAgent在独立上下文中运行,防止相互干扰
  • • 资源限制和权限控制确保系统稳定性
  • • 错误隔离机制防止单点故障影响整体系统
  • 2.智能并发调度
    • • 支持多Agent并发执行,显著提高处理效率
    • • 动态负载均衡和资源分配
    • • 基于工具安全性的智能分组执行
  • 3.弹性错误处理
    • • 多层错误捕获和恢复机制
    • • 自动模型fallback和重试逻辑
    • • 优雅的中断处理和资源清理
  • 4.高效结果合成
    • • 智能的多Agent结果聚合算法
    • • 冲突检测和一致性验证
    • • 专门的合成Agent生成统一结果

    10.2 安全机制的创新设计

    1. 1.多层权限控制
    • • 工具白名单和黑名单机制
    • • 细粒度的参数验证和限制
    • • 动态权限评估和调整
  • 2.递归调用防护
    • • 严格禁止Task工具的递归调用
    • • 调用深度限制和循环检测
    • • 智能的调用栈管理
  • 3.资源使用监控
    • • 实时的token使用量跟踪
    • • 执行时间和工具调用次数限制
    • • 内存和网络资源的安全控制

    10.3 性能优化策略

    1. 1.上下文压缩技术
    • • 智能的对话历史压缩
    • • 关键信息保留和冗余消除
    • • 动态压缩阈值调整
  • 2.并发执行优化
    • • 基于工具特性的并发安全评估
    • • 动态并发度调整
    • • 负载均衡和任务调度
  • 3.缓存和复用机制
    • • 模型响应缓存
    • • 工具结果缓存和复用
    • • 上下文片段的智能复用

    十一、实际应用场景分析

    11.1 复杂代码分析场景

    在代码分析任务中,Task工具展现出强大的能力:

    // 示例:复杂项目架构分析
    constcodeAnalysisTask = {
    description:"Analyze project architecture",
    prompt:`Analyze the architecture of this large codebase:
    1. Identify main components and their relationships
    2. Find potential architectural issues
    3. Suggest improvements and best practices
    4. Generate architecture diagrams if possible`
    };

    // Task工具会启动多个SubAgent,每个专注于不同方面:
    // - Agent 1: 组件识别和依赖分析
    // - Agent 2: 代码质量评估
    // - Agent 3: 架构模式识别
    // - Synthesis Agent: 整合所有发现,生成统一报告

    11.2 多文件重构场景

    在大规模重构任务中,并发Agent显著提高效率:

    // 示例:大规模重构任务
    constrefactoringTask = {
    description:"Refactor legacy system",
    prompt:`Refactor this legacy system to modern standards:
    1. Update deprecated APIs and libraries
    2. Improve code structure and patterns
    3. Add proper error handling and logging
    4. Ensure backward compatibility
    5. Update documentation and tests`
    };

    // 多Agent并发处理:
    // - Agent 1: API更新和依赖升级
    // - Agent 2: 代码结构改进
    // - Agent 3: 错误处理和日志系统
    // - Agent 4: 测试和文档更新
    // - Synthesis Agent: 协调更改,确保一致性

    11.3 项目初始化场景

    在项目搭建任务中,Agent协作创建完整的项目结构:

    // 示例:全栈项目初始化
    constprojectInitTask = {
    description:"Initialize fullstack project",
    prompt:`Create a complete fullstack web application:
    1. Set up backend API with database
    2. Create frontend with modern framework
    3. Configure CI/CD pipeline
    4. Set up monitoring and logging
    5. Write comprehensive documentation`
    };

    // Agent分工合作:
    // - Agent 1: 后端API和数据库设计
    // - Agent 2: 前端应用和UI组件
    // - Agent 3: DevOps和部署配置
    // - Synthesis Agent: 整合所有组件,确保整体一致性

    十二、技术实现细节补充

    12.1 上下文压缩算法

    Claude Code实现了智能的上下文压缩算法,保持对话的连贯性:

    // 上下文压缩器实现(基于wU2函数分析)
    classConversationCompressor{
    constructor() {
    this.compressionThreshold=40000; // k11常量
    this.minCompressionSize=1000; // Uu常量
    this.preservedSections= [
    'user_instructions',
    'recent_tool_results',
    'current_task_context',
    'error_messages'
    ];
    }

    asynccompressConversation(messages, context) {
    consttotalLength =this.calculateTokenLength(messages);

    if(totalLength <this.compressionThreshold) {
    return{ messages,wasCompacted:false};
    }

    // 提取关键信息
    constsummary =awaitthis.generateSummary(messages);
    constpreservedMessages =this.extractPreservedMessages(messages);

    // 创建压缩后的消息结构
    constcompressedMessages = [
    this.createSummaryMessage(summary),
    ...preservedMessages
    ];

    return{
    messages: compressedMessages,
    wasCompacted:true
    };
    }

    asyncgenerateSummary(messages) {
    constsections =this.extractConversationSections(messages);

    return{
    primaryRequest: sections.userRequests.join('\n'),
    technicalConcepts: sections.techTerms,
    fileReferences: sections.fileOperations,
    errorsAndFixes: sections.errorHistory,
    problemSolving: sections.solutions,
    pendingTasks: sections.todoItems,
    currentWork: sections.activeContext
    };
    }
    }

    12.2 模型Fallback机制

    实现了智能的模型降级机制,确保服务连续性:

    // 模型Fallback控制器(基于nO函数中的fallback逻辑)
    classModelFallbackController{
    constructor() {
    this.fallbackChain= [
    'claude-3-opus-20240229',
    'claude-3-sonnet-20240229',
    'claude-3-haiku-20240307'
    ];
    this.retryCount=newMap();
    this.maxRetries=3;
    }

    asynchandleModelError(error, currentModel, context) {
    if(!(errorinstanceofModelFallbackError)) {
    throwerror;
    }

    constfallbackModel =this.getFallbackModel(currentModel);
    if(!fallbackModel) {
    thrownewError('No fallback model available');
    }

    // 记录fallback事件
    this.recordFallbackEvent(currentModel, fallbackModel, context);

    // 更新上下文中的模型
    context.options.mainLoopModel= fallbackModel;

    returnfallbackModel;
    }

    getFallbackModel(currentModel) {
    constcurrentIndex =this.fallbackChain.indexOf(currentModel);
    if(currentIndex === -1|| currentIndex ===this.fallbackChain.length-1) {
    returnnull;
    }
    returnthis.fallbackChain[currentIndex +1];
    }
    }

    12.3 资源监控系统

    实现了全面的资源使用监控:

    // 资源监控系统(基于代码分析推断)
    classResourceMonitoringSystem{
    constructor() {
    this.metrics= {
    tokenUsage:newMap(), // agentId -> token count
    executionTime:newMap(), // agentId -> execution time
    toolCallCount:newMap(), // agentId -> tool call count
    errorCount:newMap(), // agentId -> error count
    memoryUsage:newMap() // agentId -> memory usage
    };

    this.limits= {
    maxTokensPerAgent:100000,
    maxExecutionTimeMs:300000,
    maxToolCallsPerAgent:50,
    maxMemoryMB:512
    };

    this.alerts= [];
    }

    recordMetric(agentId, metricType, value) {
    if(!this.metrics[metricType]) {
    this.metrics[metricType] =newMap();
    }

    constcurrent =this.metrics[metricType].get(agentId) ||0;
    constnewValue = current + value;
    this.metrics[metricType].set(agentId, newValue);

    // 检查限制
    this.checkLimits(agentId, metricType, newValue);
    }

    checkLimits(agentId, metricType, currentValue) {
    constlimitKey =`max${metricType.charAt(0).toUpperCase() + metricType.slice(1)}`;
    constlimit =this.limits[limitKey];

    if(limit && currentValue > limit) {
    constalert = {
    agentId,
    metricType,
    currentValue,
    limit,
    timestampate.now(),
    severity:'high'
    };

    this.alerts.push(alert);
    this.handleLimitExceeded(alert);
    }
    }

    handleLimitExceeded(alert) {
    console.warn(`Resource limit exceeded for agent${alert.agentId}:${alert.metricType}=${alert.currentValue}>${alert.limit}`);

    // 触发限制处理逻辑
    if(alert.metricType==='tokenUsage') {
    this.triggerContextCompression(alert.agentId);
    }elseif(alert.metricType==='executionTime') {
    this.requestAgentTermination(alert.agentId);
    }
    }

    getResourceSummary() {
    constsummary = {};

    for(const[metricType, agentMap]ofObject.entries(this.metrics)) {
    summary[metricType] = {
    total:Array.from(agentMap.values()).reduce((sum, val) =>sum + val,0),
    average:Array.from(agentMap.values()).reduce((sum, val) =>sum + val,0) / agentMap.size,
    max:Math.max(...Array.from(agentMap.values())),
    agents: agentMap.size
    };
    }

    return{
    metrics: summary,
    alerts:this.alerts,
    timestampate.now()
    };
    }
    }

    结论

    Claude Code的分层多Agent架构代表了AI编程助手领域的重要技术创新。通过深度逆向工程分析,我们完整还原了其核心技术实现,包括:

    核心技术成就

    1. 1.完整的Agent隔离机制:每个SubAgent在独立上下文中执行,实现了真正的隔离和安全性
    2. 2.智能的并发调度系统:UH1函数实现的并发执行调度器,支持高效的多Agent协作
    3. 3.sophisticated的工具权限控制:多层权限验证和安全策略,防止权限滥用和安全风险
    4. 4.innovative的结果合成机制:KN5函数实现的智能结果聚合,确保多Agent结果的一致性
    5. 5.robust的错误处理和恢复:完整的错误隔离、模型fallback和资源清理机制

    架构创新价值

    1. 1.可扩展性:模块化设计支持灵活的Agent扩展和工具集成
    2. 2.可靠性:多层错误处理和资源限制确保系统稳定运行
    3. 3.高效性:智能并发调度和上下文压缩显著提升性能
    4. 4.安全性:comprehensive的权限控制和隔离机制保障系统安全
    5. 5.可维护性:清晰的架构分层和模块化设计便于维护和升级

    技术影响和意义

    Claude Code的分层多Agent架构为AI编程助手的发展树立了新的标杆,其技术创新对整个行业具有重要的参考价值:

    1. 1.多Agent协作模式:展示了如何在AI系统中实现高效的多Agent协作
    2. 2.安全隔离设计:提供了AI系统安全隔离的最佳实践
    3. 3.资源管理策略:示范了大规模AI应用的资源管理和优化方法
    4. 4.错误处理机制:建立了resilient AI系统的错误处理标准

    这种先进的架构设计不仅解决了复杂任务处理的技术挑战,也为未来AI编程助手的发展指明了方向,具有重要的技术价值和实践意义。


    本文档基于对Claude Code源代码的完整逆向工程分析,通过系统性地分析混淆代码、运行时行为和架构模式,准确还原了分层多Agent架构的完整技术实现。所有分析结果都基于实际代码证据,为理解现代AI编程assistant的底层机制提供了详细而准确的技术洞察。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ