@andares/pi-toolkits 0.14.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 +23 -0
- package/README.md +101 -34
- package/README.zh-CN.md +68 -32
- package/package.json +10 -3
- package/src/features/ask/index.test.ts +15 -0
- package/src/features/ask/index.ts +18 -1
- package/src/features/init/audit.test.ts +19 -2
- package/src/features/init/audit.ts +117 -15
- package/src/features/init/index.test.ts +123 -1
- package/src/features/init/index.ts +156 -46
- package/src/features/init/parity.test.ts +176 -0
- package/src/features/init/parity.ts +229 -0
- package/src/features/init/plan.ts +25 -3
- package/src/features/init/refresh.test.ts +88 -1
- package/src/features/init/refresh.ts +43 -64
- package/src/features/init/spec.ts +11 -0
- package/src/features/init/templates/memory-readme.md +9 -2
- package/src/features/init/templates/workflow-block.md +2 -2
- package/src/features/init/test-helpers.ts +1 -0
- package/src/features/init/workflow.ts +159 -0
- package/src/features/permission/index.ts +1 -1
- package/src/features/quit-guard/constants.ts +18 -5
- package/src/features/quit-guard/index.ts +75 -29
- package/src/features/quit-guard/killer.test.ts +77 -0
- package/src/features/quit-guard/killer.ts +215 -0
- package/src/features/review/index.ts +14 -0
- package/src/features/review/review.test.ts +34 -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 +148 -63
- package/src/features/{plan → spec}/index.ts +136 -105
- 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 +20 -5
- package/src/features/uat/index.ts +16 -13
- package/src/features/uat/record-tool.ts +6 -5
- package/src/features/uat/records.test.ts +5 -5
- package/src/features/uat/state.ts +13 -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 +50 -0
- 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,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* ask-
|
|
2
|
+
* ask-spec — spec mode with a browser review round.
|
|
3
3
|
*
|
|
4
|
-
* The flow: `/ask-
|
|
5
|
-
* writes only), the agent writes a spec and calls `
|
|
4
|
+
* The flow: `/ask-spec` opens a spec-review phase (read tools, questions, markdown
|
|
5
|
+
* writes only), the agent writes a spec and calls `spec_submit`, the review
|
|
6
6
|
* server opens the page in the operator's browser, annotations come back as a
|
|
7
|
-
* message, and the loop repeats until the
|
|
7
|
+
* message, and the loop repeats until the spec is approved — then the phase ends
|
|
8
8
|
* and the agent implements.
|
|
9
9
|
*
|
|
10
10
|
* Three properties are load-bearing:
|
|
11
|
-
* - `
|
|
11
|
+
* - `spec_submit` returns immediately. Waiting for the decision inside the
|
|
12
12
|
* tool would pin the agent for as long as the operator takes to read; the
|
|
13
13
|
* decision arrives later as a normal user message instead.
|
|
14
14
|
* - the decision always carries the annotations. Approve and deny only choose
|
|
@@ -18,40 +18,44 @@
|
|
|
18
18
|
* plannotator's habit of switching models silently is the reason this one
|
|
19
19
|
* is written from scratch.
|
|
20
20
|
*/
|
|
21
|
-
import { readFileSync
|
|
22
|
-
import { isAbsolute,
|
|
21
|
+
import { readFileSync } from "node:fs";
|
|
22
|
+
import { isAbsolute, relative, resolve } from "node:path";
|
|
23
23
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
24
24
|
import { Type } from "typebox";
|
|
25
25
|
import type { ExtensionAPI, ExtensionContext, ToolCallEvent } from "@earendil-works/pi-coding-agent";
|
|
26
26
|
import type { GateHub } from "../../lib/gates.js";
|
|
27
27
|
import { getPermissionControl, type PermissionSnapshot } from "../../lib/permission-control.js";
|
|
28
28
|
import { getAskControl } from "../../lib/ask-control.js";
|
|
29
|
+
import { LEGACY_SPEC_PHASE_TOOL, SPEC_PHASE_TOOL, UAT_PHASE_TOOL } from "../../lib/phases.js";
|
|
30
|
+
import { frameworkReadiness } from "../init/parity.js";
|
|
31
|
+
import { resolveProjectRoot } from "../uat/state.js";
|
|
29
32
|
import { canonicalize, isWithin } from "../permission/paths.js";
|
|
30
33
|
import { parseCommandUnits } from "../permission/units.js";
|
|
31
34
|
import { buildFeedbackMarkdown } from "./feedback.js";
|
|
32
35
|
import { openInBrowser } from "../../lib/opener.js";
|
|
33
|
-
import {
|
|
36
|
+
import { buildSpecKickoff, buildSpecSystemPrompt } from "./prompt.js";
|
|
34
37
|
import { renderBlockHtml } from "./render.js";
|
|
35
|
-
import {
|
|
38
|
+
import { buildSpecState, startSpecServer, type SpecServerHandle } from "./server.js";
|
|
36
39
|
import {
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
appendSpecVersion,
|
|
41
|
+
createSpecSession,
|
|
39
42
|
latestVersion,
|
|
40
|
-
|
|
43
|
+
specSlug,
|
|
41
44
|
readVersionText,
|
|
42
|
-
type
|
|
45
|
+
type SpecSession,
|
|
43
46
|
} from "./store.js";
|
|
44
47
|
|
|
45
|
-
export const
|
|
46
|
-
export const
|
|
48
|
+
export const SPEC_COMMAND = "ask-spec";
|
|
49
|
+
export const SPEC_COMMAND_ALIAS = "ask-plan";
|
|
50
|
+
export const SPEC_SUBMIT_TOOL = SPEC_PHASE_TOOL;
|
|
47
51
|
|
|
48
|
-
/** Files the
|
|
52
|
+
/** Files the spec-review phase may write: project markdown, nothing else. */
|
|
49
53
|
function isMarkdownPath(path: string): boolean {
|
|
50
54
|
return /\.mdx?$/i.test(path);
|
|
51
55
|
}
|
|
52
56
|
|
|
53
57
|
/** Resolve a markdown path inside the workspace; anything else is rejected. */
|
|
54
|
-
function
|
|
58
|
+
function specMarkdownPath(path: string, cwd: string): string | undefined {
|
|
55
59
|
if (path === "" || !isMarkdownPath(path)) return undefined;
|
|
56
60
|
const absolute = canonicalize(isAbsolute(path) ? path : resolve(cwd, path));
|
|
57
61
|
if (absolute === cwd || !isWithin(cwd, absolute)) return undefined;
|
|
@@ -76,17 +80,17 @@ function sessionIdOf(ctx: ExtensionContext): string {
|
|
|
76
80
|
}
|
|
77
81
|
}
|
|
78
82
|
|
|
79
|
-
const
|
|
83
|
+
const SPEC_SUBMIT_PARAMS = Type.Object({
|
|
80
84
|
path: Type.Optional(
|
|
81
85
|
Type.String({
|
|
82
86
|
description:
|
|
83
|
-
'Spec file to submit, relative to the workspace root (default: the file chosen when
|
|
87
|
+
'Spec file to submit, relative to the workspace root (default: the file chosen when spec mode started, e.g. "specs/ask-spec.md"). Write the file first — spec_submit reads it from disk.',
|
|
84
88
|
}),
|
|
85
89
|
),
|
|
86
90
|
});
|
|
87
91
|
|
|
88
|
-
export interface
|
|
89
|
-
/** `<agent-dir>`: review history lives at `<agentDir>/ask-
|
|
92
|
+
export interface SpecOptions {
|
|
93
|
+
/** `<agent-dir>`: review history lives at `<agentDir>/ask-spec/` (default: the host's). */
|
|
90
94
|
agentDir?: string;
|
|
91
95
|
/** Opening the browser is injectable so tests never spawn one. */
|
|
92
96
|
openBrowser?: (url: string) => unknown;
|
|
@@ -96,19 +100,19 @@ export interface PlanOptions {
|
|
|
96
100
|
portStart?: number;
|
|
97
101
|
}
|
|
98
102
|
|
|
99
|
-
export function
|
|
103
|
+
export function registerSpec(pi: ExtensionAPI, gates: GateHub, options: SpecOptions = {}): void {
|
|
100
104
|
const open = options.openBrowser ?? openInBrowser;
|
|
101
105
|
const agentDir = options.agentDir ?? getAgentDir();
|
|
102
106
|
|
|
103
107
|
let active = false;
|
|
104
|
-
let session:
|
|
105
|
-
let server:
|
|
108
|
+
let session: SpecSession | undefined;
|
|
109
|
+
let server: SpecServerHandle | undefined;
|
|
106
110
|
let permissionBefore: PermissionSnapshot | undefined;
|
|
107
111
|
let permissionDuring: PermissionSnapshot | undefined;
|
|
108
112
|
let specPath: string | undefined;
|
|
109
113
|
let cwd = process.cwd();
|
|
110
114
|
let ctxRef: ExtensionContext | undefined;
|
|
111
|
-
let
|
|
115
|
+
let specSessionId: string | undefined;
|
|
112
116
|
|
|
113
117
|
function closeServer(): void {
|
|
114
118
|
if (!server) return;
|
|
@@ -117,12 +121,16 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
117
121
|
}
|
|
118
122
|
|
|
119
123
|
function toolSetWithSubmit(): string[] {
|
|
120
|
-
const tools = pi
|
|
121
|
-
|
|
124
|
+
const tools = pi
|
|
125
|
+
.getActiveTools()
|
|
126
|
+
.filter((name) => name !== SPEC_SUBMIT_TOOL && name !== LEGACY_SPEC_PHASE_TOOL);
|
|
127
|
+
return [...tools, SPEC_SUBMIT_TOOL];
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
function toolSetWithoutSubmit(): string[] {
|
|
125
|
-
return pi
|
|
131
|
+
return pi
|
|
132
|
+
.getActiveTools()
|
|
133
|
+
.filter((name) => name !== SPEC_SUBMIT_TOOL && name !== LEGACY_SPEC_PHASE_TOOL);
|
|
126
134
|
}
|
|
127
135
|
|
|
128
136
|
function leave(ctx: ExtensionContext, reason: string): void {
|
|
@@ -132,8 +140,8 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
132
140
|
pi.setActiveTools(toolSetWithoutSubmit());
|
|
133
141
|
const control = getPermissionControl();
|
|
134
142
|
const current = control?.snapshot();
|
|
135
|
-
// Restore only if the operator has not changed the policy mid-
|
|
136
|
-
// manual `/ask-perm` during
|
|
143
|
+
// Restore only if the operator has not changed the policy mid-review. A
|
|
144
|
+
// manual `/ask-perm` during the spec review is newer intent and must win.
|
|
137
145
|
if (
|
|
138
146
|
control &&
|
|
139
147
|
permissionBefore &&
|
|
@@ -145,8 +153,8 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
145
153
|
}
|
|
146
154
|
permissionBefore = undefined;
|
|
147
155
|
permissionDuring = undefined;
|
|
148
|
-
|
|
149
|
-
ctx.ui.notify(`ask-
|
|
156
|
+
specSessionId = undefined;
|
|
157
|
+
ctx.ui.notify(`ask-spec: ${reason}`, "info");
|
|
150
158
|
}
|
|
151
159
|
|
|
152
160
|
/** Compose the message the model receives for one decision. */
|
|
@@ -167,13 +175,13 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
167
175
|
perBlock: current.annotations.blocks,
|
|
168
176
|
// Approval quotes the reviewed snapshot, not the file's current bytes:
|
|
169
177
|
// the model may have kept editing after submitting this revision.
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
// During
|
|
178
|
+
specPath: current.source ?? specPath,
|
|
179
|
+
specText: readApprovedText(version) ?? current.blocks.map((block) => block.text).join("\n\n"),
|
|
180
|
+
// During the review the tool is intentionally inactive. Recommend it on
|
|
173
181
|
// approval only when the project has the framework and the tool exists.
|
|
174
182
|
recordTool:
|
|
175
183
|
pi.getAllTools().some((tool) => tool.name === "record_change") &&
|
|
176
|
-
|
|
184
|
+
frameworkReadiness(resolveProjectRoot(cwd)).ready,
|
|
177
185
|
});
|
|
178
186
|
}
|
|
179
187
|
|
|
@@ -191,26 +199,26 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
191
199
|
decision: { action: "approve" | "deny"; version: number },
|
|
192
200
|
ctx: ExtensionContext,
|
|
193
201
|
): Promise<void> {
|
|
194
|
-
// The operator may leave
|
|
202
|
+
// The operator may leave spec mode, or switch sessions, during the short
|
|
195
203
|
// close delay. A stale decision must not resurrect implementation or send
|
|
196
204
|
// an empty message into the new phase.
|
|
197
205
|
if (!active) return;
|
|
198
206
|
const message = decisionMessage(decision.version);
|
|
199
207
|
if (decision.action === "approve") {
|
|
200
208
|
// Leave the phase before the message goes out: the implementation
|
|
201
|
-
// turn must not run under the
|
|
202
|
-
leave(ctx, "
|
|
209
|
+
// turn must not run under the spec-review gates.
|
|
210
|
+
leave(ctx, "spec approved — implementing.");
|
|
203
211
|
} else {
|
|
204
|
-
ctx.ui.notify("ask-
|
|
212
|
+
ctx.ui.notify("ask-spec: feedback sent — the agent will revise the spec.", "info");
|
|
205
213
|
}
|
|
206
214
|
pi.sendUserMessage(message);
|
|
207
215
|
}
|
|
208
216
|
|
|
209
217
|
// ── Command ────────────────────────────────────────────────────────────
|
|
210
218
|
|
|
211
|
-
|
|
219
|
+
const specCommand = {
|
|
212
220
|
description:
|
|
213
|
-
"
|
|
221
|
+
"Spec first: research, write a spec, review it in the browser with block annotations, then implement after approval. `off` leaves the phase, `open` re-opens the review.",
|
|
214
222
|
handler: async (args: string, ctx: ExtensionContext) => {
|
|
215
223
|
const arg = args.trim();
|
|
216
224
|
ctxRef = ctx;
|
|
@@ -218,73 +226,96 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
218
226
|
|
|
219
227
|
if (arg === "off") {
|
|
220
228
|
if (!active) {
|
|
221
|
-
ctx.ui.notify("ask-
|
|
229
|
+
ctx.ui.notify("ask-spec: not in spec mode.", "info");
|
|
222
230
|
return;
|
|
223
231
|
}
|
|
224
|
-
leave(ctx, specPath ? `
|
|
232
|
+
leave(ctx, specPath ? `spec mode off — draft left at ${specPath}.` : "spec mode off.");
|
|
225
233
|
return;
|
|
226
234
|
}
|
|
227
235
|
|
|
228
236
|
if (arg === "open") {
|
|
229
237
|
if (!server) {
|
|
230
|
-
ctx.ui.notify("ask-
|
|
238
|
+
ctx.ui.notify("ask-spec: no review is open — submit a spec first.", "warning");
|
|
231
239
|
return;
|
|
232
240
|
}
|
|
233
241
|
open(server.url);
|
|
234
|
-
ctx.ui.notify(`ask-
|
|
242
|
+
ctx.ui.notify(`ask-spec: ${server.url}`, "info");
|
|
235
243
|
return;
|
|
236
244
|
}
|
|
237
245
|
|
|
238
246
|
if (active) {
|
|
239
|
-
ctx.ui.notify("ask-
|
|
247
|
+
ctx.ui.notify("ask-spec: already reviewing a spec. Use `off` to leave the phase.", "warning");
|
|
240
248
|
return;
|
|
241
249
|
}
|
|
242
250
|
|
|
243
|
-
|
|
251
|
+
if (!ctx.isIdle()) {
|
|
252
|
+
ctx.ui.notify("ask-spec: the agent is still running — finish or abort first.", "warning");
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (pi.getActiveTools().includes(UAT_PHASE_TOOL)) {
|
|
256
|
+
ctx.ui.notify("ask-spec: ask-uat is open — /ask-uat off first.", "warning");
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const readiness = frameworkReadiness(resolveProjectRoot(cwd));
|
|
261
|
+
if (!readiness.ready) {
|
|
262
|
+
ctx.ui.notify(
|
|
263
|
+
`ask-spec: no complete agent framework (missing: ${readiness.missing.join(", ")}). The spec still works, but record_change and /ask-uat stay unavailable until /ask-init.`,
|
|
264
|
+
"info",
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const slug = specSlug(arg || "spec");
|
|
244
269
|
specPath = `specs/${slug}.md`;
|
|
245
270
|
session = undefined;
|
|
246
|
-
|
|
271
|
+
specSessionId = sessionIdOf(ctx);
|
|
247
272
|
const permission = getPermissionControl();
|
|
248
273
|
permissionBefore = permission?.snapshot();
|
|
249
274
|
permission?.enableDefaults(ctx);
|
|
250
275
|
permissionDuring = permission?.snapshot();
|
|
251
|
-
//
|
|
252
|
-
// restore its own tool snapshot before we add
|
|
276
|
+
// Spec mode is intentionally less restrictive than ask mode. Let ask
|
|
277
|
+
// restore its own tool snapshot before we add spec_submit.
|
|
253
278
|
if (getAskControl()?.isActive()) getAskControl()?.exit(ctx);
|
|
254
279
|
active = true;
|
|
255
280
|
pi.setActiveTools(toolSetWithSubmit());
|
|
256
281
|
ctx.ui.notify(
|
|
257
|
-
`ask-
|
|
282
|
+
`ask-spec: reviewing. Write the spec to ${specPath}, then call ${SPEC_SUBMIT_TOOL}.`,
|
|
258
283
|
"info",
|
|
259
284
|
);
|
|
260
|
-
pi.sendUserMessage(
|
|
285
|
+
pi.sendUserMessage(buildSpecKickoff(arg || undefined));
|
|
261
286
|
},
|
|
262
|
-
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// `/ask-plan` stays as an alias: the workflow predates the rename, and a
|
|
290
|
+
// half-remembered command must not drop the operator into a plain chat.
|
|
291
|
+
for (const command of [SPEC_COMMAND, SPEC_COMMAND_ALIAS]) {
|
|
292
|
+
pi.registerCommand(command, specCommand);
|
|
293
|
+
}
|
|
263
294
|
|
|
264
295
|
// ── Tool ───────────────────────────────────────────────────────────────
|
|
265
296
|
|
|
266
|
-
pi.registerTool<typeof
|
|
267
|
-
name:
|
|
268
|
-
label: "Submit
|
|
297
|
+
pi.registerTool<typeof SPEC_SUBMIT_PARAMS, { version?: number; url?: string }>({
|
|
298
|
+
name: SPEC_SUBMIT_TOOL,
|
|
299
|
+
label: "Submit spec for review",
|
|
269
300
|
description:
|
|
270
301
|
"Submit the spec file you just wrote so the operator can review and annotate it in the browser. Returns immediately; the decision arrives as a new message.",
|
|
271
|
-
promptSnippet: "Submit the
|
|
302
|
+
promptSnippet: "Submit the spec for browser review (spec mode only)",
|
|
272
303
|
promptGuidelines: [
|
|
273
|
-
"Call
|
|
304
|
+
"Call spec_submit only after the spec file on disk is the version you want reviewed.",
|
|
274
305
|
"After submitting, end your turn: the operator's annotations arrive as a new message.",
|
|
275
|
-
"Never submit a file you have not written —
|
|
306
|
+
"Never submit a file you have not written — spec_submit reads the file from disk.",
|
|
276
307
|
],
|
|
277
|
-
parameters:
|
|
308
|
+
parameters: SPEC_SUBMIT_PARAMS,
|
|
278
309
|
defaultActive: false,
|
|
279
310
|
async execute(_toolCallId: string, params: { path?: string }) {
|
|
280
311
|
if (!active) {
|
|
281
312
|
return {
|
|
282
|
-
content: [{ type: "text" as const, text: "
|
|
313
|
+
content: [{ type: "text" as const, text: "Spec mode is off — use /ask-spec first." }],
|
|
283
314
|
details: {},
|
|
284
315
|
};
|
|
285
316
|
}
|
|
286
|
-
const target = params.path ?? specPath ?? "specs/
|
|
287
|
-
const absolute =
|
|
317
|
+
const target = params.path ?? specPath ?? "specs/spec.md";
|
|
318
|
+
const absolute = specMarkdownPath(target, cwd);
|
|
288
319
|
if (!absolute) {
|
|
289
320
|
return {
|
|
290
321
|
content: [
|
|
@@ -304,7 +335,7 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
304
335
|
content: [
|
|
305
336
|
{
|
|
306
337
|
type: "text" as const,
|
|
307
|
-
text: `Cannot read ${target}. Write the spec file first, then call
|
|
338
|
+
text: `Cannot read ${target}. Write the spec file first, then call spec_submit again.`,
|
|
308
339
|
},
|
|
309
340
|
],
|
|
310
341
|
details: {},
|
|
@@ -312,18 +343,18 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
312
343
|
}
|
|
313
344
|
const source = relative(cwd, absolute) || target;
|
|
314
345
|
if (!session) {
|
|
315
|
-
session =
|
|
346
|
+
session = createSpecSession({
|
|
316
347
|
agentDir,
|
|
317
|
-
slug:
|
|
348
|
+
slug: specSlug(source.replace(/\.mdx?$/i, "").replace(/^specs\//, "")),
|
|
318
349
|
text,
|
|
319
350
|
source,
|
|
320
351
|
});
|
|
321
352
|
} else {
|
|
322
|
-
|
|
353
|
+
appendSpecVersion(session, { text, source });
|
|
323
354
|
}
|
|
324
355
|
const version = latestVersion(session);
|
|
325
356
|
if (!server) {
|
|
326
|
-
server = await
|
|
357
|
+
server = await startSpecServer({
|
|
327
358
|
session,
|
|
328
359
|
render: renderBlockHtml,
|
|
329
360
|
portStart: options.portStart,
|
|
@@ -334,11 +365,11 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
334
365
|
},
|
|
335
366
|
});
|
|
336
367
|
open(server.url);
|
|
337
|
-
ctxRef?.ui.notify(`ask-
|
|
368
|
+
ctxRef?.ui.notify(`ask-spec: review open at ${server.url}`, "info");
|
|
338
369
|
} else {
|
|
339
370
|
server.broadcast("revision", { version: version.n });
|
|
340
371
|
}
|
|
341
|
-
const changed =
|
|
372
|
+
const changed = buildSpecState(session, version.n).items.filter(
|
|
342
373
|
(item) => item.status !== "unchanged",
|
|
343
374
|
).length;
|
|
344
375
|
return {
|
|
@@ -355,56 +386,56 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
355
386
|
},
|
|
356
387
|
});
|
|
357
388
|
|
|
358
|
-
// ── Gate: the
|
|
389
|
+
// ── Gate: the spec-review phase writes markdown only ──────────────────────
|
|
359
390
|
|
|
360
391
|
gates.register({
|
|
361
|
-
name: "
|
|
392
|
+
name: "spec",
|
|
362
393
|
order: 5,
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
394
|
+
evaluate(event: ToolCallEvent) {
|
|
395
|
+
if (!active) return undefined;
|
|
396
|
+
if (event.toolName === "bash") {
|
|
397
|
+
// The promise is "spec mode does not implement", and a redirect is
|
|
398
|
+
// implementation just as much as `write` is. Temp paths outside the
|
|
399
|
+
// workspace stay allowed; opaque units are left to the permission gate,
|
|
400
|
+
// which already fails closed on them (no double prompt here).
|
|
401
|
+
const command = typeof event.input.command === "string" ? event.input.command : "";
|
|
402
|
+
if (command === "") return undefined;
|
|
403
|
+
for (const unit of parseCommandUnits(command, cwd)) {
|
|
404
|
+
if (unit.opaque) continue;
|
|
405
|
+
for (const target of unit.targets) {
|
|
406
|
+
if (target.effect !== "write") continue;
|
|
407
|
+
const absolute = canonicalize(target.path);
|
|
408
|
+
if (absolute === cwd || !isWithin(cwd, absolute)) continue;
|
|
409
|
+
if (/\.mdx?$/i.test(absolute)) continue;
|
|
410
|
+
return {
|
|
411
|
+
kind: "block" as const,
|
|
412
|
+
reason:
|
|
413
|
+
`Spec mode only writes markdown while the spec is under review (blocked bash write to ${target.path}). ` +
|
|
414
|
+
"Implement the change after the spec is approved; write the spec file instead.",
|
|
415
|
+
};
|
|
386
416
|
}
|
|
387
|
-
return undefined;
|
|
388
417
|
}
|
|
389
|
-
|
|
418
|
+
return undefined;
|
|
419
|
+
}
|
|
420
|
+
if (event.toolName !== "write" && event.toolName !== "edit") return undefined;
|
|
390
421
|
const path = typeof event.input.path === "string" ? event.input.path : "";
|
|
391
|
-
if (
|
|
422
|
+
if (specMarkdownPath(path, cwd) !== undefined) return undefined;
|
|
392
423
|
return {
|
|
393
424
|
kind: "block" as const,
|
|
394
425
|
reason:
|
|
395
|
-
"
|
|
396
|
-
"Implement the change after the
|
|
426
|
+
"Spec mode only writes markdown (.md/.mdx) while the spec is under review. " +
|
|
427
|
+
"Implement the change after the spec is approved, or write the spec file instead.",
|
|
397
428
|
};
|
|
398
429
|
},
|
|
399
430
|
});
|
|
400
431
|
|
|
401
|
-
// ── System prompt while
|
|
432
|
+
// ── System prompt while reviewing ──────────────────────────────────────
|
|
402
433
|
|
|
403
434
|
pi.on("before_agent_start", (event) => {
|
|
404
435
|
if (!active) return undefined;
|
|
405
436
|
pi.setActiveTools(toolSetWithSubmit());
|
|
406
437
|
return {
|
|
407
|
-
systemPrompt: `${event.systemPrompt}\n\n${
|
|
438
|
+
systemPrompt: `${event.systemPrompt}\n\n${buildSpecSystemPrompt({
|
|
408
439
|
questionTool: pi.getActiveTools().includes("ask_user_question"),
|
|
409
440
|
})}`,
|
|
410
441
|
};
|
|
@@ -416,7 +447,7 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
416
447
|
|
|
417
448
|
pi.on("session_start", (_event, ctx) => {
|
|
418
449
|
const nextSessionId = sessionIdOf(ctx);
|
|
419
|
-
if (nextSessionId ===
|
|
450
|
+
if (nextSessionId === specSessionId) return;
|
|
420
451
|
// A different session must not inherit this session's gates, review server
|
|
421
452
|
// or permission snapshot. Permission's own session_start has already reset
|
|
422
453
|
// its state, so do not restore the old snapshot here.
|
|
@@ -424,7 +455,7 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
424
455
|
session = undefined;
|
|
425
456
|
permissionBefore = undefined;
|
|
426
457
|
permissionDuring = undefined;
|
|
427
|
-
|
|
458
|
+
specSessionId = undefined;
|
|
428
459
|
specPath = undefined;
|
|
429
460
|
ctxRef = ctx;
|
|
430
461
|
closeServer();
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
buildSpecKickoff,
|
|
4
|
+
buildSpecSystemPrompt,
|
|
5
5
|
INTERVIEW_GUIDE,
|
|
6
|
-
|
|
6
|
+
SPEC_MODE_BANNER,
|
|
7
7
|
SPEC_FRAMEWORK_GUIDE,
|
|
8
8
|
} from "./prompt.js";
|
|
9
9
|
|
|
10
|
-
describe("
|
|
10
|
+
describe("spec review prompt", () => {
|
|
11
11
|
it("states what the phase allows and how a turn may end", () => {
|
|
12
|
-
expect(
|
|
13
|
-
expect(
|
|
14
|
-
expect(
|
|
15
|
-
expect(
|
|
16
|
-
expect(
|
|
12
|
+
expect(SPEC_MODE_BANNER).toContain("[ASK-SPEC — SPEC REVIEW]");
|
|
13
|
+
expect(SPEC_MODE_BANNER).toContain("writing/editing\nmarkdown files");
|
|
14
|
+
expect(SPEC_MODE_BANNER).toContain("Not allowed: editing other files");
|
|
15
|
+
expect(SPEC_MODE_BANNER).toContain("spec_submit");
|
|
16
|
+
expect(SPEC_MODE_BANNER).toContain("End every turn with either a question or a spec_submit call");
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
it("asks in rounds instead of guessing, and never names a tool the session lacks", () => {
|
|
20
20
|
expect(INTERVIEW_GUIDE).toContain("in rounds");
|
|
21
21
|
expect(INTERVIEW_GUIDE).toContain("Do not ask what you can read");
|
|
22
|
-
expect(
|
|
23
|
-
expect(
|
|
22
|
+
expect(buildSpecSystemPrompt({ questionTool: false })).toContain("ask in plain text");
|
|
23
|
+
expect(buildSpecSystemPrompt({ questionTool: true })).toContain("`ask_user_question` is available");
|
|
24
24
|
});
|
|
25
25
|
|
|
26
26
|
it("gives the spec framework in reading order", () => {
|
|
@@ -61,8 +61,8 @@ describe("plan mode prompt", () => {
|
|
|
61
61
|
});
|
|
62
62
|
|
|
63
63
|
it("combines banner and guide without dropping either", () => {
|
|
64
|
-
const prompt =
|
|
65
|
-
expect(prompt).toContain(
|
|
64
|
+
const prompt = buildSpecSystemPrompt();
|
|
65
|
+
expect(prompt).toContain(SPEC_MODE_BANNER);
|
|
66
66
|
expect(prompt).toContain(SPEC_FRAMEWORK_GUIDE);
|
|
67
67
|
});
|
|
68
68
|
|
|
@@ -70,22 +70,22 @@ describe("plan mode prompt", () => {
|
|
|
70
70
|
// Framework prompts are authored in English, which is our choice of
|
|
71
71
|
// language for the prompt — not an instruction about the spec. The spec
|
|
72
72
|
// follows the operator's own language, so any directive would be a bug.
|
|
73
|
-
const prompt =
|
|
73
|
+
const prompt = buildSpecSystemPrompt().toLowerCase();
|
|
74
74
|
for (const banned of ["in english", "in chinese", "language", "write in"]) {
|
|
75
75
|
expect(prompt, `prompt must not mention "${banned}"`).not.toContain(banned);
|
|
76
76
|
}
|
|
77
77
|
});
|
|
78
78
|
});
|
|
79
79
|
|
|
80
|
-
describe("
|
|
80
|
+
describe("buildSpecKickoff", () => {
|
|
81
81
|
it("names the task when one was given", () => {
|
|
82
|
-
const kickoff =
|
|
83
|
-
expect(kickoff).toContain("Task: build /ask-
|
|
84
|
-
expect(kickoff).toContain("
|
|
82
|
+
const kickoff = buildSpecKickoff("build /ask-spec");
|
|
83
|
+
expect(kickoff).toContain("Task: build /ask-spec");
|
|
84
|
+
expect(kickoff).toContain("spec_submit");
|
|
85
85
|
});
|
|
86
86
|
|
|
87
87
|
it("falls back to the session discussion", () => {
|
|
88
|
-
expect(
|
|
89
|
-
expect(
|
|
88
|
+
expect(buildSpecKickoff(undefined)).toContain("the work we have been discussing in this session");
|
|
89
|
+
expect(buildSpecKickoff(" ")).toContain("the work we have been discussing in this session");
|
|
90
90
|
});
|
|
91
91
|
});
|