|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import importlib |
| 4 | +import json |
4 | 5 | import logging |
5 | 6 | from pathlib import Path |
6 | 7 | from types import ModuleType |
| 8 | +from unittest.mock import AsyncMock |
7 | 9 |
|
8 | 10 | import pytest |
| 11 | +from pydantic import ValidationError |
9 | 12 |
|
10 | | -from agents.realtime.events import RealtimeEventInfo, RealtimeRawModelEvent |
| 13 | +from agents import _debug |
| 14 | +from agents.realtime import RealtimeAgent, RealtimeSession |
| 15 | +from agents.realtime.events import RealtimeError, RealtimeEventInfo, RealtimeRawModelEvent |
11 | 16 | from agents.realtime.items import InputText, UserMessageItem |
12 | 17 | from agents.realtime.model_events import RealtimeModelItemUpdatedEvent |
| 18 | +from agents.realtime.openai_realtime import OpenAIRealtimeWebSocketModel |
13 | 19 | from agents.run_context import RunContextWrapper |
14 | 20 |
|
15 | 21 |
|
@@ -42,3 +48,100 @@ def test_item_updated_debug_summary_uses_concrete_event_type( |
42 | 48 | assert "item-1" in caplog.text |
43 | 49 | assert "input_text" in caplog.text |
44 | 50 | assert "sensitive transcript" not in caplog.text |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.asyncio |
| 54 | +@pytest.mark.parametrize("dont_log_model_data", [True, False]) |
| 55 | +@pytest.mark.parametrize("malformed", [True, False]) |
| 56 | +async def test_forwarded_provider_errors_have_payload_free_debug_summaries( |
| 57 | + app_server: ModuleType, |
| 58 | + monkeypatch: pytest.MonkeyPatch, |
| 59 | + caplog: pytest.LogCaptureFixture, |
| 60 | + dont_log_model_data: bool, |
| 61 | + malformed: bool, |
| 62 | +) -> None: |
| 63 | + marker = "synthetic-private-transcript" |
| 64 | + monkeypatch.setattr(_debug, "DONT_LOG_MODEL_DATA", dont_log_model_data) |
| 65 | + model = OpenAIRealtimeWebSocketModel() |
| 66 | + session = RealtimeSession(model=model, agent=RealtimeAgent(name="test"), context=None) |
| 67 | + model.add_listener(session) |
| 68 | + listener = AsyncMock() |
| 69 | + model.add_listener(listener) |
| 70 | + websocket = AsyncMock() |
| 71 | + manager = app_server.RealtimeWebSocketManager() |
| 72 | + manager.active_sessions["session-1"] = session |
| 73 | + manager.websockets["session-1"] = websocket |
| 74 | + |
| 75 | + # Exercise the provider validator and real session forwarding without a live connection. |
| 76 | + provider_event = ( |
| 77 | + { |
| 78 | + "type": "response.output_audio_transcript.done", |
| 79 | + "event_id": "event-1", |
| 80 | + "item_id": "item-1", |
| 81 | + "response_id": "response-1", |
| 82 | + "output_index": 0, |
| 83 | + "content_index": 0, |
| 84 | + "transcript": [marker], |
| 85 | + } |
| 86 | + if malformed |
| 87 | + else { |
| 88 | + "type": "error", |
| 89 | + "event_id": "event-1", |
| 90 | + "error": {"type": "invalid_request_error", "message": marker}, |
| 91 | + } |
| 92 | + ) |
| 93 | + with caplog.at_level(logging.DEBUG, logger=app_server.__name__): |
| 94 | + try: |
| 95 | + await model._handle_ws_event(provider_event) |
| 96 | + finally: |
| 97 | + await session.close() |
| 98 | + await manager._process_events("session-1") |
| 99 | + |
| 100 | + original_error = listener.on_event.call_args_list[-1].args[0].error |
| 101 | + if malformed: |
| 102 | + assert isinstance(original_error, ValidationError) |
| 103 | + assert marker in str(original_error) |
| 104 | + assert original_error.__traceback__ is not None |
| 105 | + assert original_error.__cause__ is None |
| 106 | + assert original_error.__context__ is None |
| 107 | + else: |
| 108 | + assert original_error.message == marker |
| 109 | + |
| 110 | + # Application consumers keep full diagnostics; only the logging representation changes. |
| 111 | + sent_events = [json.loads(call.args[0]) for call in websocket.send_text.call_args_list] |
| 112 | + sent_error = next(event for event in sent_events if event["type"] == "error") |
| 113 | + assert sent_error["error"] == str(original_error) |
| 114 | + records = [record for record in caplog.records if record.name == app_server.__name__] |
| 115 | + for record in records if not dont_log_model_data else caplog.records: |
| 116 | + assert marker not in logging.Formatter().format(record) |
| 117 | + assert marker not in repr(record.args) |
| 118 | + assert record.exc_info is None |
| 119 | + assert record.exc_text is None |
| 120 | + assert record.stack_info is None |
| 121 | + assert any("error_type" in record.getMessage() for record in records) |
| 122 | + assert any(type(original_error).__name__ in record.getMessage() for record in records) |
| 123 | + |
| 124 | + |
| 125 | +def test_error_debug_summary_omits_exception_chaining( |
| 126 | + app_server: ModuleType, caplog: pytest.LogCaptureFixture |
| 127 | +) -> None: |
| 128 | + try: |
| 129 | + try: |
| 130 | + raise ValueError("synthetic-private-cause") |
| 131 | + except ValueError as cause: |
| 132 | + raise RuntimeError("synthetic-private-error") from cause |
| 133 | + except RuntimeError as error: |
| 134 | + event = RealtimeError(error=error, info=RealtimeEventInfo(context=RunContextWrapper(None))) |
| 135 | + assert event.error.__cause__ is event.error.__context__ |
| 136 | + assert event.error.__cause__ is not None |
| 137 | + |
| 138 | + with caplog.at_level(logging.DEBUG, logger=app_server.__name__): |
| 139 | + app_server.manager._log_debug_event("session-1", event) |
| 140 | + |
| 141 | + assert "RuntimeError" in caplog.text |
| 142 | + assert "synthetic-private" not in caplog.text |
| 143 | + for record in caplog.records: |
| 144 | + assert "synthetic-private" not in repr(record.args) |
| 145 | + assert record.exc_info is None |
| 146 | + assert record.exc_text is None |
| 147 | + assert record.stack_info is None |
0 commit comments