Skip to content

Commit 9852b34

Browse files
Fix: Check for existing files before saving recording events
When saving events to numbered JSON files, check if the file already exists and increment the counter until an unused filename is found. This handles cases where files already exist from previous recordings in the same directory. Co-authored-by: openhands <[email protected]>
1 parent bba1480 commit 9852b34

File tree

1 file changed

+12
-3
lines changed
  • openhands-tools/openhands/tools/browser_use

1 file changed

+12
-3
lines changed

openhands-tools/openhands/tools/browser_use/server.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ async def _inject_scripts_to_session(self) -> None:
224224
def _save_events_to_file(self, events: list[dict]) -> str | None:
225225
"""Save events to a numbered JSON file.
226226
227+
Finds the next available filename by incrementing the counter until
228+
an unused filename is found. This handles cases where files already
229+
exist from previous recordings.
230+
227231
Args:
228232
events: List of rrweb events to save.
229233
@@ -237,9 +241,14 @@ def _save_events_to_file(self, events: list[dict]) -> str | None:
237241
return None
238242

239243
os.makedirs(self._recording_save_dir, exist_ok=True)
240-
self._recording_file_counter += 1
241-
filename = f"{self._recording_file_counter}.json"
242-
filepath = os.path.join(self._recording_save_dir, filename)
244+
245+
# Find the next available filename
246+
while True:
247+
self._recording_file_counter += 1
248+
filename = f"{self._recording_file_counter}.json"
249+
filepath = os.path.join(self._recording_save_dir, filename)
250+
if not os.path.exists(filepath):
251+
break
243252

244253
with open(filepath, "w") as f:
245254
json.dump(events, f)

0 commit comments

Comments
 (0)