Repository navigation
feat(capacity): reject oversized change sets before the first model request (#2125) - #2131
nitishagar wants to merge 1 commit into
Conversation
|
🔍 OpenCodeReview found 4 issue(s) in this PR.
|
| if opts.maxChangesetFiles > 0 { | ||
| scanTpl.MaxChangesetFiles = opts.maxChangesetFiles | ||
| } | ||
| if opts.maxChangesetBytes > 0 { | ||
| scanTpl.MaxChangesetBytes = opts.maxChangesetBytes | ||
| } |
There was a problem hiding this comment.
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:
| 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) |
| var capErr *capacity.Error | ||
| if errors.As(err, &capErr) { | ||
| a.capErr = capErr | ||
| return capErr | ||
| } | ||
| return nil |
There was a problem hiding this comment.
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:
- Returning the unexpected error directly so it surfaces as a run failure, or
- Changing
capacity.Checkto return*capacity.Errordirectly (making the type assertion unnecessary).
Option 1 is the minimal safe fix:
Suggestion:
| 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} |
| 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) | ||
| } |
There was a problem hiding this comment.
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.
| var capErr *capacity.Error | ||
| if errors.As(err, &capErr) { | ||
| a.capErr = capErr | ||
| return capErr | ||
| } | ||
| return nil |
There was a problem hiding this comment.
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:
| 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.
97a0a78 to
0ba013d
Compare
Description
Fixes #2125.
When a change set exceeds a configured capacity limit,
ocr review/ocr scanpreviously 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.Errorwith the stableOCR_CAPACITY_EXCEEDEDcode and JSON fieldscode,stage,requested_files,limit_files,requested_bytes,limit_bytes(all fields always serialized; 0 = that class had no configured limit).capacity.Checkfires 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.--max-tokens-budgetprecedent): template keysMAX_CHANGESET_FILES/MAX_CHANGESET_BYTES(default0= unlimited, validated non-negative) plus CLI flags--max-changeset-files/--max-changeset-byteson both commands; a CLI value > 0 overrides the template default.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--resumecan never be rejected because its already-done work pushes it over the limit — that would block the recovery path the limits exist to protect.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.errorobject 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
How Has This Been Tested?
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-endRuntest 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-endRunzero-request proof),internal/config/template(negative values rejected on both templates; defaults stay unlimited),cmd/opencodereview(flag validation,resolveChangesetLimitsprecedence,errorobject present on the stdout result and the stderr failure-usage record, absent on clean runs).--analysiscaveat documented.ocr scan --repo <2-file repo> --max-changeset-files 1 --format json→ exit1, 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 checkclean; fullmake testsuite (race, coverage gate) green; gofmt clean.main(compile-fail evidence); the emission tests assert anerrorfield absent from today's output.Checklist
go fmt,go vet)pages/src/content/docs/en/cli-reference.md(flag table) andconfiguration.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 greenAI disclosure: GLM via ZCode was used to draft the change and tests; I reviewed and tested the output.
Related Issues
Closes #2125.