Class WindowCompactionStrategy

java.lang.Object
org.springframework.ai.session.compaction.WindowCompactionStrategy
All Implemented Interfaces:
CompactionStrategy

public final class WindowCompactionStrategy extends Object implements CompactionStrategy
Keeps the recent conversation in the view and covers the rest: the one compaction strategy, configured by a CompactionWindow, the last events, the last turns or a token budget, and optionally a CompactionSummarizer that condenses what leaves the view into a summary turn.

One pass, whatever the window:

  1. The latest stored system message, the system prompt, is pinned at the front of the view and never summarized; every earlier stored system message is superseded and leaves the view with a pass that cuts, wherever it sits.
  2. The events before the first turn, a pre-seeded preamble, are pinned too: they are not a turn and are never cut.
  3. Without a summarizer, the summary a previous pass left is pinned and carried forward; with one, it is handed to the summarizer as the prior summary and covered by the new summary.
  4. The window gives the raw cut among the turns. The cut moves forward to the next turn start, so a turn is never split, and back to the newest turn when it would cover everything, so the newest turn stays even when it alone exceeds the window.
  5. The turns before the cut leave the view, the rest stay. With a summarizer, the summary turn stands in for the covered events; a summarizer that returns nothing leaves the view unchanged.
Pinned events use no slot of an event or turn window and come off a token budget first. Nothing is written when the turns fit the window, when the view has no turn to cut at, or when the summarizer declines. The log is never changed: SessionService.compact(java.lang.String, org.springframework.ai.session.compaction.CompactionTrigger, org.springframework.ai.session.compaction.CompactionStrategy) records the result as an appended boundary.
Since:
2.1.0
Author:
Christian Tzolov