mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 13:19:44 +08:00
fix(subagent): retain long subprocess output lines
This commit is contained in:
@@ -89,11 +89,20 @@ async def stream_process_lines(
|
||||
await queue.put(None)
|
||||
return
|
||||
try:
|
||||
pending = bytearray()
|
||||
while True:
|
||||
raw = await stream.readline()
|
||||
# JSONL backends can emit whole answers or tool results in one
|
||||
# frame, exceeding StreamReader.readline's default 64 KiB limit.
|
||||
raw = await stream.read(65536)
|
||||
if not raw:
|
||||
break
|
||||
await queue.put((channel, raw.decode("utf-8", "replace").rstrip("\r\n")))
|
||||
pending.extend(raw)
|
||||
while (boundary := pending.find(b"\n")) != -1:
|
||||
line = bytes(pending[:boundary])
|
||||
del pending[: boundary + 1]
|
||||
await queue.put((channel, line.decode("utf-8", "replace").rstrip("\r\n")))
|
||||
if pending:
|
||||
await queue.put((channel, pending.decode("utf-8", "replace").rstrip("\r\n")))
|
||||
except Exception: # pragma: no cover - defensive: a broken pipe must not hang the queue
|
||||
logger.debug("subagent %s pump failed", channel, exc_info=True)
|
||||
finally:
|
||||
|
||||
53
tests/services/test_subagent_long_lines.py
Normal file
53
tests/services/test_subagent_long_lines.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""Native subprocess output must survive large, message-granular frames."""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from deeptutor.services.subagent.openclaw import OpenClawBackend
|
||||
from deeptutor.services.subagent.process import stream_process_lines
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_large_native_lines_preserve_both_channels_and_final_fragment():
|
||||
text = "学习" * 40000
|
||||
script = (
|
||||
"import sys; "
|
||||
"sys.stdout.buffer.write(('学习' * 40000 + '\\n').encode()); "
|
||||
"sys.stdout.flush(); sys.stderr.write('diagnostic\\n'); "
|
||||
"sys.stderr.flush(); sys.stdout.buffer.write('尾部'.encode()); sys.stdout.flush()"
|
||||
)
|
||||
|
||||
async def collect():
|
||||
return [item async for item in stream_process_lines([sys.executable, "-c", script])]
|
||||
|
||||
# The unfixed reader can stop draining and leave the child blocked on its pipe.
|
||||
output = await asyncio.wait_for(collect(), timeout=5)
|
||||
assert [line for channel, line in output if channel == "stdout"] == [text, "尾部"]
|
||||
assert ("stderr", "diagnostic") in output
|
||||
assert output[-1] == ("exit", "0")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_openclaw_receives_large_native_json_answer(monkeypatch):
|
||||
answer = "x" * 100000
|
||||
script = (
|
||||
"import json; print(json.dumps({'status': 'ok', "
|
||||
"'result': {'payloads': [{'text': 'x' * 100000}]}}))"
|
||||
)
|
||||
backend = OpenClawBackend()
|
||||
monkeypatch.setattr(
|
||||
backend,
|
||||
"_build_command",
|
||||
lambda *args, **kwargs: [sys.executable, "-c", script],
|
||||
)
|
||||
events = []
|
||||
|
||||
async def on_event(event):
|
||||
events.append(event)
|
||||
|
||||
result = await asyncio.wait_for(backend.consult("question", on_event=on_event), timeout=5)
|
||||
assert result.success, result.error
|
||||
assert result.final_text == answer
|
||||
assert any(event.text == answer for event in events)
|
||||
Reference in New Issue
Block a user