You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(ai): keep token usage and partial response when the tool loop fails - #3776
A tool loop that fails now keeps the token usage and the response text of the rounds that completed. This covers all three failure returns in executeWithTools: a provider error, an infrastructure-level tool error, and running out of tool iterations.
New table test TestExecutor_ExecuteWithTools_FailureKeepsUsage, one case per path.
why
Only setFinalResult copied the accumulated usage into the result, so atmos ai exec --format json reported "tokens" as all zeros and an empty "response" on failure, although the completed rounds had been billed. Hitting the iteration limit is the worst case: every round is paid for and none is reported.
tool_calls was already preserved on these paths, so the JSON output now agrees with itself.
text and markdown output are unchanged, since both print only the error when there is one. Failed turns are still not saved to the session.
references
go test ./pkg/ai/executor/ ./pkg/ai/formatter/ fails without the change (zero tokens, empty response) and passes with it; go test ./cmd/ai/ passes.
Not run against a live provider.
This change was made with Claude Code; the tests above were run before opening the PR.
Summary by CodeRabbit
Bug Fixes
When an AI operation fails after producing a response, its available text and token usage are now preserved. This applies to provider errors, tool-related failures, and iteration-limit failures, so partial results and usage details remain available even when an operation does not complete successfully. Successful operations continue to report their response and usage as before.
Latest completed GitHub Actions runs for 7f6ac48a949f.
PR wall-clock time: 47m 33s
Aggregate runner time: 16h 00m 38s
Included: 12 workflows, 129 jobs (including matrix jobs)
Wall-clock time spans the earliest included workflow creation through the latest completion. Aggregate runner time adds each job's execution time, so concurrent jobs are counted separately.
Reviewing files that changed from the base of the PR and between 32406bd and 7f6ac48.
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
No actionable comments were generated in the recent review.
Walkthrough
The executor now preserves accumulated response text and usage when a provider error, infrastructure-level tool error, or iteration limit ends a tool loop.
Changes: Tool-Loop Failure Results
Layer / File(s)
Summary
Record results on failure pkg/ai/executor/executor.go, pkg/ai/executor/executor_test.go
A shared helper records response text and token counts. The executor uses it on the three failure paths. Tests verify that response text and token and cache usage from a completed round remain available after failure.
This change keeps token usage and partial response text in results when the tool loop fails, which fixes zero-token JSON output after billed rounds. The change is localized and tested, so merge-readiness risk is minimal. The release milestone label still needs to be set.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the main change: token usage and partial response are kept when the tool loop fails. The raw summary and objectives both describe this fix in executor.go and its new test. The title is…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the main change: token usage and partial response are kept when the tool loop fails. The raw summary and objectives both describe this fix in executor.go and its new test. The title is 71 characters and 12 words, so it is concise. It follows the conventional commit style used in the repository.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Fix failing CI checks
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between e488cdf and 32406bd.
Files selected for processing: 2
pkg/ai/executor/executor.go
pkg/ai/executor/executor_test.go
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.
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
patchA minor, backward compatible changequality:readyAll Quality Gate requirements pass for the current PR headsize/sSmall size PR
3 participants
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.
what
executeWithTools: a provider error, an infrastructure-level tool error, and running out of tool iterations.TestExecutor_ExecuteWithTools_FailureKeepsUsage, one case per path.why
setFinalResultcopied the accumulated usage into the result, soatmos ai exec --format jsonreported"tokens"as all zeros and an empty"response"on failure, although the completed rounds had been billed. Hitting the iteration limit is the worst case: every round is paid for and none is reported.tool_callswas already preserved on these paths, so the JSON output now agrees with itself.textandmarkdownoutput are unchanged, since both print only the error when there is one. Failed turns are still not saved to the session.references
go test ./pkg/ai/executor/ ./pkg/ai/formatter/fails without the change (zero tokens, empty response) and passes with it;go test ./cmd/ai/passes.This change was made with Claude Code; the tests above were run before opening the PR.
Summary by CodeRabbit