You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Compaction reduction check trusts TokenCounterUtil's per-image underestimate — image-heavy histories can never compact and pay a discarded summary call per round #4227
Since 2.0.5, ConversationCompactor.compactIfNeeded rejects a summary candidate when TokenCounterUtil.calculateToken(compacted) >= totalTokens ("Rejected summary candidate: N tokens is not smaller than the current M"). That comparison trusts the character-based estimate — and TokenCounterUtil charges a fixed 5 tokens per ImageBlock.
For conversations whose prefix is dominated by images, the estimate is far below real provider cost, so a legitimate text summary (the message built by buildSummaryMessage alone carries a ~300-char English preamble ≈ 120+ estimated tokens) is larger than the underestimated total and is rejected. Consequences:
Regular compaction never fires for image-heavy sessions. Worse, the rejection happens after the summarization LLM call, so every reasoning round above the threshold pays for a summary that is then discarded (the loop reported in [Bug]: 2.0.5 compaction rejects valid text summaries containing ThinkingBlock #4210, same shape, different cause).
Callers that build overflow recovery on compactIfNeeded (e.g. a HarnessAgent-style forceCompact) get an empty result and cannot recover — for exactly the sessions that are most likely to overflow in reality, because the provider-side cost of those images is what blew the window while the estimator still reported a small number.
To Reproduce
// 10 messages, each carrying one small image (e.g. a 1x1 PNG) — TokenCounterUtil total ≈ 100List<Msg> imageHistory = ...;
CompactionConfigcfg = CompactionConfig.builder()
.triggerMessages(1).keepMessages(2).keepTokens(0)
.flushBeforeCompact(false).offloadBeforeCompact(false)
.prune(null).truncateArgs(null)
.build();
ConversationCompactorcompactor = newConversationCompactor(modelReturningShortTextSummary, flushManager);
// modelReturningShortTextSummary.stream(...) returns "summary" as a single TextBlockcompactor.compactIfNeeded(ctx, imageHistory, cfg, "agent", "session").block();
// => Mono[Optional.empty()], log: "Rejected summary candidate: ... not smaller than the current ..."
Any history where estimate(prefix) <= preamble + summary + estimate(tail) reproduces; image-only prefixes always do.
Suggested fix
Either of:
Make the reduction check opt-out or media-aware:
add CompactionConfig.Builder.requireTokenReduction(boolean) (default true keeps current behavior; overflow-recovery callers set false), or
charge ImageBlock (and other media blocks) a realistic default weight in TokenCounterUtil, or exclude the fixed preamble from candidateTokens.
Alternatively, perform the reduction check before invoking the summarization LLM (compare an upper bound), so a doomed candidate does not cost a call per round.
Marking what is inference: the provider-side cost of images (~300+ tokens each for mainstream vision endpoints) is our measured background, not framework code; the per-round wasted summary call follows from the code path order (summarize → compare → reject).
Environment
AgentScope Java 2.0.5 (agentscope-harness), hit while upgrading a production platform whose overflow recovery wraps ConversationCompactor with triggerMessages(1); workaround we use today is replacing image blocks with weighted text stand-ins before calling compactIfNeeded, which makes the estimate honest again but duplicates knowledge the framework could own.
Correction to my report: on main this is already fixed by #3847. TokenCounterUtil now delegates to ContextTokenEstimator.approximate(), which charges any media block 2048 tokens. The bug only affects the 2.0.x maintenance line, where media blocks are still charged 5 tokens.
#4229 backports that media weight to 2.0.x as a minimal change. It also counts HintBlock by its text length, because hints are injected text, not media. The requireTokenReduction switch I suggested above should not be needed once the estimate is honest.
Activity
littlebird69 commentedon Nov 17, 2026
Correction to my report: on
mainthis is already fixed by #3847.TokenCounterUtilnow delegates toContextTokenEstimator.approximate(), which charges any media block 2048 tokens. The bug only affects the 2.0.x maintenance line, where media blocks are still charged 5 tokens.#4229 backports that media weight to
2.0.xas a minimal change. It also countsHintBlockby its text length, because hints are injected text, not media. TherequireTokenReductionswitch I suggested above should not be needed once the estimate is honest.