@@ -418,6 +418,62 @@ async def stream_fn(
418418 assert text_deltas == [expected ]
419419
420420
421+ async def test_endpoint_provider_mutation_does_not_change_shared_state_snapshot (
422+ streaming_chat_client_stub : Any ,
423+ ) -> None :
424+ """Provider mutations through the session view do not alter replayable Shared State."""
425+
426+ class MutatingProvider (ContextProvider ):
427+ async def before_run (
428+ self ,
429+ * ,
430+ agent : SupportsAgentRun ,
431+ session : AgentSession ,
432+ context : SessionContext ,
433+ state : dict [str , Any ],
434+ ) -> None :
435+ del agent , context , state
436+ identity = session .state ["identity" ]
437+ assert isinstance (identity , dict )
438+ identity ["type" ] = "provider-mutated"
439+
440+ async def stream_fn (
441+ messages : list [Message ],
442+ options : dict [str , Any ],
443+ ** kwargs : Any ,
444+ ) -> AsyncIterator [ChatResponseUpdate ]:
445+ del messages , options , kwargs
446+ yield ChatResponseUpdate (contents = [Content .from_text (text = "Completed" )])
447+
448+ agent = Agent (
449+ name = "test" ,
450+ instructions = None ,
451+ client = streaming_chat_client_stub (stream_fn ),
452+ context_providers = [MutatingProvider ("mutating" )],
453+ )
454+ app = FastAPI ()
455+ add_agent_framework_fastapi_endpoint (
456+ app ,
457+ agent ,
458+ path = "/isolated-request-state" ,
459+ state_schema = {"identity" : {"type" : "object" }},
460+ keepalive_seconds = None ,
461+ )
462+
463+ response = TestClient (app ).post (
464+ "/isolated-request-state" ,
465+ json = {
466+ "messages" : [{"role" : "user" , "content" : "Run" }],
467+ "state" : {"identity" : {"type" : "request" }},
468+ },
469+ )
470+
471+ state_snapshots = [
472+ event ["snapshot" ] for event in _decode_sse_events (response ) if event .get ("type" ) == "STATE_SNAPSHOT"
473+ ]
474+ assert state_snapshots == [{"identity" : {"type" : "request" }}]
475+
476+
421477async def test_endpoint_restores_context_provider_state_across_scoped_runs (streaming_chat_client_stub ):
422478 """Server-produced provider state survives sequential runs in one scoped thread."""
423479
0 commit comments