Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ public Single<Event> appendEvent(Session session, Event event) {
Objects.requireNonNull(session.userId(), "session.userId cannot be null");
Objects.requireNonNull(session.id(), "session.id cannot be null");

if (event.partial().orElse(false)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: VertexAiSessionService.appendEvent and FirestoreSessionService.appendEvent have the same partial-event gap. VertexAiSessionService delegates to BaseSessionService.super.appendEvent (which returns Single.just(event) early), then still chains .flatMap(...) into client.appendEvent(...), and FirestoreSessionService writes stateDelta before checking partial(). Fine to keep this PR scoped to InMemorySessionService, just flagging the other two in case you want them consistent.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for checking the other implementations! I'll keep this PR focused on InMemorySessionService to match #1987, and I've filed #1993 to track the follow-up fix for VertexAiSessionService and FirestoreSessionService.

return Single.just(event);
}

String appName = session.appName();
String userId = session.userId();
String sessionId = session.id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,43 @@ public void appendEvent_updatesSessionTimestampWithFractionalSeconds() {
assertThat(retrievedSession.lastUpdateTime()).isEqualTo(Instant.ofEpochSecond(5, 500000000L));
}

@Test
public void appendEvent_partialEvent_doesNotMutateStateOrTimestamp() {
Instant initialTime = Instant.ofEpochMilli(1000L);
InMemorySessionService sessionService =
new InMemorySessionService(
DuplicateSessionIdBehavior.OVERWRITE, () -> initialTime, () -> "session1");
Session session =
sessionService.createSession("app", "user", new HashMap<>(), "session1").blockingGet();

ConcurrentMap<String, Object> stateDelta = new ConcurrentHashMap<>();
stateDelta.put("sessionKey", "sessionValue");
stateDelta.put(State.APP_PREFIX + "appKey", "appValue");
stateDelta.put(State.USER_PREFIX + "userKey", "userValue");
Event partialEvent =
Event.builder()
.partial(true)
.timestamp(9999L)
.actions(EventActions.builder().stateDelta(stateDelta).build())
.build();

Event appended = sessionService.appendEvent(session, partialEvent).blockingGet();

assertThat(appended).isEqualTo(partialEvent);
Session retrieved =
sessionService.getSession("app", "user", "session1", Optional.empty()).blockingGet();
assertThat(retrieved.events()).isEmpty();
assertThat(retrieved.state()).doesNotContainKey("sessionKey");
assertThat(retrieved.state()).doesNotContainKey(State.APP_PREFIX + "appKey");
assertThat(retrieved.state()).doesNotContainKey(State.USER_PREFIX + "userKey");
assertThat(retrieved.lastUpdateTime()).isEqualTo(initialTime);

Session otherSession =
sessionService.createSession("app", "user", new HashMap<>(), "session2").blockingGet();
assertThat(otherSession.state()).doesNotContainKey(State.APP_PREFIX + "appKey");
assertThat(otherSession.state()).doesNotContainKey(State.USER_PREFIX + "userKey");
}

@Test
public void sequentialAgents_shareTempState() {
InMemorySessionService sessionService = new InMemorySessionService();
Expand Down