sphica 0.6.41 → 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/THIRD_PARTY_NOTICES.md +2 -2
- package/dist/capture.js +311 -164
- package/dist/cli.js +501 -343
- package/dist/deliver.js +376 -219
- package/dist/git-worker.js +365 -0
- package/dist/mcp-record.js +489 -333
- package/dist/mcp.js +430 -250
- package/package.json +1 -1
- package/skills/review/reviewers/precedent.md +5 -3
package/dist/mcp-record.js
CHANGED
|
@@ -45549,14 +45549,14 @@ function adapt(raw) {
|
|
|
45549
45549
|
}
|
|
45550
45550
|
|
|
45551
45551
|
// server/src/sqlite.ts
|
|
45552
|
-
import
|
|
45553
|
-
import
|
|
45554
|
-
import
|
|
45552
|
+
import fs3 from "node:fs";
|
|
45553
|
+
import os2 from "node:os";
|
|
45554
|
+
import path3 from "node:path";
|
|
45555
45555
|
import { constants as C, DatabaseSync } from "node:sqlite";
|
|
45556
45556
|
|
|
45557
45557
|
// server/src/plugin.ts
|
|
45558
|
-
import
|
|
45559
|
-
import
|
|
45558
|
+
import fs2 from "node:fs";
|
|
45559
|
+
import path2 from "node:path";
|
|
45560
45560
|
import { fileURLToPath } from "node:url";
|
|
45561
45561
|
|
|
45562
45562
|
// server/node_modules/smol-toml/dist/error.js
|
|
@@ -45826,6 +45826,180 @@ var NO_MATCHER = new Set(["UserPromptSubmit", "Stop", "Interrupt"]);
|
|
|
45826
45826
|
var CONTEXT = new Set(["PreToolUse", "PostToolUse", "SessionStart", "UserPromptSubmit", "SubagentStart"]);
|
|
45827
45827
|
var SHORT = new Set(["SessionEnd", "Interrupt"]);
|
|
45828
45828
|
|
|
45829
|
+
// server/src/git.ts
|
|
45830
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
45831
|
+
import fs from "node:fs";
|
|
45832
|
+
import os from "node:os";
|
|
45833
|
+
import path from "node:path";
|
|
45834
|
+
var gitHome = () => path.join(os.homedir(), ".sphica", "git");
|
|
45835
|
+
function gitEnv(extra = {}) {
|
|
45836
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([k]) => !/^GIT_/i.test(k)));
|
|
45837
|
+
return {
|
|
45838
|
+
...env,
|
|
45839
|
+
GIT_ALLOW_PROTOCOL: "",
|
|
45840
|
+
GIT_NO_LAZY_FETCH: "1",
|
|
45841
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
45842
|
+
GIT_NO_REPLACE_OBJECTS: "1",
|
|
45843
|
+
...extra
|
|
45844
|
+
};
|
|
45845
|
+
}
|
|
45846
|
+
var gitOptions = (hooks = path.join(gitHome(), "hooks")) => [
|
|
45847
|
+
"-c",
|
|
45848
|
+
"core.fsmonitor=",
|
|
45849
|
+
"-c",
|
|
45850
|
+
`core.hooksPath=${hooks}`,
|
|
45851
|
+
"-c",
|
|
45852
|
+
"core.quotePath=false",
|
|
45853
|
+
"--no-pager",
|
|
45854
|
+
"--no-optional-locks"
|
|
45855
|
+
];
|
|
45856
|
+
var DIFF_OPTIONS = [
|
|
45857
|
+
"--no-color",
|
|
45858
|
+
"--no-ext-diff",
|
|
45859
|
+
"--no-textconv",
|
|
45860
|
+
"--ignore-submodules=dirty",
|
|
45861
|
+
"--submodule=short"
|
|
45862
|
+
];
|
|
45863
|
+
function startDir() {
|
|
45864
|
+
const home = os.homedir();
|
|
45865
|
+
return fs.statSync(home, { throwIfNoEntry: false })?.isDirectory() ? home : path.dirname(process.execPath);
|
|
45866
|
+
}
|
|
45867
|
+
function run(root, args, { max = 1024 * 1024, timeout = 1e4 } = {}) {
|
|
45868
|
+
return execFileSync("git", ["-C", root, ...gitOptions(), ...args], {
|
|
45869
|
+
cwd: startDir(),
|
|
45870
|
+
env: gitEnv(),
|
|
45871
|
+
maxBuffer: max,
|
|
45872
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
45873
|
+
timeout,
|
|
45874
|
+
killSignal: "SIGKILL"
|
|
45875
|
+
});
|
|
45876
|
+
}
|
|
45877
|
+
var text2 = (root, args, limits) => run(root, args, limits).toString("utf8");
|
|
45878
|
+
var list = (out) => out.split("\x00").filter(Boolean);
|
|
45879
|
+
function revision(rev) {
|
|
45880
|
+
if (!rev || rev.startsWith("-") || /[\0\n]/.test(rev))
|
|
45881
|
+
throw new Error(`not a revision: ${JSON.stringify(rev)}`);
|
|
45882
|
+
return rev;
|
|
45883
|
+
}
|
|
45884
|
+
function objectId(oid) {
|
|
45885
|
+
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(oid))
|
|
45886
|
+
throw new Error(`not an object id: ${JSON.stringify(oid)}`);
|
|
45887
|
+
return oid;
|
|
45888
|
+
}
|
|
45889
|
+
var topLevel = (dir, limits) => text2(dir, ["rev-parse", "--show-toplevel"], limits).trim();
|
|
45890
|
+
var commitOf = (root, rev, limits) => text2(root, ["rev-parse", "--verify", "-q", `${revision(rev)}^{commit}`], limits).trim();
|
|
45891
|
+
function isAncestor(root, a, b, limits) {
|
|
45892
|
+
try {
|
|
45893
|
+
run(root, ["merge-base", "--is-ancestor", objectId(a), objectId(b)], limits);
|
|
45894
|
+
return true;
|
|
45895
|
+
} catch {
|
|
45896
|
+
return false;
|
|
45897
|
+
}
|
|
45898
|
+
}
|
|
45899
|
+
var commitDiffNames = (root, a, b, limits) => list(text2(root, ["diff", ...DIFF_OPTIONS, "--name-only", "-z", objectId(a), objectId(b), "--"], limits));
|
|
45900
|
+
var treeEntry = (root, commit, rel) => text2(root, ["ls-tree", "-z", revision(commit), "--", rel]).split("\x00")[0] ?? "";
|
|
45901
|
+
var blobSize = (root, blob) => Number(text2(root, ["cat-file", "-s", objectId(blob)]).trim());
|
|
45902
|
+
var blobBytes = (root, blob, max) => run(root, ["cat-file", "blob", objectId(blob)], { max });
|
|
45903
|
+
function listFiles(root, kind, { pathspecs = [], ...limits } = {}) {
|
|
45904
|
+
const which = {
|
|
45905
|
+
tracked: ["--cached"],
|
|
45906
|
+
untracked: ["--others", "--exclude-standard"],
|
|
45907
|
+
"tracked-and-untracked": ["--cached", "--others", "--exclude-standard"],
|
|
45908
|
+
deleted: ["--deleted"]
|
|
45909
|
+
}[kind];
|
|
45910
|
+
return list(text2(root, ["ls-files", "-z", ...which, "--", ...pathspecs], limits));
|
|
45911
|
+
}
|
|
45912
|
+
var originUrl = (root, limits) => text2(root, ["remote", "get-url", "origin"], limits).trim();
|
|
45913
|
+
function commitHolds(root, commit, rel) {
|
|
45914
|
+
try {
|
|
45915
|
+
const m = /^(\d{6}) (\w+) [0-9a-f]+\t(.*)$/.exec(treeEntry(root, commit, rel));
|
|
45916
|
+
return !!m && (m[1] === "100644" || m[1] === "100755") && m[2] === "blob" && m[3] === rel;
|
|
45917
|
+
} catch {
|
|
45918
|
+
return false;
|
|
45919
|
+
}
|
|
45920
|
+
}
|
|
45921
|
+
function repoFiles(root) {
|
|
45922
|
+
try {
|
|
45923
|
+
const max = 32 * 1024 * 1024;
|
|
45924
|
+
const deleted = new Set(listFiles(root, "deleted", { max }));
|
|
45925
|
+
return [...new Set(listFiles(root, "tracked-and-untracked", { max }))].filter((p) => !deleted.has(p));
|
|
45926
|
+
} catch {
|
|
45927
|
+
return null;
|
|
45928
|
+
}
|
|
45929
|
+
}
|
|
45930
|
+
async function worktreeStatus(root, deadline) {
|
|
45931
|
+
const out = await inIsolation(root, [{ kind: "status" }], { deadline, max: 16 * 1024 * 1024 });
|
|
45932
|
+
return out?.[0] ?? null;
|
|
45933
|
+
}
|
|
45934
|
+
function workerFile() {
|
|
45935
|
+
const js = path.join(import.meta.dirname, "git-worker.js");
|
|
45936
|
+
return fs.existsSync(js) ? js : path.join(import.meta.dirname, "git-worker.ts");
|
|
45937
|
+
}
|
|
45938
|
+
function inIsolation(root, ops, { deadline, max }) {
|
|
45939
|
+
return new Promise((resolve) => {
|
|
45940
|
+
let child;
|
|
45941
|
+
try {
|
|
45942
|
+
child = spawn(process.execPath, [workerFile()], {
|
|
45943
|
+
cwd: startDir(),
|
|
45944
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
45945
|
+
windowsHide: true,
|
|
45946
|
+
detached: process.platform !== "win32"
|
|
45947
|
+
});
|
|
45948
|
+
} catch {
|
|
45949
|
+
resolve(null);
|
|
45950
|
+
return;
|
|
45951
|
+
}
|
|
45952
|
+
const chunks = [];
|
|
45953
|
+
let size = 0;
|
|
45954
|
+
let done = false;
|
|
45955
|
+
const finish = (out) => {
|
|
45956
|
+
if (done)
|
|
45957
|
+
return;
|
|
45958
|
+
done = true;
|
|
45959
|
+
clearTimeout(timer);
|
|
45960
|
+
if (out === null) {
|
|
45961
|
+
try {
|
|
45962
|
+
if (process.platform !== "win32" && child.pid)
|
|
45963
|
+
process.kill(-child.pid, "SIGKILL");
|
|
45964
|
+
else
|
|
45965
|
+
child.kill("SIGKILL");
|
|
45966
|
+
} catch {
|
|
45967
|
+
child.kill("SIGKILL");
|
|
45968
|
+
}
|
|
45969
|
+
child.stdin?.destroy();
|
|
45970
|
+
child.stdout?.destroy();
|
|
45971
|
+
child.unref();
|
|
45972
|
+
}
|
|
45973
|
+
resolve(out);
|
|
45974
|
+
};
|
|
45975
|
+
const timer = setTimeout(() => finish(null), deadline);
|
|
45976
|
+
const cap = 6 * max * Math.max(1, ops.length * 2) + 64 * 1024;
|
|
45977
|
+
child.stdout?.on("data", (c) => {
|
|
45978
|
+
size += c.length;
|
|
45979
|
+
if (size > cap)
|
|
45980
|
+
finish(null);
|
|
45981
|
+
else
|
|
45982
|
+
chunks.push(c);
|
|
45983
|
+
});
|
|
45984
|
+
child.on("error", () => finish(null));
|
|
45985
|
+
child.on("close", () => {
|
|
45986
|
+
try {
|
|
45987
|
+
const result = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
45988
|
+
finish(result.ok ? result.out : null);
|
|
45989
|
+
} catch {
|
|
45990
|
+
finish(null);
|
|
45991
|
+
}
|
|
45992
|
+
});
|
|
45993
|
+
child.stdin?.on("error", () => finish(null));
|
|
45994
|
+
child.stdin?.end(JSON.stringify({
|
|
45995
|
+
root,
|
|
45996
|
+
ops,
|
|
45997
|
+
max,
|
|
45998
|
+
until: Date.now() + Math.floor(deadline * 0.8)
|
|
45999
|
+
}));
|
|
46000
|
+
});
|
|
46001
|
+
}
|
|
46002
|
+
|
|
45829
46003
|
// server/src/panel.ts
|
|
45830
46004
|
import { stripVTControlCharacters, styleText } from "node:util";
|
|
45831
46005
|
|
|
@@ -45856,8 +46030,8 @@ function singular(w) {
|
|
|
45856
46030
|
return w.slice(0, -1);
|
|
45857
46031
|
return w.endsWith("s") && !/(?:ss|us|is)$/.test(w) ? w.slice(0, -1) : w;
|
|
45858
46032
|
}
|
|
45859
|
-
function terms(
|
|
45860
|
-
const nfkc =
|
|
46033
|
+
function terms(text3, parts = true) {
|
|
46034
|
+
const nfkc = text3.normalize("NFKC");
|
|
45861
46035
|
const norm = nfkc.toLowerCase();
|
|
45862
46036
|
const out = [];
|
|
45863
46037
|
const keep = (w) => {
|
|
@@ -45998,9 +46172,9 @@ var MAX_QUOTED = 4096;
|
|
|
45998
46172
|
var BARE_HEAD = /[^\s"',;)]{1,256}/y;
|
|
45999
46173
|
var BARE_REST = /[^\s"',;)]*/y;
|
|
46000
46174
|
var NEXT_PARAM = /&[A-Za-z_][\w.-]*=/;
|
|
46001
|
-
var bareAt = (re,
|
|
46175
|
+
var bareAt = (re, text3, at) => {
|
|
46002
46176
|
re.lastIndex = at;
|
|
46003
|
-
const v = re.exec(
|
|
46177
|
+
const v = re.exec(text3)?.[0] ?? "";
|
|
46004
46178
|
const cut = v.search(NEXT_PARAM);
|
|
46005
46179
|
return cut < 0 ? v : v.slice(0, cut);
|
|
46006
46180
|
};
|
|
@@ -46013,49 +46187,49 @@ function secretValue(quoted, v) {
|
|
|
46013
46187
|
return false;
|
|
46014
46188
|
return !/\s/.test(v) || v.split(/\s+/).some((w) => /\d/.test(w) && /[A-Za-z]/.test(w));
|
|
46015
46189
|
}
|
|
46016
|
-
function maskFields(
|
|
46190
|
+
function maskFields(text3) {
|
|
46017
46191
|
let out = "";
|
|
46018
46192
|
let last = 0;
|
|
46019
46193
|
FIELD_NAME.lastIndex = 0;
|
|
46020
|
-
for (let m = FIELD_NAME.exec(
|
|
46194
|
+
for (let m = FIELD_NAME.exec(text3);m; m = FIELD_NAME.exec(text3)) {
|
|
46021
46195
|
const at = m.index + m[0].length;
|
|
46022
|
-
const q =
|
|
46196
|
+
const q = text3[at];
|
|
46023
46197
|
let quote = "";
|
|
46024
46198
|
let value;
|
|
46025
46199
|
if (q === '"' || q === "'") {
|
|
46026
|
-
const close =
|
|
46200
|
+
const close = text3.indexOf(q, at + 1);
|
|
46027
46201
|
if (close < 0 || close - at - 1 > MAX_QUOTED)
|
|
46028
46202
|
continue;
|
|
46029
|
-
value =
|
|
46203
|
+
value = text3.slice(at + 1, close);
|
|
46030
46204
|
if (value.includes(`
|
|
46031
46205
|
`))
|
|
46032
46206
|
continue;
|
|
46033
46207
|
quote = q;
|
|
46034
46208
|
} else {
|
|
46035
|
-
value = bareAt(BARE_HEAD,
|
|
46209
|
+
value = bareAt(BARE_HEAD, text3, at);
|
|
46036
46210
|
}
|
|
46037
46211
|
if (!secretValue(quote !== "", value))
|
|
46038
46212
|
continue;
|
|
46039
46213
|
if (!quote && value.length === 256)
|
|
46040
|
-
value += bareAt(BARE_REST,
|
|
46041
|
-
out += `${
|
|
46214
|
+
value += bareAt(BARE_REST, text3, at + 256);
|
|
46215
|
+
out += `${text3.slice(last, at)}${quote}[redacted]`;
|
|
46042
46216
|
last = at + quote.length + value.length;
|
|
46043
46217
|
FIELD_NAME.lastIndex = last;
|
|
46044
46218
|
}
|
|
46045
|
-
return out +
|
|
46219
|
+
return out + text3.slice(last);
|
|
46046
46220
|
}
|
|
46047
46221
|
var MYSQL_COMMAND = /\bmysql(?:dump|admin)?\b(?:'[^'\n]*'|"[^"\n]*"|[^\n;&|\\'"]|\\\r?\n|\\(?!\r?\n))*/g;
|
|
46048
46222
|
var MYSQL_PASSWORD = /(\s-p)(?:'[^'\n]*'|"[^"\n]*"|(?=[^\s-])\S+)/;
|
|
46049
46223
|
var URL_CREDENTIALS = /\b((?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|rediss?|amqps?|https?):\/\/[^:\s/@]*:)[^\s/]*@([^@\s/?#]+)/g;
|
|
46050
46224
|
var KEY_BEGIN = /-----BEGIN [A-Z ]*PRIVATE KEY-----/g;
|
|
46051
46225
|
var KEY_END = /-----END [A-Z ]*PRIVATE KEY-----/g;
|
|
46052
|
-
function keyBlocks(
|
|
46053
|
-
const ends = [...
|
|
46226
|
+
function keyBlocks(text3) {
|
|
46227
|
+
const ends = [...text3.matchAll(KEY_END)].map((m) => [m.index, m.index + m[0].length]);
|
|
46054
46228
|
const out = [];
|
|
46055
46229
|
let last = 0;
|
|
46056
46230
|
let e = 0;
|
|
46057
46231
|
if (ends.length > 0)
|
|
46058
|
-
for (const m of
|
|
46232
|
+
for (const m of text3.matchAll(KEY_BEGIN)) {
|
|
46059
46233
|
const after = m.index + m[0].length;
|
|
46060
46234
|
if (m.index < last)
|
|
46061
46235
|
continue;
|
|
@@ -46069,25 +46243,25 @@ function keyBlocks(text2) {
|
|
|
46069
46243
|
}
|
|
46070
46244
|
return out;
|
|
46071
46245
|
}
|
|
46072
|
-
function maskPrivateKeys(
|
|
46246
|
+
function maskPrivateKeys(text3) {
|
|
46073
46247
|
let out = "";
|
|
46074
46248
|
let last = 0;
|
|
46075
|
-
for (const [a, b] of keyBlocks(
|
|
46076
|
-
out += `${
|
|
46249
|
+
for (const [a, b] of keyBlocks(text3)) {
|
|
46250
|
+
out += `${text3.slice(last, a)}[redacted: private key]`;
|
|
46077
46251
|
last = b;
|
|
46078
46252
|
}
|
|
46079
|
-
return out +
|
|
46253
|
+
return out + text3.slice(last);
|
|
46080
46254
|
}
|
|
46081
|
-
function privateKeyRanges(
|
|
46082
|
-
return byteRanges(
|
|
46255
|
+
function privateKeyRanges(text3) {
|
|
46256
|
+
return byteRanges(text3, keyBlocks(text3));
|
|
46083
46257
|
}
|
|
46084
|
-
function byteRanges(
|
|
46258
|
+
function byteRanges(text3, ranges) {
|
|
46085
46259
|
const out = [];
|
|
46086
46260
|
let at = 0;
|
|
46087
46261
|
let pos = 0;
|
|
46088
46262
|
for (const [a, b] of ranges) {
|
|
46089
|
-
pos += bytes(
|
|
46090
|
-
const end = pos + bytes(
|
|
46263
|
+
pos += bytes(text3.slice(at, a));
|
|
46264
|
+
const end = pos + bytes(text3.slice(a, b));
|
|
46091
46265
|
out.push([pos, end]);
|
|
46092
46266
|
at = b;
|
|
46093
46267
|
pos = end;
|
|
@@ -46123,8 +46297,8 @@ function quoteSpan(raw, masked, quote) {
|
|
|
46123
46297
|
const first = kept[0];
|
|
46124
46298
|
return first !== undefined && kept.length === starts(Buffer.from(raw, "utf8"), q).length ? [first, first + q.length] : null;
|
|
46125
46299
|
}
|
|
46126
|
-
function mask(
|
|
46127
|
-
let out = maskFields(maskPrivateKeys(
|
|
46300
|
+
function mask(text3) {
|
|
46301
|
+
let out = maskFields(maskPrivateKeys(text3).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]"));
|
|
46128
46302
|
for (const [re, what] of SECRETS)
|
|
46129
46303
|
out = out.replace(re, `[redacted: ${what}]`);
|
|
46130
46304
|
return out;
|
|
@@ -46146,11 +46320,11 @@ function explain(e, depth) {
|
|
|
46146
46320
|
}
|
|
46147
46321
|
|
|
46148
46322
|
// server/src/panel.ts
|
|
46149
|
-
var title = (
|
|
46150
|
-
var rule = (
|
|
46323
|
+
var title = (text3) => `✦ ${text3}`;
|
|
46324
|
+
var rule = (text3) => text3.split(`
|
|
46151
46325
|
`).map((line) => line ? `│ ${line}` : "│").join(`
|
|
46152
46326
|
`);
|
|
46153
|
-
var foot = (
|
|
46327
|
+
var foot = (text3) => `╰─ ${text3}`;
|
|
46154
46328
|
var panel = (head2, lines, end) => [title(head2), ...lines.map(rule), foot(end)].join(`
|
|
46155
46329
|
`);
|
|
46156
46330
|
var STRING_SEQUENCE = /\u001b[\]P_^X][^\u0007\u001b]*(?:\u0007|\u001b\\)?/g;
|
|
@@ -46159,11 +46333,11 @@ var plain2 = (s) => visible(stripVTControlCharacters(s.replace(STRING_SEQUENCE,
|
|
|
46159
46333
|
var inline = (s) => plain2(s).replace(/[\n\t]+/g, " ");
|
|
46160
46334
|
|
|
46161
46335
|
// server/src/plugin.ts
|
|
46162
|
-
var MANIFEST =
|
|
46336
|
+
var MANIFEST = path2.join(".claude-plugin", "plugin.json");
|
|
46163
46337
|
var PACKAGE = "package.json";
|
|
46164
46338
|
function versionAt(root) {
|
|
46165
46339
|
try {
|
|
46166
|
-
const m = JSON.parse(
|
|
46340
|
+
const m = JSON.parse(fs2.readFileSync(path2.join(root, MANIFEST), "utf8"));
|
|
46167
46341
|
return m.name === "sphica" && typeof m.version === "string" ? m.version : null;
|
|
46168
46342
|
} catch {
|
|
46169
46343
|
return null;
|
|
@@ -46171,28 +46345,28 @@ function versionAt(root) {
|
|
|
46171
46345
|
}
|
|
46172
46346
|
function packageVersionAt(root) {
|
|
46173
46347
|
try {
|
|
46174
|
-
const m = JSON.parse(
|
|
46348
|
+
const m = JSON.parse(fs2.readFileSync(path2.join(root, PACKAGE), "utf8"));
|
|
46175
46349
|
return m.name === "sphica" && typeof m.version === "string" ? m.version : null;
|
|
46176
46350
|
} catch {
|
|
46177
46351
|
return null;
|
|
46178
46352
|
}
|
|
46179
46353
|
}
|
|
46180
|
-
var here =
|
|
46181
|
-
var ROOT = [
|
|
46354
|
+
var here = path2.dirname(fileURLToPath(import.meta.url));
|
|
46355
|
+
var ROOT = [path2.join(here, ".."), path2.join(here, "..", "..", "plugin")].find((r) => versionAt(r) !== null) ?? path2.join(here, "..");
|
|
46182
46356
|
var HOST_MARKS = new Set([".orphaned_at", ".in_use"]);
|
|
46183
46357
|
|
|
46184
46358
|
// server/src/sqlite.ts
|
|
46185
46359
|
var SCHEMA_GENERATION = 2;
|
|
46186
46360
|
var SCHEMA_REVISION = 12;
|
|
46187
|
-
var sphicaHome = () => process.env.SPHICA_HOME ||
|
|
46188
|
-
var dbFile = () => process.env.SPHICA_DB ||
|
|
46361
|
+
var sphicaHome = () => process.env.SPHICA_HOME || path3.join(os2.homedir(), ".sphica");
|
|
46362
|
+
var dbFile = () => process.env.SPHICA_DB || path3.join(sphicaHome(), "sphica.db");
|
|
46189
46363
|
function requireRuntime() {
|
|
46190
46364
|
const proto = DatabaseSync.prototype;
|
|
46191
46365
|
if (typeof proto.setAuthorizer !== "function" || typeof proto.enableDefensive !== "function")
|
|
46192
46366
|
throw new Error(`Sphica needs Node 24.15 or later (this is ${process.version}). Upgrade Node.`);
|
|
46193
46367
|
}
|
|
46194
46368
|
function requireFile(file2) {
|
|
46195
|
-
if (!
|
|
46369
|
+
if (!fs3.existsSync(file2))
|
|
46196
46370
|
throw new Error(`No database at ${file2}. Create it with \`sphica init\`.`);
|
|
46197
46371
|
}
|
|
46198
46372
|
function prepare(raw, check2, busyMs = 5000) {
|
|
@@ -46530,7 +46704,7 @@ function connectWriter(role, file2 = dbFile(), create = false, busyMs) {
|
|
|
46530
46704
|
const raw = new DatabaseSync2(file2);
|
|
46531
46705
|
try {
|
|
46532
46706
|
prepare(raw, create ? "none" : role === "ingest" || role === "forget" ? "revision" : "generation", busyMs);
|
|
46533
|
-
raw.function("sphica_terms", { deterministic: true }, (
|
|
46707
|
+
raw.function("sphica_terms", { deterministic: true }, (text3) => terms(String(text3 ?? "")).join(" "));
|
|
46534
46708
|
} catch (e) {
|
|
46535
46709
|
raw.close();
|
|
46536
46710
|
throw e;
|
|
@@ -46587,9 +46761,9 @@ async function authorityOf(db, ids, asOf) {
|
|
|
46587
46761
|
}
|
|
46588
46762
|
|
|
46589
46763
|
// server/src/capture.ts
|
|
46590
|
-
import { spawn } from "node:child_process";
|
|
46591
|
-
import
|
|
46592
|
-
import
|
|
46764
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
46765
|
+
import fs7 from "node:fs";
|
|
46766
|
+
import path7 from "node:path";
|
|
46593
46767
|
|
|
46594
46768
|
// server/src/knowledge.ts
|
|
46595
46769
|
var UNIT_KINDS = [
|
|
@@ -46617,25 +46791,24 @@ var HOSTS = ["claude-code", "codex"];
|
|
|
46617
46791
|
var sessionId = (projectId, host, externalId) => uuidFrom(String(projectId), host, externalId);
|
|
46618
46792
|
|
|
46619
46793
|
// server/src/project.ts
|
|
46620
|
-
import
|
|
46621
|
-
import
|
|
46622
|
-
import path4 from "node:path";
|
|
46794
|
+
import fs5 from "node:fs";
|
|
46795
|
+
import path5 from "node:path";
|
|
46623
46796
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
46624
46797
|
|
|
46625
46798
|
// server/src/anchors.ts
|
|
46626
46799
|
import crypto2 from "node:crypto";
|
|
46627
|
-
import
|
|
46628
|
-
import
|
|
46800
|
+
import fs4 from "node:fs";
|
|
46801
|
+
import path4 from "node:path";
|
|
46629
46802
|
var MAX_BYTES = 2 * 1024 * 1024;
|
|
46630
|
-
var leaves = (rel) => rel === ".." || rel.startsWith(`..${
|
|
46803
|
+
var leaves = (rel) => rel === ".." || rel.startsWith(`..${path4.sep}`) || path4.isAbsolute(rel);
|
|
46631
46804
|
var literal3 = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
46632
46805
|
function readBytes(root, rel) {
|
|
46633
|
-
const abs =
|
|
46634
|
-
if (leaves(
|
|
46806
|
+
const abs = path4.join(root, rel);
|
|
46807
|
+
if (leaves(path4.relative(root, abs)))
|
|
46635
46808
|
return;
|
|
46636
46809
|
let st;
|
|
46637
46810
|
try {
|
|
46638
|
-
st =
|
|
46811
|
+
st = fs4.lstatSync(abs, { throwIfNoEntry: false });
|
|
46639
46812
|
} catch (e) {
|
|
46640
46813
|
return e.code === "ENOTDIR" ? null : undefined;
|
|
46641
46814
|
}
|
|
@@ -46644,10 +46817,10 @@ function readBytes(root, rel) {
|
|
|
46644
46817
|
if (!st.isFile() || st.size > MAX_BYTES)
|
|
46645
46818
|
return;
|
|
46646
46819
|
try {
|
|
46647
|
-
const inside =
|
|
46820
|
+
const inside = path4.relative(fs4.realpathSync(root), fs4.realpathSync(abs));
|
|
46648
46821
|
if (leaves(inside))
|
|
46649
46822
|
return;
|
|
46650
|
-
const buf =
|
|
46823
|
+
const buf = fs4.readFileSync(abs);
|
|
46651
46824
|
return buf.includes(0) ? undefined : buf;
|
|
46652
46825
|
} catch {
|
|
46653
46826
|
return;
|
|
@@ -46659,25 +46832,25 @@ function readRepoText(root, rel) {
|
|
|
46659
46832
|
return { text: buf, hash: buf === null ? "absent" : "unreadable" };
|
|
46660
46833
|
return { text: buf.toString("utf8"), hash: crypto2.createHash("sha256").update(buf).digest("hex") };
|
|
46661
46834
|
}
|
|
46662
|
-
function findSymbol(
|
|
46835
|
+
function findSymbol(text3, symbol2) {
|
|
46663
46836
|
const re = new RegExp(`(?<![\\w$])${literal3(symbol2)}(?![\\w$])`);
|
|
46664
|
-
const lines =
|
|
46837
|
+
const lines = text3.split(/\r?\n/);
|
|
46665
46838
|
return { lines, i: lines.findIndex((l) => re.test(l)) };
|
|
46666
46839
|
}
|
|
46667
|
-
function swallowed(
|
|
46840
|
+
function swallowed(text3, symbol2) {
|
|
46668
46841
|
if (mask(symbol2) !== symbol2)
|
|
46669
46842
|
return true;
|
|
46670
|
-
if (
|
|
46843
|
+
if (text3 === undefined)
|
|
46671
46844
|
return true;
|
|
46672
|
-
if (
|
|
46845
|
+
if (text3 === null)
|
|
46673
46846
|
return false;
|
|
46674
46847
|
const re = new RegExp(`(?<![\\w$])${literal3(symbol2)}(?![\\w$])`, "g");
|
|
46675
46848
|
let raw = 0;
|
|
46676
|
-
for (let m = re.exec(
|
|
46849
|
+
for (let m = re.exec(text3);m; m = re.exec(text3))
|
|
46677
46850
|
raw++;
|
|
46678
46851
|
if (raw === 0)
|
|
46679
46852
|
return false;
|
|
46680
|
-
const masked = mask(
|
|
46853
|
+
const masked = mask(text3);
|
|
46681
46854
|
const holes = placeholderRanges(masked);
|
|
46682
46855
|
let h = 0;
|
|
46683
46856
|
let kept = 0;
|
|
@@ -46690,14 +46863,14 @@ function swallowed(text2, symbol2) {
|
|
|
46690
46863
|
}
|
|
46691
46864
|
return kept !== raw;
|
|
46692
46865
|
}
|
|
46693
|
-
function masksSymbolIn(
|
|
46866
|
+
function masksSymbolIn(text3, symbol2) {
|
|
46694
46867
|
const trimmed = symbol2.trim();
|
|
46695
|
-
return swallowed(
|
|
46868
|
+
return swallowed(text3, symbol2) || trimmed !== symbol2 && trimmed !== "" && swallowed(text3, trimmed);
|
|
46696
46869
|
}
|
|
46697
|
-
function locateIn(
|
|
46698
|
-
if (typeof
|
|
46870
|
+
function locateIn(text3, symbol2) {
|
|
46871
|
+
if (typeof text3 !== "string")
|
|
46699
46872
|
return null;
|
|
46700
|
-
const { lines, i } = findSymbol(
|
|
46873
|
+
const { lines, i } = findSymbol(text3, symbol2);
|
|
46701
46874
|
if (i < 0)
|
|
46702
46875
|
return null;
|
|
46703
46876
|
const line = lines[i] ?? "";
|
|
@@ -46717,20 +46890,20 @@ ${l}`).join("");
|
|
|
46717
46890
|
function fileState(root, rel) {
|
|
46718
46891
|
if (!root)
|
|
46719
46892
|
return "unknown";
|
|
46720
|
-
if (leaves(
|
|
46893
|
+
if (leaves(path4.relative(root, path4.join(root, rel))))
|
|
46721
46894
|
return "unknown";
|
|
46722
46895
|
let realRoot;
|
|
46723
46896
|
try {
|
|
46724
|
-
realRoot =
|
|
46897
|
+
realRoot = fs4.realpathSync(root);
|
|
46725
46898
|
} catch {
|
|
46726
46899
|
return "unknown";
|
|
46727
46900
|
}
|
|
46728
46901
|
let at = root;
|
|
46729
46902
|
for (const part of rel.split("/")) {
|
|
46730
|
-
at =
|
|
46903
|
+
at = path4.join(at, part);
|
|
46731
46904
|
let st;
|
|
46732
46905
|
try {
|
|
46733
|
-
st =
|
|
46906
|
+
st = fs4.lstatSync(at, { throwIfNoEntry: false });
|
|
46734
46907
|
} catch (e) {
|
|
46735
46908
|
if (e.code === "ENOTDIR")
|
|
46736
46909
|
return "gone";
|
|
@@ -46740,7 +46913,7 @@ function fileState(root, rel) {
|
|
|
46740
46913
|
return "gone";
|
|
46741
46914
|
if (st.isSymbolicLink()) {
|
|
46742
46915
|
try {
|
|
46743
|
-
if (leaves(
|
|
46916
|
+
if (leaves(path4.relative(realRoot, fs4.realpathSync(at))))
|
|
46744
46917
|
return "unknown";
|
|
46745
46918
|
} catch {
|
|
46746
46919
|
return "unknown";
|
|
@@ -46754,7 +46927,7 @@ function pathKind(root, rel) {
|
|
|
46754
46927
|
if (state !== "present" || !root)
|
|
46755
46928
|
return state === "gone" ? "gone" : "unknown";
|
|
46756
46929
|
try {
|
|
46757
|
-
const st =
|
|
46930
|
+
const st = fs4.statSync(path4.join(root, rel));
|
|
46758
46931
|
return st.isFile() ? "file" : st.isDirectory() ? "directory" : "unknown";
|
|
46759
46932
|
} catch {
|
|
46760
46933
|
return "unknown";
|
|
@@ -46762,7 +46935,7 @@ function pathKind(root, rel) {
|
|
|
46762
46935
|
}
|
|
46763
46936
|
|
|
46764
46937
|
// server/src/project.ts
|
|
46765
|
-
var localFile = () =>
|
|
46938
|
+
var localFile = () => path5.join(sphicaHome(), "projects.json");
|
|
46766
46939
|
var LOCAL_KEY = /^[a-z0-9][a-z0-9._-]*$/;
|
|
46767
46940
|
function legacyRemote(url2) {
|
|
46768
46941
|
const raw = String(url2 ?? "").trim();
|
|
@@ -46790,21 +46963,18 @@ function normalizeKey(key) {
|
|
|
46790
46963
|
const host = fold(cut < 0 ? rest : rest.slice(0, cut));
|
|
46791
46964
|
return host === "github.com" ? fold(key) : `git:${host}${cut < 0 ? "" : rest.slice(cut)}`;
|
|
46792
46965
|
}
|
|
46793
|
-
var
|
|
46966
|
+
var ask = (fn) => {
|
|
46794
46967
|
try {
|
|
46795
|
-
return
|
|
46796
|
-
encoding: "utf8",
|
|
46797
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
46798
|
-
timeout: 5000
|
|
46799
|
-
}).trim();
|
|
46968
|
+
return fn() || null;
|
|
46800
46969
|
} catch {
|
|
46801
46970
|
return null;
|
|
46802
46971
|
}
|
|
46803
46972
|
};
|
|
46973
|
+
var top = (dir) => ask(() => topLevel(dir, { timeout: 5000 }));
|
|
46804
46974
|
function localMap() {
|
|
46805
46975
|
let raw;
|
|
46806
46976
|
try {
|
|
46807
|
-
raw =
|
|
46977
|
+
raw = fs5.readFileSync(localFile(), "utf8");
|
|
46808
46978
|
} catch (e) {
|
|
46809
46979
|
if (e.code === "ENOENT")
|
|
46810
46980
|
return {};
|
|
@@ -46816,26 +46986,26 @@ function localMap() {
|
|
|
46816
46986
|
} catch {
|
|
46817
46987
|
m = null;
|
|
46818
46988
|
}
|
|
46819
|
-
if (!m || typeof m !== "object" || Array.isArray(m) || Object.entries(m).some(([root, name]) => !
|
|
46989
|
+
if (!m || typeof m !== "object" || Array.isArray(m) || Object.entries(m).some(([root, name]) => !path5.isAbsolute(root) || typeof name !== "string" || !LOCAL_KEY.test(name)))
|
|
46820
46990
|
throw new Error(`${localFile()} is not a valid JSON project table. Fix or delete it, then name the project again.`);
|
|
46821
46991
|
return m;
|
|
46822
46992
|
}
|
|
46823
46993
|
function identify(dir) {
|
|
46824
|
-
const given =
|
|
46825
|
-
const
|
|
46826
|
-
const root =
|
|
46827
|
-
const legacy =
|
|
46994
|
+
const given = path5.resolve(dir);
|
|
46995
|
+
const found = top(given);
|
|
46996
|
+
const root = found || given;
|
|
46997
|
+
const legacy = found ? legacyRemote(ask(() => originUrl(root, { timeout: 5000 }))) : null;
|
|
46828
46998
|
if (legacy) {
|
|
46829
46999
|
const key = normalizeKey(`git:${legacy}`);
|
|
46830
47000
|
const remote = key.slice(4);
|
|
46831
47001
|
return { key, legacyKey: `git:${legacy}`, root, name: remote.split("/").slice(1).join("/") || remote };
|
|
46832
47002
|
}
|
|
46833
47003
|
const map3 = localMap();
|
|
46834
|
-
for (let d = root;; d =
|
|
47004
|
+
for (let d = root;; d = path5.dirname(d)) {
|
|
46835
47005
|
const local = map3[d];
|
|
46836
47006
|
if (local && LOCAL_KEY.test(local))
|
|
46837
47007
|
return { key: `local:${local}`, legacyKey: `local:${local}`, root: d, name: local };
|
|
46838
|
-
if (
|
|
47008
|
+
if (found || path5.dirname(d) === d)
|
|
46839
47009
|
return null;
|
|
46840
47010
|
}
|
|
46841
47011
|
}
|
|
@@ -46864,11 +47034,11 @@ async function projectId(db, key) {
|
|
|
46864
47034
|
return r?.id ?? null;
|
|
46865
47035
|
}
|
|
46866
47036
|
function relativeTo(root, file2, cwd = root) {
|
|
46867
|
-
const abs =
|
|
46868
|
-
const rel =
|
|
47037
|
+
const abs = path5.resolve(cwd, file2);
|
|
47038
|
+
const rel = path5.relative(root, abs);
|
|
46869
47039
|
if (!rel || leaves(rel) || /\p{Cc}/u.test(rel))
|
|
46870
47040
|
return null;
|
|
46871
|
-
return rel.split(
|
|
47041
|
+
return rel.split(path5.sep).join("/");
|
|
46872
47042
|
}
|
|
46873
47043
|
function patchPaths(patch) {
|
|
46874
47044
|
const out = [];
|
|
@@ -46882,25 +47052,15 @@ function patchPaths(patch) {
|
|
|
46882
47052
|
}
|
|
46883
47053
|
|
|
46884
47054
|
// server/src/worktree.ts
|
|
46885
|
-
import
|
|
46886
|
-
import
|
|
46887
|
-
import path5 from "node:path";
|
|
47055
|
+
import fs6 from "node:fs";
|
|
47056
|
+
import path6 from "node:path";
|
|
46888
47057
|
var MAX_PATHS = 200;
|
|
46889
|
-
var
|
|
46890
|
-
|
|
46891
|
-
return execFileSync2("git", ["-C", root, ...args], {
|
|
46892
|
-
encoding: "utf8",
|
|
46893
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
46894
|
-
timeout: 5000,
|
|
46895
|
-
maxBuffer: 16 * 1024 * 1024
|
|
46896
|
-
});
|
|
46897
|
-
} catch {
|
|
46898
|
-
return null;
|
|
46899
|
-
}
|
|
46900
|
-
};
|
|
47058
|
+
var LIMITS = { timeout: 5000, max: 16 * 1024 * 1024 };
|
|
47059
|
+
var STATUS_DEADLINE = 5000;
|
|
46901
47060
|
var usable = (p) => p !== "" && !p.includes("\\") && !/\p{Cc}/u.test(p);
|
|
46902
|
-
function snapshot(root) {
|
|
46903
|
-
const
|
|
47061
|
+
async function snapshot(root) {
|
|
47062
|
+
const until = Date.now() + STATUS_DEADLINE;
|
|
47063
|
+
const out = await worktreeStatus(root, STATUS_DEADLINE);
|
|
46904
47064
|
if (out === null)
|
|
46905
47065
|
return null;
|
|
46906
47066
|
const fields = out.split("\x00");
|
|
@@ -46918,13 +47078,18 @@ function snapshot(root) {
|
|
|
46918
47078
|
continue;
|
|
46919
47079
|
let st;
|
|
46920
47080
|
try {
|
|
46921
|
-
st =
|
|
47081
|
+
st = fs6.lstatSync(path6.join(root, p), { throwIfNoEntry: false });
|
|
46922
47082
|
} catch {
|
|
46923
47083
|
st = undefined;
|
|
46924
47084
|
}
|
|
46925
47085
|
entries[p] = `${parts.slice(0, skip).join(" ")} ${st ? `${st.size}:${st.mtimeMs}` : "gone"}`;
|
|
46926
47086
|
}
|
|
46927
|
-
|
|
47087
|
+
let head2 = null;
|
|
47088
|
+
try {
|
|
47089
|
+
const left = until - Date.now();
|
|
47090
|
+
if (left > 0)
|
|
47091
|
+
head2 = commitOf(root, "HEAD", { ...LIMITS, timeout: left }) || null;
|
|
47092
|
+
} catch {}
|
|
46928
47093
|
return { head: head2, entries };
|
|
46929
47094
|
}
|
|
46930
47095
|
function changed(root, before, after) {
|
|
@@ -46932,8 +47097,12 @@ function changed(root, before, after) {
|
|
|
46932
47097
|
for (const p of new Set([...Object.keys(before.entries), ...Object.keys(after.entries)]))
|
|
46933
47098
|
if (before.entries[p] !== after.entries[p])
|
|
46934
47099
|
paths.add(p);
|
|
46935
|
-
if (before.head && after.head && before.head !== after.head &&
|
|
46936
|
-
|
|
47100
|
+
if (before.head && after.head && before.head !== after.head && isAncestor(root, before.head, after.head, LIMITS)) {
|
|
47101
|
+
let between = [];
|
|
47102
|
+
try {
|
|
47103
|
+
between = commitDiffNames(root, before.head, after.head, LIMITS);
|
|
47104
|
+
} catch {}
|
|
47105
|
+
for (const p of between)
|
|
46937
47106
|
if (usable(p))
|
|
46938
47107
|
paths.add(p);
|
|
46939
47108
|
}
|
|
@@ -46941,15 +47110,15 @@ function changed(root, before, after) {
|
|
|
46941
47110
|
}
|
|
46942
47111
|
|
|
46943
47112
|
// server/src/capture.ts
|
|
46944
|
-
var spoolDir = () =>
|
|
46945
|
-
var stateFile = () =>
|
|
46946
|
-
var rejectedDir = () =>
|
|
46947
|
-
var unregisteredDir = () =>
|
|
46948
|
-
var callsDir = () =>
|
|
46949
|
-
var callsRejectedDir = () =>
|
|
47113
|
+
var spoolDir = () => path7.join(sphicaHome(), "spool");
|
|
47114
|
+
var stateFile = () => path7.join(sphicaHome(), "capture.json");
|
|
47115
|
+
var rejectedDir = () => path7.join(spoolDir(), "rejected");
|
|
47116
|
+
var unregisteredDir = () => path7.join(spoolDir(), "unregistered");
|
|
47117
|
+
var callsDir = () => path7.join(spoolDir(), "calls");
|
|
47118
|
+
var callsRejectedDir = () => path7.join(callsDir(), "rejected");
|
|
46950
47119
|
var HOLD_DAYS = 30;
|
|
46951
47120
|
var HOLD_MAX = 1000;
|
|
46952
|
-
var baselineDir = () =>
|
|
47121
|
+
var baselineDir = () => path7.join(sphicaHome(), "worktree");
|
|
46953
47122
|
function current(raw) {
|
|
46954
47123
|
const r = raw;
|
|
46955
47124
|
if (typeof r.project !== "string")
|
|
@@ -46998,7 +47167,7 @@ ${z2}`,
|
|
|
46998
47167
|
function prune(held, removed = () => {}) {
|
|
46999
47168
|
let files;
|
|
47000
47169
|
try {
|
|
47001
|
-
files =
|
|
47170
|
+
files = fs7.readdirSync(held).filter((f) => f.endsWith(".json") && !f.startsWith(".")).sort();
|
|
47002
47171
|
} catch {
|
|
47003
47172
|
return;
|
|
47004
47173
|
}
|
|
@@ -47006,26 +47175,26 @@ function prune(held, removed = () => {}) {
|
|
|
47006
47175
|
const stale = files.filter((f) => Number(f.split("-")[0]) < cutoff);
|
|
47007
47176
|
const over = files.slice(0, Math.max(0, files.length - HOLD_MAX));
|
|
47008
47177
|
for (const f of new Set([...stale, ...over])) {
|
|
47009
|
-
|
|
47178
|
+
fs7.rmSync(path7.join(held, f), { force: true });
|
|
47010
47179
|
removed();
|
|
47011
47180
|
}
|
|
47012
47181
|
}
|
|
47013
47182
|
function put(dir, record4) {
|
|
47014
|
-
|
|
47183
|
+
fs7.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
47015
47184
|
const name = `${Date.now()}-${process.pid}-${Math.random().toString(36).slice(2, 10)}.json`;
|
|
47016
|
-
const tmp =
|
|
47017
|
-
|
|
47018
|
-
|
|
47019
|
-
return
|
|
47185
|
+
const tmp = path7.join(dir, `.${name}`);
|
|
47186
|
+
fs7.writeFileSync(tmp, JSON.stringify(record4), { mode: 384 });
|
|
47187
|
+
fs7.renameSync(tmp, path7.join(dir, name));
|
|
47188
|
+
return path7.join(dir, name);
|
|
47020
47189
|
}
|
|
47021
47190
|
function spool(record4) {
|
|
47022
47191
|
put(spoolDir(), record4);
|
|
47023
47192
|
}
|
|
47024
47193
|
var branchOf = (root) => {
|
|
47025
47194
|
try {
|
|
47026
|
-
const dotgit =
|
|
47027
|
-
const gitdir =
|
|
47028
|
-
const h =
|
|
47195
|
+
const dotgit = path7.join(root, ".git");
|
|
47196
|
+
const gitdir = fs7.statSync(dotgit).isFile() ? path7.resolve(root, fs7.readFileSync(dotgit, "utf8").match(/^gitdir: (.+)$/m)?.[1]?.trim() ?? "") : dotgit;
|
|
47197
|
+
const h = fs7.readFileSync(path7.join(gitdir, "HEAD"), "utf8").trim();
|
|
47029
47198
|
return h.startsWith("ref: refs/heads/") ? h.slice("ref: refs/heads/".length) : null;
|
|
47030
47199
|
} catch {
|
|
47031
47200
|
return null;
|
|
@@ -47071,7 +47240,7 @@ Notes: ${notes.trim()}` : "";
|
|
|
47071
47240
|
};
|
|
47072
47241
|
}
|
|
47073
47242
|
function captureNotice(file2 = dbFile()) {
|
|
47074
|
-
if (!
|
|
47243
|
+
if (!fs7.existsSync(file2))
|
|
47075
47244
|
return panel("sphica: no database, so conversations are not recorded", [file2], "Create it with sphica init");
|
|
47076
47245
|
const s = readState();
|
|
47077
47246
|
if (s.unreadable)
|
|
@@ -47082,15 +47251,15 @@ function captureNotice(file2 = dbFile()) {
|
|
|
47082
47251
|
return panel(`sphica: the database rejected ${plural2(s.rejected, "record")}`, [rejectedDir(), `Move them back to ${spoolDir()} to resend`], "Fix them first, then check with sphica doctor");
|
|
47083
47252
|
return null;
|
|
47084
47253
|
}
|
|
47085
|
-
var turnDir = (host, session) =>
|
|
47086
|
-
var startFile = (dir, turn) =>
|
|
47254
|
+
var turnDir = (host, session) => path7.join(baselineDir(), digest(`${host}\x00${session}`));
|
|
47255
|
+
var startFile = (dir, turn) => path7.join(dir, `${digest(turn)}.json`);
|
|
47087
47256
|
var isStart = (v) => {
|
|
47088
47257
|
const s = v;
|
|
47089
47258
|
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");
|
|
47090
47259
|
};
|
|
47091
47260
|
function readStart(file2) {
|
|
47092
47261
|
try {
|
|
47093
|
-
const s = JSON.parse(
|
|
47262
|
+
const s = JSON.parse(fs7.readFileSync(file2, "utf8"));
|
|
47094
47263
|
return isStart(s) ? s : null;
|
|
47095
47264
|
} catch {
|
|
47096
47265
|
return null;
|
|
@@ -47099,7 +47268,7 @@ function readStart(file2) {
|
|
|
47099
47268
|
function readStarts(dir) {
|
|
47100
47269
|
let names;
|
|
47101
47270
|
try {
|
|
47102
|
-
names =
|
|
47271
|
+
names = fs7.readdirSync(dir);
|
|
47103
47272
|
} catch (e) {
|
|
47104
47273
|
return e.code === "ENOENT" ? [] : null;
|
|
47105
47274
|
}
|
|
@@ -47107,7 +47276,7 @@ function readStarts(dir) {
|
|
|
47107
47276
|
for (const name of names) {
|
|
47108
47277
|
if (name.startsWith("."))
|
|
47109
47278
|
continue;
|
|
47110
|
-
const s = readStart(
|
|
47279
|
+
const s = readStart(path7.join(dir, name));
|
|
47111
47280
|
if (!s)
|
|
47112
47281
|
return null;
|
|
47113
47282
|
starts2.push(s);
|
|
@@ -47117,30 +47286,30 @@ function readStarts(dir) {
|
|
|
47117
47286
|
function newestTurn(starts2) {
|
|
47118
47287
|
if (!starts2?.length || starts2.some((s) => s.seq === null))
|
|
47119
47288
|
return null;
|
|
47120
|
-
const
|
|
47121
|
-
const at = starts2.filter((s) => s.seq ===
|
|
47289
|
+
const top2 = Math.max(...starts2.map((s) => s.seq));
|
|
47290
|
+
const at = starts2.filter((s) => s.seq === top2);
|
|
47122
47291
|
return at.length === 1 ? at[0]?.turn ?? null : null;
|
|
47123
47292
|
}
|
|
47124
47293
|
function writeStart(dir, s, file2 = startFile(dir, s.turn)) {
|
|
47125
|
-
|
|
47126
|
-
const tmp =
|
|
47127
|
-
|
|
47128
|
-
|
|
47294
|
+
fs7.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
47295
|
+
const tmp = path7.join(dir, `.${path7.basename(file2)}.${process.pid}.tmp`);
|
|
47296
|
+
fs7.writeFileSync(tmp, JSON.stringify(s), { mode: 384 });
|
|
47297
|
+
fs7.renameSync(tmp, file2);
|
|
47129
47298
|
}
|
|
47130
|
-
function openTurn(dir, turn, root) {
|
|
47299
|
+
async function openTurn(dir, turn, root) {
|
|
47131
47300
|
if (readStart(startFile(dir, turn))?.running)
|
|
47132
47301
|
return null;
|
|
47133
47302
|
const starts2 = readStarts(dir);
|
|
47134
47303
|
const seq = starts2 && !starts2.some((s) => s.seq === null) ? Math.max(0, ...starts2.map((s) => s.seq)) + 1 : null;
|
|
47135
|
-
const now = snapshot(root);
|
|
47304
|
+
const now = await snapshot(root);
|
|
47136
47305
|
return () => writeStart(dir, { head: now?.head ?? null, entries: now?.entries ?? null, running: true, turn, seq });
|
|
47137
47306
|
}
|
|
47138
|
-
function closeTurn(dir, turn, root) {
|
|
47307
|
+
async function closeTurn(dir, turn, root) {
|
|
47139
47308
|
const own2 = readStart(startFile(dir, turn));
|
|
47140
47309
|
if (!own2?.entries)
|
|
47141
47310
|
return null;
|
|
47142
47311
|
const before = { head: own2.head, entries: own2.entries };
|
|
47143
|
-
const now = snapshot(root);
|
|
47312
|
+
const now = await snapshot(root);
|
|
47144
47313
|
return () => {
|
|
47145
47314
|
if (newestTurn(readStarts(dir)) !== turn || JSON.stringify(readStart(startFile(dir, turn))) !== JSON.stringify(own2))
|
|
47146
47315
|
return [];
|
|
@@ -47155,22 +47324,22 @@ function stopTurn(dir, turn) {
|
|
|
47155
47324
|
}
|
|
47156
47325
|
function pruneBaselines() {
|
|
47157
47326
|
const cutoff = Date.now() - HOLD_DAYS * 24 * 60 * 60 * 1000;
|
|
47158
|
-
const fresh = (file2) => (
|
|
47327
|
+
const fresh = (file2) => (fs7.statSync(file2, { throwIfNoEntry: false })?.mtimeMs ?? Date.now()) >= cutoff;
|
|
47159
47328
|
let names;
|
|
47160
47329
|
try {
|
|
47161
|
-
names =
|
|
47330
|
+
names = fs7.readdirSync(baselineDir());
|
|
47162
47331
|
} catch {
|
|
47163
47332
|
return;
|
|
47164
47333
|
}
|
|
47165
47334
|
for (const name of names) {
|
|
47166
|
-
const at =
|
|
47167
|
-
if (!
|
|
47335
|
+
const at = path7.join(baselineDir(), name);
|
|
47336
|
+
if (!fs7.statSync(at, { throwIfNoEntry: false })?.isDirectory()) {
|
|
47168
47337
|
if (!fresh(at))
|
|
47169
|
-
|
|
47338
|
+
fs7.rmSync(at, { force: true });
|
|
47170
47339
|
} else {
|
|
47171
|
-
const files =
|
|
47172
|
-
if (files.length && !files.some((f) => fresh(
|
|
47173
|
-
|
|
47340
|
+
const files = fs7.readdirSync(at);
|
|
47341
|
+
if (files.length && !files.some((f) => fresh(path7.join(at, f))))
|
|
47342
|
+
fs7.rmSync(at, { recursive: true, force: true });
|
|
47174
47343
|
}
|
|
47175
47344
|
}
|
|
47176
47345
|
}
|
|
@@ -47181,7 +47350,14 @@ function attempt(fn) {
|
|
|
47181
47350
|
return;
|
|
47182
47351
|
}
|
|
47183
47352
|
}
|
|
47184
|
-
function
|
|
47353
|
+
async function attemptAsync(fn) {
|
|
47354
|
+
try {
|
|
47355
|
+
return await fn();
|
|
47356
|
+
} catch {
|
|
47357
|
+
return;
|
|
47358
|
+
}
|
|
47359
|
+
}
|
|
47360
|
+
async function onHook(host, input2) {
|
|
47185
47361
|
const event = input2.hook_event_name;
|
|
47186
47362
|
const owner = () => isOwnerTurn(input2, undefined, undefined, host === "codex" ? process.env.CODEX_THREAD_ID : undefined);
|
|
47187
47363
|
if (event === "SessionStart") {
|
|
@@ -47189,7 +47365,7 @@ function onHook(host, input2) {
|
|
|
47189
47365
|
return { flush: false };
|
|
47190
47366
|
const file2 = process.env.CLAUDE_ENV_FILE;
|
|
47191
47367
|
if (file2 && input2.session_id && /^[A-Za-z0-9_-]+$/.test(input2.session_id)) {
|
|
47192
|
-
|
|
47368
|
+
fs7.appendFileSync(file2, `export SPHICA_PARENT_SESSION=${input2.session_id}
|
|
47193
47369
|
`);
|
|
47194
47370
|
}
|
|
47195
47371
|
attempt(pruneBaselines);
|
|
@@ -47233,12 +47409,12 @@ function onHook(host, input2) {
|
|
|
47233
47409
|
say(`${turn}:owner`, "owner", prompt);
|
|
47234
47410
|
}
|
|
47235
47411
|
if (event === "UserPromptSubmit")
|
|
47236
|
-
|
|
47412
|
+
await attemptAsync(async () => (await openTurn(dir, turn, place.root))?.());
|
|
47237
47413
|
if (event === "Stop") {
|
|
47238
47414
|
if (input2.last_assistant_message)
|
|
47239
47415
|
say(`${turn}:assistant`, "assistant", input2.last_assistant_message);
|
|
47240
|
-
const paths =
|
|
47241
|
-
const finish = closeTurn(dir, turn, place.root);
|
|
47416
|
+
const paths = await attemptAsync(async () => {
|
|
47417
|
+
const finish = await closeTurn(dir, turn, place.root);
|
|
47242
47418
|
if (finish)
|
|
47243
47419
|
return finish();
|
|
47244
47420
|
stopTurn(dir, turn);
|
|
@@ -47272,7 +47448,7 @@ function onHook(host, input2) {
|
|
|
47272
47448
|
function readStateFile() {
|
|
47273
47449
|
let raw = {};
|
|
47274
47450
|
try {
|
|
47275
|
-
const parsed = JSON.parse(
|
|
47451
|
+
const parsed = JSON.parse(fs7.readFileSync(stateFile(), "utf8"));
|
|
47276
47452
|
if (parsed && typeof parsed === "object")
|
|
47277
47453
|
raw = parsed;
|
|
47278
47454
|
} catch {}
|
|
@@ -47286,7 +47462,7 @@ function readStateFile() {
|
|
|
47286
47462
|
}
|
|
47287
47463
|
function writeState(s) {
|
|
47288
47464
|
try {
|
|
47289
|
-
|
|
47465
|
+
fs7.writeFileSync(stateFile(), JSON.stringify({ ...s, pruned: s.pruned ?? readStateFile().pruned }));
|
|
47290
47466
|
} catch {}
|
|
47291
47467
|
}
|
|
47292
47468
|
function readState() {
|
|
@@ -47302,7 +47478,7 @@ function readState() {
|
|
|
47302
47478
|
}
|
|
47303
47479
|
function listQueue(dir) {
|
|
47304
47480
|
try {
|
|
47305
|
-
const names =
|
|
47481
|
+
const names = fs7.readdirSync(dir);
|
|
47306
47482
|
return {
|
|
47307
47483
|
files: names.filter((f) => f.endsWith(".json") && !f.startsWith(".")),
|
|
47308
47484
|
temp: names.filter((f) => f.endsWith(".json") && f.startsWith(".")),
|
|
@@ -47314,25 +47490,25 @@ function listQueue(dir) {
|
|
|
47314
47490
|
}
|
|
47315
47491
|
}
|
|
47316
47492
|
function lock() {
|
|
47317
|
-
const file2 =
|
|
47318
|
-
|
|
47493
|
+
const file2 = path7.join(spoolDir(), ".lock");
|
|
47494
|
+
fs7.mkdirSync(spoolDir(), { recursive: true, mode: 448 });
|
|
47319
47495
|
for (let attempt2 = 0;attempt2 < 2; attempt2++) {
|
|
47320
47496
|
try {
|
|
47321
|
-
|
|
47497
|
+
fs7.writeFileSync(file2, String(process.pid), { flag: "wx", mode: 384 });
|
|
47322
47498
|
return () => {
|
|
47323
47499
|
try {
|
|
47324
|
-
if (
|
|
47325
|
-
|
|
47500
|
+
if (fs7.readFileSync(file2, "utf8") === String(process.pid))
|
|
47501
|
+
fs7.rmSync(file2, { force: true });
|
|
47326
47502
|
} catch {}
|
|
47327
47503
|
};
|
|
47328
47504
|
} catch (e) {
|
|
47329
47505
|
if (e.code !== "EEXIST")
|
|
47330
47506
|
throw e;
|
|
47331
47507
|
}
|
|
47332
|
-
const st =
|
|
47508
|
+
const st = fs7.statSync(file2, { throwIfNoEntry: false });
|
|
47333
47509
|
if (!st)
|
|
47334
47510
|
continue;
|
|
47335
|
-
const holder = Number(
|
|
47511
|
+
const holder = Number(fs7.readFileSync(file2, "utf8") || 0);
|
|
47336
47512
|
const fresh = Date.now() - st.mtimeMs < 5 * 60000;
|
|
47337
47513
|
const alive = (() => {
|
|
47338
47514
|
if (holder <= 0)
|
|
@@ -47345,7 +47521,7 @@ function lock() {
|
|
|
47345
47521
|
})();
|
|
47346
47522
|
if (alive && fresh)
|
|
47347
47523
|
return null;
|
|
47348
|
-
|
|
47524
|
+
fs7.rmSync(file2, { force: true });
|
|
47349
47525
|
}
|
|
47350
47526
|
return null;
|
|
47351
47527
|
}
|
|
@@ -47445,23 +47621,23 @@ var REJECTED = new Set([18, 19, 20, 25]);
|
|
|
47445
47621
|
var rejected = (e) => REJECTED.has(sqliteCode(e) ?? -1);
|
|
47446
47622
|
var queued = (from) => {
|
|
47447
47623
|
try {
|
|
47448
|
-
return
|
|
47624
|
+
return fs7.readdirSync(from).filter((f) => f.endsWith(".json") && !f.startsWith(".")).sort().map((name) => ({ name, from }));
|
|
47449
47625
|
} catch {
|
|
47450
47626
|
return [];
|
|
47451
47627
|
}
|
|
47452
47628
|
};
|
|
47453
47629
|
var CODE_NAMES = { 18: "TOOBIG", 19: "CONSTRAINT", 20: "MISMATCH", 25: "RANGE" };
|
|
47454
47630
|
function setAside(from, name, dir, reason2) {
|
|
47455
|
-
|
|
47631
|
+
fs7.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
47456
47632
|
try {
|
|
47457
|
-
|
|
47633
|
+
fs7.renameSync(path7.join(from, name), path7.join(dir, name));
|
|
47458
47634
|
} catch (e) {
|
|
47459
47635
|
if (e.code === "ENOENT")
|
|
47460
47636
|
return;
|
|
47461
47637
|
throw e;
|
|
47462
47638
|
}
|
|
47463
47639
|
try {
|
|
47464
|
-
|
|
47640
|
+
fs7.writeFileSync(path7.join(dir, `${name}.reason`), reason2, { mode: 384 });
|
|
47465
47641
|
} catch {}
|
|
47466
47642
|
}
|
|
47467
47643
|
async function sendBatch(db, names) {
|
|
@@ -47471,7 +47647,7 @@ async function sendBatch(db, names) {
|
|
|
47471
47647
|
let raw;
|
|
47472
47648
|
let r;
|
|
47473
47649
|
try {
|
|
47474
|
-
raw = JSON.parse(
|
|
47650
|
+
raw = JSON.parse(fs7.readFileSync(path7.join(from, name), "utf8"));
|
|
47475
47651
|
} catch {
|
|
47476
47652
|
setAside(from, name, rejectedDir(), "unreadable");
|
|
47477
47653
|
continue;
|
|
@@ -47485,7 +47661,7 @@ async function sendBatch(db, names) {
|
|
|
47485
47661
|
if (r)
|
|
47486
47662
|
records.push({ name, from, r });
|
|
47487
47663
|
else
|
|
47488
|
-
|
|
47664
|
+
fs7.rmSync(path7.join(from, name), { force: true });
|
|
47489
47665
|
}
|
|
47490
47666
|
let sent = 0;
|
|
47491
47667
|
let strayed = [];
|
|
@@ -47513,12 +47689,12 @@ async function sendBatch(db, names) {
|
|
|
47513
47689
|
for (const x of bad)
|
|
47514
47690
|
setAside(x.from, x.name, rejectedDir(), x.why);
|
|
47515
47691
|
if (strayed.length) {
|
|
47516
|
-
|
|
47692
|
+
fs7.mkdirSync(held, { recursive: true, mode: 448 });
|
|
47517
47693
|
for (const x of strayed) {
|
|
47518
47694
|
if (x.from === held)
|
|
47519
47695
|
continue;
|
|
47520
47696
|
try {
|
|
47521
|
-
|
|
47697
|
+
fs7.renameSync(path7.join(x.from, x.name), path7.join(held, x.name));
|
|
47522
47698
|
} catch (e) {
|
|
47523
47699
|
if (e.code !== "ENOENT")
|
|
47524
47700
|
throw e;
|
|
@@ -47528,7 +47704,7 @@ async function sendBatch(db, names) {
|
|
|
47528
47704
|
const moved = new Set([...bad, ...strayed].map((x) => x.name));
|
|
47529
47705
|
for (const x of records)
|
|
47530
47706
|
if (!moved.has(x.name))
|
|
47531
|
-
|
|
47707
|
+
fs7.rmSync(path7.join(x.from, x.name), { force: true });
|
|
47532
47708
|
return { sent, rejected: bad.length };
|
|
47533
47709
|
}
|
|
47534
47710
|
async function sendObservations(db, names) {
|
|
@@ -47539,16 +47715,16 @@ async function sendObservations(db, names) {
|
|
|
47539
47715
|
};
|
|
47540
47716
|
const rows = [];
|
|
47541
47717
|
for (const { name, from } of names) {
|
|
47542
|
-
let
|
|
47718
|
+
let text3;
|
|
47543
47719
|
try {
|
|
47544
|
-
|
|
47720
|
+
text3 = fs7.readFileSync(path7.join(from, name), "utf8");
|
|
47545
47721
|
} catch {
|
|
47546
47722
|
continue;
|
|
47547
47723
|
}
|
|
47548
47724
|
let o = null;
|
|
47549
47725
|
let why = "unreadable";
|
|
47550
47726
|
try {
|
|
47551
|
-
o = observation(JSON.parse(
|
|
47727
|
+
o = observation(JSON.parse(text3));
|
|
47552
47728
|
why = "shape";
|
|
47553
47729
|
} catch {}
|
|
47554
47730
|
if (o)
|
|
@@ -47580,7 +47756,7 @@ async function sendObservations(db, names) {
|
|
|
47580
47756
|
aside(x.name, why);
|
|
47581
47757
|
else
|
|
47582
47758
|
try {
|
|
47583
|
-
|
|
47759
|
+
fs7.rmSync(path7.join(callsDir(), x.name), { force: true });
|
|
47584
47760
|
} catch {}
|
|
47585
47761
|
}
|
|
47586
47762
|
return true;
|
|
@@ -47723,7 +47899,7 @@ async function observeRecordCall(file2, input2, owner, waitMs = OBSERVE_WAIT_MS)
|
|
|
47723
47899
|
}
|
|
47724
47900
|
if (kept)
|
|
47725
47901
|
try {
|
|
47726
|
-
|
|
47902
|
+
fs7.rmSync(kept, { force: true });
|
|
47727
47903
|
} catch {}
|
|
47728
47904
|
}
|
|
47729
47905
|
async function main() {
|
|
@@ -47739,11 +47915,11 @@ async function main() {
|
|
|
47739
47915
|
}
|
|
47740
47916
|
if (host === "codex" && input2.hook_event_name === "Stop")
|
|
47741
47917
|
process.stdout.write("{}");
|
|
47742
|
-
const { flush: send, notice } = onHook(host, input2);
|
|
47918
|
+
const { flush: send, notice } = await onHook(host, input2);
|
|
47743
47919
|
if (notice)
|
|
47744
47920
|
process.stdout.write(JSON.stringify({ systemMessage: notice }));
|
|
47745
47921
|
if (send)
|
|
47746
|
-
|
|
47922
|
+
spawn2(process.execPath, [process.argv[1] ?? "", "--flush"], {
|
|
47747
47923
|
detached: true,
|
|
47748
47924
|
stdio: "ignore",
|
|
47749
47925
|
windowsHide: true
|
|
@@ -47783,13 +47959,24 @@ function closingRefs(body, repo) {
|
|
|
47783
47959
|
var repoOf = (key) => /^git:github\.com\/([^/]+\/[^/]+)$/.exec(key)?.[1] ?? null;
|
|
47784
47960
|
var MAX_RESPONSE = 16 * 1024 * 1024;
|
|
47785
47961
|
var HOST = ["--hostname", "github.com"];
|
|
47786
|
-
var plainEnv = () => ({
|
|
47787
|
-
|
|
47788
|
-
|
|
47962
|
+
var plainEnv = () => ({
|
|
47963
|
+
...Object.fromEntries(Object.entries(process.env).filter(([k]) => !/^GIT_/i.test(k))),
|
|
47964
|
+
CLICOLOR_FORCE: "0"
|
|
47965
|
+
});
|
|
47966
|
+
var ghCwd = startDir;
|
|
47967
|
+
var gh = (repo, timeout = 60000) => async (path8, all = false) => {
|
|
47968
|
+
const { stdout } = await exec("gh", ["api", `repos/${repo}/${path8}`, ...HOST, ...all ? ["--paginate", "--slurp"] : []], {
|
|
47969
|
+
encoding: "utf8",
|
|
47970
|
+
maxBuffer: MAX_RESPONSE,
|
|
47971
|
+
timeout,
|
|
47972
|
+
killSignal: "SIGKILL",
|
|
47973
|
+
env: plainEnv(),
|
|
47974
|
+
cwd: ghCwd()
|
|
47975
|
+
}).catch((e) => {
|
|
47789
47976
|
if (e.code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER")
|
|
47790
|
-
throw new Error(`${
|
|
47977
|
+
throw new Error(`${path8.split("?")[0]} is too large to read (over ${MAX_RESPONSE / 1024 / 1024} MB)`);
|
|
47791
47978
|
if (e.killed)
|
|
47792
|
-
throw new Error(`${
|
|
47979
|
+
throw new Error(`${path8.split("?")[0]} did not answer within ${timeout / 1000} seconds`);
|
|
47793
47980
|
throw e;
|
|
47794
47981
|
});
|
|
47795
47982
|
const parsed = JSON.parse(stdout);
|
|
@@ -47962,13 +48149,13 @@ async function storeItems(db, projectId2, items) {
|
|
|
47962
48149
|
const bound = authorId !== null && owners.has(authorId) && SPOKEN.has(it.kind);
|
|
47963
48150
|
const kind = bound ? "owner" : it.author?.type === "Bot" ? "bot" : "person";
|
|
47964
48151
|
const created = iso(it.createdAt);
|
|
47965
|
-
const
|
|
48152
|
+
const revision2 = Math.max(latest?.revision ?? 0, lastForgotten) + 1;
|
|
47966
48153
|
await db.insertInto("ingest_source").values({
|
|
47967
48154
|
project_id: projectId2,
|
|
47968
48155
|
kind: it.kind,
|
|
47969
48156
|
artifact: it.artifact,
|
|
47970
48157
|
external_id: it.externalId,
|
|
47971
|
-
revision,
|
|
48158
|
+
revision: revision2,
|
|
47972
48159
|
author_kind: kind,
|
|
47973
48160
|
author_login: it.author?.login ?? null,
|
|
47974
48161
|
author_external_id: authorId,
|
|
@@ -47991,12 +48178,12 @@ async function storeItems(db, projectId2, items) {
|
|
|
47991
48178
|
commit_sha: it.commit,
|
|
47992
48179
|
indexed: it.kind === "pr_event" ? 0 : 1
|
|
47993
48180
|
}).execute();
|
|
47994
|
-
ids.push(await itemId(db, projectId2, it.kind, it.externalId,
|
|
48181
|
+
ids.push(await itemId(db, projectId2, it.kind, it.externalId, revision2));
|
|
47995
48182
|
}
|
|
47996
48183
|
return ids;
|
|
47997
48184
|
}
|
|
47998
|
-
async function itemId(db, projectId2, kind, externalId,
|
|
47999
|
-
const row = await db.selectFrom("source").select("id").where("project_id", "=", projectId2).where("kind", "=", kind).where("external_id", "=", externalId).where("revision", "=",
|
|
48185
|
+
async function itemId(db, projectId2, kind, externalId, revision2) {
|
|
48186
|
+
const row = await db.selectFrom("source").select("id").where("project_id", "=", projectId2).where("kind", "=", kind).where("external_id", "=", externalId).where("revision", "=", revision2).where("session_id", "is", null).executeTakeFirstOrThrow();
|
|
48000
48187
|
return row.id;
|
|
48001
48188
|
}
|
|
48002
48189
|
async function linkIssues(db, projectId2, number5, closes) {
|
|
@@ -48036,36 +48223,6 @@ async function runSources(db, runId) {
|
|
|
48036
48223
|
]).select((eb) => eb.exists(eb.selectFrom("source_processing as p").whereRef("p.source_id", "=", "s.id").select(sql`1`.as("x"))).as("looked")).orderBy("s.created_at").orderBy("s.id").execute();
|
|
48037
48224
|
}
|
|
48038
48225
|
|
|
48039
|
-
// server/src/git.ts
|
|
48040
|
-
import { execFileSync as execFileSync3 } from "node:child_process";
|
|
48041
|
-
function cleanGit(root, args, max = 1024 * 1024) {
|
|
48042
|
-
const env = Object.fromEntries(Object.entries(process.env).filter(([k]) => !k.startsWith("GIT_")));
|
|
48043
|
-
return execFileSync3("git", ["-C", root, ...args], {
|
|
48044
|
-
env,
|
|
48045
|
-
maxBuffer: max,
|
|
48046
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
48047
|
-
timeout: 1e4
|
|
48048
|
-
});
|
|
48049
|
-
}
|
|
48050
|
-
function commitHolds(root, commit, rel) {
|
|
48051
|
-
try {
|
|
48052
|
-
const entry = cleanGit(root, ["ls-tree", "-z", commit, "--", rel]).toString("utf8").split("\x00")[0] ?? "";
|
|
48053
|
-
const m = /^(\d{6}) (\w+) [0-9a-f]+\t(.*)$/.exec(entry);
|
|
48054
|
-
return !!m && (m[1] === "100644" || m[1] === "100755") && m[2] === "blob" && m[3] === rel;
|
|
48055
|
-
} catch {
|
|
48056
|
-
return false;
|
|
48057
|
-
}
|
|
48058
|
-
}
|
|
48059
|
-
function repoFiles(root) {
|
|
48060
|
-
try {
|
|
48061
|
-
const list = (...args) => cleanGit(root, ["ls-files", "-z", ...args], 32 * 1024 * 1024).toString("utf8").split("\x00").filter(Boolean);
|
|
48062
|
-
const deleted = new Set(list("--deleted"));
|
|
48063
|
-
return [...new Set(list("--cached", "--others", "--exclude-standard"))].filter((p) => !deleted.has(p));
|
|
48064
|
-
} catch {
|
|
48065
|
-
return null;
|
|
48066
|
-
}
|
|
48067
|
-
}
|
|
48068
|
-
|
|
48069
48226
|
// server/src/judge.ts
|
|
48070
48227
|
var replaceable = (successor, old) => successor === old || ["decision", "constraint"].includes(successor) && ["decision", "constraint"].includes(old);
|
|
48071
48228
|
var owned = (kind) => kind === "decision" || kind === "constraint";
|
|
@@ -48089,11 +48246,11 @@ function judge(s) {
|
|
|
48089
48246
|
contenders.set(i.to, [...contenders.get(i.to) ?? [], i]);
|
|
48090
48247
|
}
|
|
48091
48248
|
const holders = new Map;
|
|
48092
|
-
for (const [to,
|
|
48249
|
+
for (const [to, list2] of contenders) {
|
|
48093
48250
|
const kept = openInto.get(to);
|
|
48094
|
-
const holder =
|
|
48251
|
+
const holder = list2.some((i) => i.from === kept) ? kept : list2[0].from;
|
|
48095
48252
|
holders.set(to, holder);
|
|
48096
|
-
for (const i of
|
|
48253
|
+
for (const i of list2)
|
|
48097
48254
|
if (i.from !== holder)
|
|
48098
48255
|
waits.set(i.from, { why: "place held", holder });
|
|
48099
48256
|
}
|
|
@@ -48459,34 +48616,34 @@ var KEY = /^[a-z0-9][a-z0-9._/-]{0,63}$/;
|
|
|
48459
48616
|
var FIELD_NAME2 = /^[a-z][a-z0-9_]{0,39}$/;
|
|
48460
48617
|
var SOURCE_REF = /^s[1-9][0-9]{0,15}$/;
|
|
48461
48618
|
var MAINTAINERS = new Set(["OWNER", "MEMBER", "COLLABORATOR"]);
|
|
48462
|
-
var
|
|
48619
|
+
var text3 = (max) => exports_external.string().trim().min(1).max(max);
|
|
48463
48620
|
var Quote = exports_external.object({ source: exports_external.string().regex(SOURCE_REF, "cite a source ref such as s12"), quote: exports_external.string() }).strict();
|
|
48464
48621
|
var Evidence = Quote.extend({
|
|
48465
48622
|
role: exports_external.enum(EVIDENCE_ROLES).exclude(["reconsiders"]),
|
|
48466
|
-
reported_speaker:
|
|
48623
|
+
reported_speaker: text3(100).optional()
|
|
48467
48624
|
}).strict();
|
|
48468
48625
|
var Unit = exports_external.object({
|
|
48469
48626
|
key: exports_external.string().regex(KEY, "use lowercase letters, digits, and . _ - / (at most 64)"),
|
|
48470
48627
|
kind: exports_external.enum(UNIT_KINDS),
|
|
48471
48628
|
stance: exports_external.enum(STANCES).optional(),
|
|
48472
|
-
text:
|
|
48473
|
-
why:
|
|
48474
|
-
scope_note:
|
|
48475
|
-
revisit_when:
|
|
48476
|
-
no_code_surface:
|
|
48629
|
+
text: text3(2000),
|
|
48630
|
+
why: text3(2000).optional(),
|
|
48631
|
+
scope_note: text3(1000).optional(),
|
|
48632
|
+
revisit_when: text3(1000).optional(),
|
|
48633
|
+
no_code_surface: text3(500).optional(),
|
|
48477
48634
|
options: exports_external.array(exports_external.object({
|
|
48478
|
-
text:
|
|
48635
|
+
text: text3(500),
|
|
48479
48636
|
outcome: exports_external.enum(OPTION_OUTCOMES),
|
|
48480
|
-
why:
|
|
48481
|
-
reconsider_when:
|
|
48637
|
+
why: text3(2000).optional(),
|
|
48638
|
+
reconsider_when: text3(1000).optional(),
|
|
48482
48639
|
reconsider_quote: Quote.optional(),
|
|
48483
48640
|
evidence: exports_external.array(Evidence.partial({ role: true })).max(10).default([])
|
|
48484
48641
|
}).strict()).max(12).default([]),
|
|
48485
48642
|
evidence: exports_external.array(Evidence).max(20).default([]),
|
|
48486
48643
|
adoption: exports_external.array(Quote).max(5).default([]),
|
|
48487
48644
|
anchors: exports_external.array(exports_external.object({
|
|
48488
|
-
path:
|
|
48489
|
-
symbol:
|
|
48645
|
+
path: text3(500),
|
|
48646
|
+
symbol: text3(200).optional(),
|
|
48490
48647
|
role: exports_external.enum(["applies_to", "evidence"]),
|
|
48491
48648
|
commit: exports_external.string().regex(/^[0-9a-f]{40}$/, "a full 40-character commit sha").optional(),
|
|
48492
48649
|
lines: exports_external.tuple([exports_external.number().int().positive(), exports_external.number().int().positive()]).optional()
|
|
@@ -48496,25 +48653,25 @@ var Unit = exports_external.object({
|
|
|
48496
48653
|
conflicts: exports_external.array(exports_external.string().min(1)).max(5).default([]),
|
|
48497
48654
|
fields: exports_external.array(exports_external.object({
|
|
48498
48655
|
name: exports_external.string().regex(FIELD_NAME2, "a defined field name"),
|
|
48499
|
-
value:
|
|
48656
|
+
value: text3(200),
|
|
48500
48657
|
quote: Quote
|
|
48501
48658
|
}).strict()).max(12).default([])
|
|
48502
48659
|
}).strict();
|
|
48503
48660
|
var FieldDef = exports_external.object({
|
|
48504
48661
|
name: exports_external.string().regex(FIELD_NAME2, "a lowercase letter, then lowercase letters, digits, and _ (at most 40)"),
|
|
48505
48662
|
type: exports_external.enum(FIELD_TYPES),
|
|
48506
|
-
label:
|
|
48507
|
-
description:
|
|
48508
|
-
enum: exports_external.array(
|
|
48663
|
+
label: text3(100),
|
|
48664
|
+
description: text3(500),
|
|
48665
|
+
enum: exports_external.array(text3(100)).min(1).max(30).optional(),
|
|
48509
48666
|
kinds: exports_external.array(exports_external.enum(UNIT_KINDS)).max(UNIT_KINDS.length).default([]),
|
|
48510
48667
|
quote: Quote
|
|
48511
48668
|
}).strict();
|
|
48512
48669
|
var Work = exports_external.object({
|
|
48513
48670
|
key: exports_external.string().regex(KEY),
|
|
48514
|
-
title:
|
|
48515
|
-
goal:
|
|
48516
|
-
current:
|
|
48517
|
-
next: exports_external.array(
|
|
48671
|
+
title: text3(200),
|
|
48672
|
+
goal: text3(1000),
|
|
48673
|
+
current: text3(2000),
|
|
48674
|
+
next: exports_external.array(text3(500)).max(10).default([]),
|
|
48518
48675
|
status: exports_external.enum(WORK_STATUSES)
|
|
48519
48676
|
}).strict();
|
|
48520
48677
|
var Record = exports_external.object({
|
|
@@ -48714,7 +48871,7 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
|
|
|
48714
48871
|
const key = keys[i] ?? "";
|
|
48715
48872
|
const quarantine = [];
|
|
48716
48873
|
const cites = new Set;
|
|
48717
|
-
const spans = (
|
|
48874
|
+
const spans = (list2, option = false) => list2.flatMap((e) => {
|
|
48718
48875
|
const s = sources.get(Number(e.source.slice(1)));
|
|
48719
48876
|
if (!s)
|
|
48720
48877
|
return [];
|
|
@@ -48975,8 +49132,8 @@ function valueInQuote(type, value, quote) {
|
|
|
48975
49132
|
return standsAlone(quote, value);
|
|
48976
49133
|
return [...quote.matchAll(NUMBER)].some((m) => m[0].replace(/^\+/, "").replaceAll(",", "") === value);
|
|
48977
49134
|
}
|
|
48978
|
-
var ciPath = (
|
|
48979
|
-
const p =
|
|
49135
|
+
var ciPath = (path8) => {
|
|
49136
|
+
const p = path8.toLowerCase().replace(/\/+$/, "");
|
|
48980
49137
|
return p === ".github" || p === ".github/workflows" || p.startsWith(".github/workflows/");
|
|
48981
49138
|
};
|
|
48982
49139
|
async function agentRefusal(db, u, x, s, evidence, anchors) {
|
|
@@ -48989,7 +49146,7 @@ async function agentRefusal(db, u, x, s, evidence, anchors) {
|
|
|
48989
49146
|
if (!evidence.some((e) => e.role === "decides" && e.source === x.source && e.start === x.start && e.end === x.end))
|
|
48990
49147
|
return "quote the same words as decides evidence: the AI choosing, not reporting, proposing, or asking";
|
|
48991
49148
|
const governs = anchors.filter((a) => a.role === "applies_to").map((a) => a.path);
|
|
48992
|
-
const bound = anchors.map((a) => a.path).find((
|
|
49149
|
+
const bound = anchors.map((a) => a.path).find((path8) => instructionFile(path8) || ciPath(path8));
|
|
48993
49150
|
if (bound)
|
|
48994
49151
|
return `${bound} holds rules or CI agents follow; only the owner adopts decisions about it`;
|
|
48995
49152
|
if (await db.selectFrom("agent_ineligible_source").select("source_id").where("source_id", "=", x.source).executeTakeFirst())
|
|
@@ -49091,8 +49248,8 @@ ${checked.errors.map((e) => ` ${e}`).join(`
|
|
|
49091
49248
|
created_at: now,
|
|
49092
49249
|
content_hash: hash2
|
|
49093
49250
|
}).returning("id").executeTakeFirstOrThrow();
|
|
49094
|
-
const evidence = async (
|
|
49095
|
-
for (const e of
|
|
49251
|
+
const evidence = async (list2, option) => {
|
|
49252
|
+
for (const e of list2) {
|
|
49096
49253
|
cited.add(e.source);
|
|
49097
49254
|
await trx.insertInto("unit_evidence").values({
|
|
49098
49255
|
unit_id: id,
|
|
@@ -49236,7 +49393,7 @@ var MAX_FILE = 1024 * 1024;
|
|
|
49236
49393
|
var MAINTAINERS2 = new Set(["OWNER", "MEMBER", "COLLABORATOR"]);
|
|
49237
49394
|
var SOURCE_REF2 = exports_external.string().regex(/^s[1-9][0-9]{0,15}$/, "cite a source ref such as s12");
|
|
49238
49395
|
var unit = exports_external.string().min(1).max(200);
|
|
49239
|
-
var
|
|
49396
|
+
var revision2 = exports_external.number().int().positive();
|
|
49240
49397
|
var quote = exports_external.string().min(1).max(4000);
|
|
49241
49398
|
var File2 = exports_external.object({
|
|
49242
49399
|
path: exports_external.string().min(1).max(500),
|
|
@@ -49252,18 +49409,18 @@ var Op = exports_external.discriminatedUnion("op", [
|
|
|
49252
49409
|
exports_external.object({
|
|
49253
49410
|
op: exports_external.literal("add_evidence"),
|
|
49254
49411
|
unit,
|
|
49255
|
-
revision,
|
|
49412
|
+
revision: revision2,
|
|
49256
49413
|
source: SOURCE_REF2.optional(),
|
|
49257
49414
|
file: File2.optional(),
|
|
49258
49415
|
quote,
|
|
49259
49416
|
role: exports_external.enum(EVIDENCE_ROLES).exclude(["reconsiders", "decides"]),
|
|
49260
49417
|
reported_speaker: exports_external.string().trim().min(1).max(100).optional()
|
|
49261
49418
|
}).strict(),
|
|
49262
|
-
exports_external.object({ op: exports_external.literal("adopt"), unit, revision, source: SOURCE_REF2, quote }).strict(),
|
|
49419
|
+
exports_external.object({ op: exports_external.literal("adopt"), unit, revision: revision2, source: SOURCE_REF2, quote }).strict(),
|
|
49263
49420
|
exports_external.object({
|
|
49264
49421
|
op: exports_external.literal("anchor"),
|
|
49265
49422
|
unit,
|
|
49266
|
-
revision,
|
|
49423
|
+
revision: revision2,
|
|
49267
49424
|
path: exports_external.string().min(1).max(500),
|
|
49268
49425
|
symbol: exports_external.string().min(1).max(200).optional(),
|
|
49269
49426
|
role: exports_external.enum(["applies_to", "evidence"]),
|
|
@@ -49272,7 +49429,7 @@ var Op = exports_external.discriminatedUnion("op", [
|
|
|
49272
49429
|
exports_external.object({
|
|
49273
49430
|
op: exports_external.literal("replace_anchor"),
|
|
49274
49431
|
unit,
|
|
49275
|
-
revision,
|
|
49432
|
+
revision: revision2,
|
|
49276
49433
|
from: From,
|
|
49277
49434
|
to: exports_external.object({
|
|
49278
49435
|
path: exports_external.string().min(1).max(500),
|
|
@@ -49282,11 +49439,11 @@ var Op = exports_external.discriminatedUnion("op", [
|
|
|
49282
49439
|
source: SOURCE_REF2,
|
|
49283
49440
|
quote
|
|
49284
49441
|
}).strict(),
|
|
49285
|
-
exports_external.object({ op: exports_external.literal("retire_anchor"), unit, revision, from: From, source: SOURCE_REF2, quote }).strict(),
|
|
49442
|
+
exports_external.object({ op: exports_external.literal("retire_anchor"), unit, revision: revision2, from: From, source: SOURCE_REF2, quote }).strict(),
|
|
49286
49443
|
exports_external.object({
|
|
49287
49444
|
op: exports_external.literal("retract_evidence"),
|
|
49288
49445
|
unit,
|
|
49289
|
-
revision,
|
|
49446
|
+
revision: revision2,
|
|
49290
49447
|
source: SOURCE_REF2,
|
|
49291
49448
|
quote: quote.optional(),
|
|
49292
49449
|
reason_source: SOURCE_REF2,
|
|
@@ -49295,36 +49452,35 @@ var Op = exports_external.discriminatedUnion("op", [
|
|
|
49295
49452
|
exports_external.object({
|
|
49296
49453
|
op: exports_external.literal("retract_adoption"),
|
|
49297
49454
|
unit,
|
|
49298
|
-
revision,
|
|
49455
|
+
revision: revision2,
|
|
49299
49456
|
source: SOURCE_REF2,
|
|
49300
49457
|
quote: quote.optional(),
|
|
49301
49458
|
reason_source: SOURCE_REF2,
|
|
49302
49459
|
reason_quote: quote
|
|
49303
49460
|
}).strict(),
|
|
49304
|
-
exports_external.object({ op: exports_external.literal("withdraw"), unit, revision, reason_source: SOURCE_REF2, reason_quote: quote }).strict(),
|
|
49305
|
-
exports_external.object({ op: exports_external.literal("replace_aliases"), unit, revision, aliases: exports_external.array(exports_external.string()).max(12) }).strict(),
|
|
49461
|
+
exports_external.object({ op: exports_external.literal("withdraw"), unit, revision: revision2, reason_source: SOURCE_REF2, reason_quote: quote }).strict(),
|
|
49462
|
+
exports_external.object({ op: exports_external.literal("replace_aliases"), unit, revision: revision2, aliases: exports_external.array(exports_external.string()).max(12) }).strict(),
|
|
49306
49463
|
exports_external.object({
|
|
49307
49464
|
op: exports_external.literal("resolve_conflict"),
|
|
49308
49465
|
unit,
|
|
49309
|
-
revision,
|
|
49466
|
+
revision: revision2,
|
|
49310
49467
|
with: unit,
|
|
49311
49468
|
reason_source: SOURCE_REF2,
|
|
49312
49469
|
reason_quote: quote
|
|
49313
49470
|
}).strict()
|
|
49314
49471
|
]);
|
|
49315
49472
|
var Glean = exports_external.object({ units: exports_external.array(exports_external.unknown()).max(20).default([]), ops: exports_external.array(Op).max(50).default([]) }).strict();
|
|
49316
|
-
var git3 = (root, args, max = MAX_FILE * 2) => cleanGit(root, args, max);
|
|
49317
49473
|
function readExcerpt(root, file3) {
|
|
49318
49474
|
const p = repoPath(file3.path);
|
|
49319
49475
|
if (!p)
|
|
49320
49476
|
throw new Error(`${JSON.stringify(head(file3.path, 80))} is not a path inside the repository`);
|
|
49321
49477
|
let commit;
|
|
49322
49478
|
try {
|
|
49323
|
-
commit =
|
|
49479
|
+
commit = commitOf(root, file3.commit);
|
|
49324
49480
|
} catch {
|
|
49325
49481
|
throw new Error(`${JSON.stringify(head(file3.commit, 40))} is not a commit of this repository`);
|
|
49326
49482
|
}
|
|
49327
|
-
const entry =
|
|
49483
|
+
const entry = treeEntry(root, commit, p);
|
|
49328
49484
|
const m = /^(\d{6}) (\w+) ([0-9a-f]{40})\t(.*)$/.exec(entry);
|
|
49329
49485
|
if (!m || m[4] !== p)
|
|
49330
49486
|
throw new Error(`${p} is not in commit ${commit.slice(0, 12)}`);
|
|
@@ -49333,10 +49489,10 @@ function readExcerpt(root, file3) {
|
|
|
49333
49489
|
throw new Error(`${p} is a symbolic link; cite the file it points to`);
|
|
49334
49490
|
if (type !== "blob" || !["100644", "100755"].includes(mode ?? ""))
|
|
49335
49491
|
throw new Error(`${p} is not a regular file (${type})`);
|
|
49336
|
-
const size =
|
|
49492
|
+
const size = blobSize(root, blob);
|
|
49337
49493
|
if (size > MAX_FILE)
|
|
49338
49494
|
throw new Error(`${p} is ${size} bytes, over the ${MAX_FILE}-byte limit`);
|
|
49339
|
-
const buf =
|
|
49495
|
+
const buf = blobBytes(root, blob, MAX_FILE * 2);
|
|
49340
49496
|
if (buf.includes(0))
|
|
49341
49497
|
throw new Error(`${p} is binary`);
|
|
49342
49498
|
try {
|
|
@@ -49356,12 +49512,12 @@ function readExcerpt(root, file3) {
|
|
|
49356
49512
|
if (privateKeyRanges(buf.toString("utf8")).some(([s, e]) => s < to && e > from && (s < from || e > to)))
|
|
49357
49513
|
throw new Error(`${p} lines ${a}-${b} are inside a private key; cite lines outside it`);
|
|
49358
49514
|
const raw = buf.subarray(from, to).toString("utf8");
|
|
49359
|
-
const
|
|
49515
|
+
const text4 = mask(raw);
|
|
49360
49516
|
const before = buf.subarray(0, from).toString("utf8");
|
|
49361
49517
|
const after = buf.subarray(to).toString("utf8");
|
|
49362
|
-
if (mask(before + raw) !== mask(before) +
|
|
49518
|
+
if (mask(before + raw) !== mask(before) + text4 || mask(raw + after) !== text4 + mask(after))
|
|
49363
49519
|
throw new Error(`${p} lines ${a}-${b} cut through text Sphica masks; cite more lines around it`);
|
|
49364
|
-
return { path: p, commit, blob, size, lines: [a, b], raw, text:
|
|
49520
|
+
return { path: p, commit, blob, size, lines: [a, b], raw, text: text4 };
|
|
49365
49521
|
}
|
|
49366
49522
|
var locate2 = (body, q) => {
|
|
49367
49523
|
const at = Buffer.from(body, "utf8").indexOf(Buffer.from(q, "utf8"));
|
|
@@ -49668,13 +49824,13 @@ async function excerptSource(trx, projectId2, x) {
|
|
|
49668
49824
|
if (forgotten.same)
|
|
49669
49825
|
throw new Error(`the owner forgot ${x.path} lines ${x.lines.join("-")}; cite something else`);
|
|
49670
49826
|
const now = iso(Date.now());
|
|
49671
|
-
const
|
|
49827
|
+
const revision3 = Math.max(found?.revision ?? 0, forgotten.last) + 1;
|
|
49672
49828
|
await trx.insertInto("ingest_source").values({
|
|
49673
49829
|
project_id: projectId2,
|
|
49674
49830
|
kind: "file_excerpt",
|
|
49675
49831
|
artifact: `file:${x.path}`,
|
|
49676
49832
|
external_id: external2,
|
|
49677
|
-
revision:
|
|
49833
|
+
revision: revision3,
|
|
49678
49834
|
author_kind: "person",
|
|
49679
49835
|
created_at: now,
|
|
49680
49836
|
captured_at: now,
|
|
@@ -49690,7 +49846,7 @@ async function excerptSource(trx, projectId2, x) {
|
|
|
49690
49846
|
blob_sha: x.blob,
|
|
49691
49847
|
indexed: 1
|
|
49692
49848
|
}).execute();
|
|
49693
|
-
return itemId(trx, projectId2, "file_excerpt", external2,
|
|
49849
|
+
return itemId(trx, projectId2, "file_excerpt", external2, revision3);
|
|
49694
49850
|
}
|
|
49695
49851
|
async function saveGlean(trx, target, runId, c) {
|
|
49696
49852
|
if (c.errors.length)
|
|
@@ -50003,23 +50159,23 @@ async function sessionOf(db, projectId2, given) {
|
|
|
50003
50159
|
async function beginTrace(db, projectId2, session, beginCall) {
|
|
50004
50160
|
await flush(undefined, TOOL_FLUSH_BUDGET_MS).catch(() => {});
|
|
50005
50161
|
const s = await sessionOf(db, projectId2, session);
|
|
50006
|
-
const
|
|
50162
|
+
const run2 = newRunId();
|
|
50007
50163
|
await openRun(db, {
|
|
50008
50164
|
projectId: projectId2,
|
|
50009
50165
|
origin: "trace",
|
|
50010
50166
|
target: `session:${s}`,
|
|
50011
50167
|
sessionId: s,
|
|
50012
|
-
draftId:
|
|
50168
|
+
draftId: run2,
|
|
50013
50169
|
beginCall
|
|
50014
50170
|
});
|
|
50015
|
-
return
|
|
50171
|
+
return run2;
|
|
50016
50172
|
}
|
|
50017
50173
|
async function beginGlean(db, projectId2, session, beginCall) {
|
|
50018
50174
|
await flush(undefined, TOOL_FLUSH_BUDGET_MS).catch(() => {});
|
|
50019
50175
|
const s = await sessionOf(db, projectId2, session);
|
|
50020
|
-
const
|
|
50021
|
-
await openRun(db, { projectId: projectId2, origin: "glean", target: "glean", sessionId: s, draftId:
|
|
50022
|
-
return
|
|
50176
|
+
const run2 = newRunId();
|
|
50177
|
+
await openRun(db, { projectId: projectId2, origin: "glean", target: "glean", sessionId: s, draftId: run2, beginCall });
|
|
50178
|
+
return run2;
|
|
50023
50179
|
}
|
|
50024
50180
|
async function beginHarvest(db, projectId2, number5, get, beginCall) {
|
|
50025
50181
|
const project = await db.selectFrom("project").select("key").where("id", "=", projectId2).executeTakeFirst();
|
|
@@ -50027,29 +50183,29 @@ async function beginHarvest(db, projectId2, number5, get, beginCall) {
|
|
|
50027
50183
|
return inTransaction(db, async (trx) => {
|
|
50028
50184
|
const kept = (await storeItems(trx, projectId2, pull.items)).filter((id) => id !== null).length;
|
|
50029
50185
|
await linkIssues(trx, projectId2, number5, pull.closes);
|
|
50030
|
-
const
|
|
50186
|
+
const run2 = newRunId();
|
|
50031
50187
|
const runId = await openRun(trx, {
|
|
50032
50188
|
projectId: projectId2,
|
|
50033
50189
|
origin: "harvest",
|
|
50034
50190
|
target: `pr:${number5}`,
|
|
50035
50191
|
sessionId: null,
|
|
50036
|
-
draftId:
|
|
50192
|
+
draftId: run2,
|
|
50037
50193
|
beginCall
|
|
50038
50194
|
});
|
|
50039
50195
|
for (const sourceId of await pullSourceIds(trx, projectId2, number5))
|
|
50040
50196
|
await trx.insertInto("harvest_run_source").values({ run_id: runId, source_id: sourceId }).execute();
|
|
50041
|
-
return { run, sources: kept };
|
|
50197
|
+
return { run: run2, sources: kept };
|
|
50042
50198
|
});
|
|
50043
50199
|
}
|
|
50044
50200
|
async function bound(db, id, projectId2) {
|
|
50045
|
-
const
|
|
50046
|
-
if (!
|
|
50201
|
+
const run2 = await runOf(db, id);
|
|
50202
|
+
if (!run2)
|
|
50047
50203
|
throw new Error(`No run ${id.slice(0, 40)}. Begin again`);
|
|
50048
|
-
if (
|
|
50204
|
+
if (run2.project_id !== projectId2)
|
|
50049
50205
|
throw new Error("This run belongs to another project");
|
|
50050
|
-
if (
|
|
50051
|
-
throw new Error(`This run was already ${
|
|
50052
|
-
return
|
|
50206
|
+
if (run2.status !== "running")
|
|
50207
|
+
throw new Error(`This run was already ${run2.status}. Begin again for a new one`);
|
|
50208
|
+
return run2;
|
|
50053
50209
|
}
|
|
50054
50210
|
async function sameCaller(db, begin, call) {
|
|
50055
50211
|
const [a, b] = await Promise.all([callSession(db, begin), callSession(db, call)]);
|
|
@@ -50057,8 +50213,8 @@ async function sameCaller(db, begin, call) {
|
|
|
50057
50213
|
throw new Error("This run was begun in another session; begin a run in this one");
|
|
50058
50214
|
}
|
|
50059
50215
|
async function gleanFetch(db, id, place, url2, get) {
|
|
50060
|
-
const
|
|
50061
|
-
if (
|
|
50216
|
+
const run2 = await bound(db, id, place.projectId);
|
|
50217
|
+
if (run2.origin !== "glean")
|
|
50062
50218
|
throw new Error("fetch is for glean runs");
|
|
50063
50219
|
const repo = repoOf(place.key);
|
|
50064
50220
|
const at = repo ? githubTarget(repo, url2) : null;
|
|
@@ -50079,23 +50235,23 @@ async function gleanFetch(db, id, place, url2, get) {
|
|
|
50079
50235
|
].join(`
|
|
50080
50236
|
`);
|
|
50081
50237
|
}
|
|
50082
|
-
async function agentRun(db,
|
|
50083
|
-
if (
|
|
50238
|
+
async function agentRun(db, run2, call) {
|
|
50239
|
+
if (run2.origin !== "trace" || run2.begin_call_id === null || call === undefined)
|
|
50084
50240
|
return false;
|
|
50085
|
-
const calls = [...new Set([
|
|
50241
|
+
const calls = [...new Set([run2.begin_call_id, call])];
|
|
50086
50242
|
const modes = await db.selectFrom("record_call").select("mode").where("id", "in", calls).execute();
|
|
50087
50243
|
if (modes.length !== calls.length || !modes.every((m) => m.mode === "interactive"))
|
|
50088
50244
|
return false;
|
|
50089
|
-
const [a, b] = await Promise.all([callSession(db,
|
|
50245
|
+
const [a, b] = await Promise.all([callSession(db, run2.begin_call_id), callSession(db, call)]);
|
|
50090
50246
|
return a !== null && b !== null && a.owner && b.owner && a.host === b.host && a.session === b.session;
|
|
50091
50247
|
}
|
|
50092
|
-
async function scopeOf(db,
|
|
50093
|
-
if (
|
|
50094
|
-
const number5 = Number(
|
|
50095
|
-
const sources2 = await runSources(db,
|
|
50248
|
+
async function scopeOf(db, run2, root) {
|
|
50249
|
+
if (run2.origin === "harvest") {
|
|
50250
|
+
const number5 = Number(run2.target.slice("pr:".length));
|
|
50251
|
+
const sources2 = await runSources(db, run2.id);
|
|
50096
50252
|
return {
|
|
50097
50253
|
target: {
|
|
50098
|
-
projectId:
|
|
50254
|
+
projectId: run2.project_id,
|
|
50099
50255
|
origin: "harvest",
|
|
50100
50256
|
prefix: `harvest:${number5}/`,
|
|
50101
50257
|
sessionId: null,
|
|
@@ -50112,16 +50268,16 @@ ${s2.text}`
|
|
|
50112
50268
|
tail: []
|
|
50113
50269
|
};
|
|
50114
50270
|
}
|
|
50115
|
-
const s =
|
|
50271
|
+
const s = run2.session_id ? await db.selectFrom("session").select(["id", "external_id"]).where("id", "=", run2.session_id).executeTakeFirst() : undefined;
|
|
50116
50272
|
if (!s)
|
|
50117
50273
|
throw new Error("The run's session is gone. Begin again");
|
|
50118
50274
|
const sources = await sessionSources(db, s.id);
|
|
50119
|
-
if (
|
|
50275
|
+
if (run2.origin === "glean") {
|
|
50120
50276
|
const asked = (m) => /:ask:.*:q:/.test(m.external_id ?? "");
|
|
50121
50277
|
const owner = sources.filter((m) => m.author_kind === "owner" || asked(m)).slice(-20);
|
|
50122
50278
|
return {
|
|
50123
50279
|
target: {
|
|
50124
|
-
projectId:
|
|
50280
|
+
projectId: run2.project_id,
|
|
50125
50281
|
origin: "glean",
|
|
50126
50282
|
prefix: "glean:",
|
|
50127
50283
|
sessionId: s.id,
|
|
@@ -50143,10 +50299,10 @@ ${m.text}`
|
|
|
50143
50299
|
};
|
|
50144
50300
|
}
|
|
50145
50301
|
const edits = await sessionEdits(db, s.id);
|
|
50146
|
-
const shown = sources.filter((m) => m.captured_at <=
|
|
50302
|
+
const shown = sources.filter((m) => m.captured_at <= run2.started_at);
|
|
50147
50303
|
return {
|
|
50148
50304
|
target: {
|
|
50149
|
-
projectId:
|
|
50305
|
+
projectId: run2.project_id,
|
|
50150
50306
|
origin: "trace",
|
|
50151
50307
|
prefix: `trace:${s.external_id}/`,
|
|
50152
50308
|
sessionId: s.id,
|
|
@@ -50171,10 +50327,10 @@ async function contextText(db, id, projectId2, root, after, auto = false) {
|
|
|
50171
50327
|
const before = shownTo.get(id);
|
|
50172
50328
|
if (before && before.auto !== auto)
|
|
50173
50329
|
throw new Error(before.auto ? "This run is read as an automatic trace; pass auto: true" : "This run is read as an explicit trace; call record_context without auto");
|
|
50174
|
-
const
|
|
50175
|
-
if (auto &&
|
|
50330
|
+
const run2 = await bound(db, id, projectId2);
|
|
50331
|
+
if (auto && run2.origin !== "trace")
|
|
50176
50332
|
throw new Error("auto is for trace runs");
|
|
50177
|
-
const scope = await scopeOf(db,
|
|
50333
|
+
const scope = await scopeOf(db, run2, root);
|
|
50178
50334
|
let items = scope.items;
|
|
50179
50335
|
let contextCount = 0;
|
|
50180
50336
|
if (auto) {
|
|
@@ -50244,12 +50400,12 @@ async function contextText(db, id, projectId2, root, after, auto = false) {
|
|
|
50244
50400
|
const context = end < contextCount;
|
|
50245
50401
|
const heading = label(end);
|
|
50246
50402
|
const body = entry(it, context ? CONTEXT_CHARS : PAGE_CHARS);
|
|
50247
|
-
const
|
|
50403
|
+
const text4 = heading ? `${heading}
|
|
50248
50404
|
${body}` : body;
|
|
50249
|
-
if (page.length && used +
|
|
50405
|
+
if (page.length && used + text4.length > PAGE_CHARS)
|
|
50250
50406
|
break;
|
|
50251
|
-
page.push({ id: it.id, text:
|
|
50252
|
-
used +=
|
|
50407
|
+
page.push({ id: it.id, text: text4, context });
|
|
50408
|
+
used += text4.length;
|
|
50253
50409
|
}
|
|
50254
50410
|
const left = items.length - end;
|
|
50255
50411
|
const nextNo = pageNo + (page.some((x) => !x.context) ? 1 : 0);
|
|
@@ -50304,21 +50460,21 @@ async function prepare2(db, id, projectId2, root, record4, probe, call) {
|
|
|
50304
50460
|
return { facts: gleanFacts ?? prepareRecord(root, record4, probe), gleanFacts };
|
|
50305
50461
|
}
|
|
50306
50462
|
async function validate2(trx, id, projectId2, root, record4, prepared, call) {
|
|
50307
|
-
const
|
|
50308
|
-
const scope = await scopeOf(trx,
|
|
50309
|
-
scope.target.agent = await agentRun(trx,
|
|
50463
|
+
const run2 = await bound(trx, id, projectId2);
|
|
50464
|
+
const scope = await scopeOf(trx, run2, root);
|
|
50465
|
+
scope.target.agent = await agentRun(trx, run2, call);
|
|
50310
50466
|
const quarantine = (units) => units.filter((u) => u.quarantine.length).map((u) => `${u.key} will be quarantined: ${u.quarantine.join("; ")}`);
|
|
50311
|
-
if (
|
|
50467
|
+
if (run2.origin === "glean") {
|
|
50312
50468
|
const c = await checkGlean(trx, scope.target, record4, prepared.gleanFacts);
|
|
50313
50469
|
return {
|
|
50314
|
-
run,
|
|
50470
|
+
run: run2,
|
|
50315
50471
|
errors: c.errors,
|
|
50316
50472
|
problems: c.problems,
|
|
50317
50473
|
quarantine: quarantine(c.units.units),
|
|
50318
50474
|
units: c.units.units.length,
|
|
50319
50475
|
ops: c.ops.length,
|
|
50320
50476
|
write: async (w) => {
|
|
50321
|
-
const g = await saveGlean(w, scope.target,
|
|
50477
|
+
const g = await saveGlean(w, scope.target, run2.id, c);
|
|
50322
50478
|
return { saved: g.units, changed: g.changed };
|
|
50323
50479
|
}
|
|
50324
50480
|
};
|
|
@@ -50326,7 +50482,7 @@ async function validate2(trx, id, projectId2, root, record4, prepared, call) {
|
|
|
50326
50482
|
const checked = await checkRecord(trx, scope.target, record4, prepared.facts);
|
|
50327
50483
|
checked.errors.push(...contextOnly(id, scope.items, checked));
|
|
50328
50484
|
return {
|
|
50329
|
-
run,
|
|
50485
|
+
run: run2,
|
|
50330
50486
|
errors: checked.errors,
|
|
50331
50487
|
problems: checked.problems,
|
|
50332
50488
|
quarantine: quarantine(checked.units),
|
|
@@ -50343,7 +50499,7 @@ async function validate2(trx, id, projectId2, root, record4, prepared, call) {
|
|
|
50343
50499
|
]),
|
|
50344
50500
|
...checked.fieldDefs
|
|
50345
50501
|
].map((q) => q.source));
|
|
50346
|
-
const saved = await saveRecord(w, scope.target,
|
|
50502
|
+
const saved = await saveRecord(w, scope.target, run2.id, checked, scope.looked.filter((s) => shown.has(s) || cited.has(s)));
|
|
50347
50503
|
return { saved, changed: [] };
|
|
50348
50504
|
}
|
|
50349
50505
|
};
|
|
@@ -50383,7 +50539,7 @@ async function checkText(db, id, projectId2, root, record4, call, probe) {
|
|
|
50383
50539
|
}
|
|
50384
50540
|
async function saveText(db, id, projectId2, root, record4, probe, call) {
|
|
50385
50541
|
const prepared = await prepare2(db, id, projectId2, root, record4, probe, call);
|
|
50386
|
-
const
|
|
50542
|
+
const text4 = await inTransaction(db, async (trx) => {
|
|
50387
50543
|
const v = await validate2(trx, id, projectId2, root, record4, prepared, call);
|
|
50388
50544
|
if (v.errors.length)
|
|
50389
50545
|
throw new Error(`The record is not valid:
|
|
@@ -50409,21 +50565,21 @@ ${v.errors.map((e) => ` ${e}`).join(`
|
|
|
50409
50565
|
`);
|
|
50410
50566
|
});
|
|
50411
50567
|
shownTo.delete(id);
|
|
50412
|
-
return
|
|
50568
|
+
return text4;
|
|
50413
50569
|
}
|
|
50414
50570
|
|
|
50415
50571
|
// server/src/backups.ts
|
|
50416
|
-
import
|
|
50417
|
-
import
|
|
50418
|
-
var backupDir = (file3 = dbFile()) =>
|
|
50419
|
-
var stem = (file3) =>
|
|
50572
|
+
import fs8 from "node:fs";
|
|
50573
|
+
import path8 from "node:path";
|
|
50574
|
+
var backupDir = (file3 = dbFile()) => path8.join(path8.dirname(file3), "backups");
|
|
50575
|
+
var stem = (file3) => path8.basename(file3).replace(/\.db$/, "");
|
|
50420
50576
|
var pattern = (file3) => new RegExp(`^${stem(file3).replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\.rev\\d+\\.(\\d{8}T\\d{9}Z)\\.\\d+\\.db$`);
|
|
50421
50577
|
function backups(file3 = dbFile()) {
|
|
50422
50578
|
const dir = backupDir(file3);
|
|
50423
|
-
if (!
|
|
50579
|
+
if (!fs8.existsSync(dir))
|
|
50424
50580
|
return [];
|
|
50425
50581
|
const named = pattern(file3);
|
|
50426
|
-
return
|
|
50582
|
+
return fs8.readdirSync(dir).filter((f) => named.test(f)).sort((a, b) => (named.exec(b)?.[1] ?? "").localeCompare(named.exec(a)?.[1] ?? "") || b.localeCompare(a)).map((f) => path8.join(dir, f));
|
|
50427
50583
|
}
|
|
50428
50584
|
|
|
50429
50585
|
// server/src/forget.ts
|