codeplain 0.4.3.dev61__py3-none-any.whl → 0.4.3.dev62__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,16 +2,21 @@
2
2
 
3
3
  The server declares the tools to the LLM (codeplain-api: src/agent/tools.py) and forwards
4
4
  the model's calls; this module executes them against the local build folder and returns
5
- plain-text results. Relative paths resolve against the build folder. Reads are allowed only in the
6
- build folder and the full test logs the agent was pointed to; writes only inside the build folder.
5
+ plain-text results. Relative paths resolve against the build folder. Each agent works in an
6
+ AgentWorkspace that says what it may read (by default the build folder and the full test logs it
7
+ was pointed to), what it may write (by default the build folder) and which tools it may use. A tool
8
+ given a RenderContext instead works in the unit-test fixing agent's workspace.
7
9
  """
8
10
 
11
+ import difflib
12
+ import fnmatch
9
13
  import glob
10
14
  import json
11
15
  import os
12
- import subprocess
16
+ import re
13
17
  import tempfile
14
- from typing import Callable
18
+ from dataclasses import dataclass, field
19
+ from typing import Any, Callable, Optional, Union
15
20
 
16
21
  from plain2code_console import console
17
22
  from render_machine import render_utils
@@ -22,40 +27,145 @@ MAX_LINE_CHARS = 10_000
22
27
  MAX_OUTPUT_CHARS = 30_000
23
28
  EDIT_SNIPPET_CONTEXT_LINES = 3
24
29
  MAX_EDIT_SNIPPET_LINES = 60
30
+ MAX_REMOVED_LINES_SHOWN = 8
31
+ # A search text that is not found gets the file lines most like it, when they are at least this alike.
32
+ CLOSEST_LINE_MIN_RATIO = 0.6
33
+ CLOSEST_LINES_SHOWN = 3
25
34
  MAX_GREP_CONTEXT_LINES = 20
26
- # Tools without side effects; repeating one while no file changed returns a pointer instead.
35
+ # Tools without side effects. Repeating one while no file changed returns the earlier result again with a note:
36
+ # the server's compaction may have removed that result from the conversation, so a bare pointer to it would send
37
+ # the agent into a loop of identical calls.
27
38
  READ_ONLY_TOOLS = ("read_file", "grep", "ls_files")
39
+ REPEATED_CALL_NOTE = "Same call as an earlier one and no file has changed since, so the result is the same:\n"
28
40
  GREP_EXCLUDED_DIRS = (".git", "__pycache__", "node_modules", ".venv", "target", "dist", "build")
29
41
 
30
42
 
31
- def _build_folder(render_context: RenderContext) -> str:
32
- return os.path.normpath(os.path.abspath(render_context.build_folder))
43
+ def _noop(_full_path: str) -> None:
44
+ return None
33
45
 
34
46
 
35
- def _resolve(file_path: str, render_context: RenderContext) -> str:
47
+ @dataclass
48
+ class AgentWorkspace:
49
+ """Where an agent's tool calls operate and what they may touch."""
50
+
51
+ # Relative tool paths resolve against it, and grep reports matches inside it relative to it.
52
+ build_folder: str
53
+ writable_folders: list[str]
54
+ # Readable besides the writable folders.
55
+ readable_folders: list[str] = field(default_factory=list)
56
+ readable_log_paths: set[str] = field(default_factory=set)
57
+ # Results of read-only tool calls, keyed by call; cleared whenever a file changes.
58
+ tool_result_cache: dict[str, str] = field(default_factory=dict)
59
+ # Called with the absolute path of a file right before and right after a tool changes it.
60
+ before_change: Callable[[str], None] = _noop
61
+ after_change: Callable[[str], None] = _noop
62
+ # The tools the agent may call; None allows every tool in TOOLS.
63
+ allowed_tools: Optional[frozenset[str]] = None
64
+ # Needed by run_unit_tests and run_conformance_tests only.
65
+ render_context: Any = None
66
+ # run_conformance_tests only: the tests folder it runs, and the build folder signature it last prepared.
67
+ conformance_tests_folder: str = ""
68
+ prepared_signature: Optional[tuple] = None
69
+ # run_conformance_tests only: the files it last ran the tests on, and the result.
70
+ last_test_run: Optional[tuple[tuple, dict]] = None
71
+
72
+ def __post_init__(self):
73
+ self.build_folder = _absolute(self.build_folder)
74
+ self.writable_folders = [_absolute(folder) for folder in self.writable_folders]
75
+ self.readable_folders = [_absolute(folder) for folder in self.readable_folders]
76
+
77
+ def describe_writable(self) -> str:
78
+ return ", ".join(self.writable_folders) if self.writable_folders else "nothing"
79
+
80
+
81
+ def _absolute(path: str) -> str:
82
+ return os.path.normpath(os.path.abspath(path))
83
+
84
+
85
+ def rendered_modules_folder(render_context: RenderContext) -> str:
86
+ """The folder holding every rendered module's folder (plain_modules), which the agents may read."""
87
+ return os.path.normpath(os.path.abspath(render_context.plain_module.build_folder))
88
+
89
+
90
+ def modules_layout_params(render_context: RenderContext) -> dict:
91
+ """The task params that tell an agent how the rendered modules are laid out (see the server's
92
+ modules_layout_line)."""
93
+ return {
94
+ "plain_modules_folder": rendered_modules_folder(render_context),
95
+ "required_module_names": [module.module_name for module in render_context.plain_module.all_required_modules],
96
+ }
97
+
98
+
99
+ def unit_tests_workspace(render_context: RenderContext) -> AgentWorkspace:
100
+ """The unit-test fixing agent's workspace: it reads every rendered module and writes the build folder; its test
101
+ logs, and its loop's change tracking."""
102
+ context = render_context.unit_tests_running_context
103
+ return AgentWorkspace(
104
+ build_folder=render_context.build_folder,
105
+ writable_folders=[render_context.build_folder],
106
+ readable_folders=[rendered_modules_folder(render_context)],
107
+ readable_log_paths=render_context.unit_tests_agent_session.readable_log_paths,
108
+ tool_result_cache=context.tool_result_cache,
109
+ after_change=lambda full_path: _track_change(full_path, render_context),
110
+ render_context=render_context,
111
+ )
112
+
113
+
114
+ Target = Union[AgentWorkspace, RenderContext]
115
+
116
+
117
+ def _workspace(target: Target) -> AgentWorkspace:
118
+ return target if isinstance(target, AgentWorkspace) else unit_tests_workspace(target)
119
+
120
+
121
+ def _build_folder(workspace: AgentWorkspace) -> str:
122
+ return workspace.build_folder
123
+
124
+
125
+ def _where_to_look(workspace: AgentWorkspace) -> str:
126
+ """Appended when a path or pattern found nothing or was out of reach: what relative paths resolve against, and
127
+ which folders can be read, so the next call can be right. Agents often look for a module's tests under its
128
+ code folder, while they live next to it."""
129
+ readable = sorted({workspace.build_folder, *workspace.writable_folders, *workspace.readable_folders})
130
+ return (
131
+ f" Relative paths resolve against '{workspace.build_folder}'. Readable folders: "
132
+ + ", ".join(f"'{folder}'" for folder in readable)
133
+ + ". In the rendered modules folder each module has a `code` folder (its implementation) and a `tests`"
134
+ " folder (its conformance tests) next to each other."
135
+ )
136
+
137
+
138
+ def _resolve(file_path: str, workspace: AgentWorkspace) -> str:
36
139
  if os.path.isabs(file_path):
