1#!/usr/bin/env python3
2"""Manage shared projects and run read-only tools beside their ZFS storage."""
3
4import argparse
5from contextlib import ExitStack
6import fcntl
7import json
8import os
9from pathlib import Path
10import re
11import subprocess
12import sys
13import time
14
15sys.path.insert(0, str(Path(__file__).resolve().parent))
16import client
17
18
19def component(value):
20 if (
21 not value
22 or value in (".", "..")
23 or value != value.strip()
24 or any(c in value for c in "/\\")
25 or any(ord(c) < 32 or ord(c) == 127 for c in value)
26 ):
27 raise argparse.ArgumentTypeError("Use one folder name without slashes or control characters.")
28 return value
29
30
31def main():
32 parser = argparse.ArgumentParser(
33 description=__doc__,
34 epilog="Prints the checkout path on success; jj progress goes to stderr. "
35 "PROJECT_ROOT overrides the project directory, which must already exist.",
36 )
37 commands = parser.add_subparsers(dest="command", required=True)
38 read = commands.add_parser("read", help="Search, list, or hash shared files on the storage host")
39 read.add_argument("tool", choices=("rg", "sha256sum"))
40 read.add_argument("args", nargs=argparse.REMAINDER)
41 clone = commands.add_parser("clone", help="Clone a Git repository into PROJECT/main")
42 clone.add_argument("source", metavar="URL")
43 clone.add_argument("project", type=component, metavar="PROJECT")
44 worktree = commands.add_parser("worktree", help="Create a workspace and install pnpm dependencies")
45 worktree.add_argument("project", type=component, metavar="PROJECT")
46 worktree.add_argument("identity", type=component, metavar="NAME")
47 worktree.add_argument("--from", dest="revision", default="trunk()", metavar="REV",
48 help="Parent revision, resolved in main (default: trunk())")
49 worktree.add_argument("--warm-from", type=component, metavar="WORKSPACE",
50 help="Clone an idle workspace and its build outputs on the ZFS host")
51 worktree.add_argument("--target", action="append", metavar="PATH",
52 help="Target directory relative to each workspace; repeat as needed (default: target)")
53 worktree.add_argument("--no-install", action="store_true", help="Create the workspace without installing dependencies")
54 args = parser.parse_args()
55
56 root = Path(os.environ.get("PROJECT_ROOT", "/Volumes/Code/project")).expanduser().absolute()
57 if "PROJECT_ROOT" not in os.environ and not os.path.ismount("/Volumes/Code"):
58 parser.error("/Volumes/Code is not mounted. Restore the mount before creating a checkout.")
59 if not root.is_dir():
60 parser.error(f"Project directory is missing: {root}")
61 if args.command == "read":
62 cwd = client.inside(Path.cwd())
63 if not client.host():
64 parser.error("Storage access is unavailable. Set PROJECT_STORE_SSH to its SSH alias.")
65 return client.request(dict(command="read", cwd=str(cwd), tool=args.tool, args=args.args),
66 capture=False).returncode
67 project = root / args.project
68 if project.is_symlink():
69 parser.error(f"Project directory is a symlink: {project}")
70 destination = project / ("main" if args.command == "clone" else args.identity)
71 if destination.exists() or destination.is_symlink():
72 parser.error(f"Checkout already exists: {destination}. Choose another name.")
73 if args.command == "clone":
74 if project.exists():
75 parser.error(f"Project already exists: {project}. Use project worktree or another name.")
76 command = ["jj", "git", "clone", "--no-colocate", "--", args.source, str(destination)]
77 else:
78 if args.identity in ("main", "default"):
79 parser.error("main and default are reserved. Choose another worktree name.")
80 primary = project / "main"
81 if primary.is_symlink() or not (primary / ".jj").is_dir():
82 parser.error(f"No primary Jujutsu checkout at {primary}. Clone the project first.")
83 command = ["jj", "-R", str(primary), "workspace", "add", "--name", args.identity,
84 "-r", args.revision, "--", str(destination)]
85
86 if "PROJECT_STORE_SSH" not in os.environ and (Path.home() / ".ssh/project-store").is_file():
87 os.environ["PROJECT_STORE_SSH"] = "project-store"
88 with ExitStack() as locks:
89 warm = args.command == "worktree" and args.warm_from
90 if args.command == "worktree" and args.target and not warm:
91 parser.error("--target requires --warm-from.")
92 if warm:
93 host = os.environ.get("PROJECT_STORE_SSH", "")
94 if not re.fullmatch(r"[a-zA-Z0-9][a-zA-Z0-9_.-]*", host):
95 parser.error("Set PROJECT_STORE_SSH to the SSH alias for the project-store forced command.")
96 if os.environ.get("CARGO_TARGET_DIR") or os.environ.get("CARGO_BUILD_TARGET_DIR"):
97 parser.error("Unset the Cargo target override before seeding worktree-local targets.")
98 donor = project / args.warm_from
99 if donor.is_symlink() or not (donor / ".jj").is_dir():
100 parser.error(f"No donor workspace at {donor}.")
101 targets = args.target or ["target"]
102 for target in targets:
103 path = Path(target)
104 if (path.is_absolute() or not path.parts or
105 any(p in ("..", ".jj", ".git") for p in path.parts)):
106 parser.error("Use target paths inside the workspace.")
107 source = donor / path
108 if not source.is_dir() or source.resolve() != source.absolute():
109 parser.error(f"Target is missing or follows a symlink: {source}")
110 lock_paths = sorted(set(source.glob("*/.cargo-lock")) |
111 set(source.glob("*/*/.cargo-lock")))
112 if not lock_paths:
113 parser.error(f"No Cargo build locks in {source}. Build the donor first.")
114 for lock_path in lock_paths:
115 if lock_path.resolve() != lock_path.absolute():
116 parser.error(f"Cargo build lock follows a symlink: {lock_path}")
117 fd = os.open(lock_path, os.O_RDWR | os.O_NOFOLLOW)
118 locks.callback(os.close, fd)
119 try:
120 fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
121 except BlockingIOError:
122 parser.error(f"Donor is building: {source}. Retry after the build finishes.")
123 return create(parser, args, project, destination, command, warm)
124
125
126def create(parser, args, project, destination, command, warm):
127 memory = Path.home() / ".local/share/agents/memory" / args.project
128 slug = re.sub(r"[^a-zA-Z0-9_-]", "-", str(destination))
129 memory_link = Path.home() / ".claude/projects" / slug / "memory"
130 if os.path.lexists(memory_link) and memory_link.resolve() != memory.resolve():
131 parser.error(f"Another memory directory owns {memory_link}. Resolve it before creating this checkout.")
132 if args.command == "clone":
133 project.mkdir()
134 host = os.environ.get("PROJECT_STORE_SSH")
135 if warm:
136 request = dict(project=args.project, source=args.warm_from, destination=args.identity,
137 revision=args.revision, targets=args.target or ["target"])
138 started = time.monotonic()
139 result = subprocess.run(["ssh", "-T", "-o", "BatchMode=yes", "--", host],
140 input=json.dumps(request), text=True, stdout=subprocess.PIPE)
141 if result.returncode:
142 return result.returncode
143 print(f"project: Host setup finished in {time.monotonic() - started:.1f}s.", file=sys.stderr)
144 try:
145 response = json.loads(result.stdout)
146 if not isinstance(response, dict) or response.get("workspace") != args.identity:
147 raise ValueError()
148 except (ValueError, TypeError):
149 print(f"project: Invalid storage response. Inspect {destination} before building.", file=sys.stderr)
150 return 1
151 client.wait_workspace(destination)
152 else:
153 result = subprocess.run(command, stdout=sys.stderr)
154 if result.returncode:
155 if args.command == "clone":
156 try:
157 project.rmdir()
158 except OSError:
159 pass
160 return result.returncode
161
162 memory.mkdir(parents=True, exist_ok=True, mode=0o700)
163 if not (memory / "MEMORY.md").exists():
164 (memory / "MEMORY.md").touch(mode=0o600)
165 memory_link.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
166 if not os.path.lexists(memory_link):
167 memory_link.symlink_to(os.path.relpath(memory, memory_link.parent), target_is_directory=True)
168 if args.command == "worktree" and not args.no_install:
169 manifest = destination / "package.json"
170 if manifest.is_file():
171 package = json.loads(manifest.read_text())
172 if package.get("packageManager", "").startswith("pnpm@"):
173 subprocess.run(["pnpm", "install", "--frozen-lockfile"], cwd=destination,
174 stdout=sys.stderr, check=True)
175 print(destination)
176 return 0
177
178
179if __name__ == "__main__":
180 try:
181 sys.exit(main())
182 except (OSError, ValueError, subprocess.CalledProcessError) as error:
183 print(f"project: {error}", file=sys.stderr)
184 sys.exit(1)