sphica 0.6.42 → 0.6.43
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 +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/dist/capture.js +311 -164
- package/dist/cli.js +501 -343
- package/dist/deliver.js +367 -217
- package/dist/git-worker.js +365 -0
- package/dist/mcp-record.js +489 -333
- package/dist/mcp.js +359 -232
- package/package.json +1 -1
package/dist/capture.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// server/src/capture.ts
|
|
4
|
-
import { spawn } from "node:child_process";
|
|
5
|
-
import
|
|
6
|
-
import
|
|
4
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
5
|
+
import fs6 from "node:fs";
|
|
6
|
+
import path7 from "node:path";
|
|
7
7
|
|
|
8
8
|
// server/node_modules/kysely/dist/util/object-utils.js
|
|
9
9
|
function isUndefined(obj) {
|
|
@@ -10808,14 +10808,14 @@ function adapt(raw) {
|
|
|
10808
10808
|
}
|
|
10809
10809
|
|
|
10810
10810
|
// server/src/sqlite.ts
|
|
10811
|
-
import
|
|
10812
|
-
import
|
|
10813
|
-
import
|
|
10811
|
+
import fs3 from "node:fs";
|
|
10812
|
+
import os2 from "node:os";
|
|
10813
|
+
import path3 from "node:path";
|
|
10814
10814
|
import { constants as C, DatabaseSync } from "node:sqlite";
|
|
10815
10815
|
|
|
10816
10816
|
// server/src/plugin.ts
|
|
10817
|
-
import
|
|
10818
|
-
import
|
|
10817
|
+
import fs2 from "node:fs";
|
|
10818
|
+
import path2 from "node:path";
|
|
10819
10819
|
import { fileURLToPath } from "node:url";
|
|
10820
10820
|
|
|
10821
10821
|
// server/node_modules/smol-toml/dist/error.js
|
|
@@ -11085,6 +11085,151 @@ var NO_MATCHER = new Set(["UserPromptSubmit", "Stop", "Interrupt"]);
|
|
|
11085
11085
|
var CONTEXT = new Set(["PreToolUse", "PostToolUse", "SessionStart", "UserPromptSubmit", "SubagentStart"]);
|
|
11086
11086
|
var SHORT = new Set(["SessionEnd", "Interrupt"]);
|
|
11087
11087
|
|
|
11088
|
+
// server/src/git.ts
|
|
11089
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
11090
|
+
import fs from "node:fs";
|
|
11091
|
+
import os from "node:os";
|
|
11092
|
+
import path from "node:path";
|
|
11093
|
+
var gitHome = () => path.join(os.homedir(), ".sphica", "git");
|
|
11094
|
+
function gitEnv(extra = {}) {
|
|
11095
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([k]) => !/^GIT_/i.test(k)));
|
|
11096
|
+
return {
|
|
11097
|
+
...env,
|
|
11098
|
+
GIT_ALLOW_PROTOCOL: "",
|
|
11099
|
+
GIT_NO_LAZY_FETCH: "1",
|
|
11100
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
11101
|
+
GIT_NO_REPLACE_OBJECTS: "1",
|
|
11102
|
+
...extra
|
|
11103
|
+
};
|
|
11104
|
+
}
|
|
11105
|
+
var gitOptions = (hooks = path.join(gitHome(), "hooks")) => [
|
|
11106
|
+
"-c",
|
|
11107
|
+
"core.fsmonitor=",
|
|
11108
|
+
"-c",
|
|
11109
|
+
`core.hooksPath=${hooks}`,
|
|
11110
|
+
"-c",
|
|
11111
|
+
"core.quotePath=false",
|
|
11112
|
+
"--no-pager",
|
|
11113
|
+
"--no-optional-locks"
|
|
11114
|
+
];
|
|
11115
|
+
var DIFF_OPTIONS = [
|
|
11116
|
+
"--no-color",
|
|
11117
|
+
"--no-ext-diff",
|
|
11118
|
+
"--no-textconv",
|
|
11119
|
+
"--ignore-submodules=dirty",
|
|
11120
|
+
"--submodule=short"
|
|
11121
|
+
];
|
|
11122
|
+
function startDir() {
|
|
11123
|
+
const home = os.homedir();
|
|
11124
|
+
return fs.statSync(home, { throwIfNoEntry: false })?.isDirectory() ? home : path.dirname(process.execPath);
|
|
11125
|
+
}
|
|
11126
|
+
function run(root, args, { max = 1024 * 1024, timeout = 1e4 } = {}) {
|
|
11127
|
+
return execFileSync("git", ["-C", root, ...gitOptions(), ...args], {
|
|
11128
|
+
cwd: startDir(),
|
|
11129
|
+
env: gitEnv(),
|
|
11130
|
+
maxBuffer: max,
|
|
11131
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
11132
|
+
timeout,
|
|
11133
|
+
killSignal: "SIGKILL"
|
|
11134
|
+
});
|
|
11135
|
+
}
|
|
11136
|
+
var text = (root, args, limits) => run(root, args, limits).toString("utf8");
|
|
11137
|
+
var list = (out) => out.split("\x00").filter(Boolean);
|
|
11138
|
+
function revision(rev) {
|
|
11139
|
+
if (!rev || rev.startsWith("-") || /[\0\n]/.test(rev))
|
|
11140
|
+
throw new Error(`not a revision: ${JSON.stringify(rev)}`);
|
|
11141
|
+
return rev;
|
|
11142
|
+
}
|
|
11143
|
+
function objectId(oid) {
|
|
11144
|
+
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(oid))
|
|
11145
|
+
throw new Error(`not an object id: ${JSON.stringify(oid)}`);
|
|
11146
|
+
return oid;
|
|
11147
|
+
}
|
|
11148
|
+
var topLevel = (dir, limits) => text(dir, ["rev-parse", "--show-toplevel"], limits).trim();
|
|
11149
|
+
var commitOf = (root, rev, limits) => text(root, ["rev-parse", "--verify", "-q", `${revision(rev)}^{commit}`], limits).trim();
|
|
11150
|
+
function isAncestor(root, a, b, limits) {
|
|
11151
|
+
try {
|
|
11152
|
+
run(root, ["merge-base", "--is-ancestor", objectId(a), objectId(b)], limits);
|
|
11153
|
+
return true;
|
|
11154
|
+
} catch {
|
|
11155
|
+
return false;
|
|
11156
|
+
}
|
|
11157
|
+
}
|
|
11158
|
+
var commitDiffNames = (root, a, b, limits) => list(text(root, ["diff", ...DIFF_OPTIONS, "--name-only", "-z", objectId(a), objectId(b), "--"], limits));
|
|
11159
|
+
var originUrl = (root, limits) => text(root, ["remote", "get-url", "origin"], limits).trim();
|
|
11160
|
+
async function worktreeStatus(root, deadline) {
|
|
11161
|
+
const out = await inIsolation(root, [{ kind: "status" }], { deadline, max: 16 * 1024 * 1024 });
|
|
11162
|
+
return out?.[0] ?? null;
|
|
11163
|
+
}
|
|
11164
|
+
function workerFile() {
|
|
11165
|
+
const js = path.join(import.meta.dirname, "git-worker.js");
|
|
11166
|
+
return fs.existsSync(js) ? js : path.join(import.meta.dirname, "git-worker.ts");
|
|
11167
|
+
}
|
|
11168
|
+
function inIsolation(root, ops, { deadline, max }) {
|
|
11169
|
+
return new Promise((resolve) => {
|
|
11170
|
+
let child;
|
|
11171
|
+
try {
|
|
11172
|
+
child = spawn(process.execPath, [workerFile()], {
|
|
11173
|
+
cwd: startDir(),
|
|
11174
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
11175
|
+
windowsHide: true,
|
|
11176
|
+
detached: process.platform !== "win32"
|
|
11177
|
+
});
|
|
11178
|
+
} catch {
|
|
11179
|
+
resolve(null);
|
|
11180
|
+
return;
|
|
11181
|
+
}
|
|
11182
|
+
const chunks = [];
|
|
11183
|
+
let size = 0;
|
|
11184
|
+
let done = false;
|
|
11185
|
+
const finish = (out) => {
|
|
11186
|
+
if (done)
|
|
11187
|
+
return;
|
|
11188
|
+
done = true;
|
|
11189
|
+
clearTimeout(timer);
|
|
11190
|
+
if (out === null) {
|
|
11191
|
+
try {
|
|
11192
|
+
if (process.platform !== "win32" && child.pid)
|
|
11193
|
+
process.kill(-child.pid, "SIGKILL");
|
|
11194
|
+
else
|
|
11195
|
+
child.kill("SIGKILL");
|
|
11196
|
+
} catch {
|
|
11197
|
+
child.kill("SIGKILL");
|
|
11198
|
+
}
|
|
11199
|
+
child.stdin?.destroy();
|
|
11200
|
+
child.stdout?.destroy();
|
|
11201
|
+
child.unref();
|
|
11202
|
+
}
|
|
11203
|
+
resolve(out);
|
|
11204
|
+
};
|
|
11205
|
+
const timer = setTimeout(() => finish(null), deadline);
|
|
11206
|
+
const cap = 6 * max * Math.max(1, ops.length * 2) + 64 * 1024;
|
|
11207
|
+
child.stdout?.on("data", (c) => {
|
|
11208
|
+
size += c.length;
|
|
11209
|
+
if (size > cap)
|
|
11210
|
+
finish(null);
|
|
11211
|
+
else
|
|
11212
|
+
chunks.push(c);
|
|
11213
|
+
});
|
|
11214
|
+
child.on("error", () => finish(null));
|
|
11215
|
+
child.on("close", () => {
|
|
11216
|
+
try {
|
|
11217
|
+
const result = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
11218
|
+
finish(result.ok ? result.out : null);
|
|
11219
|
+
} catch {
|
|
11220
|
+
finish(null);
|
|
11221
|
+
}
|
|
11222
|
+
});
|
|
11223
|
+
child.stdin?.on("error", () => finish(null));
|
|
11224
|
+
child.stdin?.end(JSON.stringify({
|
|
11225
|
+
root,
|
|
11226
|
+
ops,
|
|
11227
|
+
max,
|
|
11228
|
+
until: Date.now() + Math.floor(deadline * 0.8)
|
|
11229
|
+
}));
|
|
11230
|
+
});
|
|
11231
|
+
}
|
|
11232
|
+
|
|
11088
11233
|
// server/src/panel.ts
|
|
11089
11234
|
import { stripVTControlCharacters, styleText } from "node:util";
|
|
11090
11235
|
|
|
@@ -11115,8 +11260,8 @@ function singular(w) {
|
|
|
11115
11260
|
return w.slice(0, -1);
|
|
11116
11261
|
return w.endsWith("s") && !/(?:ss|us|is)$/.test(w) ? w.slice(0, -1) : w;
|
|
11117
11262
|
}
|
|
11118
|
-
function terms(
|
|
11119
|
-
const nfkc =
|
|
11263
|
+
function terms(text2, parts = true) {
|
|
11264
|
+
const nfkc = text2.normalize("NFKC");
|
|
11120
11265
|
const norm = nfkc.toLowerCase();
|
|
11121
11266
|
const out = [];
|
|
11122
11267
|
const keep = (w) => {
|
|
@@ -11257,9 +11402,9 @@ var MAX_QUOTED = 4096;
|
|
|
11257
11402
|
var BARE_HEAD = /[^\s"',;)]{1,256}/y;
|
|
11258
11403
|
var BARE_REST = /[^\s"',;)]*/y;
|
|
11259
11404
|
var NEXT_PARAM = /&[A-Za-z_][\w.-]*=/;
|
|
11260
|
-
var bareAt = (re,
|
|
11405
|
+
var bareAt = (re, text2, at) => {
|
|
11261
11406
|
re.lastIndex = at;
|
|
11262
|
-
const v = re.exec(
|
|
11407
|
+
const v = re.exec(text2)?.[0] ?? "";
|
|
11263
11408
|
const cut = v.search(NEXT_PARAM);
|
|
11264
11409
|
return cut < 0 ? v : v.slice(0, cut);
|
|
11265
11410
|
};
|
|
@@ -11272,49 +11417,49 @@ function secretValue(quoted, v) {
|
|
|
11272
11417
|
return false;
|
|
11273
11418
|
return !/\s/.test(v) || v.split(/\s+/).some((w) => /\d/.test(w) && /[A-Za-z]/.test(w));
|
|
11274
11419
|
}
|
|
11275
|
-
function maskFields(
|
|
11420
|
+
function maskFields(text2) {
|
|
11276
11421
|
let out = "";
|
|
11277
11422
|
let last = 0;
|
|
11278
11423
|
FIELD_NAME.lastIndex = 0;
|
|
11279
|
-
for (let m = FIELD_NAME.exec(
|
|
11424
|
+
for (let m = FIELD_NAME.exec(text2);m; m = FIELD_NAME.exec(text2)) {
|
|
11280
11425
|
const at = m.index + m[0].length;
|
|
11281
|
-
const q =
|
|
11426
|
+
const q = text2[at];
|
|
11282
11427
|
let quote = "";
|
|
11283
11428
|
let value;
|
|
11284
11429
|
if (q === '"' || q === "'") {
|
|
11285
|
-
const close =
|
|
11430
|
+
const close = text2.indexOf(q, at + 1);
|
|
11286
11431
|
if (close < 0 || close - at - 1 > MAX_QUOTED)
|
|
11287
11432
|
continue;
|
|
11288
|
-
value =
|
|
11433
|
+
value = text2.slice(at + 1, close);
|
|
11289
11434
|
if (value.includes(`
|
|
11290
11435
|
`))
|
|
11291
11436
|
continue;
|
|
11292
11437
|
quote = q;
|
|
11293
11438
|
} else {
|
|
11294
|
-
value = bareAt(BARE_HEAD,
|
|
11439
|
+
value = bareAt(BARE_HEAD, text2, at);
|
|
11295
11440
|
}
|
|
11296
11441
|
if (!secretValue(quote !== "", value))
|
|
11297
11442
|
continue;
|
|
11298
11443
|
if (!quote && value.length === 256)
|
|
11299
|
-
value += bareAt(BARE_REST,
|
|
11300
|
-
out += `${
|
|
11444
|
+
value += bareAt(BARE_REST, text2, at + 256);
|
|
11445
|
+
out += `${text2.slice(last, at)}${quote}[redacted]`;
|
|
11301
11446
|
last = at + quote.length + value.length;
|
|
11302
11447
|
FIELD_NAME.lastIndex = last;
|
|
11303
11448
|
}
|
|
11304
|
-
return out +
|
|
11449
|
+
return out + text2.slice(last);
|
|
11305
11450
|
}
|
|
11306
11451
|
var MYSQL_COMMAND = /\bmysql(?:dump|admin)?\b(?:'[^'\n]*'|"[^"\n]*"|[^\n;&|\\'"]|\\\r?\n|\\(?!\r?\n))*/g;
|
|
11307
11452
|
var MYSQL_PASSWORD = /(\s-p)(?:'[^'\n]*'|"[^"\n]*"|(?=[^\s-])\S+)/;
|
|
11308
11453
|
var URL_CREDENTIALS = /\b((?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|rediss?|amqps?|https?):\/\/[^:\s/@]*:)[^\s/]*@([^@\s/?#]+)/g;
|
|
11309
11454
|
var KEY_BEGIN = /-----BEGIN [A-Z ]*PRIVATE KEY-----/g;
|
|
11310
11455
|
var KEY_END = /-----END [A-Z ]*PRIVATE KEY-----/g;
|
|
11311
|
-
function keyBlocks(
|
|
11312
|
-
const ends = [...
|
|
11456
|
+
function keyBlocks(text2) {
|
|
11457
|
+
const ends = [...text2.matchAll(KEY_END)].map((m) => [m.index, m.index + m[0].length]);
|
|
11313
11458
|
const out = [];
|
|
11314
11459
|
let last = 0;
|
|
11315
11460
|
let e = 0;
|
|
11316
11461
|
if (ends.length > 0)
|
|
11317
|
-
for (const m of
|
|
11462
|
+
for (const m of text2.matchAll(KEY_BEGIN)) {
|
|
11318
11463
|
const after = m.index + m[0].length;
|
|
11319
11464
|
if (m.index < last)
|
|
11320
11465
|
continue;
|
|
@@ -11328,17 +11473,17 @@ function keyBlocks(text) {
|
|
|
11328
11473
|
}
|
|
11329
11474
|
return out;
|
|
11330
11475
|
}
|
|
11331
|
-
function maskPrivateKeys(
|
|
11476
|
+
function maskPrivateKeys(text2) {
|
|
11332
11477
|
let out = "";
|
|
11333
11478
|
let last = 0;
|
|
11334
|
-
for (const [a, b] of keyBlocks(
|
|
11335
|
-
out += `${
|
|
11479
|
+
for (const [a, b] of keyBlocks(text2)) {
|
|
11480
|
+
out += `${text2.slice(last, a)}[redacted: private key]`;
|
|
11336
11481
|
last = b;
|
|
11337
11482
|
}
|
|
11338
|
-
return out +
|
|
11483
|
+
return out + text2.slice(last);
|
|
11339
11484
|
}
|
|
11340
|
-
function mask(
|
|
11341
|
-
let out = maskFields(maskPrivateKeys(
|
|
11485
|
+
function mask(text2) {
|
|
11486
|
+
let out = maskFields(maskPrivateKeys(text2).replace(URL_CREDENTIALS, "$1[redacted]@$2").replace(AUTH_HEADER, "$1[redacted]").replace(HEADER_BEARER, "$1[redacted]").replace(ENV_ASSIGN, "$1$2[redacted]")).replace(MYSQL_COMMAND, (command) => command.replace(MYSQL_PASSWORD, "$1[redacted]"));
|
|
11342
11487
|
for (const [re, what] of SECRETS)
|
|
11343
11488
|
out = out.replace(re, `[redacted: ${what}]`);
|
|
11344
11489
|
return out;
|
|
@@ -11360,11 +11505,11 @@ function explain(e, depth) {
|
|
|
11360
11505
|
}
|
|
11361
11506
|
|
|
11362
11507
|
// server/src/panel.ts
|
|
11363
|
-
var title = (
|
|
11364
|
-
var rule = (
|
|
11508
|
+
var title = (text2) => `✦ ${text2}`;
|
|
11509
|
+
var rule = (text2) => text2.split(`
|
|
11365
11510
|
`).map((line) => line ? `│ ${line}` : "│").join(`
|
|
11366
11511
|
`);
|
|
11367
|
-
var foot = (
|
|
11512
|
+
var foot = (text2) => `╰─ ${text2}`;
|
|
11368
11513
|
var panel = (head2, lines, end) => [title(head2), ...lines.map(rule), foot(end)].join(`
|
|
11369
11514
|
`);
|
|
11370
11515
|
var STRING_SEQUENCE = /\u001b[\]P_^X][^\u0007\u001b]*(?:\u0007|\u001b\\)?/g;
|
|
@@ -11372,11 +11517,11 @@ var plain2 = (s) => visible(stripVTControlCharacters(s.replace(STRING_SEQUENCE,
|
|
|
11372
11517
|
`).replace(/(?![\t\n])\p{Cc}/gu, ""));
|
|
11373
11518
|
|
|
11374
11519
|
// server/src/plugin.ts
|
|
11375
|
-
var MANIFEST =
|
|
11520
|
+
var MANIFEST = path2.join(".claude-plugin", "plugin.json");
|
|
11376
11521
|
var PACKAGE = "package.json";
|
|
11377
11522
|
function versionAt(root) {
|
|
11378
11523
|
try {
|
|
11379
|
-
const m = JSON.parse(
|
|
11524
|
+
const m = JSON.parse(fs2.readFileSync(path2.join(root, MANIFEST), "utf8"));
|
|
11380
11525
|
return m.name === "sphica" && typeof m.version === "string" ? m.version : null;
|
|
11381
11526
|
} catch {
|
|
11382
11527
|
return null;
|
|
@@ -11384,28 +11529,28 @@ function versionAt(root) {
|
|
|
11384
11529
|
}
|
|
11385
11530
|
function packageVersionAt(root) {
|
|
11386
11531
|
try {
|
|
11387
|
-
const m = JSON.parse(
|
|
11532
|
+
const m = JSON.parse(fs2.readFileSync(path2.join(root, PACKAGE), "utf8"));
|
|
11388
11533
|
return m.name === "sphica" && typeof m.version === "string" ? m.version : null;
|
|
11389
11534
|
} catch {
|
|
11390
11535
|
return null;
|
|
11391
11536
|
}
|
|
11392
11537
|
}
|
|
11393
|
-
var here =
|
|
11394
|
-
var ROOT = [
|
|
11538
|
+
var here = path2.dirname(fileURLToPath(import.meta.url));
|
|
11539
|
+
var ROOT = [path2.join(here, ".."), path2.join(here, "..", "..", "plugin")].find((r) => versionAt(r) !== null) ?? path2.join(here, "..");
|
|
11395
11540
|
var HOST_MARKS = new Set([".orphaned_at", ".in_use"]);
|
|
11396
11541
|
|
|
11397
11542
|
// server/src/sqlite.ts
|
|
11398
11543
|
var SCHEMA_GENERATION = 2;
|
|
11399
11544
|
var SCHEMA_REVISION = 12;
|
|
11400
|
-
var sphicaHome = () => process.env.SPHICA_HOME ||
|
|
11401
|
-
var dbFile = () => process.env.SPHICA_DB ||
|
|
11545
|
+
var sphicaHome = () => process.env.SPHICA_HOME || path3.join(os2.homedir(), ".sphica");
|
|
11546
|
+
var dbFile = () => process.env.SPHICA_DB || path3.join(sphicaHome(), "sphica.db");
|
|
11402
11547
|
function requireRuntime() {
|
|
11403
11548
|
const proto = DatabaseSync.prototype;
|
|
11404
11549
|
if (typeof proto.setAuthorizer !== "function" || typeof proto.enableDefensive !== "function")
|
|
11405
11550
|
throw new Error(`Sphica needs Node 24.15 or later (this is ${process.version}). Upgrade Node.`);
|
|
11406
11551
|
}
|
|
11407
11552
|
function requireFile(file) {
|
|
11408
|
-
if (!
|
|
11553
|
+
if (!fs3.existsSync(file))
|
|
11409
11554
|
throw new Error(`No database at ${file}. Create it with \`sphica init\`.`);
|
|
11410
11555
|
}
|
|
11411
11556
|
function prepare(raw, check, busyMs = 5000) {
|
|
@@ -11729,7 +11874,7 @@ function connectWriter(role, file = dbFile(), create = false, busyMs) {
|
|
|
11729
11874
|
const raw = new DatabaseSync2(file);
|
|
11730
11875
|
try {
|
|
11731
11876
|
prepare(raw, create ? "none" : role === "ingest" || role === "forget" ? "revision" : "generation", busyMs);
|
|
11732
|
-
raw.function("sphica_terms", { deterministic: true }, (
|
|
11877
|
+
raw.function("sphica_terms", { deterministic: true }, (text2) => terms(String(text2 ?? "")).join(" "));
|
|
11733
11878
|
} catch (e) {
|
|
11734
11879
|
raw.close();
|
|
11735
11880
|
throw e;
|
|
@@ -11764,17 +11909,16 @@ function openWriter(role, file = dbFile(), busyMs) {
|
|
|
11764
11909
|
var sessionId = (projectId, host, externalId) => uuidFrom(String(projectId), host, externalId);
|
|
11765
11910
|
|
|
11766
11911
|
// server/src/project.ts
|
|
11767
|
-
import
|
|
11768
|
-
import
|
|
11769
|
-
import path4 from "node:path";
|
|
11912
|
+
import fs4 from "node:fs";
|
|
11913
|
+
import path5 from "node:path";
|
|
11770
11914
|
|
|
11771
11915
|
// server/src/anchors.ts
|
|
11772
|
-
import
|
|
11916
|
+
import path4 from "node:path";
|
|
11773
11917
|
var MAX_BYTES = 2 * 1024 * 1024;
|
|
11774
|
-
var leaves = (rel) => rel === ".." || rel.startsWith(`..${
|
|
11918
|
+
var leaves = (rel) => rel === ".." || rel.startsWith(`..${path4.sep}`) || path4.isAbsolute(rel);
|
|
11775
11919
|
|
|
11776
11920
|
// server/src/project.ts
|
|
11777
|
-
var localFile = () =>
|
|
11921
|
+
var localFile = () => path5.join(sphicaHome(), "projects.json");
|
|
11778
11922
|
var LOCAL_KEY = /^[a-z0-9][a-z0-9._-]*$/;
|
|
11779
11923
|
function legacyRemote(url) {
|
|
11780
11924
|
const raw = String(url ?? "").trim();
|
|
@@ -11802,21 +11946,18 @@ function normalizeKey(key) {
|
|
|
11802
11946
|
const host = fold(cut < 0 ? rest : rest.slice(0, cut));
|
|
11803
11947
|
return host === "github.com" ? fold(key) : `git:${host}${cut < 0 ? "" : rest.slice(cut)}`;
|
|
11804
11948
|
}
|
|
11805
|
-
var
|
|
11949
|
+
var ask = (fn) => {
|
|
11806
11950
|
try {
|
|
11807
|
-
return
|
|
11808
|
-
encoding: "utf8",
|
|
11809
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
11810
|
-
timeout: 5000
|
|
11811
|
-
}).trim();
|
|
11951
|
+
return fn() || null;
|
|
11812
11952
|
} catch {
|
|
11813
11953
|
return null;
|
|
11814
11954
|
}
|
|
11815
11955
|
};
|
|
11956
|
+
var top = (dir) => ask(() => topLevel(dir, { timeout: 5000 }));
|
|
11816
11957
|
function localMap() {
|
|
11817
11958
|
let raw;
|
|
11818
11959
|
try {
|
|
11819
|
-
raw =
|
|
11960
|
+
raw = fs4.readFileSync(localFile(), "utf8");
|
|
11820
11961
|
} catch (e) {
|
|
11821
11962
|
if (e.code === "ENOENT")
|
|
11822
11963
|
return {};
|
|
@@ -11828,35 +11969,35 @@ function localMap() {
|
|
|
11828
11969
|
} catch {
|
|
11829
11970
|
m = null;
|
|
11830
11971
|
}
|
|
11831
|
-
if (!m || typeof m !== "object" || Array.isArray(m) || Object.entries(m).some(([root, name]) => !
|
|
11972
|
+
if (!m || typeof m !== "object" || Array.isArray(m) || Object.entries(m).some(([root, name]) => !path5.isAbsolute(root) || typeof name !== "string" || !LOCAL_KEY.test(name)))
|
|
11832
11973
|
throw new Error(`${localFile()} is not a valid JSON project table. Fix or delete it, then name the project again.`);
|
|
11833
11974
|
return m;
|
|
11834
11975
|
}
|
|
11835
11976
|
function identify(dir) {
|
|
11836
|
-
const given =
|
|
11837
|
-
const
|
|
11838
|
-
const root =
|
|
11839
|
-
const legacy =
|
|
11977
|
+
const given = path5.resolve(dir);
|
|
11978
|
+
const found = top(given);
|
|
11979
|
+
const root = found || given;
|
|
11980
|
+
const legacy = found ? legacyRemote(ask(() => originUrl(root, { timeout: 5000 }))) : null;
|
|
11840
11981
|
if (legacy) {
|
|
11841
11982
|
const key = normalizeKey(`git:${legacy}`);
|
|
11842
11983
|
const remote = key.slice(4);
|
|
11843
11984
|
return { key, legacyKey: `git:${legacy}`, root, name: remote.split("/").slice(1).join("/") || remote };
|
|
11844
11985
|
}
|
|
11845
11986
|
const map = localMap();
|
|
11846
|
-
for (let d = root;; d =
|
|
11987
|
+
for (let d = root;; d = path5.dirname(d)) {
|
|
11847
11988
|
const local = map[d];
|
|
11848
11989
|
if (local && LOCAL_KEY.test(local))
|
|
11849
11990
|
return { key: `local:${local}`, legacyKey: `local:${local}`, root: d, name: local };
|
|
11850
|
-
if (
|
|
11991
|
+
if (found || path5.dirname(d) === d)
|
|
11851
11992
|
return null;
|
|
11852
11993
|
}
|
|
11853
11994
|
}
|
|
11854
11995
|
function relativeTo(root, file, cwd = root) {
|
|
11855
|
-
const abs =
|
|
11856
|
-
const rel =
|
|
11996
|
+
const abs = path5.resolve(cwd, file);
|
|
11997
|
+
const rel = path5.relative(root, abs);
|
|
11857
11998
|
if (!rel || leaves(rel) || /\p{Cc}/u.test(rel))
|
|
11858
11999
|
return null;
|
|
11859
|
-
return rel.split(
|
|
12000
|
+
return rel.split(path5.sep).join("/");
|
|
11860
12001
|
}
|
|
11861
12002
|
function patchPaths(patch) {
|
|
11862
12003
|
const out = [];
|
|
@@ -11870,25 +12011,15 @@ function patchPaths(patch) {
|
|
|
11870
12011
|
}
|
|
11871
12012
|
|
|
11872
12013
|
// server/src/worktree.ts
|
|
11873
|
-
import
|
|
11874
|
-
import
|
|
11875
|
-
import path5 from "node:path";
|
|
12014
|
+
import fs5 from "node:fs";
|
|
12015
|
+
import path6 from "node:path";
|
|
11876
12016
|
var MAX_PATHS = 200;
|
|
11877
|
-
var
|
|
11878
|
-
|
|
11879
|
-
return execFileSync2("git", ["-C", root, ...args], {
|
|
11880
|
-
encoding: "utf8",
|
|
11881
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
11882
|
-
timeout: 5000,
|
|
11883
|
-
maxBuffer: 16 * 1024 * 1024
|
|
11884
|
-
});
|
|
11885
|
-
} catch {
|
|
11886
|
-
return null;
|
|
11887
|
-
}
|
|
11888
|
-
};
|
|
12017
|
+
var LIMITS = { timeout: 5000, max: 16 * 1024 * 1024 };
|
|
12018
|
+
var STATUS_DEADLINE = 5000;
|
|
11889
12019
|
var usable = (p) => p !== "" && !p.includes("\\") && !/\p{Cc}/u.test(p);
|
|
11890
|
-
function snapshot(root) {
|
|
11891
|
-
const
|
|
12020
|
+
async function snapshot(root) {
|
|
12021
|
+
const until = Date.now() + STATUS_DEADLINE;
|
|
12022
|
+
const out = await worktreeStatus(root, STATUS_DEADLINE);
|
|
11892
12023
|
if (out === null)
|
|
11893
12024
|
return null;
|
|
11894
12025
|
const fields = out.split("\x00");
|
|
@@ -11906,13 +12037,18 @@ function snapshot(root) {
|
|
|
11906
12037
|
continue;
|
|
11907
12038
|
let st;
|
|
11908
12039
|
try {
|
|
11909
|
-
st =
|
|
12040
|
+
st = fs5.lstatSync(path6.join(root, p), { throwIfNoEntry: false });
|
|
11910
12041
|
} catch {
|
|
11911
12042
|
st = undefined;
|
|
11912
12043
|
}
|
|
11913
12044
|
entries[p] = `${parts.slice(0, skip).join(" ")} ${st ? `${st.size}:${st.mtimeMs}` : "gone"}`;
|
|
11914
12045
|
}
|
|
11915
|
-
|
|
12046
|
+
let head2 = null;
|
|
12047
|
+
try {
|
|
12048
|
+
const left = until - Date.now();
|
|
12049
|
+
if (left > 0)
|
|
12050
|
+
head2 = commitOf(root, "HEAD", { ...LIMITS, timeout: left }) || null;
|
|
12051
|
+
} catch {}
|
|
11916
12052
|
return { head: head2, entries };
|
|
11917
12053
|
}
|
|
11918
12054
|
function changed(root, before, after) {
|
|
@@ -11920,8 +12056,12 @@ function changed(root, before, after) {
|
|
|
11920
12056
|
for (const p of new Set([...Object.keys(before.entries), ...Object.keys(after.entries)]))
|
|
11921
12057
|
if (before.entries[p] !== after.entries[p])
|
|
11922
12058
|
paths.add(p);
|
|
11923
|
-
if (before.head && after.head && before.head !== after.head &&
|
|
11924
|
-
|
|
12059
|
+
if (before.head && after.head && before.head !== after.head && isAncestor(root, before.head, after.head, LIMITS)) {
|
|
12060
|
+
let between = [];
|
|
12061
|
+
try {
|
|
12062
|
+
between = commitDiffNames(root, before.head, after.head, LIMITS);
|
|
12063
|
+
} catch {}
|
|
12064
|
+
for (const p of between)
|
|
11925
12065
|
if (usable(p))
|
|
11926
12066
|
paths.add(p);
|
|
11927
12067
|
}
|
|
@@ -11929,15 +12069,15 @@ function changed(root, before, after) {
|
|
|
11929
12069
|
}
|
|
11930
12070
|
|
|
11931
12071
|
// server/src/capture.ts
|
|
11932
|
-
var spoolDir = () =>
|
|
11933
|
-
var stateFile = () =>
|
|
11934
|
-
var rejectedDir = () =>
|
|
11935
|
-
var unregisteredDir = () =>
|
|
11936
|
-
var callsDir = () =>
|
|
11937
|
-
var callsRejectedDir = () =>
|
|
12072
|
+
var spoolDir = () => path7.join(sphicaHome(), "spool");
|
|
12073
|
+
var stateFile = () => path7.join(sphicaHome(), "capture.json");
|
|
12074
|
+
var rejectedDir = () => path7.join(spoolDir(), "rejected");
|
|
12075
|
+
var unregisteredDir = () => path7.join(spoolDir(), "unregistered");
|
|
12076
|
+
var callsDir = () => path7.join(spoolDir(), "calls");
|
|
12077
|
+
var callsRejectedDir = () => path7.join(callsDir(), "rejected");
|
|
11938
12078
|
var HOLD_DAYS = 30;
|
|
11939
12079
|
var HOLD_MAX = 1000;
|
|
11940
|
-
var baselineDir = () =>
|
|
12080
|
+
var baselineDir = () => path7.join(sphicaHome(), "worktree");
|
|
11941
12081
|
function current(raw) {
|
|
11942
12082
|
const r = raw;
|
|
11943
12083
|
if (typeof r.project !== "string")
|
|
@@ -11986,7 +12126,7 @@ ${z}`,
|
|
|
11986
12126
|
function prune(held, removed = () => {}) {
|
|
11987
12127
|
let files;
|
|
11988
12128
|
try {
|
|
11989
|
-
files =
|
|
12129
|
+
files = fs6.readdirSync(held).filter((f) => f.endsWith(".json") && !f.startsWith(".")).sort();
|
|
11990
12130
|
} catch {
|
|
11991
12131
|
return;
|
|
11992
12132
|
}
|
|
@@ -11994,26 +12134,26 @@ function prune(held, removed = () => {}) {
|
|
|
11994
12134
|
const stale = files.filter((f) => Number(f.split("-")[0]) < cutoff);
|
|
11995
12135
|
const over = files.slice(0, Math.max(0, files.length - HOLD_MAX));
|
|
11996
12136
|
for (const f of new Set([...stale, ...over])) {
|
|
11997
|
-
|
|
12137
|
+
fs6.rmSync(path7.join(held, f), { force: true });
|
|
11998
12138
|
removed();
|
|
11999
12139
|
}
|
|
12000
12140
|
}
|
|
12001
12141
|
function put(dir, record) {
|
|
12002
|
-
|
|
12142
|
+
fs6.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
12003
12143
|
const name = `${Date.now()}-${process.pid}-${Math.random().toString(36).slice(2, 10)}.json`;
|
|
12004
|
-
const tmp =
|
|
12005
|
-
|
|
12006
|
-
|
|
12007
|
-
return
|
|
12144
|
+
const tmp = path7.join(dir, `.${name}`);
|
|
12145
|
+
fs6.writeFileSync(tmp, JSON.stringify(record), { mode: 384 });
|
|
12146
|
+
fs6.renameSync(tmp, path7.join(dir, name));
|
|
12147
|
+
return path7.join(dir, name);
|
|
12008
12148
|
}
|
|
12009
12149
|
function spool(record) {
|
|
12010
12150
|
put(spoolDir(), record);
|
|
12011
12151
|
}
|
|
12012
12152
|
var branchOf = (root) => {
|
|
12013
12153
|
try {
|
|
12014
|
-
const dotgit =
|
|
12015
|
-
const gitdir =
|
|
12016
|
-
const h =
|
|
12154
|
+
const dotgit = path7.join(root, ".git");
|
|
12155
|
+
const gitdir = fs6.statSync(dotgit).isFile() ? path7.resolve(root, fs6.readFileSync(dotgit, "utf8").match(/^gitdir: (.+)$/m)?.[1]?.trim() ?? "") : dotgit;
|
|
12156
|
+
const h = fs6.readFileSync(path7.join(gitdir, "HEAD"), "utf8").trim();
|
|
12017
12157
|
return h.startsWith("ref: refs/heads/") ? h.slice("ref: refs/heads/".length) : null;
|
|
12018
12158
|
} catch {
|
|
12019
12159
|
return null;
|
|
@@ -12059,7 +12199,7 @@ Notes: ${notes.trim()}` : "";
|
|
|
12059
12199
|
};
|
|
12060
12200
|
}
|
|
12061
12201
|
function captureNotice(file = dbFile()) {
|
|
12062
|
-
if (!
|
|
12202
|
+
if (!fs6.existsSync(file))
|
|
12063
12203
|
return panel("sphica: no database, so conversations are not recorded", [file], "Create it with sphica init");
|
|
12064
12204
|
const s = readState();
|
|
12065
12205
|
if (s.unreadable)
|
|
@@ -12070,15 +12210,15 @@ function captureNotice(file = dbFile()) {
|
|
|
12070
12210
|
return panel(`sphica: the database rejected ${plural(s.rejected, "record")}`, [rejectedDir(), `Move them back to ${spoolDir()} to resend`], "Fix them first, then check with sphica doctor");
|
|
12071
12211
|
return null;
|
|
12072
12212
|
}
|
|
12073
|
-
var turnDir = (host, session) =>
|
|
12074
|
-
var startFile = (dir, turn) =>
|
|
12213
|
+
var turnDir = (host, session) => path7.join(baselineDir(), digest(`${host}\x00${session}`));
|
|
12214
|
+
var startFile = (dir, turn) => path7.join(dir, `${digest(turn)}.json`);
|
|
12075
12215
|
var isStart = (v) => {
|
|
12076
12216
|
const s = v;
|
|
12077
12217
|
return !!s && typeof s === "object" && typeof s.turn === "string" && typeof s.running === "boolean" && (s.seq === null || Number.isSafeInteger(s.seq)) && (s.entries === null || typeof s.entries === "object" && !Array.isArray(s.entries)) && (s.head === null || typeof s.head === "string");
|
|
12078
12218
|
};
|
|
12079
12219
|
function readStart(file) {
|
|
12080
12220
|
try {
|
|
12081
|
-
const s = JSON.parse(
|
|
12221
|
+
const s = JSON.parse(fs6.readFileSync(file, "utf8"));
|
|
12082
12222
|
return isStart(s) ? s : null;
|
|
12083
12223
|
} catch {
|
|
12084
12224
|
return null;
|
|
@@ -12087,7 +12227,7 @@ function readStart(file) {
|
|
|
12087
12227
|
function readStarts(dir) {
|
|
12088
12228
|
let names;
|
|
12089
12229
|
try {
|
|
12090
|
-
names =
|
|
12230
|
+
names = fs6.readdirSync(dir);
|
|
12091
12231
|
} catch (e) {
|
|
12092
12232
|
return e.code === "ENOENT" ? [] : null;
|
|
12093
12233
|
}
|
|
@@ -12095,7 +12235,7 @@ function readStarts(dir) {
|
|
|
12095
12235
|
for (const name of names) {
|
|
12096
12236
|
if (name.startsWith("."))
|
|
12097
12237
|
continue;
|
|
12098
|
-
const s = readStart(
|
|
12238
|
+
const s = readStart(path7.join(dir, name));
|
|
12099
12239
|
if (!s)
|
|
12100
12240
|
return null;
|
|
12101
12241
|
starts.push(s);
|
|
@@ -12105,30 +12245,30 @@ function readStarts(dir) {
|
|
|
12105
12245
|
function newestTurn(starts) {
|
|
12106
12246
|
if (!starts?.length || starts.some((s) => s.seq === null))
|
|
12107
12247
|
return null;
|
|
12108
|
-
const
|
|
12109
|
-
const at = starts.filter((s) => s.seq ===
|
|
12248
|
+
const top2 = Math.max(...starts.map((s) => s.seq));
|
|
12249
|
+
const at = starts.filter((s) => s.seq === top2);
|
|
12110
12250
|
return at.length === 1 ? at[0]?.turn ?? null : null;
|
|
12111
12251
|
}
|
|
12112
12252
|
function writeStart(dir, s, file = startFile(dir, s.turn)) {
|
|
12113
|
-
|
|
12114
|
-
const tmp =
|
|
12115
|
-
|
|
12116
|
-
|
|
12253
|
+
fs6.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
12254
|
+
const tmp = path7.join(dir, `.${path7.basename(file)}.${process.pid}.tmp`);
|
|
12255
|
+
fs6.writeFileSync(tmp, JSON.stringify(s), { mode: 384 });
|
|
12256
|
+
fs6.renameSync(tmp, file);
|
|
12117
12257
|
}
|
|
12118
|
-
function openTurn(dir, turn, root) {
|
|
12258
|
+
async function openTurn(dir, turn, root) {
|
|
12119
12259
|
if (readStart(startFile(dir, turn))?.running)
|
|
12120
12260
|
return null;
|
|
12121
12261
|
const starts = readStarts(dir);
|
|
12122
12262
|
const seq = starts && !starts.some((s) => s.seq === null) ? Math.max(0, ...starts.map((s) => s.seq)) + 1 : null;
|
|
12123
|
-
const now = snapshot(root);
|
|
12263
|
+
const now = await snapshot(root);
|
|
12124
12264
|
return () => writeStart(dir, { head: now?.head ?? null, entries: now?.entries ?? null, running: true, turn, seq });
|
|
12125
12265
|
}
|
|
12126
|
-
function closeTurn(dir, turn, root) {
|
|
12266
|
+
async function closeTurn(dir, turn, root) {
|
|
12127
12267
|
const own = readStart(startFile(dir, turn));
|
|
12128
12268
|
if (!own?.entries)
|
|
12129
12269
|
return null;
|
|
12130
12270
|
const before = { head: own.head, entries: own.entries };
|
|
12131
|
-
const now = snapshot(root);
|
|
12271
|
+
const now = await snapshot(root);
|
|
12132
12272
|
return () => {
|
|
12133
12273
|
if (newestTurn(readStarts(dir)) !== turn || JSON.stringify(readStart(startFile(dir, turn))) !== JSON.stringify(own))
|
|
12134
12274
|
return [];
|
|
@@ -12143,22 +12283,22 @@ function stopTurn(dir, turn) {
|
|
|
12143
12283
|
}
|
|
12144
12284
|
function pruneBaselines() {
|
|
12145
12285
|
const cutoff = Date.now() - HOLD_DAYS * 24 * 60 * 60 * 1000;
|
|
12146
|
-
const fresh = (file) => (
|
|
12286
|
+
const fresh = (file) => (fs6.statSync(file, { throwIfNoEntry: false })?.mtimeMs ?? Date.now()) >= cutoff;
|
|
12147
12287
|
let names;
|
|
12148
12288
|
try {
|
|
12149
|
-
names =
|
|
12289
|
+
names = fs6.readdirSync(baselineDir());
|
|
12150
12290
|
} catch {
|
|
12151
12291
|
return;
|
|
12152
12292
|
}
|
|
12153
12293
|
for (const name of names) {
|
|
12154
|
-
const at =
|
|
12155
|
-
if (!
|
|
12294
|
+
const at = path7.join(baselineDir(), name);
|
|
12295
|
+
if (!fs6.statSync(at, { throwIfNoEntry: false })?.isDirectory()) {
|
|
12156
12296
|
if (!fresh(at))
|
|
12157
|
-
|
|
12297
|
+
fs6.rmSync(at, { force: true });
|
|
12158
12298
|
} else {
|
|
12159
|
-
const files =
|
|
12160
|
-
if (files.length && !files.some((f) => fresh(
|
|
12161
|
-
|
|
12299
|
+
const files = fs6.readdirSync(at);
|
|
12300
|
+
if (files.length && !files.some((f) => fresh(path7.join(at, f))))
|
|
12301
|
+
fs6.rmSync(at, { recursive: true, force: true });
|
|
12162
12302
|
}
|
|
12163
12303
|
}
|
|
12164
12304
|
}
|
|
@@ -12169,7 +12309,14 @@ function attempt(fn) {
|
|
|
12169
12309
|
return;
|
|
12170
12310
|
}
|
|
12171
12311
|
}
|
|
12172
|
-
function
|
|
12312
|
+
async function attemptAsync(fn) {
|
|
12313
|
+
try {
|
|
12314
|
+
return await fn();
|
|
12315
|
+
} catch {
|
|
12316
|
+
return;
|
|
12317
|
+
}
|
|
12318
|
+
}
|
|
12319
|
+
async function onHook(host, input) {
|
|
12173
12320
|
const event = input.hook_event_name;
|
|
12174
12321
|
const owner = () => isOwnerTurn(input, undefined, undefined, host === "codex" ? process.env.CODEX_THREAD_ID : undefined);
|
|
12175
12322
|
if (event === "SessionStart") {
|
|
@@ -12177,7 +12324,7 @@ function onHook(host, input) {
|
|
|
12177
12324
|
return { flush: false };
|
|
12178
12325
|
const file = process.env.CLAUDE_ENV_FILE;
|
|
12179
12326
|
if (file && input.session_id && /^[A-Za-z0-9_-]+$/.test(input.session_id)) {
|
|
12180
|
-
|
|
12327
|
+
fs6.appendFileSync(file, `export SPHICA_PARENT_SESSION=${input.session_id}
|
|
12181
12328
|
`);
|
|
12182
12329
|
}
|
|
12183
12330
|
attempt(pruneBaselines);
|
|
@@ -12221,12 +12368,12 @@ function onHook(host, input) {
|
|
|
12221
12368
|
say(`${turn}:owner`, "owner", prompt);
|
|
12222
12369
|
}
|
|
12223
12370
|
if (event === "UserPromptSubmit")
|
|
12224
|
-
|
|
12371
|
+
await attemptAsync(async () => (await openTurn(dir, turn, place.root))?.());
|
|
12225
12372
|
if (event === "Stop") {
|
|
12226
12373
|
if (input.last_assistant_message)
|
|
12227
12374
|
say(`${turn}:assistant`, "assistant", input.last_assistant_message);
|
|
12228
|
-
const paths =
|
|
12229
|
-
const finish = closeTurn(dir, turn, place.root);
|
|
12375
|
+
const paths = await attemptAsync(async () => {
|
|
12376
|
+
const finish = await closeTurn(dir, turn, place.root);
|
|
12230
12377
|
if (finish)
|
|
12231
12378
|
return finish();
|
|
12232
12379
|
stopTurn(dir, turn);
|
|
@@ -12260,7 +12407,7 @@ function onHook(host, input) {
|
|
|
12260
12407
|
function readStateFile() {
|
|
12261
12408
|
let raw = {};
|
|
12262
12409
|
try {
|
|
12263
|
-
const parsed = JSON.parse(
|
|
12410
|
+
const parsed = JSON.parse(fs6.readFileSync(stateFile(), "utf8"));
|
|
12264
12411
|
if (parsed && typeof parsed === "object")
|
|
12265
12412
|
raw = parsed;
|
|
12266
12413
|
} catch {}
|
|
@@ -12274,7 +12421,7 @@ function readStateFile() {
|
|
|
12274
12421
|
}
|
|
12275
12422
|
function writeState(s) {
|
|
12276
12423
|
try {
|
|
12277
|
-
|
|
12424
|
+
fs6.writeFileSync(stateFile(), JSON.stringify({ ...s, pruned: s.pruned ?? readStateFile().pruned }));
|
|
12278
12425
|
} catch {}
|
|
12279
12426
|
}
|
|
12280
12427
|
function readState() {
|
|
@@ -12290,7 +12437,7 @@ function readState() {
|
|
|
12290
12437
|
}
|
|
12291
12438
|
function listQueue(dir) {
|
|
12292
12439
|
try {
|
|
12293
|
-
const names =
|
|
12440
|
+
const names = fs6.readdirSync(dir);
|
|
12294
12441
|
return {
|
|
12295
12442
|
files: names.filter((f) => f.endsWith(".json") && !f.startsWith(".")),
|
|
12296
12443
|
temp: names.filter((f) => f.endsWith(".json") && f.startsWith(".")),
|
|
@@ -12316,7 +12463,7 @@ function queueReport(now = Date.now()) {
|
|
|
12316
12463
|
const temp = { count: 0, oldestMs: 0, bytes: 0 };
|
|
12317
12464
|
for (const name of listed[k].temp)
|
|
12318
12465
|
try {
|
|
12319
|
-
const st =
|
|
12466
|
+
const st = fs6.statSync(path7.join(dirs[k], name));
|
|
12320
12467
|
const age = now - st.mtimeMs;
|
|
12321
12468
|
if (age < TEMP_AGE_MS)
|
|
12322
12469
|
continue;
|
|
@@ -12331,9 +12478,9 @@ function queueReport(now = Date.now()) {
|
|
|
12331
12478
|
for (const name of listed[k].files ?? []) {
|
|
12332
12479
|
let why = "unknown";
|
|
12333
12480
|
try {
|
|
12334
|
-
const
|
|
12335
|
-
if (REASON.test(
|
|
12336
|
-
why =
|
|
12481
|
+
const text2 = fs6.readFileSync(path7.join(dirs[k], `${name}.reason`), "utf8");
|
|
12482
|
+
if (REASON.test(text2))
|
|
12483
|
+
why = text2;
|
|
12337
12484
|
} catch {}
|
|
12338
12485
|
out[why] = (out[why] ?? 0) + 1;
|
|
12339
12486
|
}
|
|
@@ -12341,7 +12488,7 @@ function queueReport(now = Date.now()) {
|
|
|
12341
12488
|
};
|
|
12342
12489
|
const read = (dir, name) => {
|
|
12343
12490
|
try {
|
|
12344
|
-
return JSON.parse(
|
|
12491
|
+
return JSON.parse(fs6.readFileSync(path7.join(dir, name), "utf8"));
|
|
12345
12492
|
} catch {
|
|
12346
12493
|
return null;
|
|
12347
12494
|
}
|
|
@@ -12370,25 +12517,25 @@ function queueReport(now = Date.now()) {
|
|
|
12370
12517
|
};
|
|
12371
12518
|
}
|
|
12372
12519
|
function lock() {
|
|
12373
|
-
const file =
|
|
12374
|
-
|
|
12520
|
+
const file = path7.join(spoolDir(), ".lock");
|
|
12521
|
+
fs6.mkdirSync(spoolDir(), { recursive: true, mode: 448 });
|
|
12375
12522
|
for (let attempt2 = 0;attempt2 < 2; attempt2++) {
|
|
12376
12523
|
try {
|
|
12377
|
-
|
|
12524
|
+
fs6.writeFileSync(file, String(process.pid), { flag: "wx", mode: 384 });
|
|
12378
12525
|
return () => {
|
|
12379
12526
|
try {
|
|
12380
|
-
if (
|
|
12381
|
-
|
|
12527
|
+
if (fs6.readFileSync(file, "utf8") === String(process.pid))
|
|
12528
|
+
fs6.rmSync(file, { force: true });
|
|
12382
12529
|
} catch {}
|
|
12383
12530
|
};
|
|
12384
12531
|
} catch (e) {
|
|
12385
12532
|
if (e.code !== "EEXIST")
|
|
12386
12533
|
throw e;
|
|
12387
12534
|
}
|
|
12388
|
-
const st =
|
|
12535
|
+
const st = fs6.statSync(file, { throwIfNoEntry: false });
|
|
12389
12536
|
if (!st)
|
|
12390
12537
|
continue;
|
|
12391
|
-
const holder = Number(
|
|
12538
|
+
const holder = Number(fs6.readFileSync(file, "utf8") || 0);
|
|
12392
12539
|
const fresh = Date.now() - st.mtimeMs < 5 * 60000;
|
|
12393
12540
|
const alive = (() => {
|
|
12394
12541
|
if (holder <= 0)
|
|
@@ -12401,7 +12548,7 @@ function lock() {
|
|
|
12401
12548
|
})();
|
|
12402
12549
|
if (alive && fresh)
|
|
12403
12550
|
return null;
|
|
12404
|
-
|
|
12551
|
+
fs6.rmSync(file, { force: true });
|
|
12405
12552
|
}
|
|
12406
12553
|
return null;
|
|
12407
12554
|
}
|
|
@@ -12501,23 +12648,23 @@ var REJECTED = new Set([18, 19, 20, 25]);
|
|
|
12501
12648
|
var rejected = (e) => REJECTED.has(sqliteCode(e) ?? -1);
|
|
12502
12649
|
var queued = (from) => {
|
|
12503
12650
|
try {
|
|
12504
|
-
return
|
|
12651
|
+
return fs6.readdirSync(from).filter((f) => f.endsWith(".json") && !f.startsWith(".")).sort().map((name) => ({ name, from }));
|
|
12505
12652
|
} catch {
|
|
12506
12653
|
return [];
|
|
12507
12654
|
}
|
|
12508
12655
|
};
|
|
12509
12656
|
var CODE_NAMES = { 18: "TOOBIG", 19: "CONSTRAINT", 20: "MISMATCH", 25: "RANGE" };
|
|
12510
12657
|
function setAside(from, name, dir, reason2) {
|
|
12511
|
-
|
|
12658
|
+
fs6.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
12512
12659
|
try {
|
|
12513
|
-
|
|
12660
|
+
fs6.renameSync(path7.join(from, name), path7.join(dir, name));
|
|
12514
12661
|
} catch (e) {
|
|
12515
12662
|
if (e.code === "ENOENT")
|
|
12516
12663
|
return;
|
|
12517
12664
|
throw e;
|
|
12518
12665
|
}
|
|
12519
12666
|
try {
|
|
12520
|
-
|
|
12667
|
+
fs6.writeFileSync(path7.join(dir, `${name}.reason`), reason2, { mode: 384 });
|
|
12521
12668
|
} catch {}
|
|
12522
12669
|
}
|
|
12523
12670
|
async function sendBatch(db, names) {
|
|
@@ -12527,7 +12674,7 @@ async function sendBatch(db, names) {
|
|
|
12527
12674
|
let raw;
|
|
12528
12675
|
let r;
|
|
12529
12676
|
try {
|
|
12530
|
-
raw = JSON.parse(
|
|
12677
|
+
raw = JSON.parse(fs6.readFileSync(path7.join(from, name), "utf8"));
|
|
12531
12678
|
} catch {
|
|
12532
12679
|
setAside(from, name, rejectedDir(), "unreadable");
|
|
12533
12680
|
continue;
|
|
@@ -12541,7 +12688,7 @@ async function sendBatch(db, names) {
|
|
|
12541
12688
|
if (r)
|
|
12542
12689
|
records.push({ name, from, r });
|
|
12543
12690
|
else
|
|
12544
|
-
|
|
12691
|
+
fs6.rmSync(path7.join(from, name), { force: true });
|
|
12545
12692
|
}
|
|
12546
12693
|
let sent = 0;
|
|
12547
12694
|
let strayed = [];
|
|
@@ -12569,12 +12716,12 @@ async function sendBatch(db, names) {
|
|
|
12569
12716
|
for (const x of bad)
|
|
12570
12717
|
setAside(x.from, x.name, rejectedDir(), x.why);
|
|
12571
12718
|
if (strayed.length) {
|
|
12572
|
-
|
|
12719
|
+
fs6.mkdirSync(held, { recursive: true, mode: 448 });
|
|
12573
12720
|
for (const x of strayed) {
|
|
12574
12721
|
if (x.from === held)
|
|
12575
12722
|
continue;
|
|
12576
12723
|
try {
|
|
12577
|
-
|
|
12724
|
+
fs6.renameSync(path7.join(x.from, x.name), path7.join(held, x.name));
|
|
12578
12725
|
} catch (e) {
|
|
12579
12726
|
if (e.code !== "ENOENT")
|
|
12580
12727
|
throw e;
|
|
@@ -12584,7 +12731,7 @@ async function sendBatch(db, names) {
|
|
|
12584
12731
|
const moved = new Set([...bad, ...strayed].map((x) => x.name));
|
|
12585
12732
|
for (const x of records)
|
|
12586
12733
|
if (!moved.has(x.name))
|
|
12587
|
-
|
|
12734
|
+
fs6.rmSync(path7.join(x.from, x.name), { force: true });
|
|
12588
12735
|
return { sent, rejected: bad.length };
|
|
12589
12736
|
}
|
|
12590
12737
|
async function sendObservations(db, names) {
|
|
@@ -12595,16 +12742,16 @@ async function sendObservations(db, names) {
|
|
|
12595
12742
|
};
|
|
12596
12743
|
const rows = [];
|
|
12597
12744
|
for (const { name, from } of names) {
|
|
12598
|
-
let
|
|
12745
|
+
let text2;
|
|
12599
12746
|
try {
|
|
12600
|
-
|
|
12747
|
+
text2 = fs6.readFileSync(path7.join(from, name), "utf8");
|
|
12601
12748
|
} catch {
|
|
12602
12749
|
continue;
|
|
12603
12750
|
}
|
|
12604
12751
|
let o = null;
|
|
12605
12752
|
let why = "unreadable";
|
|
12606
12753
|
try {
|
|
12607
|
-
o = observation(JSON.parse(
|
|
12754
|
+
o = observation(JSON.parse(text2));
|
|
12608
12755
|
why = "shape";
|
|
12609
12756
|
} catch {}
|
|
12610
12757
|
if (o)
|
|
@@ -12636,7 +12783,7 @@ async function sendObservations(db, names) {
|
|
|
12636
12783
|
aside(x.name, why);
|
|
12637
12784
|
else
|
|
12638
12785
|
try {
|
|
12639
|
-
|
|
12786
|
+
fs6.rmSync(path7.join(callsDir(), x.name), { force: true });
|
|
12640
12787
|
} catch {}
|
|
12641
12788
|
}
|
|
12642
12789
|
return true;
|
|
@@ -12779,7 +12926,7 @@ async function observeRecordCall(file, input, owner, waitMs = OBSERVE_WAIT_MS) {
|
|
|
12779
12926
|
}
|
|
12780
12927
|
if (kept)
|
|
12781
12928
|
try {
|
|
12782
|
-
|
|
12929
|
+
fs6.rmSync(kept, { force: true });
|
|
12783
12930
|
} catch {}
|
|
12784
12931
|
}
|
|
12785
12932
|
async function main() {
|
|
@@ -12795,11 +12942,11 @@ async function main() {
|
|
|
12795
12942
|
}
|
|
12796
12943
|
if (host === "codex" && input.hook_event_name === "Stop")
|
|
12797
12944
|
process.stdout.write("{}");
|
|
12798
|
-
const { flush: send, notice } = onHook(host, input);
|
|
12945
|
+
const { flush: send, notice } = await onHook(host, input);
|
|
12799
12946
|
if (notice)
|
|
12800
12947
|
process.stdout.write(JSON.stringify({ systemMessage: notice }));
|
|
12801
12948
|
if (send)
|
|
12802
|
-
|
|
12949
|
+
spawn2(process.execPath, [process.argv[1] ?? "", "--flush"], {
|
|
12803
12950
|
detached: true,
|
|
12804
12951
|
stdio: "ignore",
|
|
12805
12952
|
windowsHide: true
|