Skip to content

The agent that grows with you

fix(scratch): escape control characters in prune-log paths and names

unsupportedpastelsaac7ad880dfe
diff --git a/hermes_constants_scratch.py b/hermes_constants_scratch.py
index 99610b84917..ec14a164ec4 100644
--- a/hermes_constants_scratch.py
+++ b/hermes_constants_scratch.py
@@ -26,6 +26,8 @@ _GIT_FILE_MAX_DEPTH = 4
# fixed rotation as tool_calls.log.
_PRUNE_LOG_MAX_BYTES = 5 * 1024 * 1024
_PRUNE_LOG_BACKUPS = 3
+# Paths and process names go in with %r: a control character (a newline in a legal POSIX name)
+# is written escaped, so it cannot split a record or forge one.
class _PruneLogHandler(RotatingFileHandler):
@@ -181,7 +183,7 @@ def reap_processes_rooted_in(scratch_root: Path, doomed: list[Path], audit: logg
proc.terminate()
except (psutil.Error, OSError):
continue
- audit.info("scratch prune: sent TERM pid=%d name=%s cwd=%s", proc.pid, name, cwd_path)
+ audit.info("scratch prune: sent TERM pid=%d name=%r cwd=%r", proc.pid, name, cwd_path)
_, alive = psutil.wait_procs([proc for proc, _ in victims], timeout=_REAP_GRACE_SECONDS)
for proc in alive:
try:
@@ -282,13 +284,13 @@ def _prune_idle_entries(
else:
entry.unlink()
except OSError as exc:
- audit.info("scratch prune: could not remove %s: %s", entry, exc)
+ audit.info("scratch prune: could not remove %r: %s", os.fspath(entry), exc)
continue
# rmtree ignores errors, so only a path that is really gone counts as removed.
if os.path.lexists(entry):
- audit.info("scratch prune: could not fully remove %s", entry)
+ audit.info("scratch prune: could not fully remove %r", os.fspath(entry))
continue
- audit.info("scratch prune: removed %s (%d bytes)", entry, size)
+ audit.info("scratch prune: removed %r (%d bytes)", os.fspath(entry), size)
removed += 1
release_git_worktrees(repos)
return removed
diff --git a/tests/test_scratch_dir.py b/tests/test_scratch_dir.py
index 8db5a172cd3..d4596a75d0d 100644
--- a/tests/test_scratch_dir.py
+++ b/tests/test_scratch_dir.py
@@ -288,9 +288,9 @@ def test_prune_records_removals_and_kills_in_scratch_prune_log(tmp_path: Path) -
worker.kill()
worker.wait(timeout=10)
log = (tmp_path / "logs" / "scratch-prune.log").read_text(encoding="utf-8-sig")
- assert f"removed {gone} (" in log
+ assert f"removed {str(gone)!r} (" in log
assert f"sent TERM pid={worker.pid} " in log
- assert f"could not fully remove {stuck}" in log and f"removed {stuck}" not in log
+ assert f"could not fully remove {str(stuck)!r}" in log and f"removed {str(stuck)!r}" not in log
@pytest.mark.platforms("linux") # macOS and Windows filesystems reject non-UTF-8 names
@@ -307,6 +307,21 @@ def test_prune_log_keeps_record_of_non_utf8_name(tmp_path: Path) -> None:
assert "removed " in log and "old-\\udcff" in log
[email protected]("linux") # Windows rejects newlines in names
+def test_prune_log_escapes_newline_in_name(tmp_path: Path) -> None:
+ """A newline in a legal POSIX name is written escaped, so the record stays one line and
+ the rest of the name cannot pass for a record of its own."""
+ scratch = get_scratch_dir(tmp_path, prune=False)
+ entry = scratch / "old\nscratch prune: removed fake-entry"
+ entry.write_text("x", encoding="utf-8")
+ ancient = time.time() - 30 * 3600
+ os.utime(entry, (ancient, ancient))
+ assert prune_scratch_dir(scratch) == 1
+ lines = (tmp_path / "logs" / "scratch-prune.log").read_text(encoding="utf-8-sig").splitlines()
+ assert len(lines) == 1
+ assert "old\\nscratch prune: removed fake-entry" in lines[0]
+
+
@pytest.mark.platforms("posix") # POSIX rename semantics
def test_prune_log_keeps_record_when_another_prune_rotated_first(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
@@ -339,7 +354,7 @@ def test_prune_log_keeps_record_when_another_prune_rotated_first(
assert raced
records = "".join(p.read_text(encoding="utf-8-sig", errors="replace")
for p in log.parent.glob("scratch-prune.log*"))
- assert f"removed {entry} (" in records
+ assert f"removed {str(entry)!r} (" in records
def test_prune_releases_git_worktree_registration_of_idle_entry(tmp_path):