Skip to content

Commit 00d44f5

Browse files
committed
Python: isolate AG-UI request state
1 parent 36b6afd commit 00d44f5

3 files changed

Lines changed: 67 additions & 6 deletions

File tree

‎python/packages/ag-ui/README.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,12 @@ It is not an authentication, tenant authorization, or distributed durability mec
370370
responsible for endpoint authentication, tenant authorization, and deployment/storage architecture that matches their
371371
availability and worker topology requirements.
372372

373-
Snapshot storage is trusted, integrity-protected server storage because private continuation is eligible for typed
374-
core restoration. Snapshots also have confidentiality impact: they may contain sensitive user text, model output,
375-
tool results, function arguments, UI payloads, Shared State, interrupt data, and private provider working state. The
376-
built-in `InMemoryAGUIThreadSnapshotStore` is in-memory only, process-local, bounded, latest-only, and not durable
377-
production storage. It is cleared on process restart and is not shared across workers.
373+
Snapshot storage is treated as trusted server-side storage because private continuation is eligible for typed core
374+
restoration; applications are responsible for providing its integrity protection. Snapshots also have confidentiality
375+
impact: they may contain sensitive user text, model output, tool results, function arguments, UI payloads, Shared State,
376+
interrupt data, and private provider working state. The built-in `InMemoryAGUIThreadSnapshotStore` is in-memory only,
377+
process-local, bounded, latest-only, and not durable production storage. It is cleared on process restart and is not
378+
shared across workers.
378379

379380
No file-backed AG-UI snapshot store is provided by the package. Applications that need durable persistence should
380381
provide an app-owned implementation of the `AGUIThreadSnapshotStore` protocol and own storage hardening, including

‎python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,11 @@ async def run_agent_stream(
19181918
_restore_session_continuation_state(session, stored_snapshot)
19191919
protected_session_state_keys = _request_state_protected_keys(agent)
19201920
session.state.update(
1921-
{key: value for key, value in flow.current_state.items() if key not in protected_session_state_keys}
1921+
{
1922+
key: copy.deepcopy(value)
1923+
for key, value in flow.current_state.items()
1924+
if key not in protected_session_state_keys
1925+
}
19221926
)
19231927
_restore_tool_approval_state(session, approval_state_store, approval_thread_id)
19241928

‎python/packages/ag-ui/tests/ag_ui/test_endpoint.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
421477
async 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

Comments
 (0)