Skip to content

πŸ™Œ OpenHands: AI-Driven Development

perf: make EventStore cur_id a lazy calculated property (#9544)

Tim O'Farrell229f35093dcc
diff --git a/openhands/events/event_store.py b/openhands/events/event_store.py
index cbed3515f8..fccde45d40 100644
--- a/openhands/events/event_store.py
+++ b/openhands/events/event_store.py
@@ -49,12 +49,23 @@ class EventStore(EventStoreABC):
sid: str
file_store: FileStore
user_id: str | None
- cur_id: int = -1 # We fix this in post init if it is not specified
cache_size: int = 25
-
- def __post_init__(self) -> None:
- if self.cur_id >= 0:
- return
+ _cur_id: int | None = None # Private field to cache the calculated value
+
+ @property
+ def cur_id(self) -> int:
+ """Lazy calculated property for the current event ID."""
+ if self._cur_id is None:
+ self._cur_id = self._calculate_cur_id()
+ return self._cur_id
+
+ @cur_id.setter
+ def cur_id(self, value: int) -> None:
+ """Setter for cur_id to allow updates."""
+ self._cur_id = value
+
+ def _calculate_cur_id(self) -> int:
+ """Calculate the current event ID based on file system content."""
events = []
try:
events_dir = get_conversation_events_dir(self.sid, self.user_id)
@@ -63,14 +74,15 @@ class EventStore(EventStoreABC):
logger.debug(f'No events found for session {self.sid} at {events_dir}')
if not events:
- self.cur_id = 0
- return
+ return 0
# if we have events, we need to find the highest id to prepare for new events
+ max_id = -1
for event_str in events:
id = self._get_id_from_filename(event_str)
- if id >= self.cur_id:
- self.cur_id = id + 1
+ if id >= max_id:
+ max_id = id
+ return max_id + 1
def search_events(
self,