ceulen 0.7.0 → 0.9.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 +83 -0
- package/extensions/lib/registry.test.ts +67 -0
- package/extensions/lib/registry.ts +24 -7
- 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/advisor/index.ts +20 -2
- package/extensions/modules/advisor/lib/watcher.ts +3 -1
- package/extensions/modules/advisor/test/module.test.ts +64 -1
- package/extensions/modules/composer/test/module.test.ts +2 -2
- package/extensions/modules/config/index.ts +2 -0
- package/extensions/modules/config/test/renderTools.test.ts +1 -1
- package/extensions/modules/munin/configPanel.ts +5 -2
- package/extensions/modules/munin/test/configPanel.test.ts +27 -2
- package/extensions/modules/router/configPanel.ts +24 -10
- package/extensions/modules/router/lib/client.ts +6 -4
- package/extensions/modules/router/test/unit.test.ts +83 -2
- package/extensions/modules/rules/index.ts +120 -0
- package/extensions/modules/rules/lib/rules.ts +348 -0
- package/extensions/modules/rules/test/lib.test.ts +263 -0
- package/extensions/modules/rules/test/module.test.ts +222 -0
- package/extensions/modules/subagent/index.ts +19 -6
- package/extensions/modules/subagent/lib/routing.ts +11 -6
- package/extensions/modules/subagent/lib/security.ts +12 -1
- package/extensions/modules/subagent/test/routing.test.ts +174 -0
- package/extensions/modules/todo/configPanel.ts +55 -0
- package/extensions/modules/todo/index.ts +511 -0
- 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 +528 -0
- package/extensions/modules/usage/index.ts +11 -0
- package/extensions/modules/usage/test/session-accumulators.test.ts +85 -0
- package/extensions/modules/web/configPanel.ts +38 -13
- package/extensions/modules/web/lib/settings.ts +11 -2
- package/extensions/modules/web/test/configPanel.test.ts +67 -1
- package/extensions/modules/zai/configPanel.ts +5 -2
- package/extensions/modules/zai/lib/signing.ts +4 -1
- package/extensions/modules/zai/test/configPanel.test.ts +23 -0
- package/extensions/modules/zai/test/signing.test.ts +19 -0
- package/package.json +2 -2
- package/skills/a2a/SKILL.md +63 -0
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* todo module tests — the state machine, persistence rehydrate, and the
|
|
3
|
+
* footer indicator. Untyped harness stub: only the pi surface the module
|
|
4
|
+
* touches (usage/ponytail test tradition).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
|
|
10
|
+
import todoModule, {
|
|
11
|
+
TODO_ENTRY_TYPE,
|
|
12
|
+
TODO_STATUS_KEY,
|
|
13
|
+
applyTodo,
|
|
14
|
+
resolveTodoPhases,
|
|
15
|
+
statusLine,
|
|
16
|
+
type TodoPhase,
|
|
17
|
+
} from "../index.ts";
|
|
18
|
+
|
|
19
|
+
type Params = Parameters<typeof applyTodo>[1];
|
|
20
|
+
const op = (o: Record<string, unknown>) => o as unknown as Params;
|
|
21
|
+
|
|
22
|
+
/** Apply an action to a list and return the resulting phases (throws on error). */
|
|
23
|
+
function run(current: TodoPhase[], params: Record<string, unknown>): TodoPhase[] {
|
|
24
|
+
const r = applyTodo(current, op(params));
|
|
25
|
+
assert.deepEqual(r.errors, [], `unexpected errors: ${r.errors}`);
|
|
26
|
+
return r.phases;
|
|
27
|
+
}
|
|
28
|
+
function fail(current: TodoPhase[], params: Record<string, unknown>): string[] {
|
|
29
|
+
const r = applyTodo(current, op(params));
|
|
30
|
+
assert.ok(r.errors.length > 0, "expected an error");
|
|
31
|
+
assert.equal(r.changed, false);
|
|
32
|
+
return r.errors;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const seeded = () =>
|
|
36
|
+
run([], {
|
|
37
|
+
action: "init",
|
|
38
|
+
phases: [{ title: "Read the repo" }, { title: "Port the tool" }, { title: "Verify" }],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
function createPiHarness() {
|
|
42
|
+
const events = new Map<string, (event: unknown, ctx: unknown) => unknown>();
|
|
43
|
+
const commands = new Map<string, unknown>();
|
|
44
|
+
const tools = new Map<string, any>();
|
|
45
|
+
const entries: Array<{ customType: string; data: unknown }> = [];
|
|
46
|
+
let branchEntries: unknown[] = [];
|
|
47
|
+
const statusCalls: Array<[string, string | undefined]> = [];
|
|
48
|
+
const notifications: string[] = [];
|
|
49
|
+
|
|
50
|
+
const ui = {
|
|
51
|
+
notify: (m: string) => { notifications.push(m); },
|
|
52
|
+
setStatus: (k: string, t: string | undefined) => { statusCalls.push([k, t]); },
|
|
53
|
+
};
|
|
54
|
+
const ctx = { ui, sessionManager: { getBranch: () => branchEntries } };
|
|
55
|
+
|
|
56
|
+
const pi = {
|
|
57
|
+
on: (n: string, h: any) => { events.set(n, h); },
|
|
58
|
+
registerTool: (t: any) => { tools.set(t.name, t); },
|
|
59
|
+
registerCommand: (n: string, o: any) => { commands.set(n, o); },
|
|
60
|
+
appendEntry: (customType: string, data: unknown) => { entries.push({ customType, data }); },
|
|
61
|
+
};
|
|
62
|
+
todoModule(pi as never);
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
events, commands, tools, entries, statusCalls, notifications, ui, ctx,
|
|
66
|
+
setBranch: (e: unknown[]) => { branchEntries = e; },
|
|
67
|
+
/** Fire the tool with params through the real execute() path. */
|
|
68
|
+
call: (params: Record<string, unknown>) => tools.get("todo").execute("call-1", op(params), undefined, undefined, ctx),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
describe("todo state machine", () => {
|
|
73
|
+
it("init assigns stable ids, replaces the whole list, and auto-starts the first phase", () => {
|
|
74
|
+
const phases = seeded();
|
|
75
|
+
assert.deepEqual(phases.map((p) => [p.id, p.status]), [["p1", "in_progress"], ["p2", "pending"], ["p3", "pending"]]);
|
|
76
|
+
const replaced = run(phases, { action: "init", phases: [{ id: "a", title: "Only" }] });
|
|
77
|
+
assert.deepEqual(replaced.map((p) => p.id), ["a"]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("start marks in_progress; a second start ERRORS while one is running", () => {
|
|
81
|
+
// seeded() already auto-started p1 — p2 must be forced.
|
|
82
|
+
assert.match(fail(seeded(), { action: "start", id: "p2" })[0], /already in_progress/);
|
|
83
|
+
const forced = run(seeded(), { action: "start", id: "p2", force: true });
|
|
84
|
+
assert.deepEqual(forced.map((p) => p.status), ["pending", "in_progress", "pending"]);
|
|
85
|
+
// An explicit start that finishes the running phase advances the pointer by itself.
|
|
86
|
+
assert.equal(run(seeded(), { action: "done", id: "p1" })[1].status, "in_progress");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("done marks done (idempotent) and never reopens a finished phase", () => {
|
|
90
|
+
const done = run(run(seeded(), { action: "done", id: "p1" }), { action: "done", id: "p3" });
|
|
91
|
+
// p2 auto-started when p1 completed; the out-of-order done of p3 leaves it running.
|
|
92
|
+
assert.deepEqual(done.map((p) => p.status), ["done", "in_progress", "done"]);
|
|
93
|
+
assert.equal(run(done, { action: "done", id: "p3" })[2].status, "done", "done of already-done stays done");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("block/unblock set blockedBy; a blocked phase is skipped by the pointer", () => {
|
|
97
|
+
// p2 waits on p3 — a blocker that stays UNMET after p1 completes.
|
|
98
|
+
const blocked = run(seeded(), { action: "block", id: "p2", blockedBy: ["p3"] });
|
|
99
|
+
assert.deepEqual(blocked[1].blockedBy, ["p3"]);
|
|
100
|
+
|
|
101
|
+
// p1 completes → the pointer must SKIP blocked p2 and land on p3.
|
|
102
|
+
const advanced = run(blocked, { action: "done", id: "p1" });
|
|
103
|
+
assert.deepEqual(advanced.map((p) => p.status), ["done", "pending", "in_progress"]);
|
|
104
|
+
assert.equal(statusLine(advanced), "▸ 1/3 done · in_progress: Verify · 1 blocked");
|
|
105
|
+
|
|
106
|
+
// A met blocker unblocks the phase — but never steals a running pointer.
|
|
107
|
+
const unblocked = run(blocked, { action: "unblock", id: "p2" });
|
|
108
|
+
assert.deepEqual(unblocked[1].blockedBy, []);
|
|
109
|
+
const partial = run(run(blocked, { action: "block", id: "p2", blockedBy: ["p1"] }), { action: "unblock", id: "p2", blockedBy: ["p3"] });
|
|
110
|
+
assert.deepEqual(partial[1].blockedBy, ["p1"], "unblock drops only the named edge");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("append adds one phase with a fresh id and rejects duplicates", () => {
|
|
114
|
+
const appended = run(seeded(), { action: "append", phases: [{ title: "Ship it" }] });
|
|
115
|
+
assert.deepEqual(appended.map((p) => p.id), ["p1", "p2", "p3", "p4"]);
|
|
116
|
+
assert.match(fail(appended, { action: "append", phases: [{ title: "Ship it" }] })[0], /already exists/);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("rm removes a phase (and its dangling blocker edges), or clears the list", () => {
|
|
120
|
+
const blocked = run(seeded(), { action: "block", id: "p3", blockedBy: ["p2"] });
|
|
121
|
+
const removed = run(blocked, { action: "rm", id: "p2" });
|
|
122
|
+
assert.deepEqual(removed.map((p) => p.id), ["p1", "p3"]);
|
|
123
|
+
assert.deepEqual(removed[1].blockedBy, [], "the removed phase is no longer a blocker");
|
|
124
|
+
assert.deepEqual(run(seeded(), { action: "rm" }), [], "rm without an id clears");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("errors: unknown phase, block with no blocker, unknown blocker, self-block, duplicate ids", () => {
|
|
128
|
+
const phases = seeded();
|
|
129
|
+
assert.match(fail(phases, { action: "start", id: "nope" })[0], /not found/);
|
|
130
|
+
assert.match(fail(phases, { action: "start" })[0], /Missing phase id/);
|
|
131
|
+
assert.match(fail(phases, { action: "block", id: "p1" })[0], /needs blockedBy/);
|
|
132
|
+
assert.match(fail(phases, { action: "block", id: "p1", blockedBy: ["ghost"] })[0], /not a phase in this list/);
|
|
133
|
+
assert.match(fail(phases, { action: "block", id: "p1", blockedBy: ["p1"] })[0], /cannot block itself/);
|
|
134
|
+
assert.match(fail(phases, { action: "init", phases: [{ id: "x", title: "A" }, { id: "x", title: "B" }] })[0], /Duplicate phase id/);
|
|
135
|
+
assert.match(fail(phases, { action: "init", phases: [{ title: "A", blockedBy: ["zz"] }] })[0], /unknown phase "zz"/);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("view never changes the list", () => {
|
|
139
|
+
const phases = seeded();
|
|
140
|
+
const r = applyTodo(phases, op({ action: "view" }));
|
|
141
|
+
assert.deepEqual(r.errors, []);
|
|
142
|
+
assert.equal(r.changed, false);
|
|
143
|
+
assert.deepEqual(r.phases, phases);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("status indicator text", () => {
|
|
148
|
+
it("counts done/total, names the running phase, and clears when done", () => {
|
|
149
|
+
assert.equal(statusLine([]), undefined, "empty list → cleared");
|
|
150
|
+
assert.equal(statusLine(seeded()), "▸ 0/3 done · in_progress: Read the repo");
|
|
151
|
+
const allDone = run(run(run(seeded(), { action: "done", id: "p1" }), { action: "done", id: "p2" }), { action: "done", id: "p3" });
|
|
152
|
+
assert.equal(statusLine(allDone), undefined, "all done → cleared");
|
|
153
|
+
// A list whose remaining phases are all blocked names no pointer.
|
|
154
|
+
const blocked = run(run(seeded(), { action: "block", id: "p1", blockedBy: ["p2"] }), { action: "block", id: "p2", blockedBy: ["p3"] });
|
|
155
|
+
assert.equal(statusLine(blocked), "▸ 0/3 done · next: Verify · 2 blocked");
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("module wiring", () => {
|
|
160
|
+
it("registers one tool and one command", () => {
|
|
161
|
+
const h = createPiHarness();
|
|
162
|
+
assert.deepEqual([...h.tools.keys()], ["todo"]);
|
|
163
|
+
assert.deepEqual([...h.commands.keys()], ["todo"]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("persists to ceulen-todo on every mutation, never on view", async () => {
|
|
167
|
+
const h = createPiHarness();
|
|
168
|
+
await h.call({ action: "init", phases: [{ title: "One" }, { title: "Two" }] });
|
|
169
|
+
await h.call({ action: "view" });
|
|
170
|
+
await h.call({ action: "done", id: "p1" });
|
|
171
|
+
assert.deepEqual(h.entries.map((e) => e.customType), [TODO_ENTRY_TYPE, TODO_ENTRY_TYPE]);
|
|
172
|
+
assert.deepEqual((h.entries[1].data as any).phases.map((p: TodoPhase) => p.status), ["done", "in_progress"]);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("a failed op keeps the previous state and reports it as an error", async () => {
|
|
176
|
+
const h = createPiHarness();
|
|
177
|
+
await h.call({ action: "init", phases: [{ title: "One" }] });
|
|
178
|
+
const before = h.entries.length;
|
|
179
|
+
const r = await h.call({ action: "start", id: "ghost" });
|
|
180
|
+
assert.equal(r.isError, true);
|
|
181
|
+
assert.match(r.content[0].text, /not found/);
|
|
182
|
+
assert.match(r.content[0].text, /\[p1\] One/, "the board stays at the previous state");
|
|
183
|
+
assert.equal(h.entries.length, before, "no persistence on error");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("mutating results restate the batch contract; a view does not", async () => {
|
|
187
|
+
const h = createPiHarness();
|
|
188
|
+
const init = await h.call({ action: "init", phases: [{ title: "One" }] });
|
|
189
|
+
assert.match(init.content[0].text, /never call todo alone/);
|
|
190
|
+
assert.match(init.content[0].text, /1\. ▸ \[p1\] One/, "the board renders glyph + id + title");
|
|
191
|
+
const view = await h.call({ action: "view" });
|
|
192
|
+
assert.doesNotMatch(view.content[0].text, /never call todo alone/);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("the tool carries the batch contract as guidelines", () => {
|
|
196
|
+
const h = createPiHarness();
|
|
197
|
+
const guidelines: string[] = h.tools.get("todo").promptGuidelines;
|
|
198
|
+
assert.ok(guidelines.some((g) => /NEVER call todo alone/.test(g)));
|
|
199
|
+
assert.ok(guidelines.some((g) => /init alongside the FIRST work call/.test(g)));
|
|
200
|
+
assert.ok(guidelines.some((g) => /SAME call that starts the next action/.test(g)));
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("sets the footer indicator on mutation and clears it on shutdown", async () => {
|
|
204
|
+
const h = createPiHarness();
|
|
205
|
+
await h.call({ action: "init", phases: [{ title: "One" }] });
|
|
206
|
+
assert.deepEqual(h.statusCalls.at(-1), [TODO_STATUS_KEY, "▸ 0/1 done · in_progress: One"]);
|
|
207
|
+
await h.call({ action: "done", id: "p1" });
|
|
208
|
+
assert.deepEqual(h.statusCalls.at(-1), [TODO_STATUS_KEY, undefined], "all done → cleared");
|
|
209
|
+
await h.events.get("session_shutdown")!({}, h.ctx);
|
|
210
|
+
assert.deepEqual(h.statusCalls.at(-1), [TODO_STATUS_KEY, undefined]);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("rehydrates the LAST ceulen-todo entry on session_start", async () => {
|
|
214
|
+
const h = createPiHarness();
|
|
215
|
+
h.setBranch([
|
|
216
|
+
{ type: "message" },
|
|
217
|
+
{ type: "custom", customType: TODO_ENTRY_TYPE, data: { phases: [{ id: "old", title: "Stale", status: "pending", blockedBy: [] }] } },
|
|
218
|
+
{ type: "custom", customType: "other-entry", data: {} },
|
|
219
|
+
{ type: "custom", customType: TODO_ENTRY_TYPE, data: { phases: [{ id: "a", title: "Resumed", status: "in_progress", blockedBy: [] }] } },
|
|
220
|
+
]);
|
|
221
|
+
await h.events.get("session_start")!({}, h.ctx);
|
|
222
|
+
assert.deepEqual(h.statusCalls.at(-1), [TODO_STATUS_KEY, "▸ 0/1 done · in_progress: Resumed"]);
|
|
223
|
+
await h.call({ action: "done", id: "a" });
|
|
224
|
+
assert.deepEqual((h.entries.at(-1)!.data as any).phases[0].title, "Resumed");
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("resolveTodoPhases ignores malformed entries and unknown customTypes", () => {
|
|
228
|
+
assert.deepEqual(resolveTodoPhases(undefined), []);
|
|
229
|
+
assert.deepEqual(resolveTodoPhases([{ type: "custom", customType: "other", data: { phases: [{ id: "x", title: "y", status: "pending", blockedBy: [] }] } }]), []);
|
|
230
|
+
// Junk entries without a phases array are skipped, not fatal.
|
|
231
|
+
assert.deepEqual(
|
|
232
|
+
resolveTodoPhases([{ type: "custom", customType: TODO_ENTRY_TYPE, data: {} }, { type: "custom", customType: TODO_ENTRY_TYPE, data: { phases: [{ id: "a", title: "Good", status: "pending", blockedBy: [], junk: 1 }] } }]).map((p) => p.id),
|
|
233
|
+
["a"],
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe("/todo command", () => {
|
|
239
|
+
it("bare prints the board, unknown warns, clear resets", async () => {
|
|
240
|
+
const h = createPiHarness();
|
|
241
|
+
await h.call({ action: "init", phases: [{ title: "One" }] });
|
|
242
|
+
const cmd = h.commands.get("todo") as any;
|
|
243
|
+
await cmd.handler("", h.ctx);
|
|
244
|
+
assert.match(h.notifications.at(-1)!, /\[p1\] One/);
|
|
245
|
+
await cmd.handler("bogus", h.ctx);
|
|
246
|
+
assert.match(h.notifications.at(-1)!, /Unknown subcommand/);
|
|
247
|
+
await cmd.handler("clear", h.ctx);
|
|
248
|
+
assert.equal(h.notifications.at(-1), "Todo list cleared.");
|
|
249
|
+
await cmd.handler("", h.ctx);
|
|
250
|
+
assert.equal(h.notifications.at(-1), "Todo list is empty.");
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
// ---------------------------------------------------------------------------
|
|
255
|
+
// Themed renderers + HUD widget (lib/render.ts)
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
|
|
258
|
+
import { createTodoWidgetController, renderTodoBoard, renderTodoWidgetLines, type WidgetTheme } from "../lib/render.ts";
|
|
259
|
+
|
|
260
|
+
/** Recording theme shim — asserts colors by token, keeps text readable. */
|
|
261
|
+
function recTheme(): WidgetTheme & { tokens: string[] } {
|
|
262
|
+
const tokens: string[] = [];
|
|
263
|
+
return {
|
|
264
|
+
tokens,
|
|
265
|
+
fg: (color: string, text: string) => {
|
|
266
|
+
tokens.push(color);
|
|
267
|
+
return `<${color}>${text}</>`;
|
|
268
|
+
},
|
|
269
|
+
bold: (text: string) => `**${text}**`,
|
|
270
|
+
strikethrough: (text: string) => `~~${text}~~`,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const board = () => run([], { action: "init", phases: [{ title: "Read the repo" }, { title: "Port the tool" }, { title: "Verify" }] });
|
|
275
|
+
|
|
276
|
+
describe("renderTodoBoard (OMP look)", () => {
|
|
277
|
+
it("colors by status: done success+strikethrough, current mdLink, pending muted, blocked warning", () => {
|
|
278
|
+
const t = recTheme();
|
|
279
|
+
const phases = board();
|
|
280
|
+
mark(phases, "p1", "done");
|
|
281
|
+
mark(phases, "p2", "in_progress");
|
|
282
|
+
mark(phases, "p3", "blocked", ["p2"]);
|
|
283
|
+
const lines = renderTodoBoard(phases, t);
|
|
284
|
+
// Nested-tree geometry (OMP): rows indent under an implied head with their
|
|
285
|
+
// own connectors; a done row's connector lights accent (the progress path).
|
|
286
|
+
assert.match(lines[0]!, /<accent> ├─ <\/><success>☑<\/> <success>~~Read the repo~~<\/>/);
|
|
287
|
+
assert.match(lines[1]!, /<dim> ├─ <\/><mdLink>☐<\/> <mdLink>Port the tool<\/>/);
|
|
288
|
+
assert.match(lines[2]!, /<dim> └─ <\/><dim>☐<\/> <warning>Verify<\/><dim> \(blocked by p2\)<\/>/);
|
|
289
|
+
assert.ok(t.tokens.includes("success") && t.tokens.includes("mdLink") && t.tokens.includes("warning"));
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("done rows STAY VISIBLE with success + strikethrough (OMP parity — no omission in the full board)", () => {
|
|
293
|
+
const t = recTheme();
|
|
294
|
+
const phases = board();
|
|
295
|
+
mark(phases, "p1", "done");
|
|
296
|
+
const lines = renderTodoBoard(phases, t);
|
|
297
|
+
assert.equal(lines.length, 3);
|
|
298
|
+
assert.match(lines[0]!, /~~Read the repo~~/);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it("blocked tail lists unmet blockers only, notes ride the dim tail", () => {
|
|
302
|
+
const t = recTheme();
|
|
303
|
+
const phases = run([], { action: "init", phases: [{ id: "a", title: "One", notes: "check npm test" }, { id: "b", title: "Two", blockedBy: ["a"] }] });
|
|
304
|
+
const lines = renderTodoBoard(phases, t);
|
|
305
|
+
assert.match(lines[1]!, /\(blocked by a\)/);
|
|
306
|
+
assert.match(lines[0]!, /\(check npm test\)/);
|
|
307
|
+
// a satisfied → the dependent shows open (unmet-only rule)
|
|
308
|
+
mark(phases, "a", "done");
|
|
309
|
+
assert.match(renderTodoBoard(phases, t)[1]!, /<dim>Two<\/>$/);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("without a theme the board is plain text (no ANSI)", () => {
|
|
313
|
+
const lines = renderTodoBoard(board());
|
|
314
|
+
for (const line of lines) assert.ok(!line.includes("\u001b") && !line.includes("<"), line);
|
|
315
|
+
assert.match(lines[0]!, / ├─ ☐ Read the repo/);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("long titles truncate to width", () => {
|
|
319
|
+
const long = "x".repeat(200);
|
|
320
|
+
const phases = run([], { action: "init", phases: [{ title: long }] });
|
|
321
|
+
const lines = renderTodoBoard(phases, undefined, 40);
|
|
322
|
+
assert.ok(lines[0]!.length <= 40 + 40, `len=${lines[0]!.length}`);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
describe("renderTodoWidgetLines (OMP look)", () => {
|
|
327
|
+
it("header TODO + group head Tasks · done/total; done rows stay, current mdLink", () => {
|
|
328
|
+
const t = recTheme();
|
|
329
|
+
const phases = board();
|
|
330
|
+
mark(phases, "p1", "done");
|
|
331
|
+
mark(phases, "p2", "in_progress");
|
|
332
|
+
const lines = renderTodoWidgetLines(phases, t);
|
|
333
|
+
assert.match(lines[0]!, /<accent>\*\*TODO\*\*<\/>/);
|
|
334
|
+
assert.match(lines[1]!, /^ <dim>└─ <\/><mdLink>\*\*Tasks\*\*<\/><dim> · 1\/3<\/>/);
|
|
335
|
+
// Done rows stay visible (OMP parity), current phase drawn in mdLink.
|
|
336
|
+
assert.match(lines[2]!, /<success>☑<\/> <success>~~Read the repo~~<\/>/);
|
|
337
|
+
assert.match(lines[3]!, /<mdLink>☐<\/> <mdLink>Port the tool<\/>/);
|
|
338
|
+
assert.match(lines[4]!, /<dim>Verify<\/>/);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("caps the window at 5 open phases with a + n more tail", () => {
|
|
342
|
+
const phases = run([], { action: "init", phases: Array.from({ length: 8 }, (_, i) => ({ title: `Phase ${i + 1}` })) });
|
|
343
|
+
const lines = renderTodoWidgetLines(phases, recTheme());
|
|
344
|
+
const body = lines.filter((l) => /☐/.test(l));
|
|
345
|
+
assert.equal(body.length, 5);
|
|
346
|
+
assert.match(lines.at(-1)!, /\+ 3 more phases/);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("done phases auto-unblock dependents (real blockedBy edge, not the title)", () => {
|
|
350
|
+
// Regression: a drill init once put "blocked by p5" only in the TITLE —
|
|
351
|
+
// the dependent then auto-started while its blocker was still pending.
|
|
352
|
+
const r = run([], {
|
|
353
|
+
action: "init",
|
|
354
|
+
phases: [
|
|
355
|
+
{ title: "A" },
|
|
356
|
+
{ title: "B", blockedBy: ["p1"] },
|
|
357
|
+
],
|
|
358
|
+
});
|
|
359
|
+
assert.ok(r[1]!.status === "pending", "dependent must not auto-start");
|
|
360
|
+
assert.equal(r[1]!.blockedBy[0], "p1");
|
|
361
|
+
const after = run(r, { action: "done", id: "p1" });
|
|
362
|
+
assert.ok(after[1]!.status === "in_progress", "finishing the blocker auto-starts the dependent");
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("all-done renders the closure view: every phase checked green, counts on the group head", () => {
|
|
366
|
+
const t = recTheme();
|
|
367
|
+
const phases = board();
|
|
368
|
+
for (const p of phases) p.status = "done";
|
|
369
|
+
const lines = renderTodoWidgetLines(phases, t);
|
|
370
|
+
assert.match(lines[1]!, /<dim> · 3\/3<\/>/);
|
|
371
|
+
// All-done: the whole path is lit (head + connectors accent) — OMP's
|
|
372
|
+
// completed-tree look from the user's screenshot.
|
|
373
|
+
assert.match(lines[1]!, /^ <accent>└─ <\/>/);
|
|
374
|
+
assert.match(lines[2]!, /<accent> ├─ <\/><success>☑<\/>/);
|
|
375
|
+
assert.equal(lines.filter((l) => /☑/.test(l)).length, 3);
|
|
376
|
+
assert.ok(lines.every((l) => !l.includes("▸ ")), "no footer fraction line (counts moved to the head)");
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("empty list renders nothing", () => {
|
|
380
|
+
assert.deepEqual(renderTodoWidgetLines([], recTheme()), []);
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
describe("todo HUD widget controller", () => {
|
|
385
|
+
interface WidgetCall { content: unknown }
|
|
386
|
+
function harness() {
|
|
387
|
+
const widgets: WidgetCall[] = [];
|
|
388
|
+
const ui: any = {
|
|
389
|
+
setWidget: (key: string, content: unknown) => widgets.push({ content }),
|
|
390
|
+
setStatus: () => {},
|
|
391
|
+
};
|
|
392
|
+
const ctx = { mode: "tui", ui };
|
|
393
|
+
const ctl = createTodoWidgetController();
|
|
394
|
+
return { widgets, ui, ctx, ctl };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
it("installs on first open work, re-renders on change, headless is a no-op", () => {
|
|
398
|
+
const h = harness();
|
|
399
|
+
const live = board();
|
|
400
|
+
h.ctl.sync(h.ctx, live, 60, () => live);
|
|
401
|
+
assert.equal(h.widgets.length, 1, "widget installed once");
|
|
402
|
+
const factory = h.widgets[0]!.content as any;
|
|
403
|
+
assert.equal(typeof factory, "function", "component-factory form (theme-aware)");
|
|
404
|
+
// Re-render through the factory: captures theme, renders lines.
|
|
405
|
+
let renders = 0;
|
|
406
|
+
const comp = factory({ requestRender: () => renders++ }, recTheme());
|
|
407
|
+
const lines: string[] = comp.render(120);
|
|
408
|
+
assert.match(lines[0]!, /TODO/);
|
|
409
|
+
h.ctl.sync(h.ctx, live, 60, () => live);
|
|
410
|
+
assert.equal(h.widgets.length, 1, "already installed → requestRender, not re-set");
|
|
411
|
+
assert.ok(renders >= 1);
|
|
412
|
+
// Headless: nothing installs.
|
|
413
|
+
const headless = harness();
|
|
414
|
+
headless.ctl.sync({ mode: "rpc", ui: headless.ui } as any, board(), 60);
|
|
415
|
+
assert.equal(headless.widgets.length, 0);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it("re-renders draw CURRENT state, not the install-time snapshot (live-caught stale closure)", () => {
|
|
419
|
+
const h = harness();
|
|
420
|
+
const live = board(); // 3 phases, p1 auto-started
|
|
421
|
+
h.ctl.sync(h.ctx, live, 60, () => live);
|
|
422
|
+
const comp = (h.widgets[0]!.content as any)({ requestRender: () => {} }, recTheme());
|
|
423
|
+
assert.match(comp.render(120).join("\n"), /\*\*Tasks\*\*<\/><dim> · 0\/3<\/>/);
|
|
424
|
+
assert.ok(!/· 3\/3/.test(comp.render(120).join("\n")), "open board while work remains");
|
|
425
|
+
// Mutate the SAME array (module state) and re-sync: render must reflect it.
|
|
426
|
+
for (const p of live) p.status = "done";
|
|
427
|
+
h.ctl.sync(h.ctx, live, 60, () => live);
|
|
428
|
+
const after = comp.render(120).join("\n");
|
|
429
|
+
assert.match(after, /Tasks[^\n]*3\/3/, "group-head counts update after mutation");
|
|
430
|
+
assert.equal((after.match(/☑/g) ?? []).length, 3, "all rows checked in the completion view");
|
|
431
|
+
// Also without an explicit getPhases: sync swaps the internal view.
|
|
432
|
+
const h2 = harness();
|
|
433
|
+
const first = board();
|
|
434
|
+
h2.ctl.sync(h2.ctx, first, 60);
|
|
435
|
+
const comp2 = (h2.widgets[0]!.content as any)({ requestRender: () => {} }, recTheme());
|
|
436
|
+
const done = board();
|
|
437
|
+
for (const p of done) p.status = "done";
|
|
438
|
+
h2.ctl.sync(h2.ctx, done, 60);
|
|
439
|
+
assert.match(comp2.render(120).join("\n"), /· 3\/3/);
|
|
440
|
+
assert.equal((comp2.render(120).join("\n").match(/☑/g) ?? []).length, 3);
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("all-done with linger 0 clears the widget instantly; -1 never arms the timer", () => {
|
|
444
|
+
const done = () => { const p = board(); for (const x of p) x.status = "done"; return p; };
|
|
445
|
+
const instant = harness();
|
|
446
|
+
instant.ctl.sync(instant.ctx, board(), 60);
|
|
447
|
+
instant.ctl.sync(instant.ctx, done(), 0);
|
|
448
|
+
assert.equal((instant.widgets.at(-1)!.content as any), undefined, "linger 0 → removed");
|
|
449
|
+
assert.equal(instant.ctl.lingerArmed(), false);
|
|
450
|
+
|
|
451
|
+
const never = harness();
|
|
452
|
+
never.ctl.sync(never.ctx, board(), 60);
|
|
453
|
+
never.ctl.sync(never.ctx, done(), -1);
|
|
454
|
+
assert.equal(never.widgets.length, 1, "linger -1 → widget stays");
|
|
455
|
+
assert.equal(never.ctl.lingerArmed(), false, "no timer armed");
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it("a new mutation after all-done cancels the pending linger (generation guard)", () => {
|
|
459
|
+
const done = () => { const p = board(); for (const x of p) x.status = "done"; return p; };
|
|
460
|
+
const h = harness();
|
|
461
|
+
h.ctl.sync(h.ctx, done(), 1); // arms a 1s timer
|
|
462
|
+
assert.ok(h.ctl.lingerArmed(), "timer armed");
|
|
463
|
+
h.ctl.sync(h.ctx, board(), 60); // new work before it fires
|
|
464
|
+
assert.equal(h.ctl.lingerArmed(), false, "timer canceled by newer sync");
|
|
465
|
+
assert.equal(h.widgets.length, 1, "widget re-used, not re-installed");
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
it("clear/dispose remove the widget; clear tolerates an unknown ui (stale ctx)", () => {
|
|
469
|
+
const h = harness();
|
|
470
|
+
h.ctl.sync(h.ctx, board(), 60);
|
|
471
|
+
h.ctl.clear({ mode: "tui", ui: { setWidget: () => {} } as any });
|
|
472
|
+
assert.equal((h.widgets.at(-1)!.content as any), undefined);
|
|
473
|
+
h.ctl.sync(h.ctx, board(), 60);
|
|
474
|
+
h.ctl.dispose();
|
|
475
|
+
assert.equal((h.widgets.at(-1)!.content as any), undefined);
|
|
476
|
+
});
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
// helpers used above
|
|
480
|
+
function mark(phases: TodoPhase[], id: string, status: TodoPhase["status"], blockedBy?: string[]): void {
|
|
481
|
+
const hit = phases.find((p) => p.id === id)!;
|
|
482
|
+
hit.status = status;
|
|
483
|
+
if (blockedBy) hit.blockedBy = blockedBy;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
describe("tool renderResult + config row", () => {
|
|
487
|
+
it("renderResult draws the colored board from details.phases; falls back to the text blob", async () => {
|
|
488
|
+
const h = createPiHarness();
|
|
489
|
+
const tool = h.tools.get("todo");
|
|
490
|
+
const theme = recTheme();
|
|
491
|
+
// A mutation result carries phases in details → themed board.
|
|
492
|
+
await h.call({ action: "init", phases: [{ title: "One" }] });
|
|
493
|
+
const component = tool.renderResult(
|
|
494
|
+
{ content: [{ type: "text", text: "board" }], details: { phases: run([], { action: "init", phases: [{ title: "One" }] }) } },
|
|
495
|
+
{ expanded: true, isPartial: false },
|
|
496
|
+
theme,
|
|
497
|
+
{} as never,
|
|
498
|
+
);
|
|
499
|
+
assert.match(component.render(120).join("\n"), /<mdLink>☐<\/> <mdLink>One<\/>/);
|
|
500
|
+
// No phases (error path with junk details) → plain text passthrough.
|
|
501
|
+
const fallback = tool.renderResult(
|
|
502
|
+
{ content: [{ type: "text", text: "✗ boom" }], details: {} },
|
|
503
|
+
{ expanded: true, isPartial: false },
|
|
504
|
+
theme,
|
|
505
|
+
{} as never,
|
|
506
|
+
);
|
|
507
|
+
assert.match(fallback.render(120).join("\n"), /^✗ boom\s*$/);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it("the /config row writes todo.lingerSecs through writeTodoSection", async () => {
|
|
511
|
+
const { buildTodoGroups, todoConfig } = await import("../configPanel.ts");
|
|
512
|
+
const working = { value: "300" };
|
|
513
|
+
const groups = buildTodoGroups(working);
|
|
514
|
+
const row0 = groups[0]!.rows[0]!;
|
|
515
|
+
assert.equal(row0.key, "todo.lingerSecs");
|
|
516
|
+
assert.equal(row0.defaultValue, "60");
|
|
517
|
+
// Menu is a closed set incl. Never.
|
|
518
|
+
const menu = (row0 as any).menu();
|
|
519
|
+
assert.deepEqual(menu.map((m: any) => m.value), ["0", "60", "300", "900", "-1"]);
|
|
520
|
+
// set() mutates the working copy; save() persists via writeTodoSection.
|
|
521
|
+
row0.set("900");
|
|
522
|
+
assert.equal(working.value, "900");
|
|
523
|
+
// save() with no owned edited key is a no-op.
|
|
524
|
+
const saved: string[] = [];
|
|
525
|
+
await todoConfig().save(new Set(["other.key"]), { ui: { notify: (m: string) => saved.push(m) } } as never);
|
|
526
|
+
assert.equal(saved.length, 0);
|
|
527
|
+
});
|
|
528
|
+
});
|
|
@@ -1870,6 +1870,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
1870
1870
|
state.ctx = ctx;
|
|
1871
1871
|
resetGenRate(); // fresh session, fresh Generation Rate item
|
|
1872
1872
|
resetUsageItem(); // ...and fresh usage windows
|
|
1873
|
+
// The cumulative figures are labelled "Session cost"/"Session avg" in
|
|
1874
|
+
// /usage, so they must not survive into the next session (/new, reload,
|
|
1875
|
+
// switch, fork). session_start fires before the new session's first
|
|
1876
|
+
// message_end, so an in-flight review of the old session cannot re-add
|
|
1877
|
+
// into these (its message_end precedes this handler).
|
|
1878
|
+
state.cumulativeCost = 0;
|
|
1879
|
+
state.cumulativeOutput = 0;
|
|
1880
|
+
state.cumulativeDurationMs = 0;
|
|
1881
|
+
state.responseStartPerf = undefined; // a request spanning the switch has no valid elapsed
|
|
1882
|
+
state.lastTokPerSec = undefined;
|
|
1883
|
+
state.lastTokPerSecLabel = undefined;
|
|
1873
1884
|
updateActiveAdapter(state, ctx.model);
|
|
1874
1885
|
if (state.adapter) deferRefresh(state, true);
|
|
1875
1886
|
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session-scoped accumulators: /usage labels these figures "Session cost" and
|
|
3
|
+
* "Session avg", so they must not survive session_start (/new, reload, switch,
|
|
4
|
+
* fork) — before the fix they were created once at module load and kept
|
|
5
|
+
* accumulating, so the second session reported the first session's cost.
|
|
6
|
+
*
|
|
7
|
+
* Unsupported provider ("ollama") on purpose: no adapter, so no refresh timer
|
|
8
|
+
* is armed (no open handles) and renderSubscriptionLine takes the tok/s branch.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
import { test } from "node:test";
|
|
13
|
+
import extension, { renderSubscriptionLine, type State } from "../index.ts";
|
|
14
|
+
|
|
15
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
16
|
+
|
|
17
|
+
function harness() {
|
|
18
|
+
const handlers: Record<string, (event: any, ctx: any) => any> = {};
|
|
19
|
+
const statuses: (string | undefined)[] = [];
|
|
20
|
+
const pi = {
|
|
21
|
+
on(type: string, fn: any) {
|
|
22
|
+
handlers[type] = fn;
|
|
23
|
+
},
|
|
24
|
+
registerCommand() {},
|
|
25
|
+
registerMessageRenderer() {},
|
|
26
|
+
};
|
|
27
|
+
const ctx = {
|
|
28
|
+
model: { provider: "ollama", id: "llama" }, // unsupported → no adapter, no timer
|
|
29
|
+
isProjectTrusted: () => false,
|
|
30
|
+
ui: {
|
|
31
|
+
theme: { fg: (_c: string, s: string) => s },
|
|
32
|
+
setStatus(_key: string, value?: string) {
|
|
33
|
+
statuses.push(value);
|
|
34
|
+
},
|
|
35
|
+
notify() {},
|
|
36
|
+
},
|
|
37
|
+
getContextUsage: () => undefined,
|
|
38
|
+
};
|
|
39
|
+
const state = extension(pi as never) as State;
|
|
40
|
+
/** One assistant response: publishes tok/s and accumulates the session figures. */
|
|
41
|
+
const respond = async (cost: number) => {
|
|
42
|
+
await handlers.before_provider_request!({}, ctx);
|
|
43
|
+
await sleep(110); // clear the 100ms noise floor
|
|
44
|
+
await handlers.message_end!({ message: { role: "assistant", usage: { output: 1000, reasoning: 400, cost: { total: cost } } } }, ctx);
|
|
45
|
+
};
|
|
46
|
+
return { handlers, ctx, state, statuses, respond };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test("session_start resets the session cost/avg accumulators (a second session never inherits the first)", async () => {
|
|
50
|
+
const h = harness();
|
|
51
|
+
await h.handlers.session_start!({ reason: "startup" }, h.ctx);
|
|
52
|
+
assert.equal(h.state.cumulativeCost, 0, "fresh session starts at zero cost");
|
|
53
|
+
|
|
54
|
+
await h.respond(0.05);
|
|
55
|
+
assert.ok(h.state.cumulativeCost > 0, "cost accumulates within the session");
|
|
56
|
+
assert.ok(h.state.cumulativeOutput > 0 && h.state.cumulativeDurationMs > 0, "tok/s samples accumulate the session average");
|
|
57
|
+
assert.ok(h.state.lastTokPerSec !== undefined, "last response rate published");
|
|
58
|
+
|
|
59
|
+
// The /usage footer/session-avg surface reads these fields directly.
|
|
60
|
+
renderSubscriptionLine(h.state);
|
|
61
|
+
assert.match(String(h.statuses.at(-1)), /tok\/s/, "last response rate rendered before the switch");
|
|
62
|
+
|
|
63
|
+
// /new (or reload/switch/fork): the accumulators must reset with the session.
|
|
64
|
+
await h.handlers.session_start!({ reason: "new" }, h.ctx);
|
|
65
|
+
assert.equal(h.state.cumulativeCost, 0, "session cost reset — not the previous session's total");
|
|
66
|
+
assert.equal(h.state.cumulativeOutput, 0, "session output reset");
|
|
67
|
+
assert.equal(h.state.cumulativeDurationMs, 0, "session duration reset");
|
|
68
|
+
assert.equal(h.state.lastTokPerSec, undefined, "last-response rate cleared with the session");
|
|
69
|
+
assert.equal(h.state.lastTokPerSecLabel, undefined, "last-response label cleared too (it belongs to the old session)");
|
|
70
|
+
assert.equal(h.state.responseStartPerf, undefined, "no half-measured request carried across the switch");
|
|
71
|
+
|
|
72
|
+
renderSubscriptionLine(h.state);
|
|
73
|
+
assert.equal(h.state.cumulativeCost, 0);
|
|
74
|
+
const last = String(h.statuses.at(-1));
|
|
75
|
+
assert.ok(!/Session avg|tok\/s/.test(last), `session-scoped tok/s surfaces gone after the switch (got ${JSON.stringify(last)})`);
|
|
76
|
+
|
|
77
|
+
// Positive control: the reset is per SESSION, not per message — the new
|
|
78
|
+
// session accumulates its own figures normally. (No separate lifetime/aggregate
|
|
79
|
+
// counter exists in State, so the reset has nothing else to clobber: cost,
|
|
80
|
+
// output, duration and the last-response rate are all session-scoped.)
|
|
81
|
+
await h.respond(0.01);
|
|
82
|
+
assert.ok(h.state.cumulativeCost > 0 && h.state.cumulativeCost < 0.02, "the new session counts only its own cost");
|
|
83
|
+
await h.respond(0.01);
|
|
84
|
+
assert.ok(h.state.cumulativeCost > 0.015, "messages within one session still accumulate");
|
|
85
|
+
});
|