From 0e2ea122eb7b596d1e9d02680089bfda46140978 Mon Sep 17 00:00:00 2001 From: Patrick Buckley Date: Wed, 6 May 2026 22:14:50 -0700 Subject: [PATCH] fix(memory): wire limit kwarg through load_messages Closes round-1 review finding perf-2 (minor). Storage backends accept ``*, limit: int | None = None`` (see :meth:`StorageBackend.load_messages` at storage/_protocol.py:146) but the in-memory wrapper at memory.py:82-85 dropped the kwarg, so callers that wanted to tail-load (e.g. ``session.resume`` against a long-running coord with hundreds of wake rows + persisted reminder JSON) were forced to pull every row through the wrapper anyway. Wraparound is mechanical: signature widens, default leaves existing callers unaffected. (cherry picked from commit 885f6a9185407b00ff9ab17fb73aacff1e663b83) --- turnstone/core/memory.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/turnstone/core/memory.py b/turnstone/core/memory.py index 8c7bd6c2..99e43fc6 100644 --- a/turnstone/core/memory.py +++ b/turnstone/core/memory.py @@ -79,10 +79,15 @@ def save_messages_bulk(rows: list[dict[str, Any]]) -> None: log.warning("Failed to bulk-save %d messages", len(rows), exc_info=True) -def load_messages(ws_id: str) -> list[dict[str, Any]]: - """Load messages for a workstream and reconstruct OpenAI message format.""" +def load_messages(ws_id: str, *, limit: int | None = None) -> list[dict[str, Any]]: + """Load messages for a workstream and reconstruct OpenAI message format. + + *limit* tail-loads the most recent N rows when set; the storage + backend already accepts the kwarg. Used by callers that don't + need the full transcript (e.g. browsing a long-running coord). + """ try: - return get_storage().load_messages(ws_id) + return get_storage().load_messages(ws_id, limit=limit) except Exception: log.warning("Failed to load messages for ws=%s", ws_id, exc_info=True) return []