Skip to content

Commit 49c0a36

Browse files
GWealecopybara-github
authored andcommitted
refactor: yield the sliding-window compaction event to the runner
Move event persistence to the main runner loop, the runtime's synchronization point: sliding-window compaction now yields its event instead of appending it, and the runner appends it like any other event. Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 944093841
1 parent ad5445a commit 49c0a36

4 files changed

Lines changed: 217 additions & 114 deletions

File tree

src/google/adk/apps/compaction.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import annotations
1616

1717
import logging
18+
from typing import AsyncGenerator
1819

1920
from google.genai import types
2021

@@ -443,7 +444,7 @@ async def _run_compaction_for_sliding_window(
443444
session_service: BaseSessionService,
444445
*,
445446
skip_token_compaction: bool = False,
446-
):
447+
) -> AsyncGenerator[Event, None]:
447448
"""Runs compaction for SlidingWindowCompactor.
448449
449450
This method implements the sliding window compaction logic. It determines
@@ -521,30 +522,35 @@ async def _run_compaction_for_sliding_window(
521522
Args:
522523
app: The application instance.
523524
session: The session containing events to compact.
524-
session_service: The session service for appending events.
525+
session_service: The session service, used by the token-threshold fallback.
525526
skip_token_compaction: Whether to skip token-threshold compaction.
527+
528+
Yields:
529+
The sliding-window compaction event, if one is produced. The caller (the
530+
runner loop) is responsible for appending it to the session, so that
531+
persistence of this event stays at the runtime's synchronization point.
526532
"""
527533
events = session.events
528534
if not events:
529-
return None
535+
return
530536

531537
config = app.events_compaction_config
532538
if config is None:
533-
return None
539+
return
534540

535541
# Prefer token-threshold compaction if configured and triggered.
536542
if not skip_token_compaction and _has_token_threshold_config(config):
537543
token_compacted = await _run_compaction_for_token_threshold(
538544
app, session, session_service
539545
)
540546
if token_compacted:
541-
return None
547+
return
542548

543549
if not _has_sliding_window_config(config):
544-
return None
550+
return
545551

546552
if config.compaction_interval is None or config.overlap_size is None:
547-
return None
553+
return
548554

549555
# Find the last compaction event and its range.
550556
last_compacted_end_timestamp = 0.0
@@ -573,7 +579,7 @@ async def _run_compaction_for_sliding_window(
573579
]
574580

575581
if len(new_invocation_ids) < config.compaction_interval:
576-
return None # Not enough new invocations to trigger compaction.
582+
return # Not enough new invocations to trigger compaction.
577583

578584
# Determine the range of invocations to compact.
579585
# The end of the compaction range is the last of the new invocations.
@@ -613,20 +619,20 @@ async def _run_compaction_for_sliding_window(
613619
events_to_compact = _longest_self_contained_prefix(events_to_compact)
614620

615621
if not events_to_compact:
616-
return None
622+
return
617623

618624
if app.root_agent is None:
619-
return None
625+
return
620626
_ensure_compaction_summarizer(config=config, agent=app.root_agent)
621627
if config.summarizer is None:
622-
return None
628+
return
623629

624630
compaction_event = await _summarize_events_with_trace(
625631
session=session,
626632
config=config,
627633
events_to_compact=events_to_compact,
628634
trigger='sliding_window',
629635
)
630-
if compaction_event:
631-
await session_service.append_event(session=session, event=compaction_event)
632636
logger.debug('Event compactor finished.')
637+
if compaction_event:
638+
yield compaction_event

src/google/adk/runners.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,18 @@ async def _drive_root_node():
584584
logger.debug('Running event compactor.')
585585
from google.adk.apps.compaction import _run_compaction_for_sliding_window
586586

587-
await _run_compaction_for_sliding_window(
588-
self.app,
589-
session,
590-
self.session_service,
591-
skip_token_compaction=ic.token_compaction_checked,
592-
)
587+
async with aclosing(
588+
_run_compaction_for_sliding_window(
589+
self.app,
590+
session,
591+
self.session_service,
592+
skip_token_compaction=ic.token_compaction_checked,
593+
)
594+
) as compaction_events:
595+
async for compaction_event in compaction_events:
596+
await self.session_service.append_event(
597+
session=session, event=compaction_event
598+
)
593599

594600
async def _run_node_live(
595601
self,
@@ -1130,12 +1136,18 @@ async def execute(ctx: InvocationContext) -> AsyncGenerator[Event]:
11301136
logger.debug('Running event compactor.')
11311137
from google.adk.apps.compaction import _run_compaction_for_sliding_window
11321138

1133-
await _run_compaction_for_sliding_window(
1134-
self.app,
1135-
invocation_context.session,
1136-
self.session_service,
1137-
skip_token_compaction=invocation_context.token_compaction_checked,
1138-
)
1139+
async with aclosing(
1140+
_run_compaction_for_sliding_window(
1141+
self.app,
1142+
invocation_context.session,
1143+
self.session_service,
1144+
skip_token_compaction=invocation_context.token_compaction_checked,
1145+
)
1146+
) as compaction_events:
1147+
async for compaction_event in compaction_events:
1148+
await self.session_service.append_event(
1149+
session=invocation_context.session, event=compaction_event
1150+
)
11391151

11401152
async with aclosing(_run_with_trace(new_message, invocation_id)) as agen:
11411153
async for event in agen:

0 commit comments

Comments
 (0)