56 lines
2.8 KiB
Python
56 lines
2.8 KiB
Python
"""Refresh an owned source snapshot without commits or Git branch mutations."""
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
|
|
root = Path(os.environ['ROBORTO_PROJECT_ROOT']).resolve()
|
|
task = os.environ['ROBORTO_TASK_ID']
|
|
if len(task) != 3 or not task.isdecimal():
|
|
raise SystemExit('Invalid task ID')
|
|
snapshot = root / '.test-worktrees' / f'task-{task}'
|
|
if Path(os.environ['ROBORTO_WORKTREE']) != snapshot or snapshot.is_symlink():
|
|
raise SystemExit('Unexpected snapshot path')
|
|
marker = snapshot / '.snapshot.json'
|
|
if snapshot.exists():
|
|
if not marker.is_file() or json.loads(marker.read_text()).get('source') != str(root):
|
|
raise SystemExit('Refusing to replace an unowned snapshot')
|
|
# Detect manual edits before refreshing; snapshots are not implementation worktrees.
|
|
prior = json.loads(marker.read_text())['files']
|
|
for name, digest in prior.items():
|
|
target = snapshot / name
|
|
if not target.is_file() or hashlib.sha256(target.read_bytes()).hexdigest() != digest:
|
|
raise SystemExit(f'Snapshot changed; preserve/reconcile it before refresh: {name}')
|
|
expected = set(prior) | {'.snapshot.json'}
|
|
actual = {str(p.relative_to(snapshot)) for p in snapshot.rglob('*') if p.is_file() and 'node_modules' not in p.relative_to(snapshot).parts}
|
|
if actual - expected:
|
|
raise SystemExit('Snapshot has extra files; preserve/reconcile before refresh')
|
|
shutil.rmtree(snapshot)
|
|
names = subprocess.check_output(['git', 'ls-files', '-co', '--exclude-standard', '-z'], cwd=root).decode().split('\0')
|
|
# Explicit application/context allowlist: never copy local databases, env files or credentials.
|
|
root_files = {'AGENTS.md', '.gitignore', 'index.html', 'package.json', 'package-lock.json', 'vite.config.js', 'eslint.config.js', 'README.md'}
|
|
files = {}
|
|
for name in sorted(set(names) - {''}):
|
|
path = root / name
|
|
parts = Path(name).parts
|
|
if name not in root_files and parts[0] not in {'src', 'public', 'docs', 'scripts', '.roborto'}:
|
|
continue
|
|
if any(part.startswith('.env') or part in {'credentials', 'secrets'} for part in parts):
|
|
continue
|
|
if path.is_symlink():
|
|
raise SystemExit(f'Refusing source symlink: {name}')
|
|
if not path.is_file():
|
|
continue
|
|
target = snapshot / name
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
shutil.copy2(path, target)
|
|
digest = hashlib.sha256(path.read_bytes()).hexdigest()
|
|
if hashlib.sha256(target.read_bytes()).hexdigest() != digest:
|
|
raise SystemExit(f'Snapshot mismatch: {name}')
|
|
files[name] = digest
|
|
(snapshot / 'node_modules').symlink_to(root / 'node_modules', target_is_directory=True)
|
|
marker.write_text(json.dumps({'source': str(root), 'task': task, 'files': files}, indent=2) + '\n')
|
|
print(f'Verified {len(files)} source files in {snapshot}; node_modules shared, browser data separate.')
|