85 lines
3.8 KiB
Python
85 lines
3.8 KiB
Python
"""Bounded, private snapshots used for Agent tool change counts inside a workspace Pod."""
|
|
|
|
import os
|
|
import pathlib
|
|
import stat
|
|
import subprocess
|
|
import time
|
|
|
|
CHANGE_EXCLUDED = {".npm", ".cache", ".pytest_cache", ".output", ".nuxt", ".turbo",
|
|
"vendor", "target", "venv", ".mypy_cache"}
|
|
|
|
|
|
def snapshot_changes(root, destination):
|
|
destination.mkdir()
|
|
deadline = time.monotonic() + 2
|
|
total = visited = 0
|
|
def fail(error):
|
|
raise error
|
|
|
|
for directory, dirs, files in os.walk(root, followlinks=False, onerror=fail):
|
|
visited += 1
|
|
if visited > 5000 or time.monotonic() >= deadline:
|
|
raise ValueError("Workspace change scan limit reached")
|
|
current = pathlib.Path(directory)
|
|
dirs[:] = sorted(d for d in dirs if d not in CHANGE_EXCLUDED
|
|
and not review_secret((current / d).relative_to(root))
|
|
and not (current / d).is_symlink())
|
|
for name in sorted(files):
|
|
visited += 1
|
|
if visited > 5000 or time.monotonic() >= deadline:
|
|
raise ValueError("Workspace change scan limit reached")
|
|
source = current / name
|
|
relative = source.relative_to(root)
|
|
if review_secret(relative) or source.is_symlink() or not source.is_file():
|
|
continue
|
|
if not source.resolve().is_relative_to(root):
|
|
raise ValueError("Workspace file moved during scan")
|
|
# Nonblocking open and fstat guard against paths replaced by special files.
|
|
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0)
|
|
with os.fdopen(os.open(source, flags), "rb") as stream:
|
|
if not stat.S_ISREG(os.fstat(stream.fileno()).st_mode):
|
|
raise ValueError("Workspace file changed during scan")
|
|
data = stream.read(8_000_000 - total + 1)
|
|
total += len(data)
|
|
if total > 8_000_000:
|
|
raise ValueError("Workspace change snapshot exceeds 8 MB")
|
|
target = destination / relative
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_bytes(data)
|
|
|
|
|
|
def summarize_changes(directory):
|
|
result = subprocess.run(
|
|
["git", "-c", "core.autocrlf=false", "diff", "--no-index", "--no-ext-diff",
|
|
"--no-textconv", "--no-renames", "--numstat", "-z", "--", "before", "after"],
|
|
cwd=directory, capture_output=True, timeout=4,
|
|
env={**os.environ, "GIT_CONFIG_NOSYSTEM": "1", "GIT_CONFIG_GLOBAL": os.devnull},
|
|
)
|
|
if result.returncode not in (0, 1):
|
|
raise ValueError("Unable to compare workspace snapshots")
|
|
records = iter(result.stdout.split(b"\0"))
|
|
files, size = [], 0
|
|
for counts in records:
|
|
if not counts:
|
|
break
|
|
added, removed, path = counts.split(b"\t", 2)
|
|
if path:
|
|
raise ValueError("Unexpected Git snapshot path format")
|
|
old, new = next(records, b""), next(records, b"")
|
|
if new == b"/dev/null" and old.startswith(b"before/"):
|
|
path, status = old[len(b"before/"):], "DELETED"
|
|
elif new.startswith(b"after/") and (old == b"/dev/null" or old.startswith(b"before/")):
|
|
path = new[len(b"after/"):]
|
|
status = "ADDED" if old == b"/dev/null" else "MODIFIED"
|
|
else:
|
|
raise ValueError("Invalid Git snapshot path")
|
|
size += len(path)
|
|
if len(files) >= 120 or size > 12000 or len(path) > 1000:
|
|
return {"complete": False, "files": files}
|
|
binary = added == b"-"
|
|
files.append({"path": path.decode("utf-8"), "status": status,
|
|
"added": 0 if binary else int(added), "removed": 0 if binary else int(removed),
|
|
"binary": binary})
|
|
return {"complete": True, "files": files}
|