File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -342,8 +342,9 @@ def _warning(message: str) -> None:
342342
343343 # Update the storage session
344344 storage_session = self .sessions [app_name ][user_id ].get (session_id )
345- storage_session .events .append (event )
346- storage_session .last_update_time = event .timestamp
345+ if storage_session is not session :
346+ storage_session .events .append (event )
347+ storage_session .last_update_time = event .timestamp
347348
348349 if event .actions and event .actions .state_delta :
349350 state_deltas = _session_util .extract_state_delta (
Original file line number Diff line number Diff line change @@ -1013,6 +1013,30 @@ async def test_append_event_allows_markerless_current_session():
10131013 await service .close ()
10141014
10151015
1016+ @pytest .mark .asyncio
1017+ async def test_append_event_when_session_is_same_ref_as_storage_session ():
1018+ """Tests that appending an event to a session only appends it once if the user-passed session and the underlying storage session are the same object."""
1019+ service = InMemorySessionService ()
1020+ app_name = 'my_app'
1021+ user_id = 'test_user'
1022+
1023+ # Create a session
1024+ session = await service .create_session (app_name = app_name , user_id = user_id )
1025+
1026+ # Get the actual storage event object from the underlying storage
1027+ storage_session = service .sessions [app_name ][user_id ][session .id ]
1028+
1029+ # Append the event to the storage session directly
1030+ event = Event (invocation_id = 'inv1' , author = 'user' )
1031+ await service .append_event (session = storage_session , event = event )
1032+
1033+ # Verify that the storage session has only one event
1034+ final_session = await service .get_session (
1035+ app_name = app_name , user_id = user_id , session_id = session .id
1036+ )
1037+ assert len (final_session .events ) == 1
1038+
1039+
10161040@pytest .mark .asyncio
10171041async def test_get_session_with_config (session_service ):
10181042 app_name = 'my_app'
You can’t perform that action at this time.
0 commit comments