Repository navigation
fix(agent): make --preview apply the same selection the review applies - #801
Conversation
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 7 selected item(s). |
|
@zmwangx Right fix — one selectFiles for both paths kills the drift at the source. Please add a test asserting previewMaxTokens resolves the same value the real run applies (that's the one seam left), and rebase onto main. 👍 |
`--preview` applied only the static file filters, but a real review also ran filterLargeDiffs before it froze coverage, dropping any diff whose token count exceeded 80% of max_tokens. That filter ran before registerCoverage sealed the selected set, so a file could be listed under "Will review" and then never be reviewed — and preview could not apply the gate even in principle, because runPreviewContext passed no Template and Template.MaxTokens was therefore zero on that path. Extract the deterministic pre-dispatch selection into one pure operation, selectFiles, that returns every input diff with a decision and a stable reason. Preview and Run both consume it, so neither can re-derive the rules on its own: reimplementing the size check inside preview would just let the two drift again. Run keeps its previous log output by reporting the static exclusions and the size exclusions as two groups. Add too_large as the exclusion reason for an oversized diff, so consumers need not reproduce the tokenizer or the 80% threshold to explain the drop. The human preview renders it through the existing reason column. Give preview the real limit without initializing a provider: resolveMaxTokens needs only AppCfg, which loads independently of llm.ResolveEndpointWithOptions, so preview still requires no API key. `ocr scan --preview` already resolved its limit this way, through resolveScanPreviewMaxTokens; that helper had the same body as previewMaxTokens, so scan now calls previewMaxTokens instead. `ocr delegate preview` passes no template and keeps the gate disabled — the host agent reviews with its own context window, not OCR's max_tokens. One behavior delta beyond the fix: a deletion is decided before the size gate, so an oversized deletion now stays in the working diff set instead of being dropped by filterLargeDiffs. It was never dispatched or registered either way, and it now behaves like any other deletion. ResolveIdentity, the resume pre-flight, must hash the same selection the run records in source_artifact_sha256, so it now replays selectFiles as well. That digest skips deletions, so the delta above does not move it. The skip line for an emptied selection now names the count and drops its "All changed files" claim, which was wrong whenever the static filters had already removed some. Selection also decides the run-level skip signal now. review.skipped carries the cause in an attribute — too_large, deleted or no_supported_files, most actionable first — and is keyed on the selected set, the same quantity registerCoverage seals, so a deletion-only run counts as skipped too. Two judgement calls there: no.files.changed keeps firing exactly where it did, so existing consumers are unaffected, and a deletion-only run emits review.skipped before review.started rather than suppressing review.started, which would drop a signal main emits. An oversized changeset used to emit review.started with a review.count that included the files it then dropped, and no skip event at all. Deliberately out of scope because the outcomes are execution-specific or unavailable to preview: aggregate --max-tokens-budget exhaustion, resume reuse and LLM failures. The regression test pins that equality directly: preview's will_review set equals the set a fresh, non-resumed, unbudgeted run registers as selected coverage, over a changeset that mixes reviewable, extension-filtered, deleted and oversized files, asserting each of those outcomes is really represented. The CLI test covers both sources of the limit, since --max-tokens short-circuits config resolution; the all-oversized case drives Run, which is where the gate now runs; and the model constant table pins too_large's wire value. The unit tests that covered filterDiffs / filterLargeDiffs / countReviewable now cover the one selection they were folded into. Update architecture and review-rule docs for selection.go and the two post-gate outcomes, add too_large to the FAQ, and document the new review.skipped telemetry event in every locale. Fixes alibaba#782
Selection is one operation that preview and the run both consume, but each resolves the max_tokens it hands selection on its own path: the run from the app config its LLM runtime loaded, preview through previewMaxTokens, which loads the config without building a runtime. A source or precedence change on either path alone would put preview's will_review set out of step with the run again, with nothing failing. Drive a real review through the CLI for each source of the limit — the template default, the saved max_tokens, --max-tokens, and the flag overriding a saved value — and assert previewMaxTokens returns what the run handed its agent. The change set holds only a file the static gates exclude, so the run skips before any LLM request. Read the run's value off agent.Args through a newReviewAgent variable rather than recomputing it in the test, so the assertion follows what executeReviewContext actually applies instead of restating its precedence.
2ffd46e to
455cf91
Compare
|
@lizhengfeng101 Cool, done. I had to introduce a test hook for |
Assert previewMaxTokens parity by calling resolveMaxTokens directly (the same function the run uses) instead of monkeypatching agent.New through a package-level var that leaked into review_cmd.go. Removes global mutable test state and the production indirection.
|
Pushed a small follow-up (a7fa1e4): I think this smell is worth removing before merge. Dropped the |
alibaba#801) * fix(agent): make --preview apply the same selection the review applies `--preview` applied only the static file filters, but a real review also ran filterLargeDiffs before it froze coverage, dropping any diff whose token count exceeded 80% of max_tokens. That filter ran before registerCoverage sealed the selected set, so a file could be listed under "Will review" and then never be reviewed — and preview could not apply the gate even in principle, because runPreviewContext passed no Template and Template.MaxTokens was therefore zero on that path. Extract the deterministic pre-dispatch selection into one pure operation, selectFiles, that returns every input diff with a decision and a stable reason. Preview and Run both consume it, so neither can re-derive the rules on its own: reimplementing the size check inside preview would just let the two drift again. Run keeps its previous log output by reporting the static exclusions and the size exclusions as two groups. Add too_large as the exclusion reason for an oversized diff, so consumers need not reproduce the tokenizer or the 80% threshold to explain the drop. The human preview renders it through the existing reason column. Give preview the real limit without initializing a provider: resolveMaxTokens needs only AppCfg, which loads independently of llm.ResolveEndpointWithOptions, so preview still requires no API key. `ocr scan --preview` already resolved its limit this way, through resolveScanPreviewMaxTokens; that helper had the same body as previewMaxTokens, so scan now calls previewMaxTokens instead. `ocr delegate preview` passes no template and keeps the gate disabled — the host agent reviews with its own context window, not OCR's max_tokens. One behavior delta beyond the fix: a deletion is decided before the size gate, so an oversized deletion now stays in the working diff set instead of being dropped by filterLargeDiffs. It was never dispatched or registered either way, and it now behaves like any other deletion. ResolveIdentity, the resume pre-flight, must hash the same selection the run records in source_artifact_sha256, so it now replays selectFiles as well. That digest skips deletions, so the delta above does not move it. The skip line for an emptied selection now names the count and drops its "All changed files" claim, which was wrong whenever the static filters had already removed some. Selection also decides the run-level skip signal now. review.skipped carries the cause in an attribute — too_large, deleted or no_supported_files, most actionable first — and is keyed on the selected set, the same quantity registerCoverage seals, so a deletion-only run counts as skipped too. Two judgement calls there: no.files.changed keeps firing exactly where it did, so existing consumers are unaffected, and a deletion-only run emits review.skipped before review.started rather than suppressing review.started, which would drop a signal main emits. An oversized changeset used to emit review.started with a review.count that included the files it then dropped, and no skip event at all. Deliberately out of scope because the outcomes are execution-specific or unavailable to preview: aggregate --max-tokens-budget exhaustion, resume reuse and LLM failures. The regression test pins that equality directly: preview's will_review set equals the set a fresh, non-resumed, unbudgeted run registers as selected coverage, over a changeset that mixes reviewable, extension-filtered, deleted and oversized files, asserting each of those outcomes is really represented. The CLI test covers both sources of the limit, since --max-tokens short-circuits config resolution; the all-oversized case drives Run, which is where the gate now runs; and the model constant table pins too_large's wire value. The unit tests that covered filterDiffs / filterLargeDiffs / countReviewable now cover the one selection they were folded into. Update architecture and review-rule docs for selection.go and the two post-gate outcomes, add too_large to the FAQ, and document the new review.skipped telemetry event in every locale. Fixes alibaba#782 * test(cli): pin preview's max_tokens to the value the run applies Selection is one operation that preview and the run both consume, but each resolves the max_tokens it hands selection on its own path: the run from the app config its LLM runtime loaded, preview through previewMaxTokens, which loads the config without building a runtime. A source or precedence change on either path alone would put preview's will_review set out of step with the run again, with nothing failing. Drive a real review through the CLI for each source of the limit — the template default, the saved max_tokens, --max-tokens, and the flag overriding a saved value — and assert previewMaxTokens returns what the run handed its agent. The change set holds only a file the static gates exclude, so the run skips before any LLM request. Read the run's value off agent.Args through a newReviewAgent variable rather than recomputing it in the test, so the assertion follows what executeReviewContext actually applies instead of restating its precedence. * test(review): drop agent.New seam from max-tokens parity test Assert previewMaxTokens parity by calling resolveMaxTokens directly (the same function the run uses) instead of monkeypatching agent.New through a package-level var that leaked into review_cmd.go. Removes global mutable test state and the production indirection. --------- Co-authored-by: kite <[email protected]>
…merge Update two learnings whose references drifted when v1.12.9 landed: - cross-engine-review-exposes-file-filter-blind-spots: add the secret gate to the coreFilterReason decision order (user exclude no longer beats everything), list the current ExcludeReason constants, note that provider_directory and too_large never appear in core diff output, 31 -> 97 default exclude globs, vendor/ -> apps/ in the parity example, re-point moved line citations (selection.go, scan/agent.go, shared_flags.go, core_cmd.go), mark alibaba#782 closed by alibaba#801, and describe the cited commits as fork-branch commits now that they are pushed. - clean-merge-does-not-prove-branch-only-files-compile: re-point moved citations (review_cmd.go:136, shared.go:132-135, system_rules.go, agent_test.go:238), and state that build/vet/test is necessary but not sufficient when the branch holds copies of upstream logic, with a link to the new parity learning. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Description
--previewapplied only the static file filters, but a real review applies one more gate before it freezes coverage: any diff whose token count exceeds 80% ofmax_tokensis dropped. That gate runs before the selected set is sealed, so a file could be listed under "Will review" and then never be reviewed. Preview could not apply it even in principle —runPreviewnever passed a template, so the limit was zero on that path.Rather than teach preview the size check separately — which would just let the two drift again — this turns the deterministic pre-dispatch selection into one operation both paths consume:
internal/agent/selection.goholdsselectFiles, which takes the changed diffs and returns a decision per file: the exclusion reason, or none. It is pure — no output, no mutation, no git or LLM access — so it can run in a preview that builds none of the review runtime.RunandAgent.Previewboth call it. The three independently implemented decisions it replaces — the static filter, the size filter, and the reviewable count — are gone; the run's skip logging, its file counts, and preview's entries are all derived from the same decisions.Args.Template.MaxTokens), and zero disables the gate — which is howdelegate previewopts out.The net guarantee: preview's
will_reviewset is the set a fresh, non-resumed, unbudgeted run registers as selected coverage.User-visible changes:
too_largeexclusion reason, in the human view and in--format json.--max-tokens, then the savedmax_tokens, then the template) without building an LLM runtime, so it still needs no provider and no API key.N file(s) changed, reviewing Mline no longer counts files the size gate is about to drop.review.skipped, carrying the cause as an attribute.no.files.changedfires exactly where it did before, so existing consumers are unaffected.ocr delegate previewis deliberately unchanged: the host agent reviews with its own context window, not OCR'smax_tokens.Out of scope because they are execution-specific or unavailable to preview: aggregate
--max-tokens-budgetexhaustion, resume reuse (--previewcannot be combined with--resume), and LLM failures. The commit message covers the design and the trade-offs in detail.Type of Change
How Has This Been Tested?
make testpasses locallyThe repro from #782: one commit adding a single ~186 KB file, whose diff exceeds 80% of the default
max_tokens. No real API key is needed: preview initializes no provider, and the full run uses dummy endpoint settings that are never contacted because the size gate exits before any request.Before — preview promises a review that never happens:
After — preview reports the drop, and the run's own count agrees with it:
A regression test pins the property directly: preview's
will_reviewset equals the set the run registers as selected coverage, over a changeset that mixes reviewable, extension-filtered, deleted and oversized files.Checklist
go fmt,go vet)Related Issues
Closes #782.
Disclosure: PR authored by AI with human guidance and careful review.