Skip to content

fix(agent): max-turns termination correctly classified as HTTP 422 (#361) - #365

Merged
vybe merged 1 commit into
mainfrom
feature/361-max-turns-error-fix
Apr 17, 2026
Merged

vybe merged 1 commit into
mainfrom
feature/361-max-turns-error-fix

Conversation

@vybe

@vybe vybe commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixed max-turns termination being misclassified as authentication failure (HTTP 503 → HTTP 422)
  • Raised max_turns_task default from 20 to 50 to accommodate complex multi-step tasks
  • Added unit tests for error classification

Root Cause

When Claude Code hits max_turns, it returns is_error=true with empty result. The code fell through to _diagnose_exit_failure() which returned a message containing "setup-token", triggering auth failure detection on its own diagnosis message.

Changes

File Change
claude_code.py Detect terminal_reason="max_turns" before fallback, return HTTP 422
guardrails-baseline.json max_turns_task: 20 → 50
test_error_classification.py 7 new unit tests for max_turns detection
parallel-headless-execution.md Updated HTTP status in docs (500 → 422)

Test Plan

  • Unit tests pass: pytest unit/test_error_classification.py unit/test_guardrails.py -v (57 passed)
  • Manual: Run a scheduled task that exceeds 50 turns and verify HTTP 422 response

Closes #361

🤖 Generated with Claude Code

)

When Claude Code hits the max_turns limit, it was being misclassified as
an authentication failure (HTTP 503) because the fallback diagnosis
message contained "setup-token" which triggered auth failure detection.

Changes:
- Detect terminal_reason="max_turns" and subtype="error_max_turns" in
  result messages before falling through to generic error handling
- Return HTTP 422 with clear "Task exceeded turn limit" message
- Raise max_turns_task default from 20 to 50 (matches max_turns_chat)
- Add unit tests for max_turns error classification
- Update feature flow documentation

Closes #361

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Max-turns termination misclassified as authentication failure

1 participant