fix(core): ensure hard threshold always exceeds auto threshold#4949
Conversation
When the proportional branch dominated auto (e.g. 60K context window), rawHard fell below auto, causing hard to degrade to auto. This meant compaction only triggered at the very last moment with no gap between auto and hard tiers. Change hard from max(rawHard, auto) to min(window, max(rawHard, auto + HARD_BUFFER)), guaranteeing hard > auto for all non-zero windows while preserving behavior for large windows where rawHard already dominates. Closes #4945
qwen-code-ci-bot
left a comment
There was a problem hiding this comment.
Hi @DennisYu07 — thanks for the fix, the substance looks solid! However, the PR body doesn't follow the PR template. A few headings are missing or renamed:
## Summary/## Root Cause/## Fix→ should be## What this PR doesand## Why it's needed- Missing:
## Risk & Scope,## Linked Issues(theCloses #4945is in the body but should be under this heading),### Evidence (Before & After),### Tested on - Missing:
<details>中文说明</details>block (Chinese translation of the PR body)
Could you update the PR description to match the template? Happy to re-review once that's done.
中文说明
嗨 @DennisYu07 — 感谢修复,内容看起来不错!但 PR 描述没有按照 PR 模板 的格式。部分标题缺失或命名不一致:
## Summary/## Root Cause/## Fix→ 应改为## What this PR does和## Why it's needed- 缺少:
## Risk & Scope、## Linked Issues(Closes #4945在正文中但应放在此标题下)、### Evidence (Before & After)、### Tested on - 缺少:
<details>中文说明</details>区块(PR 正文的中文翻译)
请按照模板更新 PR 描述,更新后我们会重新审查。
— Qwen Code · qwen3.7-max
Code Coverage Summary
CLI Package - Full Text ReportCore Package - Full Text ReportFor detailed HTML reports, please see the 'coverage-reports-22.x-ubuntu-latest' artifact from the main CI run. |
|
@qwen-code /triage |
tanzhenxin
left a comment
There was a problem hiding this comment.
I found no discrete correctness issues in the threshold change or updated tests. The new hard threshold preserves existing large-window behavior while separating auto and hard tiers for the reported medium-window case.
What this PR does
Ensures the hard compaction threshold is always strictly greater than the auto threshold. Previously, on medium-sized context windows (roughly below 110K tokens), hard degraded to equal auto, leaving no gap between the two tiers. The fix changes the hard calculation from
max(rawHard, auto)tomin(window, max(rawHard, auto + HARD_BUFFER)), guaranteeing at least a 3,000-token gap.Why it's needed
When hard equals auto, the hard-tier rescue path — which force-triggers compaction to prevent context window saturation — never fires independently. Compaction only triggers at the very last moment with no safety margin. For a 60K context window, both auto and hard were 42,000 tokens; now auto stays at 42,000 and hard moves to 45,000, giving the rescue path room to act.
Reviewer Test Plan
How to verify
contextWindowSize: 60000in~/.qwen/settings.jsonunder your model provider entry.npm run dev), send any message, then run/context.Evidence (Before & After)
Before:

After:

Tested on
Environment (optional)
Unit tests via
npx vitest runinpackages/coreandpackages/cli. Manual verification vianpm run dev+/context.Risk & Scope
windowitself — acceptable since hard is a last-resort trigger.Linked Issues
Closes #4945
中文说明
做了什么
确保 hard 压缩阈值始终严格大于 auto 阈值。此前在中等大小的上下文窗口(大约 110K 以下)上,hard 会退化为与 auto 相同的值,两个层级之间没有间距。修复将 hard 的计算从
max(rawHard, auto)改为min(window, max(rawHard, auto + HARD_BUFFER)),保证至少有 3,000 token 的间距。为什么需要
当 hard 等于 auto 时,hard 层级的救援路径(强制触发压缩以防止上下文窗口饱和)永远不会独立触发。压缩只在最后一刻才触发,没有安全余量。对于 60K 上下文窗口,之前 auto 和 hard 都是 42,000 token;现在 auto 保持 42,000,hard 变为 45,000,给救援路径留出了空间。
审查测试计划
~/.qwen/settings.json的 model provider 配置中设置contextWindowSize: 60000。npm run dev),发送任意消息,然后执行/context。风险与范围
关联 Issue
Closes #4945