sphica 0.6.43 → 0.6.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +7 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +8 -2
- package/dist/deliver.js +753 -366
- package/hooks/codex.json +11 -0
- package/hooks/hooks.json +28 -0
- package/package.json +1 -1
package/dist/deliver.js
CHANGED
|
@@ -1,287 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// server/src/deliver.ts
|
|
4
|
-
import
|
|
5
|
-
import os3 from "node:os";
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
// server/src/anchors.ts
|
|
9
|
-
import path from "node:path";
|
|
10
|
-
|
|
11
|
-
// server/src/text.ts
|
|
12
|
-
import crypto from "node:crypto";
|
|
13
|
-
var segmenter = new Intl.Segmenter("ja", { granularity: "word" });
|
|
14
|
-
var HIRAGANA_ONLY = /^[\p{Script=Hiragana}ー]+$/u;
|
|
15
|
-
var STOP = new Set(["the", "a", "an", "of", "to", "in", "is", "and", "or", "for", "on", "it", "be"]);
|
|
16
|
-
var IDENT = /#\d+|[a-z0-9][a-z0-9_./#-]*[a-z0-9]/g;
|
|
17
|
-
var WORD = /[\p{L}\p{M}\p{N}_]+/gu;
|
|
18
|
-
var JAPANESE = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}ー々]+/u;
|
|
19
|
-
var COMPOUND = /^[A-Za-z0-9_]*(?:[A-Za-z0-9]_|_[A-Za-z0-9]|[a-z0-9][A-Z])[A-Za-z0-9_]*$/;
|
|
20
|
-
var CUT = /_+|(?<=[a-z0-9])(?=[A-Z])/;
|
|
21
|
-
var MAX_PARTS = 6;
|
|
22
|
-
var NAME_PART = /^(?:[A-Za-z]+[0-9]*|[0-9]+)$/;
|
|
23
|
-
var MAX_TERM = 100;
|
|
24
|
-
var OKURIGANA = /^(\p{Script=Han}+)[\p{Script=Hiragana}ー]+$/u;
|
|
25
|
-
function singular(w) {
|
|
26
|
-
if (!/^[a-z]{4,}$/.test(w))
|
|
27
|
-
return w;
|
|
28
|
-
if (w.endsWith("ies"))
|
|
29
|
-
return `${w.slice(0, -3)}y`;
|
|
30
|
-
if (w.endsWith("ie"))
|
|
31
|
-
return `${w.slice(0, -2)}y`;
|
|
32
|
-
if (/(?:ss|us|x|z|ch|sh)es$/.test(w))
|
|
33
|
-
return w.slice(0, -2);
|
|
34
|
-
if (/(?:ch|sh|us)e$/.test(w))
|
|
35
|
-
return w.slice(0, -1);
|
|
36
|
-
return w.endsWith("s") && !/(?:ss|us|is)$/.test(w) ? w.slice(0, -1) : w;
|
|
37
|
-
}
|
|
38
|
-
function terms(text, parts = true) {
|
|
39
|
-
const nfkc = text.normalize("NFKC");
|
|
40
|
-
const norm = nfkc.toLowerCase();
|
|
41
|
-
const out = [];
|
|
42
|
-
const keep = (w) => {
|
|
43
|
-
if (w.length > MAX_TERM || STOP.has(w) || HIRAGANA_ONLY.test(w))
|
|
44
|
-
return;
|
|
45
|
-
out.push(OKURIGANA.exec(w)?.[1] ?? singular(w));
|
|
46
|
-
};
|
|
47
|
-
for (const s of segmenter.segment(norm))
|
|
48
|
-
if (s.isWordLike)
|
|
49
|
-
keep(s.segment.trim());
|
|
50
|
-
for (const m of norm.matchAll(IDENT))
|
|
51
|
-
if (m[0].length >= 3)
|
|
52
|
-
keep(m[0]);
|
|
53
|
-
if (parts)
|
|
54
|
-
for (const [word] of nfkc.matchAll(WORD))
|
|
55
|
-
for (const w of word.split(JAPANESE)) {
|
|
56
|
-
if (w.length > MAX_TERM || !COMPOUND.test(w))
|
|
57
|
-
continue;
|
|
58
|
-
const cut = w.split(CUT).filter(Boolean);
|
|
59
|
-
if (cut.length <= MAX_PARTS && cut.every((p) => NAME_PART.test(p))) {
|
|
60
|
-
for (const p of cut)
|
|
61
|
-
if (p.length >= 2)
|
|
62
|
-
keep(p.toLowerCase());
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return out.filter(Boolean);
|
|
66
|
-
}
|
|
67
|
-
var QUESTION = new Set([
|
|
68
|
-
...[
|
|
69
|
-
"why",
|
|
70
|
-
"what",
|
|
71
|
-
"which",
|
|
72
|
-
"when",
|
|
73
|
-
"where",
|
|
74
|
-
"who",
|
|
75
|
-
"how",
|
|
76
|
-
"do",
|
|
77
|
-
"does",
|
|
78
|
-
"did",
|
|
79
|
-
"we",
|
|
80
|
-
"our",
|
|
81
|
-
"us",
|
|
82
|
-
"you",
|
|
83
|
-
"i",
|
|
84
|
-
"are",
|
|
85
|
-
"was",
|
|
86
|
-
"were"
|
|
87
|
-
],
|
|
88
|
-
...[
|
|
89
|
-
"will",
|
|
90
|
-
"would",
|
|
91
|
-
"should",
|
|
92
|
-
"can",
|
|
93
|
-
"could",
|
|
94
|
-
"there",
|
|
95
|
-
"this",
|
|
96
|
-
"that",
|
|
97
|
-
"with",
|
|
98
|
-
"from",
|
|
99
|
-
"any",
|
|
100
|
-
"ever",
|
|
101
|
-
"long",
|
|
102
|
-
"use",
|
|
103
|
-
"used",
|
|
104
|
-
"using"
|
|
105
|
-
],
|
|
106
|
-
...["add", "get", "make", "have", "has", "not", "no", "yes", "reason"],
|
|
107
|
-
...["理由", "仕組", "何", "方", "場合", "今", "件"]
|
|
108
|
-
]);
|
|
109
|
-
var FOLDED_QUESTION = new Set([...QUESTION].flatMap((w) => terms(w)));
|
|
110
|
-
var sha256 = (s) => crypto.createHash("sha256").update(s).digest();
|
|
111
|
-
function uuidFrom(...parts) {
|
|
112
|
-
const b = crypto.createHash("sha256").update(parts.join("\x00")).digest().subarray(0, 16);
|
|
113
|
-
b[6] = (b[6] ?? 0) & 15 | 128;
|
|
114
|
-
b[8] = (b[8] ?? 0) & 63 | 128;
|
|
115
|
-
const h = b.toString("hex");
|
|
116
|
-
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
117
|
-
}
|
|
118
|
-
var bytes = (s) => Buffer.byteLength(s, "utf8");
|
|
119
|
-
function head(s, n) {
|
|
120
|
-
if (bytes(s) <= n)
|
|
121
|
-
return s;
|
|
122
|
-
let out = "";
|
|
123
|
-
let used = 0;
|
|
124
|
-
for (const ch of s) {
|
|
125
|
-
const b = bytes(ch);
|
|
126
|
-
if (used + b > n)
|
|
127
|
-
break;
|
|
128
|
-
out += ch;
|
|
129
|
-
used += b;
|
|
130
|
-
}
|
|
131
|
-
return out;
|
|
132
|
-
}
|
|
133
|
-
function tail(s, n) {
|
|
134
|
-
if (bytes(s) <= n)
|
|
135
|
-
return s;
|
|
136
|
-
const chars = [...s];
|
|
137
|
-
let used = 0;
|
|
138
|
-
let i = chars.length;
|
|
139
|
-
while (i > 0) {
|
|
140
|
-
const b = bytes(chars[i - 1] ?? "");
|
|
141
|
-
if (used + b > n)
|
|
142
|
-
break;
|
|
143
|
-
used += b;
|
|
144
|
-
i--;
|
|
145
|
-
}
|
|
146
|
-
return chars.slice(i).join("");
|
|
147
|
-
}
|
|
148
|
-
var clean = (s) => s.replaceAll("\x00", "");
|
|
149
|
-
var visible = (s) => s.replace(/(?!\p{Join_Control}|\p{Variation_Selector})\p{Default_Ignorable_Code_Point}/gu, "");
|
|
150
|
-
var SECRETS = [
|
|
151
|
-
[/\bsk-(?:proj-|ant-)?[A-Za-z0-9_-]{20,}/g, "API key"],
|
|
152
|
-
[/\b[srp]k_(?:live|test)_[A-Za-z0-9]{16,}/g, "API key"],
|
|
153
|
-
[/\bwhsec_[A-Za-z0-9+/=]{16,}/g, "webhook signing secret"],
|
|
154
|
-
[/\bpa-[A-Za-z0-9_-]{20,}/g, "API key"],
|
|
155
|
-
[/\bAIza[0-9A-Za-z_-]{35}/g, "API key"],
|
|
156
|
-
[/\bnpg_[A-Za-z0-9]{12,}/g, "database password"],
|
|
157
|
-
[/\bnapi_[A-Za-z0-9]{30,}/g, "API key"],
|
|
158
|
-
[/\bnpm_[A-Za-z0-9]{36}\b/g, "npm token"],
|
|
159
|
-
[/\bglpat-[A-Za-z0-9_-]{20,}/g, "GitLab token"],
|
|
160
|
-
[
|
|
161
|
-
/(?<![A-Za-z0-9])ghs_eyJ[A-Za-z0-9_-]{5,200}\.[A-Za-z0-9_-]{8,2000}\.[A-Za-z0-9_-]{8,1000}/g,
|
|
162
|
-
"GitHub token"
|
|
163
|
-
],
|
|
164
|
-
[/(?<![A-Za-z0-9])gh[pousr]_[A-Za-z0-9]{30,}/g, "GitHub token"],
|
|
165
|
-
[/(?<![A-Za-z0-9])github_pat_[A-Za-z0-9_]{40,}/g, "GitHub token"],
|
|
166
|
-
[/\bxox[abprs]-[A-Za-z0-9-]{10,}/g, "Slack token"],
|
|
167
|
-
[/https:\/\/hooks\.slack\.com\/services\/[A-Za-z0-9/]+/g, "Slack webhook"],
|
|
168
|
-
[/\bAKIA[0-9A-Z]{16}\b/g, "AWS key"],
|
|
169
|
-
[/(?<![A-Za-z0-9_-])eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, "JWT"],
|
|
170
|
-
[/\b(?:Bearer|BEARER)\s+(?=[A-Za-z0-9._~+/=-]{0,512}\d)[A-Za-z0-9._~+/=-]{16,}/g, "auth header value"]
|
|
171
|
-
];
|
|
172
|
-
var AUTH_HEADER = /(\bAuthorization["']?\s*[:=]\s*(?:["']\s*)?(?:Bearer|Basic|Token|Digest)\s+)[A-Za-z0-9._~+/=-]{8,}/gi;
|
|
173
|
-
var HEADER_BEARER = /(:[ \t]*(?:["'][ \t]*)?bearer[ \t]+)[A-Za-z0-9._~+/=-]{16,}/gi;
|
|
174
|
-
var ENV_ASSIGN = /\b((?:[A-Z][A-Z0-9_]*_)?(?:API|SECRET|MASTER|ENCRYPTION|PRIVATE|ACCESS|SIGNING|AUTH)?KEY|[A-Z][A-Z0-9_]*_(?:PASS|PWD)|(?:[A-Z][A-Z0-9_]*?)?(?:TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIALS?))(\s*=\s*)(?:"(?!\$)[^"\n]+"|'(?!\$)[^'\n]+'|(?![$"'])[^\s"']+)/g;
|
|
175
|
-
var FIELD_NAME = /(?:(?:api|account|access|private|secret)[-_]?key|secret|token|passw(?:or)?d)["']?\s*(?::=|=>|[:=])\s*/gi;
|
|
176
|
-
var MAX_QUOTED = 4096;
|
|
177
|
-
var BARE_HEAD = /[^\s"',;)]{1,256}/y;
|
|
178
|
-
var BARE_REST = /[^\s"',;)]*/y;
|
|
179
|
-
var NEXT_PARAM = /&[A-Za-z_][\w.-]*=/;
|
|
180
|
-
var bareAt = (re, text, at) => {
|
|
181
|
-
re.lastIndex = at;
|
|
182
|
-
const v = re.exec(text)?.[0] ?? "";
|
|
183
|
-
const cut = v.search(NEXT_PARAM);
|
|
184
|
-
return cut < 0 ? v : v.slice(0, cut);
|
|
185
|
-
};
|
|
186
|
-
function secretValue(quoted, v) {
|
|
187
|
-
if (v.length < 8 || /^\$(?:\{|[A-Za-z_])/.test(v))
|
|
188
|
-
return false;
|
|
189
|
-
if (!quoted)
|
|
190
|
-
return !/^[#$]/.test(v) && /\d/.test(v) && /[A-Za-z]/.test(v) && !/[()]/.test(v);
|
|
191
|
-
if (/^#[0-9a-f]{3,8}$/i.test(v) || !/[A-Za-z0-9]/.test(v))
|
|
192
|
-
return false;
|
|
193
|
-
return !/\s/.test(v) || v.split(/\s+/).some((w) => /\d/.test(w) && /[A-Za-z]/.test(w));
|
|
194
|
-
}
|
|
195
|
-
function maskFields(text) {
|
|
196
|
-
let out = "";
|
|
197
|
-
let last = 0;
|
|
198
|
-
FIELD_NAME.lastIndex = 0;
|
|
199
|
-
for (let m = FIELD_NAME.exec(text);m; m = FIELD_NAME.exec(text)) {
|
|
200
|
-
const at = m.index + m[0].length;
|
|
201
|
-
const q = text[at];
|
|
202
|
-
let quote = "";
|
|
203
|
-
let value;
|
|
204
|
-
if (q === '"' || q === "'") {
|
|
205
|
-
const close = text.indexOf(q, at + 1);
|
|
206
|
-
if (close < 0 || close - at - 1 > MAX_QUOTED)
|
|
207
|
-
continue;
|
|
208
|
-
value = text.slice(at + 1, close);
|
|
209
|
-
if (value.includes(`
|
|
210
|
-
`))
|
|
211
|
-
continue;
|
|
212
|
-
quote = q;
|
|
213
|
-
} else {
|
|
214
|
-
value = bareAt(BARE_HEAD, text, at);
|
|
215
|
-
}
|
|
216
|
-
if (!secretValue(quote !== "", value))
|
|
217
|
-
continue;
|
|
218
|
-
if (!quote && value.length === 256)
|
|
219
|
-
value += bareAt(BARE_REST, text, at + 256);
|
|
220
|
-
out += `${text.slice(last, at)}${quote}[redacted]`;
|
|
221
|
-
last = at + quote.length + value.length;
|
|
222
|
-
FIELD_NAME.lastIndex = last;
|
|
223
|
-
}
|
|
224
|
-
return out + text.slice(last);
|
|
225
|
-
}
|
|
226
|
-
var MYSQL_COMMAND = /\bmysql(?:dump|admin)?\b(?:'[^'\n]*'|"[^"\n]*"|[^\n;&|\\'"]|\\\r?\n|\\(?!\r?\n))*/g;
|
|
227
|
-
var MYSQL_PASSWORD = /(\s-p)(?:'[^'\n]*'|"[^"\n]*"|(?=[^\s-])\S+)/;
|
|
228
|
-
var URL_CREDENTIALS = /\b((?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|rediss?|amqps?|https?):\/\/[^:\s/@]*:)[^\s/]*@([^@\s/?#]+)/g;
|
|
229
|
-
var KEY_BEGIN = /-----BEGIN [A-Z ]*PRIVATE KEY-----/g;
|
|
230
|
-
var KEY_END = /-----END [A-Z ]*PRIVATE KEY-----/g;
|
|
231
|
-
function keyBlocks(text) {
|
|
232
|
-
const ends = [...text.matchAll(KEY_END)].map((m) => [m.index, m.index + m[0].length]);
|
|
233
|
-
const out = [];
|
|
234
|
-
let last = 0;
|
|
235
|
-
let e = 0;
|
|
236
|
-
if (ends.length > 0)
|
|
237
|
-
for (const m of text.matchAll(KEY_BEGIN)) {
|
|
238
|
-
const after = m.index + m[0].length;
|
|
239
|
-
if (m.index < last)
|
|
240
|
-
continue;
|
|
241
|
-
while (e < ends.length && (ends[e]?.[0] ?? 0) < after)
|
|
242
|
-
e++;
|
|
243
|
-
const end = ends[e];
|
|
244
|
-
if (!end)
|
|
245
|
-
break;
|
|
246
|
-
out.push([m.index, end[1]]);
|
|
247
|
-
last = end[1];
|
|
248
|
-
}
|
|
249
|
-
return out;
|
|
250
|
-
}
|
|
251
|
-
function maskPrivateKeys(text) {
|
|
252
|
-
let out = "";
|
|
253
|
-
let last = 0;
|
|
254
|
-
for (const [a, b] of keyBlocks(text)) {
|
|
255
|
-
out += `${text.slice(last, a)}[redacted: private key]`;
|
|
256
|
-
last = b;
|
|
257
|
-
}
|
|
258
|
-
return out + text.slice(last);
|
|
259
|
-
}
|
|
260
|
-
function mask(text) {
|
|
261
|
-
let out = maskFields(maskPrivateKeys(text).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]"));
|
|
262
|
-
for (const [re, what] of SECRETS)
|
|
263
|
-
out = out.replace(re, `[redacted: ${what}]`);
|
|
264
|
-
return out;
|
|
265
|
-
}
|
|
266
|
-
var reason = (e) => explain(e, 0) || "unknown failure";
|
|
267
|
-
var plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
|
|
268
|
-
function explain(e, depth) {
|
|
269
|
-
if (!(e instanceof Error)) {
|
|
270
|
-
try {
|
|
271
|
-
return String(e);
|
|
272
|
-
} catch {
|
|
273
|
-
return "";
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
const own = e.message || (e.name === "Error" || e.name === "AggregateError" ? "" : e.name);
|
|
277
|
-
const parts = depth >= 3 ? [] : [...e instanceof AggregateError ? e.errors : [], ...e.cause === undefined ? [] : [e.cause]];
|
|
278
|
-
const inner = parts.map((x) => explain(x, depth + 1)).filter(Boolean).join(" / ");
|
|
279
|
-
return own && inner ? `${own} (${inner})` : own || inner;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// server/src/anchors.ts
|
|
283
|
-
var MAX_BYTES = 2 * 1024 * 1024;
|
|
284
|
-
var leaves = (rel) => rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel);
|
|
4
|
+
import fs9 from "node:fs";
|
|
5
|
+
import os3 from "node:os";
|
|
6
|
+
import path10 from "node:path";
|
|
285
7
|
|
|
286
8
|
// server/node_modules/kysely/dist/util/object-utils.js
|
|
287
9
|
function isUndefined(obj) {
|
|
@@ -4408,8 +4130,8 @@ class Deferred {
|
|
|
4408
4130
|
this.#resolve?.(value);
|
|
4409
4131
|
this.#resolve = this.#reject = undefined;
|
|
4410
4132
|
};
|
|
4411
|
-
reject = (
|
|
4412
|
-
this.#reject?.(
|
|
4133
|
+
reject = (reason) => {
|
|
4134
|
+
this.#reject?.(reason);
|
|
4413
4135
|
this.#reject = this.#resolve = undefined;
|
|
4414
4136
|
};
|
|
4415
4137
|
}
|
|
@@ -4461,9 +4183,9 @@ function assertNotAborted(signal, timing, beforeThrow) {
|
|
|
4461
4183
|
throwReasonWithTiming(signal.reason, timing);
|
|
4462
4184
|
}
|
|
4463
4185
|
}
|
|
4464
|
-
function throwReasonWithTiming(
|
|
4465
|
-
decorateWithTiming(
|
|
4466
|
-
throw
|
|
4186
|
+
function throwReasonWithTiming(reason, timing) {
|
|
4187
|
+
decorateWithTiming(reason, timing);
|
|
4188
|
+
throw reason;
|
|
4467
4189
|
}
|
|
4468
4190
|
var ABORTED = {};
|
|
4469
4191
|
async function waitOrAbort(promise, signal, name, onAbort) {
|
|
@@ -4488,11 +4210,11 @@ async function waitOrAbort(promise, signal, name, onAbort) {
|
|
|
4488
4210
|
}
|
|
4489
4211
|
}
|
|
4490
4212
|
function printBackgroundFail(name) {
|
|
4491
|
-
return (
|
|
4213
|
+
return (reason) => console.error(`\`${name}\` failed in the background after abortion: ${getMessage(reason)}`);
|
|
4492
4214
|
}
|
|
4493
|
-
function decorateWithTiming(
|
|
4494
|
-
if (
|
|
4495
|
-
Object.defineProperty(
|
|
4215
|
+
function decorateWithTiming(reason, timing) {
|
|
4216
|
+
if (reason !== null && typeof reason === "object" && !Object.isFrozen(reason)) {
|
|
4217
|
+
Object.defineProperty(reason, "__kysely_timing__", {
|
|
4496
4218
|
configurable: true,
|
|
4497
4219
|
enumerable: false,
|
|
4498
4220
|
value: timing,
|
|
@@ -8417,9 +8139,9 @@ class RuntimeDriver {
|
|
|
8417
8139
|
}
|
|
8418
8140
|
this.#initPromise ??= this.#driver.init(options).then(() => {
|
|
8419
8141
|
this.#initDone = true;
|
|
8420
|
-
}).catch((
|
|
8142
|
+
}).catch((reason) => {
|
|
8421
8143
|
this.#initPromise = undefined;
|
|
8422
|
-
throw
|
|
8144
|
+
throw reason;
|
|
8423
8145
|
});
|
|
8424
8146
|
await waitOrAbort(this.#initPromise, options?.signal, "init");
|
|
8425
8147
|
}
|
|
@@ -8480,9 +8202,9 @@ class RuntimeDriver {
|
|
|
8480
8202
|
return;
|
|
8481
8203
|
}
|
|
8482
8204
|
await waitOrAbort(this.#initPromise, options?.signal, "destroy:initPromise");
|
|
8483
|
-
this.#destroyPromise ??= this.#driver.destroy(options).catch((
|
|
8205
|
+
this.#destroyPromise ??= this.#driver.destroy(options).catch((reason) => {
|
|
8484
8206
|
this.#destroyPromise = undefined;
|
|
8485
|
-
throw
|
|
8207
|
+
throw reason;
|
|
8486
8208
|
});
|
|
8487
8209
|
await waitOrAbort(this.#destroyPromise, options?.signal, "destroy");
|
|
8488
8210
|
}
|
|
@@ -11003,64 +10725,343 @@ function parseArray(arr, jsonPath, options) {
|
|
|
11003
10725
|
}
|
|
11004
10726
|
return target;
|
|
11005
10727
|
}
|
|
11006
|
-
function parse(value, jsonPath, options) {
|
|
11007
|
-
if (isString(value)) {
|
|
11008
|
-
return parseString(value, jsonPath, options);
|
|
11009
|
-
}
|
|
11010
|
-
if (Array.isArray(value)) {
|
|
11011
|
-
return parseArray(value, jsonPath, options);
|
|
11012
|
-
}
|
|
11013
|
-
if (isPlainObject(value)) {
|
|
11014
|
-
return parseObject(value, jsonPath, options);
|
|
10728
|
+
function parse(value, jsonPath, options) {
|
|
10729
|
+
if (isString(value)) {
|
|
10730
|
+
return parseString(value, jsonPath, options);
|
|
10731
|
+
}
|
|
10732
|
+
if (Array.isArray(value)) {
|
|
10733
|
+
return parseArray(value, jsonPath, options);
|
|
10734
|
+
}
|
|
10735
|
+
if (isPlainObject(value)) {
|
|
10736
|
+
return parseObject(value, jsonPath, options);
|
|
10737
|
+
}
|
|
10738
|
+
return value;
|
|
10739
|
+
}
|
|
10740
|
+
function parseString(str, jsonPath, options) {
|
|
10741
|
+
const { shouldParse } = options;
|
|
10742
|
+
if (!shouldParse(str, jsonPath)) {
|
|
10743
|
+
return str;
|
|
10744
|
+
}
|
|
10745
|
+
try {
|
|
10746
|
+
return parse(JSON.parse(str, (key, value, ...otherArgs) => {
|
|
10747
|
+
if (key === "__proto__") {
|
|
10748
|
+
return;
|
|
10749
|
+
}
|
|
10750
|
+
if (key === "constructor" && isPlainObject(value) && Object.hasOwn(value, "prototype")) {
|
|
10751
|
+
delete value.prototype;
|
|
10752
|
+
}
|
|
10753
|
+
return options.reviver(key, value, ...otherArgs);
|
|
10754
|
+
}), jsonPath, { ...options, objectStrategy: "in-place" });
|
|
10755
|
+
} catch (error) {
|
|
10756
|
+
if (shouldParse !== maybeJson) {
|
|
10757
|
+
throw error;
|
|
10758
|
+
}
|
|
10759
|
+
console.error(error);
|
|
10760
|
+
return str;
|
|
10761
|
+
}
|
|
10762
|
+
}
|
|
10763
|
+
function maybeJson(value) {
|
|
10764
|
+
return value.startsWith("{") && value.endsWith("}") || value.startsWith("[") && value.endsWith("]");
|
|
10765
|
+
}
|
|
10766
|
+
function parseObject(obj, jsonPath, options) {
|
|
10767
|
+
const { objectStrategy } = options;
|
|
10768
|
+
const target = objectStrategy === "create" ? {} : obj;
|
|
10769
|
+
for (const key of Object.keys(obj)) {
|
|
10770
|
+
if (key === "__proto__") {
|
|
10771
|
+
continue;
|
|
10772
|
+
}
|
|
10773
|
+
const parsed = parse(obj[key], `${jsonPath}."${key}"`, options);
|
|
10774
|
+
if (key === "constructor" && isPlainObject(parsed) && Object.hasOwn(parsed, "prototype")) {
|
|
10775
|
+
delete parsed.prototype;
|
|
10776
|
+
}
|
|
10777
|
+
target[key] = parsed;
|
|
10778
|
+
}
|
|
10779
|
+
return target;
|
|
10780
|
+
}
|
|
10781
|
+
|
|
10782
|
+
// server/src/anchors.ts
|
|
10783
|
+
import path from "node:path";
|
|
10784
|
+
|
|
10785
|
+
// server/src/text.ts
|
|
10786
|
+
import crypto from "node:crypto";
|
|
10787
|
+
var segmenter = new Intl.Segmenter("ja", { granularity: "word" });
|
|
10788
|
+
var HIRAGANA_ONLY = /^[\p{Script=Hiragana}ー]+$/u;
|
|
10789
|
+
var STOP = new Set(["the", "a", "an", "of", "to", "in", "is", "and", "or", "for", "on", "it", "be"]);
|
|
10790
|
+
var IDENT = /#\d+|[a-z0-9][a-z0-9_./#-]*[a-z0-9]/g;
|
|
10791
|
+
var WORD = /[\p{L}\p{M}\p{N}_]+/gu;
|
|
10792
|
+
var JAPANESE = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}ー々]+/u;
|
|
10793
|
+
var COMPOUND = /^[A-Za-z0-9_]*(?:[A-Za-z0-9]_|_[A-Za-z0-9]|[a-z0-9][A-Z])[A-Za-z0-9_]*$/;
|
|
10794
|
+
var CUT = /_+|(?<=[a-z0-9])(?=[A-Z])/;
|
|
10795
|
+
var MAX_PARTS = 6;
|
|
10796
|
+
var NAME_PART = /^(?:[A-Za-z]+[0-9]*|[0-9]+)$/;
|
|
10797
|
+
var MAX_TERM = 100;
|
|
10798
|
+
var OKURIGANA = /^(\p{Script=Han}+)[\p{Script=Hiragana}ー]+$/u;
|
|
10799
|
+
function singular(w) {
|
|
10800
|
+
if (!/^[a-z]{4,}$/.test(w))
|
|
10801
|
+
return w;
|
|
10802
|
+
if (w.endsWith("ies"))
|
|
10803
|
+
return `${w.slice(0, -3)}y`;
|
|
10804
|
+
if (w.endsWith("ie"))
|
|
10805
|
+
return `${w.slice(0, -2)}y`;
|
|
10806
|
+
if (/(?:ss|us|x|z|ch|sh)es$/.test(w))
|
|
10807
|
+
return w.slice(0, -2);
|
|
10808
|
+
if (/(?:ch|sh|us)e$/.test(w))
|
|
10809
|
+
return w.slice(0, -1);
|
|
10810
|
+
return w.endsWith("s") && !/(?:ss|us|is)$/.test(w) ? w.slice(0, -1) : w;
|
|
10811
|
+
}
|
|
10812
|
+
function terms(text, parts = true) {
|
|
10813
|
+
const nfkc = text.normalize("NFKC");
|
|
10814
|
+
const norm = nfkc.toLowerCase();
|
|
10815
|
+
const out = [];
|
|
10816
|
+
const keep = (w) => {
|
|
10817
|
+
if (w.length > MAX_TERM || STOP.has(w) || HIRAGANA_ONLY.test(w))
|
|
10818
|
+
return;
|
|
10819
|
+
out.push(OKURIGANA.exec(w)?.[1] ?? singular(w));
|
|
10820
|
+
};
|
|
10821
|
+
for (const s of segmenter.segment(norm))
|
|
10822
|
+
if (s.isWordLike)
|
|
10823
|
+
keep(s.segment.trim());
|
|
10824
|
+
for (const m of norm.matchAll(IDENT))
|
|
10825
|
+
if (m[0].length >= 3)
|
|
10826
|
+
keep(m[0]);
|
|
10827
|
+
if (parts)
|
|
10828
|
+
for (const [word] of nfkc.matchAll(WORD))
|
|
10829
|
+
for (const w of word.split(JAPANESE)) {
|
|
10830
|
+
if (w.length > MAX_TERM || !COMPOUND.test(w))
|
|
10831
|
+
continue;
|
|
10832
|
+
const cut = w.split(CUT).filter(Boolean);
|
|
10833
|
+
if (cut.length <= MAX_PARTS && cut.every((p) => NAME_PART.test(p))) {
|
|
10834
|
+
for (const p of cut)
|
|
10835
|
+
if (p.length >= 2)
|
|
10836
|
+
keep(p.toLowerCase());
|
|
10837
|
+
}
|
|
10838
|
+
}
|
|
10839
|
+
return out.filter(Boolean);
|
|
10840
|
+
}
|
|
10841
|
+
var QUESTION = new Set([
|
|
10842
|
+
...[
|
|
10843
|
+
"why",
|
|
10844
|
+
"what",
|
|
10845
|
+
"which",
|
|
10846
|
+
"when",
|
|
10847
|
+
"where",
|
|
10848
|
+
"who",
|
|
10849
|
+
"how",
|
|
10850
|
+
"do",
|
|
10851
|
+
"does",
|
|
10852
|
+
"did",
|
|
10853
|
+
"we",
|
|
10854
|
+
"our",
|
|
10855
|
+
"us",
|
|
10856
|
+
"you",
|
|
10857
|
+
"i",
|
|
10858
|
+
"are",
|
|
10859
|
+
"was",
|
|
10860
|
+
"were"
|
|
10861
|
+
],
|
|
10862
|
+
...[
|
|
10863
|
+
"will",
|
|
10864
|
+
"would",
|
|
10865
|
+
"should",
|
|
10866
|
+
"can",
|
|
10867
|
+
"could",
|
|
10868
|
+
"there",
|
|
10869
|
+
"this",
|
|
10870
|
+
"that",
|
|
10871
|
+
"with",
|
|
10872
|
+
"from",
|
|
10873
|
+
"any",
|
|
10874
|
+
"ever",
|
|
10875
|
+
"long",
|
|
10876
|
+
"use",
|
|
10877
|
+
"used",
|
|
10878
|
+
"using"
|
|
10879
|
+
],
|
|
10880
|
+
...["add", "get", "make", "have", "has", "not", "no", "yes", "reason"],
|
|
10881
|
+
...["理由", "仕組", "何", "方", "場合", "今", "件"]
|
|
10882
|
+
]);
|
|
10883
|
+
var FOLDED_QUESTION = new Set([...QUESTION].flatMap((w) => terms(w)));
|
|
10884
|
+
var sha256 = (s) => crypto.createHash("sha256").update(s).digest();
|
|
10885
|
+
function uuidFrom(...parts) {
|
|
10886
|
+
const b = crypto.createHash("sha256").update(parts.join("\x00")).digest().subarray(0, 16);
|
|
10887
|
+
b[6] = (b[6] ?? 0) & 15 | 128;
|
|
10888
|
+
b[8] = (b[8] ?? 0) & 63 | 128;
|
|
10889
|
+
const h = b.toString("hex");
|
|
10890
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
10891
|
+
}
|
|
10892
|
+
var bytes = (s) => Buffer.byteLength(s, "utf8");
|
|
10893
|
+
function head(s, n) {
|
|
10894
|
+
if (bytes(s) <= n)
|
|
10895
|
+
return s;
|
|
10896
|
+
let out = "";
|
|
10897
|
+
let used = 0;
|
|
10898
|
+
for (const ch of s) {
|
|
10899
|
+
const b = bytes(ch);
|
|
10900
|
+
if (used + b > n)
|
|
10901
|
+
break;
|
|
10902
|
+
out += ch;
|
|
10903
|
+
used += b;
|
|
10904
|
+
}
|
|
10905
|
+
return out;
|
|
10906
|
+
}
|
|
10907
|
+
function tail(s, n) {
|
|
10908
|
+
if (bytes(s) <= n)
|
|
10909
|
+
return s;
|
|
10910
|
+
const chars = [...s];
|
|
10911
|
+
let used = 0;
|
|
10912
|
+
let i = chars.length;
|
|
10913
|
+
while (i > 0) {
|
|
10914
|
+
const b = bytes(chars[i - 1] ?? "");
|
|
10915
|
+
if (used + b > n)
|
|
10916
|
+
break;
|
|
10917
|
+
used += b;
|
|
10918
|
+
i--;
|
|
10919
|
+
}
|
|
10920
|
+
return chars.slice(i).join("");
|
|
10921
|
+
}
|
|
10922
|
+
var clean = (s) => s.replaceAll("\x00", "");
|
|
10923
|
+
var visible = (s) => s.replace(/(?!\p{Join_Control}|\p{Variation_Selector})\p{Default_Ignorable_Code_Point}/gu, "");
|
|
10924
|
+
var SECRETS = [
|
|
10925
|
+
[/\bsk-(?:proj-|ant-)?[A-Za-z0-9_-]{20,}/g, "API key"],
|
|
10926
|
+
[/\b[srp]k_(?:live|test)_[A-Za-z0-9]{16,}/g, "API key"],
|
|
10927
|
+
[/\bwhsec_[A-Za-z0-9+/=]{16,}/g, "webhook signing secret"],
|
|
10928
|
+
[/\bpa-[A-Za-z0-9_-]{20,}/g, "API key"],
|
|
10929
|
+
[/\bAIza[0-9A-Za-z_-]{35}/g, "API key"],
|
|
10930
|
+
[/\bnpg_[A-Za-z0-9]{12,}/g, "database password"],
|
|
10931
|
+
[/\bnapi_[A-Za-z0-9]{30,}/g, "API key"],
|
|
10932
|
+
[/\bnpm_[A-Za-z0-9]{36}\b/g, "npm token"],
|
|
10933
|
+
[/\bglpat-[A-Za-z0-9_-]{20,}/g, "GitLab token"],
|
|
10934
|
+
[
|
|
10935
|
+
/(?<![A-Za-z0-9])ghs_eyJ[A-Za-z0-9_-]{5,200}\.[A-Za-z0-9_-]{8,2000}\.[A-Za-z0-9_-]{8,1000}/g,
|
|
10936
|
+
"GitHub token"
|
|
10937
|
+
],
|
|
10938
|
+
[/(?<![A-Za-z0-9])gh[pousr]_[A-Za-z0-9]{30,}/g, "GitHub token"],
|
|
10939
|
+
[/(?<![A-Za-z0-9])github_pat_[A-Za-z0-9_]{40,}/g, "GitHub token"],
|
|
10940
|
+
[/\bxox[abprs]-[A-Za-z0-9-]{10,}/g, "Slack token"],
|
|
10941
|
+
[/https:\/\/hooks\.slack\.com\/services\/[A-Za-z0-9/]+/g, "Slack webhook"],
|
|
10942
|
+
[/\bAKIA[0-9A-Z]{16}\b/g, "AWS key"],
|
|
10943
|
+
[/(?<![A-Za-z0-9_-])eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, "JWT"],
|
|
10944
|
+
[/\b(?:Bearer|BEARER)\s+(?=[A-Za-z0-9._~+/=-]{0,512}\d)[A-Za-z0-9._~+/=-]{16,}/g, "auth header value"]
|
|
10945
|
+
];
|
|
10946
|
+
var AUTH_HEADER = /(\bAuthorization["']?\s*[:=]\s*(?:["']\s*)?(?:Bearer|Basic|Token|Digest)\s+)[A-Za-z0-9._~+/=-]{8,}/gi;
|
|
10947
|
+
var HEADER_BEARER = /(:[ \t]*(?:["'][ \t]*)?bearer[ \t]+)[A-Za-z0-9._~+/=-]{16,}/gi;
|
|
10948
|
+
var ENV_ASSIGN = /\b((?:[A-Z][A-Z0-9_]*_)?(?:API|SECRET|MASTER|ENCRYPTION|PRIVATE|ACCESS|SIGNING|AUTH)?KEY|[A-Z][A-Z0-9_]*_(?:PASS|PWD)|(?:[A-Z][A-Z0-9_]*?)?(?:TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIALS?))(\s*=\s*)(?:"(?!\$)[^"\n]+"|'(?!\$)[^'\n]+'|(?![$"'])[^\s"']+)/g;
|
|
10949
|
+
var FIELD_NAME = /(?:(?:api|account|access|private|secret)[-_]?key|secret|token|passw(?:or)?d)["']?\s*(?::=|=>|[:=])\s*/gi;
|
|
10950
|
+
var MAX_QUOTED = 4096;
|
|
10951
|
+
var BARE_HEAD = /[^\s"',;)]{1,256}/y;
|
|
10952
|
+
var BARE_REST = /[^\s"',;)]*/y;
|
|
10953
|
+
var NEXT_PARAM = /&[A-Za-z_][\w.-]*=/;
|
|
10954
|
+
var bareAt = (re, text, at) => {
|
|
10955
|
+
re.lastIndex = at;
|
|
10956
|
+
const v = re.exec(text)?.[0] ?? "";
|
|
10957
|
+
const cut = v.search(NEXT_PARAM);
|
|
10958
|
+
return cut < 0 ? v : v.slice(0, cut);
|
|
10959
|
+
};
|
|
10960
|
+
function secretValue(quoted, v) {
|
|
10961
|
+
if (v.length < 8 || /^\$(?:\{|[A-Za-z_])/.test(v))
|
|
10962
|
+
return false;
|
|
10963
|
+
if (!quoted)
|
|
10964
|
+
return !/^[#$]/.test(v) && /\d/.test(v) && /[A-Za-z]/.test(v) && !/[()]/.test(v);
|
|
10965
|
+
if (/^#[0-9a-f]{3,8}$/i.test(v) || !/[A-Za-z0-9]/.test(v))
|
|
10966
|
+
return false;
|
|
10967
|
+
return !/\s/.test(v) || v.split(/\s+/).some((w) => /\d/.test(w) && /[A-Za-z]/.test(w));
|
|
10968
|
+
}
|
|
10969
|
+
function maskFields(text) {
|
|
10970
|
+
let out = "";
|
|
10971
|
+
let last = 0;
|
|
10972
|
+
FIELD_NAME.lastIndex = 0;
|
|
10973
|
+
for (let m = FIELD_NAME.exec(text);m; m = FIELD_NAME.exec(text)) {
|
|
10974
|
+
const at = m.index + m[0].length;
|
|
10975
|
+
const q = text[at];
|
|
10976
|
+
let quote = "";
|
|
10977
|
+
let value;
|
|
10978
|
+
if (q === '"' || q === "'") {
|
|
10979
|
+
const close = text.indexOf(q, at + 1);
|
|
10980
|
+
if (close < 0 || close - at - 1 > MAX_QUOTED)
|
|
10981
|
+
continue;
|
|
10982
|
+
value = text.slice(at + 1, close);
|
|
10983
|
+
if (value.includes(`
|
|
10984
|
+
`))
|
|
10985
|
+
continue;
|
|
10986
|
+
quote = q;
|
|
10987
|
+
} else {
|
|
10988
|
+
value = bareAt(BARE_HEAD, text, at);
|
|
10989
|
+
}
|
|
10990
|
+
if (!secretValue(quote !== "", value))
|
|
10991
|
+
continue;
|
|
10992
|
+
if (!quote && value.length === 256)
|
|
10993
|
+
value += bareAt(BARE_REST, text, at + 256);
|
|
10994
|
+
out += `${text.slice(last, at)}${quote}[redacted]`;
|
|
10995
|
+
last = at + quote.length + value.length;
|
|
10996
|
+
FIELD_NAME.lastIndex = last;
|
|
11015
10997
|
}
|
|
11016
|
-
return
|
|
10998
|
+
return out + text.slice(last);
|
|
11017
10999
|
}
|
|
11018
|
-
|
|
11019
|
-
|
|
11020
|
-
|
|
11021
|
-
|
|
11022
|
-
|
|
11023
|
-
|
|
11024
|
-
|
|
11025
|
-
|
|
11026
|
-
|
|
11027
|
-
|
|
11028
|
-
|
|
11029
|
-
|
|
11030
|
-
|
|
11031
|
-
|
|
11032
|
-
|
|
11033
|
-
|
|
11034
|
-
|
|
11035
|
-
|
|
11000
|
+
var MYSQL_COMMAND = /\bmysql(?:dump|admin)?\b(?:'[^'\n]*'|"[^"\n]*"|[^\n;&|\\'"]|\\\r?\n|\\(?!\r?\n))*/g;
|
|
11001
|
+
var MYSQL_PASSWORD = /(\s-p)(?:'[^'\n]*'|"[^"\n]*"|(?=[^\s-])\S+)/;
|
|
11002
|
+
var URL_CREDENTIALS = /\b((?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|rediss?|amqps?|https?):\/\/[^:\s/@]*:)[^\s/]*@([^@\s/?#]+)/g;
|
|
11003
|
+
var KEY_BEGIN = /-----BEGIN [A-Z ]*PRIVATE KEY-----/g;
|
|
11004
|
+
var KEY_END = /-----END [A-Z ]*PRIVATE KEY-----/g;
|
|
11005
|
+
function keyBlocks(text) {
|
|
11006
|
+
const ends = [...text.matchAll(KEY_END)].map((m) => [m.index, m.index + m[0].length]);
|
|
11007
|
+
const out = [];
|
|
11008
|
+
let last = 0;
|
|
11009
|
+
let e = 0;
|
|
11010
|
+
if (ends.length > 0)
|
|
11011
|
+
for (const m of text.matchAll(KEY_BEGIN)) {
|
|
11012
|
+
const after = m.index + m[0].length;
|
|
11013
|
+
if (m.index < last)
|
|
11014
|
+
continue;
|
|
11015
|
+
while (e < ends.length && (ends[e]?.[0] ?? 0) < after)
|
|
11016
|
+
e++;
|
|
11017
|
+
const end = ends[e];
|
|
11018
|
+
if (!end)
|
|
11019
|
+
break;
|
|
11020
|
+
out.push([m.index, end[1]]);
|
|
11021
|
+
last = end[1];
|
|
11036
11022
|
}
|
|
11037
|
-
|
|
11038
|
-
|
|
11023
|
+
return out;
|
|
11024
|
+
}
|
|
11025
|
+
function maskPrivateKeys(text) {
|
|
11026
|
+
let out = "";
|
|
11027
|
+
let last = 0;
|
|
11028
|
+
for (const [a, b] of keyBlocks(text)) {
|
|
11029
|
+
out += `${text.slice(last, a)}[redacted: private key]`;
|
|
11030
|
+
last = b;
|
|
11039
11031
|
}
|
|
11032
|
+
return out + text.slice(last);
|
|
11040
11033
|
}
|
|
11041
|
-
function
|
|
11042
|
-
|
|
11034
|
+
function mask(text) {
|
|
11035
|
+
let out = maskFields(maskPrivateKeys(text).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]"));
|
|
11036
|
+
for (const [re, what] of SECRETS)
|
|
11037
|
+
out = out.replace(re, `[redacted: ${what}]`);
|
|
11038
|
+
return out;
|
|
11043
11039
|
}
|
|
11044
|
-
|
|
11045
|
-
|
|
11046
|
-
|
|
11047
|
-
|
|
11048
|
-
|
|
11049
|
-
|
|
11050
|
-
}
|
|
11051
|
-
|
|
11052
|
-
if (key === "constructor" && isPlainObject(parsed) && Object.hasOwn(parsed, "prototype")) {
|
|
11053
|
-
delete parsed.prototype;
|
|
11040
|
+
var reason = (e) => explain(e, 0) || "unknown failure";
|
|
11041
|
+
var plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
|
|
11042
|
+
function explain(e, depth) {
|
|
11043
|
+
if (!(e instanceof Error)) {
|
|
11044
|
+
try {
|
|
11045
|
+
return String(e);
|
|
11046
|
+
} catch {
|
|
11047
|
+
return "";
|
|
11054
11048
|
}
|
|
11055
|
-
target[key] = parsed;
|
|
11056
11049
|
}
|
|
11057
|
-
|
|
11050
|
+
const own = e.message || (e.name === "Error" || e.name === "AggregateError" ? "" : e.name);
|
|
11051
|
+
const parts = depth >= 3 ? [] : [...e instanceof AggregateError ? e.errors : [], ...e.cause === undefined ? [] : [e.cause]];
|
|
11052
|
+
const inner = parts.map((x) => explain(x, depth + 1)).filter(Boolean).join(" / ");
|
|
11053
|
+
return own && inner ? `${own} (${inner})` : own || inner;
|
|
11058
11054
|
}
|
|
11059
11055
|
|
|
11056
|
+
// server/src/anchors.ts
|
|
11057
|
+
var MAX_BYTES = 2 * 1024 * 1024;
|
|
11058
|
+
var leaves = (rel) => rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel);
|
|
11059
|
+
|
|
11060
11060
|
// server/src/authority.ts
|
|
11061
11061
|
var AI_DECIDED = "A record marked decided by an AI was decided by an AI in an earlier session, not by the owner (unmarked delivered decisions are the owner's): with a concrete reason you may depart from it, saying in your reply which record and why. It never relaxes the owner's rules, public contracts, or approval gates.";
|
|
11062
|
-
var ownerAdopted = (unit) => sql`exists (select 1 from unit_adoption a where a.unit_id = ${sql.ref(unit)}
|
|
11063
|
-
and a.route in ('owner_statement', 'explicit') and a.retracted_at is null)
|
|
11062
|
+
var ownerAdopted = (unit, asOf) => asOf === undefined ? sql`exists (select 1 from unit_adoption a where a.unit_id = ${sql.ref(unit)}
|
|
11063
|
+
and a.route in ('owner_statement', 'explicit') and a.retracted_at is null)` : sql`exists (select 1 from unit_adoption a where a.unit_id = ${sql.ref(unit)}
|
|
11064
|
+
and a.route in ('owner_statement', 'explicit') and a.added_at <= ${asOf} and (a.retracted_at is null or a.retracted_at > ${asOf}))`;
|
|
11064
11065
|
async function authorityOf(db, ids, asOf) {
|
|
11065
11066
|
const out = new Map(ids.map((id) => [id, "none"]));
|
|
11066
11067
|
if (!ids.length)
|
|
@@ -13198,6 +13199,225 @@ async function localChange(root, args) {
|
|
|
13198
13199
|
return { base, files, digest: sha256(read.join("\x00")).toString("hex") };
|
|
13199
13200
|
}
|
|
13200
13201
|
|
|
13202
|
+
// server/src/shell-state.ts
|
|
13203
|
+
import { createHash } from "node:crypto";
|
|
13204
|
+
import fs8 from "node:fs";
|
|
13205
|
+
import path9 from "node:path";
|
|
13206
|
+
var READS = 3;
|
|
13207
|
+
var SNAPSHOT_LIFE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
13208
|
+
var dir = (...parts) => path9.join(sphicaHome(), "shell-state", ...parts);
|
|
13209
|
+
var sha = (s) => createHash("sha256").update(s).digest("hex");
|
|
13210
|
+
var signature = (st) => ({
|
|
13211
|
+
dev: String(st.dev),
|
|
13212
|
+
ino: String(st.ino),
|
|
13213
|
+
size: String(st.size),
|
|
13214
|
+
mtimeNs: String(st.mtimeNs),
|
|
13215
|
+
ctimeNs: String(st.ctimeNs)
|
|
13216
|
+
});
|
|
13217
|
+
var same = (a, b) => a.dev === b.dev && a.ino === b.ino && a.size === b.size && a.mtimeNs === b.mtimeNs && a.ctimeNs === b.ctimeNs;
|
|
13218
|
+
function inside(root, rel) {
|
|
13219
|
+
const real = fs8.realpathSync.native(root);
|
|
13220
|
+
const file = path9.resolve(root, rel);
|
|
13221
|
+
if (leaves2(path9.relative(root, file)))
|
|
13222
|
+
return null;
|
|
13223
|
+
let probe = file;
|
|
13224
|
+
while (!fs8.existsSync(probe)) {
|
|
13225
|
+
const up = path9.dirname(probe);
|
|
13226
|
+
if (up === probe)
|
|
13227
|
+
return null;
|
|
13228
|
+
probe = up;
|
|
13229
|
+
}
|
|
13230
|
+
return leaves2(path9.relative(real, fs8.realpathSync.native(probe))) ? null : file;
|
|
13231
|
+
}
|
|
13232
|
+
var leaves2 = (rel) => rel === ".." || rel.startsWith(`..${path9.sep}`) || path9.isAbsolute(rel);
|
|
13233
|
+
var cacheFile = (root) => dir("cache", `${sha(fs8.realpathSync.native(root))}.json`);
|
|
13234
|
+
function loadCache(root) {
|
|
13235
|
+
try {
|
|
13236
|
+
const raw = JSON.parse(fs8.readFileSync(cacheFile(root), "utf8"));
|
|
13237
|
+
const out = new Map;
|
|
13238
|
+
if (raw && typeof raw === "object" && !Array.isArray(raw))
|
|
13239
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
13240
|
+
const e = v;
|
|
13241
|
+
if (e?.sig && typeof e.hash === "string" && /^[0-9a-f]{64}$/.test(e.hash) && validSig(e.sig))
|
|
13242
|
+
out.set(k, { sig: e.sig, hash: e.hash });
|
|
13243
|
+
}
|
|
13244
|
+
return out;
|
|
13245
|
+
} catch {
|
|
13246
|
+
return new Map;
|
|
13247
|
+
}
|
|
13248
|
+
}
|
|
13249
|
+
var validSig = (s) => !!s && typeof s === "object" && ["dev", "ino", "size"].every((k) => /^\d+$/.test(String(s[k]))) && ["mtimeNs", "ctimeNs"].every((k) => /^-?\d+$/.test(String(s[k])));
|
|
13250
|
+
function publish(file, text2) {
|
|
13251
|
+
fs8.mkdirSync(path9.dirname(file), { recursive: true });
|
|
13252
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
13253
|
+
fs8.writeFileSync(tmp, text2);
|
|
13254
|
+
fs8.renameSync(tmp, file);
|
|
13255
|
+
}
|
|
13256
|
+
function saveCache(root, cache, watched) {
|
|
13257
|
+
try {
|
|
13258
|
+
const kept = watched.flatMap((rel) => {
|
|
13259
|
+
const e = cache.get(rel);
|
|
13260
|
+
return e ? [[rel, e]] : [];
|
|
13261
|
+
});
|
|
13262
|
+
publish(cacheFile(root), JSON.stringify(Object.fromEntries(kept)));
|
|
13263
|
+
} catch {}
|
|
13264
|
+
}
|
|
13265
|
+
function readHash(fd, deadline) {
|
|
13266
|
+
const h = createHash("sha256");
|
|
13267
|
+
const buf = Buffer.allocUnsafe(1 << 20);
|
|
13268
|
+
for (let n = fs8.readSync(fd, buf, 0, buf.length, null);n > 0; n = fs8.readSync(fd, buf, 0, buf.length, null)) {
|
|
13269
|
+
h.update(buf.subarray(0, n));
|
|
13270
|
+
if (Date.now() >= deadline)
|
|
13271
|
+
return "deadline";
|
|
13272
|
+
}
|
|
13273
|
+
return h.digest("hex");
|
|
13274
|
+
}
|
|
13275
|
+
var failed = (e) => {
|
|
13276
|
+
const code = e.code;
|
|
13277
|
+
return code === "ENOENT" || code === "ENOTDIR" ? { kind: "missing" } : { kind: "unreadable", reason: String(code) };
|
|
13278
|
+
};
|
|
13279
|
+
function readOpened(root, file, fd, sig, deadline) {
|
|
13280
|
+
const opened = fs8.fstatSync(fd, { bigint: true });
|
|
13281
|
+
if (!same(sig, signature(opened)))
|
|
13282
|
+
return "again";
|
|
13283
|
+
const real = fs8.realpathSync.native(file);
|
|
13284
|
+
if (leaves2(path9.relative(fs8.realpathSync.native(root), real)))
|
|
13285
|
+
return { kind: "unreadable", reason: "outside the checkout" };
|
|
13286
|
+
const there = fs8.lstatSync(real, { bigint: true });
|
|
13287
|
+
if (there.dev !== opened.dev || there.ino !== opened.ino)
|
|
13288
|
+
return "again";
|
|
13289
|
+
const hash = readHash(fd, deadline);
|
|
13290
|
+
if (hash === "deadline")
|
|
13291
|
+
return { kind: "unknown", reason: "deadline" };
|
|
13292
|
+
if (!same(sig, signature(fs8.fstatSync(fd, { bigint: true }))))
|
|
13293
|
+
return "again";
|
|
13294
|
+
if (!same(sig, signature(fs8.lstatSync(file, { bigint: true }))))
|
|
13295
|
+
return "again";
|
|
13296
|
+
return { kind: "ok", sig, hash };
|
|
13297
|
+
}
|
|
13298
|
+
function stateOf(root, rel, cache, deadline) {
|
|
13299
|
+
for (let i = 0;i < READS; i++) {
|
|
13300
|
+
if (Date.now() >= deadline)
|
|
13301
|
+
return { kind: "unknown", reason: "deadline" };
|
|
13302
|
+
let file;
|
|
13303
|
+
let before;
|
|
13304
|
+
try {
|
|
13305
|
+
file = inside(root, rel);
|
|
13306
|
+
if (!file)
|
|
13307
|
+
return { kind: "unreadable", reason: "outside the checkout" };
|
|
13308
|
+
before = fs8.lstatSync(file, { bigint: true });
|
|
13309
|
+
if (before.isSymbolicLink()) {
|
|
13310
|
+
file = fs8.realpathSync.native(file);
|
|
13311
|
+
if (leaves2(path9.relative(fs8.realpathSync.native(root), file)))
|
|
13312
|
+
return { kind: "unreadable", reason: "outside the checkout" };
|
|
13313
|
+
before = fs8.lstatSync(file, { bigint: true });
|
|
13314
|
+
}
|
|
13315
|
+
} catch (e) {
|
|
13316
|
+
return failed(e);
|
|
13317
|
+
}
|
|
13318
|
+
if (!before.isFile())
|
|
13319
|
+
return { kind: "unreadable", reason: "not a regular file" };
|
|
13320
|
+
const sig = signature(before);
|
|
13321
|
+
const cached = cache.get(rel);
|
|
13322
|
+
if (cached && same(cached.sig, sig))
|
|
13323
|
+
return { kind: "ok", sig, hash: cached.hash };
|
|
13324
|
+
let fd;
|
|
13325
|
+
try {
|
|
13326
|
+
fd = fs8.openSync(file, "r");
|
|
13327
|
+
} catch (e) {
|
|
13328
|
+
if (e.code === "ENOENT")
|
|
13329
|
+
continue;
|
|
13330
|
+
return failed(e);
|
|
13331
|
+
}
|
|
13332
|
+
let outcome;
|
|
13333
|
+
try {
|
|
13334
|
+
outcome = readOpened(root, file, fd, sig, deadline);
|
|
13335
|
+
} catch (e) {
|
|
13336
|
+
outcome = e.code === "ENOENT" ? "again" : failed(e);
|
|
13337
|
+
} finally {
|
|
13338
|
+
fs8.closeSync(fd);
|
|
13339
|
+
}
|
|
13340
|
+
if (outcome === "again")
|
|
13341
|
+
continue;
|
|
13342
|
+
if (outcome.kind === "ok")
|
|
13343
|
+
cache.set(rel, { sig, hash: outcome.hash });
|
|
13344
|
+
return outcome;
|
|
13345
|
+
}
|
|
13346
|
+
return { kind: "unknown", reason: "changed while read" };
|
|
13347
|
+
}
|
|
13348
|
+
function takeStates(root, rels, cache, deadline) {
|
|
13349
|
+
const out = Object.create(null);
|
|
13350
|
+
for (const rel of rels)
|
|
13351
|
+
out[rel] = stateOf(root, rel, cache, deadline);
|
|
13352
|
+
return out;
|
|
13353
|
+
}
|
|
13354
|
+
function compare(before, after) {
|
|
13355
|
+
const changed2 = [];
|
|
13356
|
+
const unknown = [];
|
|
13357
|
+
for (const rel of new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
13358
|
+
const a = before[rel];
|
|
13359
|
+
const b = after[rel];
|
|
13360
|
+
if (!a || !b || a.kind === "unknown" || a.kind === "unreadable" || b.kind === "unknown" || b.kind === "unreadable") {
|
|
13361
|
+
unknown.push(rel);
|
|
13362
|
+
continue;
|
|
13363
|
+
}
|
|
13364
|
+
if (a.kind === "missing" && b.kind === "missing")
|
|
13365
|
+
continue;
|
|
13366
|
+
if (a.kind !== b.kind || a.kind === "ok" && b.kind === "ok" && a.hash !== b.hash)
|
|
13367
|
+
changed2.push(rel);
|
|
13368
|
+
}
|
|
13369
|
+
return { changed: changed2.sort(), unknown: unknown.sort() };
|
|
13370
|
+
}
|
|
13371
|
+
var snapshotKey = (host, session, agent, call) => sha(JSON.stringify([host, session, agent ?? "", call]));
|
|
13372
|
+
var snapshotFile = (key) => dir("calls", `${key}.json`);
|
|
13373
|
+
function writeSnapshot(s) {
|
|
13374
|
+
publish(snapshotFile(s.key), JSON.stringify(s));
|
|
13375
|
+
}
|
|
13376
|
+
function takeSnapshot(key) {
|
|
13377
|
+
const file = snapshotFile(key);
|
|
13378
|
+
let raw;
|
|
13379
|
+
try {
|
|
13380
|
+
raw = JSON.parse(fs8.readFileSync(file, "utf8"));
|
|
13381
|
+
} catch {
|
|
13382
|
+
return null;
|
|
13383
|
+
}
|
|
13384
|
+
fs8.rmSync(file, { force: true });
|
|
13385
|
+
const s = raw;
|
|
13386
|
+
if (s?.v !== 1 || s.key !== key || typeof s.root !== "string" || typeof s.project !== "string" || typeof s.at !== "string" || !s.paths || typeof s.paths !== "object" || Array.isArray(s.paths) || Number.isNaN(Date.parse(s.at)))
|
|
13387
|
+
return null;
|
|
13388
|
+
if (Date.now() - Date.parse(s.at) > SNAPSHOT_LIFE_MS)
|
|
13389
|
+
return "expired";
|
|
13390
|
+
for (const v of Object.values(s.paths)) {
|
|
13391
|
+
const st = v;
|
|
13392
|
+
if (!st || !["ok", "missing", "unreadable", "unknown"].includes(st.kind))
|
|
13393
|
+
return null;
|
|
13394
|
+
if (st.kind === "ok" && (!validSig(st.sig) || !/^[0-9a-f]{64}$/.test(st.hash)))
|
|
13395
|
+
return null;
|
|
13396
|
+
}
|
|
13397
|
+
return s;
|
|
13398
|
+
}
|
|
13399
|
+
function pruneSnapshots(now = Date.now(), deadline = Number.POSITIVE_INFINITY) {
|
|
13400
|
+
let removed = 0;
|
|
13401
|
+
let names;
|
|
13402
|
+
try {
|
|
13403
|
+
names = fs8.readdirSync(dir("calls"));
|
|
13404
|
+
} catch {
|
|
13405
|
+
return 0;
|
|
13406
|
+
}
|
|
13407
|
+
for (const n of names) {
|
|
13408
|
+
if (Date.now() >= deadline)
|
|
13409
|
+
break;
|
|
13410
|
+
const file = dir("calls", n);
|
|
13411
|
+
try {
|
|
13412
|
+
if (now - fs8.statSync(file).mtimeMs > SNAPSHOT_LIFE_MS) {
|
|
13413
|
+
fs8.rmSync(file, { force: true });
|
|
13414
|
+
removed++;
|
|
13415
|
+
}
|
|
13416
|
+
} catch {}
|
|
13417
|
+
}
|
|
13418
|
+
return removed;
|
|
13419
|
+
}
|
|
13420
|
+
|
|
13201
13421
|
// server/src/trace.ts
|
|
13202
13422
|
var PENDING_DAYS = 14;
|
|
13203
13423
|
var pendingCutoff = (now) => new Date(now.getTime() - PENDING_DAYS * 86400000).toISOString();
|
|
@@ -13241,6 +13461,7 @@ async function pendingCount(db, projectId2, now = new Date, o = {}) {
|
|
|
13241
13461
|
|
|
13242
13462
|
// server/src/deliver.ts
|
|
13243
13463
|
var CONFIRM = "If, after checking a record below against the current code and its full text (Sphica's read), what you were asked to do is a change it rejected or rules out, do not make that change yet: tell the user which record and reason it conflicts with, and ask whether to go ahead.";
|
|
13464
|
+
var AFTER_CHANGE = "If, after checking a record below against the current code and its full text (Sphica's read), what this call changed is a change it rejected or rules out, tell the user which record and reason it conflicts with, and ask whether to keep or undo it.";
|
|
13244
13465
|
var CONFIRM_GOLD = CONFIRM.replace("its full text (Sphica's read)", "the record text given here");
|
|
13245
13466
|
var ASK = CONFIRM.length + 1;
|
|
13246
13467
|
var AI_ROOM = AI_DECIDED.length + 1;
|
|
@@ -13265,16 +13486,24 @@ var noted = (text2, lead, notes) => {
|
|
|
13265
13486
|
${l}`).join("");
|
|
13266
13487
|
return { text: note ? `${text2 || lead}${note}` : text2, note };
|
|
13267
13488
|
};
|
|
13268
|
-
var deliverable = (db, projectId2) => db.selectFrom("unit as u").where("u.project_id", "=", projectId2)
|
|
13489
|
+
var deliverable = (db, projectId2, asOf) => db.selectFrom("unit as u").where("u.project_id", "=", projectId2).$call((q) => asOf === undefined ? q.where("u.lifecycle", "=", "active") : q.where(sql`(select s.to_state from unit_state s where s.unit_id = u.id and s.at <= ${asOf}
|
|
13490
|
+
order by s.at desc, s.id desc limit 1) = 'active'`)).where("u.extraction", "=", "supported").where("u.unsourced", "=", 0).where(({ not, exists, selectFrom }) => not(exists(selectFrom("unit_link as l").select("l.from_unit").where("l.kind", "=", "conflicts").$call((q) => asOf === undefined ? q.where("l.resolved_at", "is", null) : q.where("l.added_at", "<=", asOf).where((eb) => eb.or([eb("l.resolved_at", "is", null), eb("l.resolved_at", ">", asOf)]))).where((eb) => eb.or([
|
|
13269
13491
|
eb.and([
|
|
13270
13492
|
eb("l.from_unit", "=", eb.ref("u.id")),
|
|
13271
|
-
eb.or([eb.not(ownerAdopted("u.id")), ownerAdopted("l.to_unit")])
|
|
13493
|
+
eb.or([eb.not(ownerAdopted("u.id", asOf)), ownerAdopted("l.to_unit", asOf)])
|
|
13272
13494
|
]),
|
|
13273
13495
|
eb.and([
|
|
13274
13496
|
eb("l.to_unit", "=", eb.ref("u.id")),
|
|
13275
|
-
eb.or([eb.not(ownerAdopted("u.id")), ownerAdopted("l.from_unit")])
|
|
13497
|
+
eb.or([eb.not(ownerAdopted("u.id", asOf)), ownerAdopted("l.from_unit", asOf)])
|
|
13276
13498
|
])
|
|
13277
13499
|
])))));
|
|
13500
|
+
async function deliverablePaths(db, projectId2) {
|
|
13501
|
+
const rows = await deliverable(db, projectId2).innerJoin("unit_anchor as a", "a.unit_id", "u.id").where("a.role", "=", "applies_to").where("a.retired_at", "is", null).where("u.kind", "in", ["decision", "constraint"]).select("a.path").distinct().orderBy("a.path").execute();
|
|
13502
|
+
return rows.map((r) => r.path);
|
|
13503
|
+
}
|
|
13504
|
+
async function deliverableIds(db, projectId2, asOf) {
|
|
13505
|
+
return new Set((await deliverable(db, projectId2, asOf).select("u.id").execute()).map((r) => r.id));
|
|
13506
|
+
}
|
|
13278
13507
|
var line = (u, extra = "", ai = false) => `- ${inline(u.key)} (${u.kind}${u.stance ? ` ${u.stance}` : ""}${ai ? AI_MARK : ""}): ${head(inline(u.text), 240)}${extra}`;
|
|
13279
13508
|
async function aiDecided(db, ids) {
|
|
13280
13509
|
const whose = await authorityOf(db, ids);
|
|
@@ -13348,6 +13577,11 @@ function fit2(lines, chars, lead, free = []) {
|
|
|
13348
13577
|
};
|
|
13349
13578
|
}
|
|
13350
13579
|
var anchoredTo = (db, projectId2, rels) => deliverable(db, projectId2).innerJoin("unit_anchor as a", "a.unit_id", "u.id").where("a.path", "in", rels).where("a.role", "=", "applies_to").where("a.retired_at", "is", null).select(["u.id", "u.key", "u.kind", "u.stance", "u.text"]).groupBy("u.id").orderBy("u.id", "desc");
|
|
13580
|
+
async function anchoredRules(db, projectId2, rels) {
|
|
13581
|
+
const wanted = new Set(rels);
|
|
13582
|
+
const anchors = await deliverable(db, projectId2).innerJoin("unit_anchor as a", "a.unit_id", "u.id").where("a.role", "=", "applies_to").where("a.retired_at", "is", null).where("u.kind", "in", ["decision", "constraint"]).select(["u.id", "a.path"]).execute();
|
|
13583
|
+
return [...new Set(anchors.filter((a) => wanted.has(a.path)).map((a) => a.id))].sort((a, b) => b - a);
|
|
13584
|
+
}
|
|
13351
13585
|
var named = (rels) => rels.length <= 3 ? rels.map(inline).join(", ") : `${rels.slice(0, 3).map(inline).join(", ")} and ${rels.length - 3} more`;
|
|
13352
13586
|
async function beforeEdit(db, projectId2, rels) {
|
|
13353
13587
|
const rows = await anchoredTo(db, projectId2, rels).execute();
|
|
@@ -13369,10 +13603,33 @@ var agentOf = (input) => typeof input.agent_id === "string" && /^[\x21-\x7e]{1,2
|
|
|
13369
13603
|
var sameAgent = (agent) => (eb) => agent === null ? eb("d.agent_id", "is", null) : eb("d.agent_id", "=", agent);
|
|
13370
13604
|
var START_SOURCES = new Set(["startup", "resume", "clear", "compact", "fork"]);
|
|
13371
13605
|
var RESTARTS = ["compact", "clear"];
|
|
13372
|
-
async function
|
|
13606
|
+
async function sinceRestart(db, session, agent) {
|
|
13373
13607
|
const restart = await db.selectFrom("delivery as d").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.event", "=", "session_start").where("d.reason", "in", RESTARTS).select((eb) => eb.fn.max("d.id").as("id")).executeTakeFirst();
|
|
13374
13608
|
const since = restart?.id ?? 0;
|
|
13375
13609
|
const sent = await db.selectFrom("delivery as d").innerJoin("delivery_unit as x", "x.delivery_id", "d.id").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.id", ">", since).where("d.outcome", "=", "emitted").select(["x.unit_id", "d.event"]).execute();
|
|
13610
|
+
return { since, sent };
|
|
13611
|
+
}
|
|
13612
|
+
async function afterShellWrite(db, projectId2, rels, session, agent) {
|
|
13613
|
+
const { sent } = await sinceRestart(db, session, agent);
|
|
13614
|
+
const seen = new Set(sent.map((r) => r.unit_id));
|
|
13615
|
+
const ids = (await anchoredRules(db, projectId2, rels)).filter((id) => !seen.has(id));
|
|
13616
|
+
const top2 = ids.slice(0, LIMITS2.pre_edit.units);
|
|
13617
|
+
const shown = top2.length ? await db.selectFrom("unit as u").where("u.id", "in", top2).select(["u.id", "u.key", "u.kind", "u.stance", "u.text"]).orderBy("u.id", "desc").execute() : [];
|
|
13618
|
+
const why = await reasons(db, shown.map((u) => u.id));
|
|
13619
|
+
const ai = await aiDecided(db, shown.map((u) => u.id));
|
|
13620
|
+
const f = fitMarked(shown.map((u) => why.has(u.id) ? [line(u, why.get(u.id), ai.has(u.id)), line(u, "", ai.has(u.id))] : line(u, "", ai.has(u.id))), LIMITS2.pre_edit.chars - ASK + AFTER_CHANGE.length + 1, `Active decisions applying to ${named(rels)}, files whose content changed between before and after this call (current code relevance unverified). ${AFTER_CHANGE} ${NOTE}:`, ai, shown.map((u) => u.id));
|
|
13621
|
+
const omitted = ids.length - shown.length + f.omitted;
|
|
13622
|
+
return {
|
|
13623
|
+
...noted(f.text, f.lead, [leftOut(omitted)]),
|
|
13624
|
+
units: f.kept.flatMap((i) => shown[i]?.id ?? []),
|
|
13625
|
+
eligible: ids.length,
|
|
13626
|
+
omitted,
|
|
13627
|
+
path: head(rels.join(" "), 500),
|
|
13628
|
+
reason: SHELL_WRITE
|
|
13629
|
+
};
|
|
13630
|
+
}
|
|
13631
|
+
async function beforeRead(db, projectId2, rels, session, agent, how) {
|
|
13632
|
+
const { since, sent } = await sinceRestart(db, session, agent);
|
|
13376
13633
|
const spent = await db.selectFrom("delivery as d").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.id", ">", since).where("d.event", "=", "pre_read").where("d.outcome", "=", "emitted").where(({ exists, selectFrom }) => exists(selectFrom("delivery_unit as x").select("x.unit_id").whereRef("x.delivery_id", "=", "d.id"))).select(["d.id", "d.at", "d.chars"]).execute();
|
|
13377
13634
|
const shownBy = spent.length ? await db.selectFrom("delivery_unit").select(["delivery_id", "unit_id"]).where("delivery_id", "in", spent.map((r) => r.id)).execute() : [];
|
|
13378
13635
|
const marked = new Map;
|
|
@@ -13418,19 +13675,19 @@ function shellPatch(input) {
|
|
|
13418
13675
|
return typeof c === "string" && /^\s*\*\*\* Begin Patch\s*$/m.test(c) ? c : null;
|
|
13419
13676
|
}
|
|
13420
13677
|
async function namedInCommand(db, projectId2, root, cwd, command) {
|
|
13421
|
-
const paths = await
|
|
13678
|
+
const paths = (await deliverablePaths(db, projectId2)).map((p) => ({ path: p }));
|
|
13422
13679
|
const edge = `\\s'"=(){}<>|;&,`;
|
|
13423
13680
|
const esc = (t) => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
13424
13681
|
const forms = (p) => {
|
|
13425
|
-
const abs =
|
|
13426
|
-
const fromCwd =
|
|
13682
|
+
const abs = path10.join(root, p);
|
|
13683
|
+
const fromCwd = path10.relative(cwd, abs);
|
|
13427
13684
|
const out = new Set;
|
|
13428
13685
|
for (const f of [p, abs, leaves(fromCwd) ? "" : fromCwd])
|
|
13429
13686
|
if (f)
|
|
13430
13687
|
for (const sep of ["/", "\\"]) {
|
|
13431
13688
|
const t = f.split(/[\\/]/).join(sep);
|
|
13432
13689
|
out.add(t);
|
|
13433
|
-
if (!
|
|
13690
|
+
if (!path10.isAbsolute(f))
|
|
13434
13691
|
out.add(`.${sep}${t}`);
|
|
13435
13692
|
}
|
|
13436
13693
|
return [...out];
|
|
@@ -13439,7 +13696,7 @@ async function namedInCommand(db, projectId2, root, cwd, command) {
|
|
|
13439
13696
|
}
|
|
13440
13697
|
function pathNamed(prompt, root, rel) {
|
|
13441
13698
|
const steps = (p) => p.split(/[\\/]/).map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("[\\\\/]");
|
|
13442
|
-
return new RegExp(`(?<![A-Za-z0-9_$.\\-/\\\\])(?:(?:\\.[\\\\/])?${steps(rel)}|${steps(
|
|
13699
|
+
return new RegExp(`(?<![A-Za-z0-9_$.\\-/\\\\])(?:(?:\\.[\\\\/])?${steps(rel)}|${steps(path10.join(root, rel))})(?![A-Za-z0-9_$\\-/\\\\]|\\.[A-Za-z0-9_$])`).test(prompt);
|
|
13443
13700
|
}
|
|
13444
13701
|
function once(f) {
|
|
13445
13702
|
const seen = new Map;
|
|
@@ -13449,7 +13706,7 @@ function once(f) {
|
|
|
13449
13706
|
return seen.get(k);
|
|
13450
13707
|
};
|
|
13451
13708
|
}
|
|
13452
|
-
async function
|
|
13709
|
+
async function namedRecords(db, projectId2, root, prompt, asOf) {
|
|
13453
13710
|
const text2 = prompt.normalize("NFKC");
|
|
13454
13711
|
const lower = text2.toLowerCase();
|
|
13455
13712
|
const word = (w, s) => s.includes(w) && new RegExp(`(?<![\\p{L}\\p{N}_$])${w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![\\p{L}\\p{N}_$])`, "u").test(s);
|
|
@@ -13457,21 +13714,26 @@ async function onPrompt(db, projectId2, root, prompt) {
|
|
|
13457
13714
|
const slashed = prompt.replaceAll("\\", "/");
|
|
13458
13715
|
const pathIn = once((rel) => slashed.includes(rel) && pathNamed(prompt, root, rel));
|
|
13459
13716
|
const optionIn = once((option) => option.length >= 3 && word(option.normalize("NFKC").toLowerCase(), lower));
|
|
13460
|
-
const units = await deliverable(db, projectId2).select(["u.id", "u.key", "u.kind", "u.stance", "u.text"]).orderBy("u.kind").orderBy("u.id").execute();
|
|
13461
|
-
const ids = deliverable(db, projectId2).select("u.id");
|
|
13717
|
+
const units = await deliverable(db, projectId2, asOf).select(["u.id", "u.key", "u.kind", "u.stance", "u.text"]).orderBy("u.kind").orderBy("u.id").execute();
|
|
13718
|
+
const ids = deliverable(db, projectId2, asOf).select("u.id");
|
|
13462
13719
|
const [anchors, options] = await Promise.all([
|
|
13463
|
-
db.selectFrom("unit_anchor").select(["unit_id", "path", "symbol"]).where("unit_id", "in", ids).where("retired_at", "is", null).orderBy("id").execute().then(byUnit),
|
|
13720
|
+
db.selectFrom("unit_anchor").select(["unit_id", "path", "symbol"]).where("unit_id", "in", ids).$call((q) => asOf === undefined ? q.where("retired_at", "is", null) : q.where("added_at", "<=", asOf).where((eb) => eb.or([eb("retired_at", "is", null), eb("retired_at", ">", asOf)]))).orderBy("id").execute().then(byUnit),
|
|
13464
13721
|
db.selectFrom("unit_option").select(["unit_id", "text", "outcome"]).where("unit_id", "in", ids).orderBy("id").execute().then(byUnit)
|
|
13465
13722
|
]);
|
|
13466
13723
|
const hits = [];
|
|
13467
13724
|
for (const u of units) {
|
|
13468
13725
|
const a = anchors.get(u.id)?.find((x) => x.symbol && named2(x.symbol) || pathIn(x.path));
|
|
13469
13726
|
const o = options.get(u.id)?.find((x) => optionIn(x.text));
|
|
13727
|
+
const symbol = a?.symbol && named2(a.symbol) ? a.symbol : null;
|
|
13470
13728
|
if (a)
|
|
13471
|
-
hits.push({ u, why: ` [names ${
|
|
13729
|
+
hits.push({ u, why: ` [names ${symbol ?? a.path}]`, hit: symbol ? "symbol" : "path" });
|
|
13472
13730
|
else if (o)
|
|
13473
|
-
hits.push({ u, why: ` [names the ${o.outcome} option ${inline(o.text)}]
|
|
13731
|
+
hits.push({ u, why: ` [names the ${o.outcome} option ${inline(o.text)}]`, hit: "option" });
|
|
13474
13732
|
}
|
|
13733
|
+
return hits;
|
|
13734
|
+
}
|
|
13735
|
+
async function onPrompt(db, projectId2, root, prompt) {
|
|
13736
|
+
const hits = await namedRecords(db, projectId2, root, prompt);
|
|
13475
13737
|
const shown = hits.slice(0, LIMITS2.prompt.units);
|
|
13476
13738
|
const ai = await aiDecided(db, shown.map((h) => h.u.id));
|
|
13477
13739
|
const wide = withAi(CONFIRM, LIMITS2.prompt.chars, ai);
|
|
@@ -13496,6 +13758,40 @@ async function onPrompt(db, projectId2, root, prompt) {
|
|
|
13496
13758
|
reason: null
|
|
13497
13759
|
};
|
|
13498
13760
|
}
|
|
13761
|
+
var SHELL_WRITE = "shell_write";
|
|
13762
|
+
var SNAPSHOT_MS = 3500;
|
|
13763
|
+
var SHELL_WRITE_ON = new Set(["on", "1", "true", "yes"]);
|
|
13764
|
+
var TRIAL_CAP = 10 * 1024 * 1024;
|
|
13765
|
+
function shellWriteDelivery() {
|
|
13766
|
+
const env = (process.env.SPHICA_SHELL_WRITE_DELIVERY ?? "").trim().toLowerCase();
|
|
13767
|
+
if (AUTO_TRACE_OFF.has(env))
|
|
13768
|
+
return false;
|
|
13769
|
+
if (SHELL_WRITE_ON.has(env))
|
|
13770
|
+
return true;
|
|
13771
|
+
return SHELL_WRITE_ON.has((process.env.CLAUDE_PLUGIN_OPTION_SHELL_WRITE_DELIVERY ?? "").trim().toLowerCase());
|
|
13772
|
+
}
|
|
13773
|
+
function trialLog(line2) {
|
|
13774
|
+
try {
|
|
13775
|
+
const file = path10.join(sphicaHome(), "shell-state", "trial.jsonl");
|
|
13776
|
+
fs9.mkdirSync(path10.dirname(file), { recursive: true });
|
|
13777
|
+
if ((fs9.statSync(file, { throwIfNoEntry: false })?.size ?? 0) >= TRIAL_CAP)
|
|
13778
|
+
rotateTrial(path10.dirname(file), file);
|
|
13779
|
+
fs9.appendFileSync(file, `${JSON.stringify({ at: iso(Date.now()), ...line2 })}
|
|
13780
|
+
`);
|
|
13781
|
+
} catch {}
|
|
13782
|
+
}
|
|
13783
|
+
function rotateTrial(dir2, file) {
|
|
13784
|
+
try {
|
|
13785
|
+
fs9.renameSync(file, path10.join(dir2, `trial.${Date.now()}.${process.pid}.jsonl`));
|
|
13786
|
+
const archives = fs9.readdirSync(dir2).filter((n) => /^trial\.\d+\.\d+\.jsonl$/.test(n)).sort((a, b) => Number(b.split(".")[1]) - Number(a.split(".")[1]));
|
|
13787
|
+
let kept = 0;
|
|
13788
|
+
for (const n of archives) {
|
|
13789
|
+
kept += fs9.statSync(path10.join(dir2, n), { throwIfNoEntry: false })?.size ?? 0;
|
|
13790
|
+
if (kept > 2 * TRIAL_CAP)
|
|
13791
|
+
fs9.rmSync(path10.join(dir2, n), { force: true });
|
|
13792
|
+
}
|
|
13793
|
+
} catch {}
|
|
13794
|
+
}
|
|
13499
13795
|
var AUTO_TRACE_OFF = new Set(["off", "0", "false", "no"]);
|
|
13500
13796
|
var AUTO_TRACE = (n) => `- ${n} earlier session${n === 1 ? "" : "s"} of this project ${n === 1 ? "waits" : "wait"} to be traced. After the user's request is done, run the trace Skill on your own for the oldest (at most 2), as its "On your own" section says, without asking the user.`;
|
|
13501
13797
|
async function waiting(db, projectId2, place) {
|
|
@@ -13508,7 +13804,7 @@ async function waiting(db, projectId2, place) {
|
|
|
13508
13804
|
});
|
|
13509
13805
|
if (!total)
|
|
13510
13806
|
return "";
|
|
13511
|
-
if (!markOnce("auto-trace", `${
|
|
13807
|
+
if (!markOnce("auto-trace", `${path10.resolve(place.file)}\x00${place.key}\x00${place.host}\x00${place.auto}`))
|
|
13512
13808
|
return "";
|
|
13513
13809
|
return AUTO_TRACE(total);
|
|
13514
13810
|
}
|
|
@@ -13516,7 +13812,7 @@ async function waiting(db, projectId2, place) {
|
|
|
13516
13812
|
if (!n)
|
|
13517
13813
|
return "";
|
|
13518
13814
|
const day = new Date().toLocaleDateString("sv-SE");
|
|
13519
|
-
if (!markOnce("pending", `${
|
|
13815
|
+
if (!markOnce("pending", `${path10.resolve(place.file)}\x00${place.key}\x00${day}`))
|
|
13520
13816
|
return "";
|
|
13521
13817
|
const trace = place.host === "codex" ? "$sphica:trace" : "/sphica:trace";
|
|
13522
13818
|
return `- ${n} session${n === 1 ? "" : "s"} waiting to be traced: run ${trace} pending.`;
|
|
@@ -13604,10 +13900,10 @@ function markOnce(kind, key) {
|
|
|
13604
13900
|
return String(process.getuid?.() ?? "user");
|
|
13605
13901
|
}
|
|
13606
13902
|
})();
|
|
13607
|
-
const
|
|
13903
|
+
const dir2 = path10.join(os3.tmpdir(), `sphica-${sha256(user).toString("hex").slice(0, 12)}`, kind);
|
|
13608
13904
|
try {
|
|
13609
|
-
|
|
13610
|
-
|
|
13905
|
+
fs9.mkdirSync(dir2, { recursive: true, mode: 448 });
|
|
13906
|
+
fs9.writeFileSync(path10.join(dir2, sha256(key).toString("hex").slice(0, 24)), "", { flag: "wx" });
|
|
13611
13907
|
return true;
|
|
13612
13908
|
} catch (e) {
|
|
13613
13909
|
return e.code !== "EEXIST";
|
|
@@ -13651,27 +13947,103 @@ async function log(file, e, plan) {
|
|
|
13651
13947
|
await cap.destroy().catch(() => {});
|
|
13652
13948
|
}
|
|
13653
13949
|
}
|
|
13654
|
-
async function lockedPlan(file, e, make, keep) {
|
|
13950
|
+
async function lockedPlan(file, e, make, keep, logged = () => {}) {
|
|
13655
13951
|
const cap = openWriter("capture", file, LOG_WAIT_MS);
|
|
13656
13952
|
let held = false;
|
|
13953
|
+
let wrote = false;
|
|
13657
13954
|
let plan = null;
|
|
13658
13955
|
try {
|
|
13659
13956
|
await inTransaction(cap, async (trx) => {
|
|
13660
13957
|
held = true;
|
|
13661
13958
|
plan = await make();
|
|
13662
|
-
if (keep(plan))
|
|
13959
|
+
if (keep(plan)) {
|
|
13663
13960
|
await write2(trx, e, plan);
|
|
13961
|
+
wrote = true;
|
|
13962
|
+
}
|
|
13664
13963
|
});
|
|
13665
13964
|
} catch (err) {
|
|
13965
|
+
wrote = false;
|
|
13666
13966
|
if (held && !plan)
|
|
13667
13967
|
throw err;
|
|
13668
13968
|
} finally {
|
|
13669
13969
|
await cap.destroy().catch(() => {});
|
|
13670
13970
|
}
|
|
13971
|
+
logged(wrote);
|
|
13671
13972
|
return plan ?? make();
|
|
13672
13973
|
}
|
|
13974
|
+
async function snapshotCall(db, projectId2, place, host, input, started) {
|
|
13975
|
+
const paths = await deliverablePaths(db, projectId2);
|
|
13976
|
+
const cache = loadCache(place.root);
|
|
13977
|
+
const states = takeStates(place.root, paths, cache, started + SNAPSHOT_MS);
|
|
13978
|
+
saveCache(place.root, cache, paths);
|
|
13979
|
+
const expired = pruneSnapshots(Date.now(), started + SNAPSHOT_MS);
|
|
13980
|
+
if (expired)
|
|
13981
|
+
trialLog({ host, event: "snapshot_expired", count: expired });
|
|
13982
|
+
const key = snapshotKey(host, String(input.session_id), agentOf(input), String(input.tool_use_id));
|
|
13983
|
+
writeSnapshot({ v: 1, key, at: iso(started), root: place.root, project: place.key, paths: states });
|
|
13984
|
+
}
|
|
13985
|
+
async function afterShell(input, host, file, started) {
|
|
13986
|
+
if (!shellWriteDelivery() || !input.session_id || !input.tool_use_id)
|
|
13987
|
+
return "";
|
|
13988
|
+
const agent = agentOf(input);
|
|
13989
|
+
const base = { host, session: input.session_id, agent, call: input.tool_use_id };
|
|
13990
|
+
let line2 = { ...base, event: "post_shell" };
|
|
13991
|
+
let db = null;
|
|
13992
|
+
try {
|
|
13993
|
+
const before = takeSnapshot(snapshotKey(host, input.session_id, agent, input.tool_use_id));
|
|
13994
|
+
if (before === "expired" || !before) {
|
|
13995
|
+
if (!before && !identify(input.cwd ?? process.cwd()))
|
|
13996
|
+
return "";
|
|
13997
|
+
trialLog({ ...base, event: before ? "snapshot_expired" : "snapshot_missing" });
|
|
13998
|
+
return "";
|
|
13999
|
+
}
|
|
14000
|
+
const cache = loadCache(before.root);
|
|
14001
|
+
for (const [rel, st] of Object.entries(before.paths))
|
|
14002
|
+
if (st.kind === "ok")
|
|
14003
|
+
cache.set(rel, { sig: st.sig, hash: st.hash });
|
|
14004
|
+
const after = takeStates(before.root, Object.keys(before.paths), cache, started + SNAPSHOT_MS);
|
|
14005
|
+
saveCache(before.root, cache, Object.keys(before.paths));
|
|
14006
|
+
const { changed: changed2, unknown } = compare(before.paths, after);
|
|
14007
|
+
line2 = { ...line2, paths: Object.keys(before.paths).length, changed: changed2, unknown };
|
|
14008
|
+
if (!changed2.length) {
|
|
14009
|
+
trialLog({ ...line2, delivered: [] });
|
|
14010
|
+
return "";
|
|
14011
|
+
}
|
|
14012
|
+
if (!fs9.existsSync(file))
|
|
14013
|
+
throw new Error(`no database at ${file}`);
|
|
14014
|
+
db = openReader(file);
|
|
14015
|
+
const pid = await projectId(db, before.project);
|
|
14016
|
+
if (pid === null)
|
|
14017
|
+
throw new Error("the project is not registered");
|
|
14018
|
+
const reader = db;
|
|
14019
|
+
const entry = {
|
|
14020
|
+
projectId: pid,
|
|
14021
|
+
host,
|
|
14022
|
+
external: input.session_id,
|
|
14023
|
+
agent,
|
|
14024
|
+
event: "pre_edit",
|
|
14025
|
+
branch: branchOf(before.root)
|
|
14026
|
+
};
|
|
14027
|
+
let logged = false;
|
|
14028
|
+
const plan = await lockedPlan(file, entry, () => afterShellWrite(reader, pid, changed2, sessionId(pid, host, input.session_id), agent), (p) => Boolean(p.text), (w) => {
|
|
14029
|
+
logged = w;
|
|
14030
|
+
});
|
|
14031
|
+
const keys = plan.units.length ? (await reader.selectFrom("unit").select("key").where("id", "in", plan.units).execute()).map((u) => u.key) : [];
|
|
14032
|
+
trialLog({ ...line2, delivered: keys, logged: plan.text ? logged : null });
|
|
14033
|
+
return plan.text;
|
|
14034
|
+
} catch (e) {
|
|
14035
|
+
trialLog({ ...line2, delivered: [], error: head(reason(e), 200) });
|
|
14036
|
+
return "";
|
|
14037
|
+
} finally {
|
|
14038
|
+
await db?.destroy().catch(() => {});
|
|
14039
|
+
}
|
|
14040
|
+
}
|
|
13673
14041
|
async function deliver(input, host = "claude-code", file = dbFile()) {
|
|
14042
|
+
const started = Date.now();
|
|
13674
14043
|
const name = input.hook_event_name;
|
|
14044
|
+
if ((name === "PostToolUse" || name === "PostToolUseFailure") && SHELL_TOOLS.has(input.tool_name ?? ""))
|
|
14045
|
+
return afterShell(input, host, file, started);
|
|
14046
|
+
const snap = name === "PreToolUse" && SHELL_TOOLS.has(input.tool_name ?? "") && Boolean(input.tool_use_id) && shellWriteDelivery();
|
|
13675
14047
|
const call = reviewCall(input);
|
|
13676
14048
|
const event = call ? "review" : name === "SessionStart" || name === "SubagentStart" ? "session_start" : name === "UserPromptSubmit" ? "prompt" : name === "PreToolUse" ? input.tool_name === "Read" || SHELL_TOOLS.has(input.tool_name ?? "") && !(host === "codex" && shellPatch(input)) ? "pre_read" : "pre_edit" : null;
|
|
13677
14049
|
if (!event || !input.session_id)
|
|
@@ -13685,22 +14057,33 @@ async function deliver(input, host = "claude-code", file = dbFile()) {
|
|
|
13685
14057
|
const shell = SHELL_TOOLS.has(input.tool_name ?? "") && !patch && typeof ti.command === "string" ? ti.command : null;
|
|
13686
14058
|
const targets = patch ? patchPaths2(patch) : [ti.file_path, ti.notebook_path].filter((p) => typeof p === "string").slice(0, 1);
|
|
13687
14059
|
const onPath = event === "pre_edit" || event === "pre_read";
|
|
13688
|
-
if (onPath && !shell && (!(event === "pre_read" || patch || EDIT_TOOLS.has(input.tool_name ?? "")) || !targets.length))
|
|
14060
|
+
if (onPath && !shell && !snap && (!(event === "pre_read" || patch || EDIT_TOOLS.has(input.tool_name ?? "")) || !targets.length))
|
|
13689
14061
|
return "";
|
|
13690
14062
|
const place = identify(input.cwd ?? process.cwd());
|
|
13691
14063
|
if (!place)
|
|
13692
14064
|
return "";
|
|
13693
|
-
let rels = targets.map((t) =>
|
|
13694
|
-
if (onPath && !shell && !rels.length)
|
|
14065
|
+
let rels = targets.map((t) => path10.relative(place.root, path10.resolve(input.cwd ?? place.root, t))).filter((r) => r && !leaves(r)).map((r) => r.split(path10.sep).join("/"));
|
|
14066
|
+
if (onPath && !shell && !snap && !rels.length)
|
|
13695
14067
|
return "";
|
|
13696
14068
|
let db = null;
|
|
13697
14069
|
try {
|
|
13698
|
-
if (!
|
|
14070
|
+
if (!fs9.existsSync(file))
|
|
13699
14071
|
throw new Error(`no database at ${file}`);
|
|
13700
14072
|
db = openReader(file);
|
|
13701
14073
|
const pid = await projectId(db, place.key);
|
|
13702
14074
|
if (pid === null)
|
|
13703
14075
|
return "";
|
|
14076
|
+
if (snap)
|
|
14077
|
+
await snapshotCall(db, pid, place, host, input, started).catch((e) => trialLog({
|
|
14078
|
+
host,
|
|
14079
|
+
session: input.session_id,
|
|
14080
|
+
agent: agentOf(input),
|
|
14081
|
+
call: input.tool_use_id,
|
|
14082
|
+
event: "snapshot_failed",
|
|
14083
|
+
error: head(reason(e), 200)
|
|
14084
|
+
}));
|
|
14085
|
+
if (onPath && !shell && !rels.length)
|
|
14086
|
+
return "";
|
|
13704
14087
|
if (shell) {
|
|
13705
14088
|
rels = await namedInCommand(db, pid, place.root, input.cwd ?? place.root, shell);
|
|
13706
14089
|
if (!rels.length)
|
|
@@ -13767,7 +14150,11 @@ export {
|
|
|
13767
14150
|
AUTO_TRACE,
|
|
13768
14151
|
CONFIRM,
|
|
13769
14152
|
CONFIRM_GOLD,
|
|
14153
|
+
anchoredRules,
|
|
13770
14154
|
deliver,
|
|
14155
|
+
deliverableIds,
|
|
14156
|
+
deliverablePaths,
|
|
13771
14157
|
leadFor,
|
|
14158
|
+
namedRecords,
|
|
13772
14159
|
recordLines
|
|
13773
14160
|
};
|