open-code-review icon indicating copy to clipboard operation
open-code-review copied to clipboard

scan: expose token budget stop state in JSON output

Open Anon2Tokyo opened this issue 3 months ago • 2 comments

Problem

ocr scan --format json --max-tokens-budget ... already has an aggregate token budget gate: when the running token usage plus the next file estimate would exceed the configured budget, scan stops dispatching remaining files and records a token_budget_reached warning.

However, the JSON summary does not expose this state through summary.budget_exceeded=true. For automation users, this makes it hard to distinguish:

  • a complete scan with no budget pressure
  • a partial scan that stopped because the token budget gate was reached

This is mainly a machine-readable reporting gap rather than a request to change scan scheduling behavior.

Why this matters

CI, GitHub Actions, dashboards, or other wrappers may consume --format json to decide whether a scan fully covered the selected files. If budget exhaustion is only visible as a warning item, consumers need to duplicate warning parsing logic instead of relying on the existing summary field.

A clearer contract would make summary.budget_exceeded reflect the aggregate budget gate consistently across review/scan result output.

Current code path

Relevant code:

  • internal/scan/agent.go:616-625: scan detects the aggregate budget gate, records token_budget_reached, sets local budgetHit = true, and breaks dispatch.
  • internal/scan/agent.go:522-544: the outer scan loop receives budgetHit and stops scheduling subsequent batches.
  • internal/scan/agent.go:219-225: BudgetExceeded() is currently hard-coded to return false for scan.
  • cmd/opencodereview/shared.go:354-357: JSON output passes ag.BudgetExceeded() into the output layer.
  • cmd/opencodereview/output.go:316-325: summary.budget_exceeded is populated from that value.

In short, scan has the local budgetHit signal, but that signal is not carried into the result provider state used by JSON output.

Expected behavior

When the aggregate token budget gate stops ocr scan from dispatching remaining files, JSON output should include:

{
  "summary": {
    "budget_exceeded": true
  },
  "warnings": [
    {
      "type": "token_budget_reached"
    }
  ]
}

When scan completes without hitting the aggregate budget gate, the current behavior can stay unchanged: summary.budget_exceeded may remain false or be omitted because of omitempty.

Suggested implementation

A narrow implementation could be:

  1. Add a budgetExceeded bool field to internal/scan.Agent.
  2. When dispatchBatch returns budgetHit == true, set a.budgetExceeded = true before breaking out of the scan loop.
  3. Change scan.Agent.BudgetExceeded() to return a.budgetExceeded instead of always returning false.
  4. Add a focused test for ocr scan --format json --max-tokens-budget ... verifying:
    • warnings still contains token_budget_reached.
    • summary.budget_exceeded == true when the budget gate stops dispatch.
    • normal successful scans still omit or report false for budget_exceeded.

Non-goals

This does not need to change:

  • scan dispatch policy
  • exit-code semantics
  • comment generation
  • warning generation
  • manifest coverage behavior

The requested scope is only to expose an already-detected scan state in the existing machine-readable summary field.

Anon2Tokyo avatar Aug 07 '26 12:08 Anon2Tokyo

I would like to look into this

amh1k avatar Aug 07 '26 15:08 amh1k

The aggregate token budget gate you describe (--max-tokens-budget) is the right concept — stopping when running usage + next chunk exceeds the budget. But it only works if the tool knows the cost of the next chunk before it starts.

The gap: token counts are available after the call, not before. By the time you know the next chunk is 50,000 tokens, you may have already spent $5 on it.

AgentShield approaches this from the pre-dispatch side. Instead of counting tokens after the fact, it evaluates the estimated cost of each API call before it executes:

  • Estimate from prompt length (~4 chars/token) and model rate
  • Check against rules: transaction_limit, session_budget, daily_total, cascade_cost
  • Block or allow in under 1ms
  • The agent never sends the request if it is blocked

For a code review tool specifically, the cascade_cost rule type is relevant: a review call that triggers follow-up analysis calls has a cascade cost (base call + probability of follow-up × follow-up cost). Setting a max_cascade_cost threshold catches multi-step review chains before they compound.

Repo: https://github.com/kindrat86/agentshield

Disclosure: I built this. MIT licensed.

kindrat86 avatar Aug 11 '26 13:08 kindrat86