37
140
  return os.path.normpath(file_path)
38
- return os.path.normpath(os.path.join(_build_folder(render_context), file_path))
141
+ return os.path.normpath(os.path.join(workspace.build_folder, file_path))
39
142
 
40
143
 
41
144
  def _within(path: str, folder: str) -> bool:
42
145
  return path == folder or path.startswith(folder + os.sep)
43
146
 
44
147
 
45
- def _readable(path: str, render_context: RenderContext) -> bool:
46
- return (
47
- _within(path, _build_folder(render_context))
48
- or path in render_context.unit_tests_agent_session.readable_log_paths
49
- )
148
+ def _readable(path: str, workspace: AgentWorkspace) -> bool:
149
+ folders = [workspace.build_folder, *workspace.writable_folders, *workspace.readable_folders]
150
+ return any(_within(path, folder) for folder in folders) or path in workspace.readable_log_paths
50
151
 
51
152
 
52
- def register_log_path(log_path: str, render_context: RenderContext) -> None:
153
+ def register_log_path(log_path: str, scope: Target) -> None:
53
154
  """Allow read_file/grep on a full test log the agent is pointed to."""
54
- render_context.unit_tests_agent_session.readable_log_paths.add(os.path.normpath(os.path.abspath(log_path)))
155
+ _workspace(scope).readable_log_paths.add(_absolute(log_path))
156
+
55
157
 
