fix: prevent duplicate LLM stream snapshots

This commit is contained in:
leefer
2026-07-29 19:26:01 +08:00
parent 93fc4505a9
commit 02af8488b3
6 changed files with 137 additions and 7 deletions
+35
View File
@@ -0,0 +1,35 @@
from __future__ import annotations
import unittest
from llm_stream import OpenAIStreamAccumulator
class OpenAIStreamAccumulatorTests(unittest.TestCase):
def test_repeated_delta_chunks_are_preserved_as_model_output(self) -> None:
accumulator = OpenAIStreamAccumulator()
self.assertEqual(accumulator.feed({"delta": {"content": "yes"}}), "yes")
self.assertEqual(accumulator.feed({"delta": {"content": "yes"}}), "yes")
self.assertEqual(accumulator.text, "yesyes")
def test_final_snapshot_can_add_a_missing_suffix(self) -> None:
accumulator = OpenAIStreamAccumulator()
self.assertEqual(accumulator.feed({"delta": {"content": "first"}}), "first")
self.assertEqual(
accumulator.feed({"message": {"content": "first second"}}),
" second",
)
self.assertEqual(accumulator.text, "first second")
def test_incompatible_final_snapshot_is_not_appended_twice(self) -> None:
accumulator = OpenAIStreamAccumulator()
accumulator.feed({"delta": {"content": "streamed answer"}})
self.assertEqual(
accumulator.feed({"message": {"content": "rewritten final answer"}}),
"",
)
self.assertEqual(accumulator.text, "streamed answer")
if __name__ == "__main__":
unittest.main()
+37
View File
@@ -66,6 +66,43 @@ class MentorStreamTests(unittest.TestCase):
)
self.assertEqual(result["answer"], "first second")
def test_final_full_message_does_not_duplicate_streamed_deltas(self):
lines = [
b'data: {"choices":[{"delta":{"content":"first"}}]}\n',
b'data: {"choices":[{"delta":{"content":" second"}}]}\n',
b'data: {"choices":[{"message":{"content":"first second"}}]}\n',
b"data: [DONE]\n",
]
with patch(
"mentor_agent.urllib.request.urlopen",
return_value=FakeStreamResponse(lines),
):
chunks = list(
stream_with_mentor(
self.skill, {}, "question", [], "key",
"https://example.test/v1", "model",
)
)
self.assertEqual(chunks, ["first", " second"])
def test_snapshot_only_stream_emits_only_new_suffix(self):
lines = [
b'data: {"choices":[{"message":{"content":"first"}}]}\n',
b'data: {"choices":[{"message":{"content":"first second"}}]}\n',
b"data: [DONE]\n",
]
with patch(
"mentor_agent.urllib.request.urlopen",
return_value=FakeStreamResponse(lines),
):
chunks = list(
stream_with_mentor(
self.skill, {}, "question", [], "key",
"https://example.test/v1", "model",
)
)
self.assertEqual(chunks, ["first", " second"])
if __name__ == "__main__":
unittest.main()
+17
View File
@@ -45,6 +45,23 @@ class ReviewAssistantStreamingTests(unittest.TestCase):
stream_review_assistant({}, "question", [], "key", "https://example.test/v1", "model")
)
def test_final_full_message_does_not_duplicate_streamed_deltas(self):
response = StreamingResponse(
[
b'data: {"choices":[{"delta":{"content":"first"}}]}\n',
b'data: {"choices":[{"delta":{"content":" second"}}]}\n',
b'data: {"choices":[{"message":{"content":"first second"}}]}\n',
b"data: [DONE]\n",
]
)
with patch("assistant_agent.urllib.request.urlopen", return_value=response):
chunks = list(
stream_review_assistant(
{}, "question", [], "key", "https://example.test/v1", "model"
)
)
self.assertEqual(chunks, ["first", " second"])
def test_ndjson_event_writer_flushes_complete_line(self):
handler = RequestHandler.__new__(RequestHandler)
handler.wfile = io.BytesIO()