Show actual file changes recorded for each Agent task
This commit is contained in:
1 parent
d04777247b
commit
3296d378c9
14 files changed
+453
-16
No files matched your search
@@ -3,7 +3,7 @@ FROM ${BASE_IMAGE}
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY app.py runtime.py domain_runtime.py review_tools.py ./
|
||||
COPY app.py runtime.py domain_runtime.py review_tools.py workspace_changes.py ./
|
||||
USER 10001:10001
|
||||
EXPOSE 8090
|
||||
CMD ["uvicorn","app:app","--host","0.0.0.0","--port","8090","--workers","1","--no-access-log"]
|
||||
+31
-4
@@ -54,6 +54,7 @@ app.include_router(domain_router)
|
||||
class Tool(BaseModel):
|
||||
name: str = Field(max_length=40)
|
||||
arguments: dict
|
||||
trackChanges: bool = False
|
||||
|
||||
|
||||
def authorize(value: str | None):
|
||||
@@ -201,6 +202,17 @@ import pathlib,os,json,base64,subprocess,signal,tempfile
|
||||
args=json.loads(base64.b64decode(PAYLOAD))
|
||||
root=pathlib.Path('/workspace').resolve()
|
||||
name=args['name']; values=args['arguments']
|
||||
track_changes = args.get('trackChanges') and name in {'write_file', 'delete_file', 'run_command'}
|
||||
temporary = None
|
||||
before_complete = False
|
||||
if track_changes:
|
||||
try:
|
||||
temporary = tempfile.TemporaryDirectory(prefix='webup-changes-', ignore_cleanup_errors=True)
|
||||
change_dir = pathlib.Path(temporary.name)
|
||||
snapshot_changes(root, change_dir / 'before')
|
||||
before_complete = True
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
def path(value):
|
||||
if not isinstance(value,str) or len(value)>1000: raise ValueError('Invalid path')
|
||||
target=(root/value).resolve()
|
||||
@@ -288,11 +300,23 @@ try:
|
||||
if not target.is_file(): raise ValueError('Only individual files may be deleted')
|
||||
target.unlink();result={'deleted':True}
|
||||
else: raise ValueError('Unknown tool')
|
||||
print(json.dumps(result,ensure_ascii=False))
|
||||
except Exception as exc:
|
||||
print(json.dumps({'error':str(exc)[:500]}))
|
||||
result = {'error': str(exc)[:500]}
|
||||
if track_changes:
|
||||
result['fileChanges'] = {'complete': False, 'files': []}
|
||||
try:
|
||||
if before_complete:
|
||||
snapshot_changes(root, change_dir / 'after')
|
||||
result['fileChanges'] = summarize_changes(change_dir)
|
||||
except (OSError, ValueError, subprocess.SubprocessError):
|
||||
pass
|
||||
finally:
|
||||
if temporary is not None:
|
||||
temporary.cleanup()
|
||||
print(json.dumps(result, ensure_ascii=True if track_changes else False))
|
||||
'''
|
||||
SCRIPT = Path(__file__).with_name('review_tools.py').read_text(encoding='utf-8') + '\n' + SCRIPT
|
||||
SCRIPT = '\n'.join(Path(__file__).with_name(name).read_text(encoding='utf-8')
|
||||
for name in ('review_tools.py', 'workspace_changes.py')) + '\n' + SCRIPT
|
||||
|
||||
|
||||
def read_exec_output(connection, limit):
|
||||
@@ -385,7 +409,10 @@ def execute_tool(project,request,authorization,review):
|
||||
except Exception:
|
||||
connection.close()
|
||||
raise
|
||||
output = read_exec_output(connection, 16_000_000 if request.name in {'archive_source','review_export'} else MAX_OUTPUT)
|
||||
output_limit = 16_000_000 if request.name in {'archive_source','review_export'} else MAX_OUTPUT
|
||||
if request.trackChanges:
|
||||
output_limit = 512_000
|
||||
output = read_exec_output(connection, output_limit)
|
||||
try:
|
||||
return json.loads(output)
|
||||
except ValueError:
|
||||
|
||||
@@ -17,8 +17,10 @@ spec.loader.exec_module(gateway)
|
||||
|
||||
|
||||
class GatewayTests(unittest.TestCase):
|
||||
def execute_file_tool(self, root, name, arguments):
|
||||
payload = base64.b64encode(json.dumps({"name": name, "arguments": arguments}).encode()).decode()
|
||||
def execute_file_tool(self, root, name, arguments, track_changes=False):
|
||||
payload = base64.b64encode(json.dumps({
|
||||
"name": name, "arguments": arguments, "trackChanges": track_changes
|
||||
}).encode()).decode()
|
||||
script = gateway.SCRIPT.replace("pathlib.Path('/workspace')", "pathlib.Path(" + repr(str(root)) + ")")
|
||||
result = subprocess.run([sys.executable, "-X", "utf8", "-c", "PAYLOAD=" + repr(payload) + "\n" + script], capture_output=True, timeout=10)
|
||||
self.assertEqual(0, result.returncode, result.stderr.decode())
|
||||
@@ -39,6 +41,33 @@ class GatewayTests(unittest.TestCase):
|
||||
(root / "large.txt").write_bytes(b"x" * 64001)
|
||||
self.assertIn("error", self.execute_file_tool(root, "read_file", {"path": "large.txt"}))
|
||||
|
||||
def test_agent_file_change_counts_and_manual_edits_are_separate(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
self.assertNotIn("fileChanges", self.execute_file_tool(root, "write_file", {
|
||||
"path": "manual.txt", "content": "user\n"
|
||||
}))
|
||||
results = []
|
||||
for tool, args in [
|
||||
("write_file", {"path": "src/code.js", "content": "one\ntwo\n"}),
|
||||
("write_file", {"path": "src/code.js", "content": "one\nthree\nfour\n"}),
|
||||
("delete_file", {"path": "src/code.js"}),
|
||||
]:
|
||||
result = self.execute_file_tool(root, tool, args, track_changes=True)
|
||||
self.assertTrue(result["fileChanges"]["complete"])
|
||||
files = result["fileChanges"]["files"]
|
||||
self.assertEqual(["src/code.js"], [file["path"] for file in files])
|
||||
results.append(tuple(files[0][key] for key in ("status", "added", "removed")))
|
||||
self.assertEqual([("ADDED", 2, 0), ("MODIFIED", 2, 1), ("DELETED", 0, 3)], results)
|
||||
|
||||
def test_source_scan_failure_reports_partial_and_keeps_successful_write(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / "large.dat").write_bytes(b"x" * 8_000_001)
|
||||
result = self.execute_file_tool(root, "write_file", {"path": "index.js", "content": "ok"}, True)
|
||||
self.assertEqual(2, result["written"])
|
||||
self.assertEqual({"complete": False, "files": []}, result["fileChanges"])
|
||||
|
||||
def test_workspace_inspection_requires_nonempty_source_and_excludes_dependencies(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root=Path(directory)
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
"""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}
|
||||
Reference in new issue
Block a user