158
+ def _writable(path: str, workspace: AgentWorkspace) -> bool:
159
+ return any(_within(path, folder) for folder in workspace.writable_folders)
56
160
 
57
- def _writable(path: str, render_context: RenderContext) -> bool:
58
- return _within(path, _build_folder(render_context))
161
+
162
+ def _write_denied(full_path: str, workspace: AgentWorkspace) -> str:
163
+ return f"Error: write access denied for '{full_path}' (writable: {workspace.describe_writable()})."
164
+
165
+
166
+ def _changed(full_path: str, workspace: AgentWorkspace) -> None:
167
+ workspace.tool_result_cache.clear()
168
+ workspace.after_change(full_path)
59
169
 
60
170
 
61
171
  def _bound(text: str) -> str:
@@ -73,15 +183,16 @@ def _bound(text: str) -> str:
73
183
 
74
184
  def _track_change(full_path: str, render_context: RenderContext) -> None:
75
185
  context = render_context.unit_tests_running_context
76
- context.changed_files.add(os.path.relpath(full_path, _build_folder(render_context)))
186
+ context.changed_files.add(os.path.relpath(full_path, _absolute(render_context.build_folder)))
77
187
  context.verified_passing, context.verified_passing_log_path = False, None
78
188
  context.tool_result_cache.clear()
79
189
 
80
190
 
81
- def read_file(args: dict, render_context: RenderContext) -> str:
82
- full_path = _resolve(args.get("file_path", ""), render_context)
83
- if not _readable(full_path, render_context):
84
- return f"Error: read access denied for '{full_path}' (readable: build folder and test logs)."
191
+ def read_file(args: dict, scope: Target) -> str:
192
+ workspace = _workspace(scope)
193
+ full_path = _resolve(args.get("file_path", ""), workspace)
194
+ if not _readable(full_path, workspace):
195
+ return f"Error: read access denied for '{full_path}' (readable: the project folders and test logs)."
85
196
  if not os.path.isfile(full_path):
86
197
  return f"Error: file not found: '{full_path}'."
87
198
  with open(full_path, "r", encoding="utf-8", errors="replace") as f:
@@ -101,68 +212,169 @@ def read_file(args: dict, render_context: RenderContext) -> str:
101
212
  return _bound(numbered) + note
102
213
 
103
214
 
104
- def grep(args: dict, render_context: RenderContext) -> str:
215
+ def grep(args: dict, scope: Target) -> str:
216
+ """Search a file, or the files under a directory, for a Python regular expression. Matching lines come back
217
+ as `path:line:text`, context lines as `path-line-text`, with `--` between non-adjacent groups."""
218
+ workspace = _workspace(scope)
105
219
  pattern = args.get("pattern", "")
106
220
  if not pattern:
107
221
  return "Error: pattern is required."
108
- target = _resolve(args.get("file_path") or ".", render_context)
109
- if not _readable(target, render_context):
110
- return f"Error: read access denied for '{target}'."
222
+ try:
223
+ regex = re.compile(pattern)
224
+ except re.error as e:
225
+ return (
226
+ f"Error: invalid regular expression '{pattern}': {e}. The pattern uses Python regex syntax; "
227
+ "escape special characters such as ( [ . * + ? with a backslash to match them literally."
228
+ )
229
+ target = _resolve(args.get("file_path") or ".", workspace)
230
+ if not _readable(target, workspace):
231
+ return f"Error: read access denied for '{target}'." + _where_to_look(workspace)
111
232
  if not os.path.exists(target):
112
- return f"Error: path not found: '{target}'."
113
- # Run from the build folder so matches inside it come back as build-relative paths, which
114
- # is the form the other tools accept; a test log outside it is passed by absolute path.
115
- cwd = _build_folder(render_context)
116
- options = [f"--exclude-dir={d}" for d in GREP_EXCLUDED_DIRS]
233
+ return f"Error: path not found: '{target}'." + _where_to_look(workspace)
117
234
  context_lines = min(max(int(args.get("context_lines") or 0), 0), MAX_GREP_CONTEXT_LINES)
