#!/usr/bin/env python3 """Create a same-volume staging directory for one bootstrap run.""" from __future__ import annotations import argparse import json import re import sys from pathlib import Path RUN_ID = re.compile(r"^[a-z0-9][a-z0-9-]{0,63}$") MARKER = ".bootstrap-staging.json" def configure_stdio() -> None: for stream in (sys.stdout, sys.stderr): if hasattr(stream, "reconfigure"): stream.reconfigure(encoding="utf-8", errors="strict") def is_reparse(path: Path) -> bool: return path.is_symlink() or (hasattr(path, "is_junction") and path.is_junction()) def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument("--repository", required=True) parser.add_argument("--run-id", required=True) return parser.parse_args() def main() -> int: configure_stdio() args = parse_args() if not RUN_ID.fullmatch(args.run_id): raise ValueError("run-id must match [a-z0-9][a-z0-9-]{0,63}") raw_repository = Path(args.repository).absolute() if is_reparse(raw_repository): raise ValueError("repository path must not be a symlink or junction") repository = raw_repository.resolve(strict=True) if not repository.is_dir(): raise ValueError("repository must be a directory") staging = repository.parent / f".{repository.name}.ai-agent-system-{args.run_id}" if staging.exists(): raise FileExistsError(f"staging path already exists: {staging}") staging.mkdir() marker = { "schema_version": 1, "run_id": args.run_id, "repository_root": str(repository), "status": "created", } (staging / MARKER).write_text( json.dumps(marker, ensure_ascii=False, indent=2) + "\n", encoding="utf-8" ) print(json.dumps({"status": "created", "staging_root": str(staging)}, ensure_ascii=False)) return 0 if __name__ == "__main__": try: raise SystemExit(main()) except Exception as exc: configure_stdio() print(json.dumps({"status": "failed", "error": str(exc)}, ensure_ascii=False), file=sys.stderr) raise SystemExit(1)