Intermittent agent failure: classifyHandoffIfNeeded is not defined

Resolved 💬 5 comments Opened Feb 10, 2026 by flycory Closed Mar 18, 2026

Bug Description

Agents spawned via the Task tool intermittently fail with:

classifyHandoffIfNeeded is not defined

The agent never starts — work is completely lost and API costs are consumed with zero output.

Environment

  • Claude Code version: 2.1.37 (also present in 2.1.27, 2.1.30, 2.1.31, 2.1.34)
  • Platform: macOS 15.6 (Sequoia), Apple Silicon ARM64
  • Binary type: Compiled Bun executable (Mach-O arm64)

Root Cause Analysis

classifyHandoffIfNeeded is called in the agent execution loop during initialization:

sT = await classifyHandoffIfNeeded({
  agentMessages: ET,
  toolPermissionContext: ST.toolPermissionContext,
  abortSignal: IT.abortController.signal,
  isNonInteractiveSession: G.options.isNonInteractiveSession,
  subagentType: R,
  totalToolUseCount: KT.totalToolUseCount
});

The function symbol exists in the binary (strings confirms 3 references across all versions), but it fails to resolve at call time. This is consistent with a module initialization race condition in the Bun runtime — the function is defined but not yet in scope when the agent execution path invokes it.

Reproduction

  • Use the Task tool to spawn any agent type (Explore, Plan, general-purpose, etc.)
  • Failure is intermittent — sometimes agents work fine, sometimes they fail immediately
  • No specific trigger identified; appears to be timing-dependent
  • run_in_background: true does not help — the agent crashes before producing any output

Impact

  • Lost work: Agent performs zero work before crashing, but API tokens are still consumed
  • Cost: Each failed agent invocation bills for the full API round-trip
  • No workaround: The only mitigation is avoiding the Task tool entirely and performing all searches/reads inline
  • Affects all agent types: Explore, Plan, general-purpose, Bash — all go through the same code path

Expected Behavior

classifyHandoffIfNeeded should be reliably in scope when the agent execution loop calls it.

Suggested Fix

Ensure the module exporting classifyHandoffIfNeeded is synchronously imported/required before the agent execution path can invoke it, rather than relying on lazy or async module resolution that may race with agent startup.

View original on GitHub ↗

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