ceulen 0.11.0 → 0.13.0
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/CHANGELOG.md +100 -1
- package/extensions/index.ts +5 -1
- package/extensions/lib/env.test.ts +147 -0
- package/extensions/lib/env.ts +180 -0
- package/extensions/lib/order.test.ts +74 -0
- package/extensions/lib/registry.test.ts +8 -5
- package/extensions/lib/registry.ts +23 -4
- package/extensions/modules/a2a/configPanel.ts +42 -1
- package/extensions/modules/a2a/lib/config.ts +8 -3
- package/extensions/modules/a2a/test/configPanel.test.ts +7 -4
- package/extensions/modules/advisor/index.ts +6 -3
- package/extensions/modules/advisor/test/module.test.ts +31 -0
- package/extensions/modules/classifier/index.ts +55 -1
- package/extensions/modules/classifier/test/classifier.test.ts +62 -0
- package/extensions/modules/composer/test/module.test.ts +2 -2
- package/extensions/modules/config/test/renderTools.test.ts +1 -1
- package/extensions/modules/jfind/index.ts +115 -0
- package/extensions/modules/jfind/lib/cascade.ts +377 -0
- package/extensions/modules/jfind/lib/format.ts +12 -0
- package/extensions/modules/jfind/lib/judge.ts +74 -0
- package/extensions/modules/jfind/lib/lexical.ts +232 -0
- package/extensions/modules/jfind/lib/questions.ts +178 -0
- package/extensions/modules/jfind/lib/tree.ts +374 -0
- package/extensions/modules/jfind/test/cascade.test.ts +305 -0
- package/extensions/modules/notify/index.ts +33 -0
- package/extensions/modules/notify/lib/notify.ts +99 -0
- package/extensions/modules/notify/test/notify.test.ts +95 -0
- package/extensions/modules/plan/lib/plan-tools.ts +13 -0
- package/extensions/modules/repair/configPanel.ts +7 -1
- package/extensions/modules/repair/index.ts +158 -3
- package/extensions/modules/repair/lib/auto-generated.ts +89 -0
- package/extensions/modules/repair/lib/read-archive.ts +101 -0
- package/extensions/modules/repair/lib/read-sqlite.ts +185 -0
- package/extensions/modules/repair/lib/settings.ts +4 -1
- package/extensions/modules/repair/test/freshness.test.ts +41 -1
- package/extensions/modules/repair/test/guard-selector.test.ts +67 -0
- package/extensions/modules/repair/test/readers.test.ts +291 -0
- package/extensions/modules/repair/test/settings.test.ts +2 -1
- package/extensions/modules/router/configPanel.ts +4 -4
- package/extensions/modules/router/index.ts +1 -1
- package/extensions/modules/router/lib/config.ts +9 -4
- package/extensions/modules/router/lib/systemone.ts +17 -4
- package/extensions/modules/router/test/unit.test.ts +46 -1
- package/extensions/modules/rules/index.ts +13 -3
- package/extensions/modules/rules/lib/rules.ts +24 -12
- package/extensions/modules/rules/test/lib.test.ts +20 -0
- package/extensions/modules/rules/test/module.test.ts +21 -0
- package/extensions/modules/sg/index.ts +129 -0
- package/extensions/modules/sg/lib/ops.ts +144 -0
- package/extensions/modules/sg/lib/sg-cli.ts +104 -0
- package/extensions/modules/sg/test/sg.test.ts +128 -0
- package/extensions/modules/shells/index.ts +210 -0
- package/extensions/modules/shells/lib/sessions.ts +344 -0
- package/extensions/modules/shells/test/sessions.test.ts +275 -0
- package/extensions/modules/subagent/index.ts +11 -5
- package/extensions/modules/subagent/lib/herdr.ts +5 -0
- package/extensions/modules/subagent/lib/routing.ts +31 -8
- package/extensions/modules/subagent/lib/runner.ts +8 -2
- package/extensions/modules/subagent/test/routing.test.ts +45 -0
- package/extensions/modules/web/configPanel.ts +67 -8
- package/extensions/modules/web/index.ts +34 -0
- package/extensions/modules/web/lib/pdf.ts +181 -0
- package/extensions/modules/web/test/configPanel.test.ts +7 -3
- package/extensions/modules/web/test/extension.test.ts +2 -1
- package/extensions/modules/web/test/settings.test.ts +6 -0
- package/extensions/modules/web/vendor/pdfjs/LICENSE +177 -0
- package/extensions/modules/web/vendor/pdfjs/README.md +16 -0
- package/extensions/modules/web/vendor/pdfjs/pdf.min.mjs +21 -0
- package/extensions/modules/web/vendor/pdfjs/pdf.worker.mjs +21 -0
- package/package.json +3 -3
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// jfind unit tests — fake judge, fixture tree, cascade end-to-end. No
|
|
2
|
+
// network; the classifier is injected through the Judge seam.
|
|
3
|
+
|
|
4
|
+
import assert from "node:assert/strict";
|
|
5
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { after, before, describe, it } from "node:test";
|
|
9
|
+
import { clipBytes, countOccurrences, keywords, lines, takeChars } from "../lib/lexical.js";
|
|
10
|
+
import { idf, grepIndex, fileScore } from "../lib/lexical.js";
|
|
11
|
+
import {
|
|
12
|
+
eligibleFile,
|
|
13
|
+
listFiles,
|
|
14
|
+
mergeHeat,
|
|
15
|
+
plainContent,
|
|
16
|
+
rankedHeat,
|
|
17
|
+
selectWindows,
|
|
18
|
+
sketch,
|
|
19
|
+
windows,
|
|
20
|
+
} from "../lib/tree.js";
|
|
21
|
+
import { entryKey, passageKey, nameBatch, sketchBatch, passageBatch, type Judge, type Request } from "../lib/questions.js";
|
|
22
|
+
import { runCascade, type CascadeOptions } from "../lib/cascade.js";
|
|
23
|
+
|
|
24
|
+
const dir = mkdtempSync(path.join(tmpdir(), "ceulen-jfind-"));
|
|
25
|
+
after(() => rmSync(dir, { recursive: true, force: true }));
|
|
26
|
+
|
|
27
|
+
// ── primitives ──────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
describe("text primitives", () => {
|
|
30
|
+
it("lines: no phantom tail, \\r stripped", () => {
|
|
31
|
+
assert.deepEqual(lines("a\nb\r\n"), ["a", "b"]);
|
|
32
|
+
assert.deepEqual(lines(""), []);
|
|
33
|
+
});
|
|
34
|
+
it("clipBytes never splits a code point", () => {
|
|
35
|
+
const s = "aé☃".repeat(10);
|
|
36
|
+
const clipped = clipBytes(s, 5);
|
|
37
|
+
assert.ok(Buffer.byteLength(clipped) <= 5);
|
|
38
|
+
assert.ok(s.startsWith(clipped));
|
|
39
|
+
});
|
|
40
|
+
it("takeChars counts code points", () => {
|
|
41
|
+
assert.equal(takeChars("aé☃x", 2), "aé");
|
|
42
|
+
});
|
|
43
|
+
it("countOccurrences", () => {
|
|
44
|
+
assert.equal(countOccurrences("aXbXc", "X"), 2);
|
|
45
|
+
assert.equal(countOccurrences("abc", ""), 0);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("keywords", () => {
|
|
50
|
+
it("stopwords dropped, tokens stemmed, quoted phrases whole", () => {
|
|
51
|
+
const kws = keywords('parse "cron expr" into spawn-times', []);
|
|
52
|
+
assert.ok(kws.includes("cron expr"));
|
|
53
|
+
assert.ok(kws.includes("parse"));
|
|
54
|
+
assert.ok(kws.includes("spawn")); // stemmed from spawn-times token split? token is spawn, times
|
|
55
|
+
assert.ok(kws.includes("time")); // stemmed
|
|
56
|
+
assert.ok(!kws.includes("into"));
|
|
57
|
+
});
|
|
58
|
+
it("extra keywords folded, deduped, lowercased", () => {
|
|
59
|
+
const kws = keywords("parse flags", ["Parse", "CLI"]);
|
|
60
|
+
assert.deepEqual(kws.filter((k) => k === "parse").length, 1);
|
|
61
|
+
assert.ok(kws.includes("cli"));
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ── lexical prior ───────────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
describe("lexical prior", () => {
|
|
68
|
+
it("grepIndex counts per file; idf clamps; fileScore boosts path hits", async () => {
|
|
69
|
+
const a = path.join(dir, "scan-a.txt");
|
|
70
|
+
const b = path.join(dir, "scan-b.txt");
|
|
71
|
+
writeFileSync(a, "spawn x\nspawn y\nSPAWN z\nother\n");
|
|
72
|
+
writeFileSync(b, "nothing here\n");
|
|
73
|
+
const files = new Map([
|
|
74
|
+
["scan-a.txt", a],
|
|
75
|
+
["scan-b.txt", b],
|
|
76
|
+
]);
|
|
77
|
+
const index = await grepIndex(dir, ["spawn"], { files });
|
|
78
|
+
assert.equal(index.filesScanned, 2);
|
|
79
|
+
assert.deepEqual(index.perFileKw.get("scan-a.txt"), [3]);
|
|
80
|
+
assert.equal(index.perFileKw.has("scan-b.txt"), false);
|
|
81
|
+
const weights = idf(index);
|
|
82
|
+
assert.equal(weights.length, 1);
|
|
83
|
+
assert.ok(weights[0]! >= 0.5 && weights[0]! <= 6);
|
|
84
|
+
const scoreA = fileScore(index.perFileKw.get("scan-a.txt")!, weights, "scan-a.txt", ["spawn"]);
|
|
85
|
+
const scoreB = fileScore([0], weights, "scan-b.txt", ["spawn"]);
|
|
86
|
+
assert.ok(scoreA > scoreB);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// ── tree ────────────────────────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
describe("tree eligibility + walk", () => {
|
|
93
|
+
const proj = path.join(dir, "proj");
|
|
94
|
+
before(() => {
|
|
95
|
+
mkdirSync(path.join(proj, "src"), { recursive: true });
|
|
96
|
+
mkdirSync(path.join(proj, "node_modules"), { recursive: true });
|
|
97
|
+
mkdirSync(path.join(proj, ".git"), { recursive: true });
|
|
98
|
+
writeFileSync(path.join(proj, "src", "impl.ts"), "export const x = 1;\n");
|
|
99
|
+
writeFileSync(path.join(proj, "package-lock.json"), "{}");
|
|
100
|
+
writeFileSync(path.join(proj, "node_modules", "x.js"), "module.exports = 1;\n");
|
|
101
|
+
writeFileSync(path.join(proj, ".git", "config"), "[core]\n");
|
|
102
|
+
writeFileSync(path.join(proj, "secret.key"), "-----BEGIN");
|
|
103
|
+
writeFileSync(path.join(proj, ".env"), "TOP_SECRET=1\n");
|
|
104
|
+
writeFileSync(path.join(proj, ".env.example"), "FOO=\n");
|
|
105
|
+
writeFileSync(path.join(proj, "empty.ts"), "");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("eligibleFile deny-lists build noise, secrets, binaries, empty", () => {
|
|
109
|
+
assert.equal(eligibleFile("src/impl.ts", 10, false), true);
|
|
110
|
+
assert.equal(eligibleFile("package-lock.json", 10, false), false);
|
|
111
|
+
assert.equal(eligibleFile("node_modules/x.js", 10, false), false);
|
|
112
|
+
assert.equal(eligibleFile("secret.key", 10, false), false);
|
|
113
|
+
assert.equal(eligibleFile(".env", 10, false), false);
|
|
114
|
+
assert.equal(eligibleFile(".env.example", 10, false), false); // hidden dotfile: excluded unless includeHidden
|
|
115
|
+
assert.equal(eligibleFile(".env.example", 10, true), true); // template admitted when hidden included
|
|
116
|
+
assert.equal(eligibleFile("empty.ts", 0, false), false);
|
|
117
|
+
assert.equal(eligibleFile("logo.png", 10, false), false);
|
|
118
|
+
assert.equal(eligibleFile(".hidden.ts", 10, false), false);
|
|
119
|
+
assert.equal(eligibleFile(".hidden.ts", 10, true), true);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("listFiles walks the tree, skipping deny dirs; git checkout preferred", () => {
|
|
123
|
+
const files = listFiles(proj);
|
|
124
|
+
const rels = files.map((f) => f.rel);
|
|
125
|
+
assert.ok(rels.includes("src/impl.ts"));
|
|
126
|
+
assert.ok(!rels.includes(".env.example")); // hidden, walk runs includeHidden=false
|
|
127
|
+
assert.ok(!rels.some((r) => r.startsWith("node_modules") || r.startsWith(".git") || r === "package-lock.json" || r === "secret.key" || r === ".env"));
|
|
128
|
+
void proj;
|
|
129
|
+
void mkdirSync; // silence unused import in some tsconfigs
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// ── passages ────────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
describe("passages", () => {
|
|
136
|
+
const text = Array.from({ length: 30 }, (_, i) => (i === 5 ? "spawn child process" : `line ${i}`)).join("\n");
|
|
137
|
+
it("windows: byte-bounded, 1-based, keyword-scored", () => {
|
|
138
|
+
const ps = windows(text, 256, ["spawn"], [2]);
|
|
139
|
+
assert.ok(ps.length > 1);
|
|
140
|
+
assert.equal(ps[0]!.start, 1);
|
|
141
|
+
const hit = ps.find((p) => p.score > 0);
|
|
142
|
+
assert.ok(hit, "the spawn window scored");
|
|
143
|
+
assert.match(hit!.text, /L6\| spawn child process/);
|
|
144
|
+
});
|
|
145
|
+
it("selectWindows: scores first, even spread on zero scores", () => {
|
|
146
|
+
const ps = windows(text, 256, ["spawn"], [2]);
|
|
147
|
+
const sel = selectWindows(ps, 3);
|
|
148
|
+
assert.ok(sel.length <= 3);
|
|
149
|
+
const all = windows(Array.from({ length: 40 }, (_, i) => `row ${i}`).join("\n"), 128, ["zzz"], [1]);
|
|
150
|
+
const spread = selectWindows(all, 4);
|
|
151
|
+
assert.ok(spread[0]!.start < spread[1]!.start);
|
|
152
|
+
assert.ok(spread.at(-1)!.end >= 39); // reaches the end of the file
|
|
153
|
+
});
|
|
154
|
+
it("plainContent strips the L<n>| tags", () => {
|
|
155
|
+
const p = windows("alpha\nbeta\n", 4096, [], [])[0]!;
|
|
156
|
+
assert.equal(plainContent(p), "alpha\nbeta\n");
|
|
157
|
+
});
|
|
158
|
+
it("sketch: keyword-ranked verbatim lines, budget-bounded", () => {
|
|
159
|
+
const p = windows(text, 8192, ["spawn"], [2])[0]!;
|
|
160
|
+
const s = sketch(p, ["spawn"], [2], 200);
|
|
161
|
+
assert.match(s, /^6: spawn child process/m);
|
|
162
|
+
assert.ok(Buffer.byteLength(s) <= 200 + 180);
|
|
163
|
+
});
|
|
164
|
+
it("mergeHeat unions adjacent positives, keeps max p, drops below threshold", () => {
|
|
165
|
+
const heat = [
|
|
166
|
+
{ start: 1, end: 3, p: 0.9, snippet: "a" },
|
|
167
|
+
{ start: 4, end: 6, p: 0.5, snippet: "b" },
|
|
168
|
+
{ start: 10, end: 12, p: 0.15, snippet: "low" },
|
|
169
|
+
{ start: 20, end: 22, p: 0.8, snippet: "c" },
|
|
170
|
+
];
|
|
171
|
+
const merged = mergeHeat(heat, 0.2);
|
|
172
|
+
assert.equal(merged.length, 2);
|
|
173
|
+
assert.deepEqual([merged[0]!.start, merged[0]!.end], [1, 6]);
|
|
174
|
+
assert.equal(merged[0]!.p, 0.9);
|
|
175
|
+
assert.deepEqual([merged[1]!.start, merged[1]!.end], [20, 22]);
|
|
176
|
+
});
|
|
177
|
+
it("rankedHeat: strongest first, limited", () => {
|
|
178
|
+
const heat = [
|
|
179
|
+
{ start: 1, end: 2, p: 0.5, snippet: "a" },
|
|
180
|
+
{ start: 5, end: 6, p: 0.9, snippet: "b" },
|
|
181
|
+
];
|
|
182
|
+
const r = rankedHeat(heat, 1);
|
|
183
|
+
assert.equal(r.length, 1);
|
|
184
|
+
assert.equal(r[0]!.start, 5);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// ── questions ───────────────────────────────────────────────────────────────
|
|
189
|
+
|
|
190
|
+
describe("questions", () => {
|
|
191
|
+
it("keys are zero-padded and stable", () => {
|
|
192
|
+
assert.equal(entryKey(0), "e000");
|
|
193
|
+
assert.equal(entryKey(17), "e017");
|
|
194
|
+
assert.equal(passageKey(3), "p03");
|
|
195
|
+
});
|
|
196
|
+
it("nameBatch renders a tree with tags + per-entry noul questions", () => {
|
|
197
|
+
const req = nameBatch("proj", "parse flags", [
|
|
198
|
+
{ path: "/x/src/cli.ts", rel: "src/cli.ts", size: 1200 },
|
|
199
|
+
{ path: "/x/README.md", rel: "README.md", size: 400 },
|
|
200
|
+
]);
|
|
201
|
+
assert.match(String(req.state.tree), /# src\//);
|
|
202
|
+
assert.match(String(req.state.tree), /e000 cli\.ts \(1\.2 KB\)/);
|
|
203
|
+
assert.ok(req.questions.e000);
|
|
204
|
+
assert.ok(req.questions.e001);
|
|
205
|
+
assert.match(req.questions.e000!.instructions, /"cli\.ts"/);
|
|
206
|
+
});
|
|
207
|
+
it("sketchBatch + passageBatch carry criteria + query", () => {
|
|
208
|
+
const s = sketchBatch("q", [{ fileKey: "f0", rel: "a.ts", sketch: "1: x" }]);
|
|
209
|
+
assert.deepEqual((s.state.passages as Record<string, unknown>).p00, ["f0", "1: x"]);
|
|
210
|
+
const ps = windows("alpha\nbeta\n", 4096, [], []);
|
|
211
|
+
const v = passageBatch("q", "a.ts", ps);
|
|
212
|
+
assert.match(String((v.state.passages as Record<string, string>).p00!), /alpha/);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// ── cascade end-to-end with a scripted judge ────────────────────────────────
|
|
217
|
+
|
|
218
|
+
describe("cascade", () => {
|
|
219
|
+
const proj = path.join(dir, "cascadeproj");
|
|
220
|
+
before(() => {
|
|
221
|
+
mkdirSync(path.join(proj, "src"), { recursive: true });
|
|
222
|
+
writeFileSync(
|
|
223
|
+
path.join(proj, "src", "spawn.ts"),
|
|
224
|
+
[
|
|
225
|
+
"// child spawn helper",
|
|
226
|
+
"export function spawnChild(cmd: string) {",
|
|
227
|
+
' const child = spawn(cmd, { shell: true });',
|
|
228
|
+
" return child;",
|
|
229
|
+
"}",
|
|
230
|
+
"",
|
|
231
|
+
].join("\n"),
|
|
232
|
+
);
|
|
233
|
+
writeFileSync(path.join(proj, "src", "unrelated.ts"), "export const unrelated = 1;\n");
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
/** Scripted judge: noul 0.95 for any question whose instructions mention
|
|
237
|
+
* the target file or a passage (sketch/verify waves), 0.05 for the rest. */
|
|
238
|
+
function fakeJudge(positiveRel: string): Judge {
|
|
239
|
+
return {
|
|
240
|
+
label: "fake",
|
|
241
|
+
async judge(request: Request) {
|
|
242
|
+
const answers: Record<string, { probability: number }> = {};
|
|
243
|
+
for (const key of Object.keys(request.questions)) {
|
|
244
|
+
const state = JSON.stringify(request.state);
|
|
245
|
+
const passages = request.state.passages as Record<string, unknown> | undefined;
|
|
246
|
+
const hit = passages
|
|
247
|
+
? Object.values(passages).some((p) => JSON.stringify(p).includes("spawn"))
|
|
248
|
+
: state.includes(positiveRel);
|
|
249
|
+
answers[key] = { probability: hit || state.includes("spawn") ? 0.95 : 0.05 };
|
|
250
|
+
}
|
|
251
|
+
return { answers, usage: { input: 100, output: 10 } };
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
it("finds the implementing file with heat ranges, strongest first", async () => {
|
|
257
|
+
const judge = fakeJudge("src/spawn.ts");
|
|
258
|
+
const opts: CascadeOptions = {
|
|
259
|
+
root: proj,
|
|
260
|
+
query: "spawn a child process",
|
|
261
|
+
extraKeywords: [],
|
|
262
|
+
judge,
|
|
263
|
+
};
|
|
264
|
+
const result = await runCascade(opts);
|
|
265
|
+
assert.equal(result.threshold, 0.2);
|
|
266
|
+
assert.ok(result.hits.length >= 1, `hits: ${JSON.stringify(result.hits)}, stats: ${JSON.stringify(result.stats)}`);
|
|
267
|
+
const top = result.hits[0]!;
|
|
268
|
+
assert.equal(top.rel, "src/spawn.ts");
|
|
269
|
+
assert.ok(top.contentScore >= 0.2);
|
|
270
|
+
assert.ok(top.ranges.length >= 1);
|
|
271
|
+
assert.ok(top.ranges[0]!.start >= 1);
|
|
272
|
+
assert.match(top.ranges[0]!.snippet, /spawn/);
|
|
273
|
+
assert.ok(result.stats.requests > 0);
|
|
274
|
+
assert.ok(result.stats.judged > 0);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("judge failures degrade to failures[] instead of throwing", async () => {
|
|
278
|
+
const judge: Judge = {
|
|
279
|
+
label: "failing",
|
|
280
|
+
async judge() {
|
|
281
|
+
throw new Error("endpoint down");
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
const result = await runCascade({ root: proj, query: "spawn", extraKeywords: [], judge });
|
|
285
|
+
assert.equal(result.hits.length, 0);
|
|
286
|
+
assert.equal(result.stats.errors, result.stats.requests);
|
|
287
|
+
assert.ok(result.stats.failures.length >= 1);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("aborted signal surfaces (does not swallow)", async () => {
|
|
291
|
+
const judge: Judge = {
|
|
292
|
+
label: "aborting",
|
|
293
|
+
async judge(_r, options) {
|
|
294
|
+
options?.signal?.throwIfAborted?.();
|
|
295
|
+
throw new Error("aborted");
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
const controller = new AbortController();
|
|
299
|
+
controller.abort();
|
|
300
|
+
await assert.rejects(
|
|
301
|
+
runCascade({ root: proj, query: "spawn", extraKeywords: [], judge, signal: controller.signal }),
|
|
302
|
+
/aborted/,
|
|
303
|
+
);
|
|
304
|
+
});
|
|
305
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// notify module — one `notify` tool: desktop notification for long-running
|
|
2
|
+
// work / when input is needed (herdr multi-pane ergonomics). Harmless:
|
|
3
|
+
// no fs, no network; spawn failures fall back to the terminal bell, so the
|
|
4
|
+
// tool always succeeds.
|
|
5
|
+
|
|
6
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { Type } from "typebox";
|
|
8
|
+
import { readDisabledTools } from "../../lib/tools.js";
|
|
9
|
+
import { notify } from "./lib/notify.js";
|
|
10
|
+
|
|
11
|
+
export default function notifyModule(pi: ExtensionAPI): void {
|
|
12
|
+
const disabled = readDisabledTools();
|
|
13
|
+
pi.registerTool({
|
|
14
|
+
name: "notify",
|
|
15
|
+
label: "Notify",
|
|
16
|
+
description:
|
|
17
|
+
"Send a desktop notification — for long-running work (announce completion) or when you need user input while the user is away. macOS uses osascript, Linux notify-send, else the terminal bell. Harmless: no fs/network access, and delivery failures fall back to a bell.",
|
|
18
|
+
promptSnippet: "Send a desktop notification",
|
|
19
|
+
parameters: Type.Object({
|
|
20
|
+
message: Type.String({ description: "Notification body." }),
|
|
21
|
+
title: Type.Optional(Type.String({ description: 'Notification title (default "pi").' })),
|
|
22
|
+
sound: Type.Optional(Type.Boolean({ description: "Play a sound with the notification (macOS only, default false)." })),
|
|
23
|
+
}),
|
|
24
|
+
async execute(_toolCallId, params) {
|
|
25
|
+
const p = params as { message: string; title?: string; sound?: boolean };
|
|
26
|
+
const r = await notify(p);
|
|
27
|
+
let text = `notified (via ${r.via}): ${p.title ?? "pi"} — ${p.message}`;
|
|
28
|
+
if (r.note) text += `\n${r.note}`;
|
|
29
|
+
return { content: [{ type: "text", text }], details: { via: r.via } };
|
|
30
|
+
},
|
|
31
|
+
defaultActive: !disabled.has("notify"),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// notify module — desktop notifications via osascript (macOS) / notify-send
|
|
2
|
+
// (Linux), terminal bell fallback everywhere else. Zero deps: everything
|
|
3
|
+
// spawns or writes "\x07". Always resolves; note carries the fallback reason.
|
|
4
|
+
|
|
5
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
6
|
+
|
|
7
|
+
/** AppleScript string literal: wrap in "…", escape \ and ". */
|
|
8
|
+
export function appleQuote(s: string): string {
|
|
9
|
+
return `"${s.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** AppleScript args for osascript: display notification <msg> with title <t>.
|
|
13
|
+
* AppleScript string literals need DOUBLE quotes — single quotes are a
|
|
14
|
+
* syntax error (live-probed: 'x' → -2741, "x" → exit 0). */
|
|
15
|
+
export function buildDarwinArgs(title: string, message: string, sound: boolean): string[] {
|
|
16
|
+
return [
|
|
17
|
+
"-e",
|
|
18
|
+
`display notification ${appleQuote(message)} with title ${appleQuote(title)}${sound ? ` sound name "Glass"` : ""}`,
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** notify-send args; app name "Pi" so the desktop shows a sane sender.
|
|
23
|
+
* ponytail: sound flag dropped — notify-send has no portable sound story. */
|
|
24
|
+
export function buildNotifySendArgs(title: string, message: string, _sound: boolean): string[] {
|
|
25
|
+
return ["-a", "Pi", title, message];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Injectable seam — resolves when the notifier exits 0, rejects otherwise. */
|
|
29
|
+
export type SpawnFn = (cmd: string, args: string[]) => Promise<void>;
|
|
30
|
+
|
|
31
|
+
function defaultSpawn(cmd: string, args: string[]): Promise<void> {
|
|
32
|
+
return new Promise((resolveP, rejectP) => {
|
|
33
|
+
const child = spawn(cmd, args, { stdio: "ignore" });
|
|
34
|
+
child.on("error", rejectP);
|
|
35
|
+
child.on("close", (code) => {
|
|
36
|
+
if (code === 0) resolveP();
|
|
37
|
+
else rejectP(new Error(`${cmd} exited with code ${code}`));
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** PATH probe for the notifier binary (spawnSync --version, like ghAvailable). */
|
|
43
|
+
function defaultProbe(cmd: string): boolean {
|
|
44
|
+
try {
|
|
45
|
+
const probe = spawnSync(cmd, ["--version"], { stdio: "ignore", timeout: 5_000 });
|
|
46
|
+
return !probe.error && probe.status === 0;
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function bell(): void {
|
|
53
|
+
process.stdout.write("\x07");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface NotifyOptions {
|
|
57
|
+
title?: string;
|
|
58
|
+
message: string;
|
|
59
|
+
sound?: boolean;
|
|
60
|
+
spawnFn?: SpawnFn;
|
|
61
|
+
probeFn?: (cmd: string) => boolean;
|
|
62
|
+
/** ponytail: injectable so the linux branches are testable on macOS. */
|
|
63
|
+
platform?: NodeJS.Platform;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface NotifyResult {
|
|
67
|
+
ok: boolean;
|
|
68
|
+
via: "osascript" | "notify-send" | "bell";
|
|
69
|
+
/** Set when we fell back — carries the failure / fallback text. */
|
|
70
|
+
note?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Send a desktop notification; never throws. Spawn or probe failure → bell. */
|
|
74
|
+
export async function notify(opts: NotifyOptions): Promise<NotifyResult> {
|
|
75
|
+
const title = opts.title ?? "pi";
|
|
76
|
+
const sound = opts.sound ?? false;
|
|
77
|
+
const spawnFn = opts.spawnFn ?? defaultSpawn;
|
|
78
|
+
const probeFn = opts.probeFn ?? defaultProbe;
|
|
79
|
+
const platform = opts.platform ?? process.platform;
|
|
80
|
+
try {
|
|
81
|
+
if (platform === "darwin") {
|
|
82
|
+
await spawnFn("osascript", buildDarwinArgs(title, opts.message, sound));
|
|
83
|
+
return { ok: true, via: "osascript" };
|
|
84
|
+
}
|
|
85
|
+
if (platform === "linux") {
|
|
86
|
+
if (!probeFn("notify-send")) {
|
|
87
|
+
bell();
|
|
88
|
+
return { ok: true, via: "bell", note: "notify-send not found on PATH — rang the terminal bell instead" };
|
|
89
|
+
}
|
|
90
|
+
await spawnFn("notify-send", buildNotifySendArgs(title, opts.message, sound));
|
|
91
|
+
return { ok: true, via: "notify-send" };
|
|
92
|
+
}
|
|
93
|
+
bell();
|
|
94
|
+
return { ok: true, via: "bell", note: `no desktop notifier on ${platform} — rang the terminal bell instead` };
|
|
95
|
+
} catch (err) {
|
|
96
|
+
bell();
|
|
97
|
+
return { ok: true, via: "bell", note: err instanceof Error ? err.message : String(err) };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// notify module unit tests — pure lib functions plus notify() with injected
|
|
2
|
+
// fakes. No real spawns, no PATH probes, no desktop notifications.
|
|
3
|
+
|
|
4
|
+
import assert from "node:assert/strict";
|
|
5
|
+
import { describe, it } from "node:test";
|
|
6
|
+
import { appleQuote, buildDarwinArgs, buildNotifySendArgs, notify } from "../lib/notify.js";
|
|
7
|
+
|
|
8
|
+
describe("appleQuote", () => {
|
|
9
|
+
it("wraps in double quotes", () => {
|
|
10
|
+
assert.equal(appleQuote("hello"), '"hello"');
|
|
11
|
+
});
|
|
12
|
+
it("escapes internal double quotes and backslashes", () => {
|
|
13
|
+
assert.equal(appleQuote('say "hi"'), '"say \\"hi\\""');
|
|
14
|
+
assert.equal(appleQuote("back\\slash"), '"back\\\\slash"');
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("buildDarwinArgs", () => {
|
|
19
|
+
it("builds the display notification script", () => {
|
|
20
|
+
assert.deepEqual(buildDarwinArgs("pi", "done", false), ["-e", `display notification "done" with title "pi"`]);
|
|
21
|
+
});
|
|
22
|
+
it("appends the Glass sound when sound=true", () => {
|
|
23
|
+
assert.deepEqual(buildDarwinArgs("pi", "done", true), ["-e", `display notification "done" with title "pi" sound name "Glass"`]);
|
|
24
|
+
});
|
|
25
|
+
it("quotes the title safely", () => {
|
|
26
|
+
const [, script] = buildDarwinArgs("Bob's", "hi", false);
|
|
27
|
+
assert.ok(script.includes('"Bob\'s"'));
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("buildNotifySendArgs", () => {
|
|
32
|
+
it("uses -a Pi and passes title then message", () => {
|
|
33
|
+
assert.deepEqual(buildNotifySendArgs("pi", "done", false), ["-a", "Pi", "pi", "done"]);
|
|
34
|
+
});
|
|
35
|
+
it("ignores sound (no portable flag)", () => {
|
|
36
|
+
assert.deepEqual(buildNotifySendArgs("pi", "done", true), ["-a", "Pi", "pi", "done"]);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("notify", () => {
|
|
41
|
+
it("darwin: resolves via osascript when spawnFn succeeds", async () => {
|
|
42
|
+
const calls: Array<{ cmd: string; args: string[] }> = [];
|
|
43
|
+
const r = await notify({
|
|
44
|
+
title: "pi",
|
|
45
|
+
message: "done",
|
|
46
|
+
sound: true,
|
|
47
|
+
platform: "darwin",
|
|
48
|
+
spawnFn: async (cmd, args) => {
|
|
49
|
+
calls.push({ cmd, args });
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
assert.deepEqual(r, { ok: true, via: "osascript" });
|
|
53
|
+
assert.deepEqual(calls, [{ cmd: "osascript", args: buildDarwinArgs("pi", "done", true) }]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("darwin: falls back to bell when spawnFn errors, note carries the failure", async () => {
|
|
57
|
+
const r = await notify({
|
|
58
|
+
message: "done",
|
|
59
|
+
platform: "darwin",
|
|
60
|
+
spawnFn: async () => {
|
|
61
|
+
throw new Error("osascript: not allowed");
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
assert.equal(r.ok, true);
|
|
65
|
+
assert.equal(r.via, "bell");
|
|
66
|
+
assert.match(r.note ?? "", /osascript: not allowed/);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("linux: probe miss → bell fallback with note", async () => {
|
|
70
|
+
const r = await notify({
|
|
71
|
+
message: "done",
|
|
72
|
+
platform: "linux",
|
|
73
|
+
probeFn: () => false,
|
|
74
|
+
spawnFn: async () => {
|
|
75
|
+
throw new Error("fakeGh: must not spawn when the probe misses");
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
assert.deepEqual(r, { ok: true, via: "bell", note: "notify-send not found on PATH — rang the terminal bell instead" });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("linux: probe hit → notify-send path", async () => {
|
|
82
|
+
const calls: string[][] = [];
|
|
83
|
+
const r = await notify({
|
|
84
|
+
title: "pi",
|
|
85
|
+
message: "done",
|
|
86
|
+
platform: "linux",
|
|
87
|
+
probeFn: () => true,
|
|
88
|
+
spawnFn: async (_cmd, args) => {
|
|
89
|
+
calls.push(args);
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
assert.deepEqual(r, { ok: true, via: "notify-send" });
|
|
93
|
+
assert.deepEqual(calls, [buildNotifySendArgs("pi", "done", false)]);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -33,6 +33,13 @@ export const READ_ONLY_TOOLS = new Set([
|
|
|
33
33
|
// Rendered-page accessibility audit — reads the page in a local headless
|
|
34
34
|
// Chrome, mutates nothing.
|
|
35
35
|
"web_a11y",
|
|
36
|
+
// PDF text extraction (vendored pdf.js, in-process) — reads a local file.
|
|
37
|
+
"read_pdf",
|
|
38
|
+
// Semantic find — read-only judging over the workspace; costs model calls
|
|
39
|
+
// but mutates nothing.
|
|
40
|
+
"jfind",
|
|
41
|
+
// ast-grep search is read-only; the REWRITE tool (ast_edit) is NOT here.
|
|
42
|
+
"ast_grep",
|
|
36
43
|
// NOTE: herdr is deliberately NOT here — its prompt action can drive a
|
|
37
44
|
// write-capable child, so every herdr call takes the confirm tier.
|
|
38
45
|
]);
|
|
@@ -43,6 +50,12 @@ export const BLOCKED_TOOLS = new Set([
|
|
|
43
50
|
"edit", "write",
|
|
44
51
|
// Diff-style file mutation — same treatment as edit/write.
|
|
45
52
|
"apply_patch",
|
|
53
|
+
// ast-grep rewrite with write:true mutates files; dry-run default keeps it
|
|
54
|
+
// a mutator (the schema carries write, so the tool is gated as a whole).
|
|
55
|
+
"ast_edit",
|
|
56
|
+
// Background shell sessions are an arbitrary-execution escape hatch —
|
|
57
|
+
// hard-blocked like bash writers, not confirm-tier.
|
|
58
|
+
"shell",
|
|
46
59
|
// File editor (create/str_replace/insert) — a direct source mutator like edit/write.
|
|
47
60
|
"str_replace_editor",
|
|
48
61
|
// Serena file mutation (restart/onboarding tools are state-changing but no
|
|
@@ -47,6 +47,12 @@ export function buildRepairGroups(cfg: RepairSettings): PanelGroup[] {
|
|
|
47
47
|
defaultValue: DEFAULT_REPAIR_SETTINGS.guards,
|
|
48
48
|
description: "Block destructive bash (rm -rf /, dd writes) and read-on-guessed-path.",
|
|
49
49
|
}),
|
|
50
|
+
row("repair.autoGenGuard", "Auto-generated write guard", "toggle", cfg.autoGenGuard, (v) => {
|
|
51
|
+
cfg.autoGenGuard = v === true;
|
|
52
|
+
}, {
|
|
53
|
+
defaultValue: DEFAULT_REPAIR_SETTINGS.autoGenGuard,
|
|
54
|
+
description: "Refuse write/edit on generated files (lockfiles, dist/, *.min.js) — regenerate from source instead; bash is the override hatch.",
|
|
55
|
+
}),
|
|
50
56
|
row("repair.autoBg", "Auto-background bash", "toggle", cfg.autoBg, (v) => {
|
|
51
57
|
cfg.autoBg = v === true;
|
|
52
58
|
}, {
|
|
@@ -66,7 +72,7 @@ export function buildRepairGroups(cfg: RepairSettings): PanelGroup[] {
|
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
const OWNED_PREFIX = "repair.";
|
|
69
|
-
const BOOL_KEYS = ["arguments", "editRetry", "guards", "autoBg"] as const;
|
|
75
|
+
const BOOL_KEYS = ["arguments", "editRetry", "guards", "autoGenGuard", "autoBg"] as const;
|
|
70
76
|
|
|
71
77
|
/** repair's ModuleConfig for the central /config panel. The factory receives
|
|
72
78
|
* the module's guarded pi but needs nothing from it — settings live in the
|