22
33import datetime
44import json
5- import sys
65import time
76import uuid
87from pathlib import Path
1110from rich .prompt import Confirm
1211
1312from codeflash .cli_cmds .console import console
13+ from codeflash .code_utils .compat import codeflash_temp_dir
1414
1515if TYPE_CHECKING :
1616 import argparse
1717
1818
1919class CodeflashRunCheckpoint :
20- def __init__ (self , module_root : Path , checkpoint_dir : Path = Path ("/tmp" )) -> None : # noqa: S108
20+ def __init__ (self , module_root : Path , checkpoint_dir : Path | None = None ) -> None :
21+ if checkpoint_dir is None :
22+ checkpoint_dir = codeflash_temp_dir
2123 self .module_root = module_root
2224 self .checkpoint_dir = Path (checkpoint_dir )
2325 # Create a unique checkpoint file name
@@ -37,7 +39,7 @@ def _initialize_checkpoint_file(self) -> None:
3739 "last_updated" : time .time (),
3840 }
3941
40- with self .checkpoint_path .open ("w" ) as f :
42+ with self .checkpoint_path .open ("w" , encoding = "utf-8" ) as f :
4143 f .write (json .dumps (metadata ) + "\n " )
4244
4345 def add_function_to_checkpoint (
@@ -66,7 +68,7 @@ def add_function_to_checkpoint(
6668 ** additional_info ,
6769 }
6870
69- with self .checkpoint_path .open ("a" ) as f :
71+ with self .checkpoint_path .open ("a" , encoding = "utf-8" ) as f :
7072 f .write (json .dumps (function_data ) + "\n " )
7173
7274 # Update the metadata last_updated timestamp
@@ -75,7 +77,7 @@ def add_function_to_checkpoint(
7577 def _update_metadata_timestamp (self ) -> None :
7678 """Update the last_updated timestamp in the metadata."""
7779 # Read the first line (metadata)
78- with self .checkpoint_path .open () as f :
80+ with self .checkpoint_path .open (encoding = "utf-8" ) as f :
7981 metadata = json .loads (f .readline ())
8082 rest_content = f .read ()
8183
@@ -84,7 +86,7 @@ def _update_metadata_timestamp(self) -> None:
8486
8587 # Write all lines to a temporary file
8688
87- with self .checkpoint_path .open ("w" ) as f :
89+ with self .checkpoint_path .open ("w" , encoding = "utf-8" ) as f :
8890 f .write (json .dumps (metadata ) + "\n " )
8991 f .write (rest_content )
9092
@@ -94,7 +96,7 @@ def cleanup(self) -> None:
9496 self .checkpoint_path .unlink (missing_ok = True )
9597
9698 for file in self .checkpoint_dir .glob ("codeflash_checkpoint_*.jsonl" ):
97- with file .open () as f :
99+ with file .open (encoding = "utf-8" ) as f :
98100 # Skip the first line (metadata)
99101 first_line = next (f )
100102 metadata = json .loads (first_line )
@@ -116,7 +118,7 @@ def get_all_historical_functions(module_root: Path, checkpoint_dir: Path) -> dic
116118 to_delete = []
117119
118120 for file in checkpoint_dir .glob ("codeflash_checkpoint_*.jsonl" ):
119- with file .open () as f :
121+ with file .open (encoding = "utf-8" ) as f :
120122 # Skip the first line (metadata)
121123 first_line = next (f )
122124 metadata = json .loads (first_line )
@@ -139,8 +141,8 @@ def get_all_historical_functions(module_root: Path, checkpoint_dir: Path) -> dic
139141
140142def ask_should_use_checkpoint_get_functions (args : argparse .Namespace ) -> Optional [dict [str , dict [str , str ]]]:
141143 previous_checkpoint_functions = None
142- if args .all and ( sys . platform == "linux" or sys . platform == "darwin" ) and Path ( "/tmp" ). is_dir (): # noqa: S108 #TODO: use the temp dir from codeutils-compat.py
143- previous_checkpoint_functions = get_all_historical_functions (args .module_root , Path ( "/tmp" )) # noqa: S108
144+ if args .all and codeflash_temp_dir . is_dir ():
145+ previous_checkpoint_functions = get_all_historical_functions (args .module_root , codeflash_temp_dir )
144146 if previous_checkpoint_functions and Confirm .ask (
145147 "Previous Checkpoint detected from an incomplete optimization run, shall I continue the optimization from that point?" ,
146148 default = True ,
0 commit comments