-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodex-worktree-cache.mjs
More file actions
executable file
·338 lines (302 loc) · 8.36 KB
/
codex-worktree-cache.mjs
File metadata and controls
executable file
·338 lines (302 loc) · 8.36 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
#!/usr/bin/env node
import { spawnSync } from "node:child_process";
import {
constants,
copyFileSync,
cpSync,
existsSync,
mkdirSync,
readdirSync,
readFileSync,
renameSync,
rmSync,
statSync,
} from "node:fs";
import { homedir } from "node:os";
import { basename, dirname, join, resolve } from "node:path";
import { createHash } from "node:crypto";
import { fileURLToPath } from "node:url";
const ROOT = repoRoot();
const PROJECT_NAME = basename(ROOT);
const CACHE_ROOT =
process.env.SIMDECK_CODEX_CACHE_ROOT ||
join(homedir(), ".cache", "simdeck", "codex-worktree-cache");
const args = process.argv.slice(2);
const command = args[0] || "hydrate";
const force = args.includes("--force");
const bestEffort = args.includes("--best-effort") || command === "hydrate";
if (!["hydrate", "save", "status"].includes(command)) {
console.error(
"Usage: codex-worktree-cache.mjs <hydrate|save|status> [--force]",
);
process.exit(2);
}
const entries = buildEntries();
try {
if (command === "hydrate") {
hydrateEntries(entries);
} else if (command === "save") {
saveEntries(entries);
} else {
printStatus(entries);
}
} catch (error) {
if (!bestEffort) {
throw error;
}
console.warn(`[cache] ${describeError(error)}`);
}
function hydrateEntries(entries) {
const sourceRoots = cacheSourceRoots();
for (const entry of entries) {
const destination = join(ROOT, entry.destination);
if (existsAndHasContent(destination) && !force) {
log(`skip ${entry.label}; ${entry.destination} already exists`);
continue;
}
const source = findHydrationSource(entry, sourceRoots);
if (!source) {
log(`miss ${entry.label}`);
continue;
}
copyIntoPlace(source.path, destination);
log(`hydrated ${entry.label} from ${source.description}`);
}
}
function saveEntries(entries) {
mkdirSync(CACHE_ROOT, { recursive: true });
for (const entry of entries) {
const source = join(ROOT, entry.destination);
if (!existsAndHasContent(source)) {
log(`skip ${entry.label}; ${entry.destination} is missing`);
continue;
}
copyIntoPlace(source, entry.cachePath, { replace: true });
log(`saved ${entry.label}`);
}
}
function printStatus(entries) {
console.log(`Cache root: ${CACHE_ROOT}`);
for (const entry of entries) {
const destination = join(ROOT, entry.destination);
console.log(
`${entry.label}: destination=${existsAndHasContent(destination) ? "present" : "missing"} cache=${
existsAndHasContent(entry.cachePath) ? "present" : "missing"
}`,
);
}
}
function buildEntries() {
const rootLockHash = hashFiles(["package-lock.json"]);
const clientLockHash = hashFiles(["packages/client/package-lock.json"]);
const cargoHash = hashFiles([
"packages/server/Cargo.toml",
"packages/server/Cargo.lock",
]);
const rustHost = rustHostTriple();
return [
{
label: "root node_modules",
destination: "node_modules",
cachePath: join(CACHE_ROOT, "node", "root", rootLockHash, "node_modules"),
sourceLockFiles: ["package-lock.json"],
},
{
label: "client node_modules",
destination: "packages/client/node_modules",
cachePath: join(
CACHE_ROOT,
"node",
"client",
clientLockHash,
"node_modules",
),
sourceLockFiles: ["packages/client/package-lock.json"],
},
{
label: "Rust target",
destination: "packages/server/target",
cachePath: join(CACHE_ROOT, "rust", rustHost, cargoHash, "target"),
sourceLockFiles: [
"packages/server/Cargo.toml",
"packages/server/Cargo.lock",
],
},
];
}
function findHydrationSource(entry, sourceRoots) {
if (existsAndHasContent(entry.cachePath)) {
return { path: entry.cachePath, description: entry.cachePath };
}
for (const root of sourceRoots) {
if (!locksMatch(root, entry.sourceLockFiles)) {
continue;
}
const candidate = join(root, entry.destination);
if (existsAndHasContent(candidate)) {
return { path: candidate, description: root };
}
}
return null;
}
function cacheSourceRoots() {
const roots = [];
const explicitSource = process.env.SIMDECK_CACHE_SOURCE;
if (explicitSource) {
roots.push(resolve(explicitSource));
}
const commonRoot = mainCheckoutRoot();
if (commonRoot) {
roots.push(commonRoot);
}
const codexWorktrees = join(homedir(), ".codex", "worktrees");
if (existsSync(codexWorktrees)) {
for (const id of readdirSync(codexWorktrees)) {
const candidate = join(codexWorktrees, id, PROJECT_NAME);
if (candidate !== ROOT && existsSync(candidate)) {
roots.push(candidate);
}
}
}
return [...new Set(roots)]
.filter((root) => root !== ROOT && existsSync(root))
.sort((left, right) => mtimeMs(right) - mtimeMs(left));
}
function locksMatch(candidateRoot, lockFiles) {
for (const lockFile of lockFiles) {
const current = join(ROOT, lockFile);
const candidate = join(candidateRoot, lockFile);
if (!existsSync(current) || !existsSync(candidate)) {
return false;
}
if (hashPath(current) !== hashPath(candidate)) {
return false;
}
}
return true;
}
function copyIntoPlace(source, destination, { replace = false } = {}) {
if (replace) {
mkdirSync(dirname(destination), { recursive: true });
} else if (existsSync(destination)) {
if (force) {
rmSync(destination, { recursive: true, force: true });
} else {
return;
}
}
const temporary = `${destination}.tmp-${process.pid}-${Date.now()}`;
rmSync(temporary, { recursive: true, force: true });
mkdirSync(dirname(temporary), { recursive: true });
try {
clonePath(source, temporary);
if (replace) {
rmSync(destination, { recursive: true, force: true });
}
renameSync(temporary, destination);
} catch (error) {
rmSync(temporary, { recursive: true, force: true });
if (!bestEffort) {
throw error;
}
console.warn(`[cache] failed to copy ${source}: ${describeError(error)}`);
}
}
function clonePath(source, destination) {
const stats = statSync(source);
if (stats.isFile()) {
try {
copyFileSync(source, destination, constants.COPYFILE_FICLONE);
} catch {
copyFileSync(source, destination);
}
return;
}
try {
cpSync(source, destination, {
recursive: true,
verbatimSymlinks: true,
preserveTimestamps: true,
mode: constants.COPYFILE_FICLONE,
});
} catch {
cpSync(source, destination, {
recursive: true,
verbatimSymlinks: true,
preserveTimestamps: true,
});
}
}
function existsAndHasContent(path) {
try {
const stats = statSync(path);
if (stats.isDirectory()) {
return readdirSync(path).length > 0;
}
return stats.size > 0;
} catch {
return false;
}
}
function repoRoot() {
const result = spawnSync("git", ["rev-parse", "--show-toplevel"], {
encoding: "utf8",
});
if (result.status === 0) {
return result.stdout.trim();
}
return resolve(dirname(fileURLToPath(import.meta.url)), "..");
}
function mainCheckoutRoot() {
const result = spawnSync(
"git",
["rev-parse", "--path-format=absolute", "--git-common-dir"],
{ cwd: ROOT, encoding: "utf8" },
);
if (result.status !== 0) {
return null;
}
const gitCommonDir = result.stdout.trim();
if (basename(gitCommonDir) !== ".git") {
return null;
}
return dirname(gitCommonDir);
}
function hashFiles(paths) {
const hash = createHash("sha256");
for (const path of paths) {
hash.update(path);
hash.update("\0");
hash.update(readFileSync(join(ROOT, path)));
hash.update("\0");
}
return hash.digest("hex").slice(0, 16);
}
function hashPath(path) {
return createHash("sha256").update(readFileSync(path)).digest("hex");
}
function rustHostTriple() {
const result = spawnSync("rustc", ["-vV"], { encoding: "utf8" });
if (result.status !== 0) {
return `${process.platform}-${process.arch}`;
}
const host = result.stdout
.split("\n")
.find((line) => line.startsWith("host: "))
?.slice("host: ".length)
.trim();
return host || `${process.platform}-${process.arch}`;
}
function mtimeMs(path) {
try {
return statSync(path).mtimeMs;
} catch {
return 0;
}
}
function log(message) {
console.log(`[cache] ${message}`);
}
function describeError(error) {
return error instanceof Error ? error.message : String(error);
}