@yagni-app/code 0.1.0 → 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.
Files changed (155) hide show
  1. package/README.md +65 -11
  2. package/dist/claudeCompat.d.ts +109 -0
  3. package/dist/claudeCompat.js +260 -0
  4. package/dist/claudePlugins.d.ts +109 -0
  5. package/dist/claudePlugins.js +336 -0
  6. package/dist/cli.d.ts +1 -5
  7. package/dist/cli.js +79 -20
  8. package/dist/config.d.ts +1 -1
  9. package/dist/config.js +4 -2
  10. package/dist/crashReport.d.ts +135 -0
  11. package/dist/crashReport.js +291 -0
  12. package/dist/credentials.js +2 -1
  13. package/dist/distribution.d.ts +15 -0
  14. package/dist/distribution.js +45 -0
  15. package/dist/doctor.d.ts +27 -0
  16. package/dist/doctor.js +81 -3
  17. package/dist/extension/advisor.d.ts +84 -0
  18. package/dist/extension/advisor.js +101 -0
  19. package/dist/extension/askAdvisorTool.d.ts +83 -0
  20. package/dist/extension/askAdvisorTool.js +194 -0
  21. package/dist/extension/askYagniTool.d.ts +39 -0
  22. package/dist/extension/askYagniTool.js +63 -0
  23. package/dist/extension/bless.d.ts +45 -0
  24. package/dist/extension/bless.js +87 -0
  25. package/dist/extension/boostCommand.d.ts +144 -0
  26. package/dist/extension/boostCommand.js +263 -0
  27. package/dist/extension/branding.d.ts +79 -0
  28. package/dist/extension/branding.js +156 -0
  29. package/dist/extension/chipEditor.d.ts +117 -0
  30. package/dist/extension/chipEditor.js +373 -0
  31. package/dist/extension/claudeRules.d.ts +54 -0
  32. package/dist/extension/claudeRules.js +180 -0
  33. package/dist/extension/config.d.ts +173 -0
  34. package/dist/extension/config.js +194 -0
  35. package/dist/extension/costHud.d.ts +186 -0
  36. package/dist/extension/costHud.js +293 -0
  37. package/dist/extension/crashReport.d.ts +89 -0
  38. package/dist/extension/crashReport.js +241 -0
  39. package/dist/extension/decisionCapture.d.ts +52 -0
  40. package/dist/extension/decisionCapture.js +66 -0
  41. package/dist/extension/decisions.d.ts +83 -0
  42. package/dist/extension/decisions.js +200 -0
  43. package/dist/extension/diagnostics.d.ts +41 -0
  44. package/dist/extension/diagnostics.js +110 -0
  45. package/dist/extension/index.d.ts +155 -0
  46. package/dist/extension/index.js +597 -0
  47. package/dist/extension/initDone.d.ts +28 -0
  48. package/dist/extension/initDone.js +66 -0
  49. package/dist/extension/initPass.d.ts +170 -0
  50. package/dist/extension/initPass.js +394 -0
  51. package/dist/extension/mcpTools.d.ts +57 -0
  52. package/dist/extension/mcpTools.js +132 -0
  53. package/dist/extension/nextWorkTool.d.ts +51 -0
  54. package/dist/extension/nextWorkTool.js +80 -0
  55. package/dist/extension/permission.d.ts +91 -0
  56. package/dist/extension/permission.js +236 -0
  57. package/dist/extension/pipeline/activity.d.ts +37 -0
  58. package/dist/extension/pipeline/activity.js +151 -0
  59. package/dist/extension/pipeline/activityFeed.d.ts +59 -0
  60. package/dist/extension/pipeline/activityFeed.js +175 -0
  61. package/dist/extension/pipeline/budget.d.ts +48 -0
  62. package/dist/extension/pipeline/budget.js +68 -0
  63. package/dist/extension/pipeline/checkpoint.d.ts +31 -0
  64. package/dist/extension/pipeline/checkpoint.js +176 -0
  65. package/dist/extension/pipeline/eval.d.ts +205 -0
  66. package/dist/extension/pipeline/eval.js +226 -0
  67. package/dist/extension/pipeline/events.d.ts +56 -0
  68. package/dist/extension/pipeline/events.js +147 -0
  69. package/dist/extension/pipeline/findings.d.ts +42 -0
  70. package/dist/extension/pipeline/findings.js +144 -0
  71. package/dist/extension/pipeline/finish.d.ts +128 -0
  72. package/dist/extension/pipeline/finish.js +307 -0
  73. package/dist/extension/pipeline/goCommand.d.ts +146 -0
  74. package/dist/extension/pipeline/goCommand.js +1085 -0
  75. package/dist/extension/pipeline/goCompareCommand.d.ts +58 -0
  76. package/dist/extension/pipeline/goCompareCommand.js +203 -0
  77. package/dist/extension/pipeline/goFlags.d.ts +40 -0
  78. package/dist/extension/pipeline/goFlags.js +46 -0
  79. package/dist/extension/pipeline/goStatusCommands.d.ts +59 -0
  80. package/dist/extension/pipeline/goStatusCommands.js +188 -0
  81. package/dist/extension/pipeline/invocation.d.ts +45 -0
  82. package/dist/extension/pipeline/invocation.js +64 -0
  83. package/dist/extension/pipeline/orchestrator.d.ts +131 -0
  84. package/dist/extension/pipeline/orchestrator.js +645 -0
  85. package/dist/extension/pipeline/personas.d.ts +44 -0
  86. package/dist/extension/pipeline/personas.js +248 -0
  87. package/dist/extension/pipeline/resilience.d.ts +85 -0
  88. package/dist/extension/pipeline/resilience.js +166 -0
  89. package/dist/extension/pipeline/resume.d.ts +18 -0
  90. package/dist/extension/pipeline/resume.js +106 -0
  91. package/dist/extension/pipeline/runCostTable.d.ts +37 -0
  92. package/dist/extension/pipeline/runCostTable.js +165 -0
  93. package/dist/extension/pipeline/runRegistry.d.ts +112 -0
  94. package/dist/extension/pipeline/runRegistry.js +202 -0
  95. package/dist/extension/pipeline/runSession.d.ts +152 -0
  96. package/dist/extension/pipeline/runSession.js +167 -0
  97. package/dist/extension/pipeline/runState.d.ts +177 -0
  98. package/dist/extension/pipeline/runState.js +275 -0
  99. package/dist/extension/pipeline/runner.d.ts +98 -0
  100. package/dist/extension/pipeline/runner.js +310 -0
  101. package/dist/extension/pipeline/scrubSecrets.d.ts +15 -0
  102. package/dist/extension/pipeline/scrubSecrets.js +32 -0
  103. package/dist/extension/pipeline/stages.d.ts +53 -0
  104. package/dist/extension/pipeline/stages.js +115 -0
  105. package/dist/extension/pipeline/ticketResolution.d.ts +34 -0
  106. package/dist/extension/pipeline/ticketResolution.js +75 -0
  107. package/dist/extension/pipeline/types.d.ts +439 -0
  108. package/dist/extension/pipeline/types.js +48 -0
  109. package/dist/extension/pipeline/verify.d.ts +277 -0
  110. package/dist/extension/pipeline/verify.js +763 -0
  111. package/dist/extension/pipeline/workspace.d.ts +32 -0
  112. package/dist/extension/pipeline/workspace.js +73 -0
  113. package/dist/extension/pipeline/worktree.d.ts +107 -0
  114. package/dist/extension/pipeline/worktree.js +202 -0
  115. package/dist/extension/provider.d.ts +17 -0
  116. package/dist/extension/provider.js +33 -0
  117. package/dist/extension/recall.d.ts +93 -0
  118. package/dist/extension/recall.js +190 -0
  119. package/dist/extension/recordContextTool.d.ts +38 -0
  120. package/dist/extension/recordContextTool.js +85 -0
  121. package/dist/extension/recordDecisionTool.d.ts +52 -0
  122. package/dist/extension/recordDecisionTool.js +102 -0
  123. package/dist/extension/repoDocs.d.ts +81 -0
  124. package/dist/extension/repoDocs.js +260 -0
  125. package/dist/extension/rerouteNotice.d.ts +42 -0
  126. package/dist/extension/rerouteNotice.js +67 -0
  127. package/dist/extension/resilientFetch.d.ts +60 -0
  128. package/dist/extension/resilientFetch.js +133 -0
  129. package/dist/extension/reviewTool.d.ts +34 -0
  130. package/dist/extension/reviewTool.js +81 -0
  131. package/dist/extension/sessionRuns.d.ts +45 -0
  132. package/dist/extension/sessionRuns.js +77 -0
  133. package/dist/extension/spool.d.ts +92 -0
  134. package/dist/extension/spool.js +266 -0
  135. package/dist/extension/stateHome.d.ts +2 -0
  136. package/dist/extension/stateHome.js +6 -0
  137. package/dist/extension/subagents.d.ts +145 -0
  138. package/dist/extension/subagents.js +326 -0
  139. package/dist/extension/surface.d.ts +10 -0
  140. package/dist/extension/surface.js +12 -0
  141. package/dist/extension/todos.d.ts +110 -0
  142. package/dist/extension/todos.js +217 -0
  143. package/dist/extension/tokenProvider.d.ts +93 -0
  144. package/dist/extension/tokenProvider.js +234 -0
  145. package/dist/launch.d.ts +25 -3
  146. package/dist/launch.js +27 -9
  147. package/dist/login.d.ts +7 -0
  148. package/dist/login.js +3 -1
  149. package/dist/paths.d.ts +13 -4
  150. package/dist/paths.js +17 -5
  151. package/dist/profiles.d.ts +1 -1
  152. package/dist/profiles.js +5 -2
  153. package/dist/upgrade.d.ts +97 -0
  154. package/dist/upgrade.js +284 -0
  155. package/package.json +11 -15
