MCPcopy Create free account
hub / github.com/BillHuang2001/evogit / evaluate_code

Function evaluate_code

python-impl/evogit/api.py:122–178  ·  view source on GitHub ↗

Evaluate the the result of the individual. This function can be run in parallel. Parameters ---------- codegen_dir The directory of the codegen. branch The branch name. cmd List of strings. The command to run the code. For example ["python", "main

(config: EvoGitConfig, commit: str, worktree)

Source from the content-addressed store, hash-verified

120
121
122def evaluate_code(config: EvoGitConfig, commit: str, worktree) -> dict[str, str]:
123 """Evaluate the the result of the individual. This function can be run in parallel.
124
125 Parameters
126 ----------
127 codegen_dir
128 The directory of the codegen.
129 branch
130 The branch name.
131 cmd
132 List of strings. The command to run the code.
133 For example ["python", "main.py"]
134 The command will be run with the current working directory set to the branch directory.
135 handler
136 A function that takes the result of the command and return the fitness.
137 The result is a dictionary of the following: {
138 "stdout": str,
139 "stderr": str,
140 "timeout": bool
141 }
142 """
143 if not config.reevaluate:
144 # try to read the result from the git notes
145 # if the result is found, directly return the result without evaluating
146 note = git.read_note(config, commit)
147 logger.info(f"Read note of {commit}: {note}\n")
148 if note is not None:
149 return json.loads(note)
150
151 cmd = config.eval_command
152 if config.enable_sandbox:
153 # run the command in a bubblewrap sandbox
154 cmd = [Path(__file__).parent / "bubblewrap_run.sh"] + cmd
155
156 try:
157 completed_proc = subprocess.run(
158 cmd,
159 capture_output=True,
160 cwd=os.path.join(config.git_dir, worktree),
161 timeout=config.timeout,
162 )
163 stdout = completed_proc.stdout.decode("utf-8")
164 stderr = completed_proc.stderr.decode("utf-8")
165 result = {
166 "stdout": stdout,
167 "stderr": stderr,
168 "timeout": False,
169 }
170 except subprocess.TimeoutExpired:
171 result = {
172 "stdout": "",
173 "stderr": "",
174 "timeout": True,
175 }
176
177 logger.info(f"Evaluated {commit}\n")
178 return result
179

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected