Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(sdk): preserve queued events on stream close
Lanre Shittu816a999cac1a
diff --git a/nanobot/nanobot.py b/nanobot/nanobot.pyindex 6c9951ed8..3c1654cb0 100644--- a/nanobot/nanobot.py+++ b/nanobot/nanobot.py@@ -302,7 +302,7 @@ class Nanobot:))raisefinally:- emitter.close()+ await emitter.close()task = asyncio.create_task(_run())return RunStream(task, queue)diff --git a/nanobot/sdk/streaming.py b/nanobot/sdk/streaming.pyindex 1abfd64fc..b8036bd67 100644--- a/nanobot/sdk/streaming.py+++ b/nanobot/sdk/streaming.py@@ -158,15 +158,11 @@ class SDKStreamEmitter:resuming=resuming,))- def close(self) -> None:+ async def close(self) -> None:if self._closed:returnself._closed = True- if self._queue.full():- with suppress(asyncio.QueueEmpty):- self._queue.get_nowait()- with suppress(asyncio.QueueFull):- self._queue.put_nowait(_STREAM_SENTINEL)+ await self._queue.put(_STREAM_SENTINEL)class SDKStreamingHook(AgentHook):diff --git a/tests/test_sdk_streaming.py b/tests/test_sdk_streaming.pynew file mode 100644index 000000000..a292315d8--- /dev/null+++ b/tests/test_sdk_streaming.py@@ -0,0 +1,24 @@+"""Tests for SDK streaming primitives."""++import asyncio++import pytest++from nanobot.sdk.streaming import SDKStreamEmitter+from nanobot.sdk.types import STREAM_EVENT_TEXT_DELTA, StreamEvent+++async def test_close_preserves_events_when_queue_is_full():+ queue: asyncio.Queue[StreamEvent | object] = asyncio.Queue(maxsize=1)+ emitter = SDKStreamEmitter(queue)+ event = StreamEvent(type=STREAM_EVENT_TEXT_DELTA, delta="kept")+ await emitter.emit(event)++ close_task = asyncio.create_task(emitter.close())+ await asyncio.sleep(0)++ assert not close_task.done()+ assert queue.get_nowait() is event+ await close_task+ assert queue.qsize() == 1