Two budgets, both measured rather than guessed: MAX_NESTING_DEPTH=64
gates the five recursion cycles in the parser (debug frames run to
~50KB; 64 peaks at ~3.9MiB against the 8MiB stack), and
MAX_CHAIN_LENGTH=256 charges postfix segments and binary/inside
operators, whose loops deepen the AST without parser recursion.
Restore-by-assignment makes the chain budget correct on every early
return. Invariant: AST depth on any root-to-leaf path <= 64+256,
so the formatter's structural recursion is bounded too.
Deep or chain-heavy inputs now get a clean "nesting too deep" /
"expression chain too long" parse error with position, exit 2, batch
unaffected -- replacing an uncatchable stack-overflow abort that
killed whole runs (3KiB of a.b.b... sufficed before).
Round 1 was rejected in review: QA found postfix chains ungated and
the leak test unable to detect missing releases (proven by sabotage).
The rework widened the finding to binary/inside chains, added the
second budget, and made leak tests sabotage-complete (7/7); re-QA
attacked the additive-composition invariant and found no hole, with
no acceptance regressions (128-term sums, 10k statements still fine).
Dev + rework: Opus agent; QA + re-QA: Sonnet agent; full history in
docs/features/recursion-limit/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>