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 |
| Key | The callable, not its display name |
| Check | In openprogram/runtime/agent/agent_method.py, on entry: depth at the limit raises, otherwise depth + 1 |
| Restore | The token resets on return and on exception |
| Scope | One process. A function started in a subprocess begins at zero. |
| Chain | Result |
|---|---|
| A → A → A … | Stops at the sixth entry |
| A → B | Counted 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_call | Own name is not denied |
|---|---|
test_depth_increments_during_call | Depth is 1 inside a one-level call |
test_depth_backstop_raises_past_limit | Raises with the name and the limit; the body ran exactly 5 times |
test_distinct_subcalls_not_collateral_damage | A → B do not share a count |
test_depth_restored_after_return | Depth returns to its previous value |
test_depth_restored_after_exception | The 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.
Last updated · 2026-11-25