fix(agent): address session and streaming concurrency bugs

This commit is contained in:
hamb1y
2026-05-28 22:54:46 +08:00
committed by Xubin Ren
parent 1a4ae8994d
commit 0df60416ba
10 changed files with 250 additions and 39 deletions
+41
View File
@@ -21,6 +21,17 @@ class ScriptedProvider(LLMProvider):
raise response
return response
async def chat_stream(self, *args, **kwargs) -> LLMResponse:
self.calls += 1
self.last_kwargs = kwargs
response = self._responses.pop(0)
if isinstance(response, BaseException):
raise response
delta = getattr(response, "_test_stream_delta", None)
if delta and kwargs.get("on_content_delta"):
await kwargs["on_content_delta"](delta)
return response
def get_default_model(self) -> str:
return "test-model"
@@ -122,6 +133,36 @@ async def test_chat_with_retry_preserves_cancelled_error() -> None:
await provider.chat_with_retry(messages=[{"role": "user", "content": "hello"}])
@pytest.mark.asyncio
async def test_chat_stream_with_retry_does_not_retry_after_emitting_content(monkeypatch) -> None:
first = LLMResponse(content="stream stalled", finish_reason="error")
first._test_stream_delta = "partial" # type: ignore[attr-defined]
provider = ScriptedProvider([
first,
LLMResponse(content="ok"),
])
deltas: list[str] = []
delays: list[int] = []
async def _fake_sleep(delay: int) -> None:
delays.append(delay)
async def _on_delta(delta: str) -> None:
deltas.append(delta)
monkeypatch.setattr("nanobot.providers.base.asyncio.sleep", _fake_sleep)
response = await provider.chat_stream_with_retry(
messages=[{"role": "user", "content": "hello"}],
on_content_delta=_on_delta,
)
assert response.content == "stream stalled"
assert provider.calls == 1
assert deltas == ["partial"]
assert delays == []
@pytest.mark.asyncio
async def test_chat_with_retry_uses_provider_generation_defaults() -> None:
"""When callers omit generation params, provider.generation defaults are used."""