Skip to content

Commit ea12505

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Update event compaction logic to include events after compaction end times
Events are retained if they occur before the start of any compaction or after the end of any compaction. PiperOrigin-RevId: 856868176
1 parent 8e94665 commit ea12505

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

core/src/main/java/com/google/adk/flows/llmflows/Contents.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,15 @@ private List<Event> processCompactionEvent(List<Event> events) {
209209
List<Event> result = new ArrayList<>();
210210
ListIterator<Event> iter = events.listIterator(events.size());
211211
Long lastCompactionStartTime = null;
212+
Long lastCompactionEndTime = null;
212213

213214
while (iter.hasPrevious()) {
214215
Event event = iter.previous();
215216
EventCompaction compaction = event.actions().compaction().orElse(null);
216217
if (compaction == null) {
217-
if (lastCompactionStartTime == null || event.timestamp() < lastCompactionStartTime) {
218+
if (lastCompactionStartTime == null
219+
|| event.timestamp() < lastCompactionStartTime
220+
|| (lastCompactionEndTime != null && event.timestamp() > lastCompactionEndTime)) {
218221
result.add(event);
219222
}
220223
continue;
@@ -233,6 +236,10 @@ private List<Event> processCompactionEvent(List<Event> events) {
233236
lastCompactionStartTime == null
234237
? compaction.startTimestamp()
235238
: Long.min(lastCompactionStartTime, compaction.startTimestamp());
239+
lastCompactionEndTime =
240+
lastCompactionEndTime == null
241+
? compaction.endTimestamp()
242+
: Long.max(lastCompactionEndTime, compaction.endTimestamp());
236243
}
237244
return Lists.reverse(result);
238245
}

core/src/test/java/com/google/adk/flows/llmflows/ContentsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,22 @@ public void processRequest_multipleCompactions() {
561561
.containsExactly("Summary 1-4", "content 5", "Summary 6-9", "content 10");
562562
}
563563

564+
@Test
565+
public void processRequest_compactionWithUncompactedEventsBetween() {
566+
ImmutableList<Event> events =
567+
ImmutableList.of(
568+
createUserEvent("e1", "content 1", "inv1", 1),
569+
createUserEvent("e2", "content 2", "inv2", 2),
570+
createUserEvent("e3", "content 3", "inv3", 3),
571+
createCompactedEvent(1, 2, "Summary 1-2"));
572+
573+
List<Content> contents = runContentsProcessor(events);
574+
assertThat(contents)
575+
.comparingElementsUsing(
576+
transforming((Content c) -> c.parts().get().get(0).text().get(), "content text"))
577+
.containsExactly("content 3", "Summary 1-2");
578+
}
579+
564580
private static Event createUserEvent(String id, String text) {
565581
return Event.builder()
566582
.id(id)

0 commit comments

Comments
 (0)