@deathbeam/pi-plan 0.0.1 → 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/src/store.ts CHANGED
@@ -1,26 +1,27 @@
1
1
  import { execFileSync } from "node:child_process";
2
2
  import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, writeFileSync } from "node:fs";
3
3
  import { dirname, join, resolve } from "node:path";
4
- import type { KeybindingHints, Theme } from "@earendil-works/pi-coding-agent";
5
-
6
- export const STATUSES = ["todo", "doing", "done", "blocked"] as const;
4
+ import type { KeybindingHints, Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
5
+ import { wrapTextWithAnsi } from "@earendil-works/pi-tui";
7
6
 
8
7
  export type PlanStatus = (typeof STATUSES)[number];
9
8
 
10
9
  export type PlanTask = {
11
10
  id: string;
12
11
  title: string;
13
- where?: string;
14
- blockedBy: string[];
15
- doneWhen: string;
12
+ goal: string;
13
+ detail: string;
14
+ dependsOn: string[];
16
15
  status: PlanStatus;
17
16
  notes: string[];
18
17
  };
19
18
 
20
19
  export type Plan = {
21
- name: string;
20
+ id: string;
21
+ title: string;
22
22
  goal: string;
23
- context: string;
23
+ detail: string;
24
+ notes: string[];
24
25
  tasks: PlanTask[];
25
26
  };
26
27
 
@@ -30,6 +31,8 @@ export type PlanList = {
30
31
  };
31
32
 
32
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
+
33
36
  export const STATUS = {
34
37
  todo: { glyph: "○", color: "muted" },
35
38
  doing: { glyph: "◐", color: "warning" },
@@ -37,7 +40,7 @@ export const STATUS = {
37
40
  blocked: { glyph: "✗", color: "error" },
38
41
  } as const satisfies Record<PlanStatus, { glyph: string; color: string }>;
39
42
 
40
- const NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
43
+ const ID = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
41
44
  const PLANS_IGNORE = ".pi/plans/";
42
45
 
43
46
  /** Plans live at <repoRoot>/.pi/plans, beside pi-delegate's .pi/worktrees; outside git, the session cwd is the root. */
@@ -59,14 +62,42 @@ export function plansDir(root: string): string {
59
62
  return join(root, ".pi", "plans");
60
63
  }
61
64
 
62
- function planPath(root: string, name: string): string {
63
- if (!NAME.test(name)) throw new Error(`Plan name "${name}" must be kebab-case, for example my-plan.`);
64
- return join(plansDir(root), `${name}.json`);
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`);
65
68
  }
66
69
 
67
- export function readPlan(root: string, name: string): Plan | undefined {
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 {
68
98
  try {
69
- return JSON.parse(readFileSync(planPath(root, name), "utf8")) as Plan;
99
+ const value: unknown = JSON.parse(readFileSync(planPath(root, id), "utf8"));
100
+ return isPlan(value) ? value : undefined;
70
101
  } catch (error) {
71
102
  if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
72
103
  throw error;
@@ -74,7 +105,7 @@ export function readPlan(root: string, name: string): Plan | undefined {
74
105
  }
75
106
 
76
107
  export function writePlan(root: string, plan: Plan): void {
77
- const file = planPath(root, plan.name);
108
+ const file = planPath(root, plan.id);
78
109
  mkdirSync(dirname(file), { recursive: true });
79
110
  // Temp file plus rename: a crash mid-write leaves the previous plan intact.
80
111
  // shortcut: one writer, no lock; add one if a second process can write plans
@@ -95,9 +126,9 @@ export function listPlans(root: string): PlanList {
95
126
  const unreadable: string[] = [];
96
127
  for (const file of files.filter((name) => name.endsWith(".json")).sort()) {
97
128
  try {
98
- const plan = JSON.parse(readFileSync(join(plansDir(root), file), "utf8")) as Plan;
99
- if (!Array.isArray(plan?.tasks)) throw new Error("no tasks");
100
- plans.push(plan);
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);
101
132
  } catch {
102
133
  unreadable.push(file.slice(0, -5));
103
134
  }
@@ -138,51 +169,62 @@ export function doneCount(plan: Plan): number {
138
169
  /** The first todo task whose blockers are all done, the one to dispatch next. */
139
170
  export function nextTask(plan: Plan): PlanTask | undefined {
140
171
  const done = new Set(plan.tasks.filter((task) => task.status === "done").map((task) => task.id));
141
- return plan.tasks.find((task) => task.status === "todo" && task.blockedBy.every((id) => done.has(id)));
172
+ return plan.tasks.find((task) => task.status === "todo" && task.dependsOn.every((id) => done.has(id)));
142
173
  }
143
174
 
144
175
  export function taskLine(task: PlanTask): string {
145
176
  const parts = [`${task.id} [${task.status}] ${task.title}`];
146
- if (task.blockedBy.length) parts.push(`blockedBy ${task.blockedBy.join(" ")}`);
147
- if (task.where) parts.push(`where ${task.where}`);
177
+ if (task.dependsOn.length) parts.push(`dependsOn ${task.dependsOn.join(" ")}`);
148
178
  return parts.join(" — ");
149
179
  }
150
180
 
151
- /** One task as text: the status line, then the check and notes indented under it. */
152
- export function taskLines(task: PlanTask): string[] {
153
- return [
154
- taskLine(task),
155
- ...[`doneWhen: ${task.doneWhen}`, ...task.notes.map((note) => `note: ${note}`)].map((line) => ` ${line}`),
156
- ];
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}`)];
157
185
  }
