|
20 | 20 | is_openai_model_name, |
21 | 21 | select_compaction_candidate_items, |
22 | 22 | ) |
| 23 | +from agents.run_internal.items import ( |
| 24 | + TOOL_CALL_SESSION_DESCRIPTION_KEY, |
| 25 | + TOOL_CALL_SESSION_TITLE_KEY, |
| 26 | +) |
23 | 27 | from tests.fake_model import FakeModel |
24 | 28 | from tests.test_responses import get_function_tool, get_function_tool_call, get_text_message |
25 | 29 | from tests.utils.simple_session import SimpleListSession |
@@ -215,6 +219,104 @@ async def test_run_compaction_auto_without_response_id_uses_input(self) -> None: |
215 | 219 | assert "previous_response_id" not in call_kwargs |
216 | 220 | assert call_kwargs.get("input") == items |
217 | 221 |
|
| 222 | + @pytest.mark.asyncio |
| 223 | + async def test_run_compaction_input_mode_strips_internal_tool_call_metadata(self) -> None: |
| 224 | + mock_session = self.create_mock_session() |
| 225 | + items: list[TResponseInputItem] = [ |
| 226 | + cast( |
| 227 | + TResponseInputItem, |
| 228 | + { |
| 229 | + "type": "function_call", |
| 230 | + "call_id": "call_123", |
| 231 | + "name": "lookup_account", |
| 232 | + "arguments": "{}", |
| 233 | + TOOL_CALL_SESSION_DESCRIPTION_KEY: "Lookup customer records.", |
| 234 | + TOOL_CALL_SESSION_TITLE_KEY: "Lookup Account", |
| 235 | + }, |
| 236 | + ), |
| 237 | + cast( |
| 238 | + TResponseInputItem, |
| 239 | + { |
| 240 | + "type": "function_call_output", |
| 241 | + "call_id": "call_123", |
| 242 | + "output": "ok", |
| 243 | + }, |
| 244 | + ), |
| 245 | + ] |
| 246 | + mock_session.get_items.return_value = items |
| 247 | + |
| 248 | + mock_compact_response = MagicMock() |
| 249 | + mock_compact_response.output = [] |
| 250 | + |
| 251 | + mock_client = MagicMock() |
| 252 | + mock_client.responses.compact = AsyncMock(return_value=mock_compact_response) |
| 253 | + |
| 254 | + session = OpenAIResponsesCompactionSession( |
| 255 | + session_id="test", |
| 256 | + underlying_session=mock_session, |
| 257 | + client=mock_client, |
| 258 | + compaction_mode="input", |
| 259 | + ) |
| 260 | + |
| 261 | + await session.run_compaction({"force": True}) |
| 262 | + |
| 263 | + call_kwargs = mock_client.responses.compact.call_args.kwargs |
| 264 | + compact_input = cast(list[dict[str, Any]], call_kwargs["input"]) |
| 265 | + assert compact_input[0]["type"] == "function_call" |
| 266 | + assert TOOL_CALL_SESSION_DESCRIPTION_KEY not in compact_input[0] |
| 267 | + assert TOOL_CALL_SESSION_TITLE_KEY not in compact_input[0] |
| 268 | + |
| 269 | + @pytest.mark.asyncio |
| 270 | + async def test_run_compaction_uses_sanitized_cached_items_after_add(self) -> None: |
| 271 | + mock_session = self.create_mock_session() |
| 272 | + mock_session.get_items.return_value = [] |
| 273 | + |
| 274 | + mock_compact_response = MagicMock() |
| 275 | + mock_compact_response.output = [] |
| 276 | + |
| 277 | + mock_client = MagicMock() |
| 278 | + mock_client.responses.compact = AsyncMock(return_value=mock_compact_response) |
| 279 | + |
| 280 | + session = OpenAIResponsesCompactionSession( |
| 281 | + session_id="test", |
| 282 | + underlying_session=mock_session, |
| 283 | + client=mock_client, |
| 284 | + compaction_mode="input", |
| 285 | + ) |
| 286 | + |
| 287 | + await session._ensure_compaction_candidates() |
| 288 | + await session.add_items( |
| 289 | + [ |
| 290 | + cast( |
| 291 | + TResponseInputItem, |
| 292 | + { |
| 293 | + "type": "function_call", |
| 294 | + "call_id": "call_cached", |
| 295 | + "name": "lookup_account", |
| 296 | + "arguments": "{}", |
| 297 | + TOOL_CALL_SESSION_DESCRIPTION_KEY: "Lookup customer records.", |
| 298 | + TOOL_CALL_SESSION_TITLE_KEY: "Lookup Account", |
| 299 | + }, |
| 300 | + ), |
| 301 | + cast( |
| 302 | + TResponseInputItem, |
| 303 | + { |
| 304 | + "type": "function_call_output", |
| 305 | + "call_id": "call_cached", |
| 306 | + "output": "ok", |
| 307 | + }, |
| 308 | + ), |
| 309 | + ] |
| 310 | + ) |
| 311 | + |
| 312 | + await session.run_compaction({"force": True}) |
| 313 | + |
| 314 | + call_kwargs = mock_client.responses.compact.call_args.kwargs |
| 315 | + compact_input = cast(list[dict[str, Any]], call_kwargs["input"]) |
| 316 | + assert compact_input[0]["type"] == "function_call" |
| 317 | + assert TOOL_CALL_SESSION_DESCRIPTION_KEY not in compact_input[0] |
| 318 | + assert TOOL_CALL_SESSION_TITLE_KEY not in compact_input[0] |
| 319 | + |
218 | 320 | @pytest.mark.asyncio |
219 | 321 | async def test_run_compaction_auto_uses_input_when_store_false(self) -> None: |
220 | 322 | mock_session = self.create_mock_session() |
|
0 commit comments