Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(chat): flush citation carry on termination (#6401)

SivaSubramaniyen0196c629910d
diff --git a/changelog.d/6401.bugfix.md b/changelog.d/6401.bugfix.md
new file mode 100644
index 0000000000..14d003f035
--- /dev/null
+++ b/changelog.d/6401.bugfix.md
@@ -0,0 +1 @@
+Restore the buffered citation suffix when stopping a chat response mid-stream.
diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.py
index 581bcd6325..8506d60eaf 100644
--- a/src/local_deep_research/web/services/research_service.py
+++ b/src/local_deep_research/web/services/research_service.py
@@ -897,8 +897,27 @@ def _save_partial_chat_message_on_terminate(
logger.opt(exception=True).warning(
"Failed to persist partial chat message on terminate"
)
-
try:
+ flush = streaming_state.get("_flush_carry") if streaming_state else None
+ if flush:
+ leftover = flush()
+ if leftover:
+ try:
+ _socket_emitter.emit_to_subscribers(
+ "response_chunk",
+ research_id,
+ {
+ "chunk": leftover,
+ "is_streaming": True,
+ "is_final": False,
+ },
+ owner=username,
+ )
+ except Exception:
+ logger.debug(
+ "Carry-buffer flush emit on terminate failed (non-critical)"
+ )
+
_socket_emitter.emit_to_subscribers(
"response_chunk",
research_id,
diff --git a/tests/chat/test_chat_termination.py b/tests/chat/test_chat_termination.py
index 88a22c8fc5..e2e579e015 100644
--- a/tests/chat/test_chat_termination.py
+++ b/tests/chat/test_chat_termination.py
@@ -73,6 +73,85 @@ class TestPartialPersistOnTerminate:
payload = event_args[2]
assert payload["is_final"] is True
+ def test_flushes_citation_carry_on_termination(self):
+ from local_deep_research.web.services.research_service import (
+ _make_chat_stream_callback,
+ )
+
+ streaming_state = {
+ "chunks_sent": 0,
+ "chunks": [],
+ "_bytes": 0,
+ "_truncated": False,
+ }
+
+ sock = MagicMock()
+ formatter = MagicMock()
+ formatter.apply_inline_hyperlinks.side_effect = lambda text, sources: (
+ text
+ )
+
+ callback = _make_chat_stream_callback(
+ RESEARCH_ID,
+ streaming_state,
+ sock,
+ USERNAME,
+ source_resolver=lambda: [{"url": "https://example.com"}],
+ formatter=formatter,
+ )
+
+ with patch(
+ "local_deep_research.web.routes.globals.is_termination_requested",
+ return_value=False,
+ ):
+ callback("Answer before [12")
+
+ # The raw chunk is retained for termination persistence.
+ assert streaming_state["chunks"] == ["Answer before [12"]
+
+ # The incomplete citation is withheld from the live stream.
+ emitted_chunks = [
+ call.args[2]["chunk"]
+ for call in sock.emit_to_subscribers.call_args_list
+ if call.args and call.args[0] == "response_chunk"
+ ]
+ assert "Answer before " in "".join(emitted_chunks)
+ assert "[12" not in "".join(emitted_chunks)
+
+ helper, _marker, footer = _import_helper()
+
+ with (
+ patch("local_deep_research.chat.service.ChatService") as mock_svc,
+ patch(
+ "local_deep_research.web.services.research_service._socket_emitter",
+ sock,
+ ),
+ ):
+ instance = MagicMock()
+ mock_svc.return_value = instance
+
+ helper(
+ SESSION_ID,
+ RESEARCH_ID,
+ USERNAME,
+ "".join(streaming_state["chunks"]),
+ streaming_state=streaming_state,
+ )
+
+ content = instance.add_message.call_args.kwargs["content"]
+ assert content == "Answer before [12" + footer
+ assert footer.strip() in content
+
+ events = [
+ call.args[2]
+ for call in sock.emit_to_subscribers.call_args_list
+ if call.args and call.args[0] == "response_chunk"
+ ]
+ assert events[-2]["chunk"] == "[12"
+ assert events[-2]["is_final"] is False
+ assert events[-1]["chunk"] == ""
+ assert events[-1]["is_final"] is True
+
def test_persists_marker_when_no_chunks_streamed(self):
helper, marker, _footer = _import_helper()