sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -1,5 +1,6 @@
"""Regression coverage for idempotent Deep Research job submission."""
import asyncio
import json
import os
from datetime import UTC, datetime, timedelta
@@ -51,6 +52,9 @@ class _Dispatcher:
def nudge(self) -> None:
self.nudges += 1
def ensure_started(self) -> bool:
return False
def cancel_running(self, job_id: str) -> bool: # noqa: ARG002
return False
@@ -264,22 +268,19 @@ async def test_report_variant_uses_normal_job_with_selected_sources(monkeypatch)
@pytest.mark.asyncio
async def test_stream_stays_open_when_durable_replay_has_a_transient_failure(monkeypatch):
"""A stream DB blip must yield a recoverable warning, not an ASGI error group."""
async def test_stream_stays_open_when_event_store_is_unavailable(monkeypatch):
"""Writing SSE must not wait on the event table to show the first frame."""
import asyncio
async def fake_require_user(request): # noqa: ANN001
return "u1"
class _StreamJobStore:
async def get(self, job_id: str, *, user_id: str): # noqa: ARG002
return {"id": job_id, "session_id": "drs_1"}
return {"id": job_id, "session_id": "drs_1", "status": "running"}
async def get_unscoped(self, job_id: str): # noqa: ARG002
return None
class _UnavailableEventStore:
async def list_after(self, *args, **kwargs): # noqa: ANN002, ANN003
raise RuntimeError("temporary replica failure")
return {"id": job_id, "status": "running", "phase": "collecting", "progress": 10}
async def is_connected() -> bool:
return False
@@ -289,17 +290,109 @@ async def test_stream_stays_open_when_durable_replay_has_a_transient_failure(mon
app=SimpleNamespace(
state=SimpleNamespace(
deep_research_job_store=_StreamJobStore(),
deep_research_event_store=_UnavailableEventStore(),
)
),
is_disconnected=is_connected,
)
response = await deep_research.stream_job("drj_1", request)
chunk = await anext(response.body_iterator)
first = await asyncio.wait_for(anext(response.body_iterator), timeout=1.0)
await response.body_iterator.aclose()
assert "durable_replay_temporarily_unavailable" in chunk
assert first.lstrip().startswith(": ping")
@pytest.mark.asyncio
async def test_stream_emits_keepalive_ping_when_idle(monkeypatch):
"""Idle harvest must keep sending bytes so production proxies do not drop SSE."""
async def fake_require_user(request): # noqa: ANN001
return "u1"
class _RunningJobStore:
async def get(self, job_id: str, *, user_id: str): # noqa: ARG002
return {"id": job_id, "session_id": "drs_1", "status": "running"}
async def get_unscoped(self, job_id: str): # noqa: ARG002
return {"id": job_id, "status": "running", "phase": "collecting", "progress": 10}
class _EmptyEventStore:
async def list_after(self, *args, **kwargs): # noqa: ANN002, ANN003
return []
async def is_connected() -> bool:
return False
monkeypatch.setattr(deep_research, "_require_user", fake_require_user)
monkeypatch.setattr(deep_research, "_SSE_PING_INTERVAL_S", 0)
monkeypatch.setattr(deep_research, "_POLL_INTERVAL_S", 0)
request = SimpleNamespace(
app=SimpleNamespace(
state=SimpleNamespace(
deep_research_job_store=_RunningJobStore(),
deep_research_event_store=_EmptyEventStore(),
)
),
is_disconnected=is_connected,
)
response = await deep_research.stream_job("drj_1", request)
first = await anext(response.body_iterator)
second = await anext(response.body_iterator)
await response.body_iterator.aclose()
assert first.lstrip().startswith(": ping")
assert second.lstrip().startswith(": ping")
assert "Cache-Control" in response.headers
assert "no-transform" in response.headers["Cache-Control"]
@pytest.mark.asyncio
async def test_stream_delivers_live_frames_without_waiting_on_event_store(monkeypatch):
"""The first write frame must come from the live hub, not the event table."""
from app.gateway.deep_research_live_hub import DeepResearchLiveHub
async def fake_require_user(request): # noqa: ANN001
return "u1"
class _RunningJobStore:
async def get(self, job_id: str, *, user_id: str): # noqa: ARG002
return {"id": job_id, "session_id": "drs_1", "status": "running"}
async def get_unscoped(self, job_id: str): # noqa: ARG002
return {"id": job_id, "status": "running", "phase": "collecting", "progress": 10}
async def is_connected() -> bool:
return False
hub = DeepResearchLiveHub()
monkeypatch.setattr(deep_research, "_require_user", fake_require_user)
request = SimpleNamespace(
app=SimpleNamespace(
state=SimpleNamespace(
deep_research_job_store=_RunningJobStore(),
deep_research_live_hub=hub,
)
),
is_disconnected=is_connected,
)
response = await deep_research.stream_job("drj_1", request)
iterator = response.body_iterator
first = await asyncio.wait_for(anext(iterator), timeout=1.0)
await hub.publish({
"seq": 0,
"sessionId": "drs_1",
"jobId": "drj_1",
"type": "warning",
"phase": "collecting",
"payload": {"code": "write_job_started", "message": "写作任务已启动,正在读取已收集材料"},
})
second = await asyncio.wait_for(anext(iterator), timeout=1.0)
await iterator.aclose()
assert first.lstrip().startswith(": ping")
assert "write_job_started" in second
@pytest.mark.asyncio
@@ -644,7 +737,6 @@ async def test_write_report_treats_browser_materials_as_authoritative(monkeypatc
assert response["jobId"] == "drj_write"
assert response["collectorMaterialCount"] == 1
assert response["persistedSourceCount"] == 1
snapshot = jobs.created["input_snapshot"]
assert snapshot["entry"] == "chat"
assert snapshot["query"] == "前端课题"
@@ -652,9 +744,6 @@ async def test_write_report_treats_browser_materials_as_authoritative(monkeypatc
assert snapshot["client_materials_authoritative"] is True
assert snapshot["collector_materials"] == materials
assert snapshot["collector_material_count"] == 1
assert sources.rows[0]["title"] == "来源甲"
assert sources.rows[0]["selected"] is True
assert response["diagnostics"] == []
@pytest.mark.asyncio
@@ -695,7 +784,6 @@ async def test_write_report_caps_job_snapshot_to_fallback_limit(monkeypatch):
snapshot = jobs.created["input_snapshot"]
assert response["collectorMaterialCount"] == 25
assert response["persistedSourceCount"] == 25
assert len(snapshot["collector_materials"]) == 20
assert snapshot["collector_materials"][0]["title"] == "来源0"
assert snapshot["collector_materials"][-1]["title"] == "来源19"