mirror of
https://github.com/openswarm-ai/openswarm.git
synced 2026-09-08 10:47:44 +02:00
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C9zwUaHucUgrdxvK8FvjYT
38 lines
2.1 KiB
Python
38 lines
2.1 KiB
Python
"""A session socket that connects mid-reply gets the text so far, once, and nothing when there is none."""
|
|
|
|
import pathlib
|
|
|
|
from backend.apps.agents.core.stream_snapshot import stream_snapshot_payload
|
|
from backend.apps.agents.manager.streaming.PartialReply import PartialReply
|
|
|
|
|
|
def test_mid_reply_connect_gets_the_accumulated_text():
|
|
live = {"s1": PartialReply(msg_id="m1", text="The Eiffel Tower is a wrought-iron", branch_id="main")}
|
|
assert stream_snapshot_payload("s1", live) == {
|
|
"session_id": "s1", "message_id": "m1", "role": "assistant", "text": "The Eiffel Tower is a wrought-iron",
|
|
}
|
|
|
|
|
|
def test_no_reply_in_flight_means_no_snapshot():
|
|
assert stream_snapshot_payload("s1", {}) is None
|
|
assert stream_snapshot_payload("s1", {"s1": PartialReply(msg_id="m1", text="", branch_id="main")}) is None
|
|
assert stream_snapshot_payload("s1", {"other": PartialReply(msg_id="m1", text="x", branch_id="main")}) is None
|
|
|
|
|
|
def test_the_snapshot_is_sent_after_the_hello_ack_not_before():
|
|
"""The client drops every stream frame that arrives before its resume ack, so a snapshot sent
|
|
earlier would be dropped exactly like the replayed deltas it exists to replace."""
|
|
src = pathlib.Path("backend/main.py").read_text()
|
|
hello = src.index('"event": "server:hello"')
|
|
snapshot = src.index('"event": "agent:stream_snapshot"')
|
|
assert hello < snapshot
|
|
handler = src[src.index('if event == "client:hello":'):src.index('elif event == "client:ping":')]
|
|
assert "stream_snapshot_payload(session_id, p_am.live_partial, p_am.live_thinking)" in handler
|
|
|
|
|
|
def test_a_socket_that_connects_mid_thought_gets_the_thinking_so_far_and_the_answer_wins_once_it_flows():
|
|
thinking = {"s1": PartialReply(msg_id="t1", text="Considering the four legs", branch_id="main")}
|
|
assert stream_snapshot_payload("s1", {}, thinking) == {"session_id": "s1", "message_id": "t1", "role": "thinking", "text": "Considering the four legs"}
|
|
text = {"s1": PartialReply(msg_id="m1", text="The Eiffel", branch_id="main")}
|
|
assert stream_snapshot_payload("s1", text, thinking)["role"] == "assistant"
|