[Bug] Stack Overflow in CLI v2.1.37 - Missing turnCount increment in CZ function

Resolved 💬 3 comments Opened Feb 12, 2026 by sgInnora Closed Feb 15, 2026

Bug Report: Stack Overflow in Claude Code CLI v2.1.37

Summary

RangeError: Maximum call stack size exceeded in cli.js CZ function due to missing turnCount increment in recursive calls.

Environment

  • Claude Code: v2.1.37
  • Node.js: v20.19.0
  • Platform: macOS Darwin 24.6.0

Root Cause

The CZ function (main query handler) has recursive calls that do not increment turnCount, causing maxTurns checks to never trigger:

  1. maxOutputTokens recovery path: turnCount passed as j instead of j+1
  2. Stop hook message path: turnCount not incremented

When these conditions persist (e.g., repeated API errors, stop hooks returning messages), the recursion becomes infinite.

Code Evidence (from error stack)

// Path 1: maxOutputTokens recovery - NO turnCount increment
yield*CZ({...P,...k,V1}, ..., turnCount: j, ...);  // BUG: should be j+1

// Path 2: Stop hook messages - NO turnCount increment
yield*CZ({...P,...k,...W}, ..., stopHookActive: true, ...);  // BUG: missing turnCount

// Path 4: Main loop - CORRECT increment
yield*CZ({...P,...k,...y}, ..., turnCount: j1, ...);  // OK: j1 = j + 1

Reproduction Conditions

  • Long conversation history (triggers compact)
  • Multiple background agents running
  • Stop hooks returning messages
  • API errors requiring recovery

Suggested Fix

Ensure all recursive calls to CZ increment turnCount or have alternative termination conditions:

// Fix for path 1
yield*CZ({...P,...k,V1}, ..., turnCount: j + 1, maxOutputTokensRecoveryCount: X + 1, ...);

// Fix for path 2
yield*CZ({...P,...k,...W}, ..., stopHookActive: true, turnCount: j + 1, ...);

Impact

  • Users with long conversations or multiple agents hit this frequently
  • Workaround: restart CLI, /compact, or increase Node stack size

Attachments

  • Full error stack trace available
  • User context: nora-80b-runpod project, 30+ min conversation, multiple agents

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