Repository navigation
fix(chat): hard-stop runaway tool loops per turn - #1752
Merged
Merged
Conversation
Event-task CI watches thrashing through hundreds of bash polls burned multi-hour system-actor spend because soft-yield progress bypassed the slice budget. Cap tool calls per turn and fail closed with the shared execution-limit path. Co-Authored-By: David Cramer <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
dcramer
marked this pull request as ready for review
September 1, 2026 17:18
Drop the base-error class hierarchy for a type guard, strip thrash/budget jargon, and set the limit from production dispatch tool-call distributions. Co-Authored-By: David Cramer <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit c64b64b. Configure here.
Recounting tool calls from live history undercounts after compaction or handoff rewrites the transcript. Store a durable cumulative total on the turn cursor and charge against it before each tool admission.
Put admit/check/charge on resume state, drop the unused message recount helper, and keep checkpoint writes on one shared base path. Co-Authored-By: David Cramer <[email protected]>
Hard stops already had the right copy on resume, but first-turn mention and subscribed-message catch paths always posted the generic internal error. Share one reply picker and pass the failure cause through. Co-Authored-By: David Cramer <[email protected]>
Import the generic failure reply directly, read the tool-call limit from bot config inside admitToolCall, and keep cause matching to one wrap. Co-Authored-By: David Cramer <[email protected]>
dcramer
approved these changes
Sep 1, 2026
dcramer
added a commit
that referenced
this pull request
Sep 7, 2026
## Summary `MAX_TOOL_CALLS_PER_TURN` (added in #1752, shipped 2026-09-01) is a single global cap that fires for both runaway automation and legitimate long-form human turns. This turn hit it: [JUNIOR-96](https://sentry.sentry.io/issues/7709966903/), `event_id=dbb4dd6a38b64357b739f12e5cdd1499`. ## Evidence - `JUNIOR-96` fired 4 times in the 7 days since the cap shipped, all on human-initiated turns (`app.actor.type:user`), none on automated/resource-event turns — the case #1752 targeted. - ~241 sampled human-run traces in that window (~1,129 total invoke_agent traces incl. sub-agents), so this is a low but real tax on legitimate work. - 30d `gen_ai.execute_tool` span counts per trace show p90 ≈ 192, p95 ≈ 192, p99 ≈ 256 — 250 sits just under the p99 tail instead of at the p50-adjacent 150. ## Change Raise `MAX_TOOL_CALLS_PER_TURN` from 150 to 250 (config + tests only). Left the mechanism (durable `cumulativeToolCallCount`, fail-closed behavior) untouched from #1752. ## Residual risk Same global cap for automated and human turns — doesn't distinguish runaway CI-watch loops from long-form human work. A follow-up could split limits by actor if this keeps firing. <!-- junior-request-attribution:start --> Requested by **David Cramer**. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:slack%3AC0B595QDZLL%3A1788803900.539279 --> -- [View Junior Session](https://junior-prod.sentry.dev/conversations/slack%3AC0B595QDZLL%3A1788803900.539279) [[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0B595QDZLL%3A1788803900.539279/?project=4510944073809921) <!-- junior-session-footer:end --> Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: David Cramer <[email protected]>
dcramer
added a commit
that referenced
this pull request
Oct 15, 2026
Child agents doing real work keep hitting the 250 tool-call cap from #1752. The turn fails, the parent sees "model failed", and the child's work is lost. The cap was meant to stop runaway loops, and what those loops really cost was model spend. - **Spend limit:** each turn can spend `maxTurnCostUsd` of model cost, $100 by default, across all its execution slices. Retries that Junior throws away count too. The tool-call cap goes from 250 to 1,000 and only matters for models without cost data. - **Wrap up instead of failing:** the first model step past a limit has its tool calls blocked with the same message as a tool that `disableTools` (#2148) disabled. The tool list stays the same, so the provider cache stays valid. The model replies with what it finished, what is left, and how to continue, and a child agent sends that reply to its parent as a normal result. Any later tool call in the turn fails it. The turn checkpoint saves the wrap-up, so a later slice does not wrap up again. - **Child limit failures are final:** a non-final attempt used to rethrow the limit error. The retry then marked the child's Turn as "lost its worker before it could pause", so the parent never saw the limit. The $100 default comes from Sentry span cost estimates: one child agent that made 250 tool calls cost about $50. Not in this PR: - One budget shared by the root conversation and its child agents. Each child agent gets its own budget. - The root and scheduler path in `task-execution/conversation-turn.ts`, which still retries limit failures. The new subagent eval uses a 3-call limit to stand in for a spent budget. Context: JUNIOR-96, event `49c00ccc6e12485e9f4b18a97c3010d6`. <!-- junior-request-attribution:start --> via **David Cramer**. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:slack%3AC0B595QDZLL%3A1792078672.880799 --> -- [View Junior Session](https://junior-prod.sentry.dev/conversations/slack%3AC0B595QDZLL%3A1792078672.880799) [[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0B595QDZLL%3A1792078672.880799/?project=4510944073809921) <!-- junior-session-footer:end --> --------- Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: David Cramer <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Event-task CI watches (notably
getsentry/sentry#121448) kept thrashing bash polls for hours as system-actor dispatches. Soft-yield progress bypassed the existing slice budget, so unattributed spend spiked (~5.68B input tokens / multi-hour loops on that path).Root cause
One turn already has a 100-slice resume budget, but soft yield with progress can keep advancing without hitting that stop. There was no hard tool-call budget, so a single turn could execute hundreds of tool calls. A first pass counted tool calls from the live turn message slice; compaction and handoff rewrite history and reset
turnStartMessageIndexto0, so that recount undercounted and the hard cap could reset mid-turn.Fix
maxToolCallsPerTurn(150) on bot config, set from production dispatch tool-call distributions.cumulativeToolCallCounton the Redis turn cursor and charge +1 on each admittedbeforeToolCall.pendingPiHookError+block/terminateso Pi does not swallow the limit into a recoverable tool error.Residual risk
This stops one runaway turn, including after history replacement. Separate event-task fires still each get a fresh budget; follow-up could add task/dispatch-level spend or duration caps if needed.
Requested by Junior system actor
resource-event.--
View Junior Session [Sentry]