From 26f309133f05254fb72c4827162603b118f14b36 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Fri, 30 Oct 2026 10:53:11 +0100 Subject: [PATCH 1/6] Python: preserve FIDES AG-UI replay provenance --- .../ag-ui/agent_framework_ag_ui/_agent_run.py | 91 +- .../_message_adapters.py | 103 +- .../ag-ui/tests/ag_ui/test_endpoint.py | 1001 +++++++++++++++++ .../packages/core/agent_framework/security.py | 74 +- python/packages/core/tests/test_security.py | 24 + 5 files changed, 1265 insertions(+), 28 deletions(-) diff --git a/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py b/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py index 75a78aa49ab..76dd7d3c6c1 100644 --- a/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py +++ b/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py @@ -90,7 +90,12 @@ InRunPendingToolTransitionOwner, ) from ._approval_state import _APPROVAL_SCOPE_INPUT_KEY, InMemoryAGUIApprovalStateStore, approval_state_thread_id -from ._message_adapters import _APPROVAL_DECISION_IS_BOOLEAN_KEY, normalize_agui_input_messages +from ._message_adapters import ( + _APPROVAL_DECISION_IS_BOOLEAN_KEY, + _mark_backend_snapshot_origin, + _take_backend_snapshot_messages, + normalize_agui_input_messages, +) from ._predictive_state import PredictiveStateHandler from ._tooling import collect_server_tools, merge_tools from ._run_common import ( @@ -1774,6 +1779,7 @@ async def _resolve_approval_responses( pending_response_groups: dict[object, tuple[ApprovalOccurrence, list[Content]]] = {} intents_by_response_content_id: dict[int, AuthorizedExecution] = {} authenticated_non_grants: list[Content] = [] + authenticated_rejection_call_ids: set[str] = set() for response in approval_responses: resp_id = response.id function_call_id = response.function_call.call_id if response.function_call else None @@ -1871,6 +1877,7 @@ async def _resolve_approval_responses( ) if decision_is_boolean and primary_response.approved is False and not server_label: authenticated_non_grants.append(_approval_observer_response(pending_entry)) + authenticated_rejection_call_ids.add(pending_entry.identity.call_id) if ( (primary_response.approved is True or pending_entry.owner is ApprovalExecutionOwner.DEFERRED) and lifecycle is not None @@ -1912,6 +1919,14 @@ async def _resolve_approval_responses( authenticated_non_grants, session=invocation_session ) + if valid_response_content_ids: + for message in messages: + if message.contents and all( + content.type == "function_approval_response" and id(content) in valid_response_content_ids + for content in message.contents + ): + _mark_backend_snapshot_origin(message) + if response_content_ids_to_strip: filtered_messages: list[Message] = [] for message in messages: @@ -2085,12 +2100,19 @@ async def execute_local_call(approval: Content = approval, call_id: str = call_i # After _replace_approval_contents_with_results, approved tool calls have their results # placed in user messages. OpenAI requires tool results to be in role="tool" messages. # This transformation ensures the message history is valid for the LLM provider. - _convert_approval_results_to_tool_messages(messages) + _convert_approval_results_to_tool_messages( + messages, + authenticated_rejection_call_ids=authenticated_rejection_call_ids, + ) return approved_results -def _convert_approval_results_to_tool_messages(messages: list[Message]) -> None: +def _convert_approval_results_to_tool_messages( + messages: list[Message], + *, + authenticated_rejection_call_ids: set[str] | None = None, +) -> None: """Convert function_result content in user messages to proper tool messages. After approval processing, tool results end up in user messages. OpenAI and other @@ -2105,6 +2127,13 @@ def _convert_approval_results_to_tool_messages(messages: list[Message]) -> None: result: list[Message] = [] for msg in messages: + if authenticated_rejection_call_ids and any( + content.type == "function_result" + and content.call_id in authenticated_rejection_call_ids + and content.result == "Error: Tool call invocation was rejected by user." + for content in msg.contents + ): + _mark_backend_snapshot_origin(msg) if get_role_value(msg) != "user": result.append(msg) continue @@ -2123,7 +2152,8 @@ def _convert_approval_results_to_tool_messages(messages: list[Message]) -> None: # Tool messages first (right after the preceding assistant message per OpenAI requirements) for func_result in function_results: - result.append(Message(role="tool", contents=[func_result])) + tool_message = Message(role="tool", contents=[func_result]) + result.append(tool_message) # Then user message with remaining content (if any) if other_contents: @@ -2811,6 +2841,33 @@ def _split_service_session_input( return snapshot_messages[len(stored_snapshot_messages) :], snapshot_messages +def _mark_fides_state_covered_messages( + agent: SupportsAgentRun, + messages: Sequence[Message], +) -> None: + """Mark backend-restored replay messages for FIDES without importing the experimental module.""" + if not messages: + return + for provider in getattr(agent, "context_providers", ()): + marker = getattr(provider, "_mark_replay_state_covered_messages", None) + if callable(marker): + marker(messages) + + +def _fold_fides_uncovered_replay_messages( + agent: SupportsAgentRun, + session: AgentSession, + messages: Sequence[Message], + *, + complete_replay_messages: Sequence[Message] | None = None, +) -> None: + """Fold uncovered replay into FIDES state before a snapshot-only return.""" + for provider in getattr(agent, "context_providers", ()): + fold = getattr(provider, "_fold_uncovered_replay_messages", None) + if callable(fold): + fold(session, messages, complete_replay_messages=complete_replay_messages) + + def _legacy_tool_message_approval_resume( messages: Sequence[Mapping[str, Any]], *, @@ -3014,6 +3071,7 @@ async def _run_agent_stream( ) stored_snapshot = snapshot_session.stored + backend_snapshot_message_ids: set[int] = set() stored_pending_approval_interrupt_ids: set[str] = set() seeded_resume_from_snapshot = False if stored_snapshot is not None: @@ -3096,6 +3154,8 @@ async def _run_agent_stream( stored_interrupt=stored_snapshot.interrupt, ) raw_messages = provider_suffix + if not config.use_service_session: + backend_snapshot_message_ids = {id(message) for message in raw_messages[: len(stored_snapshot.messages)]} # Initialize flow state with stored state plus request-provided overrides; # endpoint-deferred defaults apply only to keys missing from both. @@ -3221,7 +3281,9 @@ async def _run_agent_stream( raw_messages, sanitize_tool_history=not (config.use_service_session and stored_snapshot is not None), protected_tool_call_ids=protected_tool_call_ids, + backend_snapshot_message_ids=backend_snapshot_message_ids, ) + _mark_fides_state_covered_messages(agent, _take_backend_snapshot_messages(messages)) if snapshot_seed_messages is not None: _, snapshot_messages = normalize_agui_input_messages( @@ -3384,6 +3446,23 @@ async def save_thread_snapshot( ), ) + def fold_fides_replay_before_snapshot_save() -> None: + complete_replay_snapshot = snapshot_messages + if resume_payload is not None and not seeded_resume_from_snapshot and snapshot_seed_messages is None: + complete_replay_snapshot = snapshot_session.resume_seeded_messages(complete_replay_snapshot) + complete_replay_messages, _ = normalize_agui_input_messages( + complete_replay_snapshot, + sanitize_tool_history=not (config.use_service_session and stored_snapshot is not None), + protected_tool_call_ids=protected_tool_call_ids, + ) + _fold_fides_uncovered_replay_messages( + agent, + session, + messages, + complete_replay_messages=complete_replay_messages, + ) + _mark_fides_state_covered_messages(agent, messages) + authenticated_cancellations = [ _approval_observer_response(occurrence, cancelled=True) for interrupt_id in cancelled_resume_ids @@ -3575,6 +3654,7 @@ async def save_thread_snapshot( forwarded_executions=forwarded_executions, ) resolved_approval_results = retained_approval_results + newly_resolved_approval_results + _mark_fides_state_covered_messages(agent, _take_backend_snapshot_messages(messages)) # Defense-in-depth: replace approval payloads in snapshot with actual tool results # so CopilotKit does not re-send stale approval content on subsequent turns. @@ -3636,6 +3716,7 @@ def merge_snapshot_interrupts( persisted_messages = snapshot_messages if resume_payload is not None and not seeded_resume_from_snapshot and snapshot_seed_messages is None: persisted_messages = snapshot_session.resume_seeded_messages(persisted_messages) + fold_fides_replay_before_snapshot_save() await save_thread_snapshot( persisted_messages=persisted_messages, state=cast(dict[str, Any], make_json_safe(flow.current_state)) if flow.current_state else None, @@ -3669,6 +3750,7 @@ def merge_snapshot_interrupts( if resume_payload is not None and not seeded_resume_from_snapshot and snapshot_seed_messages is None: persisted_messages = snapshot_session.resume_seeded_messages(persisted_messages) preserved_interrupts = remaining_stored_interrupts(retired_approval_interrupt_ids) + fold_fides_replay_before_snapshot_save() await save_thread_snapshot( persisted_messages=persisted_messages, state=cast(dict[str, Any], make_json_safe(flow.current_state)) if flow.current_state else None, @@ -3677,6 +3759,7 @@ def merge_snapshot_interrupts( # Handle confirm_changes response (state confirmation flow - emit confirmation and stop) if is_confirm_changes_response: + fold_fides_replay_before_snapshot_save() confirm_additional_properties = cast(dict[str, Any], messages[-1].additional_properties or {}) confirm_interrupt_id = confirm_additional_properties.get("tool_call_id") confirm_remaining_interrupts = remaining_stored_interrupts( diff --git a/python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py b/python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py index 41adb4a7d2d..1a306b3c59b 100644 --- a/python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py +++ b/python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py @@ -10,6 +10,7 @@ import json import logging from collections import Counter +from collections.abc import Sequence from typing import Any, cast, get_args from urllib.parse import urlsplit @@ -49,6 +50,30 @@ _ALLOWED_MULTIMODAL_URI_SCHEMES = frozenset({"data", "http", "https"}) _APPROVAL_DECISION_IS_BOOLEAN_KEY = "_ag_ui_approval_decision_is_boolean" _LEGACY_MEDIA_PLACEHOLDER = "[Unsupported legacy media omitted by AG-UI URI policy.]" +_BACKEND_SNAPSHOT_ORIGIN_KEY = "_agent_framework_ag_ui_backend_snapshot_origin" +_BACKEND_SNAPSHOT_ORIGIN_MARKER = object() + + +def _mark_backend_snapshot_origin(message: Message) -> None: + """Mark a provider message as derived from backend-owned snapshot history.""" + message.additional_properties[_BACKEND_SNAPSHOT_ORIGIN_KEY] = _BACKEND_SNAPSHOT_ORIGIN_MARKER + + +def _copy_backend_snapshot_origin(source: Message, target: Message) -> None: + """Preserve private backend-snapshot provenance on a derived provider message.""" + if source.additional_properties.get(_BACKEND_SNAPSHOT_ORIGIN_KEY) is _BACKEND_SNAPSHOT_ORIGIN_MARKER: + _mark_backend_snapshot_origin(target) + + +def _take_backend_snapshot_messages(messages: Sequence[Message]) -> list[Message]: + """Return normalized backend-snapshot messages and clear their private transport marker.""" + snapshot_messages: list[Message] = [] + for message in messages: + if message.additional_properties.get(_BACKEND_SNAPSHOT_ORIGIN_KEY) is not _BACKEND_SNAPSHOT_ORIGIN_MARKER: + continue + message.additional_properties.pop(_BACKEND_SNAPSHOT_ORIGIN_KEY, None) + snapshot_messages.append(message) + return snapshot_messages def _append_synthetic_tool_results( @@ -57,6 +82,7 @@ def _append_synthetic_tool_results( result: str, *, excluded_tool_call_ids: set[str] | None = None, + backend_snapshot_origin: bool = False, ) -> None: excluded_tool_call_ids = excluded_tool_call_ids or set() # A call ID awaiting several occurrences still gets a single synthetic result. @@ -65,17 +91,18 @@ def _append_synthetic_tool_results( logger.info("Not injecting synthetic tool result for non-abandoned call_id=%s", pending_call_id) continue logger.info("Injecting synthetic tool result for pending call_id=%s", pending_call_id) - sanitized.append( - Message( - role="tool", - contents=[ - Content.from_function_result( - call_id=pending_call_id, - result=result, - ) - ], - ) + synthetic_result = Message( + role="tool", + contents=[ + Content.from_function_result( + call_id=pending_call_id, + result=result, + ) + ], ) + if backend_snapshot_origin: + _mark_backend_snapshot_origin(synthetic_result) + sanitized.append(synthetic_result) def _tool_call_ids_per_occurrence(contents: list[Content]) -> list[str]: @@ -113,6 +140,7 @@ def _sanitize_tool_history( sanitized: list[Message] = [] pending_tool_call_ids: list[str] | None = None pending_confirm_changes_id: str | None = None + pending_tool_calls_from_backend_snapshot = False protected_ids = set(protected_tool_call_ids or set()) def non_abandoned_tool_call_ids(start: int) -> set[str]: @@ -133,9 +161,11 @@ def non_abandoned_tool_call_ids(start: int) -> set[str]: pending_tool_call_ids, "Tool execution skipped - assistant continued before the tool result was available.", excluded_tool_call_ids=non_abandoned_tool_call_ids(idx), + backend_snapshot_origin=pending_tool_calls_from_backend_snapshot, ) pending_tool_call_ids = None pending_confirm_changes_id = None + pending_tool_calls_from_backend_snapshot = False tool_ids = _tool_call_ids_per_occurrence(msg.contents or []) confirm_changes_call = None @@ -159,6 +189,7 @@ def non_abandoned_tool_call_ids(start: int) -> set[str]: if filtered_contents: # Create a new message without confirm_changes to avoid mutating the input filtered_msg = Message(role=msg.role, contents=filtered_contents) + _copy_backend_snapshot_origin(msg, filtered_msg) sanitized.append(filtered_msg) # If no contents left after filtering, don't append anything @@ -172,6 +203,9 @@ def non_abandoned_tool_call_ids(start: int) -> set[str]: sanitized.append(msg) pending_tool_call_ids = tool_ids if tool_ids else None + pending_tool_calls_from_backend_snapshot = bool(tool_ids) and ( + msg.additional_properties.get(_BACKEND_SNAPSHOT_ORIGIN_KEY) is _BACKEND_SNAPSHOT_ORIGIN_MARKER + ) pending_confirm_changes_id = ( str(confirm_changes_call.call_id) if confirm_changes_call and confirm_changes_call.call_id else None ) @@ -209,6 +243,8 @@ def non_abandoned_tool_call_ids(start: int) -> set[str]: ) ], ) + if pending_tool_calls_from_backend_snapshot: + _mark_backend_snapshot_origin(synthetic_result) sanitized.append(synthetic_result) if pending_tool_call_ids: pending_tool_call_ids = [ @@ -240,6 +276,8 @@ def non_abandoned_tool_call_ids(start: int) -> set[str]: ) ], ) + if pending_tool_calls_from_backend_snapshot: + _mark_backend_snapshot_origin(synthetic_result) sanitized.append(synthetic_result) if pending_tool_call_ids: pending_tool_call_ids = [ @@ -260,12 +298,16 @@ def non_abandoned_tool_call_ids(start: int) -> set[str]: pending_tool_call_ids, "Tool execution skipped - user provided follow-up message", excluded_tool_call_ids=non_abandoned_tool_call_ids(idx + 1), + backend_snapshot_origin=pending_tool_calls_from_backend_snapshot, ) pending_tool_call_ids = None pending_confirm_changes_id = None + pending_tool_calls_from_backend_snapshot = False sanitized.append(msg) pending_confirm_changes_id = None + if not pending_tool_call_ids: + pending_tool_calls_from_backend_snapshot = False continue if role_value == "tool": @@ -300,11 +342,13 @@ def non_abandoned_tool_call_ids(start: int) -> set[str]: pending_tool_call_ids, "Tool execution skipped - conversation continued before the tool result was available.", excluded_tool_call_ids=non_abandoned_tool_call_ids(idx), + backend_snapshot_origin=pending_tool_calls_from_backend_snapshot, ) sanitized.append(msg) pending_tool_call_ids = None pending_confirm_changes_id = None + pending_tool_calls_from_backend_snapshot = False if pending_tool_call_ids: logger.info( @@ -316,6 +360,7 @@ def non_abandoned_tool_call_ids(start: int) -> set[str]: pending_tool_call_ids, "Tool execution skipped - conversation ended before the tool result was available.", excluded_tool_call_ids=non_abandoned_tool_call_ids(len(messages)), + backend_snapshot_origin=pending_tool_calls_from_backend_snapshot, ) return sanitized @@ -661,6 +706,7 @@ def normalize_agui_input_messages( *, sanitize_tool_history: bool = True, protected_tool_call_ids: set[str] | None = None, + backend_snapshot_message_ids: set[int] | None = None, ) -> tuple[list[Message], list[dict[str, Any]]]: """Normalize raw AG-UI messages into provider and snapshot formats. @@ -671,8 +717,13 @@ def normalize_agui_input_messages( where pending-request responses must come explicitly from interrupt resume. protected_tool_call_ids: Server-owned tool calls that are still eligible to complete and must not receive synthetic skipped results. + backend_snapshot_message_ids: Object identities of raw messages restored + from the backend-owned Thread Snapshot. """ - provider_messages = agui_messages_to_agent_framework(messages) + provider_messages = agui_messages_to_agent_framework( + messages, + backend_snapshot_message_ids=backend_snapshot_message_ids, + ) if sanitize_tool_history: provider_messages = _sanitize_tool_history(provider_messages, protected_tool_call_ids=protected_tool_call_ids) provider_messages = _deduplicate_messages(provider_messages) @@ -698,11 +749,17 @@ def _deserialize_model_replay_items(serialized_items: Any) -> list[Content] | No return None -def agui_messages_to_agent_framework(messages: list[dict[str, Any]]) -> list[Message]: +def agui_messages_to_agent_framework( + messages: list[dict[str, Any]], + *, + backend_snapshot_message_ids: set[int] | None = None, +) -> list[Message]: """Convert AG-UI messages to Agent Framework format. Args: - messages: List of AG-UI messages + messages: List of AG-UI messages. + backend_snapshot_message_ids: Object identities of messages restored + from the backend-owned Thread Snapshot. Returns: List of Agent Framework Message objects @@ -801,6 +858,12 @@ def _filter_modified_args( return {key: value for key, value in modified_args.items() if key in allowed_keys} result: list[Message] = [] + + def _append_message(raw_message: dict[str, Any], message: Message) -> None: + if backend_snapshot_message_ids is not None and id(raw_message) in backend_snapshot_message_ids: + message.additional_properties[_BACKEND_SNAPSHOT_ORIGIN_KEY] = _BACKEND_SNAPSHOT_ORIGIN_MARKER + result.append(message) + for msg in messages: # Handle standard tool result messages early (role="tool") to preserve provider invariants # This path maps AG‑UI tool messages to function_result content with the correct tool_call_id @@ -850,7 +913,7 @@ def _filter_modified_args( ) if "id" in msg: chat_msg.message_id = msg["id"] - result.append(chat_msg) + _append_message(msg, chat_msg) continue if msg.get(_AGUI_HOST_PAYLOAD_OMITTED_KEY) is True: @@ -861,7 +924,7 @@ def _filter_modified_args( ) if "id" in msg: chat_msg.message_id = msg["id"] - result.append(chat_msg) + _append_message(msg, chat_msg) continue is_approval = parsed is not None and "accepted" in parsed @@ -993,7 +1056,7 @@ def _filter_modified_args( ) if "id" in msg: chat_msg.message_id = msg["id"] - result.append(chat_msg) + _append_message(msg, chat_msg) continue # Cast result_content to acceptable type for function_result content @@ -1012,7 +1075,7 @@ def _filter_modified_args( ) if "id" in msg: chat_msg.message_id = msg["id"] - result.append(chat_msg) + _append_message(msg, chat_msg) continue # Backend tool rendering payloads without an explicit role @@ -1028,7 +1091,7 @@ def _filter_modified_args( ) if "id" in msg: chat_msg.message_id = msg["id"] - result.append(chat_msg) + _append_message(msg, chat_msg) continue # If assistant message includes tool calls, convert to Content.from_function_call(s) @@ -1064,7 +1127,7 @@ def _filter_modified_args( chat_msg = Message(role="assistant", contents=contents) if "id" in msg: chat_msg.message_id = msg["id"] - result.append(chat_msg) + _append_message(msg, chat_msg) continue # No special handling required for assistant/plain messages here @@ -1108,7 +1171,7 @@ def _filter_modified_args( if "id" in msg: chat_msg.message_id = msg["id"] - result.append(chat_msg) + _append_message(msg, chat_msg) return result diff --git a/python/packages/ag-ui/tests/ag_ui/test_endpoint.py b/python/packages/ag-ui/tests/ag_ui/test_endpoint.py index d1fe7accdbd..b28748510d4 100644 --- a/python/packages/ag-ui/tests/ag_ui/test_endpoint.py +++ b/python/packages/ag-ui/tests/ag_ui/test_endpoint.py @@ -5375,6 +5375,665 @@ def _is_policy_violation(tool_results: list[str]) -> bool: return any("Policy violation" in result for result in tool_results) +async def test_endpoint_fides_snapshot_provenance_survives_reasoning_normalization( + streaming_chat_client_stub: Any, +) -> None: + """A new client tool result cannot inherit restored-snapshot FIDES provenance.""" + thread_id = "thread-fides-reasoning-provenance" + session_id = _session_id_for_thread(scope="test", thread_id=thread_id) + executed: list[str] = [] + + def public_sink() -> str: + executed.append("public_sink") + return "published" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + del options, kwargs + assert all( + "_agent_framework_ag_ui_backend_snapshot_origin" not in message.additional_properties + for message in messages + ) + assert any( + content.type == "function_result" and content.call_id == "client-call" + for message in messages + for content in message.contents + ) + if any( + content.type == "function_result" and content.call_id == "sink-call" + for message in messages + for content in message.contents + ): + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")]) + return + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="sink-call", name="public_sink", arguments={})], + ) + + config = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=public_sink, + name="public_sink", + description="Publish data to a public sink", + additional_properties={"source_integrity": "trusted"}, + ) + ], + context_providers=[config], + ) + store = InMemoryAGUIThreadSnapshotStore() + stored_messages: list[dict[str, Any]] = [ + {"id": "user-1", "role": "user", "content": "Use the client lookup tool."}, + {"id": "reasoning-1", "role": "reasoning", "content": "Stored model reasoning."}, + { + "id": "assistant-1", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "client-call", + "type": "function", + "function": {"name": "client_lookup", "arguments": "{}"}, + } + ], + }, + ] + await store.save( + scope="test", + thread_id=thread_id, + snapshot=AGUIThreadSnapshot( + messages=stored_messages, + session_state={ + config.source_id: { + "scope_id": session_id, + "context_label": _TRUSTED_PUBLIC_LABEL, + } + }, + ), + ) + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-provenance", + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + response = client.post( + "/fides-provenance", + json={ + "runId": "run-fides-provenance", + "threadId": thread_id, + "messages": [ + *stored_messages, + { + "id": "tool-result-1", + "role": "tool", + "toolCallId": "client-call", + "content": "client-controlled external result", + }, + ], + }, + ) + assert response.status_code == 200 + events = _decode_sse_events(response) + assert not [event for event in events if event["type"] == "RUN_ERROR"] + assert executed == [] + assert _is_policy_violation([event["content"] for event in events if event["type"] == "TOOL_CALL_RESULT"]) + snapshot = await store.get(scope="test", thread_id=thread_id) + assert snapshot is not None + assert snapshot.session_state is not None + context_label = json.loads(snapshot.session_state[config.source_id]["context_label"]) + assert context_label["integrity"] == "untrusted" + assert context_label["confidentiality"] == "private" + + +async def test_endpoint_fides_synthetic_skipped_result_keeps_snapshot_provenance( + streaming_chat_client_stub: Any, +) -> None: + """A framework-synthesized skipped result does not taint restored FIDES state.""" + thread_id = "thread-fides-synthetic-result" + session_id = _session_id_for_thread(scope="test", thread_id=thread_id) + executed: list[str] = [] + + def protected_action() -> str: + executed.append("protected") + return "protected result" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + del options, kwargs + assert all( + "_agent_framework_ag_ui_backend_snapshot_origin" not in message.additional_properties + for message in messages + ) + assert any( + content.type == "function_result" + and content.call_id == "client-call" + and "Tool execution skipped" in (content.result or "") + for message in messages + for content in message.contents + ) + if any( + content.type == "function_result" and content.call_id == "protected-call" + for message in messages + for content in message.contents + ): + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")]) + return + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="protected-call", name="protected_action", arguments={})], + ) + + config = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=protected_action, + name="protected_action", + description="Run an unrelated protected action", + additional_properties={ + "source_integrity": "trusted", + "max_allowed_confidentiality": "public", + }, + ) + ], + context_providers=[config], + ) + store = InMemoryAGUIThreadSnapshotStore() + await store.save( + scope="test", + thread_id=thread_id, + snapshot=AGUIThreadSnapshot( + messages=[ + {"id": "user-1", "role": "user", "content": "Use the client lookup tool."}, + { + "id": "assistant-1", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "client-call", + "type": "function", + "function": {"name": "client_lookup", "arguments": "{}"}, + } + ], + }, + ], + session_state={ + config.source_id: { + "scope_id": session_id, + "context_label": _TRUSTED_PUBLIC_LABEL, + } + }, + ), + ) + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-synthetic-result", + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + response = client.post( + "/fides-synthetic-result", + json={ + "runId": "run-fides-synthetic-result", + "threadId": thread_id, + "messages": [{"id": "user-2", "role": "user", "content": "Continue without the client tool."}], + }, + ) + + assert response.status_code == 200 + events = _decode_sse_events(response) + assert not [event for event in events if event["type"] == "RUN_ERROR"] + assert executed == ["protected"] + snapshot = await store.get(scope="test", thread_id=thread_id) + assert snapshot is not None + assert snapshot.session_state is not None + context_label = json.loads(snapshot.session_state[config.source_id]["context_label"]) + assert context_label["integrity"] == "trusted" + assert context_label["confidentiality"] == "public" + + +async def test_endpoint_fides_normal_continuation_reconstructs_missing_context_label( + streaming_chat_client_stub: Any, +) -> None: + """A normal continuation reconstructs backend history when FIDES state is absent.""" + thread_id = "thread-fides-normal-missing-state" + executed: list[str] = [] + + def protected_action() -> str: + executed.append("protected") + return "protected result" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + del options, kwargs + assert any( + content.type == "function_result" + and content.call_id == "external-call" + and content.result == "stored external result" + for message in messages + for content in message.contents + ) + if any( + content.type == "function_result" and content.call_id == "protected-call" + for message in messages + for content in message.contents + ): + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")]) + return + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="protected-call", name="protected_action", arguments={})], + ) + + config = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=protected_action, + name="protected_action", + description="Run an unrelated protected action", + additional_properties={ + "source_integrity": "trusted", + "max_allowed_confidentiality": "public", + }, + ) + ], + context_providers=[config], + ) + store = InMemoryAGUIThreadSnapshotStore() + await store.save( + scope="test", + thread_id=thread_id, + snapshot=AGUIThreadSnapshot( + messages=[ + {"id": "user-1", "role": "user", "content": "Look up the external record."}, + { + "id": "assistant-external-call", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "external-call", + "type": "function", + "function": {"name": "client_lookup", "arguments": "{}"}, + } + ], + }, + { + "id": "external-result", + "role": "tool", + "toolCallId": "external-call", + "content": "stored external result", + }, + ], + ), + ) + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-normal-missing-state", + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + response = client.post( + "/fides-normal-missing-state", + json={ + "runId": "run-fides-normal-missing-state", + "threadId": thread_id, + "messages": [{"id": "user-2", "role": "user", "content": "Run the protected action."}], + }, + ) + + assert response.status_code == 200 + events = _decode_sse_events(response) + assert not [event for event in events if event["type"] == "RUN_ERROR"] + assert executed == [] + assert _is_policy_violation([event["content"] for event in events if event["type"] == "TOOL_CALL_RESULT"]) + snapshot = await store.get(scope="test", thread_id=thread_id) + assert snapshot is not None + assert snapshot.session_state is not None + context_label = json.loads(snapshot.session_state[config.source_id]["context_label"]) + assert context_label["integrity"] == "untrusted" + assert context_label["confidentiality"] == "private" + + +async def test_endpoint_fides_confirm_changes_folds_fresh_client_result( + streaming_chat_client_stub: Any, +) -> None: + """A confirm-only return folds a fresh client result before snapshot persistence.""" + thread_id = "thread-fides-confirm-client-result" + executed: list[str] = [] + model_calls = 0 + + def protected_action() -> str: + executed.append("protected") + return "protected result" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + nonlocal model_calls + del options, kwargs + model_calls += 1 + assert any( + content.type == "function_result" and content.call_id == "client-call" + for message in messages + for content in message.contents + ) + if any( + content.type == "function_result" and content.call_id == "protected-call" + for message in messages + for content in message.contents + ): + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")]) + return + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="protected-call", name="protected_action", arguments={})], + ) + + config = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=protected_action, + name="protected_action", + description="Run an unrelated protected action", + additional_properties={ + "source_integrity": "trusted", + "max_allowed_confidentiality": "public", + }, + ) + ], + context_providers=[config], + ) + store = InMemoryAGUIThreadSnapshotStore() + await store.save( + scope="test", + thread_id=thread_id, + snapshot=AGUIThreadSnapshot( + messages=[ + {"id": "user-1", "role": "user", "content": "Draft the plan"}, + { + "id": "assistant-1", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "client-call", + "type": "function", + "function": {"name": "client_lookup", "arguments": "{}"}, + }, + { + "id": "confirm-call", + "type": "function", + "function": {"name": "confirm_changes", "arguments": "{}"}, + }, + ], + }, + ], + session_state={ + config.source_id: { + "scope_id": _session_id_for_thread(scope="test", thread_id=thread_id), + "context_label": _TRUSTED_PUBLIC_LABEL, + } + }, + ), + ) + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-confirm-client-result", + state_schema={"steps": {"type": "array", "items": {"type": "object"}}}, + predict_state_config={"steps": {"tool": "draft_steps", "tool_argument": "steps"}}, + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + confirm_response = client.post( + "/fides-confirm-client-result", + json={ + "runId": "run-fides-confirm-resume", + "threadId": thread_id, + "messages": [ + { + "id": "client-result-1", + "role": "tool", + "toolCallId": "client-call", + "content": "fresh external client result", + }, + { + "id": "confirm-result-1", + "role": "tool", + "toolCallId": "confirm-call", + "content": json.dumps( + { + "accepted": True, + "steps": [], + "function_call_id": "unknown-confirm-target", + } + ), + }, + ], + }, + ) + + assert confirm_response.status_code == 200 + assert model_calls == 0 + snapshot = await store.get(scope="test", thread_id=thread_id) + assert snapshot is not None + assert snapshot.session_state is not None + context_label = json.loads(snapshot.session_state[config.source_id]["context_label"]) + assert context_label["integrity"] == "untrusted" + assert context_label["confidentiality"] == "private" + + protected_response = client.post( + "/fides-confirm-client-result", + json={ + "runId": "run-fides-confirm-protected", + "threadId": thread_id, + "messages": [{"id": "user-2", "role": "user", "content": "Run the protected action"}], + }, + ) + + assert protected_response.status_code == 200 + protected_events = _decode_sse_events(protected_response) + assert not [event for event in protected_events if event["type"] == "RUN_ERROR"] + assert executed == [] + assert _is_policy_violation([event["content"] for event in protected_events if event["type"] == "TOOL_CALL_RESULT"]) + + +@pytest.mark.parametrize("confirmation_transport", ["message", "resume"]) +async def test_endpoint_fides_confirm_changes_reconstructs_missing_context_label( + streaming_chat_client_stub: Any, + confirmation_transport: str, +) -> None: + """A confirm-only return reconstructs stored history when FIDES state lacks its label.""" + thread_id = f"thread-fides-confirm-missing-state-{confirmation_transport}" + executed: list[str] = [] + model_calls = 0 + + def protected_action() -> str: + executed.append("protected") + return "protected result" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + nonlocal model_calls + del messages, options, kwargs + model_calls += 1 + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="protected-call", name="protected_action", arguments={})], + ) + + config = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=protected_action, + name="protected_action", + description="Run an unrelated protected action", + additional_properties={ + "source_integrity": "trusted", + "max_allowed_confidentiality": "public", + }, + ) + ], + context_providers=[config], + ) + store = InMemoryAGUIThreadSnapshotStore() + confirm_payload = {"accepted": True, "steps": []} + await store.save( + scope="test", + thread_id=thread_id, + snapshot=AGUIThreadSnapshot( + messages=[ + {"id": "user-1", "role": "user", "content": "Look up the external record"}, + { + "id": "assistant-external-call", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "external-call", + "type": "function", + "function": {"name": "client_lookup", "arguments": "{}"}, + } + ], + }, + { + "id": "external-result", + "role": "tool", + "toolCallId": "external-call", + "content": "stored external result", + }, + { + "id": "assistant-confirm-call", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "confirm-call", + "type": "function", + "function": { + "name": "confirm_changes", + "arguments": json.dumps({"function_call_id": "unknown-confirm-target"}), + }, + } + ], + }, + ], + session_state={ + config.source_id: { + "scope_id": _session_id_for_thread(scope="test", thread_id=thread_id), + } + }, + ), + ) + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-confirm-missing-state", + state_schema={"steps": {"type": "array", "items": {"type": "object"}}}, + predict_state_config={"steps": {"tool": "draft_steps", "tool_argument": "steps"}}, + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + confirmation_request: dict[str, Any] = { + "runId": f"run-fides-confirm-missing-{confirmation_transport}", + "threadId": thread_id, + "messages": [], + } + if confirmation_transport == "message": + confirmation_request["messages"] = [ + { + "id": "confirm-result", + "role": "tool", + "toolCallId": "confirm-call", + "content": json.dumps(confirm_payload), + } + ] + else: + confirmation_request["resume"] = [ + { + "interruptId": "confirm-call", + "status": "resolved", + "payload": json.dumps(confirm_payload), + } + ] + + with TestClient(app) as client: + confirm_response = client.post("/fides-confirm-missing-state", json=confirmation_request) + + assert confirm_response.status_code == 200 + assert not [event for event in _decode_sse_events(confirm_response) if event["type"] == "RUN_ERROR"] + assert model_calls == 0 + snapshot = await store.get(scope="test", thread_id=thread_id) + assert snapshot is not None + assert snapshot.session_state is not None + context_label = json.loads(snapshot.session_state[config.source_id]["context_label"]) + assert context_label["integrity"] == "untrusted" + assert context_label["confidentiality"] == "private" + + protected_response = client.post( + "/fides-confirm-missing-state", + json={ + "runId": f"run-fides-confirm-missing-protected-{confirmation_transport}", + "threadId": thread_id, + "messages": [{"id": "user-2", "role": "user", "content": "Run the protected action"}], + }, + ) + + assert protected_response.status_code == 200 + protected_events = _decode_sse_events(protected_response) + assert not [event for event in protected_events if event["type"] == "RUN_ERROR"] + assert executed == [] + assert _is_policy_violation([event["content"] for event in protected_events if event["type"] == "TOOL_CALL_RESULT"]) + + @pytest.mark.parametrize("middleware_kind", ["direct", "bundle", "wrapper", "client"]) @pytest.mark.parametrize("session_state_key", [None, "custom_security_state"], ids=["default-key", "custom-key"]) @pytest.mark.parametrize("state_origin", ["request", "snapshot"]) @@ -7884,6 +8543,168 @@ async def test_endpoint_collected_approval_preserves_original_expiration( assert executed == [] +@pytest.mark.parametrize("mixed_decisions", [False, True], ids=["deny-only", "mixed-approve-deny"]) +async def test_endpoint_fides_authenticated_rejection_preserves_restored_context( + streaming_chat_client_stub: Any, + mixed_decisions: bool, +) -> None: + """A lifecycle-authenticated denial remains control-plane replay, not external data.""" + phase = "approval" + executed: list[str] = [] + provider_calls = 0 + + def guarded_action() -> str: + executed.append("guarded") + return "guarded result" + + def guarded_action_two() -> str: + executed.append("guarded-two") + return "guarded result two" + + def protected_action() -> str: + executed.append("protected") + return "protected result" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + nonlocal provider_calls + del options, kwargs + provider_calls += 1 + if phase == "approval": + guarded_calls = [ + Content.from_function_call( + call_id="guarded-call", + name="guarded_action", + arguments={}, + ) + ] + if mixed_decisions: + guarded_calls.append( + Content.from_function_call( + call_id="guarded-call-two", + name="guarded_action_two", + arguments={}, + ) + ) + yield ChatResponseUpdate( + role="assistant", + contents=guarded_calls, + ) + return + if phase == "denied": + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("denied")]) + return + if any( + content.type == "function_result" and content.call_id == "protected-call" + for message in messages + for content in message.contents + ): + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")]) + return + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="protected-call", name="protected_action", arguments={})], + ) + + security = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=guarded_action, + name="guarded_action", + description="Require approval", + approval_mode="always_require", + ), + FunctionTool( + func=protected_action, + name="protected_action", + description="Run an unrelated protected action", + additional_properties={ + "source_integrity": "trusted", + "max_allowed_confidentiality": "public", + }, + ), + FunctionTool( + func=guarded_action_two, + name="guarded_action_two", + description="Run an already-approved sibling action", + approval_mode="never_require", + ), + ], + context_providers=[security], + ) + store = InMemoryAGUIThreadSnapshotStore() + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-authenticated-rejection", + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + pause = client.post( + "/fides-authenticated-rejection", + json={ + "runId": "run-rejection-pause", + "threadId": "thread-authenticated-rejection", + "messages": [{"id": "user-1", "role": "user", "content": "Prepare guarded action"}], + }, + ) + finished = [event for event in _decode_sse_events(pause) if event.get("type") == "RUN_FINISHED"] + interrupts = _run_finished_interrupts(finished[-1]) + assert len(interrupts) == 1 + approval_ids = {interrupt["toolCallId"]: interrupt["id"] for interrupt in interrupts} + resume_entries = [ + { + "interruptId": approval_ids["guarded-call"], + "status": "resolved", + "payload": {"approved": False}, + } + ] + + phase = "denied" + denied = client.post( + "/fides-authenticated-rejection", + json={ + "runId": "run-rejection-denied", + "threadId": "thread-authenticated-rejection", + "messages": [], + "resume": resume_entries, + }, + ) + assert denied.status_code == 200 + assert not [event for event in _decode_sse_events(denied) if event.get("type") == "RUN_ERROR"] + snapshot = await store.get(scope="test", thread_id="thread-authenticated-rejection") + assert snapshot is not None + assert snapshot.interrupt is None + assert snapshot.session_state is not None + label = json.loads(snapshot.session_state[security.source_id]["context_label"]) + assert label["integrity"] == "trusted" + assert label["confidentiality"] == "public" + + phase = "protected" + protected = client.post( + "/fides-authenticated-rejection", + json={ + "runId": "run-rejection-protected", + "threadId": "thread-authenticated-rejection", + "messages": [{"id": "user-2", "role": "user", "content": "Run protected action"}], + }, + ) + + assert protected.status_code == 200 + assert not [event for event in _decode_sse_events(protected) if event.get("type") == "RUN_ERROR"] + assert executed == (["guarded-two", "protected"] if mixed_decisions else ["protected"]) + assert provider_calls == 4 + + async def test_endpoint_agent_approval_cancel_discards_queued_tool_approval(streaming_chat_client_stub): """Cancelling a queued approval batch must not replay stale approval prompts on the next user turn.""" client, executed, messages_received, state, _ = _build_tool_approval_queue_endpoint(streaming_chat_client_stub) @@ -12754,6 +13575,186 @@ async def updates() -> AsyncIterator[AgentResponseUpdate]: assert "Tool call invocation was rejected by user" in str(snapshot.messages) +async def test_endpoint_fides_rejection_folds_client_result_before_provider_failure( + streaming_chat_client_stub: Any, +) -> None: + """A pre-run rejection snapshot persists fresh client-result restrictions.""" + from agent_framework import AgentResponse, ResponseInvalidatedException, ResponseStream + + phase = "pause" + executed: list[str] = [] + + def guarded_action() -> str: + return "guarded result" + + def protected_action() -> str: + executed.append("protected") + return "protected result" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + del options, kwargs + if phase == "pause": + yield ChatResponseUpdate( + role="assistant", + contents=[ + Content.from_function_call( + call_id="guarded-call", + name="guarded_action", + arguments={}, + ) + ], + ) + return + if any( + content.type == "function_result" and content.call_id == "protected-call" + for message in messages + for content in message.contents + ): + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")]) + return + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="protected-call", name="protected_action", arguments={})], + ) + + config = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=guarded_action, + name="guarded_action", + description="Require approval", + approval_mode="always_require", + ), + FunctionTool( + func=protected_action, + name="protected_action", + description="Run an unrelated protected action", + additional_properties={ + "source_integrity": "trusted", + "max_allowed_confidentiality": "public", + }, + ), + ], + context_providers=[config], + ) + store = InMemoryAGUIThreadSnapshotStore() + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-rejection-provider-failure", + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + pause_response = client.post( + "/fides-rejection-provider-failure", + json={ + "runId": "run-fides-rejection-pause", + "threadId": "thread-fides-rejection-provider-failure", + "messages": [{"id": "user-1", "role": "user", "content": "Prepare the guarded action"}], + }, + ) + pause_finished = [event for event in _decode_sse_events(pause_response) if event.get("type") == "RUN_FINISHED"] + approval_id = _run_finished_interrupts(pause_finished[-1])[0]["id"] + snapshot = await store.get(scope="test", thread_id="thread-fides-rejection-provider-failure") + assert snapshot is not None + assert snapshot.session_state is not None + initial_label = json.loads(snapshot.session_state[config.source_id]["context_label"]) + assert initial_label["integrity"] == "trusted" + assert initial_label["confidentiality"] == "public" + snapshot.messages.append( + { + "id": "assistant-client-call", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "client-call", + "type": "function", + "function": {"name": "client_lookup", "arguments": "{}"}, + } + ], + } + ) + await store.save( + scope="test", + thread_id="thread-fides-rejection-provider-failure", + snapshot=snapshot, + ) + + original_run = agent.run + + def failing_run(*args: Any, **kwargs: Any) -> Any: + if not kwargs.get("stream", False): + return original_run(*args, **kwargs) + + async def updates() -> AsyncIterator[AgentResponseUpdate]: + if False: # pragma: no cover + yield AgentResponseUpdate() + raise ResponseInvalidatedException("provider preparation failed") + + return ResponseStream(updates(), finalizer=AgentResponse.from_updates) + + agent.run = failing_run # type: ignore[assignment, method-assign] # ty: ignore[invalid-assignment] + rejected_response = client.post( + "/fides-rejection-provider-failure", + json={ + "runId": "run-fides-rejection-failure", + "threadId": "thread-fides-rejection-provider-failure", + "messages": [ + { + "id": "client-result", + "role": "tool", + "toolCallId": "client-call", + "content": "fresh external client result", + } + ], + "resume": [ + { + "interruptId": approval_id, + "status": "resolved", + "payload": {"approved": False}, + } + ], + }, + ) + rejected_events = _decode_sse_events(rejected_response) + assert [event["code"] for event in rejected_events if event.get("type") == "RUN_ERROR"] == [ + "ResponseInvalidatedException" + ] + failed_snapshot = await store.get(scope="test", thread_id="thread-fides-rejection-provider-failure") + assert failed_snapshot is not None + assert failed_snapshot.session_state is not None + failed_label = json.loads(failed_snapshot.session_state[config.source_id]["context_label"]) + assert failed_label["integrity"] == "untrusted" + assert failed_label["confidentiality"] == "private" + + agent.run = original_run # type: ignore[assignment, method-assign] # ty: ignore[invalid-assignment] + phase = "protected" + retry_response = client.post( + "/fides-rejection-provider-failure", + json={ + "runId": "run-fides-rejection-retry", + "threadId": "thread-fides-rejection-provider-failure", + "messages": [{"id": "user-2", "role": "user", "content": "Run the protected action"}], + }, + ) + + retry_events = _decode_sse_events(retry_response) + assert not [event for event in retry_events if event.get("type") == "RUN_ERROR"] + assert executed == [] + assert _is_policy_violation([event["content"] for event in retry_events if event["type"] == "TOOL_CALL_RESULT"]) + + async def test_agent_endpoint_approval_resume_preserves_other_stored_interrupts(): """A successful approval continuation retains unrelated stored interrupts.""" store = InMemoryAGUIThreadSnapshotStore() diff --git a/python/packages/core/agent_framework/security.py b/python/packages/core/agent_framework/security.py index 50efb9bcc46..6d51b8a92ea 100644 --- a/python/packages/core/agent_framework/security.py +++ b/python/packages/core/agent_framework/security.py @@ -141,6 +141,8 @@ _TOOL_POLICY_SOURCE_INTEGRITY_KEY = "source_integrity" _MAX_REPLAY_CONTENT_DEPTH = 32 _builtin_id = builtins.id +_REPLAY_STATE_COVERED_KEY = "_agent_framework_fides_replay_state_covered" +_REPLAY_STATE_COVERED_MARKER = object() _TOOL_SECURITY_POLICY_KEYS = frozenset({ _TOOL_POLICY_ACCEPTS_UNTRUSTED_KEY, _TOOL_POLICY_CONFIDENTIALITY_KEY, @@ -1368,15 +1370,16 @@ def _is_bound_approval_control( *, scope: _SecurityScope, approval_proofs: _ApprovalReplayProofs | None, + transport_authoritative: bool = False, ) -> Content | None: - """Return whether session state authenticates this approval control.""" + """Return whether trusted transport or session state authenticates this approval control.""" if content.type not in {"function_approval_request", "function_approval_response"} or content.id is None: return None authoritative_call = approval_proofs.function_call_for(content) if approval_proofs is not None else None function_call = authoritative_call or content.function_call if function_call is None or function_call.id is None or _function_call_replay_key(function_call) is None: return None - if authoritative_call is not None or _is_pending_approval_control(content, scope=scope): + if authoritative_call is not None or transport_authoritative or _is_pending_approval_control(content, scope=scope): return function_call return None @@ -1398,6 +1401,7 @@ def _bound_approval_call_keys( content, scope=scope, approval_proofs=approval_proofs, + transport_authoritative=_is_replay_state_covered(message), ) ) is not None @@ -1461,6 +1465,7 @@ def _replayed_message_label( content, scope=scope, approval_proofs=approval_proofs, + transport_authoritative=_is_replay_state_covered(message), ) is_bound_approval_control = authoritative_approval_call is not None has_bound_approval_control = has_bound_approval_control or is_bound_approval_control @@ -1520,6 +1525,18 @@ def _replayed_message_label( return combine_labels(*labels) +def _is_replay_state_covered(message: Message) -> bool: + """Return whether trusted transport code marked this replay as represented by restored state.""" + return message.additional_properties.get(_REPLAY_STATE_COVERED_KEY) is _REPLAY_STATE_COVERED_MARKER + + +def _clear_replay_state_covered_markers(messages: Sequence[Message]) -> None: + """Remove private replay provenance before messages reach a chat provider.""" + for message in messages: + if _is_replay_state_covered(message): + message.additional_properties.pop(_REPLAY_STATE_COVERED_KEY, None) + + # ============================================================================= # Security Middleware # ============================================================================= @@ -4416,6 +4433,53 @@ def _function_middleware_for_approval_resolution(self, session: AgentSession) -> """Return provider-customized middleware bound to one restored session scope.""" return self._middleware_for_scope(self._scope_for_session(session)) + def _mark_replay_state_covered_messages(self, messages: Sequence[Message]) -> None: + """Mark trusted transport-restored messages as already represented by session state.""" + for message in messages: + message.additional_properties[_REPLAY_STATE_COVERED_KEY] = _REPLAY_STATE_COVERED_MARKER + + def _fold_uncovered_replay_messages( + self, + session: AgentSession, + messages: Sequence[Message], + *, + complete_replay_messages: Sequence[Message] | None = None, + ) -> None: + """Fold new transport messages before a snapshot-only return persists them.""" + state = session.state.setdefault(self.source_id, {}) + if not isinstance(state, dict): + raise ValueError("Security provider state must be a dictionary.") + restored_context_label = _STATE_CONTEXT_LABEL in state + scope = _SecurityScope(cast(dict[str, Any], state), scope_id=session.session_id) + replay_messages = ( + messages if restored_context_label or complete_replay_messages is None else complete_replay_messages + ) + approval_proofs = self._capture_core_approval_replay_proofs( + session, + replay_messages, + ) + bound_call_keys = _bound_approval_call_keys( + replay_messages, + scope=scope, + approval_proofs=approval_proofs, + ) + replay_labels = [ + _replayed_message_label( + message, + reconstruct_unstamped=True, + scope=scope, + approval_proofs=approval_proofs, + bound_approval_call_keys=bound_call_keys, + ) + for message in replay_messages + if not restored_context_label or not _is_replay_state_covered(message) + ] + if replay_labels: + scope.context_label = combine_labels(scope.context_label, *replay_labels) + _clear_replay_state_covered_markers(messages) + if replay_messages is not messages: + _clear_replay_state_covered_markers(replay_messages) + @staticmethod def _capture_core_approval_replay_proofs( session: AgentSession, @@ -4634,7 +4698,8 @@ async def before_run( context: The invocation context - tools, instructions, and middleware are added here. state: The provider-scoped mutable state dict. """ - context.metadata[_RESTORED_CONTEXT_LABEL_METADATA_KEY] = _STATE_CONTEXT_LABEL in state + restored_context_label = _STATE_CONTEXT_LABEL in state + context.metadata[_RESTORED_CONTEXT_LABEL_METADATA_KEY] = restored_context_label scope = _SecurityScope(state, scope_id=session.session_id) history_source_ids = self._history_source_ids(agent) configured_labeler = self._message_labeler @@ -4714,7 +4779,7 @@ def fold_run_context(*, validate_continuation: bool) -> None: input_labels = [ _replayed_message_label( message, - reconstruct_unstamped=True, + reconstruct_unstamped=not restored_context_label or not _is_replay_state_covered(message), scope=scope, approval_proofs=approval_proofs, bound_approval_call_keys=input_bound_call_keys, @@ -4726,6 +4791,7 @@ def fold_run_context(*, validate_continuation: bool) -> None: scope.context_label, *input_labels, ) + _clear_replay_state_covered_markers(context.input_messages) middleware: list[MiddlewareTypes] = [*self._middleware_for_scope(scope)] @function_middleware diff --git a/python/packages/core/tests/test_security.py b/python/packages/core/tests/test_security.py index 6cba1cdfbc1..1f07ad0d168 100644 --- a/python/packages/core/tests/test_security.py +++ b/python/packages/core/tests/test_security.py @@ -3689,6 +3689,30 @@ async def execute(_: FunctionInvocationContext) -> list[Content]: assert executed is True + async def test_transport_marked_replay_uses_restored_state_and_clears_marker(self) -> None: + """Trusted transport replay keeps restored confidentiality without leaking its marker.""" + config = SecureAgentConfig() + session = AgentSession(session_id="transport-covered-history") + tracker, _ = await _get_session_security_middleware(config, session) + tracker._update_context_label(ContentLabel(integrity=IntegrityLabel.UNTRUSTED)) + restored_message = Message(role="assistant", contents=["Previously covered history"]) + config._mark_replay_state_covered_messages([restored_message]) + provider_context = SessionContext(session_id=session.session_id, input_messages=[restored_message]) + + await config.before_run( + agent=SimpleNamespace(context_providers=[config]), + session=session, + context=provider_context, + state=session.state.setdefault(config.source_id, {}), + ) + + scoped_tracker = provider_context.get_middleware()[0] + assert isinstance(scoped_tracker, LabelTrackingFunctionMiddleware) + label = scoped_tracker.get_context_label() + assert label.integrity == IntegrityLabel.UNTRUSTED + assert label.confidentiality == ConfidentialityLabel.PUBLIC + assert "_agent_framework_fides_replay_state_covered" not in restored_message.additional_properties + async def test_modified_hidden_placeholder_is_not_authenticated_by_client_flag(self) -> None: """A real variable ID does not authenticate a client-modified placeholder.""" config = SecureAgentConfig() From a13c157b25812741919853c59bf6bb3643fb39f7 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 5 Nov 2026 16:16:29 +0100 Subject: [PATCH 2/6] Python: address FIDES AG-UI review feedback --- .../ag-ui/agent_framework_ag_ui/_agent_run.py | 98 ++-- .../_message_adapters.py | 15 +- .../ag-ui/tests/ag_ui/test_endpoint.py | 485 ++++++++++++++++++ .../ag-ui/tests/ag_ui/test_message_hygiene.py | 30 ++ .../packages/core/agent_framework/security.py | 8 + python/packages/core/tests/test_security.py | 22 + 6 files changed, 625 insertions(+), 33 deletions(-) diff --git a/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py b/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py index 76dd7d3c6c1..4916b735adb 100644 --- a/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py +++ b/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py @@ -8,7 +8,7 @@ import json import logging import uuid -from collections.abc import AsyncIterable, Awaitable, Collection, Mapping, Sequence +from collections.abc import AsyncIterable, Awaitable, Callable, Collection, Mapping, Sequence from dataclasses import dataclass, field from functools import partial from typing import TYPE_CHECKING, Any, cast @@ -61,7 +61,7 @@ _try_execute_function_call_groups, # type: ignore normalize_function_invocation_configuration, ) -from agent_framework._types import ResponseStream +from agent_framework._types import APPROVAL_REJECTION_RESULT_KEY, ResponseStream from agent_framework.exceptions import AgentInvalidResponseException from agent_framework.observability import ( _use_telemetry_conversation_id, # pyright: ignore[reportPrivateUsage] @@ -92,6 +92,7 @@ from ._approval_state import _APPROVAL_SCOPE_INPUT_KEY, InMemoryAGUIApprovalStateStore, approval_state_thread_id from ._message_adapters import ( _APPROVAL_DECISION_IS_BOOLEAN_KEY, + _copy_backend_snapshot_origin, _mark_backend_snapshot_origin, _take_backend_snapshot_messages, normalize_agui_input_messages, @@ -1779,7 +1780,7 @@ async def _resolve_approval_responses( pending_response_groups: dict[object, tuple[ApprovalOccurrence, list[Content]]] = {} intents_by_response_content_id: dict[int, AuthorizedExecution] = {} authenticated_non_grants: list[Content] = [] - authenticated_rejection_call_ids: set[str] = set() + authenticated_rejection_occurrence_ids: set[str] = set() for response in approval_responses: resp_id = response.id function_call_id = response.function_call.call_id if response.function_call else None @@ -1877,7 +1878,7 @@ async def _resolve_approval_responses( ) if decision_is_boolean and primary_response.approved is False and not server_label: authenticated_non_grants.append(_approval_observer_response(pending_entry)) - authenticated_rejection_call_ids.add(pending_entry.identity.call_id) + authenticated_rejection_occurrence_ids.add(pending_entry.function_call_id) if ( (primary_response.approved is True or pending_entry.owner is ApprovalExecutionOwner.DEFERRED) and lifecycle is not None @@ -2094,7 +2095,14 @@ async def execute_local_call(approval: Content = approval, call_id: str = call_i if id(response) in deferred_response_content_ids } ) - _replace_approval_contents_with_results(messages, normalization_responses, replacement_groups) + resolved_contents = _replace_approval_contents_with_results(messages, normalization_responses, replacement_groups) + authenticated_rejection_result_ids = { + id(content) + for content in resolved_contents + if content.type == "function_result" + and content.id in authenticated_rejection_occurrence_ids + and content.additional_properties.get(APPROVAL_REJECTION_RESULT_KEY) is True + } # Post-process: Convert user messages with function_result content to proper tool messages. # After _replace_approval_contents_with_results, approved tool calls have their results @@ -2102,7 +2110,7 @@ async def execute_local_call(approval: Content = approval, call_id: str = call_i # This transformation ensures the message history is valid for the LLM provider. _convert_approval_results_to_tool_messages( messages, - authenticated_rejection_call_ids=authenticated_rejection_call_ids, + backend_result_content_ids=authenticated_rejection_result_ids, ) return approved_results @@ -2111,7 +2119,7 @@ async def execute_local_call(approval: Content = approval, call_id: str = call_i def _convert_approval_results_to_tool_messages( messages: list[Message], *, - authenticated_rejection_call_ids: set[str] | None = None, + backend_result_content_ids: set[int] | None = None, ) -> None: """Convert function_result content in user messages to proper tool messages. @@ -2123,18 +2131,25 @@ def _convert_approval_results_to_tool_messages( Args: messages: List of Message objects to process + backend_result_content_ids: Object identities of exact backend-synthesized + results whose derived tool messages retain snapshot provenance. """ result: list[Message] = [] + backend_result_ids = backend_result_content_ids or set() for msg in messages: - if authenticated_rejection_call_ids and any( - content.type == "function_result" - and content.call_id in authenticated_rejection_call_ids - and content.result == "Error: Tool call invocation was rejected by user." - for content in msg.contents - ): - _mark_backend_snapshot_origin(msg) if get_role_value(msg) != "user": + backend_contents = [content for content in msg.contents if id(content) in backend_result_ids] + if backend_contents and len(backend_contents) == len(msg.contents): + _mark_backend_snapshot_origin(msg) + elif backend_contents and get_role_value(msg) == "tool": + for content in msg.contents: + split_message = Message(role="tool", contents=[content]) + _copy_backend_snapshot_origin(msg, split_message) + if id(content) in backend_result_ids: + _mark_backend_snapshot_origin(split_message) + result.append(split_message) + continue result.append(msg) continue @@ -2153,11 +2168,16 @@ def _convert_approval_results_to_tool_messages( # Tool messages first (right after the preceding assistant message per OpenAI requirements) for func_result in function_results: tool_message = Message(role="tool", contents=[func_result]) + _copy_backend_snapshot_origin(msg, tool_message) + if id(func_result) in backend_result_ids: + _mark_backend_snapshot_origin(tool_message) result.append(tool_message) # Then user message with remaining content (if any) if other_contents: - result.append(Message(role="user", contents=other_contents)) + user_message = Message(role="user", contents=other_contents) + _copy_backend_snapshot_origin(msg, user_message) + result.append(user_message) messages[:] = result @@ -2854,18 +2874,32 @@ def _mark_fides_state_covered_messages( marker(messages) +def _collect_fides_replay_folders(agent: SupportsAgentRun) -> list[Callable[..., None]]: + """Return replay-folding hooks exposed by FIDES context providers.""" + folders: list[Callable[..., None]] = [] + for provider in getattr(agent, "context_providers", ()): + fold = getattr(provider, "_fold_uncovered_replay_messages", None) + if callable(fold): + folders.append(cast(Callable[..., None], fold)) + return folders + + def _fold_fides_uncovered_replay_messages( - agent: SupportsAgentRun, + folders: Sequence[Callable[..., None]], session: AgentSession, messages: Sequence[Message], *, complete_replay_messages: Sequence[Message] | None = None, + replay_history_complete: bool, ) -> None: """Fold uncovered replay into FIDES state before a snapshot-only return.""" - for provider in getattr(agent, "context_providers", ()): - fold = getattr(provider, "_fold_uncovered_replay_messages", None) - if callable(fold): - fold(session, messages, complete_replay_messages=complete_replay_messages) + for fold in folders: + fold( + session, + messages, + complete_replay_messages=complete_replay_messages, + replay_history_complete=replay_history_complete, + ) def _legacy_tool_message_approval_resume( @@ -3446,20 +3480,26 @@ async def save_thread_snapshot( ), ) + fides_replay_folders = _collect_fides_replay_folders(agent) + def fold_fides_replay_before_snapshot_save() -> None: - complete_replay_snapshot = snapshot_messages - if resume_payload is not None and not seeded_resume_from_snapshot and snapshot_seed_messages is None: - complete_replay_snapshot = snapshot_session.resume_seeded_messages(complete_replay_snapshot) - complete_replay_messages, _ = normalize_agui_input_messages( - complete_replay_snapshot, - sanitize_tool_history=not (config.use_service_session and stored_snapshot is not None), - protected_tool_call_ids=protected_tool_call_ids, - ) + if not fides_replay_folders: + return + complete_replay_messages: Sequence[Message] | None = None + if not config.use_service_session: + complete_replay_snapshot = snapshot_messages + if resume_payload is not None and not seeded_resume_from_snapshot and snapshot_seed_messages is None: + complete_replay_snapshot = snapshot_session.resume_seeded_messages(complete_replay_snapshot) + complete_replay_messages, _ = normalize_agui_input_messages( + complete_replay_snapshot, + protected_tool_call_ids=protected_tool_call_ids, + ) _fold_fides_uncovered_replay_messages( - agent, + fides_replay_folders, session, messages, complete_replay_messages=complete_replay_messages, + replay_history_complete=not config.use_service_session, ) _mark_fides_state_covered_messages(agent, messages) diff --git a/python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py b/python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py index 1a306b3c59b..7bc8c4f1f4a 100644 --- a/python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py +++ b/python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py @@ -59,6 +59,11 @@ def _mark_backend_snapshot_origin(message: Message) -> None: message.additional_properties[_BACKEND_SNAPSHOT_ORIGIN_KEY] = _BACKEND_SNAPSHOT_ORIGIN_MARKER +def _clear_backend_snapshot_origin(message: Message) -> None: + """Invalidate backend-snapshot provenance after client-controlled mutation.""" + message.additional_properties.pop(_BACKEND_SNAPSHOT_ORIGIN_KEY, None) + + def _copy_backend_snapshot_origin(source: Message, target: Message) -> None: """Preserve private backend-snapshot provenance on a derived provider message.""" if source.additional_properties.get(_BACKEND_SNAPSHOT_ORIGIN_KEY) is _BACKEND_SNAPSHOT_ORIGIN_MARKER: @@ -789,14 +794,14 @@ def _update_tool_call_arguments( function_payload["arguments"] = modified_args return - def _find_matching_func_call(call_id: str) -> Content | None: + def _find_matching_func_call(call_id: str) -> tuple[Message, Content] | None: for prev_msg in result: role_val = prev_msg.role if hasattr(prev_msg.role, "value") else str(prev_msg.role) if role_val != "assistant": continue for content in prev_msg.contents or []: if content.type == "function_call" and content.call_id == call_id and content.name != "confirm_changes": - return content + return prev_msg, content return None def _parse_arguments(arguments: Any) -> dict[str, Any] | None: @@ -946,9 +951,10 @@ def _append_message(raw_message: dict[str, Any], message: Message) -> None: resolved_call_id = _resolve_approval_call_id(tool_call_id, parsed) if resolved_call_id: approval_call_id = resolved_call_id - matching_func_call = _find_matching_func_call(approval_call_id) + matching_call = _find_matching_func_call(approval_call_id) - if matching_func_call: + if matching_call: + matching_message, matching_func_call = matching_call # Remove any existing tool result for this call_id since the framework # will re-execute the tool after approval. Keeping old results causes # OpenAI API errors ("tool message must follow assistant with tool_calls"). @@ -971,6 +977,7 @@ def _append_message(raw_message: dict[str, Any], message: Message) -> None: filtered_args = _filter_modified_args(modified_args, original_args) state_args: dict[str, Any] | None = None if filtered_args: + _clear_backend_snapshot_origin(matching_message) original_args = original_args or {} merged_args: dict[str, Any] if isinstance(original_args, dict) and original_args: diff --git a/python/packages/ag-ui/tests/ag_ui/test_endpoint.py b/python/packages/ag-ui/tests/ag_ui/test_endpoint.py index b28748510d4..b4ad543b8f9 100644 --- a/python/packages/ag-ui/tests/ag_ui/test_endpoint.py +++ b/python/packages/ag-ui/tests/ag_ui/test_endpoint.py @@ -6034,6 +6034,118 @@ async def stream_fn( assert _is_policy_violation([event["content"] for event in protected_events if event["type"] == "TOOL_CALL_RESULT"]) +async def test_endpoint_fides_service_confirmation_does_not_reconstruct_incomplete_history( + streaming_chat_client_stub: Any, +) -> None: + """A service-owned continuation cannot rebuild missing FIDES state from the AG-UI snapshot alone.""" + thread_id = "thread-fides-service-confirm-missing-state" + model_calls = 0 + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + nonlocal model_calls + del messages, options, kwargs + model_calls += 1 + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("unexpected")]) + + security = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + context_providers=[security], + ) + store = InMemoryAGUIThreadSnapshotStore() + await store.save( + scope="test", + thread_id=thread_id, + snapshot=AGUIThreadSnapshot( + messages=[ + {"id": "user-1", "role": "user", "content": "Look up the remote record"}, + { + "id": "assistant-external-call", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "external-call", + "type": "function", + "function": {"name": "client_lookup", "arguments": "{}"}, + } + ], + }, + { + "id": "external-result", + "role": "tool", + "toolCallId": "external-call", + "content": "snapshot-visible external result", + }, + { + "id": "assistant-confirm-call", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "confirm-call", + "type": "function", + "function": {"name": "confirm_changes", "arguments": "{}"}, + } + ], + }, + ], + session_state={ + security.source_id: { + "scope_id": _session_id_for_thread(scope="test", thread_id=thread_id), + "history_reconstruction_complete": True, + } + }, + ), + ) + wrapped = AgentFrameworkAgent( + agent=agent, + state_schema={"steps": {"type": "array", "items": {"type": "object"}}}, + predict_state_config={"steps": {"tool": "draft_steps", "tool_argument": "steps"}}, + use_service_session=True, + service_session_id_from_thread_id=True, + snapshot_store=store, + ) + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + wrapped, + path="/fides-service-confirm-missing-state", + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + response = client.post( + "/fides-service-confirm-missing-state", + json={ + "runId": "run-fides-service-confirm-missing-state", + "threadId": thread_id, + "messages": [ + { + "id": "confirm-result", + "role": "tool", + "toolCallId": "confirm-call", + "content": json.dumps({"accepted": True, "steps": []}), + } + ], + }, + ) + + assert response.status_code == 200 + errors = [event for event in _decode_sse_events(response) if event.get("type") == "RUN_ERROR"] + assert [event["code"] for event in errors] == ["RuntimeError"] + assert model_calls == 0 + snapshot = await store.get(scope="test", thread_id=thread_id) + assert snapshot is not None + assert snapshot.session_state is not None + assert "context_label" not in snapshot.session_state[security.source_id] + + @pytest.mark.parametrize("middleware_kind", ["direct", "bundle", "wrapper", "client"]) @pytest.mark.parametrize("session_state_key", [None, "custom_security_state"], ids=["default-key", "custom-key"]) @pytest.mark.parametrize("state_origin", ["request", "snapshot"]) @@ -8705,6 +8817,297 @@ async def stream_fn( assert provider_calls == 4 +async def test_endpoint_fides_rejection_does_not_cover_client_result_with_reused_call_id( + streaming_chat_client_stub: Any, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Only the lifecycle-synthesized rejection result receives backend provenance.""" + import agent_framework_ag_ui._agent_run as agent_run_module + + monkeypatch.setattr( + agent_run_module, + "_filter_local_approval_responses_for_provider", + lambda messages, **_kwargs: list(messages), + ) + + phase = "approval" + executed: list[str] = [] + + def guarded_action() -> str: + return "guarded result" + + def protected_action() -> str: + executed.append("protected") + return "protected result" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + del options, kwargs + if phase == "approval": + yield ChatResponseUpdate( + role="assistant", + contents=[ + Content.from_function_call( + call_id="shared-call", + name="guarded_action", + arguments={}, + ) + ], + ) + return + if any( + content.type == "function_result" and content.call_id == "protected-call" + for message in messages + for content in message.contents + ): + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")]) + return + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="protected-call", name="protected_action", arguments={})], + ) + + security = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=guarded_action, + name="guarded_action", + description="Require approval", + approval_mode="always_require", + ), + FunctionTool( + func=protected_action, + name="protected_action", + description="Run a trusted public action", + additional_properties={ + "source_integrity": "trusted", + "max_allowed_confidentiality": "public", + }, + ), + ], + context_providers=[security], + ) + store = InMemoryAGUIThreadSnapshotStore() + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-reused-rejection-result", + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + pause = client.post( + "/fides-reused-rejection-result", + json={ + "runId": "run-reused-rejection-pause", + "threadId": "thread-reused-rejection-result", + "messages": [{"id": "user-1", "role": "user", "content": "Prepare the guarded action"}], + }, + ) + finished = [event for event in _decode_sse_events(pause) if event.get("type") == "RUN_FINISHED"] + approval_id = _run_finished_interrupts(finished[-1])[0]["id"] + snapshot = await store.get(scope="test", thread_id="thread-reused-rejection-result") + assert snapshot is not None + snapshot.messages = [ + message + for message in snapshot.messages + if not (message.get("role") == "assistant" and (message.get("toolCalls") or message.get("tool_calls"))) + ] + snapshot.messages.append( + { + "id": "assistant-earlier-call", + "role": "assistant", + "content": "", + "toolCalls": [ + { + "id": "shared-call", + "type": "function", + "function": {"name": "client_lookup", "arguments": "{}"}, + } + ], + } + ) + await store.save(scope="test", thread_id="thread-reused-rejection-result", snapshot=snapshot) + + phase = "protected" + denied = client.post( + "/fides-reused-rejection-result", + json={ + "runId": "run-reused-rejection-denied", + "threadId": "thread-reused-rejection-result", + "messages": [ + { + "id": "client-owned-result", + "role": "tool", + "toolCallId": "shared-call", + "content": "Error: Tool call invocation was rejected by user.", + } + ], + "resume": [ + { + "interruptId": approval_id, + "status": "resolved", + "payload": {"approved": False}, + } + ], + }, + ) + + assert denied.status_code == 200 + events = _decode_sse_events(denied) + assert not [event for event in events if event.get("type") == "RUN_ERROR"] + assert executed == [] + assert _is_policy_violation([event["content"] for event in events if event["type"] == "TOOL_CALL_RESULT"]) + snapshot = await store.get(scope="test", thread_id="thread-reused-rejection-result") + assert snapshot is not None + assert snapshot.session_state is not None + label = json.loads(snapshot.session_state[security.source_id]["context_label"]) + assert label["integrity"] == "untrusted" + assert label["confidentiality"] == "private" + + +async def test_endpoint_fides_rejected_edited_approval_invalidates_snapshot_provenance( + streaming_chat_client_stub: Any, +) -> None: + """Client edits to a stored call remain conservative when lifecycle validation rejects them.""" + phase = "approval" + executed: list[str] = [] + + def guarded_action(value: str) -> str: + return value + + def protected_action() -> str: + executed.append("protected") + return "protected result" + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + del options, kwargs + if phase == "approval": + yield ChatResponseUpdate( + role="assistant", + contents=[ + Content.from_function_call( + call_id="guarded-call", + name="guarded_action", + arguments={"value": "original"}, + ) + ], + ) + return + if any( + content.type == "function_result" and content.call_id == "protected-call" + for message in messages + for content in message.contents + ): + yield ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")]) + return + yield ChatResponseUpdate( + role="assistant", + contents=[Content.from_function_call(call_id="protected-call", name="protected_action", arguments={})], + ) + + security = SecureAgentConfig() + agent = Agent( + client=streaming_chat_client_stub(stream_fn), + tools=[ + FunctionTool( + func=guarded_action, + name="guarded_action", + description="Require approval", + approval_mode="always_require", + ), + FunctionTool( + func=protected_action, + name="protected_action", + description="Run a trusted public action", + additional_properties={ + "source_integrity": "trusted", + "max_allowed_confidentiality": "public", + }, + ), + ], + context_providers=[security], + ) + store = InMemoryAGUIThreadSnapshotStore() + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/fides-rejected-edited-approval", + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + pause = client.post( + "/fides-rejected-edited-approval", + json={ + "runId": "run-edited-approval-pause", + "threadId": "thread-edited-approval", + "messages": [{"id": "user-1", "role": "user", "content": "Prepare the guarded action"}], + }, + ) + finished = [event for event in _decode_sse_events(pause) if event.get("type") == "RUN_FINISHED"] + interrupt = _run_finished_interrupts(finished[-1])[0] + stored_snapshot = await store.get(scope="test", thread_id="thread-edited-approval") + assert stored_snapshot is not None + + phase = "protected" + response = client.post( + "/fides-rejected-edited-approval", + json={ + "runId": "run-edited-approval-rejected", + "threadId": "thread-edited-approval", + "messages": [ + *stored_snapshot.messages, + { + "id": "edited-approval", + "role": "tool", + "toolCallId": interrupt["toolCallId"], + "content": json.dumps({"accepted": True, "value": "client-edited"}), + }, + { + "id": "user-2", + "role": "user", + "content": "Continue without accepting the edited call.", + }, + ], + "resume": [ + { + "interruptId": interrupt["id"], + "status": "resolved", + "payload": {"approved": False}, + } + ], + }, + ) + + assert response.status_code == 200 + events = _decode_sse_events(response) + assert not [event for event in events if event.get("type") == "RUN_ERROR"] + assert executed == [] + assert _is_policy_violation([event["content"] for event in events if event["type"] == "TOOL_CALL_RESULT"]) + snapshot = await store.get(scope="test", thread_id="thread-edited-approval") + assert snapshot is not None + assert snapshot.session_state is not None + label = json.loads(snapshot.session_state[security.source_id]["context_label"]) + assert label["integrity"] == "untrusted" + assert label["confidentiality"] == "private" + + async def test_endpoint_agent_approval_cancel_discards_queued_tool_approval(streaming_chat_client_stub): """Cancelling a queued approval batch must not replay stale approval prompts on the next user turn.""" client, executed, messages_received, state, _ = _build_tool_approval_queue_endpoint(streaming_chat_client_stub) @@ -13119,6 +13522,88 @@ async def stream_fn(messages: Any, options: Any, **kwargs: Any): assert any(message.get("role") == "user" and message.get("content") == "Draft the plan" for message in messages) +async def test_agent_endpoint_confirmation_without_fides_does_not_rebuild_snapshot( + streaming_chat_client_stub: Any, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Confirmation avoids a second full transcript conversion when no FIDES replay folder exists.""" + import agent_framework_ag_ui._agent_run as agent_run_module + + normalization_calls = 0 + original_normalize = agent_run_module.normalize_agui_input_messages + + def track_normalization(*args: Any, **kwargs: Any) -> Any: + nonlocal normalization_calls + normalization_calls += 1 + return original_normalize(*args, **kwargs) + + monkeypatch.setattr(agent_run_module, "normalize_agui_input_messages", track_normalization) + + async def stream_fn( + messages: list[Message], + options: dict[str, Any], + **kwargs: Any, + ) -> AsyncIterator[ChatResponseUpdate]: + del messages, options, kwargs + yield ChatResponseUpdate( + role="assistant", + contents=[ + Content.from_function_call( + name="draft_steps", + call_id="draft-call", + arguments={"steps": [{"description": "Draft outline"}]}, + ) + ], + ) + + agent = Agent(name="test", instructions="Test agent", client=streaming_chat_client_stub(stream_fn)) + store = InMemoryAGUIThreadSnapshotStore() + app = FastAPI() + add_agent_framework_fastapi_endpoint( + app, + agent, + path="/confirm-without-fides", + state_schema={"steps": {"type": "array", "items": {"type": "object"}}}, + predict_state_config={"steps": {"tool": "draft_steps", "tool_argument": "steps"}}, + snapshot_store=store, + snapshot_scope_resolver=lambda _request: "test", + keepalive_seconds=None, + ) + + with TestClient(app) as client: + pause = client.post( + "/confirm-without-fides", + json={ + "runId": "run-confirm-without-fides-pause", + "threadId": "thread-confirm-without-fides", + "messages": [{"id": "user-1", "role": "user", "content": "Draft the plan"}], + }, + ) + finished = [event for event in _decode_sse_events(pause) if event.get("type") == "RUN_FINISHED"] + confirm_id = _run_finished_interrupts(finished[-1])[0]["id"] + normalization_calls = 0 + + confirmed = client.post( + "/confirm-without-fides", + json={ + "runId": "run-confirm-without-fides-finish", + "threadId": "thread-confirm-without-fides", + "messages": [], + "resume": [ + { + "interruptId": confirm_id, + "status": "resolved", + "payload": json.dumps({"accepted": True, "steps": []}), + } + ], + }, + ) + + assert confirmed.status_code == 200 + assert not [event for event in _decode_sse_events(confirmed) if event.get("type") == "RUN_ERROR"] + assert normalization_calls == 1 + + async def test_agent_endpoint_default_state_does_not_reset_persisted_state(streaming_chat_client_stub): """Endpoint defaults fill missing keys but never override persisted Shared State.""" app = FastAPI() diff --git a/python/packages/ag-ui/tests/ag_ui/test_message_hygiene.py b/python/packages/ag-ui/tests/ag_ui/test_message_hygiene.py index fba1b91fb09..d7cdd9b74f6 100644 --- a/python/packages/ag-ui/tests/ag_ui/test_message_hygiene.py +++ b/python/packages/ag-ui/tests/ag_ui/test_message_hygiene.py @@ -145,6 +145,36 @@ def test_convert_approval_results_preserves_other_user_content() -> None: assert messages[2].contents[0].type == "text" +def test_convert_approval_results_marks_only_exact_backend_rejection() -> None: + """Reusable call IDs and rejection text cannot authenticate a client-owned result.""" + from agent_framework_ag_ui._agent_run import _convert_approval_results_to_tool_messages + from agent_framework_ag_ui._message_adapters import _take_backend_snapshot_messages + + client_result = Content.from_function_result( + call_id="shared-call", + result="Error: Tool call invocation was rejected by user.", + ) + synthesized_rejection = Content.from_function_result( + call_id="shared-call", + result="Error: Tool call invocation was rejected by user.", + ) + messages = [ + Message(role="tool", contents=[client_result]), + Message(role="user", contents=[synthesized_rejection]), + ] + + _convert_approval_results_to_tool_messages( + messages, + backend_result_content_ids={id(synthesized_rejection)}, + ) + + covered_messages = _take_backend_snapshot_messages(messages) + assert len(covered_messages) == 1 + assert len(covered_messages[0].contents) == 1 + assert covered_messages[0].contents[0] is synthesized_rejection + assert all(content is not client_result for message in covered_messages for content in message.contents) + + def test_sanitize_tool_history_filters_confirm_changes_keeps_other_tools() -> None: """Test that confirm_changes is filtered but other tools are preserved. diff --git a/python/packages/core/agent_framework/security.py b/python/packages/core/agent_framework/security.py index 6d51b8a92ea..efd33e0957b 100644 --- a/python/packages/core/agent_framework/security.py +++ b/python/packages/core/agent_framework/security.py @@ -4444,12 +4444,20 @@ def _fold_uncovered_replay_messages( messages: Sequence[Message], *, complete_replay_messages: Sequence[Message] | None = None, + replay_history_complete: bool = True, ) -> None: """Fold new transport messages before a snapshot-only return persists them.""" state = session.state.setdefault(self.source_id, {}) if not isinstance(state, dict): raise ValueError("Security provider state must be a dictionary.") restored_context_label = _STATE_CONTEXT_LABEL in state + if not restored_context_label and not replay_history_complete: + _clear_replay_state_covered_markers(messages) + if complete_replay_messages is not None and complete_replay_messages is not messages: + _clear_replay_state_covered_markers(complete_replay_messages) + raise RuntimeError( + "Cannot reconstruct service-managed history without an authoritative FIDES context label." + ) scope = _SecurityScope(cast(dict[str, Any], state), scope_id=session.session_id) replay_messages = ( messages if restored_context_label or complete_replay_messages is None else complete_replay_messages diff --git a/python/packages/core/tests/test_security.py b/python/packages/core/tests/test_security.py index 1f07ad0d168..90fec386a10 100644 --- a/python/packages/core/tests/test_security.py +++ b/python/packages/core/tests/test_security.py @@ -3713,6 +3713,28 @@ async def test_transport_marked_replay_uses_restored_state_and_clears_marker(sel assert label.confidentiality == ConfidentialityLabel.PUBLIC assert "_agent_framework_fides_replay_state_covered" not in restored_message.additional_properties + def test_transport_fold_rejects_missing_label_for_incomplete_service_history(self) -> None: + """Snapshot replay cannot stand in for unavailable service-owned history.""" + config = SecureAgentConfig() + session = AgentSession(session_id="incomplete-service-history", service_session_id="remote-conversation") + replayed = Message( + role="tool", + contents=[Content.from_function_result(call_id="external-call", result="snapshot-visible result")], + ) + + with pytest.raises( + RuntimeError, + match="Cannot reconstruct service-managed history without an authoritative FIDES context label", + ): + config._fold_uncovered_replay_messages( + session, + [replayed], + complete_replay_messages=[replayed], + replay_history_complete=False, + ) + + assert "context_label" not in session.state[config.source_id] + async def test_modified_hidden_placeholder_is_not_authenticated_by_client_flag(self) -> None: """A real variable ID does not authenticate a client-modified placeholder.""" config = SecureAgentConfig() From c0cc54dd603f9aa720074603ef279ec89c8f51b0 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 12 Nov 2026 12:23:32 +0100 Subject: [PATCH 3/6] Python: align FIDES AG-UI replay regression --- .../packages/ag-ui/tests/ag_ui/test_endpoint.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/python/packages/ag-ui/tests/ag_ui/test_endpoint.py b/python/packages/ag-ui/tests/ag_ui/test_endpoint.py index b4ad543b8f9..33bca9292f2 100644 --- a/python/packages/ag-ui/tests/ag_ui/test_endpoint.py +++ b/python/packages/ag-ui/tests/ag_ui/test_endpoint.py @@ -8129,10 +8129,10 @@ async def test_endpoint_fides_replacement_rotates_lifecycle_generation( assert executed == ["guarded"] -async def test_endpoint_fides_approval_without_replay_provenance_fails_closed( +async def test_endpoint_fides_approval_uses_backend_snapshot_provenance( streaming_chat_client_stub: Any, ) -> None: - """AG-UI snapshot replay requires explicit provenance before FIDES can reuse one approval.""" + """AG-UI snapshot provenance lets FIDES authenticate a restored approval turn.""" client, snapshot_store, security, executed, wrapped_agent = _build_fides_policy_approval_endpoint( streaming_chat_client_stub ) @@ -8160,16 +8160,15 @@ async def test_endpoint_fides_approval_without_replay_provenance_fails_closed( assert approved.status_code == 200 results = [event for event in _decode_sse_events(approved) if event.get("type") == "TOOL_CALL_RESULT"] - assert results == [] - assert executed == [] + assert [(event["toolCallId"], event["content"]) for event in results] == [ + ("call-guarded", "guarded result") + ] + assert executed == ["guarded"] finished = [event for event in _decode_sse_events(approved) if event.get("type") == "RUN_FINISHED"] - replacement_interrupts = _run_finished_interrupts(finished[-1]) - assert len(replacement_interrupts) == 1 - assert replacement_interrupts[0]["id"] != approval_id - assert replacement_interrupts[0]["toolCallId"] == "call-guarded" + assert "outcome" not in finished[-1] snapshot = await snapshot_store.get(scope="tenant-a", thread_id=thread_id) assert snapshot is not None and snapshot.session_state is not None - assert snapshot.session_state[security.source_id]["pending_policy_approvals"] + assert not snapshot.session_state[security.source_id]["pending_policy_approvals"] async def test_endpoint_agent_approval_resume_persists_replayable_tool_results(streaming_chat_client_stub): From cc166c2eacbb56ff52465756c2b12469f63cfff6 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 12 Nov 2026 12:50:52 +0100 Subject: [PATCH 4/6] Python: use Sentinel for replay marker --- python/packages/core/agent_framework/security.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/packages/core/agent_framework/security.py b/python/packages/core/agent_framework/security.py index efd33e0957b..2b022726160 100644 --- a/python/packages/core/agent_framework/security.py +++ b/python/packages/core/agent_framework/security.py @@ -35,6 +35,7 @@ from weakref import WeakKeyDictionary from pydantic import BaseModel, Field +from typing_extensions import Sentinel from ._feature_stage import ExperimentalFeature, experimental from ._middleware import ( @@ -142,7 +143,7 @@ _MAX_REPLAY_CONTENT_DEPTH = 32 _builtin_id = builtins.id _REPLAY_STATE_COVERED_KEY = "_agent_framework_fides_replay_state_covered" -_REPLAY_STATE_COVERED_MARKER = object() +_REPLAY_STATE_COVERED_MARKER = Sentinel("_REPLAY_STATE_COVERED_MARKER") _TOOL_SECURITY_POLICY_KEYS = frozenset({ _TOOL_POLICY_ACCEPTS_UNTRUSTED_KEY, _TOOL_POLICY_CONFIDENTIALITY_KEY, From adfd2e08e238fbee99b2ae198104370e8ad2c412 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 12 Nov 2026 13:16:45 +0100 Subject: [PATCH 5/6] Python: scope FIDES replay provenance --- .../packages/core/agent_framework/security.py | 68 ++++++++++++--- python/packages/core/pyproject.toml | 2 +- python/packages/core/tests/test_security.py | 85 +++++++++++++++++++ python/uv.lock | 2 +- 4 files changed, 141 insertions(+), 16 deletions(-) diff --git a/python/packages/core/agent_framework/security.py b/python/packages/core/agent_framework/security.py index 2b022726160..dc710f32531 100644 --- a/python/packages/core/agent_framework/security.py +++ b/python/packages/core/agent_framework/security.py @@ -1390,6 +1390,7 @@ def _bound_approval_call_keys( *, scope: _SecurityScope, approval_proofs: _ApprovalReplayProofs | None, + replay_state_source_id: str | None = None, ) -> set[Any]: """Return authoritative call keys bound by approval controls across one replay batch.""" return { @@ -1402,7 +1403,10 @@ def _bound_approval_call_keys( content, scope=scope, approval_proofs=approval_proofs, - transport_authoritative=_is_replay_state_covered(message), + transport_authoritative=( + replay_state_source_id is not None + and _is_replay_state_covered(message, source_id=replay_state_source_id) + ), ) ) is not None @@ -1437,6 +1441,7 @@ def _replayed_message_label( scope: _SecurityScope, approval_proofs: _ApprovalReplayProofs | None = None, bound_approval_call_keys: set[Any] | None = None, + replay_state_source_id: str | None = None, ) -> ContentLabel: """Return a restriction-only label for caller-replayed or stored history.""" role = str(message.role) @@ -1450,6 +1455,7 @@ def _replayed_message_label( [message], scope=scope, approval_proofs=approval_proofs, + replay_state_source_id=replay_state_source_id, ) for index, content in enumerate(message.contents): if not isinstance(content, Content): @@ -1466,7 +1472,10 @@ def _replayed_message_label( content, scope=scope, approval_proofs=approval_proofs, - transport_authoritative=_is_replay_state_covered(message), + transport_authoritative=( + replay_state_source_id is not None + and _is_replay_state_covered(message, source_id=replay_state_source_id) + ), ) is_bound_approval_control = authoritative_approval_call is not None has_bound_approval_control = has_bound_approval_control or is_bound_approval_control @@ -1526,15 +1535,32 @@ def _replayed_message_label( return combine_labels(*labels) -def _is_replay_state_covered(message: Message) -> bool: - """Return whether trusted transport code marked this replay as represented by restored state.""" - return message.additional_properties.get(_REPLAY_STATE_COVERED_KEY) is _REPLAY_STATE_COVERED_MARKER +def _is_replay_state_covered(message: Message, *, source_id: str) -> bool: + """Return whether restored state for one provider covers this replay.""" + coverage = message.additional_properties.get(_REPLAY_STATE_COVERED_KEY) + if not isinstance(coverage, Mapping): + return False + return cast(Mapping[str, Any], coverage).get(source_id) is _REPLAY_STATE_COVERED_MARKER -def _clear_replay_state_covered_markers(messages: Sequence[Message]) -> None: - """Remove private replay provenance before messages reach a chat provider.""" +def _clear_replay_state_covered_markers( + messages: Sequence[Message], + *, + source_id: str | None = None, +) -> None: + """Remove one provider's private replay provenance, or all provenance after a failure.""" for message in messages: - if _is_replay_state_covered(message): + coverage = message.additional_properties.get(_REPLAY_STATE_COVERED_KEY) + if source_id is None: + message.additional_properties.pop(_REPLAY_STATE_COVERED_KEY, None) + continue + if not isinstance(coverage, MutableMapping): + message.additional_properties.pop(_REPLAY_STATE_COVERED_KEY, None) + continue + mutable_coverage = cast(MutableMapping[str, Any], coverage) + if mutable_coverage.get(source_id) is _REPLAY_STATE_COVERED_MARKER: + mutable_coverage.pop(source_id, None) + if not any(marker is _REPLAY_STATE_COVERED_MARKER for marker in mutable_coverage.values()): message.additional_properties.pop(_REPLAY_STATE_COVERED_KEY, None) @@ -4437,7 +4463,11 @@ def _function_middleware_for_approval_resolution(self, session: AgentSession) -> def _mark_replay_state_covered_messages(self, messages: Sequence[Message]) -> None: """Mark trusted transport-restored messages as already represented by session state.""" for message in messages: - message.additional_properties[_REPLAY_STATE_COVERED_KEY] = _REPLAY_STATE_COVERED_MARKER + coverage = message.additional_properties.get(_REPLAY_STATE_COVERED_KEY) + if not isinstance(coverage, MutableMapping): + coverage = {} + message.additional_properties[_REPLAY_STATE_COVERED_KEY] = coverage + cast(MutableMapping[str, Any], coverage)[self.source_id] = _REPLAY_STATE_COVERED_MARKER def _fold_uncovered_replay_messages( self, @@ -4471,6 +4501,7 @@ def _fold_uncovered_replay_messages( replay_messages, scope=scope, approval_proofs=approval_proofs, + replay_state_source_id=self.source_id, ) replay_labels = [ _replayed_message_label( @@ -4479,15 +4510,16 @@ def _fold_uncovered_replay_messages( scope=scope, approval_proofs=approval_proofs, bound_approval_call_keys=bound_call_keys, + replay_state_source_id=self.source_id, ) for message in replay_messages - if not restored_context_label or not _is_replay_state_covered(message) + if not restored_context_label or not _is_replay_state_covered(message, source_id=self.source_id) ] if replay_labels: scope.context_label = combine_labels(scope.context_label, *replay_labels) - _clear_replay_state_covered_markers(messages) + _clear_replay_state_covered_markers(messages, source_id=self.source_id) if replay_messages is not messages: - _clear_replay_state_covered_markers(replay_messages) + _clear_replay_state_covered_markers(replay_messages, source_id=self.source_id) @staticmethod def _capture_core_approval_replay_proofs( @@ -4599,6 +4631,7 @@ def _fold_provider_message_map( session: AgentSession, source_ids: set[str], approval_proofs: _ApprovalReplayProofs, + replay_state_source_id: str, message_labeler: Callable[[str, Message], ContentLabel | None] | None = None, ) -> None: """Fold provider messages into one FIDES scope.""" @@ -4616,6 +4649,7 @@ def _fold_provider_message_map( messages, scope=scope, approval_proofs=approval_proofs, + replay_state_source_id=replay_state_source_id, ) for message in messages: authoritative_label = message_labeler(source_id, message) if message_labeler is not None else None @@ -4628,6 +4662,7 @@ def _fold_provider_message_map( scope=scope, approval_proofs=approval_proofs, bound_approval_call_keys=bound_call_keys, + replay_state_source_id=replay_state_source_id, ) ) if provider_labels: @@ -4776,6 +4811,7 @@ def fold_run_context(*, validate_continuation: bool) -> None: session=session, source_ids=source_ids, approval_proofs=approval_proofs, + replay_state_source_id=self.source_id, message_labeler=classify_provider_message, ) @@ -4784,14 +4820,17 @@ def fold_run_context(*, validate_continuation: bool) -> None: context.input_messages, scope=scope, approval_proofs=approval_proofs, + replay_state_source_id=self.source_id, ) input_labels = [ _replayed_message_label( message, - reconstruct_unstamped=not restored_context_label or not _is_replay_state_covered(message), + reconstruct_unstamped=not restored_context_label + or not _is_replay_state_covered(message, source_id=self.source_id), scope=scope, approval_proofs=approval_proofs, bound_approval_call_keys=input_bound_call_keys, + replay_state_source_id=self.source_id, ) for message in context.input_messages ] @@ -4800,7 +4839,7 @@ def fold_run_context(*, validate_continuation: bool) -> None: scope.context_label, *input_labels, ) - _clear_replay_state_covered_markers(context.input_messages) + _clear_replay_state_covered_markers(context.input_messages, source_id=self.source_id) middleware: list[MiddlewareTypes] = [*self._middleware_for_scope(scope)] @function_middleware @@ -4831,6 +4870,7 @@ async def fold_replayed_history( session=session, source_ids=history_source_ids, approval_proofs=approval_proofs, + replay_state_source_id=self.source_id, message_labeler=cached_provider_label, ) await call_next() diff --git a/python/packages/core/pyproject.toml b/python/packages/core/pyproject.toml index 187cf3de1d6..e10e4ac28da 100644 --- a/python/packages/core/pyproject.toml +++ b/python/packages/core/pyproject.toml @@ -24,7 +24,7 @@ classifiers = [ ] dependencies = [ "msgspec>=0.20.0,<0.23", - "typing-extensions>=4.15.0,<5", + "typing-extensions>=4.16.0,<5", "pydantic>=2,<3", "python-dotenv>=1,<2", "opentelemetry-api>=1.39.0,<2", diff --git a/python/packages/core/tests/test_security.py b/python/packages/core/tests/test_security.py index 90fec386a10..8961bf41f28 100644 --- a/python/packages/core/tests/test_security.py +++ b/python/packages/core/tests/test_security.py @@ -9,6 +9,7 @@ import math import warnings from collections.abc import Awaitable, Callable, Sequence +from copy import copy, deepcopy from datetime import datetime, timedelta, timezone from pathlib import Path from types import MappingProxyType, SimpleNamespace @@ -55,6 +56,8 @@ ) from agent_framework._types import Content, normalize_messages from agent_framework.security import ( + _REPLAY_STATE_COVERED_KEY, + _REPLAY_STATE_COVERED_MARKER, _REWRITTEN_ARGUMENT_INDICES_KEY, _SECURITY_ARGUMENTS_SNAPSHOT_CONTEXT_KEY, ConfidentialityLabel, @@ -3713,6 +3716,88 @@ async def test_transport_marked_replay_uses_restored_state_and_clears_marker(sel assert label.confidentiality == ConfidentialityLabel.PUBLIC assert "_agent_framework_fides_replay_state_covered" not in restored_message.additional_properties + def test_transport_replay_marker_survives_message_copy(self) -> None: + """Supported shallow and deep copies preserve replay sentinel identity.""" + config = SecureAgentConfig() + restored_message = Message(role="assistant", contents=["Previously covered history"]) + config._mark_replay_state_covered_messages([restored_message]) + + for copied_message in (copy(restored_message), deepcopy(restored_message)): + coverage = copied_message.additional_properties.get(_REPLAY_STATE_COVERED_KEY) + assert isinstance(coverage, dict) + assert coverage.get(config.source_id) is _REPLAY_STATE_COVERED_MARKER + + async def test_transport_marked_replay_is_consumed_per_security_provider(self) -> None: + """Each security provider consumes only its own replay coverage.""" + configs = ( + SecureAgentConfig(source_id="security-a"), + SecureAgentConfig(source_id="security-b"), + ) + session = AgentSession(session_id="provider-scoped-transport-coverage") + for config in configs: + tracker, _ = await _get_session_security_middleware(config, session) + tracker._update_context_label(ContentLabel()) + + restored_message = Message(role="assistant", contents=["Previously covered history"]) + for config in configs: + config._mark_replay_state_covered_messages([restored_message]) + provider_context = SessionContext(session_id=session.session_id, input_messages=[restored_message]) + agent = SimpleNamespace(context_providers=list(configs)) + + for index, config in enumerate(configs): + await config.before_run( + agent=agent, + session=session, + context=provider_context, + state=session.state.setdefault(config.source_id, {}), + ) + coverage = restored_message.additional_properties.get(_REPLAY_STATE_COVERED_KEY) + if index == 0: + assert isinstance(coverage, dict) + assert set(coverage) == {configs[1].source_id} + assert coverage[configs[1].source_id] is _REPLAY_STATE_COVERED_MARKER + else: + assert coverage is None + + scoped_trackers = [ + middleware + for middleware in provider_context.get_middleware() + if isinstance(middleware, LabelTrackingFunctionMiddleware) + ] + assert len(scoped_trackers) == 2 + for tracker in scoped_trackers: + label = tracker.get_context_label() + assert label.integrity == IntegrityLabel.TRUSTED + assert label.confidentiality == ConfidentialityLabel.PUBLIC + + async def test_transport_fold_consumes_replay_coverage_per_security_provider(self) -> None: + """Snapshot folding preserves coverage until every security provider consumes it.""" + configs = ( + SecureAgentConfig(source_id="security-a"), + SecureAgentConfig(source_id="security-b"), + ) + session = AgentSession(session_id="provider-scoped-snapshot-coverage") + for config in configs: + tracker, _ = await _get_session_security_middleware(config, session) + tracker._update_context_label(ContentLabel()) + + restored_message = Message(role="assistant", contents=["Previously covered history"]) + for config in configs: + config._mark_replay_state_covered_messages([restored_message]) + + for index, config in enumerate(configs): + config._fold_uncovered_replay_messages(session, [restored_message]) + label = ContentLabel.from_dict(json.loads(session.state[config.source_id]["context_label"])) + assert label.integrity == IntegrityLabel.TRUSTED + assert label.confidentiality == ConfidentialityLabel.PUBLIC + coverage = restored_message.additional_properties.get(_REPLAY_STATE_COVERED_KEY) + if index == 0: + assert isinstance(coverage, dict) + assert set(coverage) == {configs[1].source_id} + assert coverage[configs[1].source_id] is _REPLAY_STATE_COVERED_MARKER + else: + assert coverage is None + def test_transport_fold_rejects_missing_label_for_incomplete_service_history(self) -> None: """Snapshot replay cannot stand in for unavailable service-owned history.""" config = SecureAgentConfig() diff --git a/python/uv.lock b/python/uv.lock index eb88563d099..86bdd831890 100644 --- a/python/uv.lock +++ b/python/uv.lock @@ -551,7 +551,7 @@ requires-dist = [ { name = "pyyaml", specifier = ">=6.0,<7.0" }, { name = "regex", specifier = ">=2024.11.6" }, { name = "tenacity", marker = "extra == 'all'", specifier = ">=9.1.4,<9.2.0" }, - { name = "typing-extensions", specifier = ">=4.15.0,<5" }, + { name = "typing-extensions", specifier = ">=4.16.0,<5" }, ] provides-extras = ["all"] From 5eb09071e33d32e669b60d393bceaec2ce81db4e Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 12 Nov 2026 13:53:07 +0100 Subject: [PATCH 6/6] Python: make rejection marker private --- .../ag-ui/agent_framework_ag_ui/_agent_run.py | 7 +++++-- python/packages/core/agent_framework/_compaction.py | 12 +++++++++--- python/packages/core/agent_framework/_tools.py | 4 ++-- python/packages/core/agent_framework/_types.py | 2 +- python/packages/core/tests/core/test_compaction.py | 8 ++++---- .../tests/core/test_function_invocation_logic.py | 4 ++-- 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py b/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py index 4916b735adb..8e582e3a4ed 100644 --- a/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py +++ b/python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py @@ -61,7 +61,10 @@ _try_execute_function_call_groups, # type: ignore normalize_function_invocation_configuration, ) -from agent_framework._types import APPROVAL_REJECTION_RESULT_KEY, ResponseStream +from agent_framework._types import ( # pyright: ignore[reportPrivateUsage] + _APPROVAL_REJECTION_RESULT_KEY, + ResponseStream, +) from agent_framework.exceptions import AgentInvalidResponseException from agent_framework.observability import ( _use_telemetry_conversation_id, # pyright: ignore[reportPrivateUsage] @@ -2101,7 +2104,7 @@ async def execute_local_call(approval: Content = approval, call_id: str = call_i for content in resolved_contents if content.type == "function_result" and content.id in authenticated_rejection_occurrence_ids - and content.additional_properties.get(APPROVAL_REJECTION_RESULT_KEY) is True + and content.additional_properties.get(_APPROVAL_REJECTION_RESULT_KEY) is True } # Post-process: Convert user messages with function_result content to proper tool messages. diff --git a/python/packages/core/agent_framework/_compaction.py b/python/packages/core/agent_framework/_compaction.py index 06cfe99954a..70e6e82ea81 100644 --- a/python/packages/core/agent_framework/_compaction.py +++ b/python/packages/core/agent_framework/_compaction.py @@ -20,7 +20,13 @@ from ._feature_stage import ExperimentalFeature, experimental from ._sessions import ContextProvider from ._telemetry import FeatureIndex, mark_feature_used -from ._types import APPROVAL_REJECTION_RESULT_KEY, SKILL_CONTEXT_NAME_KEY, ChatResponse, Content, Message +from ._types import ( + _APPROVAL_REJECTION_RESULT_KEY, # pyright: ignore[reportPrivateUsage] + SKILL_CONTEXT_NAME_KEY, + ChatResponse, + Content, + Message, +) if TYPE_CHECKING: from ._clients import SupportsChatGetResponse @@ -1307,7 +1313,7 @@ def _completed_function_call_pairs( if ( call.name == tool_name and result.exception is None - and result.additional_properties.get(APPROVAL_REJECTION_RESULT_KEY) is not True + and result.additional_properties.get(_APPROVAL_REJECTION_RESULT_KEY) is not True and (include_consumed or call.additional_properties.get(_AGENT_COMPACTION_TRIGGER_CONSUMED_KEY) is not True) ) ] @@ -1728,7 +1734,7 @@ def _skill_names_for_removable_group(group: Sequence[Message]) -> set[str] | Non if ( result is None or result.exception is not None - or result.additional_properties.get(APPROVAL_REJECTION_RESULT_KEY) is True + or result.additional_properties.get(_APPROVAL_REJECTION_RESULT_KEY) is True ): return None skill_name = result.additional_properties.get(SKILL_CONTEXT_NAME_KEY) diff --git a/python/packages/core/agent_framework/_tools.py b/python/packages/core/agent_framework/_tools.py index 9a1f142cfb7..79475e658b1 100644 --- a/python/packages/core/agent_framework/_tools.py +++ b/python/packages/core/agent_framework/_tools.py @@ -4552,7 +4552,7 @@ def _replace_approval_contents_with_results( Returns: The terminal contents produced while resolving the approval responses, in response order. """ - from ._types import APPROVAL_REJECTION_RESULT_KEY, Content + from ._types import _APPROVAL_REJECTION_RESULT_KEY, Content # pyright: ignore[reportPrivateUsage] result_groups_by_call_id: dict[str, deque[list[Content]]] = {} for result_group in approved_function_result_groups: @@ -4681,7 +4681,7 @@ def find_approval_occurrence(approval_id: str) -> _ApprovalCallOccurrence | None result="Error: Tool call invocation was rejected by user.", additional_properties={ **content.function_call.additional_properties, - APPROVAL_REJECTION_RESULT_KEY: True, + _APPROVAL_REJECTION_RESULT_KEY: True, }, ) ] diff --git a/python/packages/core/agent_framework/_types.py b/python/packages/core/agent_framework/_types.py index fcfe33f9316..a41195f6df8 100644 --- a/python/packages/core/agent_framework/_types.py +++ b/python/packages/core/agent_framework/_types.py @@ -490,7 +490,7 @@ def _restore_compaction_annotation_in_additional_properties( "code_interpreter_tool_call", "code_interpreter_tool_result", }) -APPROVAL_REJECTION_RESULT_KEY = "_approval_rejection_result" +_APPROVAL_REJECTION_RESULT_KEY = "_approval_rejection_result" SKILL_CONTEXT_NAME_KEY = "_skill_context_name" _CONTENT_TYPES: Final[frozenset[str]] = frozenset(get_args(ContentType)) diff --git a/python/packages/core/tests/core/test_compaction.py b/python/packages/core/tests/core/test_compaction.py index f38bf07436c..665a473991b 100644 --- a/python/packages/core/tests/core/test_compaction.py +++ b/python/packages/core/tests/core/test_compaction.py @@ -50,7 +50,7 @@ extend_compaction_messages, ) from agent_framework._feature_stage import _WARNED_FEATURES, ExperimentalFeature, ExperimentalWarning -from agent_framework._types import APPROVAL_REJECTION_RESULT_KEY, SKILL_CONTEXT_NAME_KEY +from agent_framework._types import _APPROVAL_REJECTION_RESULT_KEY, SKILL_CONTEXT_NAME_KEY def _assistant_function_call(call_id: str) -> Message: @@ -1299,7 +1299,7 @@ async def test_agent_triggered_compaction_ignores_rejected_approval_result() -> rejection_result = Content.from_function_result( call_id="compact-1", result="Error: Tool call invocation was rejected by user.", - additional_properties={APPROVAL_REJECTION_RESULT_KEY: True}, + additional_properties={_APPROVAL_REJECTION_RESULT_KEY: True}, ) messages = [ _assistant_named_function_call("compact-1", strategy.tool.name), @@ -1338,7 +1338,7 @@ def test_completed_function_calls_correlates_reused_call_id_by_occurrence() -> N id="occ-rejected", call_id="shared", result="Error: Tool call invocation was rejected by user.", - additional_properties={APPROVAL_REJECTION_RESULT_KEY: True}, + additional_properties={_APPROVAL_REJECTION_RESULT_KEY: True}, ), Content.from_function_result( id="occ-approved", @@ -1654,7 +1654,7 @@ async def test_unload_skills_compaction_preserves_reused_id_group_with_unresolve ({SKILL_CONTEXT_NAME_KEY: ""}, None), ({SKILL_CONTEXT_NAME_KEY: 123}, None), ({SKILL_CONTEXT_NAME_KEY: "pdf"}, "Load failed."), - ({SKILL_CONTEXT_NAME_KEY: "pdf", APPROVAL_REJECTION_RESULT_KEY: True}, None), + ({SKILL_CONTEXT_NAME_KEY: "pdf", _APPROVAL_REJECTION_RESULT_KEY: True}, None), ], ) async def test_unload_skills_preserves_groups_without_successful_provenance( diff --git a/python/packages/core/tests/core/test_function_invocation_logic.py b/python/packages/core/tests/core/test_function_invocation_logic.py index 54fafe4c72b..a3019a20590 100644 --- a/python/packages/core/tests/core/test_function_invocation_logic.py +++ b/python/packages/core/tests/core/test_function_invocation_logic.py @@ -7958,7 +7958,7 @@ def test_replace_approval_contents_with_results_correlates_reused_call_id_pendin def test_replace_approval_contents_with_results_resolves_rejected_request() -> None: """A rejected pending call produces one terminal result.""" from agent_framework._tools import _collect_approval_responses, _replace_approval_contents_with_results - from agent_framework._types import APPROVAL_REJECTION_RESULT_KEY + from agent_framework._types import _APPROVAL_REJECTION_RESULT_KEY function_call, request, _ = _build_approved_tool_roundtrip( call_id="call_rejected", @@ -7980,7 +7980,7 @@ def test_replace_approval_contents_with_results_resolves_rejected_request() -> N results = [content for message in messages for content in message.contents if content.type == "function_result"] assert len(results) == 1 assert results[0].result == "Error: Tool call invocation was rejected by user." - assert results[0].additional_properties[APPROVAL_REJECTION_RESULT_KEY] is True + assert results[0].additional_properties[_APPROVAL_REJECTION_RESULT_KEY] is True assert resolved_contents == results