Просмотр исходного кода

Store the file edit backup file in `/tmp` (#3958)

Xingyao Wang 1 год назад
Родитель
Сommit
3435f1e5d8
1 измененных файлов с 8 добавлено и 6 удалено
  1. 8 6
      openhands/runtime/plugins/agent_skills/file_ops/file_ops.py

+ 8 - 6
openhands/runtime/plugins/agent_skills/file_ops/file_ops.py

@@ -510,11 +510,11 @@ def _edit_file_impl(
         # NOTE: we need to get env var inside this function
         # because the env var will be set AFTER the agentskills is imported
         if enable_auto_lint:
-            # BACKUP the original file
-            original_file_backup_path = os.path.join(
-                os.path.dirname(file_name),
-                f'.backup.{os.path.basename(file_name)}',
-            )
+            # Generate a random temporary file path
+            suffix = os.path.splitext(file_name)[1]
+            with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tfile:
+                original_file_backup_path = tfile.name
+
             with open(original_file_backup_path, 'w') as f:
                 f.writelines(lines)
 
@@ -597,7 +597,9 @@ def _edit_file_impl(
                     file_name, 'w'
                 ) as fout:
                     fout.write(fin.read())
-                os.remove(original_file_backup_path)
+
+                # Don't forget to remove the temporary file after you're done
+                os.unlink(original_file_backup_path)
                 return ret_str
 
     except FileNotFoundError as e: