Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions openhands-sdk/openhands/sdk/io/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion openhands-sdk/openhands/sdk/io/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/sdk/io/test_memory_filestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading