[Bug]:上下文压缩失效导致agent运行失败
"metadata": { "_chat_usage": { "inputTokens": 76789, "outputTokens": 1344, "cachedTokens": 0, "time": 76.132, "totalTokens": 78133 } }, "timestamp": "2026-09-04 17:16:02.922", "usage": { "inputTokens": 76789, "outputTokens": 1344, "cachedTokens": 0, "time": 76.132, "totalTokens": 78133 }
从studio上观测到最后一条totalTokens已经达到了78133,设置得triggerTokens=70000,通过断点调试ConversationCompactor.compactIfNeeded,TokenCounterUtil.calculateToken(messages)最终计算出来的token=3.8万,汉字居多,导致上下文超限,模型是本地部署Qwen3_5-27B-FP8
Issue Evaluation
Category: bug | Status: Confirmed
Verified against main @ 7399ed6 by source-level analysis (no build/run: this is a read-only check).
Root cause: the compaction trigger is compared against a character-ratio estimate that is not language-aware. TokenCounterUtil uses one constant for every script — agentscope-harness/src/main/java/io/agentscope/harness/agent/memory/compaction/TokenCounterUtil.java:49 (CHARS_PER_TOKEN = 2.5), applied at :224-225 (Math.ceil(text.length() / CHARS_PER_TOKEN)). ConversationCompactor gates solely on that value: :110-111 computes totalTokens and :226 returns true only when totalTokens >= config.getTriggerTokens().
For Chinese-dominant content a real BPE tokenizer lands near ~1 character/token, so the estimator reports roughly half the true count — which matches your observation exactly: ~38k estimated vs. 76789 inputTokens from the model, with triggerTokens=70000 never reached. Compaction therefore never fires and the request overflows.
Impact: the memory-compaction safety mechanism silently fails for CJK-heavy workloads; the run dies on context overflow instead of compacting. Severity: high.
Conservative fix directions (no heavyweight tokenizer forced into core by default):
- CJK-aware weighting in
estimateTextTokens— keep the ASCII ratio, use a lower ratio for non-ASCII code points; dependency-free. - Make the ratio configurable (
charsPerToken/cjkCharsPerToken) on the compaction config instead of a private constant. - A pluggable
TokenEstimatorinterface with the current char-based implementation as the default, so model-specific counters can be injected.
Two caveats for whoever fixes this: estimated counts are computed on demand and not persisted, so no cache invalidation is needed, but PruneConfig / TruncateArgsConfig thresholds are expressed in the same estimator units — tightening the estimate changes their effective aggressiveness and may need re-tuning by users who set them from observed token counts.
No automated PR will be generated for this issue: #2990 (fix(harness): prevent missed compaction for multilingual contexts, @guslegend0510, non-draft, mergeable, REVIEW_REQUIRED) already claims this issue and takes direction (1) plus additional scope (system prompt / tool definitions / response-schema inclusion in the trigger budget, primary-model context window when a separate summary model is configured, truncation/pruning when summarization is skipped). Reviewing there avoids two competing fixes in the same estimator and trigger code paths.
Automated evaluation by github-manager-bot