From 6c4d7d1fa3297126923199a41a82af171b3bf7aa Mon Sep 17 00:00:00 2001 From: VascoSch92 Date: Sun, 8 Nov 2026 20:40:15 +0100 Subject: [PATCH] fix(sdk): keep an oversize write in the in-memory file store InMemoryFileStore keeps its only copy of the data in a MemoryLRUCache, which skips a value larger than max_memory. A write of such a value was dropped, and the next read raised FileNotFoundError. The store now keeps it and evicts everything older, as LRU eviction does for any other write. --- openhands-sdk/openhands/sdk/io/cache.py | 30 ++++++++++++++---------- openhands-sdk/openhands/sdk/io/memory.py | 4 +++- tests/sdk/io/test_memory_filestore.py | 10 ++++++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/openhands-sdk/openhands/sdk/io/cache.py b/openhands-sdk/openhands/sdk/io/cache.py index eada0a4b025..43672fb3f29 100644 --- a/openhands-sdk/openhands/sdk/io/cache.py +++ b/openhands-sdk/openhands/sdk/io/cache.py @@ -21,12 +21,22 @@ class MemoryLRUCache(LRUCache): For non-string values, sys.getsizeof is used as a rough approximation. """ - def __init__(self, max_memory: int, max_size: int, *args, **kwargs): + def __init__( + self, + max_memory: int, + max_size: int, + *args, + keep_oversize: bool = False, + **kwargs, + ): # Ensure minimum maxsize of 1 to avoid LRUCache issues maxsize = max(1, max_size) super().__init__(maxsize=maxsize, *args, **kwargs) self.max_memory = max_memory self.current_memory = 0 + # A store whose only copy lives here must keep an item larger than + # max_memory (evicting everything else) rather than drop the write. + self.keep_oversize = keep_oversize def _get_size(self, value: Any) -> int: """Calculate size of value for memory tracking. @@ -55,25 +65,21 @@ def _get_size(self, value: Any) -> int: def __setitem__(self, key: Any, value: Any) -> None: new_size = self._get_size(value) + # The key's previous entry is stale either way: dropping it first keeps + # a skipped write from serving what it replaced, and keeps the memory + # accounting right if eviction below reaches this key. + if key in self: + del self[key] + # Don't cache items that are larger than max_memory # This prevents cache thrashing where one huge item evicts everything - if new_size > self.max_memory: + if new_size > self.max_memory and not self.keep_oversize: logger.debug( f"Item too large for cache ({new_size} bytes > " f"{self.max_memory} bytes), skipping cache" ) - # The store now holds this value, so the key's previous entry is - # stale: dropping it makes a read fall through to the store - # instead of returning what the write replaced. - if key in self: - del self[key] return - # Update memory accounting if key exists - if key in self: - old_value = self[key] - self.current_memory -= self._get_size(old_value) - self.current_memory += new_size # Evict items until we're under memory limit diff --git a/openhands-sdk/openhands/sdk/io/memory.py b/openhands-sdk/openhands/sdk/io/memory.py index 92f6ed91547..912e9c789d1 100644 --- a/openhands-sdk/openhands/sdk/io/memory.py +++ b/openhands-sdk/openhands/sdk/io/memory.py @@ -28,7 +28,9 @@ def __init__( max_size: int = _DEFAULT_MAX_SIZE, max_memory: int = _DEFAULT_MAX_MEMORY, ) -> None: - self.files = MemoryLRUCache(max_memory=max_memory, max_size=max_size) + self.files = MemoryLRUCache( + max_memory=max_memory, max_size=max_size, keep_oversize=True + ) self._instance_id = uuid.uuid4().hex self._lock = threading.Lock() if files is not None: diff --git a/tests/sdk/io/test_memory_filestore.py b/tests/sdk/io/test_memory_filestore.py index 50859aa38da..ceb1186b573 100644 --- a/tests/sdk/io/test_memory_filestore.py +++ b/tests/sdk/io/test_memory_filestore.py @@ -16,3 +16,13 @@ def test_list_does_not_include_siblings_sharing_a_prefix(): store = InMemoryFileStore({"a/b": "1", "ab/c": "2"}) assert store.list("a") == ["a/b"] + + +def test_oversize_write_is_not_silently_dropped(): + store = InMemoryFileStore(max_memory=16) + store.write("state.json", "old") + new_content = "x" * 32 + + store.write("state.json", new_content) + + assert store.read("state.json") == new_content