@clanker-code/pi-subagents 0.10.5

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 (130) hide show
  1. package/.plans/PLAN-next-changes.md +183 -0
  2. package/.plans/README.md +14 -0
  3. package/AGENTS.md +31 -0
  4. package/CHANGELOG.md +583 -0
  5. package/CLAUDE.md +1 -0
  6. package/LICENSE +21 -0
  7. package/README.md +630 -0
  8. package/RELEASE.md +39 -0
  9. package/dist/abort-resend.d.ts +35 -0
  10. package/dist/abort-resend.js +71 -0
  11. package/dist/agent-details.d.ts +17 -0
  12. package/dist/agent-details.js +22 -0
  13. package/dist/agent-manager.d.ts +132 -0
  14. package/dist/agent-manager.js +493 -0
  15. package/dist/agent-runner.d.ts +165 -0
  16. package/dist/agent-runner.js +732 -0
  17. package/dist/agent-tool-description.d.ts +9 -0
  18. package/dist/agent-tool-description.js +147 -0
  19. package/dist/agent-types.d.ts +60 -0
  20. package/dist/agent-types.js +157 -0
  21. package/dist/context.d.ts +12 -0
  22. package/dist/context.js +56 -0
  23. package/dist/cross-extension-rpc.d.ts +46 -0
  24. package/dist/cross-extension-rpc.js +76 -0
  25. package/dist/custom-agents.d.ts +14 -0
  26. package/dist/custom-agents.js +149 -0
  27. package/dist/default-agents.d.ts +7 -0
  28. package/dist/default-agents.js +119 -0
  29. package/dist/enabled-models.d.ts +49 -0
  30. package/dist/enabled-models.js +145 -0
  31. package/dist/env.d.ts +6 -0
  32. package/dist/env.js +28 -0
  33. package/dist/group-join.d.ts +32 -0
  34. package/dist/group-join.js +116 -0
  35. package/dist/index.d.ts +36 -0
  36. package/dist/index.js +1918 -0
  37. package/dist/invocation-config.d.ts +25 -0
  38. package/dist/invocation-config.js +19 -0
  39. package/dist/memory.d.ts +49 -0
  40. package/dist/memory.js +151 -0
  41. package/dist/model-resolver.d.ts +19 -0
  42. package/dist/model-resolver.js +62 -0
  43. package/dist/notifications.d.ts +6 -0
  44. package/dist/notifications.js +107 -0
  45. package/dist/output-file.d.ts +24 -0
  46. package/dist/output-file.js +86 -0
  47. package/dist/peek.d.ts +37 -0
  48. package/dist/peek.js +121 -0
  49. package/dist/prompts.d.ts +40 -0
  50. package/dist/prompts.js +95 -0
  51. package/dist/schedule-store.d.ts +38 -0
  52. package/dist/schedule-store.js +155 -0
  53. package/dist/schedule.d.ts +109 -0
  54. package/dist/schedule.js +338 -0
  55. package/dist/settings.d.ts +135 -0
  56. package/dist/settings.js +168 -0
  57. package/dist/skill-loader.d.ts +24 -0
  58. package/dist/skill-loader.js +93 -0
  59. package/dist/status-note.d.ts +13 -0
  60. package/dist/status-note.js +24 -0
  61. package/dist/types.d.ts +184 -0
  62. package/dist/types.js +7 -0
  63. package/dist/ui/agent-tool-rendering.d.ts +34 -0
  64. package/dist/ui/agent-tool-rendering.js +154 -0
  65. package/dist/ui/agent-widget-tree.d.ts +33 -0
  66. package/dist/ui/agent-widget-tree.js +130 -0
  67. package/dist/ui/agent-widget.d.ts +156 -0
  68. package/dist/ui/agent-widget.js +408 -0
  69. package/dist/ui/conversation-viewer.d.ts +47 -0
  70. package/dist/ui/conversation-viewer.js +290 -0
  71. package/dist/ui/menu-select.d.ts +20 -0
  72. package/dist/ui/menu-select.js +46 -0
  73. package/dist/ui/schedule-menu.d.ts +16 -0
  74. package/dist/ui/schedule-menu.js +99 -0
  75. package/dist/ui/viewer-keys.d.ts +20 -0
  76. package/dist/ui/viewer-keys.js +17 -0
  77. package/dist/usage.d.ts +50 -0
  78. package/dist/usage.js +49 -0
  79. package/dist/wait.d.ts +10 -0
  80. package/dist/wait.js +37 -0
  81. package/dist/worktree.d.ts +45 -0
  82. package/dist/worktree.js +160 -0
  83. package/docs/design/default-extension-tool-exposure.md +56 -0
  84. package/docs/superpowers/plans/2026-06-19-recursive-subagent-widget.md +600 -0
  85. package/docs/superpowers/specs/2026-06-19-recursive-subagent-widget-design.md +189 -0
  86. package/examples/agent-tool-description.md +45 -0
  87. package/package.json +56 -0
  88. package/reviews/proposal-structured-output-schema.md +135 -0
  89. package/reviews/recursive-subagent-widget-preview-rev2.png +0 -0
  90. package/reviews/recursive-subagent-widget-preview.html +137 -0
  91. package/reviews/recursive-subagent-widget-preview.png +0 -0
  92. package/reviews/subagent-features-comparison.md +350 -0
  93. package/src/abort-resend.ts +75 -0
  94. package/src/agent-details.ts +31 -0
  95. package/src/agent-manager.ts +596 -0
  96. package/src/agent-runner.ts +872 -0
  97. package/src/agent-tool-description.ts +163 -0
  98. package/src/agent-types.ts +189 -0
  99. package/src/context.ts +58 -0
  100. package/src/cross-extension-rpc.ts +122 -0
  101. package/src/custom-agents.ts +160 -0
  102. package/src/default-agents.ts +123 -0
  103. package/src/enabled-models.ts +180 -0
  104. package/src/env.ts +33 -0
  105. package/src/group-join.ts +141 -0
  106. package/src/index.ts +2115 -0
  107. package/src/invocation-config.ts +42 -0
  108. package/src/memory.ts +165 -0
  109. package/src/model-resolver.ts +81 -0
  110. package/src/notifications.ts +120 -0
  111. package/src/output-file.ts +96 -0
  112. package/src/peek.ts +155 -0
  113. package/src/prompts.ts +129 -0
  114. package/src/schedule-store.ts +153 -0
  115. package/src/schedule.ts +365 -0
  116. package/src/settings.ts +289 -0
  117. package/src/skill-loader.ts +102 -0
  118. package/src/status-note.ts +25 -0
  119. package/src/types.ts +195 -0
  120. package/src/ui/agent-tool-rendering.ts +175 -0
  121. package/src/ui/agent-widget-tree.ts +169 -0
  122. package/src/ui/agent-widget.ts +497 -0
  123. package/src/ui/conversation-viewer.ts +297 -0
  124. package/src/ui/menu-select.ts +68 -0
  125. package/src/ui/schedule-menu.ts +105 -0
  126. package/src/ui/viewer-keys.ts +39 -0
  127. package/src/usage.ts +60 -0
  128. package/src/wait.ts +44 -0
  129. package/src/worktree.ts +191 -0
  130. package/vitest.config.ts +25 -0
