ceulen 0.8.0 → 0.10.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 +72 -0
- package/extensions/lib/registry.ts +16 -3
- package/extensions/modules/a2a/configPanel.ts +435 -0
- package/extensions/modules/a2a/index.ts +1117 -0
- package/extensions/modules/a2a/lib/activity.ts +173 -0
- package/extensions/modules/a2a/lib/client.ts +1061 -0
- package/extensions/modules/a2a/lib/config.ts +920 -0
- package/extensions/modules/a2a/lib/discovery.ts +158 -0
- package/extensions/modules/a2a/lib/gateway.ts +889 -0
- package/extensions/modules/a2a/lib/mdns.ts +233 -0
- package/extensions/modules/a2a/lib/outcome.ts +48 -0
- package/extensions/modules/a2a/lib/persistence.ts +123 -0
- package/extensions/modules/a2a/lib/protocol.ts +506 -0
- package/extensions/modules/a2a/lib/registry.ts +146 -0
- package/extensions/modules/a2a/lib/retry.ts +86 -0
- package/extensions/modules/a2a/lib/security.ts +394 -0
- package/extensions/modules/a2a/lib/server.ts +1436 -0
- package/extensions/modules/a2a/test/activity.test.ts +138 -0
- package/extensions/modules/a2a/test/bridge.test.ts +101 -0
- package/extensions/modules/a2a/test/broadcast-parse.test.ts +31 -0
- package/extensions/modules/a2a/test/chai.ts +101 -0
- package/extensions/modules/a2a/test/client.test.ts +856 -0
- package/extensions/modules/a2a/test/completion-guard.test.ts +73 -0
- package/extensions/modules/a2a/test/config.test.ts +1396 -0
- package/extensions/modules/a2a/test/configPanel.test.ts +393 -0
- package/extensions/modules/a2a/test/discovery.test.ts +144 -0
- package/extensions/modules/a2a/test/extension.test.ts +135 -0
- package/extensions/modules/a2a/test/gateway.test.ts +1449 -0
- package/extensions/modules/a2a/test/helpers.ts +39 -0
- package/extensions/modules/a2a/test/mdns.test.ts +131 -0
- package/extensions/modules/a2a/test/outcome.test.ts +61 -0
- package/extensions/modules/a2a/test/persistence.test.ts +92 -0
- package/extensions/modules/a2a/test/protocol.test.ts +217 -0
- package/extensions/modules/a2a/test/registry.test.ts +114 -0
- package/extensions/modules/a2a/test/retry.test.ts +136 -0
- package/extensions/modules/a2a/test/security.test.ts +281 -0
- package/extensions/modules/a2a/test/server.test.ts +1966 -0
- package/extensions/modules/a2a/test/tmp.ts +59 -0
- package/extensions/modules/a2a/test/vendor.test.ts +57 -0
- package/extensions/modules/a2a/types/bonjour-service.d.ts +22 -0
- package/extensions/modules/a2a/vendor/VENDORED.md +18 -0
- package/extensions/modules/a2a/vendor/package.json +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/LICENSE +23 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/index.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/index.js +57 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/KeyValue.d.ts +4 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/KeyValue.js +3 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/bonjour.d.ts +13 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/bonjour.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/browser.d.ts +41 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/browser.js +209 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/dns-txt.d.ts +9 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/dns-txt.js +42 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/mdns-server.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/mdns-server.js +119 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/registry.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/registry.js +142 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service-types.d.ts +7 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service-types.js +53 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service.d.ts +58 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service.js +130 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/dns-equal.d.ts +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/dns-equal.js +13 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/equal-txt.d.ts +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/equal-txt.js +17 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-service.d.ts +4 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-service.js +22 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-txt.d.ts +3 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-txt.js +6 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/classes.js +23 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/index.js +1761 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/opcodes.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/optioncodes.js +59 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/rcodes.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/types.js +103 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/es6.js +73 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/index.js +46 -0
- package/extensions/modules/a2a/vendor/vendored_deps/ip-codec/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/ip-codec/index.cjs +250 -0
- package/extensions/modules/a2a/vendor/vendored_deps/multicast-dns/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/multicast-dns/index.js +205 -0
- package/extensions/modules/a2a/vendor/vendored_deps/thunky/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/thunky/index.js +55 -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/gh/configPanel.ts +82 -0
- package/extensions/modules/gh/index.ts +86 -0
- package/extensions/modules/gh/lib/gh-cli.ts +337 -0
- package/extensions/modules/gh/lib/ops.ts +771 -0
- package/extensions/modules/gh/test/gh.test.ts +280 -0
- package/extensions/modules/plan/lib/plan-tools.ts +8 -0
- package/extensions/modules/repair/index.ts +138 -0
- package/extensions/modules/repair/lib/guards.ts +1 -1
- package/extensions/modules/repair/lib/read-selector.ts +242 -0
- package/extensions/modules/repair/test/read-selector.test.ts +198 -0
- package/extensions/modules/steering/configPanel.ts +8 -0
- package/extensions/modules/steering/index.ts +6 -0
- package/extensions/modules/steering/lib/settings.ts +6 -2
- package/extensions/modules/steering/lib/think.ts +66 -0
- package/extensions/modules/steering/test/harness.ts +5 -0
- package/extensions/modules/steering/test/settings.test.ts +2 -0
- package/extensions/modules/steering/test/think.test.ts +77 -0
- package/extensions/modules/todo/configPanel.ts +55 -0
- package/extensions/modules/todo/index.ts +46 -13
- package/extensions/modules/todo/lib/render.ts +271 -0
- package/extensions/modules/todo/lib/settings.ts +73 -0
- package/extensions/modules/todo/test/todo.test.ts +276 -0
- package/extensions/modules/web/index.ts +37 -1
- package/extensions/modules/web/lib/a11y.ts +252 -0
- package/extensions/modules/web/test/a11y.test.ts +218 -0
- package/extensions/modules/web/test/extension.test.ts +2 -2
- package/extensions/modules/web/test/hook.test.ts +2 -2
- package/extensions/modules/web/vendor/axe/LICENSE +362 -0
- package/extensions/modules/web/vendor/axe/LICENSE-3RD-PARTY.txt +66 -0
- package/extensions/modules/web/vendor/axe/README.md +19 -0
- package/extensions/modules/web/vendor/axe/axe.min.js +12 -0
- package/package.json +2 -2
- package/skills/a2a/SKILL.md +63 -0
- package/skills/web/SKILL.md +1 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// think tool tests — definition shape, execute contract, collapsed rendering,
|
|
2
|
+
// the thinkTool gate on module load, and the kill-switch defaultActive.
|
|
3
|
+
//
|
|
4
|
+
// The agent dir is isolated for the WHOLE file (advisor catch: a test outside
|
|
5
|
+
// the gating describe would otherwise read the developer's real
|
|
6
|
+
// ~/.pi/agent/settings.json — a locally kill-switched `think` would fail it
|
|
7
|
+
// spuriously).
|
|
8
|
+
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
11
|
+
|
|
12
|
+
import steeringModule from "../index.js";
|
|
13
|
+
import { THINK_MARKER, thinkTool } from "../lib/think.js";
|
|
14
|
+
import { createFakePi, isolateAgentDir, setSteeringSettings } from "./harness.js";
|
|
15
|
+
|
|
16
|
+
describe("think tool", () => {
|
|
17
|
+
let iso: { dir: string; restore: () => void };
|
|
18
|
+
beforeEach(() => { iso = isolateAgentDir("ceulen-think-test-"); });
|
|
19
|
+
afterEach(() => iso.restore());
|
|
20
|
+
|
|
21
|
+
describe("definition", () => {
|
|
22
|
+
it("has the OMP contract: private scratchpad, no promptSnippet", () => {
|
|
23
|
+
const def = thinkTool();
|
|
24
|
+
assert.equal(def.name, "think");
|
|
25
|
+
assert.equal(def.label, "Think");
|
|
26
|
+
assert.match(def.description, /scratchpad/i);
|
|
27
|
+
assert.equal((def as Record<string, unknown>).promptSnippet, undefined, "omitted from Available tools (OMP parity)");
|
|
28
|
+
assert.ok(Array.isArray(def.promptGuidelines) && def.promptGuidelines!.length > 0);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("execute returns a minimal acknowledgment, never the thoughts", async () => {
|
|
32
|
+
const def = thinkTool();
|
|
33
|
+
const result = await def.execute();
|
|
34
|
+
assert.deepEqual(result, { content: [{ type: "text", text: "—" }] });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("renderers return pi-tui Text components wrapping the dim marker", () => {
|
|
38
|
+
const def = thinkTool();
|
|
39
|
+
const theme = { fg: (token: string, text: string) => `<${token}>${text}</>` };
|
|
40
|
+
const call = def.renderCall({ thoughts: "abcd" }, theme);
|
|
41
|
+
const result = def.renderResult(undefined as never, undefined as never, theme);
|
|
42
|
+
// Regression (live 2026-10-05): a raw string here crashes the TUI with
|
|
43
|
+
// "this.child.render is not a function" — Box needs a real Component.
|
|
44
|
+
for (const c of [call, result]) {
|
|
45
|
+
assert.equal(typeof c, "object");
|
|
46
|
+
assert.equal(typeof (c as { render?: unknown }).render, "function", "must be a pi-tui Component");
|
|
47
|
+
const rendered = (c as { render(width?: number): string[] }).render(80);
|
|
48
|
+
assert.ok(Array.isArray(rendered) && rendered.every((l) => typeof l === "string"));
|
|
49
|
+
}
|
|
50
|
+
assert.match((call as unknown as { render(w: number): string[] }).render(80).join(""), /· think \(4 chars\)/);
|
|
51
|
+
assert.match((result as unknown as { render(w: number): string[] }).render(80).join(""), /·/);
|
|
52
|
+
// No theme → plain text (renderers must not crash on thin contexts).
|
|
53
|
+
const plain = def.renderCall({ thoughts: "x" });
|
|
54
|
+
assert.match((plain as unknown as { render(w: number): string[] }).render(80).join(""), /· think \(1 chars\)/);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("defaultActive is true unless the kill-switch names think", () => {
|
|
58
|
+
assert.equal(thinkTool().defaultActive, true, "isolated dir has no ceulen.disabledTools");
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("module gating", () => {
|
|
63
|
+
it("thinkTool: false (default) registers no think tool", () => {
|
|
64
|
+
setSteeringSettings(iso.dir, {});
|
|
65
|
+
const pi = createFakePi();
|
|
66
|
+
assert.equal(pi.tools.length, 0, "no tool when the gate is off");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("thinkTool: true registers the think tool, active unless disabled", () => {
|
|
70
|
+
setSteeringSettings(iso.dir, { thinkTool: true });
|
|
71
|
+
const pi = createFakePi();
|
|
72
|
+
assert.equal(pi.tools.length, 1);
|
|
73
|
+
assert.equal(pi.tools[0]!.name, "think");
|
|
74
|
+
assert.equal(pi.tools[0]!.defaultActive, true);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* todo's /config contribution — Tasks tab, Todo section.
|
|
3
|
+
*
|
|
4
|
+
* One row: `todo.lingerSecs` — how long the all-done HUD stays above the
|
|
5
|
+
* editor before auto-clearing (OMP's tasks.todoClearDelay parity). Writes the
|
|
6
|
+
* GLOBAL agent-dir settings.json `todo` section (lib/settings.ts); read per
|
|
7
|
+
* commit, so a save applies live with no /reload.
|
|
8
|
+
*/
|
|
9
|
+
import { row, type PanelGroup } from "../../lib/panel.js";
|
|
10
|
+
import type { ModuleConfig } from "../../lib/registry.js";
|
|
11
|
+
import { LINGER_DESCRIPTIONS, LINGER_VALUES, readLingerSecs, writeTodoSection } from "./lib/settings.ts";
|
|
12
|
+
|
|
13
|
+
/** Working-copy panel groups. Exported for tests. */
|
|
14
|
+
export function buildTodoGroups(lingerSecs: { value: string }): PanelGroup[] {
|
|
15
|
+
return [
|
|
16
|
+
{
|
|
17
|
+
key: "todo",
|
|
18
|
+
label: "Todo",
|
|
19
|
+
tab: "Tasks",
|
|
20
|
+
icon: "☑",
|
|
21
|
+
rows: [
|
|
22
|
+
row("todo.lingerSecs", "Completed board lingers", "number", lingerSecs.value, (v) => {
|
|
23
|
+
lingerSecs.value = String(v ?? "");
|
|
24
|
+
}, {
|
|
25
|
+
menu: () => LINGER_VALUES.map((v) => ({ value: v, label: v === "-1" ? "Never" : `${Number(v) / 60} minute${Number(v) === 60 ? "" : "s"}`, description: LINGER_DESCRIPTIONS[v] })),
|
|
26
|
+
description: "How long the all-done TODO HUD stays above the editor before auto-clearing. Applies live.",
|
|
27
|
+
defaultValue: "60",
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const OWNED_PREFIX = "todo.";
|
|
35
|
+
|
|
36
|
+
/** todo's ModuleConfig for the central /config panel. */
|
|
37
|
+
export function todoConfig(): ModuleConfig {
|
|
38
|
+
const before = String(readLingerSecs());
|
|
39
|
+
const working = { value: before };
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
groups: () => buildTodoGroups(working),
|
|
43
|
+
save: async (edited, ctx) => {
|
|
44
|
+
if (![...edited].some((k) => k.startsWith(OWNED_PREFIX))) return;
|
|
45
|
+
const n = parseInt(working.value, 10);
|
|
46
|
+
if (!Number.isFinite(n)) return;
|
|
47
|
+
try {
|
|
48
|
+
const file = writeTodoSection(n);
|
|
49
|
+
ctx.ui.notify(`Todo config saved to ${file} — applies live (read per commit).`, "info");
|
|
50
|
+
} catch (e) {
|
|
51
|
+
ctx.ui.notify(`Todo save failed: ${e instanceof Error ? e.message : e}`, "error");
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -39,12 +39,20 @@ import type {
|
|
|
39
39
|
ExtensionContext,
|
|
40
40
|
ToolDefinition,
|
|
41
41
|
} from "@earendil-works/pi-coding-agent";
|
|
42
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
42
43
|
import { Type, type Static } from "typebox";
|
|
44
|
+
import {
|
|
45
|
+
createTodoWidgetController,
|
|
46
|
+
renderTodoBoard,
|
|
47
|
+
type WidgetTheme,
|
|
48
|
+
} from "./lib/render.ts";
|
|
49
|
+
import { readLingerSecs } from "./lib/settings.ts";
|
|
43
50
|
import { readDisabledTools } from "../../lib/tools.js";
|
|
44
51
|
|
|
45
52
|
export const TODO_TOOL = "todo";
|
|
46
53
|
export const TODO_ENTRY_TYPE = "ceulen-todo";
|
|
47
54
|
export const TODO_STATUS_KEY = "ceulen-todo";
|
|
55
|
+
export const TODO_WIDGET_KEY = "ceulen-todo";
|
|
48
56
|
|
|
49
57
|
export type TodoStatus = "pending" | "in_progress" | "done" | "blocked";
|
|
50
58
|
|
|
@@ -118,23 +126,24 @@ function clonePhases(phases: readonly TodoPhase[]): TodoPhase[] {
|
|
|
118
126
|
}
|
|
119
127
|
|
|
120
128
|
/** A phase is BLOCKED when it has unmet blockers — derived, never stored. */
|
|
121
|
-
function isBlocked(phase: TodoPhase,
|
|
129
|
+
export function isBlocked(phase: TodoPhase, phases: readonly TodoPhase[]): boolean {
|
|
130
|
+
const byId = new Map(phases.map((p) => [p.id, p]));
|
|
122
131
|
return phase.blockedBy.some((dep) => {
|
|
123
132
|
const depPhase = byId.get(dep);
|
|
124
133
|
return depPhase !== undefined && depPhase.status !== "done";
|
|
125
134
|
});
|
|
126
135
|
}
|
|
127
136
|
|
|
128
|
-
function effectiveStatus(phase: TodoPhase,
|
|
129
|
-
return isBlocked(phase,
|
|
137
|
+
export function effectiveStatus(phase: TodoPhase, phases: readonly TodoPhase[]): TodoStatus {
|
|
138
|
+
return isBlocked(phase, phases) ? "blocked" : phase.status;
|
|
130
139
|
}
|
|
131
140
|
|
|
132
141
|
/** Next actionable phase: first running one, else the first pending unblocked phase. */
|
|
133
142
|
function nextActionable(phases: readonly TodoPhase[]): TodoPhase | undefined {
|
|
134
143
|
const byId = new Map(phases.map((p) => [p.id, p]));
|
|
135
144
|
return (
|
|
136
|
-
phases.find((p) => p.status === "in_progress" && !isBlocked(p,
|
|
137
|
-
phases.find((p) => p.status === "pending" && !isBlocked(p,
|
|
145
|
+
phases.find((p) => p.status === "in_progress" && !isBlocked(p, phases)) ??
|
|
146
|
+
phases.find((p) => p.status === "pending" && !isBlocked(p, phases))
|
|
138
147
|
);
|
|
139
148
|
}
|
|
140
149
|
|
|
@@ -146,9 +155,8 @@ function counts(phases: readonly TodoPhase[]): { done: number; total: number } {
|
|
|
146
155
|
export function statusLine(phases: readonly TodoPhase[]): string | undefined {
|
|
147
156
|
const { done, total } = counts(phases);
|
|
148
157
|
if (total === 0 || done === total) return undefined;
|
|
149
|
-
const
|
|
150
|
-
const
|
|
151
|
-
const blocked = phases.filter((p) => p.status !== "done" && isBlocked(p, byId)).length;
|
|
158
|
+
const active = phases.find((p) => p.status === "in_progress" && !isBlocked(p, phases));
|
|
159
|
+
const blocked = phases.filter((p) => p.status !== "done" && isBlocked(p, phases)).length;
|
|
152
160
|
return [
|
|
153
161
|
`▸ ${done}/${total} done`,
|
|
154
162
|
active ? `in_progress: ${active.title}` : "next: " + (nextActionable(phases)?.title ?? "—"),
|
|
@@ -165,7 +173,7 @@ function renderBoard(phases: readonly TodoPhase[], note?: string): string {
|
|
|
165
173
|
const byId = new Map(phases.map((p) => [p.id, p]));
|
|
166
174
|
const { done, total } = counts(phases);
|
|
167
175
|
const lines = phases.map((p, i) => {
|
|
168
|
-
const st = effectiveStatus(p,
|
|
176
|
+
const st = effectiveStatus(p, phases);
|
|
169
177
|
const waiters = p.blockedBy.filter((dep) => byId.get(dep)?.status !== "done");
|
|
170
178
|
const tail = [
|
|
171
179
|
st === "blocked" && waiters.length > 0 ? `blocked by ${waiters.join(", ")}` : undefined,
|
|
@@ -346,6 +354,14 @@ export default function todoModule(pi: ExtensionAPI): void {
|
|
|
346
354
|
let phases: TodoPhase[] = [];
|
|
347
355
|
/** Last live ctx — the status item is set through it on every mutation. */
|
|
348
356
|
let liveUi: ExtensionContext["ui"] | undefined;
|
|
357
|
+
/** Above-editor HUD (colored board + all-done linger). */
|
|
358
|
+
const widget = createTodoWidgetController();
|
|
359
|
+
|
|
360
|
+
const syncWidget = (ctx?: { mode?: string; ui: ExtensionContext["ui"] }): void => {
|
|
361
|
+
// getPhases: the render closure reads LIVE state — a captured array would
|
|
362
|
+
// freeze the HUD at the install-time snapshot (caught live in a herdr pane).
|
|
363
|
+
widget.sync(ctx, phases, readLingerSecs(), () => phases);
|
|
364
|
+
};
|
|
349
365
|
|
|
350
366
|
const syncStatus = (ui: ExtensionContext["ui"] | undefined = liveUi): void => {
|
|
351
367
|
if (!ui) return;
|
|
@@ -356,10 +372,11 @@ export default function todoModule(pi: ExtensionAPI): void {
|
|
|
356
372
|
}
|
|
357
373
|
};
|
|
358
374
|
|
|
359
|
-
const commit = (next: TodoPhase[],
|
|
375
|
+
const commit = (next: TodoPhase[], ctx?: { mode?: string; ui: ExtensionContext["ui"] }): void => {
|
|
360
376
|
phases = next;
|
|
361
377
|
pi.appendEntry(TODO_ENTRY_TYPE, { phases });
|
|
362
|
-
syncStatus(ui);
|
|
378
|
+
syncStatus(ctx?.ui);
|
|
379
|
+
syncWidget(ctx);
|
|
363
380
|
};
|
|
364
381
|
|
|
365
382
|
const tool: ToolDefinition<typeof todoSchema, { phases: TodoPhase[] }> = {
|
|
@@ -379,12 +396,24 @@ export default function todoModule(pi: ExtensionAPI): void {
|
|
|
379
396
|
],
|
|
380
397
|
defaultActive: !readDisabledTools().has(TODO_TOOL),
|
|
381
398
|
parameters: todoSchema,
|
|
399
|
+
// TUI-only: the colored board (status colors, strikethrough on done).
|
|
400
|
+
// The LLM-facing text content stays the plain board — this only swaps
|
|
401
|
+
// what the transcript draws. No phases in details → the plain text blob.
|
|
402
|
+
renderResult(result, _options, theme) {
|
|
403
|
+
const detail = result.details as { phases?: TodoPhase[] } | undefined;
|
|
404
|
+
const boardPhases = Array.isArray(detail?.phases) && detail!.phases!.every(isTodoPhase) ? detail!.phases! : [];
|
|
405
|
+
if (boardPhases.length === 0) {
|
|
406
|
+
const text = result.content[0];
|
|
407
|
+
return new Text(text?.type === "text" ? text.text : "", 0, 0);
|
|
408
|
+
}
|
|
409
|
+
return new Text(renderTodoBoard(boardPhases, theme as unknown as WidgetTheme).join("\n"), 0, 0);
|
|
410
|
+
},
|
|
382
411
|
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
383
412
|
const ui = ctx.ui;
|
|
384
413
|
liveUi = ui;
|
|
385
414
|
const result = applyTodo(phases, params);
|
|
386
415
|
const failed = result.errors.length > 0;
|
|
387
|
-
if (!failed && result.changed) commit(result.phases,
|
|
416
|
+
if (!failed && result.changed) commit(result.phases, ctx);
|
|
388
417
|
const effective = failed ? phases : result.phases;
|
|
389
418
|
// Mutations restate the batch contract; a pure view stays quiet.
|
|
390
419
|
const tail = failed
|
|
@@ -415,7 +444,7 @@ export default function todoModule(pi: ExtensionAPI): void {
|
|
|
415
444
|
ctx.ui.notify("Todo list is already empty.", "info");
|
|
416
445
|
return;
|
|
417
446
|
}
|
|
418
|
-
commit([], ctx
|
|
447
|
+
commit([], ctx);
|
|
419
448
|
ctx.ui.notify("Todo list cleared.", "info");
|
|
420
449
|
return;
|
|
421
450
|
}
|
|
@@ -436,11 +465,15 @@ export default function todoModule(pi: ExtensionAPI): void {
|
|
|
436
465
|
[];
|
|
437
466
|
phases = resolveTodoPhases(entries);
|
|
438
467
|
syncStatus(ctx.ui);
|
|
468
|
+
// A resumed session with open work shows the HUD again; an all-done
|
|
469
|
+
// snapshot from a previous session stays hidden (no re-linger).
|
|
470
|
+
syncWidget(ctx);
|
|
439
471
|
});
|
|
440
472
|
|
|
441
473
|
// The indicator must not leak past this runtime (subagent-precedent: the
|
|
442
474
|
// widget controller owns set + clear on session change).
|
|
443
475
|
pi.on("session_shutdown", () => {
|
|
476
|
+
widget.dispose();
|
|
444
477
|
try {
|
|
445
478
|
liveUi?.setStatus(TODO_STATUS_KEY, undefined);
|
|
446
479
|
} catch {
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// todo widget — the themed above-editor HUD + shared board renderer (OMP look).
|
|
2
|
+
//
|
|
3
|
+
// Pattern: extensions/modules/subagent/lib/widget.ts (WidgetTheme shim, pure
|
|
4
|
+
// renderers, small controller owning the setWidget handle). Todo is simpler:
|
|
5
|
+
// no live subscription — the widget only changes on a tool mutation, so the
|
|
6
|
+
// controller re-renders on demand and carries the ALL-DONE LINGER timer
|
|
7
|
+
// (OMP parity: the completed board stays `lingerSecs` before auto-clearing).
|
|
8
|
+
//
|
|
9
|
+
// Visual language (matched to OMP's todo HUD, user-approved 2026-10-04;
|
|
10
|
+
// nested-tree + progress-spine per the 2026-10-04 side-by-side):
|
|
11
|
+
// TODO
|
|
12
|
+
// └─ Tasks · 2/5 head connector accents when ALL done
|
|
13
|
+
// ├─ ☑ done-title success + strikethrough (stays visible)
|
|
14
|
+
// ├─ ☐ current-title mdLink (blue = "current"), default fg title
|
|
15
|
+
// └─ ☐ pending-title dim
|
|
16
|
+
// The tree spine is OMP's progress path: a connector turns accent once its
|
|
17
|
+
// phase is done — the completed tree reads as one lit path. Blocked rows keep
|
|
18
|
+
// the warning treatment + `(blocked by …)` tail; ids and notes stay in dim
|
|
19
|
+
// tails. The LLM-facing text board (index.ts) is unchanged.
|
|
20
|
+
//
|
|
21
|
+
// Rendering uses theme tokens only (theme.fg/bold/strikethrough) — no raw ANSI.
|
|
22
|
+
|
|
23
|
+
import { truncateToWidth } from "@earendil-works/pi-tui";
|
|
24
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
25
|
+
import { effectiveStatus, TODO_WIDGET_KEY, type TodoPhase } from "../index.ts";
|
|
26
|
+
|
|
27
|
+
/** Theme shim so renderers are testable without a Theme instance. */
|
|
28
|
+
export interface WidgetTheme {
|
|
29
|
+
fg(color: string, text: string): string;
|
|
30
|
+
bold?(text: string): string;
|
|
31
|
+
strikethrough?(text: string): string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function c(theme: WidgetTheme | null | undefined, token: string, text: string): string {
|
|
35
|
+
return theme?.fg ? theme.fg(token, text) : text;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Tree-spine glyphs (OMP's renderTreeList shape, 1-column cells).
|
|
39
|
+
const SPINE_BRANCH = "├─ ";
|
|
40
|
+
const SPINE_LAST = "└─ ";
|
|
41
|
+
const SPINE_VERT = "│ ";
|
|
42
|
+
const SPINE_BLANK = " ";
|
|
43
|
+
|
|
44
|
+
/** One nested child row's spine prefix (1-space gutter + 3-cell indent under
|
|
45
|
+
* the group head — OMP's tree geometry). */
|
|
46
|
+
const childSpine = (last: boolean): string => " " + SPINE_BLANK + (last ? SPINE_LAST : SPINE_BRANCH);
|
|
47
|
+
|
|
48
|
+
/** Checked/unchecked box for a phase. */
|
|
49
|
+
function box(st: "pending" | "in_progress" | "done" | "blocked"): string {
|
|
50
|
+
return st === "done" ? "☑" : "☐";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One themed phase row. `spine` prefixes the row (tree connectors); `current`
|
|
54
|
+
* marks the in-progress phase (mdLink = OMP's blue "current"). */
|
|
55
|
+
function phaseRow(
|
|
56
|
+
p: TodoPhase,
|
|
57
|
+
phases: readonly TodoPhase[],
|
|
58
|
+
theme: WidgetTheme | null | undefined,
|
|
59
|
+
spine: string,
|
|
60
|
+
current: boolean,
|
|
61
|
+
width: number,
|
|
62
|
+
/** OMP: notes collapse to a `+n` marker — full text lives in the tool result. */
|
|
63
|
+
compact = false,
|
|
64
|
+
): string {
|
|
65
|
+
const st = effectiveStatus(p, phases);
|
|
66
|
+
const waiters = p.blockedBy.filter((dep) => phases.find((x) => x.id === dep)?.status !== "done");
|
|
67
|
+
const title = st === "done" && theme?.strikethrough ? theme.strikethrough(p.title) : p.title;
|
|
68
|
+
const noteCount = (p.notes ? 1 : 0) + (st === "blocked" && waiters.length > 0 ? 1 : 0);
|
|
69
|
+
const tail = compact
|
|
70
|
+
? ""
|
|
71
|
+
: (
|
|
72
|
+
[
|
|
73
|
+
st === "blocked" && waiters.length > 0 ? `blocked by ${waiters.join(", ")}` : undefined,
|
|
74
|
+
p.notes,
|
|
75
|
+
]
|
|
76
|
+
.filter(Boolean)
|
|
77
|
+
.join(" — ")
|
|
78
|
+
);
|
|
79
|
+
const line =
|
|
80
|
+
// OMP's progress path: the connector lights accent once its phase is done.
|
|
81
|
+
c(theme, st === "done" ? "accent" : "dim", spine) +
|
|
82
|
+
c(theme, st === "done" ? "success" : current ? "mdLink" : "dim", box(st)) +
|
|
83
|
+
" " +
|
|
84
|
+
c(theme, st === "done" ? "success" : current ? "mdLink" : st === "blocked" ? "warning" : "dim", title) +
|
|
85
|
+
(tail ? c(theme, "dim", ` (${tail})`) : "") +
|
|
86
|
+
(compact && noteCount > 0 ? c(theme, "dim", ` +${noteCount}`) : "");
|
|
87
|
+
return line.length > width ? truncateToWidth(line, width) : line;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The full board, one themed line per phase — the transcript renderResult
|
|
92
|
+
* surface and the all-done widget view. Same OMP visual language as the
|
|
93
|
+
* widget (tree spine, checkboxes); ids/notes ride the dim tails.
|
|
94
|
+
*/
|
|
95
|
+
export function renderTodoBoard(phases: readonly TodoPhase[], theme?: WidgetTheme | null, width = 100): string[] {
|
|
96
|
+
if (phases.length === 0) return [c(theme, "dim", "Todo list is empty.")];
|
|
97
|
+
const lines: string[] = [];
|
|
98
|
+
for (const [i, p] of phases.entries()) {
|
|
99
|
+
const last = i === phases.length - 1;
|
|
100
|
+
const spine = childSpine(last);
|
|
101
|
+
lines.push(phaseRow(p, phases, theme, spine, effectiveStatus(p, phases) === "in_progress", width));
|
|
102
|
+
}
|
|
103
|
+
return lines;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Phases shown around the active one in the HUD window. */
|
|
107
|
+
const WINDOW_OPEN = 5; // active phase + up to 4 following open phases (OMP's cap shape)
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The bounded HUD window (OMP look): `TODO` header, then the phase subtree —
|
|
111
|
+
* `└─ Tasks · done/total` with done phases KEPT visible (green strikethrough),
|
|
112
|
+
* the current phase in mdLink blue, pending muted, `+ n more phases` tail when
|
|
113
|
+
* truncated. An all-done list renders every phase (the closure view).
|
|
114
|
+
*/
|
|
115
|
+
export function renderTodoWidgetLines(phases: readonly TodoPhase[], theme?: WidgetTheme | null, width = 100): string[] {
|
|
116
|
+
if (phases.length === 0) return [];
|
|
117
|
+
const done = phases.filter((p) => p.status === "done").length;
|
|
118
|
+
const all = done === phases.length;
|
|
119
|
+
const header =
|
|
120
|
+
c(theme, "accent", theme?.bold ? theme.bold("TODO") : "TODO");
|
|
121
|
+
|
|
122
|
+
// Subtree head: OMP's single root elbowing into its children (`└─`), the
|
|
123
|
+
// connector accenting only when the whole path is complete. The group is
|
|
124
|
+
// the whole list — ceulen phases are flat, so the label is fixed.
|
|
125
|
+
const groupHead =
|
|
126
|
+
" " +
|
|
127
|
+
c(theme, all ? "accent" : "dim", SPINE_LAST) +
|
|
128
|
+
c(theme, "mdLink", theme?.bold ? theme.bold("Tasks") : "Tasks") +
|
|
129
|
+
c(theme, "dim", ` · ${done}/${phases.length}`) +
|
|
130
|
+
(!all && phases.some((p) => effectiveStatus(p, phases) === "blocked") ? c(theme, "warning", " · blocked") : "");
|
|
131
|
+
|
|
132
|
+
if (all) {
|
|
133
|
+
const rows = phases.map((p, i) =>
|
|
134
|
+
phaseRow(p, phases, theme, childSpine(i === phases.length - 1), false, width, true),
|
|
135
|
+
);
|
|
136
|
+
return [header, groupHead, ...rows];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// OMP parity: done rows STAY visible (green strikethrough). The window
|
|
140
|
+
// slides to show the active phase with context: one done row above it (the
|
|
141
|
+
// just-finished step) + the pending tail, capped at WINDOW_OPEN rows.
|
|
142
|
+
const activeIdx = phases.findIndex((p) => effectiveStatus(p, phases) === "in_progress");
|
|
143
|
+
const start = activeIdx === -1 ? 0 : Math.max(0, activeIdx - 1);
|
|
144
|
+
const windowed = phases.slice(start, start + WINDOW_OPEN);
|
|
145
|
+
const hidden = phases.length - windowed.length;
|
|
146
|
+
const rows = windowed.map((p, i) =>
|
|
147
|
+
phaseRow(p, phases, theme, childSpine(i === windowed.length - 1 && hidden === 0), effectiveStatus(p, phases) === "in_progress", width, true),
|
|
148
|
+
);
|
|
149
|
+
if (hidden > 0) rows.push(c(theme, "dim", `${childSpine(true)}+ ${hidden} more phase${hidden === 1 ? "" : "s"}`));
|
|
150
|
+
return [header, groupHead, ...rows];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
// Controller — owns the setWidget handle + the all-done linger timer
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
export interface WidgetUi {
|
|
158
|
+
mode?: string;
|
|
159
|
+
ui: ExtensionContext["ui"];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface TodoWidgetController {
|
|
163
|
+
/** Install/refresh/clear per current phases. `lingerSecs`:
|
|
164
|
+
* >0 = seconds the all-done board stays, 0 = clear instantly, -1 = never auto-clear.
|
|
165
|
+
* `render` reads through `getPhases` so re-renders always draw CURRENT state —
|
|
166
|
+
* a captured array would freeze the board at the install-time snapshot. */
|
|
167
|
+
sync(target: WidgetUi | undefined, phases: readonly TodoPhase[], lingerSecs: number, getPhases?: () => readonly TodoPhase[]): void;
|
|
168
|
+
/** Remove the widget + cancel the timer (stale ctx tolerated). */
|
|
169
|
+
clear(target?: WidgetUi): void;
|
|
170
|
+
dispose(): void;
|
|
171
|
+
/** Test seam: true while a linger timer is armed. */
|
|
172
|
+
lingerArmed(): boolean;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function createTodoWidgetController(): TodoWidgetController {
|
|
176
|
+
let widgetCtx: ExtensionContext["ui"] | null = null;
|
|
177
|
+
let requestWidgetRender: (() => void) | null = null;
|
|
178
|
+
let widgetTheme: WidgetTheme | null = null;
|
|
179
|
+
let lingerTimer: ReturnType<typeof setTimeout> | undefined;
|
|
180
|
+
let generation = 0;
|
|
181
|
+
/** Live view of the board — swapped by sync so the render closure never
|
|
182
|
+
* holds a stale snapshot. */
|
|
183
|
+
let current: readonly TodoPhase[] = [];
|
|
184
|
+
|
|
185
|
+
const stale = <T>(fn: () => T): T | undefined => {
|
|
186
|
+
try {
|
|
187
|
+
return fn();
|
|
188
|
+
} catch {
|
|
189
|
+
return undefined; // stale ctx after session replacement
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
function cancelLinger(): void {
|
|
194
|
+
generation += 1;
|
|
195
|
+
if (lingerTimer) {
|
|
196
|
+
clearTimeout(lingerTimer);
|
|
197
|
+
lingerTimer = undefined;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function removeWidget(): void {
|
|
202
|
+
if (widgetCtx) {
|
|
203
|
+
const ui = widgetCtx;
|
|
204
|
+
stale(() => ui.setWidget(TODO_WIDGET_KEY, undefined));
|
|
205
|
+
widgetCtx = null;
|
|
206
|
+
}
|
|
207
|
+
requestWidgetRender = null;
|
|
208
|
+
widgetTheme = null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
sync(target, phases, lingerSecs, getPhases) {
|
|
213
|
+
cancelLinger();
|
|
214
|
+
current = getPhases ? getPhases() : phases;
|
|
215
|
+
if (!target || target.mode !== "tui") return;
|
|
216
|
+
const { ui } = target;
|
|
217
|
+
if (phases.length === 0) {
|
|
218
|
+
removeWidget();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const all = phases.every((p) => p.status === "done");
|
|
222
|
+
if (all && lingerSecs === 0) {
|
|
223
|
+
removeWidget();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (widgetCtx) {
|
|
227
|
+
requestWidgetRender?.();
|
|
228
|
+
} else {
|
|
229
|
+
stale(() =>
|
|
230
|
+
ui.setWidget(TODO_WIDGET_KEY, (tui, theme) => {
|
|
231
|
+
widgetTheme = theme as unknown as WidgetTheme;
|
|
232
|
+
requestWidgetRender = () => tui.requestRender();
|
|
233
|
+
return {
|
|
234
|
+
render: (width: number) => renderTodoWidgetLines(current, widgetTheme, width),
|
|
235
|
+
invalidate: () => requestWidgetRender?.(),
|
|
236
|
+
dispose: () => {
|
|
237
|
+
widgetTheme = null;
|
|
238
|
+
requestWidgetRender = null;
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}),
|
|
242
|
+
);
|
|
243
|
+
widgetCtx = ui;
|
|
244
|
+
}
|
|
245
|
+
if (all && lingerSecs >= 0) {
|
|
246
|
+
const gen = generation;
|
|
247
|
+
lingerTimer = setTimeout(() => {
|
|
248
|
+
if (gen !== generation) return; // a newer sync canceled this arm
|
|
249
|
+
lingerTimer = undefined;
|
|
250
|
+
removeWidget();
|
|
251
|
+
}, lingerSecs * 1000);
|
|
252
|
+
lingerTimer.unref?.();
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
|
|
256
|
+
clear(target) {
|
|
257
|
+
cancelLinger();
|
|
258
|
+
if (target?.ui && target.ui !== widgetCtx) {
|
|
259
|
+
stale(() => target.ui.setWidget(TODO_WIDGET_KEY, undefined));
|
|
260
|
+
}
|
|
261
|
+
removeWidget();
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
dispose() {
|
|
265
|
+
cancelLinger();
|
|
266
|
+
removeWidget();
|
|
267
|
+
},
|
|
268
|
+
|
|
269
|
+
lingerArmed: () => lingerTimer !== undefined,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// todo settings — the `todo` section of the GLOBAL agent-dir settings.json.
|
|
2
|
+
// ponytail: deliberately global-only (no trusted-project overlay) — a cosmetic
|
|
3
|
+
// preference; add the overlay only if a project genuinely needs it.
|
|
4
|
+
//
|
|
5
|
+
// "todo": {
|
|
6
|
+
// "lingerSecs": 60 // seconds the all-done HUD stays up; 0 = instant clear, -1 = never
|
|
7
|
+
// }
|
|
8
|
+
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
10
|
+
import os from "node:os";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
|
|
13
|
+
export const DEFAULT_LINGER_SECS = 60;
|
|
14
|
+
|
|
15
|
+
/** Closed set for the /config row (plan: values 0 / 60 / 300 / 900 / -1). */
|
|
16
|
+
export const LINGER_VALUES = ["0", "60", "300", "900", "-1"] as const;
|
|
17
|
+
export type LingerValue = (typeof LINGER_VALUES)[number];
|
|
18
|
+
|
|
19
|
+
export const LINGER_DESCRIPTIONS: Record<LingerValue, string> = {
|
|
20
|
+
"0": "Instant — the HUD clears the moment the last phase completes",
|
|
21
|
+
"60": "1 minute (default)",
|
|
22
|
+
"300": "5 minutes",
|
|
23
|
+
"900": "15 minutes",
|
|
24
|
+
"-1": "Never auto-clears — /todo clear removes it",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function agentDir(): string {
|
|
28
|
+
return process.env.PI_CODING_AGENT_DIR || path.join(os.homedir(), ".pi", "agent");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function globalSettingsPath(): string {
|
|
32
|
+
return path.join(agentDir(), "settings.json");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Effective linger seconds: default ← global `todo.lingerSecs`. Unset/garbage
|
|
36
|
+
* → default. `allow` (the /config closed set) clamps the accepted values. */
|
|
37
|
+
export function readLingerSecs(): number {
|
|
38
|
+
const file = globalSettingsPath();
|
|
39
|
+
try {
|
|
40
|
+
if (!existsSync(file)) return DEFAULT_LINGER_SECS;
|
|
41
|
+
const raw = (JSON.parse(readFileSync(file, "utf8"))?.todo ?? {}) as { lingerSecs?: unknown };
|
|
42
|
+
const n = typeof raw.lingerSecs === "string" ? parseInt(raw.lingerSecs, 10) : raw.lingerSecs;
|
|
43
|
+
if (typeof n !== "number" || !Number.isFinite(n)) return DEFAULT_LINGER_SECS;
|
|
44
|
+
if (n === -1) return -1;
|
|
45
|
+
if (n < 0) return DEFAULT_LINGER_SECS;
|
|
46
|
+
// Out-of-set values are clamped into the closed set (nearest member).
|
|
47
|
+
const allowed = [...LINGER_VALUES.map(Number), DEFAULT_LINGER_SECS].sort((a, b) => a - b);
|
|
48
|
+
return allowed.reduce((best, v) => (Math.abs(v - n) < Math.abs(best - n) ? v : best));
|
|
49
|
+
} catch {
|
|
50
|
+
return DEFAULT_LINGER_SECS; // unreadable/corrupt → default
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Read-modify-write `todo.lingerSecs` into the GLOBAL settings.json (merge,
|
|
55
|
+
* never clobber; atomic tmp+rename; a corrupt file refuses to clobber).
|
|
56
|
+
* Returns the written path. Exported for tests. */
|
|
57
|
+
export function writeTodoSection(lingerSecs: number): string {
|
|
58
|
+
const file = globalSettingsPath();
|
|
59
|
+
let settings: Record<string, unknown>;
|
|
60
|
+
try {
|
|
61
|
+
settings = existsSync(file) ? (JSON.parse(readFileSync(file, "utf8")) as Record<string, unknown>) : {};
|
|
62
|
+
} catch {
|
|
63
|
+
throw new Error(`${file} is not valid JSON — fix or remove it before saving.`);
|
|
64
|
+
}
|
|
65
|
+
const s = (settings.todo ?? {}) as Record<string, unknown>;
|
|
66
|
+
s.lingerSecs = lingerSecs;
|
|
67
|
+
settings.todo = s;
|
|
68
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
69
|
+
const tmp = file + ".tmp";
|
|
70
|
+
writeFileSync(tmp, JSON.stringify(settings, null, 2) + "\n", { mode: 0o600 });
|
|
71
|
+
renameSync(tmp, file);
|
|
72
|
+
return file;
|
|
73
|
+
}
|