@@ -0,0 +1,326 @@
1
+ /**
2
+ * The general `subagent` tool — delegate a self-contained task to an agent
3
+ * with its own fresh context window, optionally in parallel.
4
+ *
5
+ * Reuses the /go pipeline's battle-tested child runner (`runStage`: NDJSON
6
+ * stream folding, output caps, abort escalation) through its `personaBody`
7
+ * seam, so a subagent is exactly a grounded pi child whose system prompt is
8
+ * the agent definition's body instead of a /go persona.
9
+ *
10
+ * Agent definitions are the Claude Code format — markdown with
11
+ * name/description/tools/model frontmatter — discovered from the repo's
12
+ * `.claude/agents/` and `.pi/agents/`, the user's `~/.claude/agents/`, and
13
+ * any Claude Code plugin `agents/` dirs the launcher passed via
14
+ * `YAGNI_CLAUDE_AGENT_DIRS` (project beats user beats plugin on a name
15
+ * collision). A built-in `general` agent is always available, so the tool
16
+ * works in a repo with no definitions at all.
17
+ * Claude model names map onto YAGNI tiers (opus→advanced, sonnet→standard,
18
+ * haiku→efficient); unknown models run on standard.
19
+ */
20
+ import * as fs from "node:fs";
21
+ import { homedir } from "node:os";
22
+ import { delimiter, join } from "node:path";
23
+ import { parseFrontmatter } from "@earendil-works/pi-coding-agent";
24
+ import { Type } from "typebox";
25
+ import { sanitizeCallerSegment } from "./config.js";
26
+ import { runStage } from "./pipeline/runner.js";
27
+ /**
28
+ * YAG-471 attribution: the `x-yagni-caller` prefix for a subagent invocation.
29
+ * The sanitized agent name is capped so the WHOLE label (prefix + name) stays
30
+ * within the model proxy's 64-char caller-label limit.
31
+ */
32
+ const SUBAGENT_CALLER_PREFIX = "subagent:";
33
+ export const SUBAGENT_TOOL_NAME = "subagent";
34
+ export const GENERAL_AGENT_NAME = "general";
35
+ export const MAX_PARALLEL_SUBAGENTS = 4;
36
+ /**
37
+ * The default tool surface a subagent gets when its definition declares none:
38
+ * the full working set plus grounded answers, mirroring what a Claude Code
39
+ * subagent would inherit.
40
+ */
41
+ export const DEFAULT_SUBAGENT_TOOLS = [
42
+ "read",
43
+ "grep",
44
+ "find",
45
+ "ls",
46
+ "bash",
47
+ "edit",
48
+ "write",
49
+ "ask_yagni",
50
+ ];
51
+ /**
52
+ * Claude Code plugin `agents/` dirs, delimiter-joined by the yagni launcher
53
+ * (see yagni-code-cli claudeCompat.ts — trust gating happens there).
54
+ */
55
+ export const CLAUDE_AGENT_DIRS_ENV = "YAGNI_CLAUDE_AGENT_DIRS";
56
+ const CLAUDE_COMPAT_DISABLE_ENV = "YAGNI_DISABLE_CLAUDE_COMPAT";
57
+ function pluginAgentDirs(env) {
58
+ const disabled = env[CLAUDE_COMPAT_DISABLE_ENV];
59
+ if (disabled !== undefined && disabled !== "" && disabled !== "0")
60
+ return [];
61
+ const raw = env[CLAUDE_AGENT_DIRS_ENV];
62
+ if (!raw)
63
+ return [];
64
+ return raw.split(delimiter).filter(Boolean);
65
+ }
66
+ const GENERAL_BODY = `You are a capable software-engineering subagent with a fresh context window. Complete the task you are given end to end, autonomously.
67
+
68
+ You are grounded in how THIS company works: call ask_yagni before inferring a convention, an ownership rule, or anything organization-specific.
69
+
70
+ Your final message is your report back to the driving agent, which has NOT seen what you read or did. Make it compressed and complete: what you did, what you found, exact file paths and key excerpts, and anything the driver must know before continuing.`;
71
+ const GENERAL_AGENT = {
72
+ name: GENERAL_AGENT_NAME,
73
+ description: "General-purpose agent for research, multi-file changes, and self-contained tasks.",
74
+ model: "standard",
75
+ body: GENERAL_BODY,
76
+ source: "builtin",
77
+ };
78
+ // Concrete tiers a subagent can actually run on. `balanced` is deliberately NOT
79
+ // a member here even though it is a member of `ModelTier`: a subagent needs
80
+ // ONE model for its whole run, and balanced is a session-level routing policy,
81
+ // not a rung — see mapModelTier's explicit branch below.
82
+ const TIER_VALUES = ["peak", "advanced", "standard", "efficient"];
83
+ /** Claude Code model names → YAGNI tiers; YAGNI tiers pass through. */
84
+ export function mapModelTier(model) {
85
+ if (!model)
86
+ return "standard";
87
+ const normalized = model.trim().toLowerCase();
88
+ if (TIER_VALUES.includes(normalized))
89
+ return normalized;
90
+ // `balanced` names a SESSION routing policy, not a tier a subagent can run
91
+ // on. Handled explicitly rather than falling through to the catch-all
92
+ // below, so frontmatter that names the policy reads as an intentional
93
+ // "router-decide" choice, not an unrecognized string: it lands on the
94
+ // built-in default lane (standard) — the session-level policy (driver
95
+ // turns) owns balanced's resolution; this is just a safe landing spot for
96
+ // frontmatter that names it.
97
+ if (normalized === "balanced")
98
+ return "standard";
99
+ if (normalized.includes("opus"))
100
+ return "advanced";
101
+ if (normalized.includes("sonnet"))
102
+ return "standard";
103
+ if (normalized.includes("haiku"))
104
+ return "efficient";
105
+ return "standard";
106
+ }
107
+ /**
108
+ * Parse one Claude Code-format agent file. Returns null when the file has no
109
+ * description — the one field the tool cannot do without (the model picks
110
+ * agents by it).
111
+ */
112
+ export function parseAgentMarkdown(content, fallbackName, source) {
113
+ const { frontmatter, body } = parseFrontmatter(content);
114
+ const description = frontmatter.description?.trim();
115
+ if (!description)
116
+ return null;
117
+ const tools = frontmatter.tools
118
+ ?.split(",")
119
+ .map((t) => t.trim())
120
+ .filter(Boolean);
121
+ return {
122
+ name: (frontmatter.name?.trim() || fallbackName).toLowerCase(),
123
+ description,
124
+ tools: tools && tools.length > 0 ? tools : undefined,
125
+ model: mapModelTier(frontmatter.model),
126
+ body: body.trim() || GENERAL_BODY,
127
+ source,
128
+ };
129
+ }
130
+ function loadAgentsFromDir(dir, source) {
131
+ let entries;
132
+ try {
133
+ entries = fs.readdirSync(dir, { withFileTypes: true });
134
+ }
135
+ catch {
136
+ return [];
137
+ }
138
+ const agents = [];
139
+ for (const entry of entries) {
140
+ if (!entry.name.endsWith(".md"))
141
+ continue;
142
+ if (!entry.isFile() && !entry.isSymbolicLink())
143
+ continue;
144
+ try {
145
+ const content = fs.readFileSync(join(dir, entry.name), "utf8");
146
+ const def = parseAgentMarkdown(content, entry.name.replace(/\.md$/, ""), source);
147
+ if (def)
148
+ agents.push(def);
149
+ }
150
+ catch {
151
+ continue;
152
+ }
153
+ }
154
+ return agents;
155
+ }
156
+ /**
157
+ * All available agents: built-in general, then Claude Code plugin `agents/`
158
+ * dirs (launcher-provided), then user `~/.claude/agents`, then the repo's
159
+ * `.pi/agents` and `.claude/agents` — later wins on a name collision, so a
160
+ * project definition beats a user one beats a plugin one beats the built-in.
161
+ */
162
+ export function discoverSubagents(deps) {
163
+ const home = deps.homeDir ?? homedir();
164
+ const layers = [
165
+ [GENERAL_AGENT],
166
+ ...pluginAgentDirs(deps.env ?? process.env).map((dir) => loadAgentsFromDir(dir, "plugin")),
167
+ loadAgentsFromDir(join(home, ".claude", "agents"), "user-claude"),
168
+ loadAgentsFromDir(join(deps.cwd, ".pi", "agents"), "project-pi"),
169
+ loadAgentsFromDir(join(deps.cwd, ".claude", "agents"), "project-claude"),
170
+ ];
171
+ const byName = new Map();
172
+ for (const layer of layers) {
173
+ for (const agent of layer)
174
+ byName.set(agent.name, agent);
175
+ }
176
+ return [...byName.values()];
177
+ }
178
+ const SOURCE_LABEL = {
179
+ builtin: "built-in",
180
+ plugin: "claude plugin",
181
+ "user-claude": "~/.claude/agents",
182
+ "project-pi": ".pi/agents",
183
+ "project-claude": ".claude/agents",
184
+ };
185
+ export function formatAgentList(agents) {
186
+ return agents
187
+ .map((a) => `${a.name} (${SOURCE_LABEL[a.source]}): ${a.description}`)
188
+ .join("\n");
189
+ }
190
+ /**
191
+ * A subagent invocation is a synthetic pipeline stage: the task rides in as
192
+ * `{ticket}` verbatim, and the agent's body replaces the /go persona through
193
+ * the runner's personaBody seam.
194
+ */
195
+ export function buildSubagentStage(def, task) {
196
+ return {
197
+ stage: {
198
+ id: "implement",
199
+ agent: def.name,
200
+ model: def.model,
201
+ tools: def.tools ?? DEFAULT_SUBAGENT_TOOLS,
202
+ taskTemplate: "{ticket}",
203
+ },
204
+ ctx: { ticket: task, grounded: true },
205
+ };
206
+ }
207
+ const parameters = Type.Object({
208
+ task: Type.Optional(Type.String({
209
+ description: "The self-contained task. Include everything needed: the subagent shares none of your context.",
210
+ })),
211
+ agent: Type.Optional(Type.String({ description: "Agent name (see /agents). Defaults to general." })),
212
+ tasks: Type.Optional(Type.Array(Type.Object({
213
+ task: Type.String(),
214
+ agent: Type.Optional(Type.String()),
215
+ }), {
216
+ description: `Run several independent tasks in parallel (max ${MAX_PARALLEL_SUBAGENTS}). Use INSTEAD of task.`,
217
+ })),
218
+ });
219
+ export function makeSubagentTool(deps = {}) {
220
+ const run = deps.runStageImpl ?? runStage;
221
+ const discover = deps.discover ?? discoverSubagents;
222
+ return {
223
+ name: SUBAGENT_TOOL_NAME,
224
+ label: "Subagent",
225
+ description: "Delegate a self-contained task to a subagent with its own fresh context window, and get back " +
226
+ "a compressed report. Use it for context-heavy exploration you don't need blow-by-blow, or to " +
227
+ "run independent tasks in parallel via `tasks`. The subagent shares NONE of your conversation: " +
228
+ "spell out the task completely. Agents come from this repo's .claude/agents and .pi/agents " +
229
+ "(list them with /agents); omit `agent` for the general-purpose one.",
230
+ promptSnippet: "subagent: delegate a self-contained task (or parallel tasks) to a fresh-context agent; returns its report.",
231
+ parameters,
232
+ async execute(_toolCallId, params, signal, onUpdate, ctx) {
233
+ const fail = (text) => ({
234
+ content: [{ type: "text", text }],
235
+ details: {},
236
+ isError: true,
237
+ });
238
+ const requested = params.tasks && params.tasks.length > 0
239
+ ? params.tasks
240
+ : params.task
241
+ ? [{ task: params.task, agent: params.agent }]
242
+ : [];
243
+ if (requested.length === 0) {
244
+ return fail("Error: pass `task` (or a `tasks` array) describing what to do.");
245
+ }
246
+ if (requested.length > MAX_PARALLEL_SUBAGENTS) {
247
+ return fail(`Error: at most ${MAX_PARALLEL_SUBAGENTS} parallel tasks per call.`);
248
+ }
249
+ const cwd = ctx?.cwd ?? process.cwd();
250
+ const agents = discover({ cwd, homeDir: deps.homeDir });
251
+ const resolved = [];
252
+ for (const req of requested) {
253
+ const name = (req.agent ?? GENERAL_AGENT_NAME).toLowerCase();
254
+ const def = agents.find((a) => a.name === name);
255
+ if (!def) {
256
+ return fail(`Error: unknown agent "${name}". Available agents:\n${formatAgentList(agents)}`);
257
+ }
258
+ resolved.push({ def, task: req.task });
259
+ }
260
+ onUpdate?.({
261
+ content: [
262
+ {
263
+ type: "text",
264
+ text: resolved.length === 1
265
+ ? `Running ${resolved[0].def.name} subagent…`
266
+ : `Running ${resolved.length} subagents in parallel…`,
267
+ },
268
+ ],
269
+ details: {},
270
+ });
271
+ const outcomes = await Promise.all(resolved.map(async ({ def, task }) => {
272
+ const { stage, ctx: stageCtx } = buildSubagentStage(def, task);
273
+ const result = await run(stage, stageCtx, {
274
+ cwd,
275
+ signal,
276
+ personaBody: () => def.body,
277
+ // YAG-471: attribute this child's completions to the specific
278
+ // subagent, not the generic /go stage label the runner would
279
+ // otherwise derive from stage.id ("implement", reused as the
280
+ // synthetic subagent stage — see buildSubagentStage).
281
+ callerLabel: `${SUBAGENT_CALLER_PREFIX}${sanitizeCallerSegment(def.name, 64 - SUBAGENT_CALLER_PREFIX.length)}`,
282
+ });
283
+ return { agent: def.name, task, result };
284
+ }));
285
+ const allFailed = outcomes.every((o) => o.result.exitCode !== 0);
286
+ const sections = outcomes.map((o) => {
287
+ const output = o.result.finalOutput.trim();
288
+ const failedNote = o.result.exitCode !== 0
289
+ ? `\n\n(subagent failed, exit ${o.result.exitCode}${o.result.stderr.trim() ? `: ${o.result.stderr.trim().slice(-500)}` : ""})`
290
+ : "";
291
+ const bodyText = output || (o.result.exitCode === 0 ? "(no output)" : "");
292
+ return outcomes.length === 1
293
+ ? `${bodyText}${failedNote}`
294
+ : `## ${o.agent}: ${o.task}\n\n${bodyText}${failedNote}`;
295
+ });
296
+ return {
297
+ content: [{ type: "text", text: sections.join("\n\n") }],
298
+ details: {
299
+ tasks: outcomes.map((o) => ({
300
+ agent: o.agent,
301
+ task: o.task,
302
+ exitCode: o.result.exitCode,
303
+ usage: o.result.usage,
304
+ toolCalls: o.result.toolCalls,
305
+ })),
306
+ },
307
+ ...(allFailed ? { isError: true } : {}),
308
+ };
309
+ },
310
+ };
311
+ }
312
+ /** Wire the subagent tool and the /agents listing command. */
313
+ export function registerSubagents(pi, deps = {}) {
314
+ const discover = deps.discover ?? discoverSubagents;
315
+ pi.registerTool(makeSubagentTool(deps));
316
+ pi.registerCommand("agents", {
317
+ description: "List the subagents available in this repo (.claude/agents, .pi/agents).",
318
+ handler: async (_args, ctx) => {
319
+ if (!ctx.hasUI)
320
+ return;
321
+ const agents = discover({ cwd: ctx.cwd ?? process.cwd(), homeDir: deps.homeDir });
322
+ ctx.ui.notify(`Available subagents:\n${formatAgentList(agents)}`, "info");
323
+ },
324
+ });
325
+ }
326
+ //# sourceMappingURL=subagents.js.map
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Which surface this process is painting for. The desktop driver sets
3
+ * YAGNI_SURFACE=desktop; the terminal CLI sets nothing. The two renderers are
4
+ * mutually exclusive: a desktop session never receives ANSI, which is why the
5
+ * escape-code leak is fixed by construction rather than stripped downstream.
6
+ * Desktop-facing widgets are structured single-line JSON records the app
7
+ * parses, not themed terminal lines.
8
+ */
9
+ export declare function isDesktopSurface(): boolean;
10
+ //# sourceMappingURL=surface.d.ts.map
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Which surface this process is painting for. The desktop driver sets
3
+ * YAGNI_SURFACE=desktop; the terminal CLI sets nothing. The two renderers are
4
+ * mutually exclusive: a desktop session never receives ANSI, which is why the
5
+ * escape-code leak is fixed by construction rather than stripped downstream.
6
+ * Desktop-facing widgets are structured single-line JSON records the app
7
+ * parses, not themed terminal lines.
8
+ */
9
+ export function isDesktopSurface() {
10
+ return process.env.YAGNI_SURFACE === "desktop";
11
+ }
12
+ //# sourceMappingURL=surface.js.map
@@ -0,0 +1,110 @@
1
+ /**
2
+ * The session todo list — the visible checklist for multi-step work.
3
+ *
4
+ * A `todo_write` tool the model calls with the FULL list every time (replace,
5
+ * not patch: replacement is idempotent under retries and always renders a
6
+ * coherent board), a persistent above-editor widget while steps remain open,
7
+ * and a `/todos` command to pull the list on demand.
8
+ *
9
+ * State follows pi's branching model the same way the session does: the
10
+ * canonical list is the LAST `todo_write` tool result on the current branch,
11
+ * reconstructed on session_start/session_tree, so forking or rewinding a
12
+ * session automatically shows the list as it stood at that point. The
13
+ * in-memory copy is just a cache of that.
14
+ */
15
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
16
+ import { Type } from "typebox";
17
+ export declare const TODO_TOOL_NAME = "todo_write";
18
+ export declare const MAX_TODOS = 50;
19
+ export declare const MAX_TODO_TEXT = 300;
20
+ /**
21
+ * The desktop's structured state record rides its own widget key, like the
22
+ * `/go` run state: one JSON line the app parses and renders itself, never
23
+ * themed terminal lines. The desktop shell hand-declares the matching parser
24
+ * (`parseTodoState` in its piProtocol.ts); a key-set assertion in
25
+ * todos.test.ts guards the two against drift.
26
+ */
27
+ export declare const TODO_STATE_KEY = "yagni-todos:state";
28
+ export type TodoStatus = "pending" | "in_progress" | "completed";
29
+ export interface TodoItem {
30
+ text: string;
31
+ status: TodoStatus;
32
+ }
33
+ /**
34
+ * Validate a full replacement list. Strict: this is model input rendered
35
+ * straight into the terminal. An empty list is valid (it clears the board).
36
+ */
37
+ export declare function normalizeTodos(raw: unknown): {
38
+ ok: true;
39
+ todos: TodoItem[];
40
+ } | {
41
+ ok: false;
42
+ error: string;
43
+ };
44
+ export declare function todoSummary(todos: TodoItem[]): {
45
+ done: number;
46
+ total: number;
47
+ active: string | null;
48
+ };
49
+ /** Plain-text checklist (tool results, /todos in headless contexts). */
50
+ export declare function formatTodoList(todos: TodoItem[]): string;
51
+ /** The slice of pi's Theme the widget styles with (matches the feed's pattern). */
52
+ export interface TodoTheme {
53
+ fg(color: string, s: string): string;
54
+ strikethrough?(s: string): string;
55
+ }
56
+ /**
57
+ * The above-editor checklist. Empty (paint nothing) when there is no list or
58
+ * every step is completed — a finished board should leave the screen. Capped
59
+ * so header + items + overflow stays under pi's 10-line widget truncation.
60
+ */
61
+ export declare function renderTodoWidget(todos: TodoItem[], theme: TodoTheme): string[];
62
+ /** The desktop state record: exactly one JSON line under TODO_STATE_KEY. */
63
+ export declare function todoStateLine(todos: TodoItem[]): string;
64
+ /** Replay the branch: the last todo_write result is the canonical list. */
65
+ export declare function reconstructTodos(entries: unknown[]): TodoItem[];
66
+ type TodoParams = {
67
+ todos: Array<{
68
+ text: string;
69
+ status: string;
70
+ }>;
71
+ };
72
+ /**
73
+ * Build the todo_write tool definition around a shared store. Separated from
74
+ * registration so tests can drive execute directly.
75
+ */
76
+ export declare function makeTodoTool(get: () => TodoItem[], set: (todos: TodoItem[]) => void): {
77
+ name: string;
78
+ label: string;
79
+ description: string;
80
+ promptSnippet: string;
81
+ parameters: Type.TObject<{
82
+ todos: Type.TArray<Type.TObject<{
83
+ text: Type.TString;
84
+ status: Type.TUnion<[Type.TLiteral<"pending">, Type.TLiteral<"in_progress">, Type.TLiteral<"completed">]>;
85
+ }>>;
86
+ }>;
87
+ execute(_toolCallId: string, params: TodoParams, _signal?: AbortSignal, _onUpdate?: unknown, ctx?: ExtensionContext): Promise<{
88
+ content: {
89
+ type: "text";
90
+ text: string;
91
+ }[];
92
+ details: {
93
+ todos: TodoItem[];
94
+ };
95
+ isError: boolean;
96
+ } | {
97
+ content: {
98
+ type: "text";
99
+ text: string;
100
+ }[];
101
+ details: {
102
+ todos: TodoItem[];
103
+ };
104
+ isError?: undefined;
105
+ }>;
106
+ };
107
+ /** Wire the tool, the branch-replay events, and the /todos command. */
108
+ export declare function registerTodos(pi: ExtensionAPI): void;
109
+ export {};
110
+ //# sourceMappingURL=todos.d.ts.map
@@ -0,0 +1,217 @@
1
+ /**
2
+ * The session todo list — the visible checklist for multi-step work.
3
+ *
4
+ * A `todo_write` tool the model calls with the FULL list every time (replace,
5
+ * not patch: replacement is idempotent under retries and always renders a
6
+ * coherent board), a persistent above-editor widget while steps remain open,
7
+ * and a `/todos` command to pull the list on demand.
8
+ *
9
+ * State follows pi's branching model the same way the session does: the
10
+ * canonical list is the LAST `todo_write` tool result on the current branch,
11
+ * reconstructed on session_start/session_tree, so forking or rewinding a
12
+ * session automatically shows the list as it stood at that point. The
13
+ * in-memory copy is just a cache of that.
14
+ */
15
+ import { Type } from "typebox";
16
+ import { isDesktopSurface } from "./surface.js";
17
+ export const TODO_TOOL_NAME = "todo_write";
18
+ export const MAX_TODOS = 50;
19
+ export const MAX_TODO_TEXT = 300;
20
+ const WIDGET_KEY = "yagni-todos";
21
+ /**
22
+ * The desktop's structured state record rides its own widget key, like the
23
+ * `/go` run state: one JSON line the app parses and renders itself, never
24
+ * themed terminal lines. The desktop shell hand-declares the matching parser
25
+ * (`parseTodoState` in its piProtocol.ts); a key-set assertion in
26
+ * todos.test.ts guards the two against drift.
27
+ */
28
+ export const TODO_STATE_KEY = "yagni-todos:state";
29
+ /** Header + items + overflow line must stay under pi's 10-line widget cap. */
30
+ const MAX_WIDGET_ITEMS = 7;
31
+ const STATUSES = ["pending", "in_progress", "completed"];
32
+ /**
33
+ * Validate a full replacement list. Strict: this is model input rendered
34
+ * straight into the terminal. An empty list is valid (it clears the board).
35
+ */
36
+ export function normalizeTodos(raw) {
37
+ if (!Array.isArray(raw))
38
+ return { ok: false, error: "todos must be an array" };
39
+ if (raw.length > MAX_TODOS)
40
+ return { ok: false, error: `todos is capped at ${MAX_TODOS} items` };
41
+ const todos = [];
42
+ for (const item of raw) {
43
+ if (!item || typeof item !== "object")
44
+ return { ok: false, error: "each todo must be an object" };
45
+ const { text, status } = item;
46
+ if (typeof text !== "string" || text.trim().length === 0) {
47
+ return { ok: false, error: "each todo needs non-empty text" };
48
+ }
49
+ if (typeof status !== "string" || !STATUSES.includes(status)) {
50
+ return { ok: false, error: `status must be one of ${STATUSES.join(", ")}` };
51
+ }
52
+ todos.push({ text: text.trim().slice(0, MAX_TODO_TEXT), status: status });
53
+ }
54
+ return { ok: true, todos };
55
+ }
56
+ export function todoSummary(todos) {
57
+ return {
58
+ done: todos.filter((t) => t.status === "completed").length,
59
+ total: todos.length,
60
+ active: todos.find((t) => t.status === "in_progress")?.text ?? null,
61
+ };
62
+ }
63
+ const GLYPH = { pending: "[ ]", in_progress: "[>]", completed: "[x]" };
64
+ /** Plain-text checklist (tool results, /todos in headless contexts). */
65
+ export function formatTodoList(todos) {
66
+ if (todos.length === 0)
67
+ return "No todos.";
68
+ const { done, total } = todoSummary(todos);
69
+ const lines = todos.map((t) => `${GLYPH[t.status]} ${t.text}`);
70
+ return [`${done}/${total} done`, ...lines].join("\n");
71
+ }
72
+ /**
73
+ * The above-editor checklist. Empty (paint nothing) when there is no list or
74
+ * every step is completed — a finished board should leave the screen. Capped
75
+ * so header + items + overflow stays under pi's 10-line widget truncation.
76
+ */
77
+ export function renderTodoWidget(todos, theme) {
78
+ const { done, total } = todoSummary(todos);
79
+ if (total === 0 || done === total)
80
+ return [];
81
+ const lines = [theme.fg("muted", `Todos ${done}/${total}`)];
82
+ const visible = todos.slice(0, MAX_WIDGET_ITEMS);
83
+ for (const todo of visible) {
84
+ if (todo.status === "completed") {
85
+ const text = theme.strikethrough ? theme.strikethrough(todo.text) : todo.text;
86
+ lines.push(`${theme.fg("success", "☑ ")}${theme.fg("dim", text)}`);
87
+ }
88
+ else if (todo.status === "in_progress") {
89
+ lines.push(`${theme.fg("accent", "▶ ")}${theme.fg("text", todo.text)}`);
90
+ }
91
+ else {
92
+ lines.push(`${theme.fg("dim", "☐ ")}${theme.fg("muted", todo.text)}`);
93
+ }
94
+ }
95
+ if (todos.length > visible.length) {
96
+ lines.push(theme.fg("dim", `…and ${todos.length - visible.length} more`));
97
+ }
98
+ return lines;
99
+ }
100
+ /** The desktop state record: exactly one JSON line under TODO_STATE_KEY. */
101
+ export function todoStateLine(todos) {
102
+ return JSON.stringify({ v: 1, todos });
103
+ }
104
+ /** Replay the branch: the last todo_write result is the canonical list. */
105
+ export function reconstructTodos(entries) {
106
+ let todos = [];
107
+ for (const entry of entries) {
108
+ const e = entry;
109
+ if (e?.type !== "message")
110
+ continue;
111
+ const msg = e.message;
112
+ if (msg?.role !== "toolResult" || msg.toolName !== TODO_TOOL_NAME)
113
+ continue;
114
+ const details = msg.details;
115
+ const normalized = normalizeTodos(details?.todos);
116
+ if (normalized.ok)
117
+ todos = normalized.todos;
118
+ }
119
+ return todos;
120
+ }
121
+ const parameters = Type.Object({
122
+ todos: Type.Array(Type.Object({
123
+ text: Type.String({ description: "The step, short and imperative." }),
124
+ status: Type.Union([
125
+ Type.Literal("pending"),
126
+ Type.Literal("in_progress"),
127
+ Type.Literal("completed"),
128
+ ]),
129
+ }), { description: "The FULL list; it replaces the previous one." }),
130
+ });
131
+ function paintWidget(ctx, todos) {
132
+ if (!ctx?.hasUI)
133
+ return;
134
+ try {
135
+ if (isDesktopSurface()) {
136
+ // The desktop renders the checklist itself, so it gets the structured
137
+ // record instead of themed lines (RPC mode forwards widgetLines
138
+ // verbatim, and ANSI would land in the DOM). Painted on EVERY change —
139
+ // including an empty or finished list — so the app can clear its board,
140
+ // and on branch replay so resume/fork restores the list. The "todos
141
+ // n/m" status chip would be redundant next to the app's own rendering.
142
+ ctx.ui.setWidget?.(TODO_STATE_KEY, [todoStateLine(todos)], { placement: "aboveEditor" });
143
+ return;
144
+ }
145
+ const theme = ctx.ui.theme;
146
+ const lines = renderTodoWidget(todos, theme);
147
+ ctx.ui.setWidget?.(WIDGET_KEY, lines.length > 0 ? lines : undefined, {
148
+ placement: "aboveEditor",
149
+ });
150
+ const { done, total } = todoSummary(todos);
151
+ ctx.ui.setStatus?.(WIDGET_KEY, total > 0 && done < total ? `todos ${done}/${total}` : undefined);
152
+ }
153
+ catch {
154
+ // Painting is chrome; it must never break the tool call.
155
+ }
156
+ }
157
+ /**
158
+ * Build the todo_write tool definition around a shared store. Separated from
159
+ * registration so tests can drive execute directly.
160
+ */
161
+ export function makeTodoTool(get, set) {
162
+ return {
163
+ name: TODO_TOOL_NAME,
164
+ label: "Todos",
165
+ description: "Track your plan for multi-step work as a checklist the user can see. " +
166
+ "Pass the FULL list every time — it replaces the previous list. Keep exactly one item " +
167
+ "in_progress at a time, mark items completed the moment they are done, and add newly " +
168
+ "discovered steps as pending. Use it for any task with three or more steps, updating as you go.",
169
+ promptSnippet: "todo_write: keep a user-visible checklist for multi-step work (full-list replacement).",
170
+ parameters,
171
+ async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
172
+ const normalized = normalizeTodos(params.todos);
173
+ if (!normalized.ok) {
174
+ return {
175
+ content: [{ type: "text", text: `Error: ${normalized.error}` }],
176
+ details: { todos: get() },
177
+ isError: true,
178
+ };
179
+ }
180
+ set(normalized.todos);
181
+ paintWidget(ctx, normalized.todos);
182
+ return {
183
+ content: [{ type: "text", text: formatTodoList(normalized.todos) }],
184
+ details: { todos: normalized.todos },
185
+ };
186
+ },
187
+ };
188
+ }
189
+ /** Wire the tool, the branch-replay events, and the /todos command. */
190
+ export function registerTodos(pi) {
191
+ let todos = [];
192
+ const reconstruct = (ctx) => {
193
+ try {
194
+ todos = reconstructTodos(ctx.sessionManager.getBranch());
195
+ }
196
+ catch {
197
+ todos = [];
198
+ }
199
+ paintWidget(ctx, todos);
200
+ };
201
+ pi.on("session_start", async (_event, ctx) => reconstruct(ctx));
202
+ pi.on("session_tree", async (_event, ctx) => reconstruct(ctx));
203
+ pi.registerTool(makeTodoTool(() => todos, (next) => {
204
+ todos = next;
205
+ }));
206
+ pi.registerCommand("todos", {
207
+ description: "Show the agent's current task list for this session.",
208
+ handler: async (_args, ctx) => {
209
+ if (!ctx.hasUI)
210
+ return;
211
+ ctx.ui.notify(todos.length === 0
212
+ ? "No todos. The agent keeps a checklist here when a task takes multiple steps."
213
+ : formatTodoList(todos), "info");
214
+ },
215
+ });
216
+ }
217
+ //# sourceMappingURL=todos.js.map