@andares/pi-toolkits 1.0.0 → 1.0.1
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 +10 -0
- package/README.md +21 -20
- package/README.zh-CN.md +23 -22
- package/package.json +5 -4
- package/src/features/ask/index.test.ts +2 -2
- package/src/features/ask/index.ts +1 -1
- package/src/features/init/index.test.ts +2 -2
- package/src/features/init/templates/workflow-block.md +2 -2
- package/src/features/permission/index.ts +1 -1
- package/src/features/review/index.ts +3 -3
- package/src/features/review/review.test.ts +2 -2
- package/src/features/spec/assets/index-CdTerMvN.css +1 -0
- package/src/features/spec/assets/index-Dv52AIjX.js +1 -0
- package/src/features/{plan → spec}/assets/index.html +3 -3
- package/src/features/{plan → spec}/assets.test.ts +5 -5
- package/src/features/{plan → spec}/blocks.ts +1 -1
- package/src/features/{plan → spec}/feedback.test.ts +26 -26
- package/src/features/{plan → spec}/feedback.ts +14 -14
- package/src/features/{plan → spec}/index.test.ts +100 -66
- package/src/features/{plan → spec}/index.ts +117 -106
- package/src/features/{plan → spec}/prompt.test.ts +20 -20
- package/src/features/{plan → spec}/prompt.ts +25 -25
- package/src/features/{plan → spec}/render.test.ts +2 -2
- package/src/features/{plan → spec}/server.test.ts +21 -21
- package/src/features/{plan → spec}/server.ts +22 -22
- package/src/features/{plan → spec}/store.test.ts +77 -35
- package/src/features/{plan → spec}/store.ts +80 -52
- package/src/features/uat/index.test.ts +5 -5
- package/src/features/uat/index.ts +8 -8
- package/src/features/uat/record-tool.ts +3 -3
- package/src/features/uat/records.test.ts +5 -5
- package/src/index.ts +6 -6
- package/src/lib/ask-control.ts +2 -2
- package/src/lib/permission-control.ts +4 -4
- package/src/lib/phases.ts +20 -7
- package/src/lib/web-review.ts +1 -1
- package/src/features/plan/assets/index-C1YLIi4I.js +0 -1
- package/src/features/plan/assets/index-DMEpYC3V.css +0 -1
- /package/src/features/{plan → spec}/blocks.test.ts +0 -0
- /package/src/features/{plan → spec}/diff.test.ts +0 -0
- /package/src/features/{plan → spec}/diff.ts +0 -0
- /package/src/features/{plan → spec}/render.ts +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* spec prompt — what the agent is told when spec review opens.
|
|
3
3
|
*
|
|
4
|
-
* Two parts, both injected as a system-prompt block on every
|
|
4
|
+
* Two parts, both injected as a system-prompt block on every spec-review turn:
|
|
5
5
|
*
|
|
6
6
|
* 1. the framing: what the phase allows (write markdown, read everything,
|
|
7
7
|
* ask questions) and the one rule that has no exception — the turn ends by
|
|
8
|
-
* asking a question or by submitting the
|
|
8
|
+
* asking a question or by submitting the spec, never by going quiet;
|
|
9
9
|
* 2. the spec framework: a skill-style guide to the shape of a reviewable
|
|
10
|
-
*
|
|
10
|
+
* spec. It is deliberately a *default* shape, not a template to fill in:
|
|
11
11
|
* the operator reviews paragraphs, so the guide also explains how to write
|
|
12
12
|
* for paragraph-level review (one idea per block, headings on their own
|
|
13
13
|
* line, no blank lines inside a list) — that is the part models get wrong
|
|
@@ -15,40 +15,40 @@
|
|
|
15
15
|
*
|
|
16
16
|
* Kept as a string constant rather than a bundled SKILL.md on purpose: a skill
|
|
17
17
|
* description would be paid for on every request in every session, while this
|
|
18
|
-
* text is only ever sent while
|
|
18
|
+
* text is only ever sent while spec review is on.
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
export const
|
|
22
|
-
"[ASK-
|
|
23
|
-
"You are
|
|
21
|
+
export const SPEC_MODE_BANNER = [
|
|
22
|
+
"[ASK-SPEC — SPEC REVIEW]",
|
|
23
|
+
"You are writing a spec with the operator before any code is written.",
|
|
24
24
|
"",
|
|
25
25
|
"Allowed now: reading tools, bash for inspection, and writing/editing",
|
|
26
26
|
"markdown files inside the project. Not allowed: editing other files, destructive commands,",
|
|
27
|
-
"or starting the implementation — that begins only after the
|
|
27
|
+
"or starting the implementation — that begins only after the spec is approved.",
|
|
28
28
|
"",
|
|
29
|
-
"Loop: explore → ask what the code cannot tell you → write the spec file →
|
|
30
|
-
"End every turn with either a question or a
|
|
29
|
+
"Loop: explore → ask what the code cannot tell you → write the spec file → spec_submit.",
|
|
30
|
+
"End every turn with either a question or a spec_submit call. Do not end it silently.",
|
|
31
31
|
].join("\n");
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* The interview half of
|
|
34
|
+
* The interview half of the review. Deliberately capability-neutral: the session's
|
|
35
35
|
* question tool is a third-party extension in most installs, so the prompt says
|
|
36
36
|
* "the session's question tool" and the caller adds a line about availability —
|
|
37
37
|
* naming a tool the harness does not have sends the model into an empty call.
|
|
38
38
|
*/
|
|
39
39
|
export const INTERVIEW_GUIDE = [
|
|
40
|
-
"[ASK-
|
|
40
|
+
"[ASK-SPEC — INTERVIEW]",
|
|
41
41
|
"Settle ambiguity by asking, in rounds, before you write the spec — not after.",
|
|
42
42
|
"Use the session's question tool when one is available; otherwise ask in plain text and wait.",
|
|
43
43
|
"One round = 1-4 questions, each with concrete options when the choice is enumerable.",
|
|
44
44
|
"Ask about what the code cannot tell you: scope, precedence between existing behaviours,",
|
|
45
45
|
"error handling, and what must not change. Do not ask what you can read from the repo.",
|
|
46
|
-
"Stop asking when the next answer would not change the
|
|
46
|
+
"Stop asking when the next answer would not change the spec you are about to write.",
|
|
47
47
|
].join("\n");
|
|
48
48
|
|
|
49
49
|
export const SPEC_FRAMEWORK_GUIDE = [
|
|
50
|
-
"[ASK-
|
|
51
|
-
"Write the
|
|
50
|
+
"[ASK-SPEC — SPEC FRAMEWORK]",
|
|
51
|
+
"Write the spec as one markdown file (convention: `specs/<slug>.md`) using roughly this shape.",
|
|
52
52
|
"Adapt it: drop sections that do not apply, merge two when they are thin, but keep the order —",
|
|
53
53
|
"the operator reads it top to bottom and reviews it paragraph by paragraph.",
|
|
54
54
|
"",
|
|
@@ -82,29 +82,29 @@ export const SPEC_FRAMEWORK_GUIDE = [
|
|
|
82
82
|
"### Revising after feedback",
|
|
83
83
|
"Annotations arrive whatever the decision — approving means \"go\", not \"notes ignored\".",
|
|
84
84
|
"When revising: change only the blocks that were annotated, keep the rest byte for byte, then",
|
|
85
|
-
"call
|
|
86
|
-
"operator has to re-read the whole
|
|
85
|
+
"call spec_submit again. A full rewrite makes the diff useless: every block turns red and the",
|
|
86
|
+
"operator has to re-read the whole spec to find your three changes.",
|
|
87
87
|
].join("\n");
|
|
88
88
|
|
|
89
|
-
/** The complete
|
|
90
|
-
export function
|
|
89
|
+
/** The complete spec-review system-prompt block. */
|
|
90
|
+
export function buildSpecSystemPrompt(options: { questionTool?: boolean } = {}): string {
|
|
91
91
|
const questionLine = options.questionTool
|
|
92
92
|
? "`ask_user_question` is available in this session and is the preferred way to ask."
|
|
93
93
|
: "This session exposes no question tool: ask in plain text and wait for the reply.";
|
|
94
|
-
return `${
|
|
94
|
+
return `${SPEC_MODE_BANNER}\n${questionLine}\n\n${INTERVIEW_GUIDE}\n\n${SPEC_FRAMEWORK_GUIDE}`;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
|
-
* One-shot kickoff text for `/ask-
|
|
98
|
+
* One-shot kickoff text for `/ask-spec <topic>`: sets the task and points at the
|
|
99
99
|
* framework without repeating it (it is already in the system prompt).
|
|
100
100
|
*/
|
|
101
|
-
export function
|
|
101
|
+
export function buildSpecKickoff(topic: string | undefined): string {
|
|
102
102
|
const subject = topic?.trim();
|
|
103
103
|
return [
|
|
104
|
-
"Start
|
|
104
|
+
"Start spec review.",
|
|
105
105
|
subject ? `Task: ${subject}` : "Task: the work we have been discussing in this session.",
|
|
106
106
|
"",
|
|
107
107
|
"Explore the code before writing, ask me what you cannot infer, then write the spec file and",
|
|
108
|
-
"submit it with
|
|
108
|
+
"submit it with spec_submit. Do not implement anything yet.",
|
|
109
109
|
].join("\n");
|
|
110
110
|
}
|
|
@@ -23,8 +23,8 @@ describe("renderBlockHtml", () => {
|
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
it("renders inline code, bold, italic and links", () => {
|
|
26
|
-
const html = render("use `
|
|
27
|
-
expect(html).toContain("<code>
|
|
26
|
+
const html = render("use `spec_submit` and **never** *skip* [the spec](https://example.com/x)");
|
|
27
|
+
expect(html).toContain("<code>spec_submit</code>");
|
|
28
28
|
expect(html).toContain("<strong>never</strong>");
|
|
29
29
|
expect(html).toContain("<em>skip</em>");
|
|
30
30
|
expect(html).toContain('href="https://example.com/x"');
|
|
@@ -5,8 +5,8 @@ import { join } from "node:path";
|
|
|
5
5
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
6
6
|
import { parseBlocks } from "./blocks.js";
|
|
7
7
|
import { renderBlockHtml } from "./render.js";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { buildSpecState, normalizeAnnotations, startSpecServer, type SpecServerHandle } from "./server.js";
|
|
9
|
+
import { appendSpecVersion, createSpecSession, type SpecSession } from "./store.js";
|
|
10
10
|
|
|
11
11
|
let agentDir: string;
|
|
12
12
|
|
|
@@ -18,8 +18,8 @@ afterAll(() => {
|
|
|
18
18
|
rmSync(agentDir, { recursive: true, force: true });
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
function makeSession(text = "##
|
|
22
|
-
return
|
|
21
|
+
function makeSession(text = "## Spec\n\nfirst step"): SpecSession {
|
|
22
|
+
return createSpecSession({
|
|
23
23
|
agentDir,
|
|
24
24
|
slug: `srv-${Math.random().toString(36).slice(2, 8)}`,
|
|
25
25
|
text,
|
|
@@ -28,10 +28,10 @@ function makeSession(text = "## Plan\n\nfirst step"): PlanSession {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
async function serve(
|
|
31
|
-
session:
|
|
32
|
-
overrides: Partial<Parameters<typeof
|
|
33
|
-
): Promise<
|
|
34
|
-
return
|
|
31
|
+
session: SpecSession,
|
|
32
|
+
overrides: Partial<Parameters<typeof startSpecServer>[0]> = {},
|
|
33
|
+
): Promise<SpecServerHandle> {
|
|
34
|
+
return startSpecServer({
|
|
35
35
|
session,
|
|
36
36
|
assetsDir: join(agentDir, "no-assets"),
|
|
37
37
|
render: renderBlockHtml,
|
|
@@ -118,21 +118,21 @@ function openStream(port: number, path: string): {
|
|
|
118
118
|
};
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
describe("
|
|
121
|
+
describe("buildSpecState", () => {
|
|
122
122
|
it("shows no badges for the first revision", () => {
|
|
123
|
-
const session = makeSession("##
|
|
124
|
-
const state =
|
|
123
|
+
const session = makeSession("## Spec\n\none\n\ntwo");
|
|
124
|
+
const state = buildSpecState(session);
|
|
125
125
|
expect(state.items.map((item) => item.status)).toEqual(["unchanged", "unchanged", "unchanged"]);
|
|
126
126
|
expect(state.totalVersions).toBe(1);
|
|
127
127
|
});
|
|
128
128
|
|
|
129
129
|
it("carries the diff statuses and the previous-block ids for a later revision", () => {
|
|
130
|
-
const session = makeSession("##
|
|
130
|
+
const session = makeSession("## Spec\n\nwrite the block parser before the server\n\nsecond step");
|
|
131
131
|
const first = session.versions[0];
|
|
132
|
-
|
|
133
|
-
text: "##
|
|
132
|
+
appendSpecVersion(session, {
|
|
133
|
+
text: "## Spec\n\nwrite the block parser before the HTTP server\n\nsecond step\n\nthird step",
|
|
134
134
|
});
|
|
135
|
-
const state =
|
|
135
|
+
const state = buildSpecState(session);
|
|
136
136
|
expect(state.version).toBe(2);
|
|
137
137
|
expect(state.items.map((item) => item.status)).toEqual([
|
|
138
138
|
"unchanged",
|
|
@@ -146,7 +146,7 @@ describe("buildPlanState", () => {
|
|
|
146
146
|
|
|
147
147
|
it("refuses an unknown revision", () => {
|
|
148
148
|
const session = makeSession();
|
|
149
|
-
expect(() =>
|
|
149
|
+
expect(() => buildSpecState(session, 9)).toThrow(/no spec revision 9/);
|
|
150
150
|
});
|
|
151
151
|
});
|
|
152
152
|
|
|
@@ -175,7 +175,7 @@ describe("normalizeAnnotations", () => {
|
|
|
175
175
|
});
|
|
176
176
|
});
|
|
177
177
|
|
|
178
|
-
describe("
|
|
178
|
+
describe("startSpecServer", () => {
|
|
179
179
|
it("serves the fallback page and refuses a missing token", async () => {
|
|
180
180
|
const session = makeSession();
|
|
181
181
|
const server = await serve(session);
|
|
@@ -193,13 +193,13 @@ describe("startPlanServer", () => {
|
|
|
193
193
|
});
|
|
194
194
|
|
|
195
195
|
it("serves state with rendered block html", async () => {
|
|
196
|
-
const session = makeSession("##
|
|
196
|
+
const session = makeSession("## Spec\n\nuse **bold** here");
|
|
197
197
|
const server = await serve(session);
|
|
198
198
|
try {
|
|
199
199
|
const response = await call({ port: server.port, path: `/api/state?t=${server.token}` });
|
|
200
200
|
expect(response.status).toBe(200);
|
|
201
201
|
const state = response.json() as { items: Array<{ html: string }> };
|
|
202
|
-
expect(state.items[0].html).toBe("<h2>
|
|
202
|
+
expect(state.items[0].html).toBe("<h2>Spec</h2>");
|
|
203
203
|
expect(state.items[1].html).toContain("<strong>bold</strong>");
|
|
204
204
|
} finally {
|
|
205
205
|
await server.close();
|
|
@@ -229,7 +229,7 @@ describe("startPlanServer", () => {
|
|
|
229
229
|
|
|
230
230
|
it("refuses to annotate an older revision", async () => {
|
|
231
231
|
const session = makeSession("one");
|
|
232
|
-
|
|
232
|
+
appendSpecVersion(session, { text: "two" });
|
|
233
233
|
const server = await serve(session);
|
|
234
234
|
try {
|
|
235
235
|
const response = await call({
|
|
@@ -362,7 +362,7 @@ describe("startPlanServer", () => {
|
|
|
362
362
|
const stream = openStream(server.port, `/api/events?t=${server.token}`);
|
|
363
363
|
try {
|
|
364
364
|
expect(await stream.until(": connected")).toContain(": connected");
|
|
365
|
-
|
|
365
|
+
appendSpecVersion(session, { text: "two" });
|
|
366
366
|
expect(parseBlocks("two")).toHaveLength(1);
|
|
367
367
|
server.broadcast("revision", { version: 2 });
|
|
368
368
|
expect(await stream.until("event: revision")).toContain('"version":2');
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* spec server — the review UI, served from inside the pi process.
|
|
3
3
|
*
|
|
4
4
|
* Shape of the contract:
|
|
5
5
|
* - one revision is reviewed at a time; the page pulls `/api/state` and then
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* shortly after, so a finished review cannot keep a port open.
|
|
13
13
|
*
|
|
14
14
|
* The HTTP shell (token, Host allowlist, Origin refusal, port walk, static
|
|
15
|
-
* files, SSE) lives in `src/lib/web-review.ts`; this file is the
|
|
15
|
+
* files, SSE) lives in `src/lib/web-review.ts`; this file is the spec-specific
|
|
16
16
|
* routes and payload.
|
|
17
17
|
*/
|
|
18
18
|
import { fileURLToPath } from "node:url";
|
|
@@ -21,16 +21,16 @@ import type { BlockStatus, DiffEntry } from "./diff.js";
|
|
|
21
21
|
import type { Annotation, BlockAnnotations } from "./feedback.js";
|
|
22
22
|
import {
|
|
23
23
|
latestVersion,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
type
|
|
27
|
-
type
|
|
24
|
+
recordSpecDecision,
|
|
25
|
+
setSpecAnnotations,
|
|
26
|
+
type SpecAnnotations,
|
|
27
|
+
type SpecSession,
|
|
28
28
|
} from "./store.js";
|
|
29
29
|
import { bindHost, isWsl, urlHost } from "../../lib/opener.js";
|
|
30
30
|
import { startWebServer, type WebServerHandle } from "../../lib/web-review.js";
|
|
31
31
|
import { renderBlockHtml } from "./render.js";
|
|
32
32
|
|
|
33
|
-
export interface
|
|
33
|
+
export interface SpecStateItem {
|
|
34
34
|
status: BlockStatus;
|
|
35
35
|
id: string;
|
|
36
36
|
prevId?: string;
|
|
@@ -40,14 +40,14 @@ export interface PlanStateItem {
|
|
|
40
40
|
label: string;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export interface
|
|
43
|
+
export interface SpecStatePayload {
|
|
44
44
|
slug: string;
|
|
45
45
|
version: number;
|
|
46
46
|
totalVersions: number;
|
|
47
47
|
at: string;
|
|
48
48
|
source?: string;
|
|
49
|
-
items:
|
|
50
|
-
annotations:
|
|
49
|
+
items: SpecStateItem[];
|
|
50
|
+
annotations: SpecAnnotations;
|
|
51
51
|
decision?: { action: "approve" | "deny"; at: string };
|
|
52
52
|
decided: boolean;
|
|
53
53
|
/** Earlier revisions, newest first — the history drawer. */
|
|
@@ -55,12 +55,12 @@ export interface PlanStatePayload {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/** Compose what the page renders: blocks of one revision, tagged with their diff status. */
|
|
58
|
-
export function
|
|
58
|
+
export function buildSpecState(session: SpecSession, versionNumber?: number): SpecStatePayload {
|
|
59
59
|
const version =
|
|
60
60
|
versionNumber === undefined
|
|
61
61
|
? latestVersion(session)
|
|
62
62
|
: session.versions.find((candidate) => candidate.n === versionNumber);
|
|
63
|
-
if (!version) throw new Error(`no
|
|
63
|
+
if (!version) throw new Error(`no spec revision ${versionNumber} in ${session.dir}`);
|
|
64
64
|
const previous = session.versions.find((candidate) => candidate.n === version.n - 1);
|
|
65
65
|
|
|
66
66
|
const byId = new Map<string, Block>(version.blocks.map((block) => [block.id, block]));
|
|
@@ -73,7 +73,7 @@ export function buildPlanState(session: PlanSession, versionNumber?: number): Pl
|
|
|
73
73
|
? version.diff
|
|
74
74
|
: version.blocks.map((block) => ({ status: "unchanged" as const, id: block.id }));
|
|
75
75
|
|
|
76
|
-
const items:
|
|
76
|
+
const items: SpecStateItem[] = [];
|
|
77
77
|
for (const entry of entries) {
|
|
78
78
|
const current = byId.get(entry.id);
|
|
79
79
|
const source = current ?? previousById.get(entry.id);
|
|
@@ -107,8 +107,8 @@ export function buildPlanState(session: PlanSession, versionNumber?: number): Pl
|
|
|
107
107
|
};
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
export interface
|
|
111
|
-
session:
|
|
110
|
+
export interface StartSpecServerOptions {
|
|
111
|
+
session: SpecSession;
|
|
112
112
|
/** Directory with the built UI. Defaults to `./assets/` next to this module. */
|
|
113
113
|
assetsDir?: string;
|
|
114
114
|
portStart?: number;
|
|
@@ -118,13 +118,13 @@ export interface StartPlanServerOptions {
|
|
|
118
118
|
/** Render a block to HTML (injected so tests can skip the renderer). */
|
|
119
119
|
render?: (block: Pick<Block, "kind" | "text">) => string;
|
|
120
120
|
onDecision?: (decision: { action: "approve" | "deny"; version: number }) => void;
|
|
121
|
-
onAnnotations?: (annotations:
|
|
121
|
+
onAnnotations?: (annotations: SpecAnnotations, version: number) => void;
|
|
122
122
|
/** Overridable for tests: bind address and host used in the URL. */
|
|
123
123
|
bindHost?: string;
|
|
124
124
|
urlHost?: string;
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
export interface
|
|
127
|
+
export interface SpecServerHandle {
|
|
128
128
|
url: string;
|
|
129
129
|
port: number;
|
|
130
130
|
token: string;
|
|
@@ -137,7 +137,7 @@ function isVerdict(value: unknown): value is Annotation["verdict"] {
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
/** Normalize whatever the page sent into the store's annotation shape. */
|
|
140
|
-
export function normalizeAnnotations(input: unknown):
|
|
140
|
+
export function normalizeAnnotations(input: unknown): SpecAnnotations {
|
|
141
141
|
const raw = (input ?? {}) as { global?: unknown; blocks?: unknown };
|
|
142
142
|
const toAnnotation = (value: unknown): Annotation | undefined => {
|
|
143
143
|
const candidate = value as { verdict?: unknown; comment?: unknown };
|
|
@@ -166,7 +166,7 @@ export function normalizeAnnotations(input: unknown): PlanAnnotations {
|
|
|
166
166
|
return { global, blocks };
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
export async function
|
|
169
|
+
export async function startSpecServer(options: StartSpecServerOptions): Promise<SpecServerHandle> {
|
|
170
170
|
const assetsDir = options.assetsDir ?? fileURLToPath(new URL("./assets/", import.meta.url));
|
|
171
171
|
const render = options.render ?? renderBlockHtml;
|
|
172
172
|
const closeDelayMs = options.closeDelayMs ?? 1500;
|
|
@@ -182,7 +182,7 @@ export async function startPlanServer(options: StartPlanServerOptions): Promise<
|
|
|
182
182
|
routes: async (ctx) => {
|
|
183
183
|
if (ctx.url.pathname === "/api/state" && ctx.req.method === "GET") {
|
|
184
184
|
const versionParam = ctx.url.searchParams.get("version");
|
|
185
|
-
const state =
|
|
185
|
+
const state = buildSpecState(
|
|
186
186
|
options.session,
|
|
187
187
|
versionParam ? Number(versionParam) : undefined,
|
|
188
188
|
);
|
|
@@ -212,7 +212,7 @@ export async function startPlanServer(options: StartPlanServerOptions): Promise<
|
|
|
212
212
|
ctx.json({ error: "only the latest undecided revision can be annotated" }, 409);
|
|
213
213
|
return true;
|
|
214
214
|
}
|
|
215
|
-
|
|
215
|
+
setSpecAnnotations(options.session, version, annotations);
|
|
216
216
|
options.onAnnotations?.(annotations, version);
|
|
217
217
|
ctx.broadcast("annotations", { version, annotations });
|
|
218
218
|
ctx.json({ ok: true, version });
|
|
@@ -244,7 +244,7 @@ export async function startPlanServer(options: StartPlanServerOptions): Promise<
|
|
|
244
244
|
);
|
|
245
245
|
return true;
|
|
246
246
|
}
|
|
247
|
-
|
|
247
|
+
recordSpecDecision(options.session, version, action);
|
|
248
248
|
ctx.json({ ok: true, action, version });
|
|
249
249
|
ctx.broadcast("decided", { action, version });
|
|
250
250
|
if (!closing) {
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
appendSpecVersion,
|
|
7
|
+
createSpecSession,
|
|
8
8
|
latestVersion,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
listSpecSessions,
|
|
10
|
+
loadSpecSession,
|
|
11
|
+
specSlug,
|
|
12
12
|
readVersionText,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
type
|
|
13
|
+
recordSpecDecision,
|
|
14
|
+
setSpecAnnotations,
|
|
15
|
+
type SpecSession,
|
|
16
16
|
} from "./store.js";
|
|
17
17
|
|
|
18
18
|
let agentDir: string;
|
|
@@ -27,8 +27,8 @@ afterAll(() => {
|
|
|
27
27
|
|
|
28
28
|
const at = (seconds: number) => new Date(Date.UTC(2026, 10, 11, 0, 0, seconds));
|
|
29
29
|
|
|
30
|
-
function makeSession(text = "##
|
|
31
|
-
return
|
|
30
|
+
function makeSession(text = "## Spec\n\nfirst step"): SpecSession {
|
|
31
|
+
return createSpecSession({
|
|
32
32
|
agentDir,
|
|
33
33
|
slug: `plan-${Math.random().toString(36).slice(2, 8)}`,
|
|
34
34
|
text,
|
|
@@ -37,40 +37,72 @@ function makeSession(text = "## Plan\n\nfirst step"): PlanSession {
|
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
describe("
|
|
40
|
+
describe("specSlug", () => {
|
|
41
41
|
it("lowercases, collapses separators and keeps CJK", () => {
|
|
42
|
-
expect(
|
|
43
|
-
expect(
|
|
44
|
-
expect(
|
|
42
|
+
expect(specSlug("Ask-Spec: Web UI!")).toBe("ask-spec-web-ui");
|
|
43
|
+
expect(specSlug("计划 评审")).toContain("计划");
|
|
44
|
+
expect(specSlug(" ")).toBe("spec");
|
|
45
45
|
});
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
-
describe("
|
|
48
|
+
describe("legacy store", () => {
|
|
49
|
+
it("migrates the pre-rename ask-plan directory on first use", () => {
|
|
50
|
+
const isolated = mkdtempSync(join(tmpdir(), "pi-spec-legacy-"));
|
|
51
|
+
const legacy = join(isolated, "ask-plan");
|
|
52
|
+
const old = join(legacy, "old");
|
|
53
|
+
mkdirSync(old, { recursive: true });
|
|
54
|
+
writeFileSync(
|
|
55
|
+
join(old, "state.json"),
|
|
56
|
+
JSON.stringify({ slug: "old", dir: old, createdAt: at(0).toISOString(), versions: [] }),
|
|
57
|
+
);
|
|
58
|
+
expect(listSpecSessions(isolated)).toContain(join(isolated, "ask-spec", "old"));
|
|
59
|
+
expect(existsSync(legacy)).toBe(false);
|
|
60
|
+
rmSync(isolated, { recursive: true, force: true });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("keeps legacy sessions visible when both store directories exist", () => {
|
|
64
|
+
const isolated = mkdtempSync(join(tmpdir(), "pi-spec-both-stores-"));
|
|
65
|
+
for (const [store, slug] of [["ask-spec", "new"], ["ask-plan", "old"]]) {
|
|
66
|
+
const dir = join(isolated, store, slug);
|
|
67
|
+
mkdirSync(dir, { recursive: true });
|
|
68
|
+
writeFileSync(
|
|
69
|
+
join(dir, "state.json"),
|
|
70
|
+
JSON.stringify({ slug, dir, createdAt: at(0).toISOString(), versions: [] }),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
expect(listSpecSessions(isolated)).toEqual(
|
|
74
|
+
expect.arrayContaining([join(isolated, "ask-spec", "new"), join(isolated, "ask-plan", "old")]),
|
|
75
|
+
);
|
|
76
|
+
rmSync(isolated, { recursive: true, force: true });
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("createSpecSession", () => {
|
|
49
81
|
it("writes the first revision and the state file", () => {
|
|
50
82
|
const session = makeSession();
|
|
51
83
|
expect(session.versions).toHaveLength(1);
|
|
52
84
|
expect(session.versions[0].n).toBe(1);
|
|
53
85
|
expect(session.versions[0].diff).toEqual([]);
|
|
54
|
-
expect(readVersionText(session, 1)).toBe("##
|
|
86
|
+
expect(readVersionText(session, 1)).toBe("## Spec\n\nfirst step");
|
|
55
87
|
const onDisk = JSON.parse(readFileSync(join(session.dir, "state.json"), "utf8"));
|
|
56
88
|
expect(onDisk.versions).toHaveLength(1);
|
|
57
89
|
expect(onDisk.slug).toBe(session.slug);
|
|
58
90
|
});
|
|
59
91
|
|
|
60
92
|
it("allocates a second directory for a slug that already exists", () => {
|
|
61
|
-
const first =
|
|
62
|
-
const second =
|
|
93
|
+
const first = createSpecSession({ agentDir, slug: "same", text: "a" });
|
|
94
|
+
const second = createSpecSession({ agentDir, slug: "same", text: "b" });
|
|
63
95
|
expect(first.slug).toBe("same");
|
|
64
96
|
expect(second.slug).toBe("same-2");
|
|
65
97
|
expect(first.dir).not.toBe(second.dir);
|
|
66
98
|
});
|
|
67
99
|
});
|
|
68
100
|
|
|
69
|
-
describe("
|
|
101
|
+
describe("appendSpecVersion", () => {
|
|
70
102
|
it("numbers revisions and diffs against the previous one", () => {
|
|
71
|
-
const session = makeSession("##
|
|
72
|
-
const second =
|
|
73
|
-
text: "##
|
|
103
|
+
const session = makeSession("## Spec\n\nfirst step\n\nsecond step");
|
|
104
|
+
const second = appendSpecVersion(session, {
|
|
105
|
+
text: "## Spec\n\nfirst step edited\n\nsecond step",
|
|
74
106
|
now: at(10),
|
|
75
107
|
});
|
|
76
108
|
expect(second.n).toBe(2);
|
|
@@ -84,9 +116,9 @@ describe("appendPlanVersion", () => {
|
|
|
84
116
|
|
|
85
117
|
it("keeps at most 50 revisions and never drops a decided one", () => {
|
|
86
118
|
const session = makeSession("v1");
|
|
87
|
-
|
|
119
|
+
recordSpecDecision(session, 1, "approve", at(1));
|
|
88
120
|
for (let i = 2; i <= 53; i += 1) {
|
|
89
|
-
|
|
121
|
+
appendSpecVersion(session, { text: `v${i}`, now: at(i) });
|
|
90
122
|
}
|
|
91
123
|
expect(session.versions.length).toBeLessThanOrEqual(50);
|
|
92
124
|
expect(session.versions.some((version) => version.n === 1 && version.decision?.action === "approve")).toBe(true);
|
|
@@ -95,7 +127,7 @@ describe("appendPlanVersion", () => {
|
|
|
95
127
|
|
|
96
128
|
// The next identity follows the highest revision number, not the number
|
|
97
129
|
// of retained entries — otherwise pruning reuses 51 and overwrites it.
|
|
98
|
-
const next =
|
|
130
|
+
const next = appendSpecVersion(session, { text: "v54", now: at(54) });
|
|
99
131
|
expect(next.n).toBe(54);
|
|
100
132
|
expect(new Set(session.versions.map((version) => version.n)).size).toBe(session.versions.length);
|
|
101
133
|
expect(readVersionText(session, 54)).toBe("v54");
|
|
@@ -105,19 +137,19 @@ describe("appendPlanVersion", () => {
|
|
|
105
137
|
describe("annotations and decisions", () => {
|
|
106
138
|
it("persists annotations on the revision they belong to", () => {
|
|
107
139
|
const session = makeSession();
|
|
108
|
-
|
|
140
|
+
setSpecAnnotations(session, 1, {
|
|
109
141
|
global: [{ verdict: "change", comment: "rethink scope" }],
|
|
110
142
|
blocks: [{ blockId: session.versions[0].blocks[0].id, annotations: [{ verdict: "question", comment: "why?" }] }],
|
|
111
143
|
});
|
|
112
|
-
const reloaded =
|
|
144
|
+
const reloaded = loadSpecSession(session.dir);
|
|
113
145
|
expect(reloaded.versions[0].annotations.global).toHaveLength(1);
|
|
114
146
|
expect(reloaded.versions[0].annotations.blocks[0].annotations[0].verdict).toBe("question");
|
|
115
147
|
});
|
|
116
148
|
|
|
117
149
|
it("records a decision with a timestamp", () => {
|
|
118
150
|
const session = makeSession();
|
|
119
|
-
|
|
120
|
-
expect(
|
|
151
|
+
recordSpecDecision(session, 1, "approve", at(5));
|
|
152
|
+
expect(loadSpecSession(session.dir).versions[0].decision).toEqual({
|
|
121
153
|
action: "approve",
|
|
122
154
|
at: at(5).toISOString(),
|
|
123
155
|
});
|
|
@@ -125,21 +157,31 @@ describe("annotations and decisions", () => {
|
|
|
125
157
|
|
|
126
158
|
it("refuses a revision that does not exist", () => {
|
|
127
159
|
const session = makeSession();
|
|
128
|
-
expect(() =>
|
|
129
|
-
expect(() =>
|
|
160
|
+
expect(() => recordSpecDecision(session, 99, "approve")).toThrow(/no spec revision 99/);
|
|
161
|
+
expect(() => setSpecAnnotations(session, 99, { global: [], blocks: [] })).toThrow(/no spec revision 99/);
|
|
130
162
|
});
|
|
131
163
|
});
|
|
132
164
|
|
|
133
|
-
describe("
|
|
165
|
+
describe("loadSpecSession", () => {
|
|
134
166
|
it("reports a corrupt state file with its path", () => {
|
|
135
167
|
const session = makeSession();
|
|
136
168
|
writeFileSync(join(session.dir, "state.json"), "{not json");
|
|
137
|
-
expect(() =>
|
|
169
|
+
expect(() => loadSpecSession(session.dir)).toThrow(/cannot read spec state at .*state\.json/);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("keeps paths in empty-session and non-object state errors", () => {
|
|
173
|
+
const session = makeSession();
|
|
174
|
+
writeFileSync(join(session.dir, "state.json"), "null");
|
|
175
|
+
expect(() => loadSpecSession(session.dir)).toThrow(
|
|
176
|
+
`spec state at ${join(session.dir, "state.json")} is not an object`,
|
|
177
|
+
);
|
|
178
|
+
session.versions = [];
|
|
179
|
+
expect(() => latestVersion(session)).toThrow(`spec session ${session.dir} has no revisions`);
|
|
138
180
|
});
|
|
139
181
|
|
|
140
182
|
it("lists sessions that have a state file", () => {
|
|
141
183
|
const session = makeSession();
|
|
142
|
-
const dirs =
|
|
184
|
+
const dirs = listSpecSessions(agentDir);
|
|
143
185
|
expect(dirs).toContain(session.dir);
|
|
144
186
|
});
|
|
145
187
|
});
|