sphica 0.6.52 → 0.6.53
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/README.md +6 -5
- package/dist/capture.js +216 -139
- package/dist/cli.js +253 -176
- package/dist/deliver.js +382 -268
- package/dist/mcp-record.js +236 -159
- package/package.json +1 -1
- package/skills/forget/SKILL.md +5 -3
package/dist/cli.js
CHANGED
|
@@ -92,9 +92,9 @@ var require_src = __commonJS(function(exports, module) {
|
|
|
92
92
|
});
|
|
93
93
|
|
|
94
94
|
// server/src/cli.ts
|
|
95
|
-
import
|
|
96
|
-
import
|
|
97
|
-
import
|
|
95
|
+
import fs13 from "node:fs";
|
|
96
|
+
import os5 from "node:os";
|
|
97
|
+
import path13 from "node:path";
|
|
98
98
|
|
|
99
99
|
// server/node_modules/@clack/core/dist/index.mjs
|
|
100
100
|
import { stdout, stdin } from "node:process";
|
|
@@ -2967,8 +2967,8 @@ async function run(app, inputs, context) {
|
|
|
2967
2967
|
}
|
|
2968
2968
|
|
|
2969
2969
|
// server/src/admin.ts
|
|
2970
|
-
import
|
|
2971
|
-
import
|
|
2970
|
+
import fs12 from "node:fs";
|
|
2971
|
+
import path12 from "node:path";
|
|
2972
2972
|
import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
|
|
2973
2973
|
|
|
2974
2974
|
// server/src/assets.ts
|
|
@@ -5184,8 +5184,49 @@ function backups(file = dbFile()) {
|
|
|
5184
5184
|
|
|
5185
5185
|
// server/src/capture.ts
|
|
5186
5186
|
import { spawn as spawn2 } from "node:child_process";
|
|
5187
|
-
import
|
|
5188
|
-
import
|
|
5187
|
+
import fs11 from "node:fs";
|
|
5188
|
+
import path11 from "node:path";
|
|
5189
|
+
|
|
5190
|
+
// server/src/codex-log.ts
|
|
5191
|
+
import fs6 from "node:fs";
|
|
5192
|
+
import path6 from "node:path";
|
|
5193
|
+
var SESSION_META_BYTES = 256 * 1024;
|
|
5194
|
+
var text2 = (v) => typeof v === "string" ? v : null;
|
|
5195
|
+
function codexThread(input) {
|
|
5196
|
+
const file = input.transcript_path;
|
|
5197
|
+
if (typeof file !== "string" || !path6.isAbsolute(file))
|
|
5198
|
+
return null;
|
|
5199
|
+
let fd;
|
|
5200
|
+
try {
|
|
5201
|
+
if (!fs6.lstatSync(file).isFile())
|
|
5202
|
+
return null;
|
|
5203
|
+
fd = fs6.openSync(file, "r");
|
|
5204
|
+
const buffer = Buffer.alloc(SESSION_META_BYTES);
|
|
5205
|
+
const read = fs6.readSync(fd, buffer, 0, SESSION_META_BYTES, 0);
|
|
5206
|
+
const end = buffer.subarray(0, read).indexOf(10);
|
|
5207
|
+
if (end < 0)
|
|
5208
|
+
return null;
|
|
5209
|
+
const line = JSON.parse(buffer.subarray(0, end).toString("utf8"));
|
|
5210
|
+
const payload = line?.payload;
|
|
5211
|
+
if (line?.type !== "session_meta" || !payload || typeof payload !== "object" || Array.isArray(payload))
|
|
5212
|
+
return null;
|
|
5213
|
+
const source = payload.source;
|
|
5214
|
+
return {
|
|
5215
|
+
id: text2(payload.id),
|
|
5216
|
+
originator: text2(payload.originator),
|
|
5217
|
+
threadSource: text2(payload.thread_source),
|
|
5218
|
+
parent: text2(payload.parent_thread_id),
|
|
5219
|
+
subagent: source && typeof source === "object" ? text2(source.subagent) : null
|
|
5220
|
+
};
|
|
5221
|
+
} catch {
|
|
5222
|
+
return null;
|
|
5223
|
+
} finally {
|
|
5224
|
+
if (fd !== undefined)
|
|
5225
|
+
fs6.closeSync(fd);
|
|
5226
|
+
}
|
|
5227
|
+
}
|
|
5228
|
+
var ownThread = (input, thread) => Boolean(input.session_id) && thread?.id === input.session_id && thread?.threadSource === "user";
|
|
5229
|
+
var childThread = (input, thread) => input.session_id && thread?.threadSource === "subagent" && thread.parent === input.session_id && thread.id !== null && thread.id !== input.session_id && /^[\x21-\x7e]{1,200}$/.test(thread.id) ? thread.id : null;
|
|
5189
5230
|
|
|
5190
5231
|
// server/node_modules/kysely/dist/util/object-utils.js
|
|
5191
5232
|
function isUndefined(obj) {
|
|
@@ -16259,7 +16300,7 @@ function connectWriter(role, file = dbFile(), create = false, busyMs) {
|
|
|
16259
16300
|
const raw = new DatabaseSync2(file);
|
|
16260
16301
|
try {
|
|
16261
16302
|
prepare(raw, create ? "none" : role === "ingest" || role === "forget" ? "revision" : "generation", busyMs);
|
|
16262
|
-
raw.function("sphica_terms", { deterministic: true }, (
|
|
16303
|
+
raw.function("sphica_terms", { deterministic: true }, (text3) => terms(String(text3 ?? "")).join(" "));
|
|
16263
16304
|
} catch (e) {
|
|
16264
16305
|
raw.close();
|
|
16265
16306
|
throw e;
|
|
@@ -16293,17 +16334,39 @@ function openWriter(role, file = dbFile(), busyMs) {
|
|
|
16293
16334
|
// server/src/knowledge.ts
|
|
16294
16335
|
var sessionId = (projectId, host, externalId) => uuidFrom(String(projectId), host, externalId);
|
|
16295
16336
|
|
|
16296
|
-
// server/src/
|
|
16337
|
+
// server/src/once.ts
|
|
16297
16338
|
import fs7 from "node:fs";
|
|
16339
|
+
import os4 from "node:os";
|
|
16298
16340
|
import path7 from "node:path";
|
|
16341
|
+
function markOnce(kind, key) {
|
|
16342
|
+
const user = (() => {
|
|
16343
|
+
try {
|
|
16344
|
+
return os4.userInfo().username;
|
|
16345
|
+
} catch {
|
|
16346
|
+
return String(process.getuid?.() ?? "user");
|
|
16347
|
+
}
|
|
16348
|
+
})();
|
|
16349
|
+
const dir = path7.join(os4.tmpdir(), `sphica-${sha2562(user).toString("hex").slice(0, 12)}`, kind);
|
|
16350
|
+
try {
|
|
16351
|
+
fs7.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
16352
|
+
fs7.writeFileSync(path7.join(dir, sha2562(key).toString("hex").slice(0, 24)), "", { flag: "wx" });
|
|
16353
|
+
return true;
|
|
16354
|
+
} catch (e) {
|
|
16355
|
+
return e.code !== "EEXIST";
|
|
16356
|
+
}
|
|
16357
|
+
}
|
|
16358
|
+
|
|
16359
|
+
// server/src/project.ts
|
|
16360
|
+
import fs9 from "node:fs";
|
|
16361
|
+
import path9 from "node:path";
|
|
16299
16362
|
|
|
16300
16363
|
// server/src/anchors.ts
|
|
16301
|
-
import
|
|
16364
|
+
import path8 from "node:path";
|
|
16302
16365
|
var MAX_BYTES = 2 * 1024 * 1024;
|
|
16303
|
-
var leaves = (rel) => rel === ".." || rel.startsWith(`..${
|
|
16366
|
+
var leaves = (rel) => rel === ".." || rel.startsWith(`..${path8.sep}`) || path8.isAbsolute(rel);
|
|
16304
16367
|
|
|
16305
16368
|
// server/src/file-lock.ts
|
|
16306
|
-
import
|
|
16369
|
+
import fs8 from "node:fs";
|
|
16307
16370
|
var WAIT_MS = 5000;
|
|
16308
16371
|
var POLL_MS = 40;
|
|
16309
16372
|
var TRIES = 5;
|
|
@@ -16326,7 +16389,7 @@ function retryBusy(op) {
|
|
|
16326
16389
|
function heldBy(lock) {
|
|
16327
16390
|
let holder = 0;
|
|
16328
16391
|
try {
|
|
16329
|
-
holder = Number(
|
|
16392
|
+
holder = Number(fs8.readFileSync(lock, "utf8").trim()) || 0;
|
|
16330
16393
|
} catch {}
|
|
16331
16394
|
if (holder <= 0)
|
|
16332
16395
|
return `${lock} is held by another process. Run this again when it finishes.`;
|
|
@@ -16345,7 +16408,7 @@ function withFileLock(lock, fn, waitMs = WAIT_MS) {
|
|
|
16345
16408
|
let fd = null;
|
|
16346
16409
|
let refused = null;
|
|
16347
16410
|
try {
|
|
16348
|
-
fd =
|
|
16411
|
+
fd = fs8.openSync(lock, "wx");
|
|
16349
16412
|
} catch (e) {
|
|
16350
16413
|
const code = e.code;
|
|
16351
16414
|
if (code === "EPERM" || code === "EACCES" || code === "EBUSY")
|
|
@@ -16356,13 +16419,13 @@ function withFileLock(lock, fn, waitMs = WAIT_MS) {
|
|
|
16356
16419
|
if (fd !== null) {
|
|
16357
16420
|
try {
|
|
16358
16421
|
try {
|
|
16359
|
-
|
|
16422
|
+
fs8.writeFileSync(fd, mine);
|
|
16360
16423
|
} finally {
|
|
16361
|
-
|
|
16424
|
+
fs8.closeSync(fd);
|
|
16362
16425
|
}
|
|
16363
16426
|
} catch (e) {
|
|
16364
16427
|
try {
|
|
16365
|
-
retryBusy(() =>
|
|
16428
|
+
retryBusy(() => fs8.rmSync(lock, { force: true }));
|
|
16366
16429
|
} catch {}
|
|
16367
16430
|
throw e;
|
|
16368
16431
|
}
|
|
@@ -16389,7 +16452,7 @@ function release(lock, mine) {
|
|
|
16389
16452
|
let held = "";
|
|
16390
16453
|
try {
|
|
16391
16454
|
retryBusy(() => {
|
|
16392
|
-
held =
|
|
16455
|
+
held = fs8.readFileSync(lock, "utf8");
|
|
16393
16456
|
});
|
|
16394
16457
|
} catch (e) {
|
|
16395
16458
|
if (e.code === "ENOENT")
|
|
@@ -16399,32 +16462,32 @@ function release(lock, mine) {
|
|
|
16399
16462
|
if (held !== mine)
|
|
16400
16463
|
return;
|
|
16401
16464
|
try {
|
|
16402
|
-
retryBusy(() =>
|
|
16465
|
+
retryBusy(() => fs8.rmSync(lock, { force: true }));
|
|
16403
16466
|
} catch (e) {
|
|
16404
16467
|
throw cannot(e);
|
|
16405
16468
|
}
|
|
16406
16469
|
}
|
|
16407
|
-
function replaceFile(file,
|
|
16470
|
+
function replaceFile(file, text3) {
|
|
16408
16471
|
const tmp = `${file}.${process.pid}.tmp`;
|
|
16409
16472
|
try {
|
|
16410
|
-
const fd =
|
|
16473
|
+
const fd = fs8.openSync(tmp, "w");
|
|
16411
16474
|
try {
|
|
16412
|
-
|
|
16413
|
-
|
|
16475
|
+
fs8.writeFileSync(fd, text3);
|
|
16476
|
+
fs8.fsyncSync(fd);
|
|
16414
16477
|
} finally {
|
|
16415
|
-
|
|
16478
|
+
fs8.closeSync(fd);
|
|
16416
16479
|
}
|
|
16417
|
-
retryBusy(() =>
|
|
16480
|
+
retryBusy(() => fs8.renameSync(tmp, file));
|
|
16418
16481
|
} catch (e) {
|
|
16419
16482
|
try {
|
|
16420
|
-
retryBusy(() =>
|
|
16483
|
+
retryBusy(() => fs8.rmSync(tmp, { force: true }));
|
|
16421
16484
|
} catch {}
|
|
16422
16485
|
throw e;
|
|
16423
16486
|
}
|
|
16424
16487
|
}
|
|
16425
16488
|
|
|
16426
16489
|
// server/src/project.ts
|
|
16427
|
-
var localFile = () =>
|
|
16490
|
+
var localFile = () => path9.join(sphicaHome(), "projects.json");
|
|
16428
16491
|
var LOCAL_KEY = /^[a-z0-9][a-z0-9._-]*$/;
|
|
16429
16492
|
function legacyRemote(url) {
|
|
16430
16493
|
const raw = String(url ?? "").trim();
|
|
@@ -16463,7 +16526,7 @@ var top = (dir) => ask(() => topLevel(dir, { timeout: 5000 }));
|
|
|
16463
16526
|
function localMap() {
|
|
16464
16527
|
let raw;
|
|
16465
16528
|
try {
|
|
16466
|
-
raw =
|
|
16529
|
+
raw = fs9.readFileSync(localFile(), "utf8");
|
|
16467
16530
|
} catch (e) {
|
|
16468
16531
|
if (e.code === "ENOENT")
|
|
16469
16532
|
return {};
|
|
@@ -16475,13 +16538,13 @@ function localMap() {
|
|
|
16475
16538
|
} catch {
|
|
16476
16539
|
m = null;
|
|
16477
16540
|
}
|
|
16478
|
-
if (!m || typeof m !== "object" || Array.isArray(m) || Object.entries(m).some(([root, name]) => !
|
|
16541
|
+
if (!m || typeof m !== "object" || Array.isArray(m) || Object.entries(m).some(([root, name]) => !path9.isAbsolute(root) || typeof name !== "string" || !LOCAL_KEY.test(name)))
|
|
16479
16542
|
throw new Error(`${localFile()} is not a valid JSON project table. Fix or delete it, then name the project again.`);
|
|
16480
16543
|
return m;
|
|
16481
16544
|
}
|
|
16482
|
-
var rootOf = (dir) => top(
|
|
16545
|
+
var rootOf = (dir) => top(path9.resolve(dir)) || path9.resolve(dir);
|
|
16483
16546
|
function identify(dir) {
|
|
16484
|
-
const given =
|
|
16547
|
+
const given = path9.resolve(dir);
|
|
16485
16548
|
const found = top(given);
|
|
16486
16549
|
const root = found || given;
|
|
16487
16550
|
const legacy = found ? legacyRemote(ask(() => originUrl(root, { timeout: 5000 }))) : null;
|
|
@@ -16491,11 +16554,11 @@ function identify(dir) {
|
|
|
16491
16554
|
return { key, legacyKey: `git:${legacy}`, root, name: remote.split("/").slice(1).join("/") || remote };
|
|
16492
16555
|
}
|
|
16493
16556
|
const map = localMap();
|
|
16494
|
-
for (let d = root;; d =
|
|
16557
|
+
for (let d = root;; d = path9.dirname(d)) {
|
|
16495
16558
|
const local = map[d];
|
|
16496
16559
|
if (local && LOCAL_KEY.test(local))
|
|
16497
16560
|
return { key: `local:${local}`, legacyKey: `local:${local}`, root: d, name: local };
|
|
16498
|
-
if (found ||
|
|
16561
|
+
if (found || path9.dirname(d) === d)
|
|
16499
16562
|
return null;
|
|
16500
16563
|
}
|
|
16501
16564
|
}
|
|
@@ -16503,14 +16566,14 @@ function checkLocalName(name) {
|
|
|
16503
16566
|
if (!LOCAL_KEY.test(name))
|
|
16504
16567
|
throw new Error(`Use only lowercase letters, digits, and . _ - in the name: ${name}`);
|
|
16505
16568
|
}
|
|
16506
|
-
var repositoryRoot = (dir) => top(
|
|
16569
|
+
var repositoryRoot = (dir) => top(path9.resolve(dir));
|
|
16507
16570
|
function nameLocal(dir, name) {
|
|
16508
16571
|
checkLocalName(name);
|
|
16509
16572
|
const place = identify(dir);
|
|
16510
16573
|
if (place?.key.startsWith("git:"))
|
|
16511
16574
|
throw new Error(`${place.root} has a git remote, so its key is ${place.key}. Add it without --name.`);
|
|
16512
16575
|
const root = rootOf(dir);
|
|
16513
|
-
|
|
16576
|
+
fs9.mkdirSync(path9.dirname(localFile()), { recursive: true, mode: 448 });
|
|
16514
16577
|
withFileLock(`${localFile()}.lock`, () => {
|
|
16515
16578
|
const m = localMap();
|
|
16516
16579
|
m[root] = name;
|
|
@@ -16520,11 +16583,11 @@ function nameLocal(dir, name) {
|
|
|
16520
16583
|
return { key: `local:${name}`, root, name };
|
|
16521
16584
|
}
|
|
16522
16585
|
function relativeTo(root, file, cwd = root) {
|
|
16523
|
-
const abs =
|
|
16524
|
-
const rel =
|
|
16586
|
+
const abs = path9.resolve(cwd, file);
|
|
16587
|
+
const rel = path9.relative(root, abs);
|
|
16525
16588
|
if (!rel || leaves(rel) || /\p{Cc}/u.test(rel))
|
|
16526
16589
|
return null;
|
|
16527
|
-
return rel.split(
|
|
16590
|
+
return rel.split(path9.sep).join("/");
|
|
16528
16591
|
}
|
|
16529
16592
|
function patchPaths(patch) {
|
|
16530
16593
|
const out = [];
|
|
@@ -16538,8 +16601,8 @@ function patchPaths(patch) {
|
|
|
16538
16601
|
}
|
|
16539
16602
|
|
|
16540
16603
|
// server/src/worktree.ts
|
|
16541
|
-
import
|
|
16542
|
-
import
|
|
16604
|
+
import fs10 from "node:fs";
|
|
16605
|
+
import path10 from "node:path";
|
|
16543
16606
|
var MAX_PATHS = 200;
|
|
16544
16607
|
var LIMITS = { timeout: 5000, max: 16 * 1024 * 1024 };
|
|
16545
16608
|
var STATUS_DEADLINE = 5000;
|
|
@@ -16564,7 +16627,7 @@ async function snapshot(root) {
|
|
|
16564
16627
|
continue;
|
|
16565
16628
|
let st;
|
|
16566
16629
|
try {
|
|
16567
|
-
st =
|
|
16630
|
+
st = fs10.lstatSync(path10.join(root, p), { throwIfNoEntry: false });
|
|
16568
16631
|
} catch {
|
|
16569
16632
|
st = undefined;
|
|
16570
16633
|
}
|
|
@@ -16596,15 +16659,15 @@ function changed(root, before, after) {
|
|
|
16596
16659
|
}
|
|
16597
16660
|
|
|
16598
16661
|
// server/src/capture.ts
|
|
16599
|
-
var spoolDir = () =>
|
|
16600
|
-
var stateFile = () =>
|
|
16601
|
-
var rejectedDir = () =>
|
|
16602
|
-
var unregisteredDir = () =>
|
|
16603
|
-
var callsDir = () =>
|
|
16604
|
-
var callsRejectedDir = () =>
|
|
16662
|
+
var spoolDir = () => path11.join(sphicaHome(), "spool");
|
|
16663
|
+
var stateFile = () => path11.join(sphicaHome(), "capture.json");
|
|
16664
|
+
var rejectedDir = () => path11.join(spoolDir(), "rejected");
|
|
16665
|
+
var unregisteredDir = () => path11.join(spoolDir(), "unregistered");
|
|
16666
|
+
var callsDir = () => path11.join(spoolDir(), "calls");
|
|
16667
|
+
var callsRejectedDir = () => path11.join(callsDir(), "rejected");
|
|
16605
16668
|
var HOLD_DAYS = 30;
|
|
16606
16669
|
var HOLD_MAX = 1000;
|
|
16607
|
-
var baselineDir = () =>
|
|
16670
|
+
var baselineDir = () => path11.join(sphicaHome(), "worktree");
|
|
16608
16671
|
function current(raw) {
|
|
16609
16672
|
const r2 = raw;
|
|
16610
16673
|
if (typeof r2.project !== "string")
|
|
@@ -16653,7 +16716,7 @@ ${z}`,
|
|
|
16653
16716
|
function prune(held, removed = () => {}) {
|
|
16654
16717
|
let files;
|
|
16655
16718
|
try {
|
|
16656
|
-
files =
|
|
16719
|
+
files = fs11.readdirSync(held).filter((f) => f.endsWith(".json") && !f.startsWith(".")).sort();
|
|
16657
16720
|
} catch {
|
|
16658
16721
|
return;
|
|
16659
16722
|
}
|
|
@@ -16661,26 +16724,26 @@ function prune(held, removed = () => {}) {
|
|
|
16661
16724
|
const stale = files.filter((f) => Number(f.split("-")[0]) < cutoff);
|
|
16662
16725
|
const over = files.slice(0, Math.max(0, files.length - HOLD_MAX));
|
|
16663
16726
|
for (const f of new Set([...stale, ...over])) {
|
|
16664
|
-
|
|
16727
|
+
fs11.rmSync(path11.join(held, f), { force: true });
|
|
16665
16728
|
removed();
|
|
16666
16729
|
}
|
|
16667
16730
|
}
|
|
16668
16731
|
function put(dir, record) {
|
|
16669
|
-
|
|
16732
|
+
fs11.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
16670
16733
|
const name = `${Date.now()}-${process.pid}-${Math.random().toString(36).slice(2, 10)}.json`;
|
|
16671
|
-
const tmp =
|
|
16672
|
-
|
|
16673
|
-
|
|
16674
|
-
return
|
|
16734
|
+
const tmp = path11.join(dir, `.${name}`);
|
|
16735
|
+
fs11.writeFileSync(tmp, JSON.stringify(record), { mode: 384 });
|
|
16736
|
+
fs11.renameSync(tmp, path11.join(dir, name));
|
|
16737
|
+
return path11.join(dir, name);
|
|
16675
16738
|
}
|
|
16676
16739
|
function spool(record) {
|
|
16677
16740
|
put(spoolDir(), record);
|
|
16678
16741
|
}
|
|
16679
16742
|
var branchOf = (root) => {
|
|
16680
16743
|
try {
|
|
16681
|
-
const dotgit =
|
|
16682
|
-
const gitdir =
|
|
16683
|
-
const h2 =
|
|
16744
|
+
const dotgit = path11.join(root, ".git");
|
|
16745
|
+
const gitdir = fs11.statSync(dotgit).isFile() ? path11.resolve(root, fs11.readFileSync(dotgit, "utf8").match(/^gitdir: (.+)$/m)?.[1]?.trim() ?? "") : dotgit;
|
|
16746
|
+
const h2 = fs11.readFileSync(path11.join(gitdir, "HEAD"), "utf8").trim();
|
|
16684
16747
|
return h2.startsWith("ref: refs/heads/") ? h2.slice("ref: refs/heads/".length) : null;
|
|
16685
16748
|
} catch {
|
|
16686
16749
|
return null;
|
|
@@ -16726,7 +16789,7 @@ Notes: ${notes.trim()}` : "";
|
|
|
16726
16789
|
};
|
|
16727
16790
|
}
|
|
16728
16791
|
function captureNotice(file = dbFile()) {
|
|
16729
|
-
if (!
|
|
16792
|
+
if (!fs11.existsSync(file))
|
|
16730
16793
|
return panel("sphica: no database, so conversations are not recorded", [file], "Create it with sphica init");
|
|
16731
16794
|
const s = readState();
|
|
16732
16795
|
if (s.unreadable)
|
|
@@ -16737,15 +16800,15 @@ function captureNotice(file = dbFile()) {
|
|
|
16737
16800
|
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");
|
|
16738
16801
|
return null;
|
|
16739
16802
|
}
|
|
16740
|
-
var turnDir = (host, session) =>
|
|
16741
|
-
var startFile = (dir, turn) =>
|
|
16803
|
+
var turnDir = (host, session) => path11.join(baselineDir(), digest(`${host}\x00${session}`));
|
|
16804
|
+
var startFile = (dir, turn) => path11.join(dir, `${digest(turn)}.json`);
|
|
16742
16805
|
var isStart = (v) => {
|
|
16743
16806
|
const s = v;
|
|
16744
16807
|
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");
|
|
16745
16808
|
};
|
|
16746
16809
|
function readStart(file) {
|
|
16747
16810
|
try {
|
|
16748
|
-
const s = JSON.parse(
|
|
16811
|
+
const s = JSON.parse(fs11.readFileSync(file, "utf8"));
|
|
16749
16812
|
return isStart(s) ? s : null;
|
|
16750
16813
|
} catch {
|
|
16751
16814
|
return null;
|
|
@@ -16754,7 +16817,7 @@ function readStart(file) {
|
|
|
16754
16817
|
function readStarts(dir) {
|
|
16755
16818
|
let names;
|
|
16756
16819
|
try {
|
|
16757
|
-
names =
|
|
16820
|
+
names = fs11.readdirSync(dir);
|
|
16758
16821
|
} catch (e) {
|
|
16759
16822
|
return e.code === "ENOENT" ? [] : null;
|
|
16760
16823
|
}
|
|
@@ -16762,7 +16825,7 @@ function readStarts(dir) {
|
|
|
16762
16825
|
for (const name of names) {
|
|
16763
16826
|
if (name.startsWith("."))
|
|
16764
16827
|
continue;
|
|
16765
|
-
const s = readStart(
|
|
16828
|
+
const s = readStart(path11.join(dir, name));
|
|
16766
16829
|
if (!s)
|
|
16767
16830
|
return null;
|
|
16768
16831
|
starts.push(s);
|
|
@@ -16777,10 +16840,10 @@ function newestTurn(starts) {
|
|
|
16777
16840
|
return at.length === 1 ? at[0]?.turn ?? null : null;
|
|
16778
16841
|
}
|
|
16779
16842
|
function writeStart(dir, s, file = startFile(dir, s.turn)) {
|
|
16780
|
-
|
|
16781
|
-
const tmp =
|
|
16782
|
-
|
|
16783
|
-
|
|
16843
|
+
fs11.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
16844
|
+
const tmp = path11.join(dir, `.${path11.basename(file)}.${process.pid}.tmp`);
|
|
16845
|
+
fs11.writeFileSync(tmp, JSON.stringify(s), { mode: 384 });
|
|
16846
|
+
fs11.renameSync(tmp, file);
|
|
16784
16847
|
}
|
|
16785
16848
|
async function openTurn(dir, turn, root) {
|
|
16786
16849
|
if (readStart(startFile(dir, turn))?.running)
|
|
@@ -16810,22 +16873,22 @@ function stopTurn(dir, turn) {
|
|
|
16810
16873
|
}
|
|
16811
16874
|
function pruneBaselines() {
|
|
16812
16875
|
const cutoff = Date.now() - HOLD_DAYS * 24 * 60 * 60 * 1000;
|
|
16813
|
-
const fresh = (file) => (
|
|
16876
|
+
const fresh = (file) => (fs11.statSync(file, { throwIfNoEntry: false })?.mtimeMs ?? Date.now()) >= cutoff;
|
|
16814
16877
|
let names;
|
|
16815
16878
|
try {
|
|
16816
|
-
names =
|
|
16879
|
+
names = fs11.readdirSync(baselineDir());
|
|
16817
16880
|
} catch {
|
|
16818
16881
|
return;
|
|
16819
16882
|
}
|
|
16820
16883
|
for (const name of names) {
|
|
16821
|
-
const at =
|
|
16822
|
-
if (!
|
|
16884
|
+
const at = path11.join(baselineDir(), name);
|
|
16885
|
+
if (!fs11.statSync(at, { throwIfNoEntry: false })?.isDirectory()) {
|
|
16823
16886
|
if (!fresh(at))
|
|
16824
|
-
|
|
16887
|
+
fs11.rmSync(at, { force: true });
|
|
16825
16888
|
} else {
|
|
16826
|
-
const files =
|
|
16827
|
-
if (files.length && !files.some((f) => fresh(
|
|
16828
|
-
|
|
16889
|
+
const files = fs11.readdirSync(at);
|
|
16890
|
+
if (files.length && !files.some((f) => fresh(path11.join(at, f))))
|
|
16891
|
+
fs11.rmSync(at, { recursive: true, force: true });
|
|
16829
16892
|
}
|
|
16830
16893
|
}
|
|
16831
16894
|
}
|
|
@@ -16843,22 +16906,36 @@ async function attemptAsync(fn) {
|
|
|
16843
16906
|
return;
|
|
16844
16907
|
}
|
|
16845
16908
|
}
|
|
16909
|
+
function whoseTurn(host, input) {
|
|
16910
|
+
if (!isOwnerTurn(input, undefined, undefined, host === "codex" ? process.env.CODEX_THREAD_ID : undefined))
|
|
16911
|
+
return "other";
|
|
16912
|
+
if (host !== "codex")
|
|
16913
|
+
return "owner";
|
|
16914
|
+
const thread = codexThread(input);
|
|
16915
|
+
if (ownThread(input, thread))
|
|
16916
|
+
return "owner";
|
|
16917
|
+
return childThread(input, thread) === null ? "unconfirmed" : "other";
|
|
16918
|
+
}
|
|
16919
|
+
var unconfirmed = (input) => !markOnce("unconfirmed", `codex\x00${input.session_id}`) ? null : typeof input.transcript_path === "string" ? panel("sphica: this Codex session is not recorded", ["Its session log does not show that these messages are yours"], "Update Sphica and Codex; if it stays, report it") : panel("sphica: this Codex session is not recorded", [
|
|
16920
|
+
"Codex named no session log (as with --ephemeral), so Sphica cannot confirm these messages are yours"
|
|
16921
|
+
], "To record a session, start Codex without --ephemeral");
|
|
16846
16922
|
async function onHook(host, input) {
|
|
16847
16923
|
const event = input.hook_event_name;
|
|
16848
|
-
const
|
|
16924
|
+
const whose = whoseTurn(host, input);
|
|
16925
|
+
if (whose !== "owner")
|
|
16926
|
+
return {
|
|
16927
|
+
flush: false,
|
|
16928
|
+
notice: whose === "unconfirmed" && event === "UserPromptSubmit" ? unconfirmed(input) : null
|
|
16929
|
+
};
|
|
16849
16930
|
if (event === "SessionStart") {
|
|
16850
|
-
if (!owner())
|
|
16851
|
-
return { flush: false };
|
|
16852
16931
|
const file = process.env.CLAUDE_ENV_FILE;
|
|
16853
16932
|
if (file && input.session_id && /^[A-Za-z0-9_-]+$/.test(input.session_id)) {
|
|
16854
|
-
|
|
16933
|
+
fs11.appendFileSync(file, `export SPHICA_PARENT_SESSION=${input.session_id}
|
|
16855
16934
|
`);
|
|
16856
16935
|
}
|
|
16857
16936
|
attempt(pruneBaselines);
|
|
16858
16937
|
return { flush: false, notice: captureNotice() };
|
|
16859
16938
|
}
|
|
16860
|
-
if (!owner())
|
|
16861
|
-
return { flush: false };
|
|
16862
16939
|
if (event === "Interrupt") {
|
|
16863
16940
|
const turn2 = input.prompt_id ?? input.turn_id;
|
|
16864
16941
|
if (turn2)
|
|
@@ -16934,7 +17011,7 @@ async function onHook(host, input) {
|
|
|
16934
17011
|
function readStateFile() {
|
|
16935
17012
|
let raw = {};
|
|
16936
17013
|
try {
|
|
16937
|
-
const parsed = JSON.parse(
|
|
17014
|
+
const parsed = JSON.parse(fs11.readFileSync(stateFile(), "utf8"));
|
|
16938
17015
|
if (parsed && typeof parsed === "object")
|
|
16939
17016
|
raw = parsed;
|
|
16940
17017
|
} catch {}
|
|
@@ -16948,7 +17025,7 @@ function readStateFile() {
|
|
|
16948
17025
|
}
|
|
16949
17026
|
function writeState(s) {
|
|
16950
17027
|
try {
|
|
16951
|
-
|
|
17028
|
+
fs11.writeFileSync(stateFile(), JSON.stringify({ ...s, pruned: s.pruned ?? readStateFile().pruned }));
|
|
16952
17029
|
} catch {}
|
|
16953
17030
|
}
|
|
16954
17031
|
function readState() {
|
|
@@ -16964,7 +17041,7 @@ function readState() {
|
|
|
16964
17041
|
}
|
|
16965
17042
|
function listQueue(dir) {
|
|
16966
17043
|
try {
|
|
16967
|
-
const names =
|
|
17044
|
+
const names = fs11.readdirSync(dir);
|
|
16968
17045
|
return {
|
|
16969
17046
|
files: names.filter((f) => f.endsWith(".json") && !f.startsWith(".")),
|
|
16970
17047
|
temp: names.filter((f) => f.endsWith(".json") && f.startsWith(".")),
|
|
@@ -16990,7 +17067,7 @@ function queueReport(now = Date.now()) {
|
|
|
16990
17067
|
const temp = { count: 0, oldestMs: 0, bytes: 0 };
|
|
16991
17068
|
for (const name of listed[k].temp)
|
|
16992
17069
|
try {
|
|
16993
|
-
const st =
|
|
17070
|
+
const st = fs11.statSync(path11.join(dirs[k], name));
|
|
16994
17071
|
const age = now - st.mtimeMs;
|
|
16995
17072
|
if (age < TEMP_AGE_MS)
|
|
16996
17073
|
continue;
|
|
@@ -17005,9 +17082,9 @@ function queueReport(now = Date.now()) {
|
|
|
17005
17082
|
for (const name of listed[k].files ?? []) {
|
|
17006
17083
|
let why = "unknown";
|
|
17007
17084
|
try {
|
|
17008
|
-
const
|
|
17009
|
-
if (REASON.test(
|
|
17010
|
-
why =
|
|
17085
|
+
const text3 = fs11.readFileSync(path11.join(dirs[k], `${name}.reason`), "utf8");
|
|
17086
|
+
if (REASON.test(text3))
|
|
17087
|
+
why = text3;
|
|
17011
17088
|
} catch {}
|
|
17012
17089
|
out[why] = (out[why] ?? 0) + 1;
|
|
17013
17090
|
}
|
|
@@ -17015,7 +17092,7 @@ function queueReport(now = Date.now()) {
|
|
|
17015
17092
|
};
|
|
17016
17093
|
const read = (dir, name) => {
|
|
17017
17094
|
try {
|
|
17018
|
-
return JSON.parse(
|
|
17095
|
+
return JSON.parse(fs11.readFileSync(path11.join(dir, name), "utf8"));
|
|
17019
17096
|
} catch {
|
|
17020
17097
|
return null;
|
|
17021
17098
|
}
|
|
@@ -17044,25 +17121,25 @@ function queueReport(now = Date.now()) {
|
|
|
17044
17121
|
};
|
|
17045
17122
|
}
|
|
17046
17123
|
function lock() {
|
|
17047
|
-
const file =
|
|
17048
|
-
|
|
17124
|
+
const file = path11.join(spoolDir(), ".lock");
|
|
17125
|
+
fs11.mkdirSync(spoolDir(), { recursive: true, mode: 448 });
|
|
17049
17126
|
for (let attempt2 = 0;attempt2 < 2; attempt2++) {
|
|
17050
17127
|
try {
|
|
17051
|
-
|
|
17128
|
+
fs11.writeFileSync(file, String(process.pid), { flag: "wx", mode: 384 });
|
|
17052
17129
|
return () => {
|
|
17053
17130
|
try {
|
|
17054
|
-
if (
|
|
17055
|
-
|
|
17131
|
+
if (fs11.readFileSync(file, "utf8") === String(process.pid))
|
|
17132
|
+
fs11.rmSync(file, { force: true });
|
|
17056
17133
|
} catch {}
|
|
17057
17134
|
};
|
|
17058
17135
|
} catch (e) {
|
|
17059
17136
|
if (e.code !== "EEXIST")
|
|
17060
17137
|
throw e;
|
|
17061
17138
|
}
|
|
17062
|
-
const st =
|
|
17139
|
+
const st = fs11.statSync(file, { throwIfNoEntry: false });
|
|
17063
17140
|
if (!st)
|
|
17064
17141
|
continue;
|
|
17065
|
-
const holder = Number(
|
|
17142
|
+
const holder = Number(fs11.readFileSync(file, "utf8") || 0);
|
|
17066
17143
|
const fresh = Date.now() - st.mtimeMs < 5 * 60000;
|
|
17067
17144
|
const alive = (() => {
|
|
17068
17145
|
if (holder <= 0)
|
|
@@ -17075,7 +17152,7 @@ function lock() {
|
|
|
17075
17152
|
})();
|
|
17076
17153
|
if (alive && fresh)
|
|
17077
17154
|
return null;
|
|
17078
|
-
|
|
17155
|
+
fs11.rmSync(file, { force: true });
|
|
17079
17156
|
}
|
|
17080
17157
|
return null;
|
|
17081
17158
|
}
|
|
@@ -17174,23 +17251,23 @@ var REJECTED = new Set([18, 19, 20, 25]);
|
|
|
17174
17251
|
var rejected = (e) => REJECTED.has(sqliteCode(e) ?? -1);
|
|
17175
17252
|
var queued = (from) => {
|
|
17176
17253
|
try {
|
|
17177
|
-
return
|
|
17254
|
+
return fs11.readdirSync(from).filter((f) => f.endsWith(".json") && !f.startsWith(".")).sort().map((name) => ({ name, from }));
|
|
17178
17255
|
} catch {
|
|
17179
17256
|
return [];
|
|
17180
17257
|
}
|
|
17181
17258
|
};
|
|
17182
17259
|
var CODE_NAMES = { 18: "TOOBIG", 19: "CONSTRAINT", 20: "MISMATCH", 25: "RANGE" };
|
|
17183
17260
|
function setAside(from, name, dir, reason2) {
|
|
17184
|
-
|
|
17261
|
+
fs11.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
17185
17262
|
try {
|
|
17186
|
-
|
|
17263
|
+
fs11.renameSync(path11.join(from, name), path11.join(dir, name));
|
|
17187
17264
|
} catch (e) {
|
|
17188
17265
|
if (e.code === "ENOENT")
|
|
17189
17266
|
return;
|
|
17190
17267
|
throw e;
|
|
17191
17268
|
}
|
|
17192
17269
|
try {
|
|
17193
|
-
|
|
17270
|
+
fs11.writeFileSync(path11.join(dir, `${name}.reason`), reason2, { mode: 384 });
|
|
17194
17271
|
} catch {}
|
|
17195
17272
|
}
|
|
17196
17273
|
async function sendBatch(db, names) {
|
|
@@ -17200,7 +17277,7 @@ async function sendBatch(db, names) {
|
|
|
17200
17277
|
let raw;
|
|
17201
17278
|
let r2;
|
|
17202
17279
|
try {
|
|
17203
|
-
raw = JSON.parse(
|
|
17280
|
+
raw = JSON.parse(fs11.readFileSync(path11.join(from, name), "utf8"));
|
|
17204
17281
|
} catch {
|
|
17205
17282
|
setAside(from, name, rejectedDir(), "unreadable");
|
|
17206
17283
|
continue;
|
|
@@ -17214,7 +17291,7 @@ async function sendBatch(db, names) {
|
|
|
17214
17291
|
if (r2)
|
|
17215
17292
|
records.push({ name, from, r: r2 });
|
|
17216
17293
|
else
|
|
17217
|
-
|
|
17294
|
+
fs11.rmSync(path11.join(from, name), { force: true });
|
|
17218
17295
|
}
|
|
17219
17296
|
let sent = 0;
|
|
17220
17297
|
let strayed = [];
|
|
@@ -17242,12 +17319,12 @@ async function sendBatch(db, names) {
|
|
|
17242
17319
|
for (const x of bad)
|
|
17243
17320
|
setAside(x.from, x.name, rejectedDir(), x.why);
|
|
17244
17321
|
if (strayed.length) {
|
|
17245
|
-
|
|
17322
|
+
fs11.mkdirSync(held, { recursive: true, mode: 448 });
|
|
17246
17323
|
for (const x of strayed) {
|
|
17247
17324
|
if (x.from === held)
|
|
17248
17325
|
continue;
|
|
17249
17326
|
try {
|
|
17250
|
-
|
|
17327
|
+
fs11.renameSync(path11.join(x.from, x.name), path11.join(held, x.name));
|
|
17251
17328
|
} catch (e) {
|
|
17252
17329
|
if (e.code !== "ENOENT")
|
|
17253
17330
|
throw e;
|
|
@@ -17257,7 +17334,7 @@ async function sendBatch(db, names) {
|
|
|
17257
17334
|
const moved = new Set([...bad, ...strayed].map((x) => x.name));
|
|
17258
17335
|
for (const x of records)
|
|
17259
17336
|
if (!moved.has(x.name))
|
|
17260
|
-
|
|
17337
|
+
fs11.rmSync(path11.join(x.from, x.name), { force: true });
|
|
17261
17338
|
return { sent, rejected: bad.length };
|
|
17262
17339
|
}
|
|
17263
17340
|
async function sendObservations(db, names) {
|
|
@@ -17268,16 +17345,16 @@ async function sendObservations(db, names) {
|
|
|
17268
17345
|
};
|
|
17269
17346
|
const rows = [];
|
|
17270
17347
|
for (const { name, from } of names) {
|
|
17271
|
-
let
|
|
17348
|
+
let text3;
|
|
17272
17349
|
try {
|
|
17273
|
-
|
|
17350
|
+
text3 = fs11.readFileSync(path11.join(from, name), "utf8");
|
|
17274
17351
|
} catch {
|
|
17275
17352
|
continue;
|
|
17276
17353
|
}
|
|
17277
17354
|
let o = null;
|
|
17278
17355
|
let why = "unreadable";
|
|
17279
17356
|
try {
|
|
17280
|
-
o = observation(JSON.parse(
|
|
17357
|
+
o = observation(JSON.parse(text3));
|
|
17281
17358
|
why = "shape";
|
|
17282
17359
|
} catch {}
|
|
17283
17360
|
if (o)
|
|
@@ -17309,7 +17386,7 @@ async function sendObservations(db, names) {
|
|
|
17309
17386
|
aside(x.name, why);
|
|
17310
17387
|
else
|
|
17311
17388
|
try {
|
|
17312
|
-
|
|
17389
|
+
fs11.rmSync(path11.join(callsDir(), x.name), { force: true });
|
|
17313
17390
|
} catch {}
|
|
17314
17391
|
}
|
|
17315
17392
|
return true;
|
|
@@ -17452,7 +17529,7 @@ async function observeRecordCall(file, input, owner, waitMs = OBSERVE_WAIT_MS) {
|
|
|
17452
17529
|
}
|
|
17453
17530
|
if (kept)
|
|
17454
17531
|
try {
|
|
17455
|
-
|
|
17532
|
+
fs11.rmSync(kept, { force: true });
|
|
17456
17533
|
} catch {}
|
|
17457
17534
|
}
|
|
17458
17535
|
async function main() {
|
|
@@ -17488,23 +17565,23 @@ import { styleText as styleText3 } from "node:util";
|
|
|
17488
17565
|
var colored2 = () => Boolean(process.stdout.isTTY && process.stderr.isTTY) && !process.env.NO_COLOR;
|
|
17489
17566
|
var columns = () => process.stdout.isTTY ? Math.max(10, process.stdout.columns || 100) : 1e4;
|
|
17490
17567
|
function capture(draw) {
|
|
17491
|
-
let
|
|
17568
|
+
let text3 = "";
|
|
17492
17569
|
const output = new Writable({
|
|
17493
17570
|
write(chunk, _encoding, done) {
|
|
17494
|
-
|
|
17571
|
+
text3 += String(chunk);
|
|
17495
17572
|
done();
|
|
17496
17573
|
}
|
|
17497
17574
|
});
|
|
17498
17575
|
Object.assign(output, { columns: columns(), isTTY: true });
|
|
17499
17576
|
draw(output);
|
|
17500
|
-
return
|
|
17577
|
+
return text3.replace(/\n$/, "");
|
|
17501
17578
|
}
|
|
17502
17579
|
var SGR = /(\u001b\[[0-9;]*m)/;
|
|
17503
|
-
var clean2 = (
|
|
17504
|
-
var oneLine = (
|
|
17505
|
-
var bold = (
|
|
17506
|
-
var dim = (
|
|
17507
|
-
var plainLine = (
|
|
17580
|
+
var clean2 = (text3) => text3.split(SGR).map((piece, i2) => i2 % 2 ? piece : plain(piece)).join("");
|
|
17581
|
+
var oneLine = (text3) => clean2(text3).replace(/[\n\t]+/g, " ");
|
|
17582
|
+
var bold = (text3) => styleText3("bold", text3);
|
|
17583
|
+
var dim = (text3) => styleText3("dim", text3);
|
|
17584
|
+
var plainLine = (text3) => wrapAnsi(oneLine(text3), columns() - 2, { hard: true, trim: true }).split(`
|
|
17508
17585
|
`).join(`
|
|
17509
17586
|
`);
|
|
17510
17587
|
var GUIDE = 3;
|
|
@@ -17517,8 +17594,8 @@ function wrapLine(line, room) {
|
|
|
17517
17594
|
const cells = MARKED.test(body) ? /^(.*\S\s{2,})(\S.*)$/.exec(body) : null;
|
|
17518
17595
|
const label = cells?.[1] ?? "";
|
|
17519
17596
|
const hang = lead + dist_default2(label);
|
|
17520
|
-
const [head2,
|
|
17521
|
-
return wrapAnsi(
|
|
17597
|
+
const [head2, text3, at] = cells?.[2] && room - hang >= 10 ? [label, cells[2], hang] : ["", body, lead];
|
|
17598
|
+
return wrapAnsi(text3, Math.max(1, room - at), { hard: true, trim: true }).split(`
|
|
17522
17599
|
`).map((part, i2) => i2 === 0 ? `${" ".repeat(lead)}${head2}${part}` : `${" ".repeat(at)}${part}`);
|
|
17523
17600
|
}
|
|
17524
17601
|
function content(lines, spacing = 0) {
|
|
@@ -17532,29 +17609,29 @@ function content(lines, spacing = 0) {
|
|
|
17532
17609
|
const wrapped = cleaned.flatMap((l) => wrapLine(l, columns() - GUIDE));
|
|
17533
17610
|
return capture((output) => log.message(wrapped, { output, spacing }));
|
|
17534
17611
|
}
|
|
17535
|
-
function title2(
|
|
17612
|
+
function title2(text3, meta) {
|
|
17536
17613
|
if (!colored2())
|
|
17537
|
-
return plainLine(
|
|
17538
|
-
const t2 = cut(oneLine(
|
|
17614
|
+
return plainLine(text3);
|
|
17615
|
+
const t2 = cut(oneLine(text3), columns() - GUIDE - 2);
|
|
17539
17616
|
const room = columns() - GUIDE - dist_default2(t2) - 2;
|
|
17540
17617
|
const extra = meta && room > 8 ? ` ${cut(oneLine(meta), room - 2)}` : "";
|
|
17541
17618
|
return capture((output) => intro(`${styleText3(["inverse", "bold"], ` ${t2} `)}${extra ? dim(extra) : ""}`, { output }));
|
|
17542
17619
|
}
|
|
17543
|
-
function section(
|
|
17544
|
-
const t2 = oneLine(
|
|
17620
|
+
function section(text3, gap = false) {
|
|
17621
|
+
const t2 = oneLine(text3);
|
|
17545
17622
|
if (!colored2())
|
|
17546
17623
|
return `${gap ? `
|
|
17547
17624
|
` : ""} ${t2}`;
|
|
17548
17625
|
return capture((output) => log.step(bold(t2), { output }));
|
|
17549
17626
|
}
|
|
17550
|
-
function indent(
|
|
17551
|
-
return content(
|
|
17627
|
+
function indent(text3) {
|
|
17628
|
+
return content(text3.split(`
|
|
17552
17629
|
`));
|
|
17553
17630
|
}
|
|
17554
|
-
function closing(
|
|
17631
|
+
function closing(text3) {
|
|
17555
17632
|
if (!colored2())
|
|
17556
|
-
return plainLine(
|
|
17557
|
-
const t2 = oneLine(
|
|
17633
|
+
return plainLine(text3);
|
|
17634
|
+
const t2 = oneLine(text3);
|
|
17558
17635
|
const lines = wrapAnsi(t2, columns() - GUIDE, { hard: true, trim: true }).split(`
|
|
17559
17636
|
`);
|
|
17560
17637
|
return capture((output) => outro(bold(lines.join(`
|
|
@@ -17582,10 +17659,10 @@ function stopped(reason2) {
|
|
|
17582
17659
|
cancel("Stopped", { output });
|
|
17583
17660
|
}).replace(/\n+$/, "");
|
|
17584
17661
|
}
|
|
17585
|
-
var cell = (
|
|
17586
|
-
var cut = (
|
|
17662
|
+
var cell = (text3) => oneLine(text3).trim();
|
|
17663
|
+
var cut = (text3, room) => dist_default2(text3) <= room ? text3 : `${wrapAnsi(text3, Math.max(1, room - 1), { hard: true }).split(`
|
|
17587
17664
|
`)[0]}…`;
|
|
17588
|
-
var padTo = (
|
|
17665
|
+
var padTo = (text3, to) => text3 + " ".repeat(Math.max(0, to - dist_default2(text3)));
|
|
17589
17666
|
function drawBlock(b) {
|
|
17590
17667
|
const fancy = colored2();
|
|
17591
17668
|
const space = fancy ? 1 : 0;
|
|
@@ -17601,8 +17678,8 @@ function drawBlock(b) {
|
|
|
17601
17678
|
const line = (r2, head2) => {
|
|
17602
17679
|
const before = r2.slice(0, -1).map((c, i2) => padTo(cut(c, widths[i2] ?? 0), (widths[i2] ?? 0) + 3));
|
|
17603
17680
|
const used = before.reduce((w, c) => w + dist_default2(c), 0);
|
|
17604
|
-
const
|
|
17605
|
-
return head2 ? dim(
|
|
17681
|
+
const text3 = before.join("") + cut(r2.at(-1) ?? "", Math.max(1, room - used));
|
|
17682
|
+
return head2 ? dim(text3) : text3;
|
|
17606
17683
|
};
|
|
17607
17684
|
return capture((output) => log.message(rows.flatMap((r2, i2) => wrapAnsi(line(r2, i2 === 0), room, { hard: true, trim: false }).split(`
|
|
17608
17685
|
`)), { output, spacing: space }));
|
|
@@ -17638,10 +17715,10 @@ function drawBlock(b) {
|
|
|
17638
17715
|
case "note": {
|
|
17639
17716
|
if (!fancy)
|
|
17640
17717
|
return content([cell(b.text)]);
|
|
17641
|
-
const
|
|
17718
|
+
const text3 = wrapAnsi(cell(b.text), columns() - GUIDE, { hard: true, trim: true });
|
|
17642
17719
|
return capture((output) => {
|
|
17643
17720
|
const say = { info: log.info, warning: log.warn, error: log.error, success: log.success }[b.tone];
|
|
17644
|
-
say(
|
|
17721
|
+
say(text3, { output });
|
|
17645
17722
|
});
|
|
17646
17723
|
}
|
|
17647
17724
|
}
|
|
@@ -17881,16 +17958,16 @@ function settleForMigration(io, runs, at) {
|
|
|
17881
17958
|
}
|
|
17882
17959
|
|
|
17883
17960
|
// server/src/admin.ts
|
|
17884
|
-
var say = (
|
|
17885
|
-
var SCHEMA = () =>
|
|
17886
|
-
var MIGRATIONS = () =>
|
|
17887
|
-
var MIGRATION = (dir, revision2) =>
|
|
17888
|
-
var CHECK = (dir, revision2) =>
|
|
17961
|
+
var say = (text3) => console.log(indent(text3));
|
|
17962
|
+
var SCHEMA = () => path12.join(dbDir(), "schema.sql");
|
|
17963
|
+
var MIGRATIONS = () => path12.join(dbDir(), "migrations");
|
|
17964
|
+
var MIGRATION = (dir, revision2) => path12.join(dir, `${String(revision2).padStart(4, "0")}.sql`);
|
|
17965
|
+
var CHECK = (dir, revision2) => path12.join(dir, `${String(revision2).padStart(4, "0")}.check.sql`);
|
|
17889
17966
|
var STOP2 = "sphica_migration_stop";
|
|
17890
17967
|
var NOTE = "sphica_migration_note";
|
|
17891
17968
|
var KEEP2 = 3;
|
|
17892
17969
|
function backUp(raw, file, from) {
|
|
17893
|
-
|
|
17970
|
+
fs12.mkdirSync(backupDir(file), { recursive: true, mode: 448 });
|
|
17894
17971
|
const done = backupPath(file, from);
|
|
17895
17972
|
const partial = `${done}.partial`;
|
|
17896
17973
|
try {
|
|
@@ -17906,10 +17983,10 @@ function backUp(raw, file, from) {
|
|
|
17906
17983
|
} finally {
|
|
17907
17984
|
look.close();
|
|
17908
17985
|
}
|
|
17909
|
-
|
|
17986
|
+
fs12.renameSync(partial, done);
|
|
17910
17987
|
} catch (e) {
|
|
17911
17988
|
for (const f of [partial, `${partial}-wal`, `${partial}-shm`])
|
|
17912
|
-
|
|
17989
|
+
fs12.rmSync(f, { force: true });
|
|
17913
17990
|
throw new Error(`Could not back up ${file} before migrating it, so nothing was migrated (${e.message}). Check free space in ${backupDir(file)}.`);
|
|
17914
17991
|
}
|
|
17915
17992
|
return done;
|
|
@@ -17923,7 +18000,7 @@ function prune2(file, kept) {
|
|
|
17923
18000
|
}
|
|
17924
18001
|
for (const old of all.filter((b) => b !== kept).slice(KEEP2 - 1))
|
|
17925
18002
|
try {
|
|
17926
|
-
|
|
18003
|
+
fs12.rmSync(old, { force: true });
|
|
17927
18004
|
} catch {}
|
|
17928
18005
|
}
|
|
17929
18006
|
var versionOf = (raw) => raw.prepare("pragma user_version").get().user_version;
|
|
@@ -18015,27 +18092,27 @@ function migrate(file = dbFile(), dir = MIGRATIONS()) {
|
|
|
18015
18092
|
try {
|
|
18016
18093
|
for (let r2 = from + 1;r2 <= SCHEMA_REVISION; r2++) {
|
|
18017
18094
|
const script = MIGRATION(dir, r2);
|
|
18018
|
-
if (!
|
|
18095
|
+
if (!fs12.existsSync(script))
|
|
18019
18096
|
throw new Error(`This Sphica has no migration script for revision ${r2} (${script} is missing). Reinstall sphica (\`npm i -g sphica@${packageVersionAt(ROOT) ?? "latest"}\`), then run \`sphica init\` again.`);
|
|
18020
18097
|
raw.exec("pragma foreign_keys = off");
|
|
18021
18098
|
try {
|
|
18022
18099
|
const notes = immediate(raw, () => {
|
|
18023
18100
|
if (versionOf(raw) !== r2 - 1)
|
|
18024
18101
|
return [];
|
|
18025
|
-
if (
|
|
18026
|
-
raw.exec(
|
|
18102
|
+
if (fs12.existsSync(CHECK(dir, r2))) {
|
|
18103
|
+
raw.exec(fs12.readFileSync(CHECK(dir, r2), "utf8"));
|
|
18027
18104
|
const stops = taken(raw, STOP2);
|
|
18028
18105
|
if (stops.length)
|
|
18029
18106
|
throw new Stop(r2, stops);
|
|
18030
18107
|
}
|
|
18031
|
-
raw.exec(
|
|
18108
|
+
raw.exec(fs12.readFileSync(script, "utf8"));
|
|
18032
18109
|
if (r2 === 10)
|
|
18033
18110
|
judgeEveryRecord(raw);
|
|
18034
18111
|
const broken = raw.prepare("pragma foreign_key_check").all().length;
|
|
18035
18112
|
if (broken)
|
|
18036
18113
|
throw new Error(`Migration to revision ${r2} left ${plural(broken, "broken reference")}; nothing was changed`);
|
|
18037
18114
|
if (versionOf(raw) !== r2)
|
|
18038
|
-
throw new Error(`${
|
|
18115
|
+
throw new Error(`${path12.basename(script)} did not set revision ${r2}`);
|
|
18039
18116
|
return taken(raw, NOTE);
|
|
18040
18117
|
});
|
|
18041
18118
|
if (notes.length)
|
|
@@ -18053,16 +18130,16 @@ ${e.list}` : "";
|
|
|
18053
18130
|
if (now === from) {
|
|
18054
18131
|
let left = "the backup made for this run was removed";
|
|
18055
18132
|
try {
|
|
18056
|
-
|
|
18133
|
+
fs12.rmSync(backup, { force: true });
|
|
18057
18134
|
} catch {
|
|
18058
18135
|
left = `the backup made for this run is still at ${backup}; delete it yourself`;
|
|
18059
18136
|
}
|
|
18060
18137
|
const retry = sqliteCode(e) === SQLITE_BUSY ? " Another process is writing to the database. Run `sphica init` again when it has finished." : "";
|
|
18061
18138
|
throw new Error(`${said}. No migration step was committed: the database is still at revision ${from}, and there is no need to replace it with a backup (${left}).${retry}${list2}`);
|
|
18062
18139
|
}
|
|
18063
|
-
const base =
|
|
18140
|
+
const base = path12.basename(file);
|
|
18064
18141
|
const state = now === null ? "The committed revision could not be confirmed after the failure" : `The database is now at revision ${now} (this run, or another \`sphica init\` running at the same time, migrated it that far)`;
|
|
18065
|
-
throw new Error(`${said}. ${state}. The database before migrating is at ${backup} (anything recorded after it was made is not in it). To go back to it, close every session using Sphica, move ${base}, ${base}-wal, and ${base}-shm in ${
|
|
18142
|
+
throw new Error(`${said}. ${state}. The database before migrating is at ${backup} (anything recorded after it was made is not in it). To go back to it, close every session using Sphica, move ${base}, ${base}-wal, and ${base}-shm in ${path12.dirname(file)} aside, then copy the backup to ${file}.${list2}`);
|
|
18066
18143
|
}
|
|
18067
18144
|
optimize(raw);
|
|
18068
18145
|
prune2(file, backup);
|
|
@@ -18070,7 +18147,7 @@ ${e.list}` : "";
|
|
|
18070
18147
|
});
|
|
18071
18148
|
}
|
|
18072
18149
|
function dbInit(file = dbFile()) {
|
|
18073
|
-
if (
|
|
18150
|
+
if (fs12.existsSync(file)) {
|
|
18074
18151
|
const look = new DatabaseSync3(file, { readOnly: true });
|
|
18075
18152
|
let generation;
|
|
18076
18153
|
try {
|
|
@@ -18091,32 +18168,32 @@ function dbInit(file = dbFile()) {
|
|
|
18091
18168
|
}
|
|
18092
18169
|
return;
|
|
18093
18170
|
}
|
|
18094
|
-
|
|
18171
|
+
fs12.mkdirSync(path12.dirname(file), { recursive: true, mode: 448 });
|
|
18095
18172
|
const tmp = `${file}.${process.pid}.tmp`;
|
|
18096
|
-
|
|
18173
|
+
fs12.rmSync(tmp, { force: true });
|
|
18097
18174
|
try {
|
|
18098
18175
|
withOwner(tmp, (raw) => {
|
|
18099
18176
|
raw.exec("pragma journal_mode = wal");
|
|
18100
|
-
raw.exec(
|
|
18177
|
+
raw.exec(fs12.readFileSync(SCHEMA(), "utf8"));
|
|
18101
18178
|
if (versionOf(raw) !== SCHEMA_REVISION)
|
|
18102
18179
|
throw new Error(`db/schema.sql has user_version ${versionOf(raw)}, but the code expects ${SCHEMA_REVISION}`);
|
|
18103
18180
|
optimize(raw);
|
|
18104
18181
|
}, true);
|
|
18105
18182
|
const taken2 = () => new Error(`${file} already exists (another sphica init created it first). Run this again to check it.`);
|
|
18106
18183
|
withFileLock(`${file}.init.lock`, () => {
|
|
18107
|
-
if (
|
|
18184
|
+
if (fs12.existsSync(file))
|
|
18108
18185
|
throw taken2();
|
|
18109
18186
|
try {
|
|
18110
|
-
|
|
18187
|
+
fs12.linkSync(tmp, file);
|
|
18111
18188
|
} catch (e) {
|
|
18112
18189
|
if (e.code === "EEXIST")
|
|
18113
18190
|
throw taken2();
|
|
18114
|
-
|
|
18191
|
+
fs12.renameSync(tmp, file);
|
|
18115
18192
|
}
|
|
18116
18193
|
});
|
|
18117
18194
|
} finally {
|
|
18118
18195
|
for (const f of [tmp, `${tmp}-wal`, `${tmp}-shm`])
|
|
18119
|
-
|
|
18196
|
+
fs12.rmSync(f, { force: true });
|
|
18120
18197
|
}
|
|
18121
18198
|
say(`Created: ${file} (revision ${SCHEMA_REVISION})`);
|
|
18122
18199
|
}
|
|
@@ -18152,7 +18229,7 @@ function reindex(file = dbFile()) {
|
|
|
18152
18229
|
say(`Rebuilt the index: ${plural(counts.units, "unit")}, ${plural(counts.sources, "source")}`);
|
|
18153
18230
|
}
|
|
18154
18231
|
function inspect(file = dbFile()) {
|
|
18155
|
-
const size = (f) =>
|
|
18232
|
+
const size = (f) => fs12.existsSync(f) ? fs12.statSync(f).size : 0;
|
|
18156
18233
|
return withOwner(file, (raw) => {
|
|
18157
18234
|
const check = (table) => {
|
|
18158
18235
|
try {
|
|
@@ -19174,11 +19251,11 @@ async function adoptionStops(db, waiting, say2) {
|
|
|
19174
19251
|
continue;
|
|
19175
19252
|
const from = new Date(first.called_at).toLocaleString("sv-SE");
|
|
19176
19253
|
const n2 = group2.length;
|
|
19177
|
-
const
|
|
19254
|
+
const text3 = first.host === "claude-code" ? (() => {
|
|
19178
19255
|
const resend = group2.filter((c) => c.tool_use_id && waiting.has(c.tool_use_id)).length;
|
|
19179
19256
|
return `Claude Code replies from ${from} on are not adopted as AI decisions: ${plural(n2, "record tool call")} without a hook observation (${resend} waiting to be resent, ${n2 - resend} that cannot be)`;
|
|
19180
19257
|
})() : first.host === "codex" ? `Codex replies from ${from} on are not adopted as AI decisions: ${plural(n2, "record tool call")} that named no session` : `replies of every host from ${from} on are not adopted as AI decisions: ${plural(n2, "record tool call")} that named no host`;
|
|
19181
|
-
say2("warn", "AI adoption", `${named(first)}: ${
|
|
19258
|
+
say2("warn", "AI adoption", `${named(first)}: ${text3}`);
|
|
19182
19259
|
}
|
|
19183
19260
|
for (const t2 of turnless)
|
|
19184
19261
|
say2("warn", "AI adoption", `${named(t2)}: Claude Code replies in ${plural(Number(t2.sessions), "session")} from ${new Date(t2.first ?? "").toLocaleString("sv-SE")} on are not adopted as AI decisions: a record tool's hook did not know the turn`);
|
|
@@ -19191,9 +19268,9 @@ async function doctor(cwd) {
|
|
|
19191
19268
|
if (m === "fail")
|
|
19192
19269
|
process.exitCode = 1;
|
|
19193
19270
|
};
|
|
19194
|
-
const say2 = (m, label,
|
|
19271
|
+
const say2 = (m, label, text3) => {
|
|
19195
19272
|
count(m, label);
|
|
19196
|
-
console.log(indent(` ${mark(m)} ${pad(label, 26)}${
|
|
19273
|
+
console.log(indent(` ${mark(m)} ${pad(label, 26)}${text3}`));
|
|
19197
19274
|
};
|
|
19198
19275
|
console.log(title2("sphica doctor"));
|
|
19199
19276
|
const plugin = report(observe(identify(cwd)?.root ?? cwd));
|
|
@@ -19215,7 +19292,7 @@ async function doctor(cwd) {
|
|
|
19215
19292
|
runtime = false;
|
|
19216
19293
|
say2("fail", "Node", plain(reason(e)));
|
|
19217
19294
|
}
|
|
19218
|
-
const tree =
|
|
19295
|
+
const tree = fs13.lstatSync(path13.join(os5.homedir(), ".sphica"), { throwIfNoEntry: false }) ? repositoryRoot(cwd) : null;
|
|
19219
19296
|
if (tree) {
|
|
19220
19297
|
const place = await workerPlace(tree);
|
|
19221
19298
|
if (place === "ready")
|
|
@@ -19229,7 +19306,7 @@ async function doctor(cwd) {
|
|
|
19229
19306
|
let usable2 = false;
|
|
19230
19307
|
if (!runtime)
|
|
19231
19308
|
say2("none", "DB", "cannot check until Node is upgraded");
|
|
19232
|
-
else if (!
|
|
19309
|
+
else if (!fs13.existsSync(file))
|
|
19233
19310
|
say2("fail", "DB", `missing (${file}). Create it with sphica init`);
|
|
19234
19311
|
else {
|
|
19235
19312
|
try {
|
|
@@ -19317,7 +19394,7 @@ var GH_WHY = {
|
|
|
19317
19394
|
};
|
|
19318
19395
|
async function bindGitHub() {
|
|
19319
19396
|
const user = await ghUser();
|
|
19320
|
-
const line = (m,
|
|
19397
|
+
const line = (m, text3) => console.log(indent(`${mark(m)} ${text3}`));
|
|
19321
19398
|
if (!user.ok) {
|
|
19322
19399
|
line("warn", `GitHub account not bound: ${GH_WHY[user.reason]}`);
|
|
19323
19400
|
return;
|
|
@@ -19335,7 +19412,7 @@ async function bindGitHub() {
|
|
|
19335
19412
|
}
|
|
19336
19413
|
async function init(flags) {
|
|
19337
19414
|
const cwd = flags.cwd ?? process.cwd();
|
|
19338
|
-
if (!
|
|
19415
|
+
if (!fs13.statSync(cwd, { throwIfNoEntry: false })?.isDirectory())
|
|
19339
19416
|
throw new Error(`${cwd} is not a directory`);
|
|
19340
19417
|
const found = identify(cwd);
|
|
19341
19418
|
if (flags.name !== undefined) {
|
|
@@ -19364,7 +19441,7 @@ async function init(flags) {
|
|
|
19364
19441
|
async function uninstall(yes) {
|
|
19365
19442
|
const home = sphicaHome();
|
|
19366
19443
|
const inHome = (p) => {
|
|
19367
|
-
const rel =
|
|
19444
|
+
const rel = path13.relative(home, path13.resolve(p));
|
|
19368
19445
|
return rel === "" || !leaves(rel);
|
|
19369
19446
|
};
|
|
19370
19447
|
const where = [
|
|
@@ -19374,7 +19451,7 @@ async function uninstall(yes) {
|
|
|
19374
19451
|
await boxed("sphica uninstall", async () => {
|
|
19375
19452
|
if (process.env.SPHICA_HOME)
|
|
19376
19453
|
throw new Error("SPHICA_HOME is set; uninstall deletes only ~/.sphica. Unset SPHICA_HOME and run it again");
|
|
19377
|
-
if (!
|
|
19454
|
+
if (!fs13.existsSync(home))
|
|
19378
19455
|
console.log(indent(`${mark("none")} ${home} does not exist`));
|
|
19379
19456
|
else {
|
|
19380
19457
|
if (!yes) {
|
|
@@ -19386,7 +19463,7 @@ async function uninstall(yes) {
|
|
|
19386
19463
|
if (isCancel(answer) || !answer)
|
|
19387
19464
|
return "cancelled";
|
|
19388
19465
|
}
|
|
19389
|
-
|
|
19466
|
+
fs13.rmSync(home, { recursive: true, force: true });
|
|
19390
19467
|
console.log(indent(`${mark("ok")} deleted ${home}`));
|
|
19391
19468
|
}
|
|
19392
19469
|
if (where.length > 1)
|