Skip to content

feat(capacity): reject oversized change sets before the first model request (#2125) - #2131

Open
nitishagar wants to merge 1 commit into
alibaba:mainfrom
nitishagar:feat/issue-2125-capacity-preflight
Open

nitishagar wants to merge 1 commit into
alibaba:mainfrom
nitishagar:feat/issue-2125-capacity-preflight

Conversation

@nitishagar

@nitishagar nitishagar commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #2125.

When a change set exceeds a configured capacity limit, ocr review / ocr scan previously had no way to reject it up front: an oversized run burned its whole task window in per-file timeouts / tool-round exhaustion and left automation with a late failure or an empty result that reads like success at a glance.

Change:

  • internal/capacity (new): typed *capacity.Error with the stable OCR_CAPACITY_EXCEEDED code and JSON fields code, stage, requested_files, limit_files, requested_bytes, limit_bytes (all fields always serialized; 0 = that class had no configured limit). capacity.Check fires on strictly-greater-than, so a change set exactly at a limit is accepted, and an unconfigured run (both limits 0) behaves byte-identically to before.
  • Config surface (follows the --max-tokens-budget precedent): template keys MAX_CHANGESET_FILES / MAX_CHANGESET_BYTES (default 0 = unlimited, validated non-negative) plus CLI flags --max-changeset-files / --max-changeset-bytes on both commands; a CLI value > 0 overrides the template default.
  • Review pre-flight (internal/agent): after the reviewability selection (deletions never dispatch) and before any LLM call — the grouping call is the first request — an oversized change set is rejected, recorded as a run failure (input), and finalized so the session ends with a failed manifest. Counts cover exactly the would-dispatch set: files whose checkpoints a resumed run would reuse are excluded, so --resume can never be rejected because its already-done work pushes it over the limit — that would block the recovery path the limits exist to protect.
  • Scan pre-flight (internal/scan): after selection and the zero-reviewable skip, before the cost estimate and the first request; binary items never dispatch and are excluded, while empty-content items DO dispatch (only the plan phase skips them) and are counted; resume-reusable items likewise excluded.
  • Structured rejection output: review publishes the failed manifest on stdout as usual, and the JSON result gains an error object with the fields above (status stays "failed" — no new status string); the existing failure-usage record on stderr carries the same object for both commands, so scan's rejection is parseable on stderr with exit 1 while its stdout stays clean (unchanged convention: stdout is the result stream).

What this deliberately does not change: the all-files-too-large skip path (status skipped) keeps its current behavior; scan's per-file 2 MiB silent skip is untouched (per-file filter, not a change-set rejection); no env vars or app-config keys; no viewer changes. One caveat: with --analysis, the Analysis agent runs alongside the review and is not gated by the review's capacity check (the review itself is).

Type of Change

  • New feature (non-breaking change that adds functionality)

How Has This Been Tested?

  • New unit tests: internal/capacity (Check matrix, JSON shape, message), internal/agent (checkChangesetCapacity: no-limit/within/exactly-at-limit/over-file/over-byte + deletions never counted) plus an end-to-end Run test over a real temp repo proving a capacity rejection issues zero model requests and finalizes a failed manifest, internal/scan (same matrix + binary-excluded/empty-counted + an end-to-end Run zero-request proof), internal/config/template (negative values rejected on both templates; defaults stay unlimited), cmd/opencodereview (flag validation, resolveChangesetLimits precedence, error object present on the stdout result and the stderr failure-usage record, absent on clean runs).
  • Red-team pass applied: counts made resume-aware (a resumed run can no longer be falsely rejected by its completed work), scan empty-content items counted (they dispatch), flag help/docs corrected to "0 = use the configured or template default", review-side zero-request test added, --analysis caveat documented.
  • E2E smoke with the built binary: ocr scan --repo <2-file repo> --max-changeset-files 1 --format json → exit 1, empty stdout, parseable stderr JSON: {"status":"failed",...,"session_id":"…","error":{"code":"OCR_CAPACITY_EXCEEDED","stage":"scan_changeset_limits","requested_files":2,"limit_files":1,"requested_bytes":20,"limit_bytes":0}}.
  • make check clean; full make test suite (race, coverage gate) green; gofmt clean.
  • Failing-first: the new tests reference APIs that don't exist on main (compile-fail evidence); the emission tests assert an error field absent from today's output.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable) — pages/src/content/docs/en/cli-reference.md (flag table) and configuration.md (new "Change-set capacity limits" section); localized section headings added to all locale docs (zh/ja/ko/ru/tr) so the pages heading-sync test stays green
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

AI disclosure: GLM via ZCode was used to draft the change and tests; I reviewed and tested the output.

Related Issues

Closes #2125.

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 4 issue(s) in this PR.

  • ✅ Successfully posted inline: 4 comment(s)