118
- if context_lines:
119
- options.append(f"-C{context_lines}")
120
- if args.get("include"):
121
- options.append(f"--include={args['include']}")
122
- command = ["grep", "-rnI", *options, "-e", pattern, "--"]
123
- command.append(os.path.relpath(target, cwd) if _within(target, cwd) else target)
124
- result = subprocess.run(command, capture_output=True, text=True, cwd=cwd)
125
- if result.returncode == 1:
235
+ # Paths inside the build folder come back build-relative, the form the other tools accept; a test log
236
+ # outside it keeps its absolute path.
237
+ build_folder = _build_folder(workspace)
238
+ output: list[str] = []
239
+ for path in _grep_files(target, args.get("include") or ""):
240
+ display = os.path.relpath(path, build_folder) if _within(path, build_folder) else path
241
+ output.extend(_grep_file(path, display, regex, context_lines, output_has_groups=bool(output)))
242
+ if sum(len(line) + 1 for line in output) > MAX_OUTPUT_CHARS:
243
+ break
244
+ if not output:
126
245
  return f"No matches for '{pattern}' in '{target}'."
127
- if result.returncode != 0:
128
- return f"Error: grep failed: {result.stderr.strip()}"
129
- lines = [line[2:] if line.startswith("./") else line for line in result.stdout.rstrip("\n").split("\n")]
130
- return _bound("\n".join(lines))
131
-
132
-
133
- def ls_files(args: dict, render_context: RenderContext) -> str:
134
- target = _resolve(args.get("pattern") or ".", render_context)
135
- if not _readable(target, render_context):
136
- return f"Error: read access denied for '{target}'."
246
+ return _bound("\n".join(output))
247
+
248
+
249
+ def _grep_files(target: str, include: str):
250
+ """The text files grep searches: `target` itself, or the files under it (skipping GREP_EXCLUDED_DIRS) whose
251
+ name matches the `include` glob."""
252
+ if os.path.isfile(target):
253
+ yield target
254
+ return
255
+ for root, dirs, files in os.walk(target):
256
+ dirs[:] = sorted(d for d in dirs if d not in GREP_EXCLUDED_DIRS)
257
+ for name in sorted(files):
258
+ if not include or fnmatch.fnmatch(name, include):
259
+ yield os.path.join(root, name)
260
+
261
+
262
+ def _grep_file(path: str, display: str, regex: re.Pattern, context_lines: int, output_has_groups: bool) -> list[str]:
263
+ try:
264
+ with open(path, "rb") as f:
265
+ data = f.read()
266
+ except OSError:
267
+ return []
268
+ if b"\0" in data[:8192]:
269
+ return [] # binary
270
+ lines = data.decode("utf-8", errors="replace").splitlines()
271
+ matches = [i for i, line in enumerate(lines) if regex.search(line)]
272
+ if not matches:
273
+ return []
274
+ shown: list[tuple[int, int]] = [] # merged [first, last] line ranges, matches plus their context
275
+ for i in matches:
276
+ first, last = max(i - context_lines, 0), min(i + context_lines, len(lines) - 1)
277
+ if shown and first <= shown[-1][1] + 1:
278
+ shown[-1] = (shown[-1][0], max(shown[-1][1], last))
279
+ else:
280
+ shown.append((first, last))
281
+ matched = set(matches)
282
+ result: list[str] = []
283
+ for first, last in shown:
284
+ if context_lines and (result or output_has_groups):
285
+ result.append("--")
286
+ for i in range(first, last + 1):
287
+ separator = ":" if i in matched else "-"
288
+ result.append(f"{display}{separator}{i + 1}{separator}{lines[i]}")
289
+ return result
290
+
291
+
292
+ def ls_files(args: dict, scope: Target) -> str:
293
+ workspace = _workspace(scope)
294
+ target = _resolve(args.get("pattern") or ".", workspace)
295
+ if not _readable(target, workspace):
296
+ return f"Error: read access denied for '{target}'." + _where_to_look(workspace)
137
297
  if os.path.isdir(target):
138
298
  entries = sorted(os.listdir(target))
139
299
  listing = [entry + "/" if os.path.isdir(os.path.join(target, entry)) else entry for entry in entries]