158
186
 
159
- export function planText(plan: Plan): string {
187
+ function planBlock(plan: Plan): string {
160
188
  return [
161
- `plan ${plan.name} ${doneCount(plan)}/${plan.tasks.length}`,
162
- `goal: ${plan.goal}`,
163
- `context: ${plan.context}`,
164
- ...plan.tasks.flatMap(taskLines),
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)] : []),
165
197
  ].join("\n");
166
198
  }
167
199
 
168
- export function taskText(plan: Plan, task: PlanTask): string {
200
+ function taskBlock(task: PlanTask): string {
169
201
  return [
170
- `plan ${plan.name} ${doneCount(plan)}/${plan.tasks.length}`,
171
- `goal: ${plan.goal}`,
172
- `context: ${plan.context}`,
202
+ `## Task ${task.id} [${task.status}]: ${task.title}`,
203
+ ...(task.dependsOn.length ? [`Depends on: ${task.dependsOn.join(", ")}`] : []),
173
204
  "",
174
- ...taskLines(task),
205
+ "### Goal",
206
+ task.goal,
207
+ "",
208
+ "### Detail",
209
+ task.detail,
210
+ ...(task.notes.length ? ["", "### Notes", ...task.notes.flatMap(noteItem)] : []),
175
211
  ].join("\n");
176
212
  }
177
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
+
178
224
  export function listText(root: string, list: PlanList, active: string | undefined): string {
179
225
  if (!list.plans.length && !list.unreadable.length) return `No plans in ${plansDir(root)}.`;
180
226
  return [
181
- ...list.plans.map((plan) => {
182
- const next = nextTask(plan);
183
- const mark = plan.name === active ? "*" : " ";
184
- return `${mark} ${plan.name} ${planCount(plan)}${next ? ` · next ${next.id}` : ""}`;
185
- }),
227
+ ...list.plans.map((plan) => `${plan.id === active ? "*" : " "} ${plan.id} ${plan.title} ${planCount(plan)}`),
186
228
  ...list.unreadable.map((name) => `? ${name} unreadable`),
187
229
  ].join("\n");
188
230
  }
@@ -195,14 +237,9 @@ export function planCount(plan: Plan): string {
195
237
  return `${doneCount(plan)}/${plan.tasks.length}`;
196
238
  }
197
239
 
198
- /** `name 1/4 · doing t2 · next t3`: the line the widget, the tool header and the list all lead with. */
240
+ /** `id title 1/4`: the line the widget head, the tool result header and the list all lead with. */
199
241
  export function planSummary(theme: Theme, plan: Plan): string {
200
- const stats = [planCount(plan)];
201
- const doing = plan.tasks.find((task) => task.status === "doing");
202
- if (doing) stats.push(`doing ${doing.id}`);
203
- const next = nextTask(plan);
204
- if (next) stats.push(`next ${next.id}`);
205
- 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))}`;
206
243
  }
207
244
 
208
245
  /** The plan's own status: done when every task is, doing while one is, todo otherwise. */
@@ -213,18 +250,39 @@ export function planStatus(plan: Plan): Exclude<PlanStatus, "blocked"> {
213
250
 
214
251
  /** A task row: glyph, id, title, and the blockers still open; a done blocker is no noise worth showing. */
215
252
  export function taskRow(theme: Theme, plan: Plan, task: PlanTask): string {
216
- const open = task.blockedBy.filter((id) => !plan.tasks.some((item) => item.id === id && item.status === "done"));
217
- const blocked = open.length ? ` ${theme.fg("muted", `· blockedBy ${open.join(" ")}`)}` : "";
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(" ")}`)}` : "";
218
255
  return `${statusText(theme, task.status)} ${theme.fg("accent", task.id)} ${task.title}${blocked}`;
219
256
  }
220
257
 
221
- /** The muted lines under a task row: where it lives, its check, and its notes. */
222
- export function taskFacts(theme: Theme, task: PlanTask): string[] {
258
+ /** Wrapped rows whose continuations sit `indent` columns in, so a wrapped line never reads as a new one. */
259
+ export function hanging(text: string, width: number, indent: number): string[] {
260
+ return wrapTextWithAnsi(text, Math.max(1, width - indent)).map(
261
+ (line, index) => (index ? " ".repeat(indent) : "") + line,
262
+ );
263
+ }
264
+
265
+ /** A labelled line under a task row or in the plan header: the label muted so it reads as a key, the value in `color`, wrapping in the label's column. */
266
+ export function fact(
267
+ theme: Theme,
268
+ label: string,
269
+ text: string,
270
+ width: number,
271
+ indent = 2,
272
+ color: ThemeColor = label ? "text" : "dim",
273
+ ): string[] {
274
+ return hanging((label ? theme.fg("muted", label) : "") + theme.fg(color, text), width - indent, label.length).map(
275
+ (line) => " ".repeat(indent) + line,
276
+ );
277
+ }
278
+
279
+ /** The lines under a task row: its goal, its brief as dim text, and its dim notes as written. */
280
+ export function taskFacts(theme: Theme, task: PlanTask, width: number): string[] {
223
281
  return [
224
- ...(task.where ? [`where ${task.where}`] : []),
225
- `doneWhen: ${task.doneWhen}`,
226
- ...task.notes.map((note) => `note: ${note}`),
227
- ].map((line) => ` ${theme.fg("dim", line)}`);
282
+ ...fact(theme, "Goal: ", task.goal, width),
283
+ ...(task.detail ? fact(theme, "Detail: ", task.detail, width, 2, "dim") : []),
284
+ ...task.notes.flatMap((note) => fact(theme, "", note, width)),
285
+ ];
228
286
  }
229
287
 
230
288
  /** pi's built-in expand hint; callers place it on its own row, so it carries no leading newline. */
@@ -232,7 +290,12 @@ export function moreLines(theme: Theme, hidden: number, keyHint: KeybindingHints
232
290
  return `${theme.fg("muted", `... (${hidden} more lines,`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`;
233
291
  }
234
292
 
235
- /** The one-line widget: the plan summary every other view also leads with. */
236
- export function widgetLine(theme: Theme, plan: Plan): string {
237
- return `${theme.fg("accent", "plan")} ${planSummary(theme, plan)}`;
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
+ ];
238
301
  }