Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/copper_pilot_cli/copper_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions tests/test_durability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down