@@ -221,6 +221,59 @@ def __call__(self, context: dict[str, Any]) -> bool:
221221 mock_client .responses .compact .assert_not_awaited ()
222222
223223 @pytest .mark .asyncio
224+ async def test_queued_input_compaction_survives_destructive_pop (self ) -> None :
225+ class BlockingPopSession (SimpleListSession ):
226+ def __init__ (self , history : list [TResponseInputItem ]) -> None :
227+ super ().__init__ (history = history )
228+ self .pop_started = asyncio .Event ()
229+ self .allow_pop = asyncio .Event ()
230+
231+ async def pop_item (self ) -> TResponseInputItem | None :
232+ self .pop_started .set ()
233+ await self .allow_pop .wait ()
234+ return await super ().pop_item ()
235+
236+ history : list [TResponseInputItem ] = [
237+ cast (
238+ TResponseInputItem ,
239+ {"type" : "message" , "role" : "assistant" , "content" : "old" },
240+ ),
241+ ]
242+ underlying = BlockingPopSession (history )
243+ compacted_response = MagicMock ()
244+ compacted_response .output = []
245+ client = MagicMock ()
246+ client .responses .compact = AsyncMock (return_value = compacted_response )
247+
248+ session = OpenAIResponsesCompactionSession (
249+ session_id = "test" ,
250+ underlying_session = underlying ,
251+ client = client ,
252+ compaction_mode = "input" ,
253+ )
254+ session ._response_id = "resp-old"
255+
256+ pop_task = asyncio .create_task (session .pop_item ())
257+ await underlying .pop_started .wait ()
258+
259+ compaction_task = asyncio .create_task (
260+ session .run_compaction ({"force" : True , "compaction_mode" : "input" })
261+ )
262+ await asyncio .sleep (0 )
263+ assert not compaction_task .done ()
264+
265+ underlying .allow_pop .set ()
266+ assert await pop_task == history [0 ]
267+
268+ await compaction_task
269+
270+ client .responses .compact .assert_awaited_once ()
271+ call_kwargs = client .responses .compact .call_args .kwargs
272+ assert "previous_response_id" not in call_kwargs
273+ assert call_kwargs ["input" ] == []
274+
275+
276+ @pytest .mark .asyncio
224277 async def test_run_compaction_input_mode_without_response_id (self ) -> None :
225278 mock_session = self .create_mock_session ()
226279 items : list [TResponseInputItem ] = [
0 commit comments