sphica 0.6.42 → 0.6.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +7 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +3 -1
- package/dist/capture.js +311 -164
- package/dist/cli.js +501 -343
- package/dist/deliver.js +1078 -541
- package/dist/git-worker.js +365 -0
- package/dist/mcp-record.js +489 -333
- package/dist/mcp.js +359 -232
- package/hooks/codex.json +11 -0
- package/hooks/hooks.json +28 -0
- package/package.json +1 -1
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// server/src/git-worker.ts
|
|
2
|
+
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
3
|
+
import fs2 from "node:fs";
|
|
4
|
+
import os2 from "node:os";
|
|
5
|
+
import path2 from "node:path";
|
|
6
|
+
|
|
7
|
+
// server/src/git.ts
|
|
8
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import os from "node:os";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
var gitHome = () => path.join(os.homedir(), ".sphica", "git");
|
|
13
|
+
function gitEnv(extra = {}) {
|
|
14
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([k]) => !/^GIT_/i.test(k)));
|
|
15
|
+
return {
|
|
16
|
+
...env,
|
|
17
|
+
GIT_ALLOW_PROTOCOL: "",
|
|
18
|
+
GIT_NO_LAZY_FETCH: "1",
|
|
19
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
20
|
+
GIT_NO_REPLACE_OBJECTS: "1",
|
|
21
|
+
...extra
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
var gitOptions = (hooks = path.join(gitHome(), "hooks")) => [
|
|
25
|
+
"-c",
|
|
26
|
+
"core.fsmonitor=",
|
|
27
|
+
"-c",
|
|
28
|
+
`core.hooksPath=${hooks}`,
|
|
29
|
+
"-c",
|
|
30
|
+
"core.quotePath=false",
|
|
31
|
+
"--no-pager",
|
|
32
|
+
"--no-optional-locks"
|
|
33
|
+
];
|
|
34
|
+
var DIFF_OPTIONS = [
|
|
35
|
+
"--no-color",
|
|
36
|
+
"--no-ext-diff",
|
|
37
|
+
"--no-textconv",
|
|
38
|
+
"--ignore-submodules=dirty",
|
|
39
|
+
"--submodule=short"
|
|
40
|
+
];
|
|
41
|
+
function startDir() {
|
|
42
|
+
const home = os.homedir();
|
|
43
|
+
return fs.statSync(home, { throwIfNoEntry: false })?.isDirectory() ? home : path.dirname(process.execPath);
|
|
44
|
+
}
|
|
45
|
+
function run(root, args, { max = 1024 * 1024, timeout = 1e4 } = {}) {
|
|
46
|
+
return execFileSync("git", ["-C", root, ...gitOptions(), ...args], {
|
|
47
|
+
cwd: startDir(),
|
|
48
|
+
env: gitEnv(),
|
|
49
|
+
maxBuffer: max,
|
|
50
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
51
|
+
timeout,
|
|
52
|
+
killSignal: "SIGKILL"
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
var text = (root, args, limits) => run(root, args, limits).toString("utf8");
|
|
56
|
+
function revision(rev) {
|
|
57
|
+
if (!rev || rev.startsWith("-") || /[\0\n]/.test(rev))
|
|
58
|
+
throw new Error(`not a revision: ${JSON.stringify(rev)}`);
|
|
59
|
+
return rev;
|
|
60
|
+
}
|
|
61
|
+
var commitOf = (root, rev, limits) => text(root, ["rev-parse", "--verify", "-q", `${revision(rev)}^{commit}`], limits).trim();
|
|
62
|
+
var gitPath = (root, name, limits) => path.resolve(root, text(root, ["rev-parse", "--git-path", name], limits).replace(/\n$/, ""));
|
|
63
|
+
var commonDir = (root, limits) => path.resolve(root, text(root, ["rev-parse", "--git-common-dir"], limits).replace(/\n$/, ""));
|
|
64
|
+
function unbornHead(root, limits) {
|
|
65
|
+
let ref;
|
|
66
|
+
try {
|
|
67
|
+
ref = text(root, ["symbolic-ref", "-q", "HEAD"], limits).trim();
|
|
68
|
+
} catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
run(root, ["show-ref", "--verify", "-q", "--", revision(ref)], limits);
|
|
73
|
+
return false;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
return e.status === 1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
var SAFE_KEYS = {
|
|
79
|
+
"core.ignorecase": "bool",
|
|
80
|
+
"core.precomposeunicode": "bool",
|
|
81
|
+
"core.filemode": "bool",
|
|
82
|
+
"core.symlinks": "bool",
|
|
83
|
+
"core.trustctime": "bool",
|
|
84
|
+
"core.sparseCheckout": "bool",
|
|
85
|
+
"core.sparseCheckoutCone": "bool",
|
|
86
|
+
"index.sparse": "bool",
|
|
87
|
+
"core.autocrlf": "bool-or-input",
|
|
88
|
+
"core.eol": "text",
|
|
89
|
+
"core.checkStat": "text",
|
|
90
|
+
"extensions.objectFormat": "text",
|
|
91
|
+
"core.excludesFile": "path"
|
|
92
|
+
};
|
|
93
|
+
function configGet(root, key, limits) {
|
|
94
|
+
const type = SAFE_KEYS[key];
|
|
95
|
+
if (type === "bool-or-input")
|
|
96
|
+
try {
|
|
97
|
+
return text(root, ["config", "--type=bool", "--get", key], limits).replace(/\n$/, "");
|
|
98
|
+
} catch {}
|
|
99
|
+
try {
|
|
100
|
+
return text(root, ["config", ...type === "bool" || type === "path" ? [`--type=${type}`] : [], "--get", key], limits).replace(/\n$/, "");
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// server/src/git-worker.ts
|
|
107
|
+
var INDEX_LIMIT = 256 * 1024 * 1024;
|
|
108
|
+
var FILE_LIMIT = 1024 * 1024;
|
|
109
|
+
var STALE_MS = 60 * 60 * 1000;
|
|
110
|
+
var inside = (root, p) => {
|
|
111
|
+
const rel = path2.relative(root, p);
|
|
112
|
+
return rel === "" || rel !== ".." && !rel.startsWith(`..${path2.sep}`) && !path2.isAbsolute(rel);
|
|
113
|
+
};
|
|
114
|
+
var overlap = (a, b) => inside(a, b) || inside(b, a);
|
|
115
|
+
function ownDir(parent, name) {
|
|
116
|
+
const dir = path2.join(parent, name);
|
|
117
|
+
if (!fs2.existsSync(dir))
|
|
118
|
+
fs2.mkdirSync(dir, { mode: 448 });
|
|
119
|
+
const st = fs2.lstatSync(dir);
|
|
120
|
+
if (!st.isDirectory() || st.isSymbolicLink())
|
|
121
|
+
throw new Error(`${dir} is not a plain directory`);
|
|
122
|
+
return dir;
|
|
123
|
+
}
|
|
124
|
+
function isolatedHome(root) {
|
|
125
|
+
const home = fs2.realpathSync(os2.homedir());
|
|
126
|
+
const git = ownDir(ownDir(home, ".sphica"), "git");
|
|
127
|
+
const hooks = ownDir(git, "hooks");
|
|
128
|
+
if (fs2.readdirSync(hooks).length)
|
|
129
|
+
throw new Error(`${hooks} is not empty`);
|
|
130
|
+
const iso = fs2.mkdtempSync(path2.join(git, "iso-"));
|
|
131
|
+
const st = fs2.lstatSync(iso);
|
|
132
|
+
if (!st.isDirectory() || st.isSymbolicLink())
|
|
133
|
+
throw new Error(`${iso} is not a plain directory`);
|
|
134
|
+
const real = fs2.realpathSync(iso);
|
|
135
|
+
if (!inside(home, real) || real !== iso)
|
|
136
|
+
throw new Error(`${iso} leads out of HOME`);
|
|
137
|
+
for (const other of [root, os2.tmpdir()]) {
|
|
138
|
+
const otherReal = fs2.realpathSync(other);
|
|
139
|
+
if (overlap(real, otherReal) || overlap(git, otherReal))
|
|
140
|
+
throw new Error(`${git} overlaps ${otherReal}, which an agent can write`);
|
|
141
|
+
}
|
|
142
|
+
return { git, iso, hooks };
|
|
143
|
+
}
|
|
144
|
+
function sweep(git, now = Date.now()) {
|
|
145
|
+
let names;
|
|
146
|
+
try {
|
|
147
|
+
names = fs2.readdirSync(git).filter((n) => n.startsWith("iso-"));
|
|
148
|
+
} catch {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
for (const name of names) {
|
|
152
|
+
const dir = path2.join(git, name);
|
|
153
|
+
try {
|
|
154
|
+
const st = fs2.lstatSync(dir);
|
|
155
|
+
if (st.isDirectory() && !st.isSymbolicLink() && now - st.mtimeMs > STALE_MS)
|
|
156
|
+
fs2.rmSync(dir, { recursive: true, force: true });
|
|
157
|
+
} catch {}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
var NO_FOLLOW = fs2.constants.O_NOFOLLOW ?? 0;
|
|
161
|
+
var NON_BLOCK = fs2.constants.O_NONBLOCK ?? 0;
|
|
162
|
+
function readLimited(file, limit) {
|
|
163
|
+
return readStamped(file, limit)?.bytes ?? null;
|
|
164
|
+
}
|
|
165
|
+
function readStamped(file, limit) {
|
|
166
|
+
let before;
|
|
167
|
+
if (!NO_FOLLOW) {
|
|
168
|
+
before = fs2.lstatSync(file, { throwIfNoEntry: false });
|
|
169
|
+
if (!before)
|
|
170
|
+
return null;
|
|
171
|
+
if (!before.isFile())
|
|
172
|
+
throw new Error(`${file} is not a regular file`);
|
|
173
|
+
}
|
|
174
|
+
let fd;
|
|
175
|
+
try {
|
|
176
|
+
fd = fs2.openSync(file, fs2.constants.O_RDONLY | NO_FOLLOW | NON_BLOCK);
|
|
177
|
+
} catch (e) {
|
|
178
|
+
if (e.code === "ENOENT")
|
|
179
|
+
return null;
|
|
180
|
+
throw new Error(`${file} cannot be opened as a regular file`);
|
|
181
|
+
}
|
|
182
|
+
try {
|
|
183
|
+
const st = fs2.fstatSync(fd);
|
|
184
|
+
if (!st.isFile())
|
|
185
|
+
throw new Error(`${file} is not a regular file`);
|
|
186
|
+
if (before && (st.dev !== before.dev || st.ino !== before.ino))
|
|
187
|
+
throw new Error(`${file} changed while opened`);
|
|
188
|
+
if (st.size > limit)
|
|
189
|
+
throw new Error(`${file} is over ${limit} bytes`);
|
|
190
|
+
const buf = Buffer.alloc(limit + 1);
|
|
191
|
+
let got = 0;
|
|
192
|
+
for (;; ) {
|
|
193
|
+
const n = fs2.readSync(fd, buf, got, buf.length - got, null);
|
|
194
|
+
if (n === 0)
|
|
195
|
+
break;
|
|
196
|
+
got += n;
|
|
197
|
+
if (got > limit)
|
|
198
|
+
throw new Error(`${file} is over ${limit} bytes`);
|
|
199
|
+
}
|
|
200
|
+
return { bytes: buf.subarray(0, got), mtime: st.mtime };
|
|
201
|
+
} finally {
|
|
202
|
+
fs2.closeSync(fd);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
var normalized = (value) => value.trim().toLowerCase();
|
|
206
|
+
function isolatedConfig(values) {
|
|
207
|
+
const lines = ["[core]", "\tbare = false", "\tquotePath = false"];
|
|
208
|
+
const extensions = [];
|
|
209
|
+
const allowed = {
|
|
210
|
+
"core.autocrlf": ["true", "false", "input"],
|
|
211
|
+
"core.eol": ["lf", "crlf", "native"],
|
|
212
|
+
"core.checkStat": ["default", "minimal"],
|
|
213
|
+
"extensions.objectFormat": ["sha1", "sha256"]
|
|
214
|
+
};
|
|
215
|
+
const sections = { core: lines, index: [] };
|
|
216
|
+
for (const [key, type] of Object.entries(SAFE_KEYS)) {
|
|
217
|
+
const value = values[key];
|
|
218
|
+
if (value === null || value === undefined || type === "path")
|
|
219
|
+
continue;
|
|
220
|
+
const normal = normalized(value);
|
|
221
|
+
const ok = type === "bool" ? ["true", "false"] : allowed[key] ?? [];
|
|
222
|
+
if (!ok.includes(normal))
|
|
223
|
+
throw new Error(`${key} has a value Sphica does not copy: ${JSON.stringify(value.slice(0, 40))}`);
|
|
224
|
+
const [section, name] = key.split(".");
|
|
225
|
+
if (section === "extensions")
|
|
226
|
+
extensions.push(` ${name} = ${normal}`);
|
|
227
|
+
else
|
|
228
|
+
sections[section]?.push(` ${name} = ${normal}`);
|
|
229
|
+
}
|
|
230
|
+
const sha256 = values["extensions.objectFormat"] === "sha256";
|
|
231
|
+
lines.splice(1, 0, ` repositoryformatversion = ${sha256 ? 1 : 0}`);
|
|
232
|
+
return [
|
|
233
|
+
...lines,
|
|
234
|
+
...sections.index?.length ? ["[index]", ...sections.index] : [],
|
|
235
|
+
...sha256 ? ["[extensions]", ...extensions] : [],
|
|
236
|
+
""
|
|
237
|
+
].join(`
|
|
238
|
+
`);
|
|
239
|
+
}
|
|
240
|
+
function excludesFile(root, limits) {
|
|
241
|
+
const set = configGet(root, "core.excludesFile", limits);
|
|
242
|
+
if (set === "")
|
|
243
|
+
return null;
|
|
244
|
+
if (set)
|
|
245
|
+
return set.startsWith("~/") ? path2.join(os2.homedir(), set.slice(2)) : path2.resolve(root, set);
|
|
246
|
+
const xdg = process.env.XDG_CONFIG_HOME;
|
|
247
|
+
return path2.join(xdg && path2.isAbsolute(xdg) ? xdg : path2.join(os2.homedir(), ".config"), "git", "ignore");
|
|
248
|
+
}
|
|
249
|
+
function prepare(root, iso, hooks, remaining) {
|
|
250
|
+
for (const dir of ["refs", "objects", "info"])
|
|
251
|
+
fs2.mkdirSync(path2.join(iso, dir));
|
|
252
|
+
let head;
|
|
253
|
+
try {
|
|
254
|
+
head = commitOf(root, "HEAD", remaining());
|
|
255
|
+
} catch (e) {
|
|
256
|
+
if (!unbornHead(root, remaining()))
|
|
257
|
+
throw e;
|
|
258
|
+
head = "ref: refs/heads/sphica-unborn";
|
|
259
|
+
}
|
|
260
|
+
fs2.writeFileSync(path2.join(iso, "HEAD"), `${head}
|
|
261
|
+
`);
|
|
262
|
+
const values = Object.fromEntries(Object.keys(SAFE_KEYS).map((k) => [k, configGet(root, k, remaining())]));
|
|
263
|
+
fs2.writeFileSync(path2.join(iso, "config"), isolatedConfig(values));
|
|
264
|
+
const index = gitPath(root, "index", remaining());
|
|
265
|
+
const copyIndex = (from, name) => {
|
|
266
|
+
const got = readStamped(from, INDEX_LIMIT);
|
|
267
|
+
if (!got)
|
|
268
|
+
return;
|
|
269
|
+
fs2.writeFileSync(path2.join(iso, name), got.bytes);
|
|
270
|
+
fs2.utimesSync(path2.join(iso, name), got.mtime, got.mtime);
|
|
271
|
+
};
|
|
272
|
+
copyIndex(index, "index");
|
|
273
|
+
for (const name of fs2.readdirSync(path2.dirname(index)).filter((n) => /^sharedindex\.[0-9a-f]{40,64}$/.test(n)))
|
|
274
|
+
copyIndex(path2.join(path2.dirname(index), name), name);
|
|
275
|
+
for (const name of ["info/exclude", "info/attributes", "info/sparse-checkout"]) {
|
|
276
|
+
const bytes = readLimited(gitPath(root, name, remaining()), FILE_LIMIT);
|
|
277
|
+
if (bytes)
|
|
278
|
+
fs2.writeFileSync(path2.join(iso, name), bytes);
|
|
279
|
+
}
|
|
280
|
+
const ignore = path2.join(iso, "global-ignore");
|
|
281
|
+
const global = excludesFile(root, remaining());
|
|
282
|
+
fs2.writeFileSync(ignore, (global && readLimited(global, FILE_LIMIT)) ?? "");
|
|
283
|
+
return {
|
|
284
|
+
env: gitEnv({
|
|
285
|
+
GIT_DIR: iso,
|
|
286
|
+
GIT_WORK_TREE: root,
|
|
287
|
+
GIT_OBJECT_DIRECTORY: path2.join(commonDir(root, remaining()), "objects"),
|
|
288
|
+
GIT_CONFIG_NOSYSTEM: "1",
|
|
289
|
+
GIT_CONFIG_GLOBAL: "/dev/null"
|
|
290
|
+
}),
|
|
291
|
+
options: [...gitOptions(hooks), "-c", `core.excludesFile=${ignore}`]
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function argsOf(op) {
|
|
295
|
+
const oid = (s) => {
|
|
296
|
+
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(s))
|
|
297
|
+
throw new Error(`not an object id: ${JSON.stringify(s)}`);
|
|
298
|
+
return s;
|
|
299
|
+
};
|
|
300
|
+
if (op.kind === "status")
|
|
301
|
+
return ["status", "--porcelain=v2", "-z", "--untracked-files=all", "--ignore-submodules=dirty"];
|
|
302
|
+
if (op.kind === "diff")
|
|
303
|
+
return [
|
|
304
|
+
"diff",
|
|
305
|
+
...DIFF_OPTIONS,
|
|
306
|
+
"--no-renames",
|
|
307
|
+
"--src-prefix=a/",
|
|
308
|
+
"--dst-prefix=b/",
|
|
309
|
+
oid(op.from),
|
|
310
|
+
"--"
|
|
311
|
+
];
|
|
312
|
+
return ["diff", ...DIFF_OPTIONS, "-M", "-l1000", "--name-status", "-z", oid(op.commit), "--"];
|
|
313
|
+
}
|
|
314
|
+
function work(request) {
|
|
315
|
+
try {
|
|
316
|
+
const { git, iso, hooks } = isolatedHome(request.root);
|
|
317
|
+
sweep(git);
|
|
318
|
+
const remaining = () => ({ timeout: Math.max(1, Math.floor(request.until - Date.now())) });
|
|
319
|
+
try {
|
|
320
|
+
const { env, options } = prepare(request.root, iso, hooks, remaining);
|
|
321
|
+
const out = request.ops.flatMap((op) => {
|
|
322
|
+
const run2 = (args) => execFileSync2("git", ["-C", request.root, ...options, ...args], {
|
|
323
|
+
cwd: startDir(),
|
|
324
|
+
env,
|
|
325
|
+
...remaining(),
|
|
326
|
+
killSignal: "SIGKILL",
|
|
327
|
+
encoding: "utf8",
|
|
328
|
+
maxBuffer: request.max,
|
|
329
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
330
|
+
});
|
|
331
|
+
if (op.kind === "diff")
|
|
332
|
+
return [
|
|
333
|
+
run2(argsOf(op)),
|
|
334
|
+
run2(["diff", ...DIFF_OPTIONS, "--no-renames", "--name-only", "-z", op.from, "--"])
|
|
335
|
+
];
|
|
336
|
+
return [run2(argsOf(op))];
|
|
337
|
+
});
|
|
338
|
+
return { ok: true, out };
|
|
339
|
+
} finally {
|
|
340
|
+
fs2.rmSync(iso, { recursive: true, force: true });
|
|
341
|
+
}
|
|
342
|
+
} catch (e) {
|
|
343
|
+
return { ok: false, error: e.message };
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if (process.argv[1] && path2.resolve(process.argv[1]) === path2.resolve(import.meta.filename ?? "")) {
|
|
347
|
+
const chunks = [];
|
|
348
|
+
process.stdin.on("data", (c) => chunks.push(c));
|
|
349
|
+
process.stdin.on("end", () => {
|
|
350
|
+
let request;
|
|
351
|
+
try {
|
|
352
|
+
request = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
353
|
+
} catch {
|
|
354
|
+
process.stdout.write(JSON.stringify({ ok: false, error: "the request is not JSON" }));
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
process.stdout.write(JSON.stringify(work(request)));
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
export {
|
|
361
|
+
isolatedConfig,
|
|
362
|
+
isolatedHome,
|
|
363
|
+
readLimited,
|
|
364
|
+
sweep
|
|
365
|
+
};
|