140
300
  return f"{target}:\n" + ("\n".join(listing) if listing else "(empty)")
141
- matches = sorted(glob.glob(target, recursive=True))
142
- return _bound("\n".join(matches)) if matches else f"No files match '{target}'."
301
+ matches = sorted(glob.glob(target, recursive=True, include_hidden=True))
302
+ return _bound("\n".join(matches)) if matches else f"No files match '{target}'." + _where_to_look(workspace)
143
303
 
144
304
 
145
- def edit_file(args: dict, render_context: RenderContext) -> str:
146
- full_path = _resolve(args.get("file_path", ""), render_context)
305
+ def edit_file(args: dict, scope: Target) -> str:
306
+ workspace = _workspace(scope)
307
+ full_path = _resolve(args.get("file_path", ""), workspace)
147
308
  search, replace = args.get("search", ""), args.get("replace", "")
148
309
  if not search:
149
310
  return "Error: search is required."
150
- if not _writable(full_path, render_context):
151
- return f"Error: write access denied for '{full_path}' (writable: build folder only)."
311
+ if not _writable(full_path, workspace):
312
+ return _write_denied(full_path, workspace)
152
313
  if not os.path.isfile(full_path):
153
314
  return f"Error: file not found: '{full_path}'. Use write_file to create new files."
154
315
  with open(full_path, "r", encoding="utf-8") as f:
155
316
  content = f.read()
156
317
  occurrences = content.count(search)
318
+ if occurrences == 0:
319
+ return f"Error: search text found 0 times in '{full_path}'; it must appear exactly once." + _closest_lines(
320
+ content, search
321
+ )
157
322
  if occurrences != 1:
158
323
  return (
159
324
  f"Error: search text found {occurrences} times in '{full_path}'; it must appear exactly once. "
160
325
  "Read the file and use a larger, unique snippet."
161
326
  )
327
+ workspace.before_change(full_path)
162
328
  with open(full_path, "w", encoding="utf-8") as f:
163
329
  f.write(content.replace(search, replace, 1))
164
- _track_change(full_path, render_context)
165
- return f"Edited '{full_path}'. The edited region now reads:\n" + _edit_snippet(content, search, replace)
330
+ _changed(full_path, workspace)
331
+ result = f"Edited '{full_path}'. The edited region now reads:\n" + _edit_snippet(content, search, replace)
332
+ return result + _removed_lines_notice(search, replace)
333
+
334
+
335
+ def _closest_lines(content: str, search: str) -> str:
336
+ """The file lines most like the longest line of a search text that was not found, numbered and exactly as the
337
+ file has them, so a near miss (escaping, whitespace, a stale line) is fixed without re-reading the file."""
338
+ needle = max((line.strip() for line in search.split("\n")), key=len, default="")
339
+ if not needle:
340
+ return " Read the file and copy the text exactly as it is there."
341
+ scored = []
342
+ for number, line in enumerate(content.split("\n"), start=1):
343
+ stripped = line.strip()
344
+ if not stripped:
345
+ continue
346
+ matcher = difflib.SequenceMatcher(None, needle, stripped, autojunk=False)
347
+ if matcher.real_quick_ratio() < CLOSEST_LINE_MIN_RATIO or matcher.quick_ratio() < CLOSEST_LINE_MIN_RATIO:
348
+ continue
349
+ ratio = matcher.ratio()
350
+ if ratio >= CLOSEST_LINE_MIN_RATIO:
351
+ scored.append((ratio, number, line))
352
+ if not scored:
353
+ return " No line of the file resembles it; read the file and copy the text exactly as it is there."
354
+ best = sorted(scored, key=lambda item: -item[0])[:CLOSEST_LINES_SHOWN]
355
+ shown = "\n".join(f"{number}: {line}" for _, number, line in sorted(best, key=lambda item: item[1]))
356
+ return (
357
+ " The closest lines in the file are (line number, then the line exactly as the file has it, backslashes"
358
+ " and quotes included):\n" + shown + "\nCopy the search text from these lines character for character."
359
+ )
360
+
361
+
362
+ def _removed_lines_notice(search: str, replace: str) -> str:
363
+ """A note naming the non-blank lines of `search` that no longer appear anywhere in `replace`, so an accidental
364
+ deletion (a closing tag or brace, an import, an assertion) is noticed right away."""
365
+ kept = {line.strip() for line in replace.split("\n")}
366
+ removed = [line.strip() for line in search.split("\n") if line.strip() and line.strip() not in kept]
367
+ if not removed:
368
+ return ""
369
+ shown = removed[:MAX_REMOVED_LINES_SHOWN]
370
+ more = f" (and {len(removed) - len(shown)} more)" if len(removed) > len(shown) else ""
371
+ return (
372
+ "\nThis edit removed these lines"
373
+ + more
374
+ + ": "
375
+ + "; ".join(f"`{line}`" for line in shown)
376
+ + ". Check that removing them was intended."
377
+ )
166
378
 
