|
@@ -278,7 +278,10 @@ class AgentRunner:
|
|
|
self,
|
|
self,
|
|
|
trace_id: str,
|
|
trace_id: str,
|
|
|
) -> tuple[str, ResourceBudget] | None:
|
|
) -> tuple[str, ResourceBudget] | None:
|
|
|
- """Resolve the immutable Recursive root budget for one local Trace."""
|
|
|
|
|
|
|
+ """解析本地 Trace 所属 Recursive 根树的不可变预算快照。
|
|
|
|
|
+
|
|
|
|
|
+ 由模型调用、工具用量记账和子 Agent 创建入口调用;Legacy Trace 直接返回无预算。
|
|
|
|
|
+ """
|
|
|
if not self.trace_store:
|
|
if not self.trace_store:
|
|
|
return None
|
|
return None
|
|
|
trace = await self.trace_store.get_trace(trace_id)
|
|
trace = await self.trace_store.get_trace(trace_id)
|
|
@@ -306,7 +309,10 @@ class AgentRunner:
|
|
|
fail_on_post_response_exhaustion: bool = False,
|
|
fail_on_post_response_exhaustion: bool = False,
|
|
|
**kwargs: Any,
|
|
**kwargs: Any,
|
|
|
) -> Dict[str, Any]:
|
|
) -> Dict[str, Any]:
|
|
|
- """Call an LLM and atomically account for Recursive tree usage."""
|
|
|
|
|
|
|
+ """Recursive 树中统一的 LLM 调用和资源记账入口。
|
|
|
|
|
+
|
|
|
|
|
+ Agent 主循环、上下文压缩、图片描述和 Validator 共用;请求前预留次数,响应后记账。
|
|
|
|
|
+ """
|
|
|
llm = call or self.llm_call
|
|
llm = call or self.llm_call
|
|
|
if not llm:
|
|
if not llm:
|
|
|
raise ValueError("llm_call function not provided")
|
|
raise ValueError("llm_call function not provided")
|
|
@@ -342,7 +348,10 @@ class AgentRunner:
|
|
|
trace_id: str,
|
|
trace_id: str,
|
|
|
tool_usage: Dict[str, Any],
|
|
tool_usage: Dict[str, Any],
|
|
|
) -> None:
|
|
) -> None:
|
|
|
- """Account for a tool-owned model call when the tool reports usage."""
|
|
|
|
|
|
|
+ """登记工具内部自行发起的模型用量。
|
|
|
|
|
+
|
|
|
|
|
+ Agent 主循环在 Tool Result 携带 ``tool_usage`` 时调用,并计入同一棵 Recursive 树。
|
|
|
|
|
+ """
|
|
|
resolved = await self._resource_budget_for_trace(trace_id)
|
|
resolved = await self._resource_budget_for_trace(trace_id)
|
|
|
if resolved is None:
|
|
if resolved is None:
|
|
|
return
|
|
return
|
|
@@ -370,7 +379,10 @@ class AgentRunner:
|
|
|
deterministic_failure: Optional[Dict[str, Any]] = None,
|
|
deterministic_failure: Optional[Dict[str, Any]] = None,
|
|
|
root_validator: bool = False,
|
|
root_validator: bool = False,
|
|
|
) -> ValidationRun:
|
|
) -> ValidationRun:
|
|
|
- """Run one framework-owned, tool-free validation Trace."""
|
|
|
|
|
|
|
+ """运行一次由框架控制、不带工具的独立验收 Trace。
|
|
|
|
|
+
|
|
|
|
|
+ 子 Agent 结果汇合或根 Agent 候选答案完成时调用,返回权威 ``ValidationResult``。
|
|
|
|
|
+ """
|
|
|
if not self.trace_store or not self.llm_call:
|
|
if not self.trace_store or not self.llm_call:
|
|
|
raise RuntimeError("Validator requires trace_store and llm_call")
|
|
raise RuntimeError("Validator requires trace_store and llm_call")
|
|
|
evaluated = await self.trace_store.get_trace(evaluated_trace_id)
|
|
evaluated = await self.trace_store.get_trace(evaluated_trace_id)
|
|
@@ -665,8 +677,8 @@ class AgentRunner:
|
|
|
"""
|
|
"""
|
|
|
停止运行中的 Trace
|
|
停止运行中的 Trace
|
|
|
|
|
|
|
|
- 设置取消信号,agent loop 在下一个 LLM 调用前检查并退出。
|
|
|
|
|
- Trace 状态置为 "stopped"。
|
|
|
|
|
|
|
+ Trace API 定位实际 Runner 后调用本方法;Legacy 只停当前 Trace,
|
|
|
|
|
+ Recursive 由 ``request_stop`` 把信号传给当前进程内已登记的子树。
|
|
|
|
|
|
|
|
Returns:
|
|
Returns:
|
|
|
True 如果成功发送停止信号,False 如果该 trace 不在运行中
|
|
True 如果成功发送停止信号,False 如果该 trace 不在运行中
|
|
@@ -674,7 +686,10 @@ class AgentRunner:
|
|
|
return self.request_stop(trace_id)
|
|
return self.request_stop(trace_id)
|
|
|
|
|
|
|
|
def request_stop(self, trace_id: str) -> bool:
|
|
def request_stop(self, trace_id: str) -> bool:
|
|
|
- """同步设置停止信号,供 API 与 stdin 回调共用。"""
|
|
|
|
|
|
|
+ """同步设置停止信号,供 API 与 stdin 回调共用。
|
|
|
|
|
+
|
|
|
|
|
+ Recursive Trace 会沿进程内父子登记表向下遍历,不影响父级或兄弟分支。
|
|
|
|
|
+ """
|
|
|
if trace_id not in self._cancel_events:
|
|
if trace_id not in self._cancel_events:
|
|
|
return False
|
|
return False
|
|
|
|
|
|
|
@@ -700,7 +715,10 @@ class AgentRunner:
|
|
|
parent_trace_id: str,
|
|
parent_trace_id: str,
|
|
|
child_trace_id: str,
|
|
child_trace_id: str,
|
|
|
) -> asyncio.Event:
|
|
) -> asyncio.Event:
|
|
|
- """登记已创建但可能仍在排队的 Recursive 直属孩子。"""
|
|
|
|
|
|
|
+ """登记已创建但可能仍在排队的 Recursive 直属孩子。
|
|
|
|
|
+
|
|
|
|
|
+ ``agent`` 工具预创建孩子后、Runner 启动 Validator 前调用,使父级停止能传递到后代。
|
|
|
|
|
+ """
|
|
|
event = self._cancel_events.setdefault(child_trace_id, asyncio.Event())
|
|
event = self._cancel_events.setdefault(child_trace_id, asyncio.Event())
|
|
|
self._recursive_active_traces[child_trace_id] = event
|
|
self._recursive_active_traces[child_trace_id] = event
|
|
|
self._active_children.setdefault(parent_trace_id, set()).add(child_trace_id)
|
|
self._active_children.setdefault(parent_trace_id, set()).add(child_trace_id)
|
|
@@ -824,7 +842,9 @@ class AgentRunner:
|
|
|
config: RunConfig,
|
|
config: RunConfig,
|
|
|
) -> Tuple[Trace, Optional[GoalTree], int]:
|
|
) -> Tuple[Trace, Optional[GoalTree], int]:
|
|
|
"""
|
|
"""
|
|
|
- 准备 Trace:创建新的或加载已有的
|
|
|
|
|
|
|
+ 准备 Trace:为 ``run`` 选择新建、续跑或回溯路径。
|
|
|
|
|
+
|
|
|
|
|
+ 在 Agent 主循环前调用,并在任何 Trace 操作前校验已废弃的配置项。
|
|
|
|
|
|
|
|
Returns:
|
|
Returns:
|
|
|
(trace, goal_tree, next_sequence)
|
|
(trace, goal_tree, next_sequence)
|
|
@@ -840,7 +860,10 @@ class AgentRunner:
|
|
|
messages: List[Dict],
|
|
messages: List[Dict],
|
|
|
config: RunConfig,
|
|
config: RunConfig,
|
|
|
) -> Tuple[Trace, Optional[GoalTree], int]:
|
|
) -> Tuple[Trace, Optional[GoalTree], int]:
|
|
|
- """创建新 Trace"""
|
|
|
|
|
|
|
+ """创建并持久化一个新根 Trace。
|
|
|
|
|
+
|
|
|
|
|
+ ``run`` 首次执行时调用;Recursive 会在此固化模式、根验收标准和树级预算。
|
|
|
|
|
+ """
|
|
|
# 在任何标题生成/LLM 调用前完成模式校验。
|
|
# 在任何标题生成/LLM 调用前完成模式校验。
|
|
|
policy = policy_from_environment(recursive_revision=2)
|
|
policy = policy_from_environment(recursive_revision=2)
|
|
|
if policy.mode is AgentMode.RECURSIVE:
|
|
if policy.mode is AgentMode.RECURSIVE:
|
|
@@ -916,7 +939,10 @@ class AgentRunner:
|
|
|
self,
|
|
self,
|
|
|
config: RunConfig,
|
|
config: RunConfig,
|
|
|
) -> Tuple[Trace, Optional[GoalTree], int]:
|
|
) -> Tuple[Trace, Optional[GoalTree], int]:
|
|
|
- """加载已有 Trace(续跑或回溯)"""
|
|
|
|
|
|
|
+ """加载已有 Trace,决定续跑或回溯。
|
|
|
|
|
+
|
|
|
|
|
+ ``run`` 携带 ``trace_id`` 时调用;Recursive 只信任持久化模式、预算和协议状态。
|
|
|
|
|
+ """
|
|
|
if not self.trace_store:
|
|
if not self.trace_store:
|
|
|
raise ValueError("trace_store required for continue/rewind")
|
|
raise ValueError("trace_store required for continue/rewind")
|
|
|
|
|
|
|
@@ -1482,7 +1508,10 @@ class AgentRunner:
|
|
|
inject_skills: Optional[List[str]] = None,
|
|
inject_skills: Optional[List[str]] = None,
|
|
|
skill_recency_threshold: int = 10,
|
|
skill_recency_threshold: int = 10,
|
|
|
) -> AsyncIterator[Union[Trace, Message]]:
|
|
) -> AsyncIterator[Union[Trace, Message]]:
|
|
|
- """ReAct 循环"""
|
|
|
|
|
|
|
+ """执行 Agent 的 ReAct 主循环。
|
|
|
|
|
+
|
|
|
|
|
+ ``run`` 在 Trace 准备后调用;Recursive 的预算、停止、工具门禁和根验收都在此串联。
|
|
|
|
|
+ """
|
|
|
trace_id = trace.trace_id
|
|
trace_id = trace.trace_id
|
|
|
runtime_tool_names = self._get_runtime_tool_names(config, trace)
|
|
runtime_tool_names = self._get_runtime_tool_names(config, trace)
|
|
|
tool_schemas = self._get_runtime_tool_schemas(
|
|
tool_schemas = self._get_runtime_tool_schemas(
|
|
@@ -2807,9 +2836,9 @@ class AgentRunner:
|
|
|
goal_tree: Optional[GoalTree],
|
|
goal_tree: Optional[GoalTree],
|
|
|
) -> int:
|
|
) -> int:
|
|
|
"""
|
|
"""
|
|
|
- 执行回溯:快照 GoalTree,重建干净树,设置 head_sequence
|
|
|
|
|
|
|
+ 回溯 Trace:快照 GoalTree,重建干净树并设置 ``head_sequence``。
|
|
|
|
|
|
|
|
- 新消息的 parent_sequence 将指向 rewind 点,旧消息通过树结构自然脱离主路径。
|
|
|
|
|
|
|
+ ``_prepare_existing_trace`` 判定为回溯时调用;Recursive 同时重建待审核和待重规划状态。
|
|
|
|
|
|
|
|
Returns:
|
|
Returns:
|
|
|
下一个可用的 sequence 号
|
|
下一个可用的 sequence 号
|
|
@@ -3848,7 +3877,10 @@ class AgentRunner:
|
|
|
*,
|
|
*,
|
|
|
in_side_branch: bool = False,
|
|
in_side_branch: bool = False,
|
|
|
) -> set[str]:
|
|
) -> set[str]:
|
|
|
- """在基础能力上应用 Recursive 协议状态门禁。"""
|
|
|
|
|
|
|
+ """在 RunConfig 基础能力上应用 Recursive 协议状态门禁。
|
|
|
|
|
+
|
|
|
|
|
+ 主循环每轮调用,使待审核、待执行与报告阶段只暴露当前允许的工具。
|
|
|
|
|
+ """
|
|
|
tool_names = self._get_configured_tool_names(
|
|
tool_names = self._get_configured_tool_names(
|
|
|
config.tools,
|
|
config.tools,
|
|
|
config.tool_groups,
|
|
config.tool_groups,
|
|
@@ -3883,7 +3915,11 @@ class AgentRunner:
|
|
|
in_side_branch: bool = False,
|
|
in_side_branch: bool = False,
|
|
|
runtime_tool_names: Optional[set[str]] = None,
|
|
runtime_tool_names: Optional[set[str]] = None,
|
|
|
) -> List[Dict]:
|
|
) -> List[Dict]:
|
|
|
- """按 Trace 持久化模式和当前协议状态过滤工具 Schema。"""
|
|
|
|
|
|
|
+ """按 Trace 持久化模式和当前协议状态生成工具 Schema。
|
|
|
|
|
+
|
|
|
|
|
+ 主循环把结果交给 LLM;Recursive revision 2 本地委托使用 ``task_brief``,
|
|
|
|
|
+ 仍保留 ``remote_*`` 的 ``task``,Legacy 和 Recursive revision 1 继续使用 ``task``。
|
|
|
|
|
+ """
|
|
|
tool_names = (
|
|
tool_names = (
|
|
|
runtime_tool_names
|
|
runtime_tool_names
|
|
|
if runtime_tool_names is not None
|
|
if runtime_tool_names is not None
|
|
@@ -3933,7 +3969,10 @@ class AgentRunner:
|
|
|
side_branch_ctx: Optional[SideBranchContext],
|
|
side_branch_ctx: Optional[SideBranchContext],
|
|
|
trigger_event: Optional[str],
|
|
trigger_event: Optional[str],
|
|
|
) -> Dict[str, Any]:
|
|
) -> Dict[str, Any]:
|
|
|
- """构建工具上下文;Recursive 内部字段最后覆盖,不能由调用方伪造。"""
|
|
|
|
|
|
|
+ """构建 ToolRegistry 执行时注入的隐藏上下文。
|
|
|
|
|
+
|
|
|
|
|
+ 主循环在 dispatch 前调用;Recursive 权限快照和调度配置最后覆盖,不可伪造。
|
|
|
|
|
+ """
|
|
|
framework_context = {
|
|
framework_context = {
|
|
|
"store": self.trace_store,
|
|
"store": self.trace_store,
|
|
|
"trace_id": trace_id,
|
|
"trace_id": trace_id,
|