1212from __future__ import annotations
1313
1414import contextlib
15+ import datetime
1516import importlib .machinery
1617import io
1718import json
@@ -81,6 +82,7 @@ def __init__(
8182 config_file_path : Path | None = None ,
8283 max_function_count : int = 256 ,
8384 timeout : int | None = None , # seconds
85+ command : str | None = None ,
8486 ) -> None :
8587 """Use this class to trace function calls.
8688
@@ -91,6 +93,7 @@ def __init__(
9193 :param max_function_count: Maximum number of times to trace one function
9294 :param timeout: Timeout in seconds for the tracer, if the traced code takes more than this time, then tracing
9395 stops and normal execution continues. If this is None then no timeout applies
96+ :param command: The command that initiated the tracing (for metadata storage)
9497 """
9598 if functions is None :
9699 functions = []
@@ -148,6 +151,9 @@ def __init__(
148151 assert "test_framework" in self .config , "Please specify 'test-framework' in pyproject.toml config file"
149152 self .t = self .timer ()
150153
154+ # Store command information for metadata table
155+ self .command = command if command else " " .join (sys .argv )
156+
151157 def __enter__ (self ) -> None :
152158 if self .disable :
153159 return
@@ -174,6 +180,22 @@ def __enter__(self) -> None:
174180 "CREATE TABLE function_calls(type TEXT, function TEXT, classname TEXT, filename TEXT, "
175181 "line_number INTEGER, last_frame_address INTEGER, time_ns INTEGER, args BLOB)"
176182 )
183+
184+ # Create metadata table to store command information
185+ cur .execute ("CREATE TABLE metadata(key TEXT PRIMARY KEY, value TEXT)" )
186+
187+ # Store command metadata
188+ cur .execute ("INSERT INTO metadata VALUES (?, ?)" , ("command" , self .command ))
189+ cur .execute ("INSERT INTO metadata VALUES (?, ?)" , ("program_name" , self .file_being_called_from ))
190+ cur .execute (
191+ "INSERT INTO metadata VALUES (?, ?)" ,
192+ ("functions_filter" , json .dumps (self .functions ) if self .functions else None ),
193+ )
194+ cur .execute (
195+ "INSERT INTO metadata VALUES (?, ?)" ,
196+ ("timestamp" , datetime .datetime .now (datetime .timezone .utc ).isoformat ()),
197+ )
198+ cur .execute ("INSERT INTO metadata VALUES (?, ?)" , ("project_root" , str (self .project_root )))
177199 console .rule ("Codeflash: Traced Program Output Begin" , style = "bold blue" )
178200 frame = sys ._getframe (0 ) # Get this frame and simulate a call to it # noqa: SLF001
179201 self .dispatch ["call" ](self , frame , 0 )
@@ -842,6 +864,7 @@ def main() -> ArgumentParser:
842864 max_function_count = args .max_function_count ,
843865 timeout = args .tracer_timeout ,
844866 config_file_path = args .codeflash_config ,
867+ command = " " .join (sys .argv ),
845868 ).runctx (code , globs , None )
846869
847870 except BrokenPipeError as exc :
0 commit comments