167
379
 
168
380
  def _edit_snippet(content: str, search: str, replace: str) -> str:
@@ -176,25 +388,72 @@ def _edit_snippet(content: str, search: str, replace: str) -> str:
176
388
  return _bound(snippet)
177
389
 
178
390
 
179
- def write_file(args: dict, render_context: RenderContext) -> str:
180
- full_path = _resolve(args.get("file_path", ""), render_context)
181
- if not _writable(full_path, render_context):
182
- return f"Error: write access denied for '{full_path}' (writable: build folder only)."
391
+ def write_file(args: dict, scope: Target) -> str:
392
+ workspace = _workspace(scope)
393
+ full_path = _resolve(args.get("file_path", ""), workspace)
394
+ if not _writable(full_path, workspace):
395
+ return _write_denied(full_path, workspace)
396
+ renamed = _match_letter_case(full_path, workspace)
397
+ workspace.before_change(full_path)
183
398
  os.makedirs(os.path.dirname(full_path), exist_ok=True)
184
399
  with open(full_path, "w", encoding="utf-8") as f:
185
400
  f.write(args.get("content", ""))
186
- _track_change(full_path, render_context)
187
- return f"Wrote '{full_path}'."
188
-
189
-
190
- def delete_file(args: dict, render_context: RenderContext) -> str:
191
- full_path = _resolve(args.get("file_path", ""), render_context)
192
- if not _writable(full_path, render_context):
193
- return f"Error: write access denied for '{full_path}' (writable: build folder only)."
401
+ _changed(full_path, workspace)
402
+ note = "".join(
403
+ f" '{old}' was renamed to '{new}': the file system ignores letter case, so writing under the new spelling"
404
+ " would otherwise have kept the old one."
405
+ for old, new in renamed
406
+ )
407
+ return f"Wrote '{full_path}'.{note}"
408
+
409
+
410
+ def _match_letter_case(full_path: str, workspace: AgentWorkspace) -> list[tuple[str, str]]:
411
+ """On a file system that ignores letter case, a file or folder named differently only in case is the same entry,
412
+ and writing under the new spelling keeps the old one: a package folder or a class file could then never get its
413
+ case right. Renames every existing part of `full_path` below its writable folder whose spelling on disk differs
414
+ from `full_path` only in case, and returns the (old, new) paths."""
415
+ roots = [folder for folder in workspace.writable_folders if _within(full_path, folder)]
416
+ if not roots:
417
+ return []
418
+ root = max(roots, key=len)
419
+ renamed = []
420
+ current = root
421
+ for part in os.path.relpath(full_path, root).split(os.sep):
422
+ entries = os.listdir(current) if os.path.isdir(current) else []
423
+ on_disk = None if part in entries else next((e for e in entries if e.lower() == part.lower()), None)
424
+ target = os.path.join(current, part)
425
+ source = os.path.join(current, on_disk) if on_disk is not None else ""
426
+ # Only a file system that ignores letter case resolves the new spelling to the old entry; on one that
427
+ # respects case the two spellings are two separate entries and nothing is renamed.
428
+ if on_disk is not None and os.path.exists(target) and os.path.samefile(source, target):
429
+ moved = (
430
+ [os.path.join(d, f) for d, _, files in os.walk(source) for f in files]
431
+ if os.path.isdir(source)
432
+ else [source]
433
+ )
434
+ for path in moved:
435
+ workspace.before_change(path)
436
+ # Two steps: a direct rename to a name that differs only in case is a no-op on some file systems.
437
+ temporary = os.path.join(current, f".{part}.case-rename")
438
+ os.rename(source, temporary)
439
+ os.rename(temporary, target)
440
+ for path in moved:
441
+ _changed(target + path[len(source) :], workspace)
442
+ renamed.append((source, target))
443
+ current = target
444
+ return renamed
445
+
446
+
447
+ def delete_file(args: dict, scope: Target) -> str:
448
+ workspace = _workspace(scope)
449
+ full_path = _resolve(args.get("file_path", ""), workspace)
450
+ if not _writable(full_path, workspace):
451
+ return _write_denied(full_path, workspace)
194
452
  if not os.path.isfile(full_path):
