Skip to content

fix(agent): auto-compact no longer silently drops head content past the 80K slice - #1061

Closed
shadowinlife wants to merge 1 commit into
HKUDS:mainfrom
shadowinlife:fix/auto-compact-silent-loss
Closed

fix(agent): auto-compact no longer silently drops head content past the 80K slice#1061
shadowinlife wants to merge 1 commit into
HKUDS:mainfrom
shadowinlife:fix/auto-compact-silent-loss

Conversation

@shadowinlife

Copy link
Copy Markdown
Contributor

Summary

  • Fixes the silent information loss in AgentLoop._auto_compact() reported in [Bug] Auto-compaction can silently drop conversation content during summarization #1055: the head is now folded through the summarization LLM in budget-sized, message-boundary chunks instead of being hard-sliced at 80,000 serialized characters.
  • Every head message now reaches the summarization LLM exactly once, upholding the method's documented "zero info decay" contract.

Why

_auto_compact() built its summarization prompt with:

conv_text = json.dumps(head, default=str, ensure_ascii=False)[:80000]

head is everything not kept in the ~20K-token tail (TAIL_TOKEN_BUDGET). Once json.dumps(head) exceeded 80,000 characters, everything past the cut was neither seen by the summarization LLM nor preserved in the tail — silent information loss with no exception, contradicting the docstring's "zero info decay" guarantee.

Verified against main @ c33133f with the issue's reproduction script: messages [7..13] were present in neither the summarization prompt nor the reconstructed conversation.

Closes #1055

Changes

  • agent/src/agent/loop.py
    • New module helper _chunk_messages_for_summary(): splits messages into chunks whose serialized JSON stays under the budget; message boundaries are never broken; a single oversized message forms its own chunk rather than being dropped.
    • New constant COMPACT_HEAD_CHAR_BUDGET = 80_000 (same per-pass budget as the old slice).
    • _auto_compact() replaces the single truncated pass with a fold: the first pass starts from the previous summary (or the structured template, keeping prior behavior), and each subsequent pass folds the next chunk into the running summary via the existing _ITERATIVE_UPDATE_PROMPT. Single-chunk heads behave exactly as before.
    • Docstring updated to state the chunked no-drop guarantee.
  • agent/tests/test_auto_compact_no_silent_loss.py — regression tests:
    • the issue's exact scenario (20 tagged messages, oversized head): zero lost markers, multiple summarization passes;
    • chunk helper: exact-once coverage, per-chunk budget invariant, oversized-single-message preservation, empty input.

Note for maintainers: this touches the protected src/agent/ path. The issue author asked which remediation direction was intended (overflow preservation vs. chunked summarization). This PR implements chunked summarization, since it is the only option that keeps the "every message is either summarized or preserved verbatim" contract without growing the retained tail unboundedly; happy to reshape if a different direction is preferred.

Test Plan

Checklist

  • No changes to protected areas (src/agent/, src/session/, src/providers/) without prior discussion — touches src/agent/loop.py; flagged above for maintainer direction, per the question raised in [Bug] Auto-compaction can silently drop conversation content during summarization #1055
  • No hardcoded values (API keys, file paths, magic numbers) — the 80K budget is a named constant
  • Code follows CONTRIBUTING.md guidelines — DCO sign-off on the commit
  • Documentation updated (if user-facing change) — _auto_compact docstring updated

…he 80K slice

_auto_compact() serialized the head with a hard json.dumps(head)[:80000]
slice. Once the head exceeded that slice, everything past the cut was
neither fed to the summarization LLM nor kept in the preserved tail —
silent information loss that contradicts the method's own "zero info
decay" contract.

Fold the head through the summarization LLM in budget-sized,
message-boundary chunks instead: the first pass starts from the previous
summary (or the structured template) and each later pass folds the next
chunk into the running summary. Every head message reaches the
summarization LLM exactly once, and a single oversized message forms its
own chunk rather than being dropped.

Closes HKUDS#1055

Signed-off-by: shadowinlife <shadowinlife@gmail.com>
Co-Authored-By: OpenCode <noreply@opencode.ai>
AI-Model: alibaba-cn/qwen3.8-max
AI-Contributed/Feature: 65/65
AI-Contributed/UT: 115/115
@shadowinlife
shadowinlife marked this pull request as draft August 11, 2026 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Auto-compaction can silently drop conversation content during summarization

1 participant