Skip to content

Commit 03d043f

Browse files
Poggeccicopybara-github
authored andcommitted
fix: InMemorySessionService mergeWithGlobalState not called in appendEvent
PiperOrigin-RevId: 826481854
1 parent 2a86ae8 commit 03d043f

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

core/src/main/java/com/google/adk/sessions/InMemorySessionService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ public Single<Event> appendEvent(Session session, Event event) {
251251
.computeIfAbsent(appName, k -> new ConcurrentHashMap<>())
252252
.computeIfAbsent(userId, k -> new ConcurrentHashMap<>())
253253
.put(userStateKey, value);
254+
} else {
255+
session.state().put(key, value);
254256
}
255257
});
256258
}
@@ -265,6 +267,8 @@ public Single<Event> appendEvent(Session session, Event event) {
265267
.getOrDefault(userId, new ConcurrentHashMap<>())
266268
.put(sessionId, session);
267269

270+
mergeWithGlobalState(appName, userId, session);
271+
268272
return Single.just(event);
269273
}
270274

core/src/test/java/com/google/adk/sessions/InMemorySessionServiceTest.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,36 @@ public void lifecycle_deleteSession() {
107107
}
108108

109109
@Test
110-
public void appendEvent_withStateDelta_mutatesStateCorrectly() {
111-
ConcurrentMap<String, Object> startingState1 = new ConcurrentHashMap<>();
112-
startingState1.put("key_to_be_removed", "value_to_be_removed");
113-
ConcurrentMap<String, Object> startingState2 = new ConcurrentHashMap<>();
114-
startingState2.put("key_to_be_removed", "value_to_be_removed");
115-
116-
ConcurrentMap<String, Object> stateDelta = new ConcurrentHashMap<>();
117-
State expectedFinalState = new State(startingState1, stateDelta);
118-
expectedFinalState.remove("key_to_be_removed");
119-
expectedFinalState.put("new_key", "new_value");
120-
110+
public void appendEvent_updatesSessionState() {
121111
InMemorySessionService sessionService = new InMemorySessionService();
122112
Session session =
123113
sessionService
124-
.createSession("app-name", "user-id", startingState2, /* sessionId= */ null)
114+
.createSession("app", "user", new ConcurrentHashMap<>(), "session1")
125115
.blockingGet();
126116

127-
var unused =
117+
ConcurrentMap<String, Object> stateDelta = new ConcurrentHashMap<>();
118+
stateDelta.put("sessionKey", "sessionValue");
119+
stateDelta.put("_app_appKey", "appValue");
120+
stateDelta.put("_user_userKey", "userValue");
121+
122+
Event event =
123+
Event.builder().actions(EventActions.builder().stateDelta(stateDelta).build()).build();
124+
125+
var unused = sessionService.appendEvent(session, event).blockingGet();
126+
127+
// After appendEvent, session state in memory should contain session-specific state from delta
128+
// and merged global state.
129+
assertThat(session.state()).containsEntry("sessionKey", "sessionValue");
130+
assertThat(session.state()).containsEntry("_app_appKey", "appValue");
131+
assertThat(session.state()).containsEntry("_user_userKey", "userValue");
132+
133+
// getSession should return session with merged state.
134+
Session retrievedSession =
128135
sessionService
129-
.appendEvent(
130-
session,
131-
Event.builder()
132-
.actions(EventActions.builder().stateDelta(stateDelta).build())
133-
.build())
136+
.getSession(session.appName(), session.userId(), session.id(), Optional.empty())
134137
.blockingGet();
135-
136-
assertThat(session.state().entrySet()).containsExactlyElementsIn(expectedFinalState.entrySet());
138+
assertThat(retrievedSession.state()).containsEntry("sessionKey", "sessionValue");
139+
assertThat(retrievedSession.state()).containsEntry("_app_appKey", "appValue");
140+
assertThat(retrievedSession.state()).containsEntry("_user_userKey", "userValue");
137141
}
138142
}

0 commit comments

Comments
 (0)