Comment on lines +154 to +159
if opts.maxChangesetFiles > 0 {
scanTpl.MaxChangesetFiles = opts.maxChangesetFiles
}
if opts.maxChangesetBytes > 0 {
scanTpl.MaxChangesetBytes = opts.maxChangesetBytes
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · low
Minor maintainability: review_cmd.go uses the shared resolveChangesetLimits helper for this exact logic, but here it's inlined. Consider using the same helper for consistency and to avoid future divergence if the override semantics change.

Suggestion:

Suggested change
if opts.maxChangesetFiles > 0 {
scanTpl.MaxChangesetFiles = opts.maxChangesetFiles
}
if opts.maxChangesetBytes > 0 {
scanTpl.MaxChangesetBytes = opts.maxChangesetBytes
}
scanTpl.MaxChangesetFiles, scanTpl.MaxChangesetBytes = resolveChangesetLimits(
scanTpl.MaxChangesetFiles, opts.maxChangesetFiles,
scanTpl.MaxChangesetBytes, opts.maxChangesetBytes)

Comment thread internal/agent/agent.go
Comment on lines +559 to +564
var capErr *capacity.Error
if errors.As(err, &capErr) {
a.capErr = capErr
return capErr
}
return nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · medium
If capacity.Check were ever refactored to return a non-*capacity.Error error (e.g., wrapping, or a new internal validation), this fallback silently returns nil, bypassing the capacity gate entirely with no log or diagnostic. Today capacity.Check only returns *capacity.Error or nil, so this path is dead code — but it's a latent correctness risk.

Since the function signature is error (not *capacity.Error), consider either:

  1. Returning the unexpected error directly so it surfaces as a run failure, or
  2. Changing capacity.Check to return *capacity.Error directly (making the type assertion unnecessary).

Option 1 is the minimal safe fix:

Suggestion:

Suggested change
var capErr *capacity.Error
if errors.As(err, &capErr) {
a.capErr = capErr
return capErr
}
return nil
var capErr *capacity.Error
if errors.As(err, &capErr) {
a.capErr = capErr
return capErr
}
// Unexpected error type from capacity.Check — surface it rather than
// silently bypassing the gate.
return &capacity.Error{Code: capacity.Code, Stage: capacity.StageReviewChangeset}

Comment on lines +38 to +41
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s rejected %d file(s) / %d bytes (limits: %d file(s) / %d bytes)",
Code, e.Stage, e.RequestedFiles, e.RequestedBytes, e.LimitFiles, e.LimitBytes)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · medium
When only one limit class is configured (e.g., limitFiles=10, limitBytes=0), the error message renders the unconfigured dimension as 0, producing output like:

OCR_CAPACITY_EXCEEDED: review_changeset_limits rejected 15 file(s) / 5000 bytes (limits: 10 file(s) / 0 bytes)

This is misleading: 0 bytes looks like a zero-byte limit was exceeded rather than "no byte limit configured." Since this string is recorded via SetRunFailure into the manifest and may be displayed to users, consider conditionally rendering only the dimension(s) that actually triggered the rejection, or using a sentinel like "unlimited" / "-" for zero-limit fields.

Comment thread internal/scan/agent.go
Comment on lines +245 to +250
var capErr *capacity.Error
if errors.As(err, &capErr) {
a.capErr = capErr
return capErr
}
return nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · medium
Same silent-swallow issue as in internal/agent/agent.go: if capacity.Check returns an unexpected error type, the capacity gate is silently bypassed. Apply the same fix here.

Suggestion:

Suggested change
var capErr *capacity.Error
if errors.As(err, &capErr) {
a.capErr = capErr
return capErr
}
return nil
var capErr *capacity.Error
if errors.As(err, &capErr) {
a.capErr = capErr
return capErr
}
// Unexpected error type from capacity.Check — surface it rather than
// silently bypassing the gate.
return &capacity.Error{Code: capacity.Code, Stage: capacity.StageScanChangeset}

…equest (alibaba#2125)

ocr review / ocr scan gain hard pre-flight change-set limits
(--max-changeset-files / --max-changeset-bytes, template keys
MAX_CHANGESET_FILES / MAX_CHANGESET_BYTES as defaults, 0 = keep the
configured or template default). When the change set that would actually
dispatch strictly exceeds a configured limit, the run is rejected with a
typed OCR_CAPACITY_EXCEEDED error before any LLM request instead of
burning its task window and failing late with a timeout or an empty
result that reads like success.

The structured error carries code, stage, requested_files, limit_files,
requested_bytes, and limit_bytes (always serialized; 0 = no limit for
that class). Counts are resume-aware — files whose checkpoints a resumed
run would reuse are excluded, so a resume cannot be rejected by its own
completed work — and scan counts empty-content items (they dispatch;
only binaries are excluded). Review records a run failure and publishes
the failed manifest with the error object in the JSON result; both
commands emit the same record via the failure-usage path (stderr in JSON
mode), keeping the stdout result stream convention intact. Unconfigured
runs behave byte-identically. With --analysis, the Analysis agent runs
alongside the review and is not gated by its capacity check (documented).

Locale configuration.md files carry the matching section so the docs
heading-sync test stays green.
@nitishagar
nitishagar force-pushed the feat/issue-2125-capacity-preflight branch from 97a0a78 to 0ba013d Compare November 28, 2026 10:57

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Structured capacity/limit errors before the first model request

1 participant