Skip to content

fix: reduce stack usage during recursive shell execution - #318

Closed
gautamsirdeshmukh wants to merge 4 commits into
strands-agents:mainfrom
gautamsirdeshmukh:fix/recursive-shell-stack
Closed

gautamsirdeshmukh wants to merge 4 commits into
strands-agents:mainfrom
gautamsirdeshmukh:fix/recursive-shell-stack

Conversation

@gautamsirdeshmukh

@gautamsirdeshmukh gautamsirdeshmukh commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

Finite recursive shell calls can consume a normal 2 MiB worker stack before reaching the configured depth limit. In Box, this leaves too little stack for Cedar and causes policy evaluation to report recursion limit reached and deny commands.

Finish pipeline preparation and builtin I/O setup before executing nested commands. Their large temporary stack frames then return before recursion starts. Construct the remaining large futures outside recursive poll frames and keep the 8 KiB read buffer on the heap. Executable lookup and redirection helpers own their allocation boundaries. Control-flow futures use the same constructor boundary, so even unused branches do not reserve temporary space in each recursive frame.

Execution keeps the same thread, borrowed process, task locals, permission checks, cancellation behavior, and configured depth limit. There is no production stack-growth dependency; stacker is a dev dependency for measuring callback headroom. Native stack use still grows with recursion, so arbitrary nesting is not guaranteed.

Related Issues

Related to strands-agents/staging-boxy#585. Box's diagnostic buffering and exit presentation are separate work.

Documentation PR

None. This changes internal allocation and preserves the public API and configuration.

Type of Change

Bug fix

Testing

On macOS arm64:

  • CARGO_INCREMENTAL=0 cargo +1.98.1 xtask check --rust-only passes with normal debug settings: formatting, clippy, all 2,642 Rust tests, and documentation with warnings denied. No new lint warnings.

  • An isolated Box build completes function depths 40 and 60, eval at its last permitted depth, and recursive find -exec and xargs at depth 31. Each run uses 33 custom policies plus generated policies. No recursion warnings.

  • Regression coverage checks 512 KiB of callback headroom, policy denial, captured execution, the configured depth limit, and cancellation.

  • Nine randomized release benchmark rounds measured 7.4% to 20.4% lower latency than upstream. Depth 60 takes 1.36 ms versus upstream's 1.56 ms; with 100 async resumptions, 3.07 ms versus 3.31 ms. Candidate stacks are 2 MiB; upstream requires 8 MiB for the deep cases. These are local Shell microbenchmarks, not end-to-end Box latency measurements.

  • The callback-headroom regression also passes on x86_64 macOS under Rosetta with normal debug settings. Linux, Windows, Python, Node.js, and WASM were not exercised locally; CI checks the new revision.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)

  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@github-actions github-actions Bot added the bug Something isn't working label Nov 18, 2026
@gautamsirdeshmukh gautamsirdeshmukh changed the title fix: preserve stack space during recursive shell execution fix: reduce stack usage during recursive shell execution Nov 18, 2026
@abhisheksp

Copy link
Copy Markdown
Contributor

Context from the Claude Code case in #323. An embedder runs the shell on a thread with the default 2 MiB stack. There, a plain function recursion n=0; f() { n=$((n+1)); [ $n -lt 60 ] && f; return 0; }; f stopped at depth 36 (release build, macOS arm64), when the embedder's policy evaluation ran out of stack.

#324 removes the recursion that Claude Code's grep and find wrappers caused (command grep called the grep function again). This PR still covers the general case of deep but finite nesting. I am not duplicating it. The one gap I see: max_depth 64 is still not guaranteed to fit the default stack, as this PR's description says.

@gautamsirdeshmukh

Copy link
Copy Markdown
Contributor Author

Closing in favor of #324 and #325

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants