From e79453e65c6b19981f733aea9c9c0618b4b80136 Mon Sep 17 00:00:00 2001 From: rootkiller6788 Date: Sat, 22 Aug 2026 02:13:57 +0800 Subject: [PATCH 1/2] fix(memory): skip non-text parts and use millis timestamps in memory search searchMemory called Part.text().get() on every part, which throws NoSuchElementException when a stored event contains a non-text part (e.g. a function call or function response). Only text parts are searchable, so non-text parts are now skipped instead of crashing the loadMemory tool. formatTimestamp treated the event timestamp as epoch seconds, but Event.timestamp() is epoch milliseconds, so returned memory timestamps were off by ~1000x. Use Instant.ofEpochMilli to match the event timestamp unit. --- .../adk/memory/InMemoryMemoryService.java | 20 +-- .../adk/memory/InMemoryMemoryServiceTest.java | 117 ++++++++++++++++++ 2 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTest.java diff --git a/core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java b/core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java index ff2995c74..397646345 100644 --- a/core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java +++ b/core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java @@ -20,7 +20,6 @@ import com.google.adk.events.Event; import com.google.adk.sessions.Session; -import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.genai.types.Part; @@ -105,12 +104,17 @@ public Single searchMemory(String appName, String userId, Set wordsInEvent = new HashSet<>(); for (Part part : event.content().get().parts().get()) { - if (!Strings.isNullOrEmpty(part.text().get())) { - Matcher matcher = WORD_PATTERN.matcher(part.text().get()); - while (matcher.find()) { - wordsInEvent.add(matcher.group().toLowerCase(Locale.ROOT)); - } - } + // Only text parts contain searchable words; other part types (e.g. function + // calls) must be skipped, not read as text. + part.text() + .filter(text -> !text.isEmpty()) + .ifPresent( + text -> { + Matcher matcher = WORD_PATTERN.matcher(text); + while (matcher.find()) { + wordsInEvent.add(matcher.group().toLowerCase(Locale.ROOT)); + } + }); } if (wordsInEvent.isEmpty()) { @@ -136,6 +140,6 @@ public Single searchMemory(String appName, String userId, } private String formatTimestamp(long timestamp) { - return Instant.ofEpochSecond(timestamp).toString(); + return Instant.ofEpochMilli(timestamp).toString(); } } diff --git a/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTest.java b/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTest.java new file mode 100644 index 000000000..f2855adaf --- /dev/null +++ b/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.adk.memory; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.adk.events.Event; +import com.google.adk.sessions.Session; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.genai.types.Content; +import com.google.genai.types.FunctionCall; +import com.google.genai.types.Part; +import java.time.Instant; +import java.util.concurrent.ConcurrentHashMap; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class InMemoryMemoryServiceTest { + + private final InMemoryMemoryService memoryService = new InMemoryMemoryService(); + + /** Sessions that used tools contain function-call parts; searching them must not crash. */ + @Test + public void searchMemory_skipsNonTextParts() { + long timestamp = 1_700_000_000_000L; + Session session = + Session.builder("session-1") + .appName("app") + .userId("user") + .state(new ConcurrentHashMap<>()) + .events( + ImmutableList.of( + Event.builder() + .author("user") + .content( + Content.builder() + .role("user") + .parts(Part.fromText("My name is James")) + .build()) + .timestamp(timestamp) + .build(), + Event.builder() + .author("model") + .content( + Content.builder() + .role("model") + .parts( + ImmutableList.of( + Part.builder() + .functionCall( + FunctionCall.builder() + .name("loadMemory") + .args(ImmutableMap.of("query", "name")) + .build()) + .build())) + .build()) + .timestamp(timestamp) + .build())) + .build(); + + memoryService.addSessionToMemory(session).blockingAwait(); + + SearchMemoryResponse response = memoryService.searchMemory("app", "user", "name").blockingGet(); + + assertThat(response.memories()).hasSize(1); + assertThat(response.memories().get(0).content().parts().get().get(0).text().get()) + .isEqualTo("My name is James"); + } + + /** Memory timestamps are rendered from epoch milliseconds, matching {@link Event#timestamp()}. */ + @Test + public void searchMemory_formatsTimestampAsEpochMillis() { + long epochMillis = 1_700_000_000_000L; + Session session = + Session.builder("session-1") + .appName("app") + .userId("user") + .state(new ConcurrentHashMap<>()) + .events( + ImmutableList.of( + Event.builder() + .author("user") + .content( + Content.builder() + .role("user") + .parts(Part.fromText("My name is James")) + .build()) + .timestamp(epochMillis) + .build())) + .build(); + + memoryService.addSessionToMemory(session).blockingAwait(); + + SearchMemoryResponse response = memoryService.searchMemory("app", "user", "name").blockingGet(); + + assertThat(response.memories()).hasSize(1); + assertThat(response.memories().get(0).timestamp()) + .isEqualTo(Instant.ofEpochMilli(epochMillis).toString()); + } +} From 8ae0daac9bdfd72195635a3c5f444a4bec4faf29 Mon Sep 17 00:00:00 2001 From: rootkiller6788 Date: Sat, 29 Aug 2026 21:32:57 +0800 Subject: [PATCH 2/2] fix(memory): narrow PR to just the millis timestamp fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dropping the non-text part guard from this branch — it duplicates the other open PR (#1465) which already fixes the same crash and adds its own test. Keeping it here would collide on the same test file once that PR lands. What's left is the formatTimestamp fix: Event.timestamp() is epoch milliseconds, so reading it as epoch seconds was producing memory timestamps ~1000x in the future. Switch to Instant.ofEpochMilli and add a focused test for it under a separate file so there's no name clash. --- .../adk/memory/InMemoryMemoryService.java | 18 +++---- ...> InMemoryMemoryServiceTimestampTest.java} | 52 +------------------ 2 files changed, 8 insertions(+), 62 deletions(-) rename core/src/test/java/com/google/adk/memory/{InMemoryMemoryServiceTest.java => InMemoryMemoryServiceTimestampTest.java} (51%) diff --git a/core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java b/core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java index 397646345..983a86a13 100644 --- a/core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java +++ b/core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java @@ -20,6 +20,7 @@ import com.google.adk.events.Event; import com.google.adk.sessions.Session; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.genai.types.Part; @@ -104,17 +105,12 @@ public Single searchMemory(String appName, String userId, Set wordsInEvent = new HashSet<>(); for (Part part : event.content().get().parts().get()) { - // Only text parts contain searchable words; other part types (e.g. function - // calls) must be skipped, not read as text. - part.text() - .filter(text -> !text.isEmpty()) - .ifPresent( - text -> { - Matcher matcher = WORD_PATTERN.matcher(text); - while (matcher.find()) { - wordsInEvent.add(matcher.group().toLowerCase(Locale.ROOT)); - } - }); + if (!Strings.isNullOrEmpty(part.text().get())) { + Matcher matcher = WORD_PATTERN.matcher(part.text().get()); + while (matcher.find()) { + wordsInEvent.add(matcher.group().toLowerCase(Locale.ROOT)); + } + } } if (wordsInEvent.isEmpty()) { diff --git a/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTest.java b/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTimestampTest.java similarity index 51% rename from core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTest.java rename to core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTimestampTest.java index f2855adaf..874c34f3c 100644 --- a/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTest.java +++ b/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceTimestampTest.java @@ -21,9 +21,7 @@ import com.google.adk.events.Event; import com.google.adk.sessions.Session; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.genai.types.Content; -import com.google.genai.types.FunctionCall; import com.google.genai.types.Part; import java.time.Instant; import java.util.concurrent.ConcurrentHashMap; @@ -32,58 +30,10 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public final class InMemoryMemoryServiceTest { +public final class InMemoryMemoryServiceTimestampTest { private final InMemoryMemoryService memoryService = new InMemoryMemoryService(); - /** Sessions that used tools contain function-call parts; searching them must not crash. */ - @Test - public void searchMemory_skipsNonTextParts() { - long timestamp = 1_700_000_000_000L; - Session session = - Session.builder("session-1") - .appName("app") - .userId("user") - .state(new ConcurrentHashMap<>()) - .events( - ImmutableList.of( - Event.builder() - .author("user") - .content( - Content.builder() - .role("user") - .parts(Part.fromText("My name is James")) - .build()) - .timestamp(timestamp) - .build(), - Event.builder() - .author("model") - .content( - Content.builder() - .role("model") - .parts( - ImmutableList.of( - Part.builder() - .functionCall( - FunctionCall.builder() - .name("loadMemory") - .args(ImmutableMap.of("query", "name")) - .build()) - .build())) - .build()) - .timestamp(timestamp) - .build())) - .build(); - - memoryService.addSessionToMemory(session).blockingAwait(); - - SearchMemoryResponse response = memoryService.searchMemory("app", "user", "name").blockingGet(); - - assertThat(response.memories()).hasSize(1); - assertThat(response.memories().get(0).content().parts().get().get(0).text().get()) - .isEqualTo("My name is James"); - } - /** Memory timestamps are rendered from epoch milliseconds, matching {@link Event#timestamp()}. */ @Test public void searchMemory_formatsTimestampAsEpochMillis() {