test_mcp_sse_event_ids.py
python
sha256:d7af2b937831db3288db5589530c6c2f8e90f0c2c4efbbeb5dd0577076168b29
fix(mcp): SSE event ids + Last-Event-ID replay (#290); per-…
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """The MCP push channel carries event IDs and replays after ``Last-Event-ID`` (musehub#290). |
| 2 | |
| 3 | * every pushed frame starts with an ``id:`` line; IDs increase per session and are unique to this worker process; |
| 4 | * a reconnect with the last ID seen replays exactly the later buffered events, in order; |
| 5 | * an ID older than the buffer (evicted) replays the whole buffer; an unknown or foreign ID replays nothing; |
| 6 | * the ring buffer keeps the 50 most recent events. |
| 7 | |
| 8 | In-process, database-free (real ``session`` and ``context`` modules). |
| 9 | """ |
| 10 | |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import asyncio |
| 14 | |
| 15 | import pytest |
| 16 | |
| 17 | from musehub.mcp import context, session |
| 18 | from musehub.mcp.sse import sse_notification |
| 19 | |
| 20 | pytestmark = pytest.mark.no_db |
| 21 | |
| 22 | |
| 23 | def _id(frame: str) -> str: |
| 24 | first = frame.split("\n", 1)[0] |
| 25 | assert first.startswith("id: "), frame |
| 26 | return first[len("id: "):] |
| 27 | |
| 28 | |
| 29 | async def _replay(sess: session.MCPSession, last_event_id: str | None) -> list[str]: |
| 30 | out: list[str] = [] |
| 31 | agen = session.register_sse_queue(sess, last_event_id) |
| 32 | try: |
| 33 | while True: |
| 34 | out.append(await asyncio.wait_for(agen.__anext__(), 0.1)) |
| 35 | except (asyncio.TimeoutError, StopAsyncIteration): |
| 36 | pass |
| 37 | finally: |
| 38 | await agen.aclose() |
| 39 | return out |
| 40 | |
| 41 | |
| 42 | def _push(sess: session.MCPSession, n: int) -> None: |
| 43 | for i in range(n): |
| 44 | session.push_to_session(sess, sse_notification("notifications/message", {"data": i})) |
| 45 | |
| 46 | |
| 47 | @pytest.mark.asyncio |
| 48 | async def test_live_frames_carry_increasing_ids() -> None: |
| 49 | sess = session.create_session("u", {}) |
| 50 | q: asyncio.Queue[str | None] = asyncio.Queue() |
| 51 | sess.sse_queues.append(q) |
| 52 | for i in range(3): |
| 53 | await context.ToolCallContext("u", sess, progress_token="t").progress(i + 1, 3) |
| 54 | frames = [q.get_nowait() or "" for _ in range(3)] |
| 55 | ids = [_id(f) for f in frames] |
| 56 | assert len(set(ids)) == 3 |
| 57 | seqs = [int(i.rsplit("-", 1)[1]) for i in ids] |
| 58 | assert seqs == sorted(seqs) and all('"progressToken":"t"' in f and "\ndata: " in f for f in frames) |
| 59 | assert [eid for eid, _ in sess.event_buffer] == ids |
| 60 | session.delete_session(sess.session_id) |
| 61 | |
| 62 | |
| 63 | @pytest.mark.asyncio |
| 64 | async def test_reconnect_replays_exactly_the_later_events_in_order() -> None: |
| 65 | sess = session.create_session("u", {}) |
| 66 | _push(sess, 5) |
| 67 | ids = [eid for eid, _ in sess.event_buffer] |
| 68 | replayed = await _replay(sess, ids[1]) |
| 69 | assert [_id(f) for f in replayed] == ids[2:] |
| 70 | assert await _replay(sess, ids[-1]) == [] |
| 71 | session.delete_session(sess.session_id) |
| 72 | |
| 73 | |
| 74 | @pytest.mark.asyncio |
| 75 | async def test_unknown_or_foreign_ids_replay_nothing_and_evicted_ids_replay_the_buffer() -> None: |
| 76 | sess = session.create_session("u", {}) |
| 77 | _push(sess, session._SSE_BUFFER_SIZE + 10) |
| 78 | assert len(sess.event_buffer) == session._SSE_BUFFER_SIZE |
| 79 | first_kept = sess.event_buffer[0][0] |
| 80 | tag, seq = first_kept.rsplit("-", 1) |
| 81 | assert len(await _replay(sess, f"{tag}-{int(seq) - 5}")) == session._SSE_BUFFER_SIZE # evicted: all later |
| 82 | assert await _replay(sess, "1") == [] |
| 83 | assert await _replay(sess, "nope") == [] |
| 84 | assert await _replay(sess, f"other{tag}-{int(seq) + 3}") == [] # another worker's ID |
| 85 | assert await _replay(sess, None) == [] # no header: no replay |
| 86 | session.delete_session(sess.session_id) |
| 87 | |
| 88 | |
| 89 | @pytest.mark.asyncio |
| 90 | async def test_sessions_number_their_events_independently() -> None: |
| 91 | a, b = session.create_session("u", {}), session.create_session("u", {}) |
| 92 | _push(a, 3) |
| 93 | _push(b, 1) |
| 94 | assert a.event_buffer[0][0] == b.event_buffer[0][0] # each session counts from its own start |
| 95 | assert a.event_buffer[0][0] != a.event_buffer[1][0] |
| 96 | session.delete_session(a.session_id) |
| 97 | session.delete_session(b.session_id) |
File History
1 commit
sha256:d7af2b937831db3288db5589530c6c2f8e90f0c2c4efbbeb5dd0577076168b29
fix(mcp): SSE event ids + Last-Event-ID replay (#290); per-…
Opus 5.5
minor
⚠
1 day ago