mcp_session_table.py
python
sha256:e6c07a0f17d2b30516cfe406c094c493c3aeaf8ea72db9ca098d24013bcc3bb9
Merge branch 'task/wave-hub-mcp-289' into dev
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """An in-memory stand-in for the ``musehub_mcp_sessions`` table, for the database-free MCP docs contracts (musehub#289). |
| 2 | |
| 3 | ``tests/docs_contract`` never reaches a database (musehub#273), but every Streamable HTTP session is now a row every |
| 4 | worker reads. These modules install :class:`SessionTable` with ``musehub.mcp.session.use_shared_store`` so the |
| 5 | transport runs unchanged; it implements the ``SharedSessionStore`` contract with the same expiry and touch rules as |
| 6 | ``musehub.mcp.session_store.PostgresSessionStore``, which ``tests/test_mcp_shared_sessions.py`` exercises on PostgreSQL. |
| 7 | """ |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | from collections.abc import Iterator |
| 12 | from contextlib import contextmanager |
| 13 | from dataclasses import replace |
| 14 | from datetime import datetime |
| 15 | |
| 16 | from musehub.mcp import session |
| 17 | from musehub.mcp.session import Principal, SessionRecord |
| 18 | |
| 19 | |
| 20 | class SessionTable: |
| 21 | """One dict shared by every simulated worker: clearing ``session._SESSIONS`` leaves it intact.""" |
| 22 | |
| 23 | def __init__(self) -> None: |
| 24 | self.rows: dict[str, SessionRecord] = {} |
| 25 | |
| 26 | async def insert(self, record: SessionRecord) -> None: |
| 27 | assert record.session_key not in self.rows |
| 28 | self.rows[record.session_key] = record |
| 29 | |
| 30 | async def lookup( |
| 31 | self, session_key: str, now: datetime, *, principal: Principal, touch_before: datetime, expires_at: datetime, |
| 32 | ) -> SessionRecord | None: |
| 33 | record = self.rows.get(session_key) |
| 34 | if record is None or record.principal != principal: |
| 35 | return None |
| 36 | if record.expires_at <= now: |
| 37 | del self.rows[session_key] |
| 38 | return None |
| 39 | if record.last_seen_at <= touch_before: |
| 40 | record = self.rows[session_key] = replace(record, last_seen_at=now, expires_at=expires_at) |
| 41 | return record |
| 42 | |
| 43 | async def set_focus(self, session_key: str, focus: tuple[str, str] | None) -> None: |
| 44 | if session_key in self.rows: |
| 45 | self.rows[session_key] = replace(self.rows[session_key], repo_focus=focus) |
| 46 | |
| 47 | async def delete(self, session_key: str, principal: Principal) -> bool: |
| 48 | record = self.rows.get(session_key) |
| 49 | if record is None or record.principal != principal: |
| 50 | return False |
| 51 | del self.rows[session_key] |
| 52 | return True |
| 53 | |
| 54 | async def live_keys(self, session_keys: list[str], now: datetime) -> set[str]: |
| 55 | return {k for k in session_keys if k in self.rows and self.rows[k].expires_at > now} |
| 56 | |
| 57 | async def sweep(self, now: datetime, limit: int) -> int: |
| 58 | expired = sorted((r.expires_at, k) for k, r in self.rows.items() if r.expires_at <= now)[:limit] |
| 59 | for _, k in expired: |
| 60 | del self.rows[k] |
| 61 | return len(expired) |
| 62 | |
| 63 | |
| 64 | @contextmanager |
| 65 | def installed() -> Iterator[SessionTable]: |
| 66 | """Serve sessions from a fresh :class:`SessionTable` for the duration of the block.""" |
| 67 | table = SessionTable() |
| 68 | previous = session.use_shared_store(table) |
| 69 | try: |
| 70 | yield table |
| 71 | finally: |
| 72 | session.use_shared_store(previous) |
File History
3 commits
sha256:e6c07a0f17d2b30516cfe406c094c493c3aeaf8ea72db9ca098d24013bcc3bb9
Merge branch 'task/wave-hub-mcp-289' into dev
Opus 5.5
minor
⚠
1 day ago
sha256:08c30c873257205e342a68f5d55803ff3c6dd6abd4747b5444c68e1da4b8da39
fix(mcp): bind sessions to their principal; honest cross-wo…
Opus 5.5
minor
⚠
1 day ago
sha256:5e59815d34816a1aa12717d0ac82362d6a9919e8b6e096e66ed49c47a028d76f
fix(mcp): share Streamable HTTP sessions across worker proc…
Opus 5.5
minor
⚠
1 day ago