@@ -579,16 +579,20 @@ async def test_stream_session_events_returns_204_when_no_turn_started() -> None:
579579async def test_stream_session_events_attaches_when_turn_in_progress_no_events_yet () -> (
580580 None
581581):
582+ session_id = uuid .uuid4 ()
583+ curr_run_id = uuid .uuid4 ()
582584 stream = _fake_stream ()
583585 response = await _run_stream_endpoint (
584- session = _fake_stream_session (status_value = "running" ),
586+ session = _fake_stream_session (status_value = "running" , curr_run_id = curr_run_id ),
585587 stream = stream ,
586588 headers = {},
589+ session_id = session_id ,
587590 )
588591
589592 assert isinstance (response , StreamingResponse )
590593 stream .sse .assert_called_once ()
591594 assert stream .sse .call_args .kwargs ["last_id" ] == "0-0"
595+ assert stream .sse .call_args .kwargs ["message_id" ] == f"{ session_id } :{ curr_run_id } "
592596
593597
594598@pytest .mark .anyio
@@ -609,9 +613,10 @@ async def test_stream_session_events_waiting_without_cursor_uses_db_history() ->
609613@pytest .mark .anyio
610614async def test_stream_session_events_resumes_after_last_event_id () -> None :
611615 """A live cursor newer than the buffer min resumes after it (composite id)."""
616+ curr_run_id = uuid .uuid4 ()
612617 stream = _fake_stream (min_id = "1000-0" )
613618 response = await _run_stream_endpoint (
614- session = _fake_stream_session (status_value = "running" ),
619+ session = _fake_stream_session (status_value = "running" , curr_run_id = curr_run_id ),
615620 stream = stream ,
616621 headers = {"Last-Event-ID" : "1234-0:2" },
617622 )
@@ -647,9 +652,10 @@ async def test_stream_session_events_terminal_reconnect_uses_latest_run_id() ->
647652@pytest .mark .anyio
648653async def test_stream_session_events_stale_cursor_running_replays_from_start () -> None :
649654 """Cursor older than the buffer min, still running -> replay from 0-0."""
655+ curr_run_id = uuid .uuid4 ()
650656 stream = _fake_stream (min_id = "2000-0" )
651657 response = await _run_stream_endpoint (
652- session = _fake_stream_session (status_value = "running" ),
658+ session = _fake_stream_session (status_value = "running" , curr_run_id = curr_run_id ),
653659 stream = stream ,
654660 headers = {"Last-Event-ID" : "1000-0:0" },
655661 )
@@ -672,4 +678,22 @@ async def test_stream_session_events_stale_cursor_terminal_finishes() -> None:
672678
673679 assert isinstance (response , StreamingResponse )
674680 stream .finished_sse .assert_called_once ()
681+ assert stream .finished_sse .call_args .kwargs ["message_id" ] is None
682+ stream .sse .assert_not_called ()
683+
684+
685+ @pytest .mark .anyio
686+ async def test_stream_session_events_terminal_without_run_id_finishes () -> None :
687+ """Terminal reconnects without a run id do not synthesize a new bubble id."""
688+ stream = _fake_stream (min_id = "1000-0" )
689+ response = await _run_stream_endpoint (
690+ session = _fake_stream_session (status_value = "stopped" ),
691+ stream = stream ,
692+ headers = {"Last-Event-ID" : "1234-0:2" },
693+ latest_run_id = None ,
694+ )
695+
696+ assert isinstance (response , StreamingResponse )
697+ stream .finished_sse .assert_called_once ()
698+ assert stream .finished_sse .call_args .kwargs ["message_id" ] is None
675699 stream .sse .assert_not_called ()
0 commit comments