|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +import json |
4 | 5 | import logging |
5 | 6 | import sqlite3 |
6 | 7 | import threading |
|
11 | 12 | from typing import Any, cast |
12 | 13 | from unittest.mock import AsyncMock, MagicMock, patch |
13 | 14 |
|
| 15 | +import httpx2 |
14 | 16 | import pytest |
| 17 | +from openai import AsyncOpenAI |
15 | 18 | from openai.types.responses.response_usage import ( |
16 | 19 | InputTokensDetails, |
17 | 20 | OutputTokensDetails, |
18 | 21 | ResponseUsage, |
19 | 22 | ) |
20 | 23 |
|
21 | 24 | import agents._debug as _debug |
22 | | -from agents import Agent, Runner |
| 25 | +from agents import Agent, OpenAIResponsesModel, RunConfig, Runner |
23 | 26 | from agents.items import MessageOutputItem, TResponseInputItem |
24 | 27 | from agents.memory import ( |
25 | 28 | OpenAIResponsesCompactionSession, |
|
45 | 48 | ) |
46 | 49 | from agents.run_state import RunState |
47 | 50 | from agents.testing import ModelStep, ScriptedModel |
| 51 | +from tests.model_test_helpers import get_response_obj |
48 | 52 | from tests.test_responses import get_function_tool, get_function_tool_call, get_text_message |
49 | 53 | from tests.utils.simple_session import SimpleListSession |
50 | 54 |
|
51 | 55 |
|
| 56 | +@pytest.mark.asyncio |
| 57 | +@pytest.mark.allow_call_model_methods |
| 58 | +@pytest.mark.parametrize("history_source", ["automatic", "manual", "stored"]) |
| 59 | +async def test_compaction_replay_strips_created_by(history_source: str) -> None: |
| 60 | + compacted_item = { |
| 61 | + "id": "cmp_synthetic", |
| 62 | + "type": "compaction", |
| 63 | + "encrypted_content": "synthetic-encrypted-content", |
| 64 | + "created_by": "server", |
| 65 | + } |
| 66 | + request_inputs: list[list[dict[str, Any]]] = [] |
| 67 | + |
| 68 | + async def handler(request: httpx2.Request) -> httpx2.Response: |
| 69 | + request_inputs.append(json.loads(request.content)["input"]) |
| 70 | + if request.url.path.endswith("/compact"): |
| 71 | + return httpx2.Response( |
| 72 | + 200, |
| 73 | + json={ |
| 74 | + "id": "cmp_response", |
| 75 | + "object": "response.compaction", |
| 76 | + "created_at": 1, |
| 77 | + "output": [compacted_item], |
| 78 | + }, |
| 79 | + ) |
| 80 | + assert request.url.path.endswith("/responses") |
| 81 | + return httpx2.Response( |
| 82 | + 200, content=get_response_obj([get_text_message("42")]).model_dump_json() |
| 83 | + ) |
| 84 | + |
| 85 | + underlying = SQLiteSession("created-by-replay") |
| 86 | + try: |
| 87 | + async with AsyncOpenAI( |
| 88 | + api_key="synthetic-key", |
| 89 | + http_client=httpx2.AsyncClient(transport=httpx2.MockTransport(handler)), |
| 90 | + ) as client: |
| 91 | + session = OpenAIResponsesCompactionSession( |
| 92 | + session_id="created-by-replay", |
| 93 | + underlying_session=underlying, |
| 94 | + client=client, |
| 95 | + model="gpt-5", |
| 96 | + compaction_mode="input", |
| 97 | + should_trigger_compaction=lambda ctx: bool(ctx["compaction_candidate_items"]), |
| 98 | + ) |
| 99 | + agent = Agent(name="test", model=OpenAIResponsesModel("gpt-5", openai_client=client)) |
| 100 | + config = RunConfig(tracing_disabled=True) |
| 101 | + if history_source == "stored": |
| 102 | + # History saved before the fix must also remain usable. |
| 103 | + await underlying.add_items([cast(TResponseInputItem, compacted_item)]) |
| 104 | + elif history_source == "manual": |
| 105 | + await session.add_items([{"role": "user", "content": "How many?"}]) |
| 106 | + await session.run_compaction({"force": True}) |
| 107 | + else: |
| 108 | + await Runner.run(agent, "How many?", session=session, run_config=config) |
| 109 | + |
| 110 | + stored_items = await underlying.get_items() |
| 111 | + assert stored_items == [compacted_item] |
| 112 | + if history_source == "manual": |
| 113 | + # Repeated manual compaction reuses cached output without a history reload. |
| 114 | + await session.add_items([{"role": "user", "content": "More context"}]) |
| 115 | + await session.run_compaction({"force": True}) |
| 116 | + result = await Runner.run(agent, "And urgent?", session=session, run_config=config) |
| 117 | + assert result.final_output == "42" |
| 118 | + |
| 119 | + # Inspect encoded inputs to both Responses and the next compact request. |
| 120 | + replayed_items = [ |
| 121 | + item |
| 122 | + for items in request_inputs |
| 123 | + for item in items |
| 124 | + if item.get("type") == "compaction" |
| 125 | + ] |
| 126 | + assert replayed_items == [ |
| 127 | + { |
| 128 | + "id": "cmp_synthetic", |
| 129 | + "type": "compaction", |
| 130 | + "encrypted_content": "synthetic-encrypted-content", |
| 131 | + } |
| 132 | + ] * (3 if history_source == "manual" else 2) |
| 133 | + assert stored_items[0] == compacted_item |
| 134 | + assert (await underlying.get_items())[0] == compacted_item |
| 135 | + finally: |
| 136 | + underlying.close() |
| 137 | + |
| 138 | + |
52 | 139 | class TestIsOpenAIModelName: |
53 | 140 | def test_gpt_models(self) -> None: |
54 | 141 | assert is_openai_model_name("gpt-4o") is True |
|
0 commit comments