Runs compaction for SlidingWindowCompactor. This method implements the sliding window compaction logic. It determines if enough new invocations have occurred since the last compaction based on `compaction_invocation_threshold`. If so, it selects a range of events to compact based on `overla
(
app: App,
session: Session,
session_service: BaseSessionService,
*,
skip_token_compaction: bool = False,
)
| 438 | |
| 439 | |
| 440 | async def _run_compaction_for_sliding_window( |
| 441 | app: App, |
| 442 | session: Session, |
| 443 | session_service: BaseSessionService, |
| 444 | *, |
| 445 | skip_token_compaction: bool = False, |
| 446 | ): |
| 447 | """Runs compaction for SlidingWindowCompactor. |
| 448 | |
| 449 | This method implements the sliding window compaction logic. It determines |
| 450 | if enough new invocations have occurred since the last compaction based on |
| 451 | `compaction_invocation_threshold`. If so, it selects a range of events to |
| 452 | compact based on `overlap_size`, and calls `maybe_compact_events` on the |
| 453 | compactor. |
| 454 | |
| 455 | The compaction process is controlled by two parameters: |
| 456 | 1. `compaction_invocation_threshold`: The number of *new* user-initiated |
| 457 | invocations that, once fully |
| 458 | represented in the session's events, will trigger a compaction. |
| 459 | 2. `overlap_size`: The number of preceding invocations to include from the |
| 460 | end of the last |
| 461 | compacted range. This creates an overlap between consecutive compacted |
| 462 | summaries, |
| 463 | maintaining context. |
| 464 | |
| 465 | The compactor is called after an agent has finished processing a turn and all |
| 466 | its events |
| 467 | have been added to the session. It checks if a new compaction is needed. |
| 468 | |
| 469 | When a compaction is triggered: |
| 470 | - The compactor identifies the range of `invocation_id`s to be summarized. |
| 471 | - This range starts `overlap_size` invocations before the beginning of the |
| 472 | new block of `compaction_invocation_threshold` invocations and ends |
| 473 | with the last |
| 474 | invocation |
| 475 | in the current block. |
| 476 | - A `CompactedEvent` is created, summarizing all events within this |
| 477 | determined |
| 478 | `invocation_id` range. This `CompactedEvent` is then appended to the |
| 479 | session. |
| 480 | |
| 481 | Here is an example with `compaction_invocation_threshold = 2` and |
| 482 | `overlap_size = 1`: |
| 483 | Let's assume events are added for `invocation_id`s 1, 2, 3, and 4 in order. |
| 484 | |
| 485 | 1. **After `invocation_id` 2 events are added:** |
| 486 | - The session now contains events for invocations 1 and 2. This |
| 487 | fulfills the `compaction_invocation_threshold = 2` criteria. |
| 488 | - Since this is the first compaction, the range starts from the |
| 489 | beginning. |
| 490 | - A `CompactedEvent` is generated, summarizing events within |
| 491 | `invocation_id` range [1, 2]. |
| 492 | - The session now contains: `[ |
| 493 | E(inv=1, role=user), E(inv=1, role=model), |
| 494 | E(inv=2, role=user), E(inv=2, role=model), |
| 495 | CompactedEvent(inv=[1, 2])]`. |
| 496 | |
| 497 | 2. **After `invocation_id` 3 events are added:** |