-
-
Notifications
You must be signed in to change notification settings - Fork 831
Expand file tree
/
Copy pathcodex_j.py
More file actions
360 lines (303 loc) · 10 KB
/
Copy pathcodex_j.py
File metadata and controls
360 lines (303 loc) · 10 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import os
import subprocess
import sys
from pathlib import Path
HOME = Path.home()
DEFAULT_REPO_ROOT = Path(
os.environ.get(
"FLOW_CODEX_J_REPO",
str(HOME / "repos" / "openai" / "codex"),
)
).expanduser()
STATE_DIR = Path(
os.environ.get(
"FLOW_CODEX_FORK_STATE_DIR",
str(HOME / ".flow" / "codex-fork"),
)
).expanduser()
DEFAULT_INFRA_BIN = os.environ.get("FLOW_CODEX_J_INFRA_BIN", "infra")
DEFAULT_BUILDER = os.environ.get("FLOW_CODEX_J_BUILDER", "j-mac-1")
DEFAULT_PROFILE = os.environ.get("FLOW_CODEX_J_PROFILE", "release")
HOME_BRANCH = "j"
TRUNK_BRANCH = "main"
PUBLIC_REMOTE = "origin"
PUSH_REMOTE = "private"
DARWIN_TARGET = "aarch64-apple-darwin"
CODEX_PACKAGE = "codex-cli"
def fail(message: str, code: int = 1) -> int:
print(f"Error: {message}", file=sys.stderr)
return code
def run(
cmd: list[str],
*,
cwd: Path,
capture: bool = False,
check: bool = True,
) -> subprocess.CompletedProcess[str]:
result = subprocess.run(
cmd,
cwd=str(cwd),
text=True,
capture_output=capture,
check=False,
)
if check and result.returncode != 0:
if capture and result.stderr:
print(result.stderr.rstrip(), file=sys.stderr)
raise SystemExit(result.returncode)
return result
def capture(cmd: list[str], *, cwd: Path) -> str:
return run(cmd, cwd=cwd, capture=True).stdout.strip()
def resolve_repo_root(path_arg: str | None) -> Path:
if path_arg:
return Path(path_arg).expanduser().resolve()
return DEFAULT_REPO_ROOT.resolve()
def codex_rs_root(repo_root: Path) -> Path:
return repo_root / "codex-rs"
def managed_binary_path() -> Path:
return STATE_DIR / "bin" / "current" / "codex"
def managed_dev_binary_path() -> Path:
return STATE_DIR / "bin" / "current-dev" / "codex"
def managed_binary_target(path: Path) -> str | None:
if not path.exists():
return None
try:
return str(path.resolve())
except OSError:
return str(path)
def ensure_repo(repo_root: Path) -> None:
if not repo_root.exists():
raise SystemExit(fail(f"repo does not exist: {repo_root}"))
probe = run(
["git", "rev-parse", "--is-inside-work-tree"],
cwd=repo_root,
capture=True,
check=False,
)
if probe.returncode != 0 or probe.stdout.strip() != "true":
raise SystemExit(fail(f"not a git checkout: {repo_root}"))
if not (repo_root / ".jj").exists():
raise SystemExit(fail(f"not a colocated jj checkout: {repo_root}"))
def ensure_branch_exists(repo_root: Path, branch: str) -> None:
result = run(
["git", "show-ref", "--verify", "--quiet", f"refs/heads/{branch}"],
cwd=repo_root,
check=False,
)
if result.returncode != 0:
raise SystemExit(fail(f"branch `{branch}` does not exist in {repo_root}"))
def ensure_clean(repo_root: Path) -> None:
status = capture(["git", "status", "--porcelain"], cwd=repo_root)
if status:
raise SystemExit(
fail(
"home checkout is dirty; finish, squash, or move the work before running `f codex-j-sync`"
)
)
def ensure_home_attached(repo_root: Path) -> None:
branch = capture(["git", "branch", "--show-current"], cwd=repo_root)
if branch == HOME_BRANCH:
return
if branch and branch != HOME_BRANCH:
raise SystemExit(
fail(f"current branch is `{branch}`; switch back to `{HOME_BRANCH}` before syncing")
)
run(["git", "switch", HOME_BRANCH], cwd=repo_root)
def ensure_local_config(repo_root: Path) -> None:
pairs = [
("flow.homeBranch", HOME_BRANCH),
("flow.publicRemote", PUBLIC_REMOTE),
(f"branch.{HOME_BRANCH}.remote", PUBLIC_REMOTE),
(f"branch.{HOME_BRANCH}.merge", f"refs/heads/{TRUNK_BRANCH}"),
(f"branch.{HOME_BRANCH}.pushRemote", PUSH_REMOTE),
("remote.pushDefault", PUSH_REMOTE),
]
for key, value in pairs:
run(["git", "config", "--local", key, value], cwd=repo_root)
def ensure_main_tracks_origin(repo_root: Path) -> None:
run(
["jj", "bookmark", "set", TRUNK_BRANCH, "-r", f"{TRUNK_BRANCH}@{PUBLIC_REMOTE}"],
cwd=repo_root,
)
def conflict_revisions(repo_root: Path) -> str:
return capture(
[
"jj",
"log",
"-r",
"conflicts()",
"--no-graph",
"-T",
"commit_id.short() ++ \" \" ++ description.first_line() ++ \"\\n\"",
],
cwd=repo_root,
)
def show_status(repo_root: Path) -> None:
managed_bin = managed_binary_path()
print(f"managed_binary={managed_bin}", flush=True)
target = managed_binary_target(managed_bin)
if target:
print(f"managed_binary_target={target}", flush=True)
else:
print("managed_binary_target=(missing)", flush=True)
managed_dev_bin = managed_dev_binary_path()
print(f"managed_dev_binary={managed_dev_bin}", flush=True)
dev_target = managed_binary_target(managed_dev_bin)
if dev_target:
print(f"managed_dev_binary_target={dev_target}", flush=True)
else:
print("managed_dev_binary_target=(missing)", flush=True)
run(["flow", "status"], cwd=repo_root)
def build(
repo_root: Path,
*,
builder: str | None,
profile: str,
release: bool,
install_as: str,
push_cache: bool,
) -> None:
ensure_repo(repo_root)
cargo_root = codex_rs_root(repo_root)
if not cargo_root.exists():
raise SystemExit(fail(f"missing codex-rs workspace under {repo_root}"))
cmd = [
DEFAULT_INFRA_BIN,
"build",
"rust",
str(cargo_root),
"--package",
CODEX_PACKAGE,
"--bin",
"codex",
"--target",
DARWIN_TARGET,
"--sign-local",
"--install-as",
install_as,
]
resolved_builder = builder or DEFAULT_BUILDER
if resolved_builder:
cmd.extend(["--builder", resolved_builder])
if release or profile == "release":
cmd.append("--release")
else:
cmd.extend(["--profile", profile])
if push_cache:
cmd.append("--push-cache")
run(cmd, cwd=repo_root)
def sync(repo_root: Path, push: bool) -> None:
ensure_repo(repo_root)
ensure_branch_exists(repo_root, HOME_BRANCH)
ensure_clean(repo_root)
ensure_home_attached(repo_root)
ensure_local_config(repo_root)
ensure_main_tracks_origin(repo_root)
cmd = ["flow", "jj", "sync", "--bookmark", HOME_BRANCH]
if not push:
cmd.append("--no-push")
run(cmd, cwd=repo_root)
conflicts = conflict_revisions(repo_root)
if conflicts:
raise SystemExit(
fail(
"jj sync completed with conflicts still present:\n" + conflicts.rstrip()
)
)
run(["jj", "new", HOME_BRANCH], cwd=repo_root)
run(["git", "switch", HOME_BRANCH], cwd=repo_root)
show_status(repo_root)
def main() -> int:
parser = argparse.ArgumentParser(
description="Manage the personal `j` home-branch workflow for ~/repos/openai/codex"
)
subparsers = parser.add_subparsers(dest="command", required=True)
status_parser = subparsers.add_parser(
"status",
help="Show Flow status for the Codex `j` home-branch checkout",
)
status_parser.add_argument(
"--repo-root",
help=f"Repo root (default: {DEFAULT_REPO_ROOT})",
)
sync_parser = subparsers.add_parser(
"sync",
help="Sync origin/main into `j`, then normalize the JJ/Git checkout back onto `j`",
)
sync_parser.add_argument(
"--repo-root",
help=f"Repo root (default: {DEFAULT_REPO_ROOT})",
)
sync_parser.add_argument(
"--push",
action="store_true",
help="Push `j` to the configured push remote after sync",
)
build_parser = subparsers.add_parser(
"build",
help="Build Codex remotely for Darwin, sign locally, and promote it for `j` or `jdev`",
)
build_parser.add_argument(
"--repo-root",
help=f"Repo root (default: {DEFAULT_REPO_ROOT})",
)
build_parser.add_argument(
"--builder",
help="Named infra builder to use (defaults to FLOW_CODEX_J_BUILDER or infra default builder)",
)
build_parser.add_argument(
"--profile",
default=DEFAULT_PROFILE,
help=f"Cargo profile for the remote build (default: {DEFAULT_PROFILE})",
)
build_parser.add_argument(
"--release",
action="store_true",
help="Force the release profile",
)
build_parser.add_argument(
"--push-cache",
action="store_true",
help="Push the built artifact into the configured infra cache",
)
build_parser.add_argument(
"--dev",
action="store_true",
help="Build the dev/debug profile and install it as `jdev`",
)
build_parser.add_argument(
"--install-as",
choices=["j", "jdev"],
help="Managed install slot to update (defaults to `j`, or `jdev` with --dev)",
)
args = parser.parse_args()
repo_root = resolve_repo_root(getattr(args, "repo_root", None))
if args.command == "status":
ensure_repo(repo_root)
ensure_branch_exists(repo_root, HOME_BRANCH)
ensure_local_config(repo_root)
show_status(repo_root)
return 0
if args.command == "sync":
sync(repo_root, push=args.push)
return 0
if args.command == "build":
install_as = args.install_as or ("jdev" if args.dev else "j")
profile = args.profile
if args.dev and profile == DEFAULT_PROFILE and not args.release:
profile = "dev"
build(
repo_root,
builder=args.builder,
profile=profile,
release=args.release,
install_as=install_as,
push_cache=args.push_cache,
)
return 0
raise SystemExit(fail(f"unsupported command: {args.command}"))
if __name__ == "__main__":
raise SystemExit(main())