chunhualin1
2026-07-15 1a1ec5d071834d7720039ca94fc0c357d05d67c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/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)