|
20 | 20 | select_compaction_candidate_items, |
21 | 21 | ) |
22 | 22 | from tests.fake_model import FakeModel |
23 | | -from tests.test_responses import get_text_message |
| 23 | +from tests.test_responses import get_function_tool, get_function_tool_call, get_text_message |
24 | 24 | from tests.utils.simple_session import SimpleListSession |
25 | 25 |
|
26 | 26 |
|
@@ -289,6 +289,118 @@ async def test_compaction_runs_during_runner_flow(self) -> None: |
289 | 289 | items = await session.get_items() |
290 | 290 | assert any(isinstance(item, dict) and item.get("type") == "compaction" for item in items) |
291 | 291 |
|
| 292 | + @pytest.mark.asyncio |
| 293 | + async def test_compaction_skips_when_tool_outputs_present(self) -> None: |
| 294 | + underlying = SimpleListSession() |
| 295 | + mock_client = MagicMock() |
| 296 | + mock_client.responses.compact = AsyncMock() |
| 297 | + |
| 298 | + session = OpenAIResponsesCompactionSession( |
| 299 | + session_id="demo", |
| 300 | + underlying_session=underlying, |
| 301 | + client=mock_client, |
| 302 | + should_trigger_compaction=lambda ctx: True, |
| 303 | + ) |
| 304 | + |
| 305 | + tool = get_function_tool(name="do_thing", return_value="done") |
| 306 | + model = FakeModel(initial_output=[get_function_tool_call("do_thing")]) |
| 307 | + agent = Agent( |
| 308 | + name="assistant", |
| 309 | + model=model, |
| 310 | + tools=[tool], |
| 311 | + tool_use_behavior="stop_on_first_tool", |
| 312 | + ) |
| 313 | + |
| 314 | + await Runner.run(agent, "hello", session=session) |
| 315 | + |
| 316 | + mock_client.responses.compact.assert_not_called() |
| 317 | + |
| 318 | + @pytest.mark.asyncio |
| 319 | + async def test_compaction_runs_after_deferred_tool_outputs_when_due(self) -> None: |
| 320 | + underlying = SimpleListSession() |
| 321 | + compacted = SimpleNamespace( |
| 322 | + output=[{"type": "compaction", "summary": "compacted"}], |
| 323 | + ) |
| 324 | + mock_client = MagicMock() |
| 325 | + mock_client.responses.compact = AsyncMock(return_value=compacted) |
| 326 | + |
| 327 | + def should_trigger_compaction(context: dict[str, Any]) -> bool: |
| 328 | + return any( |
| 329 | + isinstance(item, dict) and item.get("type") == "function_call_output" |
| 330 | + for item in context["session_items"] |
| 331 | + ) |
| 332 | + |
| 333 | + session = OpenAIResponsesCompactionSession( |
| 334 | + session_id="demo", |
| 335 | + underlying_session=underlying, |
| 336 | + client=mock_client, |
| 337 | + should_trigger_compaction=should_trigger_compaction, |
| 338 | + ) |
| 339 | + |
| 340 | + tool = get_function_tool(name="do_thing", return_value="done") |
| 341 | + model = FakeModel() |
| 342 | + model.add_multiple_turn_outputs( |
| 343 | + [ |
| 344 | + [get_function_tool_call("do_thing")], |
| 345 | + [get_text_message("ok")], |
| 346 | + ] |
| 347 | + ) |
| 348 | + agent = Agent( |
| 349 | + name="assistant", |
| 350 | + model=model, |
| 351 | + tools=[tool], |
| 352 | + tool_use_behavior="stop_on_first_tool", |
| 353 | + ) |
| 354 | + |
| 355 | + await Runner.run(agent, "hello", session=session) |
| 356 | + await Runner.run(agent, "followup", session=session) |
| 357 | + |
| 358 | + mock_client.responses.compact.assert_awaited_once() |
| 359 | + |
| 360 | + @pytest.mark.asyncio |
| 361 | + async def test_deferred_compaction_persists_across_tool_turns(self) -> None: |
| 362 | + underlying = SimpleListSession() |
| 363 | + compacted = SimpleNamespace( |
| 364 | + output=[{"type": "compaction", "summary": "compacted"}], |
| 365 | + ) |
| 366 | + mock_client = MagicMock() |
| 367 | + mock_client.responses.compact = AsyncMock(return_value=compacted) |
| 368 | + |
| 369 | + should_compact_calls = {"count": 0} |
| 370 | + |
| 371 | + def should_trigger_compaction(context: dict[str, Any]) -> bool: |
| 372 | + should_compact_calls["count"] += 1 |
| 373 | + return should_compact_calls["count"] == 1 |
| 374 | + |
| 375 | + session = OpenAIResponsesCompactionSession( |
| 376 | + session_id="demo", |
| 377 | + underlying_session=underlying, |
| 378 | + client=mock_client, |
| 379 | + should_trigger_compaction=should_trigger_compaction, |
| 380 | + ) |
| 381 | + |
| 382 | + tool = get_function_tool(name="do_thing", return_value="done") |
| 383 | + model = FakeModel() |
| 384 | + model.add_multiple_turn_outputs( |
| 385 | + [ |
| 386 | + [get_function_tool_call("do_thing")], |
| 387 | + [get_function_tool_call("do_thing")], |
| 388 | + [get_text_message("ok")], |
| 389 | + ] |
| 390 | + ) |
| 391 | + agent = Agent( |
| 392 | + name="assistant", |
| 393 | + model=model, |
| 394 | + tools=[tool], |
| 395 | + tool_use_behavior="stop_on_first_tool", |
| 396 | + ) |
| 397 | + |
| 398 | + await Runner.run(agent, "hello", session=session) |
| 399 | + await Runner.run(agent, "again", session=session) |
| 400 | + await Runner.run(agent, "final", session=session) |
| 401 | + |
| 402 | + mock_client.responses.compact.assert_awaited_once() |
| 403 | + |
292 | 404 |
|
293 | 405 | class TestTypeGuard: |
294 | 406 | def test_is_compaction_aware_session_true(self) -> None: |
|
0 commit comments