pi-plans 0.1.1 → 0.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.
@@ -8,10 +8,14 @@ import { Text } from "@earendil-works/pi-tui";
8
8
  import { Type } from "typebox";
9
9
  import * as fs from "node:fs";
10
10
  import * as path from "node:path";
11
- import { startExecution } from "../src/exec.ts";
12
- import { latestPlanVersion, parseChecklist } from "../src/plan.ts";
11
+ import {
12
+ startExecution,
13
+ } from "../src/exec.ts";
14
+ import { disableAutoComplete } from "../src/autocomplete.ts";
15
+ import { latestPlanVersion, parseChecklist, parseImplItems } from "../src/plan.ts";
13
16
  import { normalizeWorkdir, readActive } from "../src/state.ts";
14
17
 
18
+
15
19
  const ExecutePlanParams = Type.Object({
16
20
  planPath: Type.Optional(
17
21
  Type.String({ description: "Path to the accepted PLAN_vN.md. Default: highest version in the active run's artifact directory." }),
@@ -55,14 +59,17 @@ export async function executeHandoff(
55
59
  return { status: "error", message: `Plan file not found: ${planPath}` };
56
60
  }
57
61
 
58
- const items = parseChecklist(fs.readFileSync(planPath, "utf8"));
62
+ const planText = fs.readFileSync(planPath, "utf8");
63
+ const items = parseChecklist(planText);
59
64
  if (items.length === 0) {
60
65
  return {
61
66
  status: "error",
62
67
  message: `${planPath} has no parsable \`## Verifier Checklist\` with \`- [ ] \`VC-###\` ...\` items. Fix the plan before execution.`,
63
68
  };
64
69
  }
70
+ const implItems = parseImplItems(planText);
65
71
 
72
+ disableAutoComplete(ctx, "execution handoff");
66
73
  if (!ctx.hasUI) {
67
74
  return {
68
75
  status: "error",
@@ -80,12 +87,13 @@ export async function executeHandoff(
80
87
  return { status: "declined", message: "User declined execution. Stay in planning; ask how to proceed.", planPath };
81
88
  }
82
89
 
83
- startExecution(getCurrentApi(), ctx, planPath, items);
90
+ await startExecution(getCurrentApi(), ctx, planPath, items, implItems);
91
+ const scopeNote = implItems.length ? ` Tracking ${implItems.length} implementation item(s).` : "";
84
92
  return {
85
93
  status: "executing",
86
94
  planPath,
87
95
  itemCount: items.length,
88
- message: `Execution approved. ${items.length} verifier item(s) queued; implement in dependency order and mark verified items with [DONE:VC-xxx].`,
96
+ message: `Execution approved. ${items.length} verifier item(s) queued; implement in dependency order and mark verified items with [DONE:VC-xxx].${scopeNote}`,
89
97
  };
90
98
  }
91
99
 
package/tools/plans.ts CHANGED
@@ -96,7 +96,20 @@ const PlansParams = Type.Object({
96
96
  ),
97
97
  });
98
98
 
99
+ // Module-level reference so the start-run action can append a session entry
100
+ // (pi-plans-run-start) at the moment the planning run begins. The ExtensionAPI
101
+ // itself is not in scope for `startRun`, mirroring `setCurrentApi` in
102
+ // tools/execute-plan.ts.
103
+ let runStartAppender: ((runId: string, artifactDir: string) => void) | null = null;
104
+
105
+ export function setRunStartAppender(appender: ((runId: string, artifactDir: string) => void) | null): void {
106
+ runStartAppender = appender;
107
+ }
108
+
99
109
  export function registerPlansTool(pi: ExtensionAPI): void {
110
+ setRunStartAppender((runId, artifactDir) => {
111
+ pi.appendEntry("pi-plans-run-start", { runId, artifactDir });
112
+ });
100
113
  pi.registerTool({
101
114
  name: "plans",
102
115
  label: "Plans",
@@ -157,6 +170,9 @@ export function registerPlansTool(pi: ExtensionAPI): void {
157
170
  topic: params.topic,
158
171
  skill: params.skill,
159
172
  requestText: params.requestText,
173
+ onStart: (run) => {
174
+ runStartAppender?.(run.run_id, run.artifact_dir);
175
+ },
160
176
  });
161
177
  break;
162
178
  }
package/tools/refine.ts CHANGED
@@ -17,6 +17,7 @@ import * as path from "node:path";
17
17
  import { loadConfig, normalizeWorkdir, readActive, recordSubagent, resolveStateRootOrNull, StateError, type RoleConfig } from "../src/state.ts";
18
18
  import { buildCriticizerTask, buildReviewerTask, reviewerLanes } from "../src/refine-prompts.ts";
19
19
  import { runPiSubagent, stripFrontmatter } from "../src/subagent.ts";
20
+ import { RefineOverlayController, refineOverlayContext } from "../src/refine-ui.ts";
20
21
 
21
22
  const RefineParams = Type.Object({
22
23
  role: StringEnum(["reviewer", "criticizer"] as const, { description: "Refinement role to run" }),
@@ -46,7 +47,29 @@ function roleGateError(role: string, roleConfig: RoleConfig | undefined, problem
46
47
  );
47
48
  }
48
49
 
50
+ function setupRefinementExecution(
51
+ ctx: ExtensionContext,
52
+ parentSignal: AbortSignal | undefined,
53
+ role: "reviewer" | "criticizer",
54
+ lanes: Array<{ id: string; label?: string }>,
55
+ ) {
56
+ const controller = new AbortController();
57
+ const relayAbort = () => controller.abort();
58
+ if (parentSignal?.aborted) controller.abort();
59
+ else parentSignal?.addEventListener("abort", relayAbort, { once: true });
49
60
 
61
+ const overlay = ctx.mode === "tui" ? new RefineOverlayController(role, lanes, relayAbort) : undefined;
62
+ overlay?.open(refineOverlayContext(ctx));
63
+
64
+ return {
65
+ signal: controller.signal,
66
+ overlay,
67
+ async close() {
68
+ await overlay?.close();
69
+ parentSignal?.removeEventListener("abort", relayAbort);
70
+ },
71
+ };
72
+ }
50
73
 
51
74
  export function registerRefineTool(pi: ExtensionAPI, baseDir: string): void {
52
75
  const agentsDir = path.join(baseDir, "agents");
@@ -119,28 +142,35 @@ export function registerRefineTool(pi: ExtensionAPI, baseDir: string): void {
119
142
 
120
143
  if (params.role === "criticizer") {
121
144
  const name = `${roleConfig.name_prefix}-criticizer-${Date.now().toString(36)}`;
122
- const result = await runPiSubagent({
123
- systemPrompt,
124
- task: buildCriticizerTask({ planText, planPath, focus: params.focus, context: params.context }),
125
- cwd: workdir,
126
- model,
127
- signal,
128
- });
129
- record(name, result.ok ? result.model ?? model : null);
130
- if (!result.ok) {
131
- throw new Error(
132
- `criticizer subagent failed: ${result.errorMessage ?? "unknown error"}${result.stderr ? `\nstderr: ${result.stderr.slice(0, 2000)}` : ""}`,
133
- );
145
+ const execution = setupRefinementExecution(ctx, signal, "criticizer", [{ id: name, label: "criticizer" }]);
146
+ try {
147
+ const result = await runPiSubagent({
148
+ systemPrompt,
149
+ task: buildCriticizerTask({ planText, planPath, focus: params.focus, context: params.context }),
150
+ cwd: workdir,
151
+ model,
152
+ signal: execution.signal,
153
+ onProgress: (event) => execution.overlay?.update(name, event),
154
+ });
155
+ execution.overlay?.complete(name, result);
156
+ record(name, result.ok ? result.model ?? model : null);
157
+ if (!result.ok) {
158
+ throw new Error(
159
+ `criticizer subagent failed: ${result.errorMessage ?? "unknown error"}${result.stderr ? `\nstderr: ${result.stderr.slice(0, 2000)}` : ""}`,
160
+ );
161
+ }
162
+ return {
163
+ content: [
164
+ {
165
+ type: "text",
166
+ text: `${result.output}\n\n---\nAsk each criticizer question with ask_choice (one call per question, in the configured language), record every answer, then revise the plan only after every question has an answer.`,
167
+ },
168
+ ],
169
+ details: { mode: "delegated-subagent", role: params.role, planPath, model: result.model ?? model },
170
+ };
171
+ } finally {
172
+ await execution.close();
134
173
  }
135
- return {
136
- content: [
137
- {
138
- type: "text",
139
- text: `${result.output}\n\n---\nAsk each criticizer question with ask_choice (one call per question, in the configured language), record every answer, then revise the plan only after every question has an answer.`,
140
- },
141
- ],
142
- details: { mode: "delegated-subagent", role: params.role, planPath, model: result.model ?? model },
143
- };
144
174
  }
145
175
 
146
176
  // Reviewer round: 1 by default, 3 for the big-plan concurrent round.
@@ -152,62 +182,86 @@ export function registerRefineTool(pi: ExtensionAPI, baseDir: string): void {
152
182
  return { lane, name, task };
153
183
  });
154
184
 
155
- const results = await Promise.all(
156
- jobs.map(async (job) => {
157
- try {
158
- const result = await runPiSubagent({ systemPrompt, task: job.task, cwd: workdir, model, signal });
159
- record(job.name, result.ok ? result.model ?? model : null);
160
- return { job, result };
161
- } catch (error) {
162
- record(job.name, null);
163
- const message = error instanceof Error ? error.message : String(error);
164
- return {
165
- job,
166
- result: { ok: false, output: "", model: model ?? undefined, errorMessage: message, stderr: "", turns: 0 },
167
- };
168
- }
169
- }),
185
+ const execution = setupRefinementExecution(
186
+ ctx,
187
+ signal,
188
+ "reviewer",
189
+ jobs.map((job) => ({ id: job.lane.id, label: job.lane.id })),
170
190
  );
191
+ try {
192
+ const results = await Promise.all(
193
+ jobs.map(async (job) => {
194
+ try {
195
+ const result = await runPiSubagent({
196
+ systemPrompt,
197
+ task: job.task,
198
+ cwd: workdir,
199
+ model,
200
+ signal: execution.signal,
201
+ onProgress: (event) => execution.overlay?.update(job.lane.id, event),
202
+ });
203
+ execution.overlay?.complete(job.lane.id, result);
204
+ record(job.name, result.ok ? result.model ?? model : null);
205
+ return { job, result };
206
+ } catch (error) {
207
+ record(job.name, null);
208
+ const message = error instanceof Error ? error.message : String(error);
209
+ const result = {
210
+ ok: false,
211
+ output: "",
212
+ model: model ?? undefined,
213
+ errorMessage: message,
214
+ stderr: "",
215
+ turns: 0,
216
+ };
217
+ execution.overlay?.complete(job.lane.id, result);
218
+ return { job, result };
219
+ }
220
+ }),
221
+ );
171
222
 
172
- const sections: string[] = [];
173
- let failures = 0;
174
- for (const { job, result } of results) {
175
- const title = job.lane.lens ? `${job.name} — ${job.lane.lens}` : job.name;
176
- if (!result.ok) {
177
- failures += 1;
178
- sections.push(`### ${title} — FAILED\n${result.errorMessage ?? "unknown error"}`);
179
- continue;
223
+ const sections: string[] = [];
224
+ let failures = 0;
225
+ for (const { job, result } of results) {
226
+ const title = job.lane.lens ? `${job.name} — ${job.lane.lens}` : job.name;
227
+ if (!result.ok) {
228
+ failures += 1;
229
+ sections.push(`### ${title} — FAILED\n${result.errorMessage ?? "unknown error"}`);
230
+ continue;
231
+ }
232
+ sections.push(`### ${title}\n${result.output}`);
233
+ }
234
+ if (failures === results.length) {
235
+ const first = results[0];
236
+ throw new Error(
237
+ `all reviewer subagents failed: ${first?.result.errorMessage ?? "unknown error"}${first?.result.stderr ? `\nstderr: ${first.result.stderr.slice(0, 2000)}` : ""}${model ? `\nIf the model selector "${model}" is unavailable, reset the confirmation (plans set-role --reset-confirmation) and re-ask the model-confirmation question.` : ""}`,
238
+ );
180
239
  }
181
- sections.push(`### ${title}\n${result.output}`);
182
- }
183
- if (failures === results.length) {
184
- const first = results[0];
185
- throw new Error(
186
- `all reviewer subagents failed: ${first?.result.errorMessage ?? "unknown error"}${first?.result.stderr ? `\nstderr: ${first.result.stderr.slice(0, 2000)}` : ""}${model ? `\nIf the model selector "${model}" is unavailable, reset the confirmation (plans set-role --reset-confirmation) and re-ask the model-confirmation question.` : ""}`,
187
- );
188
- }
189
240
 
190
- const combined = sections.join("\n\n---\n\n");
191
- const truncation = truncateHead(combined, { maxLines: 2000, maxBytes: 50 * 1024 });
192
- let text = truncation.content;
193
- if (truncation.truncated) text += `\n\n[Output truncated; full outputs remain in this tool result's details.]`;
241
+ const combined = sections.join("\n\n---\n\n");
242
+ const truncation = truncateHead(combined, { maxLines: 2000, maxBytes: 50 * 1024 });
243
+ let text = truncation.content;
244
+ if (truncation.truncated) text += `\n\n[Output truncated; full outputs remain in this tool result's details.]`;
194
245
 
195
- return {
196
- content: [
197
- {
198
- type: "text",
199
- text: `${text}\n\n---\nConsolidate: merge and dedupe findings into PLAN_vN_reviewer_comments.md${count === 3 ? " (one consolidated file; keep each finding's source reviewer, severity, evidence, and disposition)" : ""}, accept or reject each finding on repo/reference evidence, surface at most five high-priority findings to the user, then immediately ask the next refinement-mode question with ask_choice.`,
246
+ return {
247
+ content: [
248
+ {
249
+ type: "text",
250
+ text: `${text}\n\n---\nConsolidate: merge and dedupe findings into PLAN_vN_reviewer_comments.md${count === 3 ? " (one consolidated file; keep each finding's source reviewer, severity, evidence, and disposition)" : ""}, accept or reject each finding on repo/reference evidence, surface at most five high-priority findings to the user, then immediately ask the next refinement-mode question with ask_choice.`,
251
+ },
252
+ ],
253
+ details: {
254
+ mode: "delegated-subagent",
255
+ role: "reviewer",
256
+ planPath,
257
+ reviewers: count,
258
+ model,
259
+ outputs: results.map(({ job, result }) => ({ name: job.name, lane: job.lane.id, lens: job.lane.lens, ok: result.ok, output: result.output, stderr: result.stderr, turns: result.turns })),
200
260
  },
201
- ],
202
- details: {
203
- mode: "delegated-subagent",
204
- role: "reviewer",
205
- planPath,
206
- reviewers: count,
207
- model,
208
- outputs: results.map(({ job, result }) => ({ name: job.name, lane: job.lane.id, lens: job.lane.lens, ok: result.ok, output: result.output, stderr: result.stderr, turns: result.turns })),
209
- },
210
- };
261
+ };
262
+ } finally {
263
+ await execution.close();
264
+ }
211
265
  },
212
266
 
213
267
  renderCall(args, theme) {