ceulen 0.10.1 → 0.12.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 +107 -1
- package/README.md +56 -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/plan-bridge.ts +18 -0
- package/extensions/lib/registry.test.ts +8 -5
- package/extensions/lib/registry.ts +22 -3
- package/extensions/modules/a2a/configPanel.ts +42 -1
- package/extensions/modules/a2a/index.ts +4 -2
- 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/attachments/configPanel.ts +91 -0
- package/extensions/modules/attachments/index.ts +274 -0
- package/extensions/modules/attachments/lib/clipboard-files.ts +80 -0
- package/extensions/modules/attachments/lib/pastes.ts +45 -0
- package/extensions/modules/attachments/lib/paths.ts +74 -0
- package/extensions/modules/attachments/lib/registry.ts +70 -0
- package/extensions/modules/attachments/lib/settings.ts +99 -0
- package/extensions/modules/attachments/lib/tray.ts +74 -0
- package/extensions/modules/attachments/test/configPanel.test.ts +122 -0
- package/extensions/modules/attachments/test/index.test.ts +870 -0
- package/extensions/modules/attachments/test/module.test.ts +53 -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/index.ts +3 -0
- package/extensions/modules/config/test/renderTools.test.ts +1 -1
- package/extensions/modules/cron/configPanel.ts +103 -0
- package/extensions/modules/cron/index.ts +638 -0
- package/extensions/modules/cron/lib/jobs.ts +201 -0
- package/extensions/modules/cron/lib/schedule.ts +161 -0
- package/extensions/modules/cron/test/cwd-repro.test.ts +98 -0
- package/extensions/modules/cron/test/index.test.ts +859 -0
- package/extensions/modules/cron/test/schedule.test.ts +93 -0
- package/extensions/modules/munin/index.ts +3 -1
- package/extensions/modules/permission/index.ts +567 -0
- package/extensions/modules/permission/test/permission.test.ts +789 -0
- package/extensions/modules/plan/index.ts +5 -0
- package/extensions/modules/repair/index.ts +160 -5
- package/extensions/modules/repair/test/freshness.test.ts +209 -0
- package/extensions/modules/repair/test/guard-selector.test.ts +67 -0
- 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/subagent/index.ts +49 -10
- 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 +356 -14
- package/extensions/modules/subagent/test/routing.test.ts +45 -0
- package/extensions/modules/subagent/test/worktree.test.ts +230 -0
- package/extensions/modules/usage/index.ts +193 -71
- package/extensions/modules/usage/test/context.test.ts +160 -120
- package/extensions/modules/web/configPanel.ts +67 -8
- package/extensions/modules/web/index.ts +3 -1
- package/extensions/modules/web/test/a11y.test.ts +1 -1
- package/extensions/modules/web/test/configPanel.test.ts +7 -3
- package/extensions/modules/web/test/settings.test.ts +6 -0
- package/package.json +2 -2
- package/skills/cron/SKILL.md +58 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// attachments module wiring: the ceulen- widget key, the node:test conversion
|
|
2
|
+
// invariants, and the registry entry shape.
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { mkdtempSync, rmSync, mkdirSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { after, before, describe, it } from "node:test";
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { MODULES } from "../../../lib/registry.js";
|
|
10
|
+
import attachmentsModule from "../index.js";
|
|
11
|
+
|
|
12
|
+
let TMP: string;
|
|
13
|
+
|
|
14
|
+
before(() => {
|
|
15
|
+
TMP = mkdtempSync(join(tmpdir(), "ceulen-att-mod-"));
|
|
16
|
+
mkdirSync(join(TMP, "agent"), { recursive: true });
|
|
17
|
+
process.env.PI_CODING_AGENT_DIR = join(TMP, "agent");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
after(() => {
|
|
21
|
+
rmSync(TMP, { recursive: true, force: true });
|
|
22
|
+
if (process.env.PI_CODING_AGENT_DIR === join(TMP, "agent")) delete process.env.PI_CODING_AGENT_DIR;
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("attachments module wiring", () => {
|
|
26
|
+
it("registers in the MODULES registry on the Files tab with its config factory", () => {
|
|
27
|
+
const entry = MODULES.find((m) => m.name === "attachments");
|
|
28
|
+
assert.ok(entry, "registry entry exists");
|
|
29
|
+
assert.equal(entry.category, "Files");
|
|
30
|
+
assert.equal(entry.load, attachmentsModule);
|
|
31
|
+
assert.ok(entry.config, "config factory wired");
|
|
32
|
+
assert.ok(!("tools" in entry) || entry.tools === undefined, "no tools to per-toggle");
|
|
33
|
+
assert.ok(!entry.core, "kill-switchable");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("widget key is ceulen-prefixed (source scan — no pi- attachments key)", () => {
|
|
37
|
+
const src = readFileSync(new URL("../index.ts", import.meta.url), "utf8");
|
|
38
|
+
assert.ok(src.includes('"ceulen-attachments"'), "ceulen-attachments widget key present");
|
|
39
|
+
assert.ok(!src.includes('"pi-attachments"'), "upstream widget key gone");
|
|
40
|
+
assert.ok(!src.includes("pi-attachments-test"), "no upstream temp-dir prefix");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("factory registers the shortcut + input/session handlers via guarded pi surface", () => {
|
|
44
|
+
const registered: string[] = [];
|
|
45
|
+
const shortcuts: string[] = [];
|
|
46
|
+
attachmentsModule({
|
|
47
|
+
on: (event: string) => registered.push(event),
|
|
48
|
+
registerShortcut: (key: string) => shortcuts.push(key),
|
|
49
|
+
} as any);
|
|
50
|
+
assert.deepEqual(registered.sort(), ["input", "session_start"]);
|
|
51
|
+
assert.equal(shortcuts.length, 1);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -228,6 +228,26 @@ export default function (pi: ExtensionAPI) {
|
|
|
228
228
|
|
|
229
229
|
const cache = createVerdictCache();
|
|
230
230
|
|
|
231
|
+
// Failure circuit-breaker (advisor's MAX_CONSECUTIVE_FAILURES precedent):
|
|
232
|
+
// the bash hot path must never pay the classify deadline per command. With
|
|
233
|
+
// the transport's 30s deadline, a cataloged-but-dead endpoint would stall
|
|
234
|
+
// every unique non-risky command ~30s — repeatedly, buying nothing (the
|
|
235
|
+
// verdict never gates execution). After 3 consecutive failures the hook
|
|
236
|
+
// fails open instantly for CLASSIFIER_PAUSE_COOLDOWN_MS; a successful
|
|
237
|
+
// verdict or a settings change re-arms immediately.
|
|
238
|
+
let consecutiveFailures = 0;
|
|
239
|
+
let pausedUntil = 0;
|
|
240
|
+
let pausedSettingsRef = "";
|
|
241
|
+
const CLASSIFIER_PAUSE_AFTER = 3;
|
|
242
|
+
const CLASSIFIER_PAUSE_COOLDOWN_MS = 5 * 60_000;
|
|
243
|
+
// The bash hot path's OWN wait budget. The verdict is annotation-only in
|
|
244
|
+
// pi 1.0.0 (no approval prompt to skip), so nothing justifies parking a
|
|
245
|
+
// tool call on a slow Jev: a verdict that misses this budget reads as
|
|
246
|
+
// "no verdict" (fail open to the normal prompt, audited). The 30s
|
|
247
|
+
// transport deadline stays as the hard backstop; the circuit breaker
|
|
248
|
+
// above then stops repeated retries.
|
|
249
|
+
const CLASSIFIER_HOOK_BUDGET_MS = 2_500;
|
|
250
|
+
|
|
231
251
|
pi.on("tool_call", async (event, ctx) => {
|
|
232
252
|
if (event.toolName !== "bash") return undefined;
|
|
233
253
|
const command = String(event.input?.command || "");
|
|
@@ -236,6 +256,24 @@ export default function (pi: ExtensionAPI) {
|
|
|
236
256
|
const s = getClassifierSettings();
|
|
237
257
|
if (!s.permission.enabled) return undefined;
|
|
238
258
|
|
|
259
|
+
// Circuit open → instant fail-open (no network attempt). A changed
|
|
260
|
+
// settings object re-arms at once: the operator switching the classifier
|
|
261
|
+
// model mid-pause should not wait out the cooldown.
|
|
262
|
+
const settingsRef = JSON.stringify([s.model, s.permission.enabled, s.permission.mode, s.permission.threshold]);
|
|
263
|
+
if (settingsRef !== pausedSettingsRef) {
|
|
264
|
+
pausedSettingsRef = settingsRef;
|
|
265
|
+
if (consecutiveFailures >= CLASSIFIER_PAUSE_AFTER) consecutiveFailures = 0; // manual remediation
|
|
266
|
+
}
|
|
267
|
+
// Circuit open? Instant fail-open. Else, if the pause window has ELAPSED
|
|
268
|
+
// (pausedUntil > 0 — 0 means "never paused", which must not reset the
|
|
269
|
+
// counter), close the circuit and retry.
|
|
270
|
+
const paused = consecutiveFailures >= CLASSIFIER_PAUSE_AFTER;
|
|
271
|
+
if (paused && Date.now() < pausedUntil) return undefined;
|
|
272
|
+
if (paused && pausedUntil > 0 && Date.now() >= pausedUntil) {
|
|
273
|
+
consecutiveFailures = 0; // cooldown elapsed → retry
|
|
274
|
+
pausedUntil = 0;
|
|
275
|
+
}
|
|
276
|
+
|
|
239
277
|
// static list first — credential-touching / irreversible commands never
|
|
240
278
|
// reach Jev and never leave the normal prompt
|
|
241
279
|
if (segments(command).some(isRisky)) return undefined;
|
|
@@ -265,6 +303,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
265
303
|
const started = Date.now();
|
|
266
304
|
try {
|
|
267
305
|
const answers = await askJev(ctx, state, questions);
|
|
306
|
+
consecutiveFailures = 0;
|
|
268
307
|
const rev = noul(answers, "reversible");
|
|
269
308
|
const serves = state.task ? noul(answers, "serves_task") : rev; // no task → reversibility only
|
|
270
309
|
const ok = rev >= s.permission.threshold && serves >= s.permission.threshold;
|
|
@@ -273,12 +312,27 @@ export default function (pi: ExtensionAPI) {
|
|
|
273
312
|
await audit({ command, ...decision, ms: Date.now() - started, model: s.model || "(auto)" });
|
|
274
313
|
return decision;
|
|
275
314
|
} catch (e) {
|
|
315
|
+
consecutiveFailures++;
|
|
316
|
+
if (consecutiveFailures >= CLASSIFIER_PAUSE_AFTER) {
|
|
317
|
+
pausedUntil = Date.now() + CLASSIFIER_PAUSE_COOLDOWN_MS;
|
|
318
|
+
await audit({ command, pause: true, failures: consecutiveFailures, cooldown_ms: CLASSIFIER_PAUSE_COOLDOWN_MS });
|
|
319
|
+
}
|
|
276
320
|
await audit({ command, error: String(e instanceof Error ? e.message : e), ms: Date.now() - started, model: s.model || "(auto)" });
|
|
277
321
|
return null; // fail-safe: fall back to the normal prompt
|
|
278
322
|
}
|
|
279
323
|
};
|
|
280
324
|
|
|
281
|
-
|
|
325
|
+
// Race the verdict against the hook budget — pi awaits this handler
|
|
326
|
+
// before executing bash, so the budget (not the transport deadline) is
|
|
327
|
+
// what the user feels. A late verdict is discarded (no cache poison).
|
|
328
|
+
let decision: { approve: boolean; reversible: number; serves_task: number } | null | undefined = cached;
|
|
329
|
+
if (decision === undefined) {
|
|
330
|
+
const budget = AbortSignal.timeout(CLASSIFIER_HOOK_BUDGET_MS);
|
|
331
|
+
decision = await Promise.race([
|
|
332
|
+
decide(),
|
|
333
|
+
new Promise<null>((resolve) => budget.addEventListener("abort", () => resolve(null), { once: true })),
|
|
334
|
+
]);
|
|
335
|
+
}
|
|
282
336
|
|
|
283
337
|
// Both modes only annotate; neither changes execution (pi 1.0.0 has no
|
|
284
338
|
// approval prompt for tool_call to skip — see module docstring). Enforce
|
|
@@ -374,6 +374,68 @@ describe("permission hook", () => {
|
|
|
374
374
|
} finally { fx2.cleanup(); }
|
|
375
375
|
});
|
|
376
376
|
|
|
377
|
+
it("circuit breaker: 3 consecutive classify failures pause the hook (instant fail-open, no network attempt)", async () => {
|
|
378
|
+
// With the transport's 30s deadline, a dead endpoint would stall every
|
|
379
|
+
// unique non-risky command ~30s — repeatedly. After 3 consecutive
|
|
380
|
+
// failures the hook must fail open instantly until the cooldown.
|
|
381
|
+
let classifyCalls = 0;
|
|
382
|
+
let fail = true;
|
|
383
|
+
const registry = stubRegistry({
|
|
384
|
+
classify: async () => {
|
|
385
|
+
classifyCalls++;
|
|
386
|
+
if (fail) throw new Error("endpoint dead");
|
|
387
|
+
return { stopReason: "stop", answers: { reversible: { type: "bool", probability: 0.99 } } };
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
const fx = await hookFixture(registry);
|
|
391
|
+
try {
|
|
392
|
+
fx.messageEnd("do some work");
|
|
393
|
+
// Three DISTINCT commands (fresh cache keys) each pay one failed call.
|
|
394
|
+
assert.equal(await fx.call("ls -la"), undefined);
|
|
395
|
+
assert.equal(await fx.call("git status"), undefined);
|
|
396
|
+
assert.equal(await fx.call("pwd"), undefined);
|
|
397
|
+
assert.equal(classifyCalls, 3);
|
|
398
|
+
assert.ok(readFileSync(fx.logPath, "utf8").includes('"pause":true'), "pause transition audited");
|
|
399
|
+
|
|
400
|
+
// Circuit open: further commands fail open with NO network attempt.
|
|
401
|
+
assert.equal(await fx.call("cat file.txt"), undefined);
|
|
402
|
+
assert.equal(await fx.call("echo hi"), undefined);
|
|
403
|
+
assert.equal(classifyCalls, 3, "no classify attempts while paused");
|
|
404
|
+
} finally { fx.cleanup(); }
|
|
405
|
+
|
|
406
|
+
// Fresh module state = fresh breaker; a healthy verdict serves normally.
|
|
407
|
+
const fx2 = await hookFixture(registry);
|
|
408
|
+
try {
|
|
409
|
+
fail = false;
|
|
410
|
+
fx2.messageEnd("more work");
|
|
411
|
+
assert.equal(await fx2.call("ls"), undefined);
|
|
412
|
+
assert.equal(classifyCalls, 4, "healthy verdict went through");
|
|
413
|
+
} finally { fx2.cleanup(); }
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it("hook budget: a slow classify loses the race — bash proceeds without waiting for the verdict", async () => {
|
|
417
|
+
// pi awaits the tool_call handler before executing bash; the verdict is
|
|
418
|
+
// annotation-only, so a slow Jev must not sit on the hot path. A verdict
|
|
419
|
+
// slower than the budget is discarded (no cache poison).
|
|
420
|
+
let classifyCalls = 0;
|
|
421
|
+
const registry = stubRegistry({
|
|
422
|
+
classify: async () => {
|
|
423
|
+
classifyCalls++;
|
|
424
|
+
await new Promise((r) => setTimeout(r, 60_000)); // way over budget
|
|
425
|
+
return { stopReason: "stop", answers: { reversible: { type: "bool", probability: 0.99 } } };
|
|
426
|
+
},
|
|
427
|
+
});
|
|
428
|
+
const fx = await hookFixture(registry);
|
|
429
|
+
try {
|
|
430
|
+
fx.messageEnd("slow endpoint");
|
|
431
|
+
const t0 = Date.now();
|
|
432
|
+
assert.equal(await fx.call("ls -la"), undefined);
|
|
433
|
+
const elapsed = Date.now() - t0;
|
|
434
|
+
assert.ok(elapsed < 5_000, `hook returned in ${elapsed}ms — budget raced, not the 30s transport deadline`);
|
|
435
|
+
assert.equal(classifyCalls, 1, "one attempt was made (then raced away)");
|
|
436
|
+
} finally { fx.cleanup(); }
|
|
437
|
+
});
|
|
438
|
+
|
|
377
439
|
it("classify error / no-model → fail-safe to prompt", async () => {
|
|
378
440
|
for (const registry of [
|
|
379
441
|
stubRegistry({ classify: async () => ({ stopReason: "error", errorMessage: "boom", answers: {} }) }),
|
|
@@ -306,8 +306,8 @@ describe("core module contract", () => {
|
|
|
306
306
|
it("nextDisabled excludes core modules; withEnableRow adds no row for them", async () => {
|
|
307
307
|
const { nextDisabled, withEnableRow } = await import("../../config/index.js");
|
|
308
308
|
// working = enabled set; nextDisabled lists the NOT-enabled non-core modules.
|
|
309
|
-
assert.deepEqual(nextDisabled(new Set(["munin"])), ["zai", "ponytail", "subagent", "plan", "a2a", "todo", "repair", "serena", "fff", "web", "rules", "gh", "steering", "
|
|
310
|
-
assert.deepEqual(nextDisabled(new Set(["router", "classifier", "ux", "munin", "usage", "ponytail", "subagent", "plan", "a2a", "todo", "repair", "serena", "fff", "web", "gh", "rules", "steering", "zai", "rtk", "config"])), [], "all-on → nothing disabled");
|
|
309
|
+
assert.deepEqual(nextDisabled(new Set(["munin"])), ["zai", "ponytail", "subagent", "plan", "a2a", "todo", "cron", "repair", "serena", "fff", "web", "rules", "attachments", "gh", "rtk", "steering", "permission"]);
|
|
310
|
+
assert.deepEqual(nextDisabled(new Set(["router", "classifier", "ux", "munin", "usage", "ponytail", "subagent", "plan", "a2a", "todo", "cron", "repair", "serena", "fff", "web", "gh", "rules", "steering", "zai", "rtk", "config", "attachments", "permission"])), [], "all-on → nothing disabled");
|
|
311
311
|
for (const core of ["composer", "advisor", "router", "classifier", "usage", "ux", "config"]) {
|
|
312
312
|
assert.ok(!nextDisabled(new Set()).includes(core), `core (${core}) is never disableable`);
|
|
313
313
|
}
|
|
@@ -254,6 +254,9 @@ export default function configModule(pi: ExtensionAPI, deps?: ModuleLoadDeps): v
|
|
|
254
254
|
ponytail: { section: "Ponytail", icon: "🦥" },
|
|
255
255
|
plan: { section: "Plan mode", icon: "🗺️" },
|
|
256
256
|
todo: { section: "Todo", icon: "☑" },
|
|
257
|
+
cron: { section: "Cron", icon: "⏰" },
|
|
258
|
+
attachments: { section: "Attachments", icon: "📎" },
|
|
259
|
+
permission: { section: "Permissions", icon: "🛡" },
|
|
257
260
|
serena: { section: "Serena" },
|
|
258
261
|
repair: { section: "Repair", icon: "🔧" },
|
|
259
262
|
fff: { section: "FFF search" },
|
|
@@ -34,7 +34,7 @@ function toolGroups(): PanelGroup[] {
|
|
|
34
34
|
describe("render smoke — ported modules' /config sections", () => {
|
|
35
35
|
it("renders Enable + per-tool rows inside the boxed frame with zero overflow", () => {
|
|
36
36
|
const groups = toolGroups();
|
|
37
|
-
assert.equal(groups.length,
|
|
37
|
+
assert.equal(groups.length, 16, "serena + fff + ux + munin + classifier + advisor + subagent + repair + plan + todo + web + rules + a2a + gh + cron + steering·think (rtk has no tools)");
|
|
38
38
|
const m = new ConfigPanelModel(groups, theme, "Settings");
|
|
39
39
|
m.getHeight = () => 40;
|
|
40
40
|
for (const w of [110, 100, 80, 60]) {
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cron's /config contribution — Tasks tab, Cron section.
|
|
3
|
+
*
|
|
4
|
+
* Rows write the GLOBAL agent-dir settings.json `cron` section (a
|
|
5
|
+
* writeCronSection sibling of readCronSettings). The module re-reads settings
|
|
6
|
+
* at load (global-only) and on every session_start (project overlay), so a
|
|
7
|
+
* save applies to the next session without /reload; the tick interval also
|
|
8
|
+
* re-arms on session_start.
|
|
9
|
+
*/
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { row, type PanelGroup } from "../../lib/panel.js";
|
|
14
|
+
import type { ModuleConfig } from "../../lib/registry.js";
|
|
15
|
+
import { readCronSettings, type CronSettings } from "./index.ts";
|
|
16
|
+
|
|
17
|
+
/** Effective settings for the panel — the same chain the scheduler uses. */
|
|
18
|
+
export function loadCronSettings(): CronSettings {
|
|
19
|
+
return readCronSettings();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Write ONE patch into the GLOBAL agent-dir `cron` section (never the merged
|
|
23
|
+
* snapshot — a project-overlay value must not be promoted to global). */
|
|
24
|
+
export function writeCronSection(patch: Partial<CronSettings>): string {
|
|
25
|
+
const file = join(process.env.PI_CODING_AGENT_DIR || join(homedir(), ".pi", "agent"), "settings.json");
|
|
26
|
+
let settings: Record<string, unknown>;
|
|
27
|
+
try {
|
|
28
|
+
settings = existsSync(file) ? (JSON.parse(readFileSync(file, "utf8")) as Record<string, unknown>) : {};
|
|
29
|
+
} catch {
|
|
30
|
+
throw new Error(`${file} is not valid JSON — fix or remove it before saving.`);
|
|
31
|
+
}
|
|
32
|
+
const s = (settings.cron ?? {}) as Record<string, unknown>;
|
|
33
|
+
Object.assign(s, patch);
|
|
34
|
+
settings.cron = s;
|
|
35
|
+
mkdirSync(join(file, ".."), { recursive: true });
|
|
36
|
+
const tmp = file + ".tmp";
|
|
37
|
+
writeFileSync(tmp, JSON.stringify(settings, null, 2) + "\n", { mode: 0o600 });
|
|
38
|
+
renameSync(tmp, file);
|
|
39
|
+
return file;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Build the cron panel groups over a working copy (mutated by row setters).
|
|
43
|
+
* Exported for tests. */
|
|
44
|
+
export function buildCronGroups(cfg: CronSettings): PanelGroup[] {
|
|
45
|
+
return [
|
|
46
|
+
{
|
|
47
|
+
key: "cron",
|
|
48
|
+
label: "Cron",
|
|
49
|
+
tab: "Tasks",
|
|
50
|
+
icon: "⏰",
|
|
51
|
+
rows: [
|
|
52
|
+
row("cron.enabled", "Scheduler enabled", "toggle", cfg.enabled, (v) => {
|
|
53
|
+
cfg.enabled = Boolean(v);
|
|
54
|
+
}, {
|
|
55
|
+
description: "Global kill switch for the in-process scheduler (30s ticks fire due jobs into the live session).",
|
|
56
|
+
defaultValue: true,
|
|
57
|
+
}),
|
|
58
|
+
row("cron.tickMs", "Tick interval (ms)", "number", cfg.tickMs, (v) => {
|
|
59
|
+
const n = Number(v);
|
|
60
|
+
if (Number.isFinite(n) && n > 0) cfg.tickMs = n;
|
|
61
|
+
}, {
|
|
62
|
+
description: "How often due jobs are checked. Clamped 5s–10min on read (default 30s). Re-arms on next session.",
|
|
63
|
+
defaultValue: 30_000,
|
|
64
|
+
}),
|
|
65
|
+
row("cron.timeoutMs", "Headless child cap (ms)", "number", cfg.timeoutMs, (v) => {
|
|
66
|
+
const n = Number(v);
|
|
67
|
+
if (Number.isFinite(n) && n > 0) cfg.timeoutMs = n;
|
|
68
|
+
}, {
|
|
69
|
+
description: "Hard cap for pinned (headless) job runs: SIGTERM at the cap, SIGKILL after 5s grace. Clamped 1min–24h (default 10min).",
|
|
70
|
+
defaultValue: 600_000,
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const OWNED_PREFIX = "cron.";
|
|
78
|
+
|
|
79
|
+
/** cron's ModuleConfig for the central /config panel. */
|
|
80
|
+
export function cronConfig(): ModuleConfig {
|
|
81
|
+
const before = loadCronSettings();
|
|
82
|
+
const working: CronSettings = { ...before };
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
groups: () => buildCronGroups(working),
|
|
86
|
+
save: async (edited, ctx) => {
|
|
87
|
+
const FIELDS: (keyof CronSettings)[] = ["enabled", "tickMs", "timeoutMs"];
|
|
88
|
+
const patch: Partial<CronSettings> = {};
|
|
89
|
+
for (const key of edited) {
|
|
90
|
+
if (!key.startsWith(OWNED_PREFIX)) continue;
|
|
91
|
+
const field = key.slice(OWNED_PREFIX.length) as keyof CronSettings;
|
|
92
|
+
if (FIELDS.includes(field)) (patch as Record<string, unknown>)[field] = working[field];
|
|
93
|
+
}
|
|
94
|
+
if (!Object.keys(patch).length) return;
|
|
95
|
+
try {
|
|
96
|
+
const file = writeCronSection(patch);
|
|
97
|
+
ctx.ui.notify(`Cron settings saved to ${file} — applies next session (project .pi/settings.json may shadow per field).`, "info");
|
|
98
|
+
} catch (e) {
|
|
99
|
+
ctx.ui.notify(`Cron save failed: ${e instanceof Error ? e.message : e}`, "error");
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|