Skip to content

Official Codebase of 3DCodeVerse

Python

envelope: one directory-shaped path discarded the whole generation

gaoypengd22dbce0c8e3
diff --git a/harness/codeverse/tracks/envelope.py b/harness/codeverse/tracks/envelope.py
index 6d856f32..94164776 100644
--- a/harness/codeverse/tracks/envelope.py
+++ b/harness/codeverse/tracks/envelope.py
@@ -118,6 +118,8 @@ def safe_relpath(path: str, allowed_roots: tuple[str, ...] = ALLOWED_ROOTS) -> s
p = _clean_path(path)
if not p or p.startswith("/") or ".." in Path(p).parts or re.match(r"^[A-Za-z]:", p):
raise GenerationError(f"refusing to write outside the workspace: {path!r}")
+ if p.endswith("/"): # '=== FILE: src/parts/ ===' names a directory, not a file
+ raise GenerationError(f"path {path!r} is a directory, not a file")
if not any(p.startswith(root) for root in allowed_roots):
raise GenerationError(f"path {p!r} is outside the allowed roots {allowed_roots}")
return p
@@ -132,10 +134,10 @@ def write_files(
) -> list[FileChange]:
"""Write parsed files under the workspace; returns git-style FileChange rows.
- A block whose path is unsafe or outside ``allowed_roots`` (flash models love
- to add README.md / package.json despite the format rule) is SKIPPED — reported
- via ``on_skip`` — instead of aborting the whole write: the valid files were
- already paid for."""
+ A block whose path is unsafe, outside ``allowed_roots`` (flash models love
+ to add README.md / package.json despite the format rule) or unwritable is
+ SKIPPED — reported via ``on_skip`` — instead of aborting the whole write:
+ the valid files were already paid for."""
changes: list[FileChange] = []
for raw, content in files.items():
try:
@@ -147,8 +149,17 @@ def write_files(
continue
dest = ws.root / rel
existed = dest.exists()
- dest.parent.mkdir(parents=True, exist_ok=True)
- dest.write_text(content if content.endswith("\n") else content + "\n")
+ try:
+ dest.parent.mkdir(parents=True, exist_ok=True)
+ dest.write_text(content if content.endswith("\n") else content + "\n")
+ except OSError as e:
+ # Same contract as an out-of-root path: one unwritable block (a path that
+ # is already a directory, a name the filesystem rejects) must not throw
+ # away the files that DID parse -- the whole answer was already paid for.
+ if on_skip is not None:
+ on_skip(raw, str(e))
+ log.warning("skipping unwritable file from generator: %s: %s", rel, e)
+ continue
changes.append(FileChange(path=rel, status="modified" if existed else "added",
lines_added=content.count("\n") + 1))
return changes
diff --git a/harness/tests/orchestrator_tracks/test_generation_planner_repair.py b/harness/tests/orchestrator_tracks/test_generation_planner_repair.py
index 91e10154..06c339a8 100644
--- a/harness/tests/orchestrator_tracks/test_generation_planner_repair.py
+++ b/harness/tests/orchestrator_tracks/test_generation_planner_repair.py
@@ -83,6 +83,55 @@ def test_safe_relpath_and_write_files(tmp_ws):
assert "=== FILE:" in SINGLE_SHOT_FORMAT
+def test_a_directory_shaped_path_is_skipped_not_fatal(tmp_ws):
+ """CP-3/CP-5: '=== FILE: src/parts/ ===' used to abort the whole write.
+
+ safe_relpath passed it (allowed root, no '..'), write_files caught only
+ GenerationError, so IsADirectoryError from write_text killed the loop on the
+ FIRST bad entry and the real, already-paid-for src/object.js was never
+ written -- the round was then recorded failed with zero files."""
+ (tmp_ws.src / "parts").mkdir(parents=True, exist_ok=True)
+ skipped: list[tuple[str, str]] = []
+
+ changes = write_files(
+ tmp_ws,
+ {"src/parts/": "Here are the parts.\n", "src/object.js": "export const x = 1;\n"},
+ on_skip=lambda path, why: skipped.append((path, why)),
+ )
+
+ assert [c.path for c in changes] == ["src/object.js"]
+ assert (tmp_ws.src / "object.js").read_text() == "export const x = 1;\n"
+ assert [path for path, _ in skipped] == ["src/parts/"]
+ assert (tmp_ws.src / "parts").is_dir() # and it was not clobbered into a file
+
+
+def test_a_directory_shaped_path_that_does_not_exist_yet_is_also_skipped(tmp_ws):
+ """The silent half of the same defect: with no src/parts on disk, write_text
+ quietly created a FILE named 'parts' holding the prose body."""
+ skipped: list[str] = []
+
+ changes = write_files(
+ tmp_ws,
+ {"src/parts/": "Here are the parts.\n", "src/object.js": "export const x = 1;\n"},
+ on_skip=lambda path, why: skipped.append(path),
+ )
+
+ assert [c.path for c in changes] == ["src/object.js"]
+ assert skipped == ["src/parts/"]
+ assert not (tmp_ws.src / "parts").is_file()
+
+
+def test_write_files_survives_an_oserror_from_the_filesystem(tmp_ws):
+ """Any OSError, not just IsADirectoryError, must skip one block -- never the answer."""
+ (tmp_ws.src / "a.js").mkdir(parents=True, exist_ok=True)
+ skipped: list[str] = []
+
+ changes = write_files(tmp_ws, {"src/a.js": "x", "src/b.js": "y"},
+ on_skip=lambda path, why: skipped.append(path))
+
+ assert [c.path for c in changes] == ["src/b.js"] and skipped == ["src/a.js"]
+
+
# ----------------------------------------------------------------------------- strategies
def test_generate_single_shot_writes_files_and_charges(tmp_ws):
model = FakeChatModel(lambda req: "=== FILE: src/model.py ===\nimport bpy\n=== END FILE ===")