|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import warnings as warnings_module |
| 4 | +from types import SimpleNamespace |
4 | 5 | from typing import Any, cast |
5 | 6 | from unittest.mock import AsyncMock, MagicMock |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 |
|
| 10 | +from agents import Agent, Runner |
9 | 11 | from agents.items import TResponseInputItem |
10 | 12 | from agents.memory import ( |
11 | 13 | OpenAIResponsesCompactionSession, |
|
17 | 19 | is_openai_model_name, |
18 | 20 | select_compaction_candidate_items, |
19 | 21 | ) |
| 22 | +from tests.fake_model import FakeModel |
| 23 | +from tests.test_responses import get_text_message |
| 24 | +from tests.utils.simple_session import SimpleListSession |
20 | 25 |
|
21 | 26 |
|
22 | 27 | class TestIsOpenAIModelName: |
@@ -253,6 +258,36 @@ def model_dump( |
253 | 258 | await session.run_compaction({"response_id": "resp-123"}) |
254 | 259 |
|
255 | 260 | assert warning_model.received_warnings_arg is False |
| 261 | + mock_client.responses.compact.assert_called_once_with( |
| 262 | + previous_response_id="resp-123", |
| 263 | + model="gpt-4.1", |
| 264 | + ) |
| 265 | + |
| 266 | + @pytest.mark.asyncio |
| 267 | + async def test_compaction_runs_during_runner_flow(self) -> None: |
| 268 | + """Ensure Runner triggers compaction when using a compaction-aware session.""" |
| 269 | + underlying = SimpleListSession() |
| 270 | + compacted = SimpleNamespace( |
| 271 | + output=[{"type": "compaction", "encrypted_content": "enc"}], |
| 272 | + ) |
| 273 | + mock_client = MagicMock() |
| 274 | + mock_client.responses.compact = AsyncMock(return_value=compacted) |
| 275 | + |
| 276 | + session = OpenAIResponsesCompactionSession( |
| 277 | + session_id="demo", |
| 278 | + underlying_session=underlying, |
| 279 | + client=mock_client, |
| 280 | + should_trigger_compaction=lambda ctx: True, |
| 281 | + ) |
| 282 | + |
| 283 | + model = FakeModel(initial_output=[get_text_message("ok")]) |
| 284 | + agent = Agent(name="assistant", model=model) |
| 285 | + |
| 286 | + await Runner.run(agent, "hello", session=session) |
| 287 | + |
| 288 | + mock_client.responses.compact.assert_awaited_once() |
| 289 | + items = await session.get_items() |
| 290 | + assert any(isinstance(item, dict) and item.get("type") == "compaction" for item in items) |
256 | 291 |
|
257 | 292 |
|
258 | 293 | class TestTypeGuard: |
|
0 commit comments