Skip to content

Feat: Implement Anchored Iterative Summarization in adk-js context compactors#470

Open
AmaadMartin wants to merge 3 commits into
google:mainfrom
AmaadMartin:feat/anchored-summarization
Open

Feat: Implement Anchored Iterative Summarization in adk-js context compactors#470
AmaadMartin wants to merge 3 commits into
google:mainfrom
AmaadMartin:feat/anchored-summarization

Conversation

@AmaadMartin

Copy link
Copy Markdown
Collaborator

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

  1. Link to an existing issue (if applicable):

Closes: #issue_number
Related: #issue_number

  1. Or, if no issue exists, describe the change:
    Implement Anchored Iterative Summarization in adk-js context compactors.

This introduces AnchoredContextCompactor which compresses historical event transcripts using an anchored summarization strategy:

  • Anchored Summary (Index 0): It designates the first event slot (index 0) in the active history for a persistent, cumulative summary/scratchpad event (represented as a CompactedEvent).
  • Iterative Merging: During each compaction cycle, the compactor takes the previous summary (if present at index 0) and all subsequent uncompacted raw events, and passes them to the LLM. The LLM generates a new consolidated summary.
  • State Replacement: The old summary and the processed raw events are removed, and a new CompactedEvent containing the updated summary is inserted back at index 0.
  • Preservation of Recent Context: This allows long-running conversations to retain a high-level summary of all past history in a fixed token footprint, while recent messages are left untouched to preserve active context.

Rationale (Comparison with TokenBasedContextCompactor)

While both compactors summarize event history, they handle the session state differently:

  • Zero Session Bloat: TokenBasedContextCompactor retains all old, summarized raw events in the session history array, causing the session database/storage to grow indefinitely. AnchoredContextCompactor physically prunes the array in-place, preventing storage bloat for long-running conversations.
  • Fixed Anchor Position: AnchoredContextCompactor keeps the summary consistently at index 0 of the event array. This keeps the session history structurally clean and intuitive when inspected in developer dashboards, databases, or logs, as opposed to having the summary shifting positions.
  • Simpler Triggers: It provides a straightforward count-threshold based trigger (compacting when uncompacted events exceed a set number), offering an alternative to token-based triggers when token counting overhead is not desired.

Testing Plan
Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes.

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Manual End-to-End (E2E) Tests:
Created an integration test at tests/integration/context_compaction/anchored/agent_test.ts representing multiple turns of context compaction, verifying correct creation and iterative updates to the scratchpad. We also added an E2E test file.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

@AmaadMartin AmaadMartin closed this Jul 1, 2026
@AmaadMartin AmaadMartin reopened this Jul 1, 2026
@AmaadMartin AmaadMartin force-pushed the feat/anchored-summarization branch 2 times, most recently from cbcc14c to 34dddf9 Compare July 6, 2026 17:07
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.

1 participant