1111from rich .prompt import Confirm
1212
1313from codeflash .cli_cmds .console import console
14+ from codeflash .code_utils .compat import codeflash_temp_dir
1415
1516if TYPE_CHECKING :
1617 import argparse
1718
1819
1920class CodeflashRunCheckpoint :
20- def __init__ (self , module_root : Path , checkpoint_dir : Path = Path ("/tmp" )) -> None : # noqa: S108
21+ def __init__ (self , module_root : Path , checkpoint_dir : Path | None = None ) -> None :
22+ if checkpoint_dir is None :
23+ checkpoint_dir = codeflash_temp_dir
2124 self .module_root = module_root
2225 self .checkpoint_dir = Path (checkpoint_dir )
2326 # Create a unique checkpoint file name
@@ -37,7 +40,7 @@ def _initialize_checkpoint_file(self) -> None:
3740 "last_updated" : time .time (),
3841 }
3942
40- with self .checkpoint_path .open ("w" ) as f :
43+ with self .checkpoint_path .open ("w" , encoding = "utf-8" ) as f :
4144 f .write (json .dumps (metadata ) + "\n " )
4245
4346 def add_function_to_checkpoint (
@@ -66,7 +69,7 @@ def add_function_to_checkpoint(
6669 ** additional_info ,
6770 }
6871
69- with self .checkpoint_path .open ("a" ) as f :
72+ with self .checkpoint_path .open ("a" , encoding = "utf-8" ) as f :
7073 f .write (json .dumps (function_data ) + "\n " )
7174
7275 # Update the metadata last_updated timestamp
@@ -75,7 +78,7 @@ def add_function_to_checkpoint(
7578 def _update_metadata_timestamp (self ) -> None :
7679 """Update the last_updated timestamp in the metadata."""
7780 # Read the first line (metadata)
78- with self .checkpoint_path .open () as f :
81+ with self .checkpoint_path .open (encoding = "utf-8" ) as f :
7982 metadata = json .loads (f .readline ())
8083 rest_content = f .read ()
8184
@@ -84,7 +87,7 @@ def _update_metadata_timestamp(self) -> None:
8487
8588 # Write all lines to a temporary file
8689
87- with self .checkpoint_path .open ("w" ) as f :
90+ with self .checkpoint_path .open ("w" , encoding = "utf-8" ) as f :
8891 f .write (json .dumps (metadata ) + "\n " )
8992 f .write (rest_content )
9093
@@ -94,7 +97,7 @@ def cleanup(self) -> None:
9497 self .checkpoint_path .unlink (missing_ok = True )
9598
9699 for file in self .checkpoint_dir .glob ("codeflash_checkpoint_*.jsonl" ):
97- with file .open () as f :
100+ with file .open (encoding = "utf-8" ) as f :
98101 # Skip the first line (metadata)
99102 first_line = next (f )
100103 metadata = json .loads (first_line )
@@ -116,7 +119,7 @@ def get_all_historical_functions(module_root: Path, checkpoint_dir: Path) -> dic
116119 to_delete = []
117120
118121 for file in checkpoint_dir .glob ("codeflash_checkpoint_*.jsonl" ):
119- with file .open () as f :
122+ with file .open (encoding = "utf-8" ) as f :
120123 # Skip the first line (metadata)
121124 first_line = next (f )
122125 metadata = json .loads (first_line )
@@ -139,8 +142,8 @@ def get_all_historical_functions(module_root: Path, checkpoint_dir: Path) -> dic
139142
140143def ask_should_use_checkpoint_get_functions (args : argparse .Namespace ) -> Optional [dict [str , dict [str , str ]]]:
141144 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
145+ if args .all and codeflash_temp_dir . is_dir ():
146+ previous_checkpoint_functions = get_all_historical_functions (args .module_root , codeflash_temp_dir )
144147 if previous_checkpoint_functions and Confirm .ask (
145148 "Previous Checkpoint detected from an incomplete optimization run, shall I continue the optimization from that point?" ,
146149 default = True ,
0 commit comments