@andares/pi-toolkits 0.13.0 → 0.14.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.
@@ -0,0 +1,460 @@
1
+ /**
2
+ * whats-new feature — parser, version ordering, store and startup tests.
3
+ *
4
+ * Everything is deterministic: temp directories for state and fixtures, an
5
+ * injected version, and a fake `pi` that records entries instead of rendering
6
+ * them. The startup cases encode the product rule exactly — the notice fires on
7
+ * the first TUI start after an upgrade and never again for that version.
8
+ */
9
+ import {
10
+ existsSync,
11
+ mkdirSync,
12
+ mkdtempSync,
13
+ readFileSync,
14
+ rmSync,
15
+ writeFileSync,
16
+ } from "node:fs";
17
+ import { tmpdir } from "node:os";
18
+ import { join } from "node:path";
19
+ import { afterEach, describe, expect, it } from "vitest";
20
+ import type {
21
+ ExtensionAPI,
22
+ ExtensionContext,
23
+ SessionStartEvent,
24
+ } from "@earendil-works/pi-coding-agent";
25
+ import {
26
+ parseChangelog,
27
+ readPackageVersion,
28
+ releasesForUpgrade,
29
+ sortReleasesDescending,
30
+ } from "./changelog.js";
31
+ import { registerWhatsNew, type WhatsNewOptions } from "./index.js";
32
+ import {
33
+ buildUpgradeNotice,
34
+ renderReleaseSections,
35
+ } from "./notice.js";
36
+ import { WhatsNewStore, normalizeState } from "./store.js";
37
+ import { compareVersions, parseVersion } from "./version.js";
38
+
39
+ const CHANGELOG = `# Changelog
40
+
41
+ Intro prose that is not part of any release.
42
+
43
+ ## Unreleased
44
+
45
+ - work in progress
46
+
47
+ ## 0.13.1 — 2026-11-13
48
+
49
+ - fix one
50
+ - fix two
51
+ - nested detail is not a notice line
52
+ - fix three
53
+ - fix four
54
+ - fix five
55
+ - fix six
56
+ - fix seven
57
+
58
+ ## 0.13.0 — 2026-11-12
59
+
60
+ - plan feature
61
+
62
+ ## 0.12.0 — 2026-11-06
63
+
64
+ - old feature
65
+ `;
66
+
67
+ const dirs: string[] = [];
68
+
69
+ function tempDir(): string {
70
+ const dir = mkdtempSync(join(tmpdir(), "pi-whats-new-"));
71
+ dirs.push(dir);
72
+ return dir;
73
+ }
74
+
75
+ afterEach(() => {
76
+ for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
77
+ });
78
+
79
+ interface Fixture {
80
+ storeFile: string;
81
+ changelogFile: string;
82
+ packageFile: string;
83
+ dir: string;
84
+ }
85
+
86
+ function fixture(options: { version?: string; changelog?: string } = {}): Fixture {
87
+ const dir = tempDir();
88
+ const changelogFile = join(dir, "CHANGELOG.md");
89
+ const packageFile = join(dir, "package.json");
90
+ writeFileSync(changelogFile, options.changelog ?? CHANGELOG, "utf8");
91
+ writeFileSync(packageFile, JSON.stringify({ version: options.version ?? "0.13.1" }), "utf8");
92
+ return { dir, storeFile: join(dir, "state", "whats-new.json"), changelogFile, packageFile };
93
+ }
94
+
95
+ function readState(file: string): { lastSeenVersion?: string } {
96
+ return JSON.parse(readFileSync(file, "utf8")) as { lastSeenVersion?: string };
97
+ }
98
+
99
+ interface Harness {
100
+ start(reason: SessionStartEvent["reason"], ctx: ExtensionContext): void;
101
+ entries: Array<{ type: string; data: unknown }>;
102
+ commands: string[];
103
+ }
104
+
105
+ function harness(options: WhatsNewOptions): Harness {
106
+ const entries: Harness["entries"] = [];
107
+ const commands: string[] = [];
108
+ let startHandler: ((event: SessionStartEvent, ctx: ExtensionContext) => void) | undefined;
109
+
110
+ const pi = {
111
+ on(event: string, handler: (event: SessionStartEvent, ctx: ExtensionContext) => void) {
112
+ if (event === "session_start") startHandler = handler;
113
+ },
114
+ registerEntryRenderer() {},
115
+ appendEntry(type: string, data: unknown) {
116
+ entries.push({ type, data });
117
+ },
118
+ registerCommand(name: string) {
119
+ commands.push(name);
120
+ },
121
+ } as unknown as ExtensionAPI;
122
+
123
+ registerWhatsNew(pi, options);
124
+ if (!startHandler) throw new Error("whats-new did not register");
125
+
126
+ return {
127
+ entries,
128
+ commands,
129
+ start: (reason, ctx) => startHandler?.({ type: "session_start", reason }, ctx),
130
+ };
131
+ }
132
+
133
+ interface CtxOptions {
134
+ mode?: ExtensionContext["mode"];
135
+ sessionId?: string;
136
+ }
137
+
138
+ function makeCtx(options: CtxOptions = {}): {
139
+ ctx: ExtensionContext;
140
+ notes: Array<{ message: string; kind?: string }>;
141
+ } {
142
+ const notes: Array<{ message: string; kind?: string }> = [];
143
+ const ctx = {
144
+ mode: options.mode ?? "tui",
145
+ hasUI: true,
146
+ cwd: "/tmp/project",
147
+ ui: {
148
+ notify: (message: string, kind?: string) => notes.push({ message, kind }),
149
+ },
150
+ sessionManager: { getSessionId: () => options.sessionId ?? "session-1" },
151
+ } as unknown as ExtensionContext;
152
+ return { ctx, notes };
153
+ }
154
+
155
+ describe("versions", () => {
156
+ it("orders numerically, not lexically", () => {
157
+ expect(compareVersions("0.9.0", "0.10.0")).toBeLessThan(0);
158
+ expect(compareVersions("0.13.0", "0.13.1")).toBeLessThan(0);
159
+ expect(compareVersions("1.0.0", "0.99.99")).toBeGreaterThan(0);
160
+ expect(compareVersions("0.13.1", "0.13.1")).toBe(0);
161
+ });
162
+
163
+ it("treats unparseable versions as unknown (zero) and never as an upgrade", () => {
164
+ expect(parseVersion("0.13.1")).toEqual({ major: 0, minor: 13, patch: 1 });
165
+ expect(parseVersion("v1.2.3")).toEqual({ major: 1, minor: 2, patch: 3 });
166
+ expect(parseVersion("0.13")).toBeUndefined();
167
+ expect(parseVersion("next")).toBeUndefined();
168
+ expect(compareVersions("next", "0.13.0")).toBe(0);
169
+ });
170
+ });
171
+
172
+ describe("changelog parsing", () => {
173
+ it("reads one section per version and only top-level bullets", () => {
174
+ const releases = parseChangelog(CHANGELOG);
175
+ expect(releases.map((release) => release.version)).toEqual([
176
+ "0.13.1",
177
+ "0.13.0",
178
+ "0.12.0",
179
+ ]);
180
+ const latest = releases[0];
181
+ expect(latest.notes).toHaveLength(7);
182
+ expect(latest.notes).not.toContain("nested detail is not a notice line");
183
+ });
184
+
185
+ it("ignores Unreleased and text before the first version heading", () => {
186
+ const releases = parseChangelog(CHANGELOG);
187
+ expect(releases.some((release) => release.version === "Unreleased")).toBe(false);
188
+ expect(releases.flatMap((release) => release.notes)).not.toContain(
189
+ "work in progress",
190
+ );
191
+ });
192
+
193
+ it("accepts bracketed and dated headings", () => {
194
+ const releases = parseChangelog("## [0.2.0] - 2026-01-02\n\n- note\n");
195
+ expect(releases).toEqual([{ version: "0.2.0", notes: ["note"] }]);
196
+ });
197
+
198
+ it("keeps empty sections so the version still shows up", () => {
199
+ expect(parseChangelog("## 0.2.0\n\n## 0.3.0\n- note\n")).toEqual([
200
+ { version: "0.2.0", notes: [] },
201
+ { version: "0.3.0", notes: ["note"] },
202
+ ]);
203
+ });
204
+
205
+ it("filters an upgrade range: from is exclusive, to inclusive, newest first", () => {
206
+ const releases = parseChangelog(CHANGELOG);
207
+ expect(releasesForUpgrade(releases, "0.12.0", "0.13.1").map((r) => r.version)).toEqual([
208
+ "0.13.1",
209
+ "0.13.0",
210
+ ]);
211
+ expect(releasesForUpgrade(releases, "0.13.1", "0.13.1")).toEqual([]);
212
+ expect(releasesForUpgrade(releases, "0.13.1", "0.12.0")).toEqual([]);
213
+ });
214
+
215
+ it("sorts releases newest first", () => {
216
+ expect(sortReleasesDescending(parseChangelog(CHANGELOG))[0].version).toBe("0.13.1");
217
+ });
218
+
219
+ it("reads the version from a package manifest and rejects junk", () => {
220
+ const dir = tempDir();
221
+ const file = join(dir, "package.json");
222
+ writeFileSync(file, JSON.stringify({ version: "0.13.1" }));
223
+ expect(readPackageVersion(file)).toBe("0.13.1");
224
+ writeFileSync(file, JSON.stringify({ version: "not-a-version" }));
225
+ expect(readPackageVersion(file)).toBeUndefined();
226
+ writeFileSync(file, "{ broken");
227
+ expect(readPackageVersion(file)).toBeUndefined();
228
+ expect(readPackageVersion(join(dir, "missing.json"))).toBeUndefined();
229
+ });
230
+ });
231
+
232
+ describe("notice text", () => {
233
+ it("caps notes per version and points at the changelog", () => {
234
+ const releases = parseChangelog(CHANGELOG);
235
+ const lines = renderReleaseSections(releasesForUpgrade(releases, "0.12.0", "0.13.1"), {
236
+ maxNotesPerVersion: 3,
237
+ });
238
+ expect(lines).toContain("0.13.1");
239
+ expect(lines.filter((line) => line.startsWith(" - fix"))).toHaveLength(3);
240
+ expect(lines).toContain(" - (+4 more in CHANGELOG.md)");
241
+ });
242
+
243
+ it("caps versions and points at the changelog", () => {
244
+ const releases = ["0.3.0", "0.2.0", "0.1.0"].map((version) => ({
245
+ version,
246
+ notes: ["note"],
247
+ }));
248
+ const lines = renderReleaseSections(releases, { maxVersions: 1 });
249
+ expect(lines).toContain("(+2 older versions — see CHANGELOG.md)");
250
+ });
251
+
252
+ it("builds the upgrade title from the two versions", () => {
253
+ const notice = buildUpgradeNotice("0.12.0", "0.13.1", []);
254
+ expect(notice.title).toBe("pi-toolkits updated 0.12.0 → 0.13.1");
255
+ expect(notice.lines).toContain("No release notes are bundled for this version.");
256
+ });
257
+
258
+ });
259
+
260
+ describe("state store", () => {
261
+ it("defaults to no recorded version", () => {
262
+ const file = join(tempDir(), "state.json");
263
+ const store = new WhatsNewStore(file);
264
+ expect(store.get()).toEqual({});
265
+ });
266
+
267
+ it("round-trips lastSeenVersion", () => {
268
+ const file = join(tempDir(), "nested", "state.json");
269
+ const store = new WhatsNewStore(file);
270
+ store.markSeen("0.13.0");
271
+ expect(readState(file)).toEqual({ lastSeenVersion: "0.13.0" });
272
+ });
273
+
274
+ it("normalizes junk files and ignores invalid versions", () => {
275
+ expect(normalizeState(null)).toEqual({});
276
+ expect(normalizeState({ enabled: false, lastSeenVersion: "nope" })).toEqual({});
277
+ const file = join(tempDir(), "state.json");
278
+ writeFileSync(file, "{ broken");
279
+ const store = new WhatsNewStore(file);
280
+ expect(store.get()).toEqual({});
281
+ store.markSeen("bad-version");
282
+ expect(store.get()).toEqual({});
283
+ });
284
+
285
+ it("never throws when the state path is unusable", () => {
286
+ const dir = tempDir();
287
+ const blocker = join(dir, "blocker");
288
+ writeFileSync(blocker, "not a directory");
289
+ const store = new WhatsNewStore(join(blocker, "state.json"));
290
+ expect(() => store.markSeen("0.13.1")).not.toThrow();
291
+ expect(existsSync(join(blocker, "state.json"))).toBe(false);
292
+ });
293
+ });
294
+
295
+ describe("startup notice", () => {
296
+ it("does not claim a global slash command", () => {
297
+ const app = harness(fixture());
298
+ expect(app.commands).toEqual([]);
299
+ });
300
+
301
+ it("stays silent on a fresh install and records the running version", () => {
302
+ const files = fixture();
303
+ const app = harness({
304
+ storeFile: files.storeFile,
305
+ changelogFile: files.changelogFile,
306
+ packageFile: files.packageFile,
307
+ });
308
+ const { ctx } = makeCtx();
309
+ app.start("startup", ctx);
310
+ expect(app.entries).toHaveLength(0);
311
+ expect(readState(files.storeFile).lastSeenVersion).toBe("0.13.1");
312
+ });
313
+
314
+ it("announces once after an upgrade, with only the new sections", () => {
315
+ const files = fixture();
316
+ mkdirSync(join(files.dir, "state"), { recursive: true });
317
+ writeFileSync(
318
+ files.storeFile,
319
+ JSON.stringify({ lastSeenVersion: "0.12.0" }),
320
+ );
321
+ const app = harness({
322
+ storeFile: files.storeFile,
323
+ changelogFile: files.changelogFile,
324
+ packageFile: files.packageFile,
325
+ });
326
+ const { ctx } = makeCtx();
327
+ app.start("startup", ctx);
328
+
329
+ expect(app.entries).toHaveLength(1);
330
+ const data = app.entries[0].data as { title: string; lines: string[] };
331
+ expect(data.title).toBe("pi-toolkits updated 0.12.0 → 0.13.1");
332
+ expect(data.lines).toContain("0.13.1");
333
+ expect(data.lines).toContain("0.13.0");
334
+ expect(data.lines).not.toContain("0.12.0");
335
+ expect(data.lines).not.toContain(" - old feature");
336
+ expect(readState(files.storeFile).lastSeenVersion).toBe("0.13.1");
337
+ });
338
+
339
+ it("never announces the same version twice", () => {
340
+ const files = fixture();
341
+ mkdirSync(join(files.dir, "state"), { recursive: true });
342
+ writeFileSync(
343
+ files.storeFile,
344
+ JSON.stringify({ lastSeenVersion: "0.12.0" }),
345
+ );
346
+ const app = harness({
347
+ storeFile: files.storeFile,
348
+ changelogFile: files.changelogFile,
349
+ packageFile: files.packageFile,
350
+ });
351
+ app.start("startup", makeCtx({ sessionId: "first" }).ctx);
352
+ app.start("startup", makeCtx({ sessionId: "second" }).ctx);
353
+ expect(app.entries).toHaveLength(1);
354
+ });
355
+
356
+ it("deduplicates a repeated emission for the same session", () => {
357
+ const files = fixture();
358
+ mkdirSync(join(files.dir, "state"), { recursive: true });
359
+ writeFileSync(
360
+ files.storeFile,
361
+ JSON.stringify({ lastSeenVersion: "0.12.0" }),
362
+ );
363
+ const app = harness({
364
+ storeFile: files.storeFile,
365
+ changelogFile: files.changelogFile,
366
+ packageFile: files.packageFile,
367
+ });
368
+ const { ctx } = makeCtx({ sessionId: "same" });
369
+ app.start("startup", ctx);
370
+ app.start("startup", ctx);
371
+ expect(app.entries).toHaveLength(1);
372
+ });
373
+
374
+ it("ignores every reason except a process startup", () => {
375
+ const files = fixture();
376
+ mkdirSync(join(files.dir, "state"), { recursive: true });
377
+ writeFileSync(
378
+ files.storeFile,
379
+ JSON.stringify({ lastSeenVersion: "0.12.0" }),
380
+ );
381
+ const app = harness({
382
+ storeFile: files.storeFile,
383
+ changelogFile: files.changelogFile,
384
+ packageFile: files.packageFile,
385
+ });
386
+ for (const reason of ["reload", "new", "resume", "fork"] as const) {
387
+ app.start(reason, makeCtx({ sessionId: reason }).ctx);
388
+ }
389
+ expect(app.entries).toHaveLength(0);
390
+ expect(readState(files.storeFile).lastSeenVersion).toBe("0.12.0");
391
+ });
392
+
393
+ it("stays silent outside the interactive TUI and does not consume the notice", () => {
394
+ const files = fixture();
395
+ mkdirSync(join(files.dir, "state"), { recursive: true });
396
+ writeFileSync(
397
+ files.storeFile,
398
+ JSON.stringify({ lastSeenVersion: "0.12.0" }),
399
+ );
400
+ const app = harness({
401
+ storeFile: files.storeFile,
402
+ changelogFile: files.changelogFile,
403
+ packageFile: files.packageFile,
404
+ });
405
+ for (const mode of ["print", "json", "rpc"] as const) {
406
+ app.start("startup", makeCtx({ mode, sessionId: mode }).ctx);
407
+ }
408
+ expect(app.entries).toHaveLength(0);
409
+ expect(readState(files.storeFile).lastSeenVersion).toBe("0.12.0");
410
+ });
411
+
412
+
413
+ it("does not announce a downgrade and keeps the recorded version", () => {
414
+ const files = fixture({ version: "0.12.0" });
415
+ mkdirSync(join(files.dir, "state"), { recursive: true });
416
+ writeFileSync(
417
+ files.storeFile,
418
+ JSON.stringify({ lastSeenVersion: "0.13.1" }),
419
+ );
420
+ const app = harness({
421
+ storeFile: files.storeFile,
422
+ changelogFile: files.changelogFile,
423
+ packageFile: files.packageFile,
424
+ });
425
+ app.start("startup", makeCtx().ctx);
426
+ expect(app.entries).toHaveLength(0);
427
+ expect(readState(files.storeFile).lastSeenVersion).toBe("0.13.1");
428
+ });
429
+
430
+ it("still reports the upgrade when the changelog has no section for it", () => {
431
+ const files = fixture({ version: "0.14.0" });
432
+ mkdirSync(join(files.dir, "state"), { recursive: true });
433
+ writeFileSync(
434
+ files.storeFile,
435
+ JSON.stringify({ lastSeenVersion: "0.13.1" }),
436
+ );
437
+ const app = harness({
438
+ storeFile: files.storeFile,
439
+ changelogFile: files.changelogFile,
440
+ packageFile: files.packageFile,
441
+ });
442
+ app.start("startup", makeCtx().ctx);
443
+ const data = app.entries[0].data as { lines: string[] };
444
+ expect(data.lines).toContain("No release notes are bundled for this version.");
445
+ });
446
+
447
+ it("uses an explicit version without reading package.json", () => {
448
+ const files = fixture();
449
+ const app = harness({
450
+ storeFile: files.storeFile,
451
+ changelogFile: files.changelogFile,
452
+ packageFile: join(files.dir, "missing.json"),
453
+ version: "0.13.0",
454
+ });
455
+ app.start("startup", makeCtx().ctx);
456
+ expect(app.entries).toHaveLength(0);
457
+ expect(readState(files.storeFile).lastSeenVersion).toBe("0.13.0");
458
+ });
459
+ });
460
+
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@
15
15
  * - init — agent memory framework install / adoption (/ask-init)
