OpenProgram Docs

Agent 方法自递归防护

不带工具参数的 runtime.exec(content=...) 使用 DEFAULT_TOOLSET = "full",其中包含作为工具暴露的 Agent 方法,因此内层模型可以调用它正在运行的方法。防护是一个深度上限:同一方法嵌套进入超过 5 层时抛出 RecursionError。

Agent method {name} exceeded max nesting depth 5

如何计数

上限_MAX_AGENTIC_RECURSION_DEPTH = 5, openprogram/runtime/agent/call_state.py
计数器_recursion_depth,一个 ContextVar,保存被包装函数到深度的映射
键可调用对象本身,不是显示名
检查在 openprogram/runtime/agent/agent_method.py 入口:已到上限则抛出,否则深度加一
恢复返回和异常时都重置 token
范围单个进程。在子进程中启动的函数从零开始。
调用链结果
A → A → A …第六次进入时停止
A → B分别计数,B 嵌套再深也不占 A 的额度
A → B → A → B …有界:映射沿调用链传递,最先到 5 层的方法终止循环

工具保持可见

包装器不把方法自身的名字加入 _current_tool_policy["deny"]。该策略的其他用途不变:source、allow、toolset 和无人值守时的 deny。

测试覆盖的行为

tests/unit/programs/runtime/test_self_recursion_guard.py

test_self_name_NOT_denied_during_call自身名字不被拒绝
test_depth_increments_during_call一层调用内深度为 1
test_depth_backstop_raises_past_limit抛出时带名字和上限;函数体恰好运行 5 次
test_distinct_subcalls_not_collateral_damageA → B 不共用计数
test_depth_restored_after_return深度回到之前的值
test_depth_restored_after_exception抛出后同样恢复

局限

  • 上限触发前,模型仍可能进行最多 5 层嵌套调用。
  • 防护不改变工具授权、会话图语义或统一的系统前缀。

会话图

Last updated · 2026-11-25