@andares/pi-toolkits 1.0.2 → 1.2.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 +21 -0
- package/README.md +65 -1
- package/README.zh-CN.md +58 -1
- package/package.json +7 -4
- package/src/features/keep/constants.ts +44 -0
- package/src/features/keep/format.ts +77 -0
- package/src/features/keep/index.ts +409 -0
- package/src/features/keep/keep.test.ts +955 -0
- package/src/features/keep/ledger.ts +340 -0
- package/src/features/keep/picker.ts +257 -0
- package/src/features/keep/store.ts +271 -0
- package/src/features/review-page/assets/index-CrDwcsqI.css +1 -0
- package/src/features/review-page/assets/index-DYxi32gA.js +1 -0
- package/src/features/{spec → review-page}/assets/index.html +2 -2
- package/src/features/{spec → review-page}/blocks.test.ts +17 -0
- package/src/features/{spec → review-page}/blocks.ts +9 -0
- package/src/features/review-page/headings.test.ts +44 -0
- package/src/features/review-page/headings.ts +67 -0
- package/src/features/spec/assets.test.ts +2 -2
- package/src/features/spec/diff.test.ts +1 -1
- package/src/features/spec/diff.ts +1 -1
- package/src/features/spec/feedback.test.ts +1 -1
- package/src/features/spec/feedback.ts +1 -1
- package/src/features/spec/index.test.ts +121 -0
- package/src/features/spec/index.ts +82 -23
- package/src/features/spec/server.test.ts +65 -2
- package/src/features/spec/server.ts +29 -5
- package/src/features/spec/store.ts +1 -1
- package/src/features/talk/index.test.ts +230 -0
- package/src/features/talk/index.ts +241 -0
- package/src/features/talk/message.test.ts +212 -0
- package/src/features/talk/message.ts +152 -0
- package/src/features/talk/reply.test.ts +116 -0
- package/src/features/talk/reply.ts +92 -0
- package/src/features/talk/sections.test.ts +39 -0
- package/src/features/talk/sections.ts +44 -0
- package/src/features/talk/server.test.ts +247 -0
- package/src/features/talk/server.ts +211 -0
- package/src/features/uat/ledger.ts +13 -62
- package/src/index.ts +7 -0
- package/src/lib/event-ledger.ts +112 -0
- package/src/features/spec/assets/index-CdTerMvN.css +0 -1
- package/src/features/spec/assets/index-Dv52AIjX.js +0 -1
- /package/src/features/{spec → review-page}/render.test.ts +0 -0
- /package/src/features/{spec → review-page}/render.ts +0 -0
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* block notes (about one paragraph). There is no inline/range annotation, which
|
|
11
11
|
* is what keeps the block model from needing re-anchoring after an edit.
|
|
12
12
|
*/
|
|
13
|
-
import { blockLabel, type Block } from "
|
|
13
|
+
import { blockLabel, type Block } from "../review-page/blocks.js";
|
|
14
14
|
import type { DiffEntry } from "./diff.js";
|
|
15
15
|
|
|
16
16
|
export type AnnotationVerdict = "change" | "suggest" | "question";
|
|
@@ -133,6 +133,55 @@ async function decide(url: string, action: "approve" | "deny"): Promise<number>
|
|
|
133
133
|
});
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
/** Read `/api/state` the way the page does. */
|
|
137
|
+
async function reviewState(url: string): Promise<{
|
|
138
|
+
totalVersions: number;
|
|
139
|
+
version: number;
|
|
140
|
+
history: Array<{ n: number }>;
|
|
141
|
+
}> {
|
|
142
|
+
const parsed = new URL(url);
|
|
143
|
+
const token = parsed.searchParams.get("t") ?? "";
|
|
144
|
+
return new Promise((resolve, reject) => {
|
|
145
|
+
const req = request(
|
|
146
|
+
{
|
|
147
|
+
host: "127.0.0.1",
|
|
148
|
+
port: Number(parsed.port),
|
|
149
|
+
path: "/api/state",
|
|
150
|
+
method: "GET",
|
|
151
|
+
headers: { "x-plan-token": token },
|
|
152
|
+
},
|
|
153
|
+
(res) => {
|
|
154
|
+
const chunks: Buffer[] = [];
|
|
155
|
+
res.on("data", (chunk: Buffer) => chunks.push(chunk));
|
|
156
|
+
res.on("end", () => {
|
|
157
|
+
if (res.statusCode !== 200) {
|
|
158
|
+
reject(new Error(`state HTTP ${res.statusCode}`));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")) as never);
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
);
|
|
165
|
+
req.on("error", reject);
|
|
166
|
+
req.end();
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Resolve once nothing accepts connections on that URL's port any more. */
|
|
171
|
+
async function waitUntilClosed(url: string): Promise<void> {
|
|
172
|
+
const port = Number(new URL(url).port);
|
|
173
|
+
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
174
|
+
const up = await new Promise<boolean>((resolve) => {
|
|
175
|
+
const req = request({ host: "127.0.0.1", port, path: "/", method: "GET" }, () => resolve(true));
|
|
176
|
+
req.on("error", () => resolve(false));
|
|
177
|
+
req.end();
|
|
178
|
+
});
|
|
179
|
+
if (!up) return;
|
|
180
|
+
await new Promise((done) => setTimeout(done, 20));
|
|
181
|
+
}
|
|
182
|
+
throw new Error(`server on port ${port} never closed`);
|
|
183
|
+
}
|
|
184
|
+
|
|
136
185
|
describe("ask-spec command", () => {
|
|
137
186
|
it("enters spec mode: permission on, submit tool active, kickoff sent", async () => {
|
|
138
187
|
const h = harness();
|
|
@@ -167,6 +216,78 @@ describe("ask-spec command", () => {
|
|
|
167
216
|
expect(h.notify).toHaveBeenCalledWith(expect.stringContaining("specs/spec.md"), "info");
|
|
168
217
|
});
|
|
169
218
|
|
|
219
|
+
it("leaves spec mode when the bare command is run again", async () => {
|
|
220
|
+
const h = harness();
|
|
221
|
+
const handler = h.commands.get(SPEC_COMMAND)!.handler;
|
|
222
|
+
await handler("topic", h.ctx);
|
|
223
|
+
h.notify.mockClear();
|
|
224
|
+
|
|
225
|
+
await handler("", h.ctx);
|
|
226
|
+
expect(h.control.restore).toHaveBeenCalled();
|
|
227
|
+
expect(h.activeTools()).not.toContain(SPEC_SUBMIT_TOOL);
|
|
228
|
+
expect(h.notify).toHaveBeenCalledWith(expect.stringContaining("spec mode off"), "info");
|
|
229
|
+
expect(h.sendUserMessage).toHaveBeenCalledTimes(1);
|
|
230
|
+
|
|
231
|
+
// The next bare invocation starts a fresh default-topic review.
|
|
232
|
+
await handler("", h.ctx);
|
|
233
|
+
expect(h.activeTools()).toContain(SPEC_SUBMIT_TOOL);
|
|
234
|
+
expect(h.notify).toHaveBeenLastCalledWith(expect.stringContaining("specs/spec.md"), "info");
|
|
235
|
+
expect(h.sendUserMessage).toHaveBeenCalledTimes(2);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("opens a new window and server for the revision after feedback, keeping history", async () => {
|
|
239
|
+
const h = harness();
|
|
240
|
+
const handler = h.commands.get(SPEC_COMMAND)!.handler;
|
|
241
|
+
await handler("topic", h.ctx);
|
|
242
|
+
|
|
243
|
+
writeSpec("specs/topic.md", "# SPEC: one");
|
|
244
|
+
const first = await h.tools.get(SPEC_SUBMIT_TOOL)!.execute("id", { path: "specs/topic.md" });
|
|
245
|
+
const firstUrl = (first.details as { url: string }).url;
|
|
246
|
+
expect(h.openBrowser).toHaveBeenCalledTimes(1);
|
|
247
|
+
|
|
248
|
+
// Feedback closes the review server shortly after the decision.
|
|
249
|
+
expect(await decide(firstUrl, "deny")).toBe(200);
|
|
250
|
+
await waitUntilClosed(firstUrl);
|
|
251
|
+
|
|
252
|
+
// The next revision must get its own server and its own window.
|
|
253
|
+
writeSpec("specs/topic.md", "# SPEC: one\n\nA second paragraph.");
|
|
254
|
+
const second = await h.tools.get(SPEC_SUBMIT_TOOL)!.execute("id", {
|
|
255
|
+
path: "specs/topic.md",
|
|
256
|
+
});
|
|
257
|
+
const secondUrl = (second.details as { url: string }).url;
|
|
258
|
+
|
|
259
|
+
expect(secondUrl).not.toBe(firstUrl);
|
|
260
|
+
expect(h.openBrowser).toHaveBeenCalledTimes(2);
|
|
261
|
+
const state = await reviewState(secondUrl);
|
|
262
|
+
expect(state.totalVersions).toBe(2);
|
|
263
|
+
expect(state.history.map((entry) => entry.n)).toEqual([2, 1]);
|
|
264
|
+
|
|
265
|
+
await handler("off", h.ctx);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("restarts a closed review server when the page is reopened", async () => {
|
|
269
|
+
const h = harness();
|
|
270
|
+
const handler = h.commands.get(SPEC_COMMAND)!.handler;
|
|
271
|
+
await handler("topic", h.ctx);
|
|
272
|
+
|
|
273
|
+
writeSpec("specs/topic.md", "# SPEC: one");
|
|
274
|
+
const first = await h.tools.get(SPEC_SUBMIT_TOOL)!.execute("id", { path: "specs/topic.md" });
|
|
275
|
+
const firstUrl = (first.details as { url: string }).url;
|
|
276
|
+
expect(await decide(firstUrl, "deny")).toBe(200);
|
|
277
|
+
await waitUntilClosed(firstUrl);
|
|
278
|
+
|
|
279
|
+
h.notify.mockClear();
|
|
280
|
+
await handler("open", h.ctx);
|
|
281
|
+
expect(h.openBrowser).toHaveBeenCalledTimes(2);
|
|
282
|
+
const notified = String(h.notify.mock.calls.at(-1)?.[0] ?? "");
|
|
283
|
+
const secondUrl = /http:\/\/\S+/.exec(notified)?.[0] ?? "";
|
|
284
|
+
expect(secondUrl).not.toBe("");
|
|
285
|
+
expect(secondUrl).not.toBe(firstUrl);
|
|
286
|
+
expect((await reviewState(secondUrl)).totalVersions).toBe(1);
|
|
287
|
+
|
|
288
|
+
await handler("off", h.ctx);
|
|
289
|
+
});
|
|
290
|
+
|
|
170
291
|
it("refuses to start twice and can be turned off", async () => {
|
|
171
292
|
const h = harness();
|
|
172
293
|
const handler = h.commands.get(SPEC_COMMAND)!.handler;
|
|
@@ -34,7 +34,7 @@ import { parseCommandUnits } from "../permission/units.js";
|
|
|
34
34
|
import { buildFeedbackMarkdown } from "./feedback.js";
|
|
35
35
|
import { openInBrowser } from "../../lib/opener.js";
|
|
36
36
|
import { buildSpecKickoff, buildSpecSystemPrompt } from "./prompt.js";
|
|
37
|
-
import { renderBlockHtml } from "
|
|
37
|
+
import { renderBlockHtml } from "../review-page/render.js";
|
|
38
38
|
import { buildSpecState, startSpecServer, type SpecServerHandle } from "./server.js";
|
|
39
39
|
import {
|
|
40
40
|
appendSpecVersion,
|
|
@@ -98,6 +98,8 @@ export interface SpecOptions {
|
|
|
98
98
|
closeDelayMs?: number;
|
|
99
99
|
/** Port search starts here; the review server walks upward. */
|
|
100
100
|
portStart?: number;
|
|
101
|
+
/** How many ports the review server may try before giving up (default 20). */
|
|
102
|
+
portAttempts?: number;
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
export function registerSpec(pi: ExtensionAPI, gates: GateHub, options: SpecOptions = {}): void {
|
|
@@ -116,8 +118,47 @@ export function registerSpec(pi: ExtensionAPI, gates: GateHub, options: SpecOpti
|
|
|
116
118
|
|
|
117
119
|
function closeServer(): void {
|
|
118
120
|
if (!server) return;
|
|
119
|
-
|
|
121
|
+
const closing = server;
|
|
120
122
|
server = undefined;
|
|
123
|
+
void closing.close();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Start the review server for the current session.
|
|
128
|
+
*
|
|
129
|
+
* Every start gets its own token and walks the port range, so a restarted
|
|
130
|
+
* review never reuses a dead socket and two sessions cannot share a window.
|
|
131
|
+
*/
|
|
132
|
+
function startReviewServer(current: SpecSession): Promise<SpecServerHandle> {
|
|
133
|
+
return startSpecServer({
|
|
134
|
+
session: current,
|
|
135
|
+
render: renderBlockHtml,
|
|
136
|
+
portStart: options.portStart,
|
|
137
|
+
portAttempts: options.portAttempts,
|
|
138
|
+
closeDelayMs: options.closeDelayMs,
|
|
139
|
+
onDecision: ({ action, version: decidedVersion }) => {
|
|
140
|
+
const ctx = ctxRef;
|
|
141
|
+
if (ctx) void handleDecision({ action, version: decidedVersion }, ctx);
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* A live handle, or a freshly started one with its window opened.
|
|
148
|
+
*
|
|
149
|
+
* A review server closes itself shortly after a decision. The handle that
|
|
150
|
+
* produced that decision is dead: broadcasting a revision into it reaches no
|
|
151
|
+
* page, and printing its URL gives the operator a dead link. Detect that and
|
|
152
|
+
* start a new server instead — the next window is a new window on purpose.
|
|
153
|
+
*/
|
|
154
|
+
async function ensureReviewServer(): Promise<{ handle: SpecServerHandle; started: boolean }> {
|
|
155
|
+
if (server && server.active()) return { handle: server, started: false };
|
|
156
|
+
if (!session) throw new Error("no spec revision to review yet");
|
|
157
|
+
server = undefined;
|
|
158
|
+
const handle = await startReviewServer(session);
|
|
159
|
+
server = handle;
|
|
160
|
+
open(handle.url);
|
|
161
|
+
return { handle, started: true };
|
|
121
162
|
}
|
|
122
163
|
|
|
123
164
|
function toolSetWithSubmit(): string[] {
|
|
@@ -218,7 +259,7 @@ export function registerSpec(pi: ExtensionAPI, gates: GateHub, options: SpecOpti
|
|
|
218
259
|
|
|
219
260
|
const specCommand = {
|
|
220
261
|
description:
|
|
221
|
-
"Spec first: research, write a spec, review it in the browser with block annotations, then implement after approval. `off
|
|
262
|
+
"Spec first: research, write a spec, review it in the browser with block annotations, then implement after approval. Run without arguments again, or use `off`, to leave the phase; `open` re-opens the review.",
|
|
222
263
|
handler: async (args: string, ctx: ExtensionContext) => {
|
|
223
264
|
const arg = args.trim();
|
|
224
265
|
ctxRef = ctx;
|
|
@@ -234,12 +275,27 @@ export function registerSpec(pi: ExtensionAPI, gates: GateHub, options: SpecOpti
|
|
|
234
275
|
}
|
|
235
276
|
|
|
236
277
|
if (arg === "open") {
|
|
237
|
-
if (!
|
|
278
|
+
if (!session) {
|
|
238
279
|
ctx.ui.notify("ask-spec: no review is open — submit a spec first.", "warning");
|
|
239
280
|
return;
|
|
240
281
|
}
|
|
241
|
-
|
|
242
|
-
|
|
282
|
+
try {
|
|
283
|
+
const ensured = await ensureReviewServer();
|
|
284
|
+
// A fresh server already opened its window; an existing one is
|
|
285
|
+
// simply pointed at again.
|
|
286
|
+
if (!ensured.started) open(ensured.handle.url);
|
|
287
|
+
ctx.ui.notify(`ask-spec: ${ensured.handle.url}`, "info");
|
|
288
|
+
} catch (error) {
|
|
289
|
+
ctx.ui.notify(
|
|
290
|
+
`ask-spec: cannot start the review server — ${error instanceof Error ? error.message : String(error)}`,
|
|
291
|
+
"warning",
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (active && arg === "") {
|
|
298
|
+
leave(ctx, specPath ? `spec mode off — draft left at ${specPath}.` : "spec mode off.");
|
|
243
299
|
return;
|
|
244
300
|
}
|
|
245
301
|
|
|
@@ -353,21 +409,24 @@ export function registerSpec(pi: ExtensionAPI, gates: GateHub, options: SpecOpti
|
|
|
353
409
|
appendSpecVersion(session, { text, source });
|
|
354
410
|
}
|
|
355
411
|
const version = latestVersion(session);
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
412
|
+
let handle: SpecServerHandle;
|
|
413
|
+
try {
|
|
414
|
+
const ensured = await ensureReviewServer();
|
|
415
|
+
handle = ensured.handle;
|
|
416
|
+
if (!ensured.started) handle.broadcast("revision", { version: version.n });
|
|
417
|
+
if (ensured.started) {
|
|
418
|
+
ctxRef?.ui.notify(`ask-spec: review open at ${handle.url}`, "info");
|
|
419
|
+
}
|
|
420
|
+
} catch (error) {
|
|
421
|
+
return {
|
|
422
|
+
content: [
|
|
423
|
+
{
|
|
424
|
+
type: "text" as const,
|
|
425
|
+
text: `Cannot open the review page: ${error instanceof Error ? error.message : String(error)}. The spec is saved; retry spec_submit.`,
|
|
426
|
+
},
|
|
427
|
+
],
|
|
428
|
+
details: {},
|
|
429
|
+
};
|
|
371
430
|
}
|
|
372
431
|
const changed = buildSpecState(session, version.n).items.filter(
|
|
373
432
|
(item) => item.status !== "unchanged",
|
|
@@ -378,10 +437,10 @@ export function registerSpec(pi: ExtensionAPI, gates: GateHub, options: SpecOpti
|
|
|
378
437
|
type: "text" as const,
|
|
379
438
|
text:
|
|
380
439
|
`Submitted v${version.n} of ${session.versions.length} (${changed} changed blocks). ` +
|
|
381
|
-
`Review: ${
|
|
440
|
+
`Review: ${handle.url}\nEnd your turn now — the decision arrives as a new message.`,
|
|
382
441
|
},
|
|
383
442
|
],
|
|
384
|
-
details: { version: version.n, url:
|
|
443
|
+
details: { version: version.n, url: handle.url },
|
|
385
444
|
};
|
|
386
445
|
},
|
|
387
446
|
});
|
|
@@ -3,8 +3,8 @@ import { request } from "node:http";
|
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
6
|
-
import { parseBlocks } from "
|
|
7
|
-
import { renderBlockHtml } from "
|
|
6
|
+
import { parseBlocks } from "../review-page/blocks.js";
|
|
7
|
+
import { renderBlockHtml } from "../review-page/render.js";
|
|
8
8
|
import { buildSpecState, normalizeAnnotations, startSpecServer, type SpecServerHandle } from "./server.js";
|
|
9
9
|
import { appendSpecVersion, createSpecSession, type SpecSession } from "./store.js";
|
|
10
10
|
|
|
@@ -126,6 +126,20 @@ describe("buildSpecState", () => {
|
|
|
126
126
|
expect(state.totalVersions).toBe(1);
|
|
127
127
|
});
|
|
128
128
|
|
|
129
|
+
it("declares spec mode and ships an outline over the viewed revision", () => {
|
|
130
|
+
const session = makeSession("# Top\n\nintro\n\n## Section\n\nbody");
|
|
131
|
+
const state = buildSpecState(session);
|
|
132
|
+
expect(state.mode).toBe("spec");
|
|
133
|
+
expect(state.headings.map((heading) => [heading.level, heading.text])).toEqual([
|
|
134
|
+
[1, "Top"],
|
|
135
|
+
[2, "Section"],
|
|
136
|
+
]);
|
|
137
|
+
// Anchors point at blocks the page actually renders.
|
|
138
|
+
for (const heading of state.headings) {
|
|
139
|
+
expect(state.items.some((item) => item.id === heading.blockId)).toBe(true);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
129
143
|
it("carries the diff statuses and the previous-block ids for a later revision", () => {
|
|
130
144
|
const session = makeSession("## Spec\n\nwrite the block parser before the server\n\nsecond step");
|
|
131
145
|
const first = session.versions[0];
|
|
@@ -372,3 +386,52 @@ describe("startSpecServer", () => {
|
|
|
372
386
|
}
|
|
373
387
|
});
|
|
374
388
|
});
|
|
389
|
+
|
|
390
|
+
describe("review server lifecycle", () => {
|
|
391
|
+
it("walks to the next free port so two reviews can be open at once", async () => {
|
|
392
|
+
const portStart = 44_000 + Math.floor(Math.random() * 500);
|
|
393
|
+
const first = await serve(makeSession("# first"), { portStart });
|
|
394
|
+
const second = await serve(makeSession("# second"), { portStart });
|
|
395
|
+
try {
|
|
396
|
+
// The pool is 20 ports walked upward: the second review must not
|
|
397
|
+
// silently take the first one's address.
|
|
398
|
+
expect(second.port).toBeGreaterThan(first.port);
|
|
399
|
+
expect(second.url).not.toBe(first.url);
|
|
400
|
+
expect(second.token).not.toBe(first.token);
|
|
401
|
+
expect(first.active()).toBe(true);
|
|
402
|
+
expect(second.active()).toBe(true);
|
|
403
|
+
} finally {
|
|
404
|
+
await first.close();
|
|
405
|
+
await second.close();
|
|
406
|
+
}
|
|
407
|
+
expect(first.active()).toBe(false);
|
|
408
|
+
expect(second.active()).toBe(false);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("reports the handle dead as soon as a decision is accepted", async () => {
|
|
412
|
+
const session = makeSession();
|
|
413
|
+
const server = await serve(session, { closeDelayMs: 20 });
|
|
414
|
+
expect(server.active()).toBe(true);
|
|
415
|
+
const response = await call({
|
|
416
|
+
port: server.port,
|
|
417
|
+
path: "/api/decide",
|
|
418
|
+
method: "POST",
|
|
419
|
+
token: server.token,
|
|
420
|
+
body: { action: "deny" },
|
|
421
|
+
});
|
|
422
|
+
expect(response.status).toBe(200);
|
|
423
|
+
// The extension keys "broadcast a revision" vs "start a new window" off
|
|
424
|
+
// this flag, so it must flip before the socket is actually gone.
|
|
425
|
+
expect(server.active()).toBe(false);
|
|
426
|
+
await vi.waitFor(async () => {
|
|
427
|
+
await expect(call({ port: server.port, path: "/api/state" })).rejects.toThrow();
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it("closes idempotently", async () => {
|
|
432
|
+
const server = await serve(makeSession());
|
|
433
|
+
await server.close();
|
|
434
|
+
await server.close();
|
|
435
|
+
expect(server.active()).toBe(false);
|
|
436
|
+
});
|
|
437
|
+
});
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
* routes and payload.
|
|
17
17
|
*/
|
|
18
18
|
import { fileURLToPath } from "node:url";
|
|
19
|
-
import { blockLabel, type Block } from "
|
|
19
|
+
import { blockLabel, type Block } from "../review-page/blocks.js";
|
|
20
|
+
import { extractHeadings, type Heading } from "../review-page/headings.js";
|
|
20
21
|
import type { BlockStatus, DiffEntry } from "./diff.js";
|
|
21
22
|
import type { Annotation, BlockAnnotations } from "./feedback.js";
|
|
22
23
|
import {
|
|
@@ -28,7 +29,7 @@ import {
|
|
|
28
29
|
} from "./store.js";
|
|
29
30
|
import { bindHost, isWsl, urlHost } from "../../lib/opener.js";
|
|
30
31
|
import { startWebServer, type WebServerHandle } from "../../lib/web-review.js";
|
|
31
|
-
import { renderBlockHtml } from "
|
|
32
|
+
import { renderBlockHtml } from "../review-page/render.js";
|
|
32
33
|
|
|
33
34
|
export interface SpecStateItem {
|
|
34
35
|
status: BlockStatus;
|
|
@@ -41,6 +42,7 @@ export interface SpecStateItem {
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
export interface SpecStatePayload {
|
|
45
|
+
mode: "spec";
|
|
44
46
|
slug: string;
|
|
45
47
|
version: number;
|
|
46
48
|
totalVersions: number;
|
|
@@ -52,6 +54,8 @@ export interface SpecStatePayload {
|
|
|
52
54
|
decided: boolean;
|
|
53
55
|
/** Earlier revisions, newest first — the history drawer. */
|
|
54
56
|
history: Array<{ n: number; at: string; changed: number }>;
|
|
57
|
+
/** Outline entries over the revision being viewed (shared with talk mode). */
|
|
58
|
+
headings: Heading[];
|
|
55
59
|
}
|
|
56
60
|
|
|
57
61
|
/** Compose what the page renders: blocks of one revision, tagged with their diff status. */
|
|
@@ -88,6 +92,7 @@ export function buildSpecState(session: SpecSession, versionNumber?: number): Sp
|
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
return {
|
|
95
|
+
mode: "spec",
|
|
91
96
|
slug: session.slug,
|
|
92
97
|
version: version.n,
|
|
93
98
|
totalVersions: latestVersion(session).n,
|
|
@@ -104,6 +109,9 @@ export function buildSpecState(session: SpecSession, versionNumber?: number): Sp
|
|
|
104
109
|
changed: candidate.diff.filter((entry) => entry.status !== "unchanged").length,
|
|
105
110
|
}))
|
|
106
111
|
.sort((a, b) => b.n - a.n),
|
|
112
|
+
// Outline entries follow the revision being viewed, so "View a previous
|
|
113
|
+
// version" moves the table of contents with it.
|
|
114
|
+
headings: extractHeadings(version.blocks),
|
|
107
115
|
};
|
|
108
116
|
}
|
|
109
117
|
|
|
@@ -130,6 +138,12 @@ export interface SpecServerHandle {
|
|
|
130
138
|
token: string;
|
|
131
139
|
close: () => Promise<void>;
|
|
132
140
|
broadcast: (event: string, data: unknown) => void;
|
|
141
|
+
/**
|
|
142
|
+
* False once a decision has been accepted (the port closes shortly after) or
|
|
143
|
+
* after `close()`. Callers must start a new server instead of broadcasting
|
|
144
|
+
* into a handle whose socket is gone — the old page cannot come back.
|
|
145
|
+
*/
|
|
146
|
+
active: () => boolean;
|
|
133
147
|
}
|
|
134
148
|
|
|
135
149
|
function isVerdict(value: unknown): value is Annotation["verdict"] {
|
|
@@ -167,12 +181,21 @@ export function normalizeAnnotations(input: unknown): SpecAnnotations {
|
|
|
167
181
|
}
|
|
168
182
|
|
|
169
183
|
export async function startSpecServer(options: StartSpecServerOptions): Promise<SpecServerHandle> {
|
|
170
|
-
const assetsDir = options.assetsDir ?? fileURLToPath(new URL("
|
|
184
|
+
const assetsDir = options.assetsDir ?? fileURLToPath(new URL("../review-page/assets/", import.meta.url));
|
|
171
185
|
const render = options.render ?? renderBlockHtml;
|
|
172
186
|
const closeDelayMs = options.closeDelayMs ?? 1500;
|
|
173
187
|
let closing = false;
|
|
188
|
+
let closed = false;
|
|
174
189
|
let handle: WebServerHandle | undefined;
|
|
175
190
|
|
|
191
|
+
/** Idempotent: a decision, `/ask-spec off` and process teardown may all call it. */
|
|
192
|
+
const closeHandle = async (): Promise<void> => {
|
|
193
|
+
if (closed) return;
|
|
194
|
+
closed = true;
|
|
195
|
+
closing = true;
|
|
196
|
+
await handle?.close();
|
|
197
|
+
};
|
|
198
|
+
|
|
176
199
|
const server = await startWebServer({
|
|
177
200
|
assetsDir,
|
|
178
201
|
portStart: options.portStart,
|
|
@@ -251,7 +274,7 @@ export async function startSpecServer(options: StartSpecServerOptions): Promise<
|
|
|
251
274
|
closing = true;
|
|
252
275
|
setTimeout(() => {
|
|
253
276
|
options.onDecision?.({ action, version });
|
|
254
|
-
void
|
|
277
|
+
void closeHandle();
|
|
255
278
|
}, closeDelayMs).unref();
|
|
256
279
|
}
|
|
257
280
|
return true;
|
|
@@ -266,7 +289,8 @@ export async function startSpecServer(options: StartSpecServerOptions): Promise<
|
|
|
266
289
|
url: server.url,
|
|
267
290
|
port: server.port,
|
|
268
291
|
token: server.token,
|
|
269
|
-
close:
|
|
292
|
+
close: closeHandle,
|
|
270
293
|
broadcast: server.broadcast,
|
|
294
|
+
active: () => !closing && !closed,
|
|
271
295
|
};
|
|
272
296
|
}
|
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
writeFileSync,
|
|
23
23
|
} from "node:fs";
|
|
24
24
|
import { join } from "node:path";
|
|
25
|
-
import { parseBlocks, type Block } from "
|
|
25
|
+
import { parseBlocks, type Block } from "../review-page/blocks.js";
|
|
26
26
|
import { diffBlocks, type DiffEntry } from "./diff.js";
|
|
27
27
|
import type { Annotation, BlockAnnotations } from "./feedback.js";
|
|
28
28
|
|