OpenProgram Docs

Agentic self-recursion guard

A bare runtime.exec(content=...) gets DEFAULT_TOOLSET = "full", which includes Agent methods exposed as tools, so the inner model can call the method it is running in. The guard is a depth cap: past 5 nested entries of the same method, the call raises RecursionError.

Agent method {name} exceeded max nesting depth 5

How it counts

Limit_MAX_AGENTIC_RECURSION_DEPTH = 5, openprogram/runtime/agent/call_state.py
Counter_recursion_depth, a ContextVar holding a map from the wrapped function to its depth
KeyThe callable, not its display name
CheckIn openprogram/runtime/agent/agent_method.py, on entry: depth at the limit raises, otherwise depth + 1
RestoreThe token resets on return and on exception
ScopeOne process. A function started in a subprocess begins at zero.
ChainResult
A → A → A …Stops at the sixth entry
A → BCounted separately; deep B does not use A's quota
A → B → A → B …Bounded: the map carries down the chain, so the first method to reach 5 stops the cycle

The tool stays visible

The wrapper does not add the method's own name to _current_tool_policy["deny"]. Other uses of that policy are unchanged: source, allow, toolset and the unattended deny.

Behaviour under test

tests/unit/programs/runtime/test_self_recursion_guard.py

test_self_name_NOT_denied_during_callOwn name is not denied
test_depth_increments_during_callDepth is 1 inside a one-level call
test_depth_backstop_raises_past_limitRaises with the name and the limit; the body ran exactly 5 times
test_distinct_subcalls_not_collateral_damageA → B do not share a count
test_depth_restored_after_returnDepth returns to its previous value
test_depth_restored_after_exceptionThe same after a raise

Limits

  • The model can still spend up to 5 nested calls before the cap fires.
  • The guard does not change tool authorization, session graph semantics or the shared system prefix.

Conversation graph

Last updated · 2026-11-25