sync update from LAN repo (source ji @ 85078a31)
This commit is contained in:
1 parent
5d6627af50
commit
e602d88695
864 files changed
+99150
-6346
No files matched your search
@@ -304,6 +304,134 @@ def _parallel_request() -> SimpleNamespace:
|
||||
)
|
||||
|
||||
|
||||
async def _collect_parallel_events(payload: dict[str, object]) -> list[dict[str, object]]:
|
||||
response = await parallel_agents.run_parallel_agents_stream(
|
||||
_parallel_request(),
|
||||
payload,
|
||||
)
|
||||
events: list[dict[str, object]] = []
|
||||
async for chunk in response.body_iterator:
|
||||
text = chunk.decode() if isinstance(chunk, bytes) else chunk
|
||||
for line in text.splitlines():
|
||||
if line.startswith("data:"):
|
||||
events.append(json.loads(line[5:].strip()))
|
||||
return events
|
||||
|
||||
|
||||
def test_parallel_agent_tool_progress_message_is_specific_and_bounded() -> None:
|
||||
assert (
|
||||
parallel_agents._tool_progress_message(
|
||||
"skill_view",
|
||||
{"skill_name": "industry-research"},
|
||||
)
|
||||
== "正在读取技能说明:industry-research"
|
||||
)
|
||||
assert (
|
||||
parallel_agents._tool_progress_message(
|
||||
"llmwiki_search",
|
||||
{"query": " 人工智能 产业趋势 "},
|
||||
)
|
||||
== "正在检索知识库:人工智能 产业趋势"
|
||||
)
|
||||
long_message = parallel_agents._tool_progress_message(
|
||||
"read_file",
|
||||
{"path": "/mnt/user-data/" + ("very-long-segment-" * 8)},
|
||||
)
|
||||
assert long_message.startswith("正在读取资料:/mnt/user-data/")
|
||||
assert long_message.endswith("…")
|
||||
assert len(long_message.removeprefix("正在读取资料:").removesuffix("…")) == 64
|
||||
|
||||
|
||||
def test_parallel_agent_stream_heartbeat_precedes_terminal_event(monkeypatch) -> None:
|
||||
async def fake_run_one_agent(*_args, **_kwargs):
|
||||
await asyncio.sleep(0.035)
|
||||
yield {
|
||||
"status": "done",
|
||||
"content": "这是一份包含目标、执行路径、风险和验收标准的完整策略正文。",
|
||||
"agent_name": "strategy-selection-angle-security",
|
||||
"tool_calls": [],
|
||||
}
|
||||
# Keep the upstream iterator alive briefly after its terminal payload;
|
||||
# this used to allow a heartbeat to arrive after agent_done.
|
||||
await asyncio.sleep(0.025)
|
||||
|
||||
@asynccontextmanager
|
||||
async def fake_client(*_args, **_kwargs):
|
||||
yield object()
|
||||
|
||||
async def fake_create_thread(*_args, **_kwargs):
|
||||
return "thread-id"
|
||||
|
||||
monkeypatch.setattr(parallel_agents, "_run_one_agent", fake_run_one_agent)
|
||||
monkeypatch.setattr(parallel_agents, "_make_loopback_client", fake_client)
|
||||
monkeypatch.setattr(parallel_agents, "_create_thread", fake_create_thread)
|
||||
monkeypatch.setattr(parallel_agents, "_loopback_base", lambda _request: "http://gateway.test")
|
||||
monkeypatch.setattr(parallel_agents, "_auth_headers", lambda _request: {})
|
||||
monkeypatch.setattr(parallel_agents, "PROGRESS_HEARTBEAT_SECONDS", 0.01)
|
||||
monkeypatch.setattr(parallel_agents, "AGENT_STREAM_IDLE_TIMEOUT_SECONDS", 1.0)
|
||||
|
||||
events = asyncio.run(
|
||||
_collect_parallel_events(
|
||||
{
|
||||
"task_name": "heartbeat order",
|
||||
"brief": "show progress while a skill is silent",
|
||||
"agent_ids": ["strategy-selection-angle-security"],
|
||||
}
|
||||
)
|
||||
)
|
||||
event_types = [str(event.get("type")) for event in events]
|
||||
done_index = event_types.index("agent_done")
|
||||
progress_indexes = [index for index, event_type in enumerate(event_types) if event_type == "agent_progress"]
|
||||
|
||||
assert event_types[0] == "agent_start"
|
||||
assert progress_indexes
|
||||
assert all(index < done_index for index in progress_indexes)
|
||||
assert event_types[-1] == "all_done"
|
||||
|
||||
|
||||
def test_parallel_agent_stream_idle_timeout_finishes_with_error(monkeypatch) -> None:
|
||||
async def fake_run_one_agent(*_args, **_kwargs):
|
||||
await asyncio.sleep(60)
|
||||
if False: # pragma: no cover - keep this function an async generator
|
||||
yield None
|
||||
|
||||
@asynccontextmanager
|
||||
async def fake_client(*_args, **_kwargs):
|
||||
yield object()
|
||||
|
||||
async def fake_create_thread(*_args, **_kwargs):
|
||||
return "thread-id"
|
||||
|
||||
monkeypatch.setattr(parallel_agents, "_run_one_agent", fake_run_one_agent)
|
||||
monkeypatch.setattr(parallel_agents, "_make_loopback_client", fake_client)
|
||||
monkeypatch.setattr(parallel_agents, "_create_thread", fake_create_thread)
|
||||
monkeypatch.setattr(parallel_agents, "_loopback_base", lambda _request: "http://gateway.test")
|
||||
monkeypatch.setattr(parallel_agents, "_auth_headers", lambda _request: {})
|
||||
monkeypatch.setattr(parallel_agents, "PROGRESS_HEARTBEAT_SECONDS", 0.005)
|
||||
monkeypatch.setattr(parallel_agents, "AGENT_STREAM_IDLE_TIMEOUT_SECONDS", 0.025)
|
||||
|
||||
async def collect_with_deadline() -> list[dict[str, object]]:
|
||||
return await asyncio.wait_for(
|
||||
_collect_parallel_events(
|
||||
{
|
||||
"task_name": "timeout",
|
||||
"brief": "a stalled skill must not leave the page pending",
|
||||
"agent_ids": ["strategy-selection-angle-security"],
|
||||
}
|
||||
),
|
||||
timeout=1.0,
|
||||
)
|
||||
|
||||
events = asyncio.run(collect_with_deadline())
|
||||
errors = [event for event in events if event.get("type") == "agent_error"]
|
||||
|
||||
assert any(event.get("type") == "agent_progress" for event in events)
|
||||
assert len(errors) == 1
|
||||
assert "没有返回进度" in str(errors[0].get("message"))
|
||||
assert not any(event.get("type") == "agent_done" for event in events)
|
||||
assert events[-1]["type"] == "all_done"
|
||||
|
||||
|
||||
def test_parallel_agent_stream_separates_inline_and_provider_reasoning(monkeypatch) -> None:
|
||||
async def fake_run_one_agent(*_args, **_kwargs):
|
||||
yield "event: messages"
|
||||
|
||||
Reference in new issue
Block a user