From f4a7ad16aad17a4e9d811cb4ce770f7b6565017d Mon Sep 17 00:00:00 2001 From: Jiajun Xie Date: Thu, 16 Apr 2026 09:19:08 +0800 Subject: [PATCH] fix(memory): handle missing cursor key in history entries - Use .get('cursor') instead of direct dict access to prevent KeyError - Skip entries without cursor and log a warning - Fix _next_cursor fallback to safely check for cursor existence Fixes #3190 --- nanobot/agent/memory.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/nanobot/agent/memory.py b/nanobot/agent/memory.py index 3f8b24314..bed1b6d3e 100644 --- a/nanobot/agent/memory.py +++ b/nanobot/agent/memory.py @@ -239,13 +239,21 @@ class MemoryStore: pass # Fallback: read last line's cursor from the JSONL file. last = self._read_last_entry() - if last: + if last and last.get("cursor") is not None: return last["cursor"] + 1 return 1 def read_unprocessed_history(self, since_cursor: int) -> list[dict[str, Any]]: """Return history entries with cursor > *since_cursor*.""" - return [e for e in self._read_entries() if e["cursor"] > since_cursor] + entries = [] + for e in self._read_entries(): + cursor = e.get("cursor") + if cursor is None: + logger.warning("Skipping history entry without cursor: {}", e.get("timestamp", "unknown")) + continue + if cursor > since_cursor: + entries.append(e) + return entries def compact_history(self) -> None: """Drop oldest entries if the file exceeds *max_history_entries*."""