@deathbeam/pi-plan 0.0.2 → 0.0.3
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/README.md +2 -3
- package/index.ts +143 -92
- package/package.json +1 -1
- package/skills/import-plan/SKILL.md +14 -12
- package/skills/prepare-plan/SKILL.md +29 -10
- package/src/inspector.ts +25 -24
- package/src/store.ts +97 -57
package/README.md
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
# [@deathbeam/pi-plan](https://www.npmjs.com/package/@deathbeam/pi-plan)
|
|
2
2
|
|
|
3
|
-
Structured plans: a `plan` tool, a progress widget, and a `/plan`
|
|
4
|
-
Each task carries its own `doneWhen` check, and `plan get <id>` returns one task with the plan's goal and context.
|
|
3
|
+
Structured plans: a `plan` tool, a progress widget, and a `/plan` inspector over every plan in the project.
|
|
5
4
|
The bundled `prepare-plan` skill writes a plan, and `import-plan` migrates existing plan documents (`.plans/*.md`, Cursor, Kiro, Taskmaster).
|
|
6
5
|
|
|
7
|
-
Plans live in `.pi/plans/<
|
|
6
|
+
Plans live in `.pi/plans/<id>.json` at the repository root. Add `.pi/plans/` to `.gitignore` if the first `plan create` cannot write it to `.git/info/exclude`.
|
|
8
7
|
|
|
9
8
|
## Installation
|
|
10
9
|
|
package/index.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import {
|
|
3
|
+
getMarkdownTheme,
|
|
4
|
+
type ExtensionAPI,
|
|
5
|
+
type ExtensionContext,
|
|
6
|
+
type KeybindingHints,
|
|
7
|
+
type SessionEntry,
|
|
8
|
+
type Theme,
|
|
8
9
|
} from "@earendil-works/pi-coding-agent";
|
|
9
|
-
import { type Component, Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
10
|
+
import { Markdown, type Component, Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
10
11
|
import { Type } from "typebox";
|
|
11
|
-
import { openPlans } from "./src/inspector.ts";
|
|
12
|
+
import { hardBreaks, openPlans } from "./src/inspector.ts";
|
|
12
13
|
import {
|
|
13
14
|
doneCount,
|
|
14
15
|
excludePlans,
|
|
@@ -17,7 +18,7 @@ import {
|
|
|
17
18
|
listPlans,
|
|
18
19
|
listText,
|
|
19
20
|
moreLines,
|
|
20
|
-
|
|
21
|
+
planStatus,
|
|
21
22
|
planSummary,
|
|
22
23
|
planText,
|
|
23
24
|
plansDir,
|
|
@@ -30,15 +31,13 @@ import {
|
|
|
30
31
|
taskLine,
|
|
31
32
|
taskRow,
|
|
32
33
|
taskText,
|
|
33
|
-
|
|
34
|
+
widgetLines,
|
|
34
35
|
writePlan,
|
|
35
36
|
type Plan,
|
|
36
37
|
type PlanList,
|
|
37
38
|
type PlanStatus,
|
|
38
39
|
} from "./src/store.ts";
|
|
39
40
|
|
|
40
|
-
const ACTIONS = ["create", "update", "get", "list"] as const;
|
|
41
|
-
|
|
42
41
|
type Action = (typeof ACTIONS)[number];
|
|
43
42
|
|
|
44
43
|
type PlanDetails = {
|
|
@@ -54,26 +53,42 @@ type PlanDetails = {
|
|
|
54
53
|
list?: PlanList;
|
|
55
54
|
};
|
|
56
55
|
|
|
56
|
+
const ACTIONS = ["create", "update", "get", "list"] as const;
|
|
57
|
+
const DETAIL_HEADING = /^#{1,3}\s/m;
|
|
58
|
+
|
|
57
59
|
const PlanParams = Type.Object({
|
|
58
60
|
action: StringEnum(ACTIONS),
|
|
59
|
-
|
|
60
|
-
Type.String({ description: "Plan
|
|
61
|
+
plan: Type.Optional(
|
|
62
|
+
Type.String({ description: "Plan id in kebab-case; get and update default to the active plan." }),
|
|
61
63
|
),
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
title: Type.Optional(
|
|
65
|
+
Type.String({
|
|
66
|
+
description: "create: a three to eight word summary of the outcome, shown in the widget and list.",
|
|
67
|
+
}),
|
|
68
|
+
),
|
|
69
|
+
goal: Type.Optional(Type.String({ description: "create: the observable outcome and the check that proves it." })),
|
|
70
|
+
detail: Type.Optional(
|
|
71
|
+
Type.String({
|
|
72
|
+
description:
|
|
73
|
+
"create: markdown sections: current behaviour with path:line, problem, chosen design and rejected alternatives, accepted behaviour changes, out of scope, open questions. Headings inside use #### or lower.",
|
|
74
|
+
}),
|
|
65
75
|
),
|
|
66
76
|
tasks: Type.Optional(
|
|
67
77
|
Type.Array(
|
|
68
78
|
Type.Object({
|
|
69
|
-
title: Type.String({
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
),
|
|
74
|
-
doneWhen: Type.String({
|
|
79
|
+
title: Type.String({
|
|
80
|
+
description: "The deliverable or behaviour in one line; the widget and get show it beside the id.",
|
|
81
|
+
}),
|
|
82
|
+
goal: Type.String({
|
|
75
83
|
description: "Observable result, with the command to run and its expected output.",
|
|
76
84
|
}),
|
|
85
|
+
detail: Type.String({
|
|
86
|
+
description:
|
|
87
|
+
"Approach, interfaces to match, pitfalls, and a Where: line citing path:line. Headings inside use #### or lower.",
|
|
88
|
+
}),
|
|
89
|
+
dependsOn: Type.Optional(
|
|
90
|
+
Type.Array(Type.String(), { description: "Task ids this task waits on, such as t1." }),
|
|
91
|
+
),
|
|
77
92
|
status: Type.Optional(StringEnum(STATUSES, { description: "Defaults to todo." })),
|
|
78
93
|
notes: Type.Optional(
|
|
79
94
|
Type.Array(Type.String(), { description: "Existing notes or log lines for this task." }),
|
|
@@ -82,13 +97,18 @@ const PlanParams = Type.Object({
|
|
|
82
97
|
{ description: "create: tasks in execution order; ids t1..tn follow this order." },
|
|
83
98
|
),
|
|
84
99
|
),
|
|
85
|
-
|
|
100
|
+
task: Type.Optional(
|
|
86
101
|
Type.String({
|
|
87
|
-
description: "update: task id to change. get: this task alone, with the plan's goal and
|
|
102
|
+
description: "update: task id to change. get: this task alone, with the plan's goal and detail.",
|
|
88
103
|
}),
|
|
89
104
|
),
|
|
90
105
|
status: Type.Optional(StringEnum(STATUSES, { description: "update: the task's new state." })),
|
|
91
|
-
note: Type.Optional(
|
|
106
|
+
note: Type.Optional(
|
|
107
|
+
Type.String({
|
|
108
|
+
description:
|
|
109
|
+
"update: a note appended to the task's notes; without task, to the plan's notes (use for cross-task rulings).",
|
|
110
|
+
}),
|
|
111
|
+
),
|
|
92
112
|
});
|
|
93
113
|
|
|
94
114
|
const WIDGET_KEY = "plan";
|
|
@@ -154,7 +174,7 @@ function resultText(result: { content: { type: string; text?: string }[] }): str
|
|
|
154
174
|
return result.content.flatMap((part) => (part.type === "text" && part.text ? [part.text] : [])).join("\n");
|
|
155
175
|
}
|
|
156
176
|
|
|
157
|
-
/** The whole plan: a summary line, then one row per task; only the expansion carries goal,
|
|
177
|
+
/** The whole plan: a summary line, then one row per task; only the expansion carries goal, detail and notes. */
|
|
158
178
|
function planLines(plan: Plan, theme: Theme, expanded: boolean, width: number): string[] {
|
|
159
179
|
const tasks = plan.tasks.flatMap((task) =>
|
|
160
180
|
expanded
|
|
@@ -165,20 +185,12 @@ function planLines(plan: Plan, theme: Theme, expanded: boolean, width: number):
|
|
|
165
185
|
"",
|
|
166
186
|
`${theme.fg("accent", "plan")} ${planSummary(theme, plan)}`,
|
|
167
187
|
...(expanded
|
|
168
|
-
? [...fact(theme, "Goal: ", plan.goal, width), ...fact(theme, "
|
|
188
|
+
? [...fact(theme, "Goal: ", plan.goal, width), ...fact(theme, "Detail: ", plan.detail, width, 2, "dim")]
|
|
169
189
|
: []),
|
|
170
190
|
...tasks,
|
|
171
191
|
];
|
|
172
192
|
}
|
|
173
193
|
|
|
174
|
-
/** One task of a plan: its row, then the check and notes that make it a brief. */
|
|
175
|
-
function focusLines(plan: Plan, id: string, theme: Theme, width: number): string[] {
|
|
176
|
-
const task = plan.tasks.find((item) => item.id === id);
|
|
177
|
-
return task
|
|
178
|
-
? ["", ...hanging(taskRow(theme, plan, task), width, task.id.length + 3), ...taskFacts(theme, task, width)]
|
|
179
|
-
: [];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
194
|
/** What changed: the task row, the note this call appended, and where the plan now stands. */
|
|
183
195
|
function updateLines(
|
|
184
196
|
plan: Plan,
|
|
@@ -188,10 +200,9 @@ function updateLines(
|
|
|
188
200
|
width: number,
|
|
189
201
|
): string[] {
|
|
190
202
|
const task = plan.tasks.find((item) => item.id === id);
|
|
191
|
-
if (!task) return [];
|
|
192
203
|
return [
|
|
193
204
|
"",
|
|
194
|
-
...hanging(taskRow(theme, plan, task), width, task.id.length + 3),
|
|
205
|
+
...(task ? hanging(taskRow(theme, plan, task), width, task.id.length + 3) : []),
|
|
195
206
|
...(note ? fact(theme, "", note, width) : []),
|
|
196
207
|
theme.fg("muted", planSummary(theme, plan)),
|
|
197
208
|
];
|
|
@@ -202,7 +213,7 @@ function listLines(list: PlanList, active: string | undefined, theme: Theme): st
|
|
|
202
213
|
return [
|
|
203
214
|
"",
|
|
204
215
|
...list.plans.map(
|
|
205
|
-
(plan) => `${plan.
|
|
216
|
+
(plan) => `${plan.id === active ? theme.fg("accent", "›") : " "} ${planSummary(theme, plan)}`,
|
|
206
217
|
),
|
|
207
218
|
...list.unreadable.map((name) => theme.fg("error", `? ${name} unreadable`)),
|
|
208
219
|
];
|
|
@@ -213,8 +224,8 @@ function callLine(args: Record<string, unknown>, theme: Theme): Component {
|
|
|
213
224
|
const action = argText(args.action);
|
|
214
225
|
const update = action === "update";
|
|
215
226
|
const target = update
|
|
216
|
-
? argText(args.
|
|
217
|
-
: [argText(args.
|
|
227
|
+
? argText(args.task)
|
|
228
|
+
: [argText(args.plan), action === "get" ? argText(args.task) : ""].filter(Boolean).join(" ");
|
|
218
229
|
const status = update ? argStatus(args.status) : undefined;
|
|
219
230
|
const note = update ? argText(args.note) : "";
|
|
220
231
|
const head = `${theme.fg("toolTitle", theme.bold("plan"))} ${theme.fg("accent", [action || "…", target].filter(Boolean).join(" "))}`;
|
|
@@ -227,6 +238,18 @@ function callLine(args: Record<string, unknown>, theme: Theme): Component {
|
|
|
227
238
|
return new Text([head, tail].filter(Boolean).join(" "), 0, 0);
|
|
228
239
|
}
|
|
229
240
|
|
|
241
|
+
/** Strict models send "null" for an omitted string or list; anything else that is empty is a bug. */
|
|
242
|
+
function blank(value: string | undefined): boolean {
|
|
243
|
+
return !value?.trim() || value === "null";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function taskList(value: unknown, field: string): string[] {
|
|
247
|
+
if (value === undefined || value === null || value === "null") return [];
|
|
248
|
+
if (!Array.isArray(value)) throw new Error(`${field} must be an array.`);
|
|
249
|
+
// pi coerces a bare "null" for an array param into ["null"] before execute runs.
|
|
250
|
+
return value.length === 1 && value[0] === "null" ? [] : (value as string[]);
|
|
251
|
+
}
|
|
252
|
+
|
|
230
253
|
export default function (pi: ExtensionAPI) {
|
|
231
254
|
// The last plan this session created, updated or read; restored from the active branch.
|
|
232
255
|
let active: string | undefined;
|
|
@@ -237,7 +260,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
237
260
|
try {
|
|
238
261
|
plan = active ? readPlan(repoRoot(ctx.cwd), active) : undefined;
|
|
239
262
|
} catch {
|
|
240
|
-
// A restored
|
|
263
|
+
// A restored id or a corrupt file must not break the session; the tool reports the real error.
|
|
241
264
|
plan = undefined;
|
|
242
265
|
}
|
|
243
266
|
if (!plan) {
|
|
@@ -245,7 +268,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
245
268
|
ctx.ui.setWidget(WIDGET_KEY, undefined);
|
|
246
269
|
return;
|
|
247
270
|
}
|
|
248
|
-
|
|
271
|
+
// A finished plan clears the widget, like pi-delegate's finished jobs; new work brings it back.
|
|
272
|
+
ctx.ui.setWidget(WIDGET_KEY, planStatus(plan) === "done" ? undefined : widgetLines(ctx.ui.theme, plan));
|
|
249
273
|
}
|
|
250
274
|
|
|
251
275
|
function restore(ctx: ExtensionContext) {
|
|
@@ -260,22 +284,22 @@ export default function (pi: ExtensionAPI) {
|
|
|
260
284
|
name: "plan",
|
|
261
285
|
label: "Plan",
|
|
262
286
|
description:
|
|
263
|
-
"Read and write the project plan: a
|
|
264
|
-
promptSnippet: "
|
|
287
|
+
"Read and write the project plan: a plan at .pi/plans/<id>.json with a goal, the detail a cold reader needs, and tasks. Actions: create, update, get, list.",
|
|
288
|
+
promptSnippet: "Write the plan for multi-step work in this project: goal, detail, one task per verifiable step",
|
|
265
289
|
promptGuidelines: [
|
|
266
290
|
"Mark a task `doing` when work on it starts.",
|
|
267
291
|
"When another agent works a task, give it the `get` output for that task and update the plan here from its result; a second writer would overwrite plan state.",
|
|
268
|
-
"Mark a task `done` only after its
|
|
292
|
+
"Mark a task `done` only after its goal's check passed, and put the result in the note, for example `npm test -- packages/x: 42 passed`.",
|
|
269
293
|
"Leave a task open when its outcome is unproven: `blocked` with a note of what remains, or `todo`.",
|
|
270
|
-
"Record a
|
|
271
|
-
'When the user refers to a plan by a loose name ("continue the npc draft"), run `list`, then `get` the matching
|
|
294
|
+
"Record a cross-task ruling as a note on the plan: `update` with a note and no task.",
|
|
295
|
+
'When the user refers to a plan by a loose name ("continue the npc draft"), run `list`, then `get` the matching id; that makes it active.',
|
|
272
296
|
],
|
|
273
297
|
parameters: PlanParams,
|
|
274
298
|
|
|
275
299
|
async execute(_toolCallId, rawParams, _signal, _onUpdate, ctx) {
|
|
276
300
|
// Strict models inject literal "null" for optional params they omit; drop it from those only.
|
|
277
301
|
const params = { ...rawParams } as typeof rawParams & Record<string, unknown>;
|
|
278
|
-
for (const key of ["
|
|
302
|
+
for (const key of ["plan", "title", "goal", "detail", "tasks", "task", "status", "note"]) {
|
|
279
303
|
if (params[key] === null || params[key] === "null") delete params[key];
|
|
280
304
|
}
|
|
281
305
|
const root = repoRoot(ctx.cwd);
|
|
@@ -283,66 +307,84 @@ export default function (pi: ExtensionAPI) {
|
|
|
283
307
|
refreshWidget(ctx);
|
|
284
308
|
return { content: [{ type: "text" as const, text }], details: { action, active, ...extra } };
|
|
285
309
|
};
|
|
286
|
-
const
|
|
287
|
-
const
|
|
288
|
-
if (!
|
|
289
|
-
return
|
|
310
|
+
const requireId = () => {
|
|
311
|
+
const id = params.plan ?? active;
|
|
312
|
+
if (!id) throw new Error("No active plan; run list, then pass plan.");
|
|
313
|
+
return id;
|
|
290
314
|
};
|
|
291
315
|
const requirePlan = () => {
|
|
292
|
-
const
|
|
293
|
-
const plan = readPlan(root,
|
|
294
|
-
if (!plan) throw new Error(`No plan ${
|
|
316
|
+
const id = requireId();
|
|
317
|
+
const plan = readPlan(root, id);
|
|
318
|
+
if (!plan) throw new Error(`No plan ${id} in ${plansDir(root)}; run list for the ids.`);
|
|
295
319
|
return plan;
|
|
296
320
|
};
|
|
297
321
|
|
|
298
322
|
switch (params.action as Action) {
|
|
299
323
|
case "create": {
|
|
300
|
-
const {
|
|
301
|
-
if (!
|
|
302
|
-
throw new Error("create needs
|
|
303
|
-
if (
|
|
324
|
+
const { plan: id, title, goal, detail, tasks } = params;
|
|
325
|
+
if (!id || !title || !goal || !detail || !tasks || blank(goal) || blank(detail))
|
|
326
|
+
throw new Error("create needs plan, title, goal, detail and tasks.");
|
|
327
|
+
if (tasks.some((task) => blank(task.goal)))
|
|
328
|
+
throw new Error("Every task needs a goal and the check that proves it.");
|
|
329
|
+
if (tasks.some((task) => blank(task.detail)))
|
|
330
|
+
throw new Error(
|
|
331
|
+
"Every task needs a detail: the approach, the interfaces and a Where: path:line.",
|
|
332
|
+
);
|
|
333
|
+
if (DETAIL_HEADING.test(detail) || tasks.some((task) => DETAIL_HEADING.test(task.detail)))
|
|
334
|
+
throw new Error(
|
|
335
|
+
"Headings inside detail use #### or lower; # to ### belong to the plan layout.",
|
|
336
|
+
);
|
|
337
|
+
if (readPlan(root, id)) throw new Error(`Plan ${id} already exists; use get or update.`);
|
|
304
338
|
const plan: Plan = {
|
|
305
|
-
|
|
339
|
+
id,
|
|
340
|
+
title,
|
|
306
341
|
goal,
|
|
307
|
-
|
|
342
|
+
detail,
|
|
343
|
+
notes: [],
|
|
308
344
|
tasks: tasks.map((task, index) => ({
|
|
309
345
|
id: `t${index + 1}`,
|
|
310
346
|
title: task.title,
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
347
|
+
goal: task.goal,
|
|
348
|
+
detail: task.detail,
|
|
349
|
+
dependsOn: taskList(task.dependsOn, `t${index + 1} dependsOn`),
|
|
314
350
|
status: (task.status ?? "todo") as PlanStatus,
|
|
315
|
-
notes: task.notes
|
|
351
|
+
notes: taskList(task.notes, `t${index + 1} notes`),
|
|
316
352
|
})),
|
|
317
353
|
};
|
|
318
354
|
const ids = new Map(plan.tasks.map((task, index) => [task.id, index]));
|
|
319
355
|
for (const [index, task] of plan.tasks.entries())
|
|
320
|
-
for (const id of task.
|
|
356
|
+
for (const id of task.dependsOn) {
|
|
321
357
|
const target = ids.get(id);
|
|
322
358
|
if (target === undefined)
|
|
323
|
-
throw new Error(`${task.id}
|
|
359
|
+
throw new Error(`${task.id} dependsOn ${id}, a task this plan lacks.`);
|
|
324
360
|
if (target >= index)
|
|
325
|
-
throw new Error(`${task.id}
|
|
361
|
+
throw new Error(`${task.id} dependsOn ${id}; dependsOn takes only earlier ids.`);
|
|
326
362
|
}
|
|
327
363
|
excludePlans(root);
|
|
328
364
|
writePlan(root, plan);
|
|
329
|
-
active = plan.
|
|
365
|
+
active = plan.id;
|
|
330
366
|
return respond(planText(plan), "create", { plan });
|
|
331
367
|
}
|
|
332
368
|
|
|
333
369
|
case "update": {
|
|
334
370
|
const plan = requirePlan();
|
|
335
|
-
if (!params.
|
|
371
|
+
if (!params.task) {
|
|
372
|
+
if (!params.note) throw new Error("update needs task, or a note for the plan.");
|
|
373
|
+
plan.notes.push(params.note);
|
|
374
|
+
writePlan(root, plan);
|
|
375
|
+
active = plan.id;
|
|
376
|
+
const progress = `${plan.id} ${doneCount(plan)}/${plan.tasks.length}`;
|
|
377
|
+
return respond(`note: ${params.note}\n${progress}`, "update", { plan, note: params.note });
|
|
378
|
+
}
|
|
336
379
|
if (!params.status && !params.note) throw new Error("update needs status or note.");
|
|
337
|
-
const task = plan.tasks.find((item) => item.id === params.
|
|
338
|
-
if (!task) throw new Error(`Plan ${plan.
|
|
380
|
+
const task = plan.tasks.find((item) => item.id === params.task);
|
|
381
|
+
if (!task) throw new Error(`Plan ${plan.id} has no task ${params.task}.`);
|
|
339
382
|
if (params.status) task.status = params.status as PlanStatus;
|
|
340
383
|
if (params.note) task.notes.push(params.note);
|
|
341
384
|
writePlan(root, plan);
|
|
342
|
-
active = plan.
|
|
385
|
+
active = plan.id;
|
|
343
386
|
// The model already holds the plan; echo only what changed and where it now stands.
|
|
344
|
-
const
|
|
345
|
-
const progress = `${plan.name} ${doneCount(plan)}/${plan.tasks.length}${next ? ` · next ${next.id}` : ""}`;
|
|
387
|
+
const progress = `${plan.id} ${doneCount(plan)}/${plan.tasks.length}`;
|
|
346
388
|
return respond(`${taskLine(task)}\n${progress}`, "update", {
|
|
347
389
|
plan,
|
|
348
390
|
id: task.id,
|
|
@@ -352,9 +394,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
352
394
|
|
|
353
395
|
case "get": {
|
|
354
396
|
const plan = requirePlan();
|
|
355
|
-
const task = params.
|
|
356
|
-
if (params.
|
|
357
|
-
active = plan.
|
|
397
|
+
const task = params.task ? plan.tasks.find((item) => item.id === params.task) : undefined;
|
|
398
|
+
if (params.task && !task) throw new Error(`Plan ${plan.id} has no task ${params.task}.`);
|
|
399
|
+
active = plan.id;
|
|
358
400
|
return respond(task ? taskText(plan, task) : planText(plan), "get", {
|
|
359
401
|
plan,
|
|
360
402
|
...(task ? { id: task.id } : {}),
|
|
@@ -380,8 +422,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
380
422
|
const note = details?.note;
|
|
381
423
|
let rows: ((width: number) => string[]) | undefined;
|
|
382
424
|
if (list) rows = () => listLines(list, active, theme);
|
|
383
|
-
else if (plan && details?.action === "
|
|
384
|
-
|
|
425
|
+
else if (plan && details?.action === "get") {
|
|
426
|
+
const task = plan.tasks.find((item) => item.id === id);
|
|
427
|
+
const markdown = new Markdown(
|
|
428
|
+
hardBreaks(task ? taskText(plan, task) : planText(plan)),
|
|
429
|
+
0,
|
|
430
|
+
0,
|
|
431
|
+
getMarkdownTheme(),
|
|
432
|
+
);
|
|
433
|
+
rows = (width) => ["", ...markdown.render(Math.max(1, width)).map((line) => line.trimEnd())];
|
|
434
|
+
} else if (plan && details?.action === "update")
|
|
435
|
+
rows = (width) => updateLines(plan, id, note, theme, width);
|
|
385
436
|
else if (plan) rows = (width) => planLines(plan, theme, expanded, width);
|
|
386
437
|
return rows
|
|
387
438
|
? resultView(rows, theme, expanded, context.keyHint)
|
|
@@ -390,34 +441,34 @@ export default function (pi: ExtensionAPI) {
|
|
|
390
441
|
});
|
|
391
442
|
|
|
392
443
|
pi.registerCommand("plan", {
|
|
393
|
-
description: "Show every plan and pick the active one; /plan <
|
|
444
|
+
description: "Show every plan and pick the active one; /plan <id> sets it directly",
|
|
394
445
|
handler: async (args, ctx) => {
|
|
395
446
|
const root = repoRoot(ctx.cwd);
|
|
396
|
-
const setActive = (
|
|
397
|
-
active =
|
|
398
|
-
pi.appendEntry(ACTIVE_ENTRY,
|
|
447
|
+
const setActive = (id: string) => {
|
|
448
|
+
active = id;
|
|
449
|
+
pi.appendEntry(ACTIVE_ENTRY, id);
|
|
399
450
|
refreshWidget(ctx);
|
|
400
451
|
};
|
|
401
|
-
const
|
|
402
|
-
if (
|
|
452
|
+
const id = args.trim();
|
|
453
|
+
if (id) {
|
|
403
454
|
let plan: Plan | undefined;
|
|
404
455
|
try {
|
|
405
|
-
plan = readPlan(root,
|
|
456
|
+
plan = readPlan(root, id);
|
|
406
457
|
} catch (error) {
|
|
407
458
|
ctx.ui.notify((error as Error).message, "error");
|
|
408
459
|
return;
|
|
409
460
|
}
|
|
410
461
|
if (!plan) {
|
|
411
|
-
ctx.ui.notify(`No plan ${
|
|
462
|
+
ctx.ui.notify(`No plan ${id} in ${plansDir(root)}.`, "error");
|
|
412
463
|
return;
|
|
413
464
|
}
|
|
414
|
-
setActive(plan.
|
|
415
|
-
ctx.ui.notify(`Active plan: ${plan.
|
|
465
|
+
setActive(plan.id);
|
|
466
|
+
ctx.ui.notify(`Active plan: ${plan.id}`, "info");
|
|
416
467
|
return;
|
|
417
468
|
}
|
|
418
469
|
const list = listPlans(root);
|
|
419
470
|
if (ctx.mode !== "tui") {
|
|
420
|
-
const plan = list.plans.find((item) => item.
|
|
471
|
+
const plan = list.plans.find((item) => item.id === active);
|
|
421
472
|
ctx.ui.notify(plan ? planText(plan) : listText(root, list, active), "info");
|
|
422
473
|
return;
|
|
423
474
|
}
|
package/package.json
CHANGED
|
@@ -21,29 +21,31 @@ Migrate every plan you find in the project. When the request names specific file
|
|
|
21
21
|
|
|
22
22
|
2. **Read a source whole.** Read every task list, checkbox, status field, note and log line before mapping. The mapping below covers the common formats; a format you have not seen gets mapped by the same rules.
|
|
23
23
|
|
|
24
|
-
3. **Skip a taken
|
|
24
|
+
3. **Skip a taken id.** If `.pi/plans/<id>.json` already exists, report the skip and convert the next source.
|
|
25
25
|
|
|
26
26
|
4. **Create one plan per source.** Use the mapping rules, then call `plan create`.
|
|
27
27
|
|
|
28
|
-
5. **Report.** End with one table: source file, new plan
|
|
28
|
+
5. **Report.** End with one table: source file, new plan id, tasks by status, and what you skipped or guessed. Tell the user they can remove the source files once the plan reads correctly. Never edit or delete a source yourself.
|
|
29
29
|
|
|
30
30
|
## Mapping rules
|
|
31
31
|
|
|
32
32
|
Map each field by what it says; its position in the file does not decide.
|
|
33
33
|
|
|
34
|
-
- **
|
|
35
|
-
- **
|
|
36
|
-
- **
|
|
34
|
+
- **Plan id.** The file name without its extension, in kebab-case; the plan title when the file name says nothing.
|
|
35
|
+
- **title.** The plan's title or goal in three to eight words.
|
|
36
|
+
- **goal.** The plan's goal, summary or objective, as one observable outcome plus the check that proves it.
|
|
37
|
+
- **detail.** The plan's context, decisions, constraints, design notes and open questions, in sections. Condense it, but keep every decision and every path.
|
|
38
|
+
- **notes.** A top-level Notes, log or rulings section becomes plan notes. Create the plan first, then append each note with `update` and a `note` but no `task`.
|
|
37
39
|
- **Tasks.** Each top-level actionable item becomes one task.
|
|
38
40
|
- `title`: the item's text.
|
|
39
|
-
- `
|
|
40
|
-
- `
|
|
41
|
-
- `
|
|
41
|
+
- `goal`: its verification, acceptance or test step, with the command when the source names it.
|
|
42
|
+
- `detail`: the item's body, sub-bullets and interfaces, with a `Where:` line citing `path:line` for each path it mentions.
|
|
43
|
+
- `dependsOn`: only the dependencies the source states, mapped to the new ids. List order alone is no dependency.
|
|
42
44
|
- **Status.** A checked or completed item is `done`. An in-progress item, or the next open one the source marks as current, is `doing`. Everything else is `todo`. Take every status from the source.
|
|
43
|
-
- **
|
|
45
|
+
- **Task notes.** Execution notes and log lines go to the notes of the task they belong to.
|
|
44
46
|
|
|
45
|
-
Fold a nested sub-step into its parent task's `
|
|
47
|
+
Fold a nested sub-step into its parent task's `detail` or notes. When a sub-step is independently verifiable, it becomes its own task with `dependsOn` on the parent.
|
|
46
48
|
|
|
47
|
-
When an item has no verification step, set `
|
|
49
|
+
When an item has no verification step, set its `goal` to `unverified: <what to check>`.
|
|
48
50
|
|
|
49
|
-
Task ids follow list order (`t1..tn`); translate stated dependencies after you order the tasks. `update` changes only status and notes, so check titles,
|
|
51
|
+
Task ids follow list order (`t1..tn`); translate stated dependencies after you order the tasks. `update` changes only status and notes, so check titles, goals, details and `dependsOn` before the `create` call.
|
|
@@ -5,32 +5,51 @@ description: Turn a request into a plan of independently verifiable tasks with t
|
|
|
5
5
|
|
|
6
6
|
# Prepare plan
|
|
7
7
|
|
|
8
|
-
Write the plan with the `plan` tool, action `create`. The plan is the source of truth: each task is worked from
|
|
8
|
+
Write the plan with the `plan` tool, action `create`. The plan is the source of truth: each task is worked from `get <task>` and updated when its goal's check passes.
|
|
9
9
|
|
|
10
10
|
## Process
|
|
11
11
|
|
|
12
|
-
1. **Orient.**
|
|
12
|
+
1. **Orient.** Read the code the change touches, its callers, its tests and the repo instructions. Every fact, `path:line` and command in the plan comes from code you or a helper read, not from memory. Stop when you can name the behaviour to change, the seam it goes through, and the check that proves it.
|
|
13
13
|
|
|
14
|
-
2. **
|
|
14
|
+
2. **Gather facts before writing.** When others explore for you, ask each for a report with `path:line` evidence: current behaviour, seams, callers, tests and checks. Write the plan from those reports and the user's decisions. A plan written from conversation memory cites nothing and leaves every task to guesswork.
|
|
15
15
|
|
|
16
|
-
3. **
|
|
16
|
+
3. **Resolve decisions.** Find facts in the code and docs rather than asking. Ask the user only for a decision that changes the plan. Settle a blocking choice before the plan exists and record it in the plan `detail`.
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
4. **Write the plan detail.** Plan `detail` is markdown in sections (headings inside it use `####` or lower, below the fixed `##`):
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
- Current behaviour, with `path:line` for each site.
|
|
21
|
+
- The problem: why that code is wrong today.
|
|
22
|
+
- The chosen design, and the alternatives rejected, with the reason.
|
|
23
|
+
- Accepted behaviour changes.
|
|
24
|
+
- Out of scope.
|
|
25
|
+
- Open questions.
|
|
21
26
|
|
|
22
|
-
5. **
|
|
27
|
+
5. **Write the tasks.** Prefer existing code and the smallest change that meets the goal. Split the work into tasks that are independently verifiable and leave the repo working. For a feature, slice by behaviour rather than by layer: "create with status and notes", not "add schema", then "add handler". For a wide mechanical refactor, plan expand, migrate, contract.
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
Each task is executed from `get <task>` alone: the plan's goal and detail, and that one task. Write it for a reader who never saw this conversation.
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
- `title`: the deliverable or behaviour, one line.
|
|
32
|
+
- `goal`: the observable result and the command that proves it.
|
|
33
|
+
- `detail`: the approach, the interfaces it must match, the pitfalls, and a `Where:` line citing `path:line` for each file that changes. Headings inside it use `####` or lower, below the fixed `###`.
|
|
34
|
+
|
|
35
|
+
Add `dependsOn` only for a real dependency; tasks without one run at the same time, so keep their files disjoint.
|
|
36
|
+
|
|
37
|
+
6. **Self-check.** Trace every requirement in the request to a task, and every task to a goal with a check. Replace each "handle edge cases" or "write tests" with the behaviour and the check. Confirm that no task names a path or interface that a later task defines differently. Read each task as `get <task>` will show it and confirm it can be done and checked without this conversation.
|
|
38
|
+
|
|
39
|
+
7. **Create the plan.** One `plan create` call. Set `title` to three to eight words naming the outcome. Task ids follow list order (`t1..tn`), so write `dependsOn` with those ids.
|
|
40
|
+
|
|
41
|
+
## Writing a goal
|
|
42
|
+
|
|
43
|
+
The plan `goal` and a task `goal` mean the same thing: the observable outcome plus the check that proves it, for example:
|
|
27
44
|
|
|
28
45
|
- `npm test -- packages/x: 42 passed`
|
|
29
46
|
- `curl localhost:8080/health returns 200 with {"ok":true}`
|
|
30
47
|
- `open the settings page and press Save: the toast reads Saved`
|
|
31
48
|
|
|
49
|
+
The plan goal is the final verification of the whole plan. Mark a task `done` only after its goal's check passed.
|
|
50
|
+
|
|
32
51
|
For manual or visual work, describe the observation and how to reproduce it. When no check exists yet, write the one to run: `unverified: confirm X with Y`.
|
|
33
52
|
|
|
34
53
|
## Hand off
|
|
35
54
|
|
|
36
|
-
Report the plan
|
|
55
|
+
Report the plan id, the task count and any decision still open. Planning ends here: start the tasks when the user asks for it.
|
package/src/inspector.ts
CHANGED
|
@@ -30,11 +30,13 @@ type PlansScreenOptions = {
|
|
|
30
30
|
keys: KeybindingsManager;
|
|
31
31
|
/** Terminal rows, read per frame. */
|
|
32
32
|
height: () => number;
|
|
33
|
-
setActive: (
|
|
33
|
+
setActive: (id: string) => void;
|
|
34
34
|
onClose: () => void;
|
|
35
35
|
requestRender: () => void;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
type Focus = "list" | "detail";
|
|
39
|
+
|
|
38
40
|
/** Fixed rows: title, top rule, bottom rule, footer. */
|
|
39
41
|
const CHROME_ROWS = 4;
|
|
40
42
|
/** Prose past this width is hard to follow on a wide terminal; the pane leaves the rest empty. */
|
|
@@ -42,14 +44,12 @@ const MAX_TEXT_WIDTH = 120;
|
|
|
42
44
|
/** Rendered markdown by theme, then width and text; cleared by invalidate() and on a width change. */
|
|
43
45
|
const MARKDOWN_CACHE = new WeakMap<Theme, Map<string, string[]>>();
|
|
44
46
|
|
|
45
|
-
type Focus = "list" | "detail";
|
|
46
|
-
|
|
47
47
|
function hint(keys: KeybindingsManager, ids: Keybinding[], description: string): string {
|
|
48
48
|
return rawKeyHint(ids.flatMap((id) => keys.getKeys(id)).join("/"), description);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/** Markdown reads a lone newline as a space; a planner's one-newline paragraph break is meant as a break. */
|
|
52
|
-
function hardBreaks(text: string): string {
|
|
52
|
+
export function hardBreaks(text: string): string {
|
|
53
53
|
const lines = text.split("\n");
|
|
54
54
|
let fenced = false;
|
|
55
55
|
return lines
|
|
@@ -90,7 +90,7 @@ function section(theme: Theme, word: string, width: number, trailing = ""): stri
|
|
|
90
90
|
|
|
91
91
|
/** The title row: the glyph, the title, and the id at the right edge, with the blockers still open after it. */
|
|
92
92
|
function taskLines(plan: Plan, task: PlanTask, theme: Theme, width: number, current: boolean): string[] {
|
|
93
|
-
const open = task.
|
|
93
|
+
const open = task.dependsOn.filter((id) => !plan.tasks.some((item) => item.id === id && item.status === "done"));
|
|
94
94
|
const id = theme.fg("muted", open.length ? `${task.id} · after ${open.join(", ")}` : task.id);
|
|
95
95
|
// Accent means headings and labels, so the task the reader is on is bold text instead.
|
|
96
96
|
const title = theme.fg("text", task.title);
|
|
@@ -111,7 +111,7 @@ function field(theme: Theme, label: string, value: string, width: number, prose
|
|
|
111
111
|
return [lead + first, ...rest.map((line) => " ".repeat(column) + line)];
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
/** One task: its title row, then the
|
|
114
|
+
/** One task: its title row, then the goal, the brief and notes of every task still open. */
|
|
115
115
|
function taskBlock(
|
|
116
116
|
plan: Plan,
|
|
117
117
|
task: PlanTask,
|
|
@@ -124,15 +124,15 @@ function taskBlock(
|
|
|
124
124
|
if (!expanded && task.status === "done") return head;
|
|
125
125
|
return [
|
|
126
126
|
...head,
|
|
127
|
-
//
|
|
128
|
-
...field(theme, "
|
|
129
|
-
...(task.
|
|
127
|
+
// Every label pads to nine columns, so the values start in one column.
|
|
128
|
+
...field(theme, "Goal", task.goal, width, true),
|
|
129
|
+
...(task.detail ? field(theme, "Detail", task.detail, width, true) : []),
|
|
130
130
|
...task.notes.flatMap((note) => ["", ...markdown(theme, note, Math.max(1, width - 2), 2)]),
|
|
131
131
|
"",
|
|
132
132
|
];
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
/** Goal, tasks and
|
|
135
|
+
/** Goal, tasks and detail as three sections; the headings show where the reader is in a long plan. */
|
|
136
136
|
function taskRows(plan: Plan, theme: Theme, width: number, expanded: boolean): string[] {
|
|
137
137
|
const current = plan.tasks.find((task) => task.status === "doing") ?? nextTask(plan);
|
|
138
138
|
const blocks = plan.tasks.flatMap((task) => taskBlock(plan, task, theme, width, task === current, expanded));
|
|
@@ -144,12 +144,13 @@ function taskRows(plan: Plan, theme: Theme, width: number, expanded: boolean): s
|
|
|
144
144
|
section(theme, "Tasks", width, theme.fg("muted", ` ${planCount(plan)}`)),
|
|
145
145
|
...blocks,
|
|
146
146
|
"",
|
|
147
|
-
section(theme, "
|
|
148
|
-
...markdown(theme, plan.
|
|
147
|
+
section(theme, "Detail", width),
|
|
148
|
+
...markdown(theme, plan.detail, width),
|
|
149
|
+
...plan.notes.flatMap((note) => ["", ...markdown(theme, note, Math.max(1, width - 2), 2)]),
|
|
149
150
|
].map((line) => truncateToWidth(line, width, "…", true));
|
|
150
151
|
}
|
|
151
152
|
|
|
152
|
-
/** Two rows per plan, like pi-delegate's job rows: cursor, status glyph,
|
|
153
|
+
/** Two rows per plan, like pi-delegate's job rows: cursor, status glyph, id and count, then the title. */
|
|
153
154
|
function sidebarRows(
|
|
154
155
|
plans: Plan[],
|
|
155
156
|
unreadable: string[],
|
|
@@ -167,18 +168,18 @@ function sidebarRows(
|
|
|
167
168
|
const cursor = chosen ? theme.fg(focus === "list" ? "accent" : "muted", "›") : " ";
|
|
168
169
|
return {
|
|
169
170
|
lines: [
|
|
170
|
-
`${cursor} ${statusText(theme, planStatus(plan))} ${theme.fg(chosen ? "accent" : "text", plan.
|
|
171
|
-
theme.fg("dim", ` ${plan.
|
|
171
|
+
`${cursor} ${statusText(theme, planStatus(plan))} ${theme.fg(chosen ? "accent" : "text", plan.id)} ${theme.fg("muted", planCount(plan))}`,
|
|
172
|
+
theme.fg("dim", ` ${plan.id === active ? "active · " : ""}${plan.title}`),
|
|
172
173
|
],
|
|
173
174
|
index: planIndex,
|
|
174
175
|
};
|
|
175
176
|
}),
|
|
176
|
-
...unreadable.map((
|
|
177
|
+
...unreadable.map((file) => ({ lines: [theme.fg("error", ` ? ${file} unreadable`)] })),
|
|
177
178
|
];
|
|
178
179
|
const rows = all.flatMap((entry) => entry.lines.map((line, at) => ({ line, index: entry.index, head: at === 0 })));
|
|
179
180
|
const selectedRow = rows.findIndex((row) => row.index === selected);
|
|
180
181
|
let start = Math.max(0, Math.min(selectedRow - Math.floor(height / 2), rows.length - height));
|
|
181
|
-
// A window never opens on a plan's second row, which would show a
|
|
182
|
+
// A window never opens on a plan's second row, which would show a title with no id above it.
|
|
182
183
|
while (start > 0 && !rows[start]!.head) start--;
|
|
183
184
|
const shown = rows.slice(start, start + height);
|
|
184
185
|
return {
|
|
@@ -210,7 +211,7 @@ export class PlansScreen implements Component {
|
|
|
210
211
|
constructor(options: PlansScreenOptions) {
|
|
211
212
|
this.options = options;
|
|
212
213
|
this.active = options.active;
|
|
213
|
-
const found = options.active ? options.plans.findIndex((plan) => plan.
|
|
214
|
+
const found = options.active ? options.plans.findIndex((plan) => plan.id === options.active) : -1;
|
|
214
215
|
this.index = Math.max(0, found);
|
|
215
216
|
}
|
|
216
217
|
|
|
@@ -267,8 +268,8 @@ export class PlansScreen implements Component {
|
|
|
267
268
|
else if (keys.matches(data, "tui.select.confirm")) {
|
|
268
269
|
const plan = this.options.plans[this.index];
|
|
269
270
|
if (!plan) return;
|
|
270
|
-
this.active = plan.
|
|
271
|
-
setActive(plan.
|
|
271
|
+
this.active = plan.id;
|
|
272
|
+
setActive(plan.id);
|
|
272
273
|
} else if (keys.matches(data, "app.tools.expand")) {
|
|
273
274
|
this.expanded = !this.expanded;
|
|
274
275
|
this.top = 0;
|
|
@@ -297,7 +298,7 @@ export class PlansScreen implements Component {
|
|
|
297
298
|
return { handled: true };
|
|
298
299
|
}
|
|
299
300
|
|
|
300
|
-
/** The right pane: the plan's
|
|
301
|
+
/** The right pane: the plan's id and title stay put over the goal, tasks and detail, with a counter below. */
|
|
301
302
|
private detail(plan: Plan, textWidth: number, width: number, body: number): string[] {
|
|
302
303
|
const { theme } = this.options;
|
|
303
304
|
if (this.lastTextWidth !== textWidth) {
|
|
@@ -306,9 +307,9 @@ export class PlansScreen implements Component {
|
|
|
306
307
|
}
|
|
307
308
|
const blocked = plan.tasks.some((task) => task.status === "blocked") ? " · blocked" : "";
|
|
308
309
|
const progress = theme.fg("muted", `${doneCount(plan)} of ${plan.tasks.length} done${blocked}`);
|
|
309
|
-
const
|
|
310
|
+
const head = `${theme.fg("accent", plan.id)} ${theme.fg("text", plan.title)}`;
|
|
310
311
|
const header = [
|
|
311
|
-
|
|
312
|
+
head + " ".repeat(Math.max(0, textWidth - visibleWidth(head) - visibleWidth(progress))) + progress,
|
|
312
313
|
];
|
|
313
314
|
const height = Math.max(0, body - header.length - 1);
|
|
314
315
|
const all = taskRows(plan, theme, textWidth, this.expanded);
|
|
@@ -353,7 +354,7 @@ export function openPlans(
|
|
|
353
354
|
ctx: ExtensionContext,
|
|
354
355
|
list: PlanList,
|
|
355
356
|
active: string | undefined,
|
|
356
|
-
setActive: (
|
|
357
|
+
setActive: (id: string) => void,
|
|
357
358
|
): Promise<void> {
|
|
358
359
|
const screen = { overlay: true, overlayOptions: { width: "100%", maxHeight: "100%", row: 0, col: 0 } } as const;
|
|
359
360
|
return ctx.ui.custom<void>(
|
package/src/store.ts
CHANGED
|
@@ -4,24 +4,24 @@ import { dirname, join, resolve } from "node:path";
|
|
|
4
4
|
import type { KeybindingHints, Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
5
5
|
import { wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
6
6
|
|
|
7
|
-
export const STATUSES = ["todo", "doing", "done", "blocked"] as const;
|
|
8
|
-
|
|
9
7
|
export type PlanStatus = (typeof STATUSES)[number];
|
|
10
8
|
|
|
11
9
|
export type PlanTask = {
|
|
12
10
|
id: string;
|
|
13
11
|
title: string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
goal: string;
|
|
13
|
+
detail: string;
|
|
14
|
+
dependsOn: string[];
|
|
17
15
|
status: PlanStatus;
|
|
18
16
|
notes: string[];
|
|
19
17
|
};
|
|
20
18
|
|
|
21
19
|
export type Plan = {
|
|
22
|
-
|
|
20
|
+
id: string;
|
|
21
|
+
title: string;
|
|
23
22
|
goal: string;
|
|
24
|
-
|
|
23
|
+
detail: string;
|
|
24
|
+
notes: string[];
|
|
25
25
|
tasks: PlanTask[];
|
|
26
26
|
};
|
|
27
27
|
|
|
@@ -31,6 +31,8 @@ export type PlanList = {
|
|
|
31
31
|
};
|
|
32
32
|
|
|
33
33
|
/** One status vocabulary for the widget, the tool results and the /plan overlay. */
|
|
34
|
+
export const STATUSES = ["todo", "doing", "done", "blocked"] as const;
|
|
35
|
+
|
|
34
36
|
export const STATUS = {
|
|
35
37
|
todo: { glyph: "○", color: "muted" },
|
|
36
38
|
doing: { glyph: "◐", color: "warning" },
|
|
@@ -38,7 +40,7 @@ export const STATUS = {
|
|
|
38
40
|
blocked: { glyph: "✗", color: "error" },
|
|
39
41
|
} as const satisfies Record<PlanStatus, { glyph: string; color: string }>;
|
|
40
42
|
|
|
41
|
-
const
|
|
43
|
+
const ID = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
42
44
|
const PLANS_IGNORE = ".pi/plans/";
|
|
43
45
|
|
|
44
46
|
/** Plans live at <repoRoot>/.pi/plans, beside pi-delegate's .pi/worktrees; outside git, the session cwd is the root. */
|
|
@@ -60,14 +62,42 @@ export function plansDir(root: string): string {
|
|
|
60
62
|
return join(root, ".pi", "plans");
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
function planPath(root: string,
|
|
64
|
-
if (!
|
|
65
|
-
return join(plansDir(root), `${
|
|
65
|
+
function planPath(root: string, id: string): string {
|
|
66
|
+
if (!ID.test(id)) throw new Error(`Plan id "${id}" must be kebab-case, for example my-plan.`);
|
|
67
|
+
return join(plansDir(root), `${id}.json`);
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
|
|
70
|
+
function isTask(value: unknown): value is PlanTask {
|
|
71
|
+
const task = value as PlanTask;
|
|
72
|
+
return (
|
|
73
|
+
typeof task?.id === "string" &&
|
|
74
|
+
typeof task.title === "string" &&
|
|
75
|
+
typeof task.goal === "string" &&
|
|
76
|
+
typeof task.detail === "string" &&
|
|
77
|
+
Array.isArray(task.dependsOn) &&
|
|
78
|
+
Array.isArray(task.notes) &&
|
|
79
|
+
STATUSES.includes(task.status)
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A file without the current shape is no plan: old files stay unreadable instead of crashing a renderer. */
|
|
84
|
+
function isPlan(value: unknown): value is Plan {
|
|
85
|
+
const plan = value as Plan;
|
|
86
|
+
return (
|
|
87
|
+
Array.isArray(plan?.tasks) &&
|
|
88
|
+
plan.tasks.every(isTask) &&
|
|
89
|
+
typeof plan.id === "string" &&
|
|
90
|
+
typeof plan.title === "string" &&
|
|
91
|
+
typeof plan.goal === "string" &&
|
|
92
|
+
typeof plan.detail === "string" &&
|
|
93
|
+
Array.isArray(plan.notes)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function readPlan(root: string, id: string): Plan | undefined {
|
|
69
98
|
try {
|
|
70
|
-
|
|
99
|
+
const value: unknown = JSON.parse(readFileSync(planPath(root, id), "utf8"));
|
|
100
|
+
return isPlan(value) ? value : undefined;
|
|
71
101
|
} catch (error) {
|
|
72
102
|
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
73
103
|
throw error;
|
|
@@ -75,7 +105,7 @@ export function readPlan(root: string, name: string): Plan | undefined {
|
|
|
75
105
|
}
|
|
76
106
|
|
|
77
107
|
export function writePlan(root: string, plan: Plan): void {
|
|
78
|
-
const file = planPath(root, plan.
|
|
108
|
+
const file = planPath(root, plan.id);
|
|
79
109
|
mkdirSync(dirname(file), { recursive: true });
|
|
80
110
|
// Temp file plus rename: a crash mid-write leaves the previous plan intact.
|
|
81
111
|
// shortcut: one writer, no lock; add one if a second process can write plans
|
|
@@ -96,9 +126,9 @@ export function listPlans(root: string): PlanList {
|
|
|
96
126
|
const unreadable: string[] = [];
|
|
97
127
|
for (const file of files.filter((name) => name.endsWith(".json")).sort()) {
|
|
98
128
|
try {
|
|
99
|
-
const
|
|
100
|
-
if (!
|
|
101
|
-
plans.push(
|
|
129
|
+
const value: unknown = JSON.parse(readFileSync(join(plansDir(root), file), "utf8"));
|
|
130
|
+
if (!isPlan(value)) throw new Error("not a plan");
|
|
131
|
+
plans.push(value);
|
|
102
132
|
} catch {
|
|
103
133
|
unreadable.push(file.slice(0, -5));
|
|
104
134
|
}
|
|
@@ -139,51 +169,62 @@ export function doneCount(plan: Plan): number {
|
|
|
139
169
|
/** The first todo task whose blockers are all done, the one to dispatch next. */
|
|
140
170
|
export function nextTask(plan: Plan): PlanTask | undefined {
|
|
141
171
|
const done = new Set(plan.tasks.filter((task) => task.status === "done").map((task) => task.id));
|
|
142
|
-
return plan.tasks.find((task) => task.status === "todo" && task.
|
|
172
|
+
return plan.tasks.find((task) => task.status === "todo" && task.dependsOn.every((id) => done.has(id)));
|
|
143
173
|
}
|
|
144
174
|
|
|
145
175
|
export function taskLine(task: PlanTask): string {
|
|
146
176
|
const parts = [`${task.id} [${task.status}] ${task.title}`];
|
|
147
|
-
if (task.
|
|
148
|
-
if (task.where) parts.push(`where ${task.where}`);
|
|
177
|
+
if (task.dependsOn.length) parts.push(`dependsOn ${task.dependsOn.join(" ")}`);
|
|
149
178
|
return parts.join(" — ");
|
|
150
179
|
}
|
|
151
180
|
|
|
152
|
-
/**
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
...[`doneWhen: ${task.doneWhen}`, ...task.notes.map((note) => `note: ${note}`)].map((line) => ` ${line}`),
|
|
157
|
-
];
|
|
181
|
+
/** Notes as list items; a continuation line indents by two so it stays inside its item. */
|
|
182
|
+
function noteItem(note: string): string[] {
|
|
183
|
+
const [first = "", ...rest] = note.split("\n");
|
|
184
|
+
return [`- ${first}`, ...rest.map((line) => ` ${line}`)];
|
|
158
185
|
}
|
|
159
186
|
|
|
160
|
-
|
|
187
|
+
function planBlock(plan: Plan): string {
|
|
161
188
|
return [
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
189
|
+
`# Plan ${plan.id}: ${plan.title} (${doneCount(plan)}/${plan.tasks.length} done)`,
|
|
190
|
+
"",
|
|
191
|
+
"## Goal",
|
|
192
|
+
plan.goal,
|
|
193
|
+
"",
|
|
194
|
+
"## Detail",
|
|
195
|
+
plan.detail,
|
|
196
|
+
...(plan.notes.length ? ["", "## Notes", ...plan.notes.flatMap(noteItem)] : []),
|
|
166
197
|
].join("\n");
|
|
167
198
|
}
|
|
168
199
|
|
|
169
|
-
|
|
200
|
+
function taskBlock(task: PlanTask): string {
|
|
170
201
|
return [
|
|
171
|
-
|
|
172
|
-
`
|
|
173
|
-
|
|
202
|
+
`## Task ${task.id} [${task.status}]: ${task.title}`,
|
|
203
|
+
...(task.dependsOn.length ? [`Depends on: ${task.dependsOn.join(", ")}`] : []),
|
|
204
|
+
"",
|
|
205
|
+
"### Goal",
|
|
206
|
+
task.goal,
|
|
174
207
|
"",
|
|
175
|
-
|
|
208
|
+
"### Detail",
|
|
209
|
+
task.detail,
|
|
210
|
+
...(task.notes.length ? ["", "### Notes", ...task.notes.flatMap(noteItem)] : []),
|
|
176
211
|
].join("\n");
|
|
177
212
|
}
|
|
178
213
|
|
|
214
|
+
/** One `get` result as markdown: the plan, then every task as its own section. */
|
|
215
|
+
export function planText(plan: Plan): string {
|
|
216
|
+
return [planBlock(plan), ...plan.tasks.map((task) => taskBlock(task))].join("\n\n");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The same plan header, then the one task an executor works from. */
|
|
220
|
+
export function taskText(plan: Plan, task: PlanTask): string {
|
|
221
|
+
return [planBlock(plan), taskBlock(task)].join("\n\n");
|
|
222
|
+
}
|
|
223
|
+
|
|
179
224
|
export function listText(root: string, list: PlanList, active: string | undefined): string {
|
|
180
225
|
if (!list.plans.length && !list.unreadable.length) return `No plans in ${plansDir(root)}.`;
|
|
181
226
|
return [
|
|
182
|
-
...list.plans.map((plan) => {
|
|
183
|
-
const next = nextTask(plan);
|
|
184
|
-
const mark = plan.name === active ? "*" : " ";
|
|
185
|
-
return `${mark} ${plan.name} ${planCount(plan)}${next ? ` · next ${next.id}` : ""}`;
|
|
186
|
-
}),
|
|
227
|
+
...list.plans.map((plan) => `${plan.id === active ? "*" : " "} ${plan.id} ${plan.title} ${planCount(plan)}`),
|
|
187
228
|
...list.unreadable.map((name) => `? ${name} unreadable`),
|
|
188
229
|
].join("\n");
|
|
189
230
|
}
|
|
@@ -196,14 +237,9 @@ export function planCount(plan: Plan): string {
|
|
|
196
237
|
return `${doneCount(plan)}/${plan.tasks.length}`;
|
|
197
238
|
}
|
|
198
239
|
|
|
199
|
-
/** `
|
|
240
|
+
/** `id title 1/4`: the line the widget head, the tool result header and the list all lead with. */
|
|
200
241
|
export function planSummary(theme: Theme, plan: Plan): string {
|
|
201
|
-
|
|
202
|
-
const doing = plan.tasks.find((task) => task.status === "doing");
|
|
203
|
-
if (doing) stats.push(`doing ${doing.id}`);
|
|
204
|
-
const next = nextTask(plan);
|
|
205
|
-
if (next) stats.push(`next ${next.id}`);
|
|
206
|
-
return `${theme.fg("accent", plan.name)} ${theme.fg("muted", stats.join(" · "))}`;
|
|
242
|
+
return `${theme.fg("accent", plan.id)} ${plan.title} ${theme.fg("muted", planCount(plan))}`;
|
|
207
243
|
}
|
|
208
244
|
|
|
209
245
|
/** The plan's own status: done when every task is, doing while one is, todo otherwise. */
|
|
@@ -214,8 +250,8 @@ export function planStatus(plan: Plan): Exclude<PlanStatus, "blocked"> {
|
|
|
214
250
|
|
|
215
251
|
/** A task row: glyph, id, title, and the blockers still open; a done blocker is no noise worth showing. */
|
|
216
252
|
export function taskRow(theme: Theme, plan: Plan, task: PlanTask): string {
|
|
217
|
-
const open = task.
|
|
218
|
-
const blocked = open.length ? ` ${theme.fg("muted", `·
|
|
253
|
+
const open = task.dependsOn.filter((id) => !plan.tasks.some((item) => item.id === id && item.status === "done"));
|
|
254
|
+
const blocked = open.length ? ` ${theme.fg("muted", `· dependsOn ${open.join(" ")}`)}` : "";
|
|
219
255
|
return `${statusText(theme, task.status)} ${theme.fg("accent", task.id)} ${task.title}${blocked}`;
|
|
220
256
|
}
|
|
221
257
|
|
|
@@ -240,12 +276,11 @@ export function fact(
|
|
|
240
276
|
);
|
|
241
277
|
}
|
|
242
278
|
|
|
243
|
-
/** The lines under a task row:
|
|
279
|
+
/** The lines under a task row: its goal, its brief as dim text, and its dim notes as written. */
|
|
244
280
|
export function taskFacts(theme: Theme, task: PlanTask, width: number): string[] {
|
|
245
281
|
return [
|
|
246
|
-
|
|
247
|
-
...(task.
|
|
248
|
-
...fact(theme, "Done when: ", task.doneWhen, width),
|
|
282
|
+
...fact(theme, "Goal: ", task.goal, width),
|
|
283
|
+
...(task.detail ? fact(theme, "Detail: ", task.detail, width, 2, "dim") : []),
|
|
249
284
|
...task.notes.flatMap((note) => fact(theme, "", note, width)),
|
|
250
285
|
];
|
|
251
286
|
}
|
|
@@ -255,7 +290,12 @@ export function moreLines(theme: Theme, hidden: number, keyHint: KeybindingHints
|
|
|
255
290
|
return `${theme.fg("muted", `... (${hidden} more lines,`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`;
|
|
256
291
|
}
|
|
257
292
|
|
|
258
|
-
/** The one
|
|
259
|
-
export function
|
|
260
|
-
return
|
|
293
|
+
/** The widget head, then one line per task in progress: parallel delegates can run several at once. */
|
|
294
|
+
export function widgetLines(theme: Theme, plan: Plan): string[] {
|
|
295
|
+
return [
|
|
296
|
+
`${theme.fg("accent", "plan")} ${plan.title} ${theme.fg("muted", planCount(plan))}`,
|
|
297
|
+
...plan.tasks
|
|
298
|
+
.filter((task) => task.status === "doing")
|
|
299
|
+
.map((task) => ` ${theme.fg("muted", task.id)} ${task.title}`),
|
|
300
|
+
];
|
|
261
301
|
}
|