feat(scan): report token budget stop in JSON summary.budget_exceeded - #25
feat(scan): report token budget stop in JSON summary.budget_exceeded#25chethanuk wants to merge 1 commit into
Conversation
scan already detects the aggregate token-budget stop (it prints the
"[ocr] token budget reached" line and records a token_budget_reached
warning) but BudgetExceeded() was hard-coded false, so
summary.budget_exceeded never appeared in `ocr scan --format json`.
The write goes next to `budgetHit = true` in dispatchBatch's per-file
gate. That is the only site that sets budgetHit, and it covers all three
exits that carry the stop out of dispatchBatch: normal return, ctx-cancel
return, and the caller's `if budgetHit { break }`. Setting it at the
dispatchSubtasks break instead would lose it on the ctx-cancel path.
Plain bool, no mutex: dispatchBatch's loop is the only writer, it runs on
the caller's goroutine, and the value is read by emitRunResult after Run
returns. The spawned subtask goroutines never touch it. Matches the
existing internal/agent.Agent.budgetExceeded field.
Status and exit code are untouched — reaching the budget is a controlled
truncation, so out.Status stays the warning-derived value.
🤖 CodeAnt AI — Review Status
|
Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThe scan agent now records aggregate token-budget exhaustion during dispatch. Tests verify the state through direct dispatch and end-to-end JSON scan reporting, including warnings and optional field behavior. ChangesScan budget reporting
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant TestScanBudgetJSON
participant ScanAgent
participant FixedUsageFakeClient
participant JSONResultEmitter
TestScanBudgetJSON->>ScanAgent: run scan
ScanAgent->>FixedUsageFakeClient: request analysis
FixedUsageFakeClient-->>ScanAgent: return fixed token usage
ScanAgent->>JSONResultEmitter: emit scan result
JSONResultEmitter-->>TestScanBudgetJSON: validate status and warnings
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
User descriptionDescription
The signal already existed inside scan; it just never reached the result provider. The flag is set where No mutex or atomic: Scan's status semantics are unchanged: it publishes no run manifest, so LimitationThe new CLI-level test drives the real Type of Change
How Has This Been Tested?
Checklist
Related Issuescloses alibaba#771 CodeAnt-AI DescriptionReport scan token-budget stops in JSON summaries What Changed
Impact
💡 Usage GuideChecking Your Pull RequestEvery time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later. Talking to CodeAnt AIGot a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask: This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code. ExamplePreserve Org Learnings with CodeAntYou can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input: This helps CodeAnt AI learn and adapt to your team's coding style and standards. ExampleRetrigger reviewAsk CodeAnt AI to review the PR again, by typing: Check Your Repository HealthTo analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health. |
Description
ocr scan --format json --max-tokens-budget Nalready detects the aggregate budget stop — it records atoken_budget_reachedwarning and stops dispatching — butsummary.budget_exceededstayedfalse, becausescan.Agent.BudgetExceeded()was hardcoded toreturn false(internal/scan/agent.go:229). Automation consuming the JSON had to parse the warning list to tell a complete scan from one truncated by the budget.The signal already existed inside scan; it just never reached the result provider.
The flag is set where
budgetHitis set, not at thebreak.dispatchBatchhas three exits that carrybudgetHit, and the ctx-cancel exit at:638reachesdispatchSubtasks'err != nilreturn at:528— which fires before theif budgetHitcheck at:547. Writing the flag at the break would silently lose it on cancel-after-budget-hit. One write at:628covers all three exits.No mutex or atomic:
budgetHit = trueruns indispatchBatch's own loop body, beforesem <- struct{}{}and outside the worker closure, anddispatchBatchhas one caller in a sequential batch loop — one writer on one goroutine, read only afterRunreturns.internal/agent/agent.go:176stores the same flag as a plainboolon the diff-review path.make testruns with-race, so this is enforced rather than argued.Scan's status semantics are unchanged: it publishes no run manifest, so
statusstayssuccess, andomitemptykeepsbudget_exceededout of the JSON entirely when the gate does not trip.Limitation
The new CLI-level test drives the real
*scan.Agentand the realemitRunResult, not a spawnedocrbinary — no test in this repo spawns it, and doing so would need live provider credentials. The flag-parsing layer betweenparseScanFlagsandscan.Args.MaxTokensBudgetis covered separately bycmd/opencodereview/scan_cmd_test.go:145.Type of Change
How Has This Been Tested?
make testpasses locallymake test(with-race) andmake checkboth pass.cmd/opencodereview/scan_budget_json_test.go(new) — drives a real*scan.Agentover eight fixture files through the real budget gate andemitRunResult, assertingsummary.budget_exceeded == trueplus thetoken_budget_reachedwarning atMaxTokensBudget=120_000, and that the raw JSON contains nobudget_exceededkey at all when no budget is set. Both subtests also assertstatus == "success", guarding against this leaking into scan's status semantics.internal/scan/budget_exceeded_test.go(new) — table-driven, both directions, throughdispatchSubtasks.a.budgetExceeded = trueline fails both.Checklist
go fmt,go vet)budget_exceededis currently undocumented underpages/for the review path too, so documenting it belongs in a separate docs PR rather than this one.Related Issues
closes alibaba#771