ReAct 步循环:一步内 model→tool→memory→event 的次序

ReAct 步循环:一步内 model→tool→memory→event 的次序 由 Archify 生成的时序图。 run(request) ① runInternal:maxSteps 上限 + memory 校验 ② addUserInput(effectiveInput) ③ STEP_START + BEFORE_TURN ④ buildPrompt → executeModel AgentModelResult:文本 或 toolCalls ⑤ addOutputItems(对齐 provider memoryItems) ⑥ executeToolCalls(串行/并行) AgentToolResult(含 trace 载荷) ⑦ addToolOutput(callId, output) ⑧ TOOL_RESULT + STEP_END → 下一步或 FINAL_OUTPUT 宿主调用方 · run / runStream · 时序参与者 宿主调用方 run / runStream AgentSession · run 入口 · 时序参与者 AgentSession run 入口 ReActRuntime · 步循环主体 · 时序参与者 ReActRuntime 步循环主体 ModelClient · Chat / Responses · 时序参与者 ModelClient Chat / Responses ToolExecutor · 校验 · 串/并行 · 时序参与者 ToolExecutor 校验 · 串/并行 AgentMemory · 输入/输出/结果 · 时序参与者 AgentMemory 输入/输出/结果 Publisher · → Listener/Trace · 时序参与者 Publisher → Listener/Trace 图例 默认消息

步循环骨架

  • • while (step < maxSteps) 单步推进;模型无 toolCalls → publish FINAL_OUTPUT + AFTER_TURN + STEP_END 后返回 AgentResult(COMPLETED)
  • • 每步开头 autoCompactIfNecessary 先按预算裁剪,再 buildPrompt 组装 systemPrompt+instructions+可见工具

工具执行路径

  • • AgentToolCallSanitizer 校验失败 → 直接合成 FAILED result 并 addToolOutput,不进入 executor
  • • parallelToolCalls 且 calls>1 → executeToolCallsInParallel(并发度受 maxParallelToolCalls 封顶;toolCallTimeoutMillis 超时转为 FAILED 结果),否则顺序执行;HostInputRequired 中断时为未配对 call 写占位 output 保持协议可恢复

事件谱系

  • • 顺序固定:STEP_START → BEFORE_TURN → TOOL_CALL → TOOL_RESULT → AFTER_TURN → STEP_END → FINAL_OUTPUT
  • • AgentTraceListener 消费同一事件流建 RUN>STEP>MODEL/TOOL span 树;监听器异常不阻断主流程