From ed8143f7d0daf43b76adcaf8a7e663089362311f Mon Sep 17 00:00:00 2001 From: yixi ning Date: Sat, 17 Oct 2026 08:58:06 +0000 Subject: [PATCH] fix(agent): preserve separators in streamed inline reasoning --- nanobot/utils/helpers.py | 4 +-- tests/agent/test_progress_inline_reasoning.py | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/agent/test_progress_inline_reasoning.py diff --git a/nanobot/utils/helpers.py b/nanobot/utils/helpers.py index 0af917472..08e8d8d51 100644 --- a/nanobot/utils/helpers.py +++ b/nanobot/utils/helpers.py @@ -288,9 +288,9 @@ class IncrementalThinkExtractor: thinking, _ = extract_think(buf) if not thinking or thinking == self._emitted: return False - new = thinking[len(self._emitted) :].strip() + new = thinking[len(self._emitted) :] self._emitted = thinking - if not new: + if not new.strip(): return False await emit(new) return True diff --git a/tests/agent/test_progress_inline_reasoning.py b/tests/agent/test_progress_inline_reasoning.py new file mode 100644 index 000000000..a0417e37c --- /dev/null +++ b/tests/agent/test_progress_inline_reasoning.py @@ -0,0 +1,32 @@ +"""Inline reasoning should not change when a provider splits its stream.""" + +import pytest + +from nanobot.agent.hook import AgentHookContext +from nanobot.agent.progress_hook import AgentProgressHook +from nanobot.bus.outbound_events import ProgressEvent +from nanobot.events import AgentEvent, EventSink + + +@pytest.mark.parametrize("chunks", [ + ["first paragraphsecond paragraph"], + ["first paragraph", "second paragraph"], +]) +async def test_inline_reasoning_preserves_paragraph_separators(chunks: list[str]) -> None: + events: list[AgentEvent] = [] + + async def publish(event: AgentEvent) -> None: + events.append(event) + + hook = AgentProgressHook(EventSink(publish=publish), streaming=True) + context = AgentHookContext(iteration=0, messages=[]) + + for chunk in chunks: + await hook.on_stream(context, chunk) + + reasoning = "".join( + event.content for event in events + if isinstance(event, ProgressEvent) and event.reasoning_delta + ) + assert reasoning == "first paragraph\n\nsecond paragraph" + assert context.streamed_reasoning is True