16
16
  * - uat — acceptance ledger and browser page (/ask-uat, record_change)
17
17
  * - quit-guard — quit watchdog: force-exit when pi's shutdown hangs (/quit-guard)
18
+ * - whats-new — automatic release notes on the first start after an upgrade
18
19
  *
19
20
  * `ask` and `permission` are independent gates on one bus: each answers its own
20
21
  * question and the strictest answer wins, so they compose rather than replace
@@ -34,6 +35,7 @@ import { registerQuitGuard } from "./features/quit-guard/index.js";
34
35
  import { registerReview } from "./features/review/index.js";
35
36
  import { registerStash } from "./features/stash/index.js";
36
37
  import { registerUat } from "./features/uat/index.js";
38
+ import { registerWhatsNew } from "./features/whats-new/index.js";
37
39
 
38
40
  export default function piToolkits(pi: ExtensionAPI): void {
39
41
  // One rolling audit sink for every gate. Creation is lazy and side-effect
@@ -58,6 +60,7 @@ export default function piToolkits(pi: ExtensionAPI): void {
58
60
  // UAT registers after plan so its `before_agent_start` re-assertion runs last
59
61
  // and can add `record_change` to whatever tool set plan installed.
60
62
  registerUat(pi);
63
+ registerWhatsNew(pi);
61
64
 
62
65
  gates.install(pi);
63
66
  }