open-code-review/cmd/opencodereview
Nitish Agarwal 2640f5830c
Some checks are pending
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
feat(agent): add token-cost budget guardrails to the review path (#508)
Add a runtime token-budget guardrail to the diff-review path so a large MR
stops itself before runaway cost (issue #409: ~90.4M tokens in one failed
attempt), instead of timing out and losing all structured output.

New `ocr review` flag (mirror scan's proven --max-tokens-budget):
- --max-tokens-budget N : cap aggregate token usage; dispatch stops once the
  running total + a per-file look-ahead would exceed it (0 = unlimited).

Mechanism mirrors scan/agent.go's dispatchBatch gate exactly: read the
existing atomic Runner counters (no duplicate counter — a second one would be
a drift bug) before acquiring the semaphore, and break the dispatch loop on
exceed. In-flight workers are allowed to finish (overrun bounded by the
in-flight count, <= concurrency), matching scan's documented contract.

Budget exhaustion returns the partial comments already produced with a nil
error (not a Go error — the failure path suppresses output), and signals
budget-exceeded out-of-band so the output layer sets a typed `budget_exceeded`
status distinct from success / completed_with_warnings / completed_with_errors.

Additional changes tied to the invariants:
- Pre-review scale warning (files, diff tokens, configured budget) printed
  before any model spend; non-blocking, warn-only.
- Structured usage emitted on the failure path (stderr) so the cost of a
  failed attempt is never lost. Carries only token/tool tallies — no
  credentials or prompts. Reports the agent's actual BudgetExceeded() state so
  the residual budget-trip + all-dispatched-fail edge can never contradict the
  typed status.
- summary.budget_exceeded JSON field (additive, omitempty) so old parsers are
  unaffected; default 0/unlimited restores prior behavior.

Internal/scan gains only the BudgetExceeded() accessor (returns false) so the
shared ResultProvider interface compiles; scan keeps its own token budget and
its JSON output is unchanged.

Tests mirror internal/scan/budget_test.go: token-budget gate stops dispatch
early and sets BudgetExceeded(); unlimited default runs all files; estimate
helpers project sane values (with a humanTokens parity table so the two copies
cannot diverge); JSON output asserts the typed status and the failure-path
usage record. Full `make test` (-race) green.
2026-07-30 10:49:13 +08:00
..
background_file.go fix(background-file) Manage path at git repository root directory Refs:#324 (#327) 2026-07-13 11:27:54 +08:00
background_file_test.go fix(background-file) Manage path at git repository root directory Refs:#324 (#327) 2026-07-13 11:27:54 +08:00
budget_output_test.go feat(agent): add token-cost budget guardrails to the review path (#508) 2026-07-30 10:49:13 +08:00
config_cmd.go feat(mcp): support remote MCP servers via Streamable HTTP (#360) 2026-07-27 20:29:29 +08:00
config_cmd_test.go feat(mcp): support remote MCP servers via Streamable HTTP (#360) 2026-07-27 20:29:29 +08:00
config_dispatch_test.go test: improve coverage for cmd/opencodereview package from 42% to 70% 2026-06-27 01:47:48 +08:00
delegate_cmd.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
emit_run_result_test.go feat(agent): add token-cost budget guardrails to the review path (#508) 2026-07-30 10:49:13 +08:00
flags.go feat(agent): add token-cost budget guardrails to the review path (#508) 2026-07-30 10:49:13 +08:00
flags_test.go feat(agent): add token-cost budget guardrails to the review path (#508) 2026-07-30 10:49:13 +08:00
git.go fix(tool): resolve file_read paths against git top-level in monorepos (#309) 2026-07-07 20:06:28 +08:00
git_test.go test: fix golangci-lint errcheck/staticcheck issues in test code (#323) 2026-07-08 22:46:18 +08:00
llm_cmd.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
main.go fix(cli): align help text and aliases with actual behavior (#557) 2026-07-28 19:13:20 +08:00
output.go feat(agent): add token-cost budget guardrails to the review path (#508) 2026-07-30 10:49:13 +08:00
output_helpers_test.go feat(agent): add token-cost budget guardrails to the review path (#508) 2026-07-30 10:49:13 +08:00
output_test.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
procattr_unix.go feat: add standard MCP tool support (#212) 2026-07-01 19:10:16 +08:00
procattr_windows.go feat: add standard MCP tool support (#212) 2026-07-01 19:10:16 +08:00
provider_cmd.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
provider_cmd_test.go test: fix golangci-lint errcheck/staticcheck issues in test code (#323) 2026-07-08 22:46:18 +08:00
provider_tui.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
provider_tui_funcs_test.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
provider_tui_test.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
review_cmd.go feat(agent): add token-cost budget guardrails to the review path (#508) 2026-07-30 10:49:13 +08:00
review_cmd_test.go fix(cli): reject --to without --from in review flags (#151) 2026-06-16 20:26:22 +08:00
rules_cmd.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
scan_cmd.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
scan_cmd_test.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
session_cmd.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
session_cmd_test.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
shared.go feat(agent): add token-cost budget guardrails to the review path (#508) 2026-07-30 10:49:13 +08:00
shared_test.go fix: align Go module path with actual GitHub repository (#526) 2026-07-27 19:52:35 +08:00
shell_unix.go feat: add standard MCP tool support (#212) 2026-07-01 19:10:16 +08:00
shell_windows.go feat: add standard MCP tool support (#212) 2026-07-01 19:10:16 +08:00
smallfiles_test.go test: improve coverage for cmd/opencodereview package from 42% to 70% 2026-06-27 01:47:48 +08:00
version.go feat(cli): display GitHub repository URL in version and help output 2026-06-02 22:50:36 +08:00
viewer_cmd.go fix(cli): align help text and aliases with actual behavior (#557) 2026-07-28 19:13:20 +08:00