195
453
  return f"Error: file not found: '{full_path}'."
454
+ workspace.before_change(full_path)
196
455
  os.remove(full_path)
197
- _track_change(full_path, render_context)
456
+ _changed(full_path, workspace)
198
457
  return f"Deleted '{full_path}'."
199
458
 
200
459
 
@@ -202,9 +461,11 @@ def full_log_pointer(log_file_path: str | None) -> str:
202
461
  return f" Full log: {log_file_path} (search it with grep, passing that path as file_path)." if log_file_path else ""
203
462
 
204
463
 
205
- def run_unit_tests(_args: dict, render_context: RenderContext) -> dict:
464
+ def run_unit_tests(_args: dict, scope: Target) -> dict:
206
465
  """Returns a result dict: a short header in `output` and the raw failure output in
207
466
  `test_output`, which the server condenses (summarizing it when it is long)."""
467
+ workspace = _workspace(scope)
468
+ render_context = workspace.render_context
208
469
  exit_code, output, log_file_path = render_utils.execute_script(
209
470
  os.path.normpath(render_context.unittests_script),
210
471
  [render_context.build_folder],
@@ -221,14 +482,84 @@ def run_unit_tests(_args: dict, render_context: RenderContext) -> dict:
221
482
  f.write(output)
222
483
  log_file_path = f.name
223
484
  if log_file_path:
224
- register_log_path(log_file_path, render_context)
485
+ register_log_path(log_file_path, workspace)
225
486
  return {
226
487
  "output": f"Unit tests failed (exit code {exit_code}).{full_log_pointer(log_file_path)}",
227
488
  "test_output": output,
228
489
  }
229
490
 
230
491
 
231
- TOOLS: dict[str, Callable[[dict, RenderContext], str | dict]] = {
492
+ def test_run_files(render_context, tests_folder: str) -> tuple:
493
+ """What a conformance tests run depends on: the build folder and the conformance tests folder, file by file."""
494
+ from render_machine.actions import prepare_testing_environment
495
+
496
+ return (
497
+ prepare_testing_environment._build_folder_signature(render_context.build_folder),
498
+ prepare_testing_environment._build_folder_signature(tests_folder),
499
+ )
500
+
501
+
502
+ def run_conformance_tests(_args: dict, scope: Target) -> dict:
503
+ """Runs the failing conformance tests (the folder the fixer fixes) on the build folder as it is now, the way the
504
+ harness does: the prepare environment script first, when there is one and the build folder changed since the
505
+ last preparation, then the conformance tests script. Unit tests are not run. Returns a result dict like
506
+ run_unit_tests."""
507
+ workspace = _workspace(scope)
508
+ render_context = workspace.render_context
509
+ tests_folder = workspace.conformance_tests_folder
510
+ files = test_run_files(render_context, tests_folder)
511
+ if workspace.last_test_run is not None and workspace.last_test_run[0] == files:
512
+ result = dict(workspace.last_test_run[1])
513
+ result["output"] = (
514
+ "Nothing changed in the build folder or the conformance tests folder since your last run_conformance_tests,"
515
+ " so running the tests again would give the same result; it is repeated below. Change something first. "
516
+ + result["output"]
517
+ )
518
+ return result
519
+ result = _run_conformance_tests(render_context, tests_folder, workspace)
520
+ # The scripts may write into the folders (__pycache__, build output): key the result on the files as the run
521
+ # left them, or the next call would always see a change.
522
+ workspace.last_test_run = (test_run_files(render_context, tests_folder), result)
523
+ return result
524
+
525
+
526
+ def _run_conformance_tests(render_context, tests_folder: str, workspace) -> dict:
527
+ # Imported here: prepare_testing_environment imports render_context, which this module must not import early.
528
+ from render_machine.actions import prepare_testing_environment
529
+
530
+ if render_context.prepare_environment_script:
531
+ signature = prepare_testing_environment._build_folder_signature(render_context.build_folder)
532
+ if signature != workspace.prepared_signature:
533
+ exit_code, output, log_file_path = prepare_testing_environment._run_script(render_context)
534
+ if exit_code != 0:
535
+ return _failed_run("Preparing the test environment failed", exit_code, output, log_file_path, workspace)
536
+ exit_code, output, log_file_path = render_utils.execute_script(
537
+ os.path.normpath(render_context.conformance_tests_script),
538
+ [render_context.build_folder, tests_folder],
539
+ "Conformance Tests",
540
+ timeout=render_context.test_script_timeout,
541
+ stop_event=render_context.stop_event,
542
+ )
543
+ if render_context.prepare_environment_script:
544
+ # The environment is prepared for the build folder as the scripts left it: what they wrote there themselves
545
+ # (build output, caches) is no change that needs another preparation.
546
+ workspace.prepared_signature = prepare_testing_environment._build_folder_signature(render_context.build_folder)
547
+ if exit_code == 0:
548
+ return {"output": "The conformance tests passed."}
549
+ return _failed_run("The conformance tests failed", exit_code, output, log_file_path, workspace)
550
+
551
+
552
+ def _failed_run(header: str, exit_code: int, output: str, log_file_path: Optional[str], workspace) -> dict:
553
+ if not log_file_path and output:
554
+ with tempfile.NamedTemporaryFile("w", encoding="utf-8", delete=False, suffix=".script_output") as f:
555
+ f.write(output)
556
+ log_file_path = f.name
557
+ if log_file_path:
558
+ register_log_path(log_file_path, workspace)
559
+ return {"output": f"{header} (exit code {exit_code}).{full_log_pointer(log_file_path)}", "test_output": output}
560
+
561
+
562
+ TOOLS: dict[str, Callable[[dict, Target], str | dict]] = {
232
563
  "read_file": read_file,
233
564
  "grep": grep,
234
565
  "ls_files": ls_files,
@@ -236,30 +567,30 @@ TOOLS: dict[str, Callable[[dict, RenderContext], str | dict]] = {
236
567
  "write_file": write_file,
237
568
  "delete_file": delete_file,
238
569
  "run_unit_tests": run_unit_tests,
570
+ "run_conformance_tests": run_conformance_tests,
239
571
  }
240
572
 
241
573
 
242
- def execute_calls(calls: list[dict], render_context: RenderContext) -> list[dict]:
574
+ def execute_calls(calls: list[dict], scope: Target) -> list[dict]:
243
575
  """Execute the agent's tool calls in order; every call gets a result, errors included.
244
576
 
245
577
  A repeated read-only call (same tool and arguments, no file changed in between) is not
246
- re-executed: its result is already in the conversation, so a short pointer is returned."""
247
- cache = render_context.unit_tests_running_context.tool_result_cache
578
+ re-executed: the earlier result is returned again with a note saying so."""
579
+ workspace = _workspace(scope)
580
+ cache = workspace.tool_result_cache
248
581
  results = []
249
582
  for call in calls:
250
- tool = TOOLS.get(call["name"])
583
+ allowed = workspace.allowed_tools is None or call["name"] in workspace.allowed_tools
584
+ tool = TOOLS.get(call["name"]) if allowed else None
251
585
  cache_key = json.dumps([call["name"], call.get("args") or {}], sort_keys=True)
252
586
  result: dict
253
587
  if tool is None:
254
588
  result = {"output": f"Error: unknown tool '{call['name']}'."}
255
589
  elif call["name"] in READ_ONLY_TOOLS and cache_key in cache:
256
- result = {
257
- "output": "Same call as an earlier one and no file has changed since; "
258
- "its result is unchanged (see the earlier result above)."
259
- }
590
+ result = {"output": REPEATED_CALL_NOTE + cache[cache_key]}
260
591
  else:
261
592
  try:
262
- output = tool(call.get("args") or {}, render_context)
593
+ output = tool(call.get("args") or {}, workspace)
263
594
  result = output if isinstance(output, dict) else {"output": output}
264
595
  except Exception as e:
265
596
  result = {"output": f"Error: tool '{call['name']}' failed: {type(e).__name__}: {e}"}