diff --git a/src/copper_pilot_cli/copper_background.py b/src/copper_pilot_cli/copper_background.py index 7715786..e4ad9e0 100644 --- a/src/copper_pilot_cli/copper_background.py +++ b/src/copper_pilot_cli/copper_background.py @@ -58,15 +58,14 @@ def text(self) -> str: async def drain(stream: asyncio.StreamReader | None, buffer: OutputBuffer) -> None: - """Copy a subprocess stream into the buffer until EOF or the limit.""" + """Copy a subprocess stream into the buffer until EOF, discarding past the limit.""" if stream is None: return while True: chunk = await stream.read(65536) if not chunk: return - if not buffer.append(chunk): - return + buffer.append(chunk) async def kill_process_tree(process: asyncio.subprocess.Process) -> None: diff --git a/tests/test_durability.py b/tests/test_durability.py index 693506d..b3cd421 100644 --- a/tests/test_durability.py +++ b/tests/test_durability.py @@ -1026,6 +1026,39 @@ async def test_run_shell_caps_output(tmp_path) -> None: assert result["result"] == "x" * 10 +@pytest.mark.asyncio +async def test_background_output_overflow_does_not_stall_wait_all(tmp_path): + manager = BackgroundProcessManager() + + script = ( + "import sys, time; " + "print('started', flush=True); " + "time.sleep(0.15); " + "sys.stdout.write('x' * 2_000_000); " + "sys.stdout.flush()" + ) + + started = await run_shell( + py(script), + tmp_path, + manager=manager, + foreground_seconds=0.03, + output_limit=128, + ) + + assert started["background"] is True + + try: + finished = await asyncio.wait_for(manager.wait_all(), timeout=3) + finally: + await manager.kill_all() + + task = finished[0] + assert task["output_truncated"] is True + assert len(task["output"]) <= 128 + assert task["status"] == "completed" + + @pytest.mark.asyncio async def test_broker_execute_uses_the_background_contract(tmp_path) -> None: broker = LocalToolBroker(tmp_path, mode=ApprovalMode.YOLO)