@@ -0,0 +1,289 @@
1
+ // Persistence for pi-subagents operational settings.
2
+ // - Global: ~/.pi/agent/subagents.json (via getAgentDir()) — manual defaults, never written here
3
+ // - Project: <cwd>/.pi/subagents.json — written by /agents → Settings; overrides global on load
4
+
5
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
6
+ import { dirname, join } from "node:path";
7
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
8
+ import type { JoinMode } from "./types.js";
9
+ import type { WidgetDisplayMode } from "./ui/agent-widget-tree.js";
10
+
11
+ export interface SubagentsSettings {
12
+ maxConcurrent?: number;
13
+ /**
14
+ * 0 = unlimited — the extension's single source of truth for that convention:
15
+ * `normalizeMaxTurns()` in agent-runner.ts treats 0 → `undefined`, and the
16
+ * `/agents` → Settings input prompt explicitly says "0 = unlimited".
17
+ */
18
+ defaultMaxTurns?: number;
19
+ graceTurns?: number;
20
+ defaultJoinMode?: JoinMode;
21
+ /**
22
+ * Master switch for the schedule subagent feature. Defaults to `true`.
23
+ * When `false`: the `Agent` tool's `schedule` param + its guideline are
24
+ * stripped from the tool spec at registration (zero LLM-context cost), the
25
+ * scheduler doesn't bind to the session, and the `/agents → Scheduled jobs`
26
+ * menu entry is hidden. Schema-level removal applies at extension load
27
+ * (next pi session); runtime menu/runtime-fire short-circuit is immediate.
28
+ */
29
+ schedulingEnabled?: boolean;
30
+ /**
31
+ * When true, the effective model of each subagent spawn is validated
32
+ * against `enabledModels` from pi's settings — both global
33
+ * (`<agentDir>/settings.json`) and project-local (`<cwd>/.pi/settings.json`),
34
+ * with project overriding global (mirrors pi's SettingsManager deep-merge).
35
+ *
36
+ * scopeModels guards against runtime LLM choices, not user-level config.
37
+ * Out-of-scope handling reflects this:
38
+ * - Caller-supplied via `Agent({ model: "..." })` (only when frontmatter
39
+ * has no `model:`, since frontmatter is authoritative): hard error
40
+ * returned to the orchestrator, listing the allowed models. The LLM
41
+ * made an explicit out-of-scope choice and gets explicit feedback.
42
+ * - Frontmatter-pinned: warning toast + the pinned model runs. The
43
+ * agent's author/installer chose this; trust it.
44
+ * - Parent-inherited (neither caller nor frontmatter sets a model):
45
+ * warning toast + parent's model runs. The user chose the parent's
46
+ * model when starting the session; trust it.
47
+ *
48
+ * No-op when pi's `enabledModels` is empty or absent — nothing to validate
49
+ * against. Defaults to false: subagents may use any model.
50
+ */
51
+ scopeModels?: boolean;
52
+ /**
53
+ * When true, the three built-in default agents (general-purpose, Explore, Plan)
54
+ * are not registered at startup. User-defined agents from .pi/agents/*.md are
55
+ * completely unaffected — only the hardcoded DEFAULT_AGENTS are suppressed.
56
+ * Defaults to false.
57
+ */
58
+ disableDefaultAgents?: boolean;
59
+ /**
60
+ * Which Agent tool description the LLM sees. "full" (default) is the rich
61
+ * Claude Code-style prompt; "compact" is a ~75% smaller version (one-line
62
+ * agent type list, terse usage notes) for small/local models where tool-spec
63
+ * tokens are expensive; "custom" reads `.pi/agent-tool-description.md`
64
+ * (project, falling back to `<agentDir>/agent-tool-description.md`) with
65
+ * `{{placeholder}}` substitution — a missing/empty file falls back to "full".
66
+ * The mode is read once at tool registration — changing it applies on the
67
+ * next pi session.
68
+ */
69
+ toolDescriptionMode?: ToolDescriptionMode;
70
+ /**
71
+ * How long (seconds) `get_subagent_result wait:true` blocks before returning
72
+ * the agent's current status instead of its result. Bounds the parent turn so
73
+ * a long-running subagent can't wedge it indefinitely; the caller re-invokes
74
+ * to keep waiting. Default 270 (4m30s) to stay under the typical 5-minute LLM
75
+ * prompt-cache window. Range 30–3600.
76
+ */
77
+ waitTimeoutSeconds?: number;
78
+ /**
79
+ * The keyboard shortcut that aborts the current turn AND auto-sends queued
80
+ * message(s) as the next turn (instead of Escape, which dumps the queue back
81
+ * into the editor for manual re-submit). Default "f9" — a distinct key on
82
+ * every terminal. Override with any KeyId (e.g. "shift+escape", "f8").
83
+ * Read at session start; a change applies on the next pi session.
84
+ * The PI_ABORT_RESEND_KEY env var, if set, takes precedence over this.
85
+ */
86
+ abortResendKey?: string;
87
+ /** How the live subagent widget renders recursive trees. Defaults to auto. */
88
+ widgetDisplayMode?: WidgetDisplayMode;
89
+ }
90
+
91
+ export type ToolDescriptionMode = "full" | "compact" | "custom";
92
+
93
+ /** Default wait timeout for `get_subagent_result wait:true` (4.5 minutes). */
94
+ export const DEFAULT_WAIT_TIMEOUT_SECONDS = 270;
95
+
96
+ /** Setter hooks used by applySettings to wire persisted values into in-memory state. */
97
+ export interface SettingsAppliers {
98
+ setMaxConcurrent: (n: number) => void;
99
+ setDefaultMaxTurns: (n: number) => void;
100
+ setGraceTurns: (n: number) => void;
101
+ setDefaultJoinMode: (mode: JoinMode) => void;
102
+ setSchedulingEnabled: (b: boolean) => void;
103
+ setScopeModels: (enabled: boolean) => void;
104
+ setDisableDefaultAgents: (b: boolean) => void;
105
+ setToolDescriptionMode: (mode: ToolDescriptionMode) => void;
106
+ setWaitTimeoutSeconds: (seconds: number) => void;
107
+ setAbortResendKey: (key: string) => void;
108
+ setWidgetDisplayMode: (mode: WidgetDisplayMode) => void;
109
+ }
110
+
111
+ /** Emit callback — a subset of `pi.events.emit` to keep helpers testable. */
112
+ export type SettingsEmit = (event: string, payload: unknown) => void;
113
+
114
+ const VALID_JOIN_MODES: ReadonlySet<string> = new Set<JoinMode>(["async", "group", "smart"]);
115
+ const VALID_TOOL_DESCRIPTION_MODES: ReadonlySet<string> = new Set<ToolDescriptionMode>(["full", "compact", "custom"]);
116
+ const VALID_WIDGET_DISPLAY_MODES: ReadonlySet<string> = new Set<WidgetDisplayMode>(["auto", "rich", "compact"]);
117
+
118
+ // Sanity ceilings — prevent hand-edited configs from asking for values that
119
+ // make no operational sense (e.g. 1e6 concurrent subagents). Permissive enough
120
+ // that any realistic power-user setting passes through.
121
+ const MAX_CONCURRENT_CEILING = 1024;
122
+ const MAX_TURNS_CEILING = 10_000;
123
+ const GRACE_TURNS_CEILING = 1_000;
124
+ const WAIT_TIMEOUT_MIN = 30;
125
+ const WAIT_TIMEOUT_MAX = 3600;
126
+
127
+ /** Drop fields that don't match the expected shape. Silent — garbage becomes absent. */
128
+ function sanitize(raw: unknown): SubagentsSettings {
129
+ if (!raw || typeof raw !== "object") return {};
130
+ const r = raw as Record<string, unknown>;
131
+ const out: SubagentsSettings = {};
132
+ if (
133
+ Number.isInteger(r.maxConcurrent) &&
134
+ (r.maxConcurrent as number) >= 1 &&
135
+ (r.maxConcurrent as number) <= MAX_CONCURRENT_CEILING
136
+ ) {
137
+ out.maxConcurrent = r.maxConcurrent as number;
138
+ }
139
+ if (
140
+ Number.isInteger(r.defaultMaxTurns) &&
141
+ (r.defaultMaxTurns as number) >= 0 &&
142
+ (r.defaultMaxTurns as number) <= MAX_TURNS_CEILING
143
+ ) {
144
+ out.defaultMaxTurns = r.defaultMaxTurns as number;
145
+ }
146
+ if (
147
+ Number.isInteger(r.graceTurns) &&
148
+ (r.graceTurns as number) >= 1 &&
149
+ (r.graceTurns as number) <= GRACE_TURNS_CEILING
150
+ ) {
151
+ out.graceTurns = r.graceTurns as number;
152
+ }
153
+ if (typeof r.defaultJoinMode === "string" && VALID_JOIN_MODES.has(r.defaultJoinMode)) {
154
+ out.defaultJoinMode = r.defaultJoinMode as JoinMode;
155
+ }
156
+ if (typeof r.schedulingEnabled === "boolean") {
157
+ out.schedulingEnabled = r.schedulingEnabled;
158
+ }
159
+ if (typeof r.scopeModels === "boolean") {
160
+ out.scopeModels = r.scopeModels;
161
+ }
162
+ if (typeof r.disableDefaultAgents === "boolean") {
163
+ out.disableDefaultAgents = r.disableDefaultAgents;
164
+ }
165
+ if (typeof r.toolDescriptionMode === "string" && VALID_TOOL_DESCRIPTION_MODES.has(r.toolDescriptionMode)) {
166
+ out.toolDescriptionMode = r.toolDescriptionMode as ToolDescriptionMode;
167
+ }
168
+ if (
169
+ Number.isInteger(r.waitTimeoutSeconds) &&
170
+ (r.waitTimeoutSeconds as number) >= WAIT_TIMEOUT_MIN &&
171
+ (r.waitTimeoutSeconds as number) <= WAIT_TIMEOUT_MAX
172
+ ) {
173
+ out.waitTimeoutSeconds = r.waitTimeoutSeconds as number;
174
+ }
175
+ if (typeof r.abortResendKey === "string" && r.abortResendKey.trim() !== "") {
176
+ out.abortResendKey = (r.abortResendKey as string).trim();
177
+ }
178
+ if (typeof r.widgetDisplayMode === "string" && VALID_WIDGET_DISPLAY_MODES.has(r.widgetDisplayMode)) {
179
+ out.widgetDisplayMode = r.widgetDisplayMode as WidgetDisplayMode;
180
+ }
181
+ return out;
182
+ }
183
+
184
+ function globalPath(): string {
185
+ return join(getAgentDir(), "subagents.json");
186
+ }
187
+
188
+ function projectPath(cwd: string): string {
189
+ return join(cwd, ".pi", "subagents.json");
190
+ }
191
+
192
+ /**
193
+ * Read a settings file. Missing file is silent (returns `{}`). A file that
194
+ * exists but can't be parsed emits a warning to stderr so users aren't
195
+ * silently reverted to defaults — and still returns `{}` so startup proceeds.
196
+ */
197
+ function readSettingsFile(path: string): SubagentsSettings {
198
+ if (!existsSync(path)) return {};
199
+ try {
200
+ return sanitize(JSON.parse(readFileSync(path, "utf-8")));
201
+ } catch (err) {
202
+ const reason = err instanceof Error ? err.message : String(err);
203
+ console.warn(`[pi-subagents] Ignoring malformed settings at ${path}: ${reason}`);
204
+ return {};
205
+ }
206
+ }
207
+
208
+ /** Load merged settings: global provides defaults, project overrides. */
209
+ export function loadSettings(cwd: string = process.cwd()): SubagentsSettings {
210
+ return { ...readSettingsFile(globalPath()), ...readSettingsFile(projectPath(cwd)) };
211
+ }
212
+
213
+ /**
214
+ * Write project-local settings. Global is never touched from code.
215
+ * Returns `true` on success, `false` if the write (or mkdir) failed so the
216
+ * caller can surface a warning — persistence isn't fatal but isn't silent.
217
+ */
218
+ export function saveSettings(s: SubagentsSettings, cwd: string = process.cwd()): boolean {
219
+ const path = projectPath(cwd);
220
+ try {
221
+ mkdirSync(dirname(path), { recursive: true });
222
+ writeFileSync(path, JSON.stringify(s, null, 2), "utf-8");
223
+ return true;
224
+ } catch {
225
+ return false;
226
+ }
227
+ }
228
+
229
+ /** Apply persisted settings to the in-memory state via caller-supplied setters. */
230
+ export function applySettings(s: SubagentsSettings, appliers: SettingsAppliers): void {
231
+ if (typeof s.maxConcurrent === "number") appliers.setMaxConcurrent(s.maxConcurrent);
232
+ if (typeof s.defaultMaxTurns === "number") appliers.setDefaultMaxTurns(s.defaultMaxTurns);
233
+ if (typeof s.graceTurns === "number") appliers.setGraceTurns(s.graceTurns);
234
+ if (s.defaultJoinMode) appliers.setDefaultJoinMode(s.defaultJoinMode);
235
+ if (typeof s.schedulingEnabled === "boolean") appliers.setSchedulingEnabled(s.schedulingEnabled);
236
+ if (typeof s.scopeModels === "boolean") appliers.setScopeModels(s.scopeModels);
237
+ if (typeof s.disableDefaultAgents === "boolean") appliers.setDisableDefaultAgents(s.disableDefaultAgents);
238
+ if (s.toolDescriptionMode) appliers.setToolDescriptionMode(s.toolDescriptionMode);
239
+ if (typeof s.waitTimeoutSeconds === "number") appliers.setWaitTimeoutSeconds(s.waitTimeoutSeconds);
240
+ if (typeof s.abortResendKey === "string") appliers.setAbortResendKey(s.abortResendKey);
241
+ if (s.widgetDisplayMode) appliers.setWidgetDisplayMode(s.widgetDisplayMode);
242
+ }
243
+
244
+ /**
245
+ * Format the user-facing toast for a settings mutation. Pure function —
246
+ * routes the success/failure of `saveSettings` into the right message + level
247
+ * so the UI layer (index.ts) stays a thin wire between input and notification.
248
+ */
249
+ export function persistToastFor(
250
+ successMsg: string,
251
+ persisted: boolean,
252
+ ): { message: string; level: "info" | "warning" } {
253
+ return persisted
254
+ ? { message: successMsg, level: "info" }
255
+ : { message: `${successMsg} (session only; failed to persist)`, level: "warning" };
256
+ }
257
+
258
+ /**
259
+ * Load merged settings, apply them to in-memory state, and emit the
260
+ * `subagents:settings_loaded` lifecycle event. Returns the loaded settings so
261
+ * callers can log/inspect. Extension init wires this once.
262
+ */
263
+ export function applyAndEmitLoaded(
264
+ appliers: SettingsAppliers,
265
+ emit: SettingsEmit,
266
+ cwd: string = process.cwd(),
267
+ ): SubagentsSettings {
268
+ const settings = loadSettings(cwd);
269
+ applySettings(settings, appliers);
270
+ emit("subagents:settings_loaded", { settings });
271
+ return settings;
272
+ }
273
+
274
+ /**
275
+ * Persist a settings snapshot, emit the `subagents:settings_changed` event
276
+ * (regardless of persist outcome so listeners see the in-memory change), and
277
+ * return the toast the UI should display. Event payload carries the `persisted`
278
+ * flag so listeners can react to write failures.
279
+ */
280
+ export function saveAndEmitChanged(
281
+ snapshot: SubagentsSettings,
282
+ successMsg: string,
283
+ emit: SettingsEmit,
284
+ cwd: string = process.cwd(),
285
+ ): { message: string; level: "info" | "warning" } {
286
+ const persisted = saveSettings(snapshot, cwd);
287
+ emit("subagents:settings_changed", { settings: snapshot, persisted });
288
+ return persistToastFor(successMsg, persisted);
289
+ }
@@ -0,0 +1,102 @@
1
+ /**
2
+ * skill-loader.ts — Preload named skills.
3
+ *
4
+ * Roots, in precedence order:
5
+ * - <cwd>/.pi/skills (project, Pi's standard)
6
+ * - <cwd>/.agents/skills (project, cross-tool Agent Skills spec — https://agentskills.io)
7
+ * - getAgentDir()/skills (user, default ~/.pi/agent/skills — Pi's standard)
8
+ * - ~/.agents/skills (user, cross-tool Agent Skills spec)
9
+ * - ~/.pi/skills (legacy global, pre-Pi)
10
+ *
11
+ * Layout per root:
12
+ * - <root>/<name>.md (flat file at the top level)
13
+ * - <root>/.../<name>/SKILL.md (directory skill, may be nested — Pi's standard)
14
+ *
15
+ * Recursion skips dotfile entries and node_modules. A directory that itself contains
16
+ * SKILL.md is a skill — we don't descend into it (Pi: skills don't nest).
17
+ *
18
+ * Symlinks are rejected for security (deviation from Pi, which follows them).
19
+ */
20
+
21
+ import type { Dirent } from "node:fs";
22
+ import { existsSync, readdirSync } from "node:fs";
23
+ import { homedir } from "node:os";
24
+ import { join } from "node:path";
25
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
26
+ import { isSymlink, isUnsafeName, safeReadFile } from "./memory.js";
27
+
28
+ export interface PreloadedSkill {
29
+ name: string;
30
+ content: string;
31
+ }
32
+
33
+ export function preloadSkills(skillNames: string[], cwd: string): PreloadedSkill[] {
34
+ return skillNames.map((name) => ({ name, content: loadSkillContent(name, cwd) }));
35
+ }
36
+
37
+ function loadSkillContent(name: string, cwd: string): string {
38
+ if (isUnsafeName(name)) {
39
+ return `(Skill "${name}" skipped: name contains path traversal characters)`;
40
+ }
41
+ const roots = [
42
+ join(cwd, ".pi", "skills"), // project — Pi standard
43
+ join(cwd, ".agents", "skills"), // project — Agent Skills spec
44
+ join(getAgentDir(), "skills"), // user — Pi standard
45
+ join(homedir(), ".agents", "skills"), // user — Agent Skills spec
46
+ join(homedir(), ".pi", "skills"), // legacy global, pre-Pi
47
+ ];
48
+ for (const root of roots) {
49
+ const content = findInRoot(root, name);
50
+ if (content !== undefined) return content;
51
+ }
52
+ return `(Skill "${name}" not found in .pi/skills/, .agents/skills/, or global skill locations)`;
53
+ }
54
+
55
+ function findInRoot(root: string, name: string): string | undefined {
56
+ if (isSymlink(root)) return undefined; // reject symlinked roots entirely
57
+ const flat = safeReadFile(join(root, `${name}.md`))?.trim();
58
+ if (flat !== undefined) return flat;
59
+ return findSkillDirectory(root, name);
60
+ }
61
+
62
+ /** BFS under `root` for a directory named `name` containing `SKILL.md`. Pi-conforming filters. */
63
+ function findSkillDirectory(root: string, name: string): string | undefined {
64
+ if (!existsSync(root)) return undefined;
65
+ const queue: string[] = [root];
66
+
67
+ while (queue.length > 0) {
68
+ const current = queue.shift();
69
+ if (current === undefined) continue;
70
+
71
+ let entries: Dirent<string>[];
72
+ try {
73
+ entries = readdirSync(current, { withFileTypes: true });
74
+ } catch {
75
+ continue;
76
+ }
77
+
78
+ // Deterministic byte-order traversal — locale-independent.
79
+ entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
80
+
81
+ for (const entry of entries) {
82
+ if (!entry.isDirectory()) continue;
83
+ if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
84
+
85
+ // Symlinked dirs already filtered by entry.isDirectory() — Dirent uses lstat semantics.
86
+ const path = join(current, entry.name);
87
+ const skillMd = join(path, "SKILL.md");
88
+ const isSkillDir = existsSync(skillMd);
89
+
90
+ if (isSkillDir) {
91
+ if (entry.name === name) {
92
+ const content = safeReadFile(skillMd)?.trim();
93
+ if (content !== undefined) return content;
94
+ }
95
+ continue; // Pi rule: skills don't nest — don't descend into a skill dir
96
+ }
97
+
98
+ queue.push(path);
99
+ }
100
+ }
101
+ return undefined;
102
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * status-note.ts — Parenthetical status note appended to agent result text.
3
+ */
4
+
5
+ /**
6
+ * Explicit parenthetical note for a non-normal terminal outcome, so the parent
7
+ * agent can't mistake partial output for a completed result. Empty string for a
8
+ * clean completion (and any unknown/non-terminal status).
9
+ *
10
+ * `stopped` (a human aborted it) is deliberately distinct from `aborted` (the
11
+ * turn limit was hit) — the parent should treat human intervention differently
12
+ * from a budget cutoff.
13
+ */
14
+ export function getStatusNote(status: string): string {
15
+ switch (status) {
16
+ case "stopped":
17
+ return " (STOPPED BY THE USER before completion — output is partial; the task was NOT finished)";
18
+ case "aborted":
19
+ return " (aborted — hit the turn limit before completion; output may be incomplete)";
20
+ case "steered":
21
+ return " (wrapped up at the turn limit — output may be partial)";
22
+ default:
23
+ return "";
24
+ }
25
+ }
package/src/types.ts ADDED
@@ -0,0 +1,195 @@
1
+ /**
2
+ * types.ts — Type definitions for the subagent system.
3
+ */
4
+
5
+ import type { ThinkingLevel } from "@earendil-works/pi-ai";
6
+ import type { AgentSession } from "@earendil-works/pi-coding-agent";
7
+ import type { LifetimeUsage } from "./usage.js";
8
+
9
+ export type { ThinkingLevel };
10
+
11
+ /** Agent type: any string name (built-in defaults or user-defined). */
12
+ export type SubagentType = string;
13
+
14
+ /** Names of the three embedded default agents. */
15
+ export const DEFAULT_AGENT_NAMES = ["general-purpose", "Explore", "Plan"] as const;
16
+
17
+ /** Maximum recursive subagent depth. Parent/orchestrator is depth 0; agents may run at depths 1..4. */
18
+ export const MAX_RECURSIVE_DEPTH = 4;
19
+
20
+ /** Memory scope for persistent agent memory. */
21
+ export type MemoryScope = "user" | "project" | "local";
22
+
23
+ /** Isolation mode for agent execution. */
24
+ export type IsolationMode = "worktree";
25
+
26
+ /** Unified agent configuration — used for both default and user-defined agents. */
27
+ export interface AgentConfig {
28
+ name: string;
29
+ displayName?: string;
30
+ description: string;
31
+ builtinToolNames?: string[];
32
+ /** Raw `ext:` selector entries from the `tools:` CSV, e.g. ["ext:foo", "ext:bar/x"].
33
+ * Presence of any entry flips extension tools to an explicit allowlist. */
34
+ extSelectors?: string[];
35
+ /** Tool denylist — these tools are removed even if `builtinToolNames` or extensions include them. */
36
+ disallowedTools?: string[];
37
+ /** true = inherit all, string[] = only listed, false = none */
38
+ extensions: true | string[] | false;
39
+ /** Extension-name denylist applied after the `extensions:` include set. Exclude wins.
40
+ * Plain canonical names only (case-insensitive); no paths, no wildcard. */
41
+ excludeExtensions?: string[];
42
+ /** true = inherit all, string[] = only listed, false = none */
43
+ skills: true | string[] | false;
44
+ model?: string;
45
+ thinking?: ThinkingLevel;
46
+ maxTurns?: number;
47
+ systemPrompt: string;
48
+ promptMode: "replace" | "append";
49
+ /** Default for spawn: fork parent conversation. undefined = caller decides. */
50
+ inheritContext?: boolean;
51
+ /** Default for spawn: run in background. undefined = caller decides.
52
+ * NOTE: this fork always runs agents in the background, so a configured value
53
+ * here is accepted (frontmatter/RPC) but ignored at spawn time. Kept on the
54
+ * type for upstream/fixture compatibility. */
55
+ runInBackground?: boolean;
56
+ /** Default for spawn: no extension tools. undefined = caller decides. */
57
+ isolated?: boolean;
58
+ /** Persistent memory scope — agents with memory get a persistent directory and MEMORY.md */
59
+ memory?: MemoryScope;
60
+ /** Isolation mode — "worktree" runs the agent in a temporary git worktree */
61
+ isolation?: IsolationMode;
62
+ /** true = this is an embedded default agent (informational) */
63
+ isDefault?: boolean;
64
+ /** false = agent is hidden from the registry */
65
+ enabled?: boolean;
66
+ /** Where this agent was loaded from */
67
+ source?: "default" | "project" | "global";
68
+ }
69
+
70
+ export type JoinMode = 'async' | 'group' | 'smart';
71
+
72
+ export interface AgentRecord {
73
+ id: string;
74
+ type: SubagentType;
75
+ description: string;
76
+ status: "queued" | "running" | "completed" | "steered" | "aborted" | "stopped" | "error";
77
+ result?: string;
78
+ error?: string;
79
+ toolUses: number;
80
+ startedAt: number;
81
+ completedAt?: number;
82
+ session?: AgentSession;
83
+ abortController?: AbortController;
84
+ promise?: Promise<string>;
85
+ groupId?: string;
86
+ joinMode?: JoinMode;
87
+ /** Set when result was already consumed via get_subagent_result — suppresses completion notification. */
88
+ resultConsumed?: boolean;
89
+ /** Steering messages queued before the session was ready. */
90
+ pendingSteers?: string[];
91
+ /** Worktree info if the agent is running in an isolated worktree. */
92
+ worktree?: { path: string; branch: string; baseSha: string; workPath: string };
93
+ /** Worktree cleanup result after agent completion. */
94
+ worktreeResult?: { hasChanges: boolean; branch?: string };
95
+ /** The tool_use_id from the original Agent tool call. */
96
+ toolCallId?: string;
97
+ /** Recursive subagent depth. Parent/orchestrator is 0; spawned agents are 1..4. */
98
+ depth: number;
99
+ /** Parent subagent id when spawned recursively from another subagent. */
100
+ parentAgentId?: string;
101
+ /** Path to the streaming output transcript file. */
102
+ outputFile?: string;
103
+ /** Cleanup function for the output file stream subscription. */
104
+ outputCleanup?: () => void;
105
+ /**
106
+ * Lifetime usage breakdown, accumulated via `message_end` events. Survives
107
+ * compaction. Total = input + output + cacheWrite (cacheRead deliberately
108
+ * excluded — see issue #38). Initialized to zeros at spawn.
109
+ */
110
+ lifetimeUsage: LifetimeUsage;
111
+ /** Number of times this agent's session has compacted. Initialized to 0 at spawn. */
112
+ compactionCount: number;
113
+ /** Resolved spawn params, captured for UI display. Fixed at spawn time. */
114
+ invocation?: AgentInvocation;
115
+ }
116
+
117
+ export interface AgentInvocation {
118
+ /** Short display name, e.g. "haiku" — only set when different from parent. */
119
+ modelName?: string;
120
+ thinking?: ThinkingLevel;
121
+ maxTurns?: number;
122
+ isolated?: boolean;
123
+ inheritContext?: boolean;
124
+ runInBackground?: boolean;
125
+ isolation?: IsolationMode;
126
+ depth?: number;
127
+ parentAgentId?: string;
128
+ maxDepth?: number;
129
+ }
130
+
131
+ /** Details attached to custom notification messages for visual rendering. */
132
+ export interface NotificationDetails {
133
+ id: string;
134
+ description: string;
135
+ status: string;
136
+ toolUses: number;
137
+ turnCount: number;
138
+ maxTurns?: number;
139
+ totalTokens: number;
140
+ durationMs: number;
141
+ outputFile?: string;
142
+ error?: string;
143
+ resultPreview: string;
144
+ /** Additional agents in a group notification. */
145
+ others?: NotificationDetails[];
146
+ }
147
+
148
+ export interface EnvInfo {
149
+ isGitRepo: boolean;
150
+ branch: string;
151
+ platform: string;
152
+ }
153
+
154
+ /**
155
+ * A subagent spawn registered to fire on a schedule.
156
+ *
157
+ * Stored at `<cwd>/.pi/subagent-schedules/<sessionId>.json`. Session-scoped:
158
+ * survives `/resume` but resets on `/new`, mirroring pi-chonky-tasks.
159
+ */
160
+ export interface ScheduledSubagent {
161
+ id: string;
162
+ /** Unique within store. Defaults to `description`. */
163
+ name: string;
164
+ description: string;
165
+ /** Raw user input — cron expr | "+10m" | ISO | "5m". */
166
+ schedule: string;
167
+ scheduleType: "cron" | "once" | "interval";
168
+ /** Computed at create time for interval/once. */
169
+ intervalMs?: number;
170
+
171
+ // spawn params (subset of Agent tool params; no inherit_context, no resume)
172
+ subagent_type: SubagentType;
173
+ prompt: string;
174
+ model?: string;
175
+ thinking?: ThinkingLevel;
176
+ max_turns?: number;
177
+ isolated?: boolean;
178
+ isolation?: IsolationMode;
179
+
180
+ // state
181
+ enabled: boolean;
182
+ /** ISO timestamp. */
183
+ createdAt: string;
184
+ lastRun?: string;
185
+ lastStatus?: "success" | "error" | "running";
186
+ /** Refreshed on every fire and on store load. */
187
+ nextRun?: string;
188
+ runCount: number;
189
+ }
190
+
191
+ export interface ScheduleStoreData {
192
+ /** For future migrations. */
193
+ version: 1;
194
+ jobs: ScheduledSubagent[];
195
+ }