Repository navigation
refactor(agent,scan): share one 80% token threshold - #421
Merged
lizhengfeng101 merged 1 commit intoJul 21, 2026
Merged
Conversation
tokenWarningThreshold = 0.80 is defined in internal/llmloop, but it is unexported, so the four call sites outside that package could not reach it and each hardcoded '* 4 / 5' instead. The 80% policy therefore had two encodings that could silently drift apart. Add llmloop.PromptTokenLimit and route all six sites through it: internal/agent/agent.go:594, 790 internal/scan/agent.go:321, 555 internal/llmloop/compression.go:99 internal/llmloop/loop.go:440 loop.go:439's softLimit stays inline; it uses the separate 60% tokenSoftThreshold, which has a single consumer and stays unexported. Behaviour is unchanged. 0.80 in float64 is strictly above exact 4/5, so for positive x the product never rounds below the exact value and truncation lands on the same integer; verified over x in [-5_000_000, 5_000_000] with zero divergences. The float form is also the more robust of the two: x*4 overflows int64 above MaxInt64/4, where the old expression silently returned 0 (i.e. no limit). The <= 0 guards stay at the call sites. filterLargeDiffs/filterLargeScans treat a non-positive limit as keep-everything, while the pre-flight gates treat it as a ceiling that rejects every prompt, so the helper does not special-case it. Tests: TestPromptTokenLimit pins the arithmetic with hand-computed expectations, and new boundary tests pin the threshold itself - with MaxTokens=100 an exactly-80-token input is kept and an 81-token one dropped. The pre-existing filter tests survive mutating the constant to both 0.75 and 0.85; the new ones fail on both. Closes alibaba#417
Contributor
|
✅ OpenCodeReview: No comments generated. Looks good to me. |
Githab-capibara
added a commit
to Githab-capibara/open-code-review
that referenced
this pull request
Sep 17, 2026
tokenWarningThreshold = 0.80 is defined in internal/llmloop, but it is unexported, so the four call sites outside that package could not reach it and each hardcoded '* 4 / 5' instead. The 80% policy therefore had two encodings that could silently drift apart. Add llmloop.PromptTokenLimit and route all six sites through it: internal/agent/agent.go:594, 790 internal/scan/agent.go:321, 555 internal/llmloop/compression.go:99 internal/llmloop/loop.go:440 loop.go:439's softLimit stays inline; it uses the separate 60% tokenSoftThreshold, which has a single consumer and stays unexported. Behaviour is unchanged. 0.80 in float64 is strictly above exact 4/5, so for positive x the product never rounds below the exact value and truncation lands on the same integer; verified over x in [-5_000_000, 5_000_000] with zero divergences. The float form is also the more robust of the two: x*4 overflows int64 above MaxInt64/4, where the old expression silently returned 0 (i.e. no limit). The <= 0 guards stay at the call sites. filterLargeDiffs/filterLargeScans treat a non-positive limit as keep-everything, while the pre-flight gates treat it as a ceiling that rejects every prompt, so the helper does not special-case it. Tests: TestPromptTokenLimit pins the arithmetic with hand-computed expectations, and new boundary tests pin the threshold itself - with MaxTokens=100 an exactly-80-token input is kept and an 81-token one dropped. The pre-existing filter tests survive mutating the constant to both 0.75 and 0.85; the new ones fail on both. Closes alibaba#417
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The "80% of
max_tokens" prompt ceiling had two encodings that could drift apart.tokenWarningThreshold = 0.80is defined atinternal/llmloop/compression.go:19, but itis unexported — so the four call sites outside
llmloopcould not reach it and eachhardcoded
* 4 / 5instead. This adds a small exported helper so every site shares thesingle definition.
graph LR subgraph before["before — two encodings, six sites"] A1["agent.go:594<br/>maxAllowed * 4 / 5"] A2["agent.go:789<br/>MaxTokens * 4 / 5"] A3["scan/agent.go:321<br/>MaxTokens * 4 / 5"] A4["scan/agent.go:555<br/>maxAllowed * 4 / 5"] A5["compression.go:101<br/>int(float64(x)*tokenWarningThreshold)"] A6["loop.go:440<br/>int(float64(x)*tokenWarningThreshold)"] end subgraph after["after — one"] H["llmloop.PromptTokenLimit(maxTokens)"] C["tokenWarningThreshold = 0.80<br/>compression.go:19"] H --> C end A1 --> H A2 --> H A3 --> H A4 --> H A5 --> H A6 --> HAll six sites now route through the helper.
loop.go:439'ssoftLimitis left inlinebecause it uses the separate
tokenSoftThreshold(60%) constant, which has a singleconsumer and stays unexported.
Behaviour is unchanged
The replaced integer form
x * 4 / 5and the helper'sint(float64(x) * 0.80)agreeacross every reachable input.
0.80in float64 is0.80000000000000004…, i.e. strictlyabove exact 4/5, so for positive
xthe product never rounds below the exact value andtruncation lands on the same integer. Brute-forced over
x ∈ [-5_000_000, +5_000_000]:zero divergences. Negatives agree too, since Go integer division truncates toward zero.
The first divergence is at
x = 2^52(~4.5 quadrillion tokens).The float form is also the more robust of the two at extremes:
x * 4overflowsint64above
MaxInt64/4, where the old expression silently returned0— i.e. "no limit".MaxTokensis validated> 0atinternal/config/template/template.go:189and:203before any agent is constructed, so the non-positive case is unreachable in production;
the existing
_ZeroMaxTokens/_NoLimittests still pin the guards for directconstruction.
Out of scope
Left alone deliberately, to keep this a mechanical refactor: the literal
80inuser-facing message strings and doc comments (
agent.go:596,788,800,809;scan/agent.go:319,330,338,557;llmloop/loop.go:434-435), and the* 4 / 5example inpages/src/content/docs/{en,zh,ja}/architecture.md. Those are display copy and prose, notthe arithmetic this issue targets. Glad to follow up separately if wanted.
One observation, no change made:
Template.MaxTokensis also sent to the provider as theoutput cap (
internal/llm/client.go:493MaxCompletionTokens,internal/llm/responses_client.go:188MaxOutputTokens), so this helper is really "80%of the output cap, used as a prompt budget". That conflation predates this PR — I kept the
existing semantics rather than changing them, but if you'd prefer the helper named
WarnTokenLimitto tie it to the constant rather than implying a prompt/completiondistinction the config doesn't make, that's a one-line change.
AI assistance: Claude Code helped implement and verify this change. I reviewed the full
diff and take responsibility for it.
Type of Change
How Has This Been Tested?
make testpasses locally (full suite,-race, 23 packages, zero failures)make fmt,make vet,make check— cleanmake coverage— 81.5%, above the 80% gate;PromptTokenLimititself at 100%go test ./internal/llmloop/ -run TestPromptTokenLimit -v— 8 subtests + parity test passgo test ./internal/agent/ ./internal/scan/ -run 'FilterLarge' -v— 6/6 passgrep -rn "4 / 5" --include="*.go" .— emptyMutation-tested the new boundary tests
The pre-existing
TestFilterLargeDiffs/TestFilterLargeScansuse margins wide enoughto pass at any threshold, so before this PR nothing actually pinned 80%. I verified the
new tests close that gap by mutating the constant in both directions:
TestFilterLargeDiffsTestFilterLargeDiffs_BoundaryTestFilterLargeScansTestFilterLargeScans_BoundaryBoth new tests are two-sided mutation killers; the pre-existing ones survive both mutants.
Tests added
TestPromptTokenLimit— table-driven, expectations hand-computed rather than derivedfrom the implementation (which would tautologically pass). The
maxTokens: 5row is theload-bearing one: the exact product is
4.000000000000000222, and the half-ULP at4.0makes it round down to exactly
4.0and truncate to 4, not 3.TestPromptTokenLimitMatchesReplacedExpression— parity with the removedx*4/5form.Documented as specific to
0.80, to be deleted rather than "fixed" if the threshold moves.TestFilterLargeDiffs_Boundary/TestFilterLargeScans_Boundary— withMaxTokens=100the limit is 80, so an exactly-80-token input is kept and an exactly-81-token one is
dropped. Fixtures are built through
llm.CountTokens— the same function the code undertest uses — and
t.Fatalfif the tokenizer ever disagrees, so drift fails loudly ratherthan quietly weakening the assertion.
Checklist
go fmt,go vet)Related Issues
Closes #417