@timmyjl12/pi-subagents 0.0.0-stage → 0.21.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 (244) hide show
  1. package/CHANGELOG.md +862 -0
  2. package/CONTRIBUTING.md +72 -0
  3. package/LICENSE +21 -0
  4. package/README.md +1125 -3
  5. package/SECURITY.md +97 -0
  6. package/dist/abortable.d.ts +12 -0
  7. package/dist/abortable.js +42 -0
  8. package/dist/active-agents-prompt.d.ts +40 -0
  9. package/dist/active-agents-prompt.js +98 -0
  10. package/dist/advisor-status.d.ts +16 -0
  11. package/dist/advisor-status.js +74 -0
  12. package/dist/agent-color.d.ts +35 -0
  13. package/dist/agent-color.js +131 -0
  14. package/dist/agent-file-toggle.d.ts +125 -0
  15. package/dist/agent-file-toggle.js +264 -0
  16. package/dist/agent-manager.d.ts +872 -0
  17. package/dist/agent-manager.js +2910 -0
  18. package/dist/agent-runner.d.ts +358 -0
  19. package/dist/agent-runner.js +1219 -0
  20. package/dist/agent-types.d.ts +142 -0
  21. package/dist/agent-types.js +315 -0
  22. package/dist/awaited-result.d.ts +37 -0
  23. package/dist/awaited-result.js +150 -0
  24. package/dist/child-context.d.ts +15 -0
  25. package/dist/child-context.js +181 -0
  26. package/dist/child-session-shutdown.d.ts +21 -0
  27. package/dist/child-session-shutdown.js +97 -0
  28. package/dist/cleanup-plans.d.ts +10 -0
  29. package/dist/cleanup-plans.js +112 -0
  30. package/dist/context.d.ts +12 -0
  31. package/dist/context.js +56 -0
  32. package/dist/cross-extension-rpc.d.ts +73 -0
  33. package/dist/cross-extension-rpc.js +192 -0
  34. package/dist/custom-agents.d.ts +79 -0
  35. package/dist/custom-agents.js +583 -0
  36. package/dist/default-agents.d.ts +7 -0
  37. package/dist/default-agents.js +125 -0
  38. package/dist/enabled-models.d.ts +49 -0
  39. package/dist/enabled-models.js +145 -0
  40. package/dist/env.d.ts +6 -0
  41. package/dist/env.js +28 -0
  42. package/dist/error-log.d.ts +64 -0
  43. package/dist/error-log.js +176 -0
  44. package/dist/force-main-model.d.ts +21 -0
  45. package/dist/force-main-model.js +28 -0
  46. package/dist/goal-bridge.d.ts +155 -0
  47. package/dist/goal-bridge.js +305 -0
  48. package/dist/group-join.d.ts +36 -0
  49. package/dist/group-join.js +161 -0
  50. package/dist/index.d.ts +52 -0
  51. package/dist/index.js +6390 -0
  52. package/dist/invocation-config.d.ts +164 -0
  53. package/dist/invocation-config.js +158 -0
  54. package/dist/memory.d.ts +53 -0
  55. package/dist/memory.js +165 -0
  56. package/dist/mention-clone.d.ts +87 -0
  57. package/dist/mention-clone.js +178 -0
  58. package/dist/mention.d.ts +81 -0
  59. package/dist/mention.js +131 -0
  60. package/dist/model-resolver.d.ts +36 -0
  61. package/dist/model-resolver.js +95 -0
  62. package/dist/model-scope.d.ts +49 -0
  63. package/dist/model-scope.js +48 -0
  64. package/dist/nested-tools.d.ts +76 -0
  65. package/dist/nested-tools.js +393 -0
  66. package/dist/output-diagnostics.d.ts +11 -0
  67. package/dist/output-diagnostics.js +64 -0
  68. package/dist/output-file.d.ts +64 -0
  69. package/dist/output-file.js +351 -0
  70. package/dist/prompts.d.ts +61 -0
  71. package/dist/prompts.js +94 -0
  72. package/dist/recovery.d.ts +56 -0
  73. package/dist/recovery.js +341 -0
  74. package/dist/report-cleanup.d.ts +204 -0
  75. package/dist/report-cleanup.js +1632 -0
  76. package/dist/report-store.d.ts +738 -0
  77. package/dist/report-store.js +3612 -0
  78. package/dist/schedule-store.d.ts +52 -0
  79. package/dist/schedule-store.js +240 -0
  80. package/dist/schedule.d.ts +121 -0
  81. package/dist/schedule.js +420 -0
  82. package/dist/settings.d.ts +388 -0
  83. package/dist/settings.js +268 -0
  84. package/dist/skill-loader.d.ts +24 -0
  85. package/dist/skill-loader.js +93 -0
  86. package/dist/standalone-fallback.d.ts +30 -0
  87. package/dist/standalone-fallback.js +239 -0
  88. package/dist/status-note.d.ts +61 -0
  89. package/dist/status-note.js +85 -0
  90. package/dist/structured-output.d.ts +61 -0
  91. package/dist/structured-output.js +112 -0
  92. package/dist/terminal-progress.d.ts +115 -0
  93. package/dist/terminal-progress.js +206 -0
  94. package/dist/types.d.ts +411 -0
  95. package/dist/types.js +5 -0
  96. package/dist/ui/agent-mention.d.ts +82 -0
  97. package/dist/ui/agent-mention.js +187 -0
  98. package/dist/ui/agent-widget.d.ts +304 -0
  99. package/dist/ui/agent-widget.js +770 -0
  100. package/dist/ui/cleanup-preview.d.ts +59 -0
  101. package/dist/ui/cleanup-preview.js +466 -0
  102. package/dist/ui/conversation-viewer.d.ts +105 -0
  103. package/dist/ui/conversation-viewer.js +544 -0
  104. package/dist/ui/fleet-list.d.ts +183 -0
  105. package/dist/ui/fleet-list.js +494 -0
  106. package/dist/ui/fleet-overlay.d.ts +203 -0
  107. package/dist/ui/fleet-overlay.js +887 -0
  108. package/dist/ui/hub-feed.d.ts +52 -0
  109. package/dist/ui/hub-feed.js +148 -0
  110. package/dist/ui/native-transcript.d.ts +33 -0
  111. package/dist/ui/native-transcript.js +234 -0
  112. package/dist/ui/schedule-menu.d.ts +16 -0
  113. package/dist/ui/schedule-menu.js +94 -0
  114. package/dist/ui/select-item.d.ts +27 -0
  115. package/dist/ui/select-item.js +34 -0
  116. package/dist/ui/tool-progress.d.ts +22 -0
  117. package/dist/ui/tool-progress.js +56 -0
  118. package/dist/ui/viewer-keys.d.ts +20 -0
  119. package/dist/ui/viewer-keys.js +17 -0
  120. package/dist/ui/workflow-card.d.ts +175 -0
  121. package/dist/ui/workflow-card.js +332 -0
  122. package/dist/ui/workflow-dialog.d.ts +305 -0
  123. package/dist/ui/workflow-dialog.js +848 -0
  124. package/dist/ui/workflow-menu.d.ts +60 -0
  125. package/dist/ui/workflow-menu.js +147 -0
  126. package/dist/usage.d.ts +147 -0
  127. package/dist/usage.js +147 -0
  128. package/dist/workflow/collisions.d.ts +95 -0
  129. package/dist/workflow/collisions.js +88 -0
  130. package/dist/workflow/entry.d.ts +32 -0
  131. package/dist/workflow/entry.js +29 -0
  132. package/dist/workflow/host.d.ts +65 -0
  133. package/dist/workflow/host.js +477 -0
  134. package/dist/workflow/journal.d.ts +113 -0
  135. package/dist/workflow/journal.js +177 -0
  136. package/dist/workflow/json-schema.d.ts +51 -0
  137. package/dist/workflow/json-schema.js +111 -0
  138. package/dist/workflow/meta.d.ts +67 -0
  139. package/dist/workflow/meta.js +317 -0
  140. package/dist/workflow/progress.d.ts +224 -0
  141. package/dist/workflow/progress.js +361 -0
  142. package/dist/workflow/runtime.d.ts +373 -0
  143. package/dist/workflow/runtime.js +919 -0
  144. package/dist/workflow/saved.d.ts +90 -0
  145. package/dist/workflow/saved.js +203 -0
  146. package/dist/workflow/task.d.ts +145 -0
  147. package/dist/workflow/task.js +213 -0
  148. package/dist/workflow/tool-description.d.ts +43 -0
  149. package/dist/workflow/tool-description.js +204 -0
  150. package/dist/workflow/worker-source.d.ts +47 -0
  151. package/dist/workflow/worker-source.js +778 -0
  152. package/dist/worktree.d.ts +52 -0
  153. package/dist/worktree.js +164 -0
  154. package/dist/xml.d.ts +10 -0
  155. package/dist/xml.js +12 -0
  156. package/docs/DEVELOPMENT.md +40 -0
  157. package/docs/artifacts.md +93 -0
  158. package/docs/recovery.md +62 -0
  159. package/docs/rpc.md +252 -0
  160. package/docs/runtime-performance.md +53 -0
  161. package/docs/workflows.md +467 -0
  162. package/examples/agent-tool-description.md +43 -0
  163. package/examples/workflows/compose.js +51 -0
  164. package/examples/workflows/fan-out-audit.js +47 -0
  165. package/examples/workflows/gated-fix.js +60 -0
  166. package/examples/workflows/lib/count-child.js +27 -0
  167. package/examples/workflows/review-panel.js +66 -0
  168. package/examples/workflows/structured-findings.js +78 -0
  169. package/package.json +75 -6
  170. package/src/abortable.ts +43 -0
  171. package/src/active-agents-prompt.ts +115 -0
  172. package/src/advisor-status.ts +74 -0
  173. package/src/agent-color.ts +169 -0
  174. package/src/agent-file-toggle.ts +273 -0
  175. package/src/agent-manager.ts +3312 -0
  176. package/src/agent-runner.ts +1500 -0
  177. package/src/agent-types.ts +390 -0
  178. package/src/awaited-result.ts +187 -0
  179. package/src/child-context.ts +173 -0
  180. package/src/child-session-shutdown.ts +114 -0
  181. package/src/cleanup-plans.ts +112 -0
  182. package/src/context.ts +58 -0
  183. package/src/cross-extension-rpc.ts +257 -0
  184. package/src/custom-agents.ts +689 -0
  185. package/src/default-agents.ts +129 -0
  186. package/src/enabled-models.ts +180 -0
  187. package/src/env.ts +33 -0
  188. package/src/error-log.ts +202 -0
  189. package/src/force-main-model.ts +29 -0
  190. package/src/goal-bridge.ts +468 -0
  191. package/src/group-join.ts +188 -0
  192. package/src/index.ts +6856 -0
  193. package/src/invocation-config.ts +276 -0
  194. package/src/memory.ts +179 -0
  195. package/src/mention-clone.ts +221 -0
  196. package/src/mention.ts +141 -0
  197. package/src/model-resolver.ts +118 -0
  198. package/src/model-scope.ts +70 -0
  199. package/src/nested-tools.ts +553 -0
  200. package/src/output-diagnostics.ts +68 -0
  201. package/src/output-file.ts +380 -0
  202. package/src/prompts.ts +151 -0
  203. package/src/recovery.ts +320 -0
  204. package/src/report-cleanup.ts +2044 -0
  205. package/src/report-store.ts +4130 -0
  206. package/src/schedule-store.ts +244 -0
  207. package/src/schedule.ts +450 -0
  208. package/src/settings.ts +632 -0
  209. package/src/skill-loader.ts +102 -0
  210. package/src/standalone-fallback.ts +195 -0
  211. package/src/status-note.ts +90 -0
  212. package/src/structured-output.ts +130 -0
  213. package/src/terminal-progress.ts +215 -0
  214. package/src/types.ts +424 -0
  215. package/src/ui/agent-mention.ts +216 -0
  216. package/src/ui/agent-widget.ts +866 -0
  217. package/src/ui/cleanup-preview.ts +518 -0
  218. package/src/ui/conversation-viewer.ts +527 -0
  219. package/src/ui/fleet-list.ts +563 -0
  220. package/src/ui/fleet-overlay.ts +1048 -0
  221. package/src/ui/hub-feed.ts +179 -0
  222. package/src/ui/native-transcript.ts +224 -0
  223. package/src/ui/schedule-menu.ts +105 -0
  224. package/src/ui/select-item.ts +45 -0
  225. package/src/ui/tool-progress.ts +67 -0
  226. package/src/ui/viewer-keys.ts +39 -0
  227. package/src/ui/workflow-card.ts +470 -0
  228. package/src/ui/workflow-dialog.ts +1119 -0
  229. package/src/ui/workflow-menu.ts +193 -0
  230. package/src/usage.ts +217 -0
  231. package/src/workflow/collisions.ts +123 -0
  232. package/src/workflow/entry.ts +47 -0
  233. package/src/workflow/host.ts +524 -0
  234. package/src/workflow/journal.ts +221 -0
  235. package/src/workflow/json-schema.ts +128 -0
  236. package/src/workflow/meta.ts +325 -0
  237. package/src/workflow/progress.ts +550 -0
  238. package/src/workflow/runtime.ts +1342 -0
  239. package/src/workflow/saved.ts +217 -0
  240. package/src/workflow/task.ts +313 -0
  241. package/src/workflow/tool-description.ts +205 -0
  242. package/src/workflow/worker-source.ts +781 -0
  243. package/src/worktree.ts +205 -0
  244. package/src/xml.ts +13 -0
@@ -0,0 +1,358 @@
1
+ /**
2
+ * agent-runner.ts — Core execution engine: creates sessions, runs agents, collects results.
3
+ */
4
+ import type { Model } from "@earendil-works/pi-ai";
5
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
6
+ import { type AgentSession, DefaultResourceLoader, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
7
+ import { type AdvisorStatus } from "./advisor-status.ts";
8
+ import { type ChildShutdownOutcome } from "./child-session-shutdown.ts";
9
+ import { resolvePinnedModel } from "./invocation-config.ts";
10
+ import { type ArchivedReportReader, type NestedAgentManager } from "./nested-tools.ts";
11
+ import type { AgentConfig, SubagentType, ThinkingLevel } from "./types.ts";
12
+ import type { LifetimeUsage } from "./usage.ts";
13
+ import type { CompiledSchema } from "./workflow/json-schema.ts";
14
+ /**
15
+ * Tool names registered by THIS extension. Single source of truth so the
16
+ * registration sites (index.ts) and the subagent exclusion list below can't
17
+ * drift apart. These are our own tools, not pi built-ins, so they can't be
18
+ * derived from pi — but they only need defining once.
19
+ */
20
+ export declare const SUBAGENT_TOOL_NAMES: {
21
+ readonly AGENT: "Agent";
22
+ readonly WORKFLOW: "SubagentWorkflow";
23
+ readonly GET_RESULT: "get_subagent_result";
24
+ readonly STEER: "steer_subagent";
25
+ readonly STOP: "stop_subagent";
26
+ };
27
+ /**
28
+ * Canonical name of an extension for `extensions: [...]` allowlist matching.
29
+ * Lowercased — extension names match case-insensitively so `extensions: [Mcp]`
30
+ * resolves the same as `[mcp]`. Tool names within `ext:foo/bar` are not affected.
31
+ * Directory extensions (`foo/index.ts`) resolve to the parent directory name;
32
+ * single-file extensions to the basename minus `.ts`/`.js`.
33
+ */
34
+ export declare function extensionCanonicalName(extPath: string): string;
35
+ /**
36
+ * All names an extension answers to for allowlist matching (lowercased): its
37
+ * path-derived {@link extensionCanonicalName} plus, when a pi package manifest
38
+ * declares this entry, that package's unscoped short name (`@scope/foo` → `foo`).
39
+ * #143: an extension installed via `pi.extensions: ["./src/index.ts"]` would
40
+ * otherwise only ever match as `src` (the source directory), never by its
41
+ * package name. The path-derived name is preserved, so it keeps matching too.
42
+ */
43
+ export declare function extensionCanonicalNames(extPath: string): string[];
44
+ /** Extensions implementing the PI_SUBAGENT_CHILD/advisor contract. Removed from a child's loader when its frontmatter says `advisor: false`. */
45
+ export declare const ADVISOR_EXTENSION_NAMES: Set<string>;
46
+ /**
47
+ * Classify `extensions: string[]` frontmatter entries for the loader-level filter.
48
+ *
49
+ * An entry is a PATH iff it contains a path separator or starts with `~`; otherwise
50
+ * it is a NAME. `"*"` sets the wildcard flag (keep all default-discovered extensions).
51
+ *
52
+ * Path entries are resolved (`~` expanded, made absolute against `cwd`) into `paths`
53
+ * — and their canonical name is also added to `names`. The loader override matches
54
+ * everything by canonical name, so path-loaded extensions are matched via their name
55
+ * rather than their post-staging `Extension.path`.
56
+ */
57
+ export declare function parseExtensionsSpec(entries: string[], cwd: string): {
58
+ names: Set<string>;
59
+ paths: string[];
60
+ wildcard: boolean;
61
+ };
62
+ /**
63
+ * Parse raw `ext:` selector strings (from the `tools:` CSV) into the set of
64
+ * extension names to keep loaded and a per-extension tool-narrowing map.
65
+ *
66
+ * `ext:foo` → `extNames` has `foo`, no narrowing entry (all of foo's tools).
67
+ * `ext:foo/bar` → `extNames` has `foo`, `narrowing.foo` has `bar` (only `bar`).
68
+ * A name lands in `narrowing` only when a `/tool` form is seen, so a bare
69
+ * `ext:foo` alongside `ext:foo/bar` leaves narrowing in effect (narrowing wins).
70
+ * The split is on the first `/`; extension canonical names never contain `/`.
71
+ */
72
+ export declare function parseExtSelectors(entries: string[]): {
73
+ extNames: Set<string>;
74
+ narrowing: Map<string, Set<string>>;
75
+ };
76
+ /**
77
+ * Keep a subagent's tool scope correct as extensions register tools over time.
78
+ *
79
+ * Extensions may call `registerTool` long after load — pi-mcp from `session_start`,
80
+ * context-mode from `before_agent_start` — so scope has to be re-derived rather than
81
+ * snapshotted. `registerTool` writes into the very `extension.tools` maps this reads,
82
+ * so `inScope()` sees late arrivals on the next call.
83
+ *
84
+ * Two enforcement points, because neither covers the whole picture:
85
+ *
86
+ * - `turn_end` re-narrows the ACTIVE set. pi emits `turn_end` immediately before
87
+ * `prepareNextTurn` re-snapshots `agent.state.tools`, and session listeners run
88
+ * synchronously, so the narrow lands in time for turns 2..N.
89
+ * - `beforeToolCall` blocks out-of-scope calls. Turn 1 cannot be narrowed at all:
90
+ * `before_agent_start` fires INSIDE `prompt()` and may widen the tool set, but
91
+ * `createContextSnapshot()` freezes that turn's tools immediately after — there
92
+ * is no hook in between. A call-time check is the only correct guard there.
93
+ *
94
+ * Both are installed on the session and deliberately NOT unsubscribed: they must
95
+ * outlive the `runAgent` call so resumed/steered turns stay scoped. pi's `dispose()`
96
+ * clears `_eventListeners`, so they die with the session rather than leaking.
97
+ *
98
+ * Only meaningful when extensions are loaded — under `noExtensions`/`isolated` the
99
+ * static `allowedToolNames` allowlist already gates the registry itself.
100
+ */
101
+ export declare function installExtensionToolScope(session: AgentSession, ctx: {
102
+ loader: DefaultResourceLoader;
103
+ toolNames: string[];
104
+ disallowedSet: Set<string> | undefined;
105
+ extNames: Set<string>;
106
+ narrowing: Map<string, Set<string>>;
107
+ /**
108
+ * Injected `customTools` to keep active regardless of the built-in list.
109
+ *
110
+ * Two kinds arrive here and they are blocked for different reasons: opt-in
111
+ * nested-delegation tools share EXCLUDED_TOOL_NAMES' names, and
112
+ * StructuredOutput is simply not a built-in, so neither survives a `keep`
113
+ * seeded from `toolNames`.
114
+ */
115
+ readmitToolNames: Set<string>;
116
+ }): void;
117
+ /** Normalize max turns. undefined or 0 = unlimited, otherwise minimum 1. */
118
+ export declare function normalizeMaxTurns(n: number | undefined): number | undefined;
119
+ /** Get the default max turns value. undefined = unlimited. */
120
+ export declare function getDefaultMaxTurns(): number | undefined;
121
+ /** Set the default max turns value. undefined or 0 = unlimited, otherwise minimum 1. */
122
+ export declare function setDefaultMaxTurns(n: number | undefined): void;
123
+ /** Whether callers may impose per-call turn limits. Default false. */
124
+ export declare function getAllowPerCallTurnLimits(): boolean;
125
+ /** Set whether callers may impose per-call turn limits. */
126
+ export declare function setAllowPerCallTurnLimits(b: boolean): void;
127
+ /**
128
+ * The turn limit a run of `type` will actually enforce: an explicit value if the
129
+ * caller supplied one, else the agent's own `max_turns`, else the project
130
+ * default. `undefined` = unlimited.
131
+ *
132
+ * Exported because the widget's turn counter (`↻3≤20`) has to predict this
133
+ * before the run starts, and a second copy of the expression would drift from
134
+ * the one below that enforces it.
135
+ */
136
+ export declare function resolveEffectiveMaxTurns(type: string, explicit?: number): number | undefined;
137
+ /** Whether subagent sessions are persisted by default. */
138
+ export declare function getRememberAgents(): boolean;
139
+ /** Set whether subagent sessions are persisted by default. */
140
+ export declare function setRememberAgents(b: boolean): void;
141
+ /** Get the grace turns value. */
142
+ export declare function getGraceTurns(): number;
143
+ /** Set the grace turns value (minimum 1). */
144
+ export declare function setGraceTurns(n: number): void;
145
+ /**
146
+ * Resolve the authoritative agent pin, inheriting only when no pin exists.
147
+ *
148
+ * `isBuiltInDefault` marks an embedded default agent (Explore/Plan): when its
149
+ * qualified pin names a provider the user lacks, the same id on another
150
+ * provider wins, else the parent model is used with a `notice` the caller must
151
+ * surface. User-authored pins stay strict and throw. Returns `{ model, notice
152
+ * }` so the caller can report a substitution; `notice` is undefined when the
153
+ * pin resolved exactly.
154
+ */
155
+ export declare function resolveDefaultModel(parentModel: Model<any> | undefined, registry: Parameters<typeof resolvePinnedModel>[1], configModel?: string, opts?: {
156
+ isBuiltInDefault?: boolean;
157
+ }): {
158
+ model: Model<any> | undefined;
159
+ notice?: string;
160
+ };
161
+ /** Info about a tool event in the subagent. */
162
+ export interface ToolActivity {
163
+ type: "start" | "end";
164
+ toolName: string;
165
+ }
166
+ export interface RunOptions {
167
+ /** ExtensionAPI instance — used for pi.exec() instead of execSync. */
168
+ pi: ExtensionAPI;
169
+ /**
170
+ * Internal, manager-only: the agent definition admitted with this run.
171
+ *
172
+ * The manager snapshots this from its registry BEFORE a spawn is queued or
173
+ * starts awaiting work, and callers that resolve a definition against a
174
+ * scoped registry pass their own snapshot. The runner reads prompt, tools,
175
+ * model, memory, session and nesting policy from THIS object, never from
176
+ * `getAgentConfig`/`getToolNamesForType` once the run has suspended: a
177
+ * reload during a queued wait or a worktree copy must not change the agent
178
+ * that was admitted. Undefined keeps the historical direct-call behaviour
179
+ * (including the unknown-type general-purpose fallback).
180
+ */
181
+ agentDefinition?: AgentConfig;
182
+ /** Manager-assigned id; suffixes session name to disambiguate parallel spawns (e.g. `Explore#a1b2c3d4`). */
183
+ agentId?: string;
184
+ model?: Model<any>;
185
+ maxTurns?: number;
186
+ signal?: AbortSignal;
187
+ isolated?: boolean;
188
+ inheritContext?: boolean;
189
+ thinkingLevel?: ThinkingLevel;
190
+ /**
191
+ * Reopen this pi session file rather than starting an empty conversation.
192
+ * `createAgentSession` seeds itself from whatever its SessionManager holds,
193
+ * so pointing it at an existing file rehydrates that agent's history and the
194
+ * prompt continues it. Everything else — tools, model, system prompt, turn
195
+ * caps — is still resolved from the agent type, so the continuation runs
196
+ * under the type's *current* definition, not the one the original run used.
197
+ */
198
+ resumeSessionFile?: string;
199
+ /**
200
+ * True when another agent spawned this one. Only top-level agents get a
201
+ * handle, so only they can be reopened by name — which is the whole reason
202
+ * `rememberAgents` persists a session at all. A nested run's transcript would
203
+ * be unreachable by anything, so it stays in memory unless its own
204
+ * frontmatter asks otherwise.
205
+ */
206
+ nested?: boolean;
207
+ /**
208
+ * True when a workflow run spawned this agent. Its final text is the value
209
+ * `agent()` resolves to rather than a report a person reads, and the prompt
210
+ * says so — but only when `structuredOutput` is unset, since that child
211
+ * already has a `StructuredOutput` tool to answer through and two competing
212
+ * "this is how you return your answer" instructions is worse than one.
213
+ */
214
+ workflow?: boolean;
215
+ /** Override working directory (e.g. for worktree isolation). */
216
+ cwd?: string;
217
+ /**
218
+ * Directory the worktree copy was created from. Set only when `cwd` points
219
+ * into a worktree — the prompt then tells the agent to stay in the copy
220
+ * instead of following the inherited parent prompt back to the main tree.
221
+ */
222
+ worktreeBase?: string;
223
+ /**
224
+ * Where .pi config is discovered (project extensions, skills, pi settings,
225
+ * agent memory). Default: same as the working directory. The manager sets
226
+ * this to the parent session's cwd when `SpawnOptions.cwd` points the
227
+ * working directory elsewhere — the agent works *there* but carries the
228
+ * parent project's config (the target's `.pi` extensions never execute).
229
+ *
230
+ * WARNING for future callers: if you pass `cwd` pointing at a directory the
231
+ * user didn't open, you almost certainly must pass `configCwd` too —
232
+ * omitting it makes the target's `.pi` extensions execute in this process.
233
+ * (Worktree isolation is the one intentional exception: its copy IS the
234
+ * parent's repo, so config resolving inside it is correct.)
235
+ */
236
+ configCwd?: string;
237
+ /** Called on tool start/end with activity info. */
238
+ onToolActivity?: (activity: ToolActivity) => void;
239
+ /** Called on streaming text deltas from the assistant response. */
240
+ onTextDelta?: (delta: string, fullText: string) => void;
241
+ onSessionCreated?: (session: AgentSession) => void;
242
+ /**
243
+ * Internal, manager-only: fires synchronously as soon as `createAgentSession`
244
+ * returns, BEFORE extension binding. The manager registers the allocated SDK
245
+ * instance and its plain provenance so a bind failure or a hang during
246
+ * `session_start` cannot make teardown look complete. It must not steer or
247
+ * announce a ready session here — `onSessionCreated` remains that boundary.
248
+ */
249
+ onSessionAllocated?: (session: AgentSession) => void;
250
+ /**
251
+ * Internal, manager-only: reports the explicit outcome of a partial-startup
252
+ * teardown (currently a bind failure) without rejecting the run's error path.
253
+ */
254
+ onSessionTeardown?: (session: AgentSession, outcome: ChildShutdownOutcome) => void;
255
+ /** Child-local advisor runtime telemetry; independent of the child UI. */
256
+ onAdvisorStatus?: (status: AdvisorStatus) => void;
257
+ /** Refresh effective model metadata after recovery switches, including on resume. */
258
+ onModelChange?: (session: AgentSession) => void;
259
+ /** Called at the end of each agentic turn with the cumulative count. */
260
+ onTurnEnd?: (turnCount: number) => void;
261
+ /**
262
+ * Called once per assistant message_end with that message's usage delta.
263
+ * Lets callers maintain a lifetime accumulator that survives compaction
264
+ * (which replaces session.state.messages and resets stats-derived sums).
265
+ *
266
+ * `cost` is pi's own `usage.cost.total` for that message — priced from the
267
+ * model's rates, so it is 0 (not missing) for a model pi has no pricing for.
268
+ * We never price anything ourselves; every dollar figure this extension shows
269
+ * or reports traces back to this field.
270
+ */
271
+ onAssistantUsage?: (usage: LifetimeUsage) => void;
272
+ /**
273
+ * Called when the session successfully compacts. `tokensBefore` is upstream's
274
+ * pre-compaction context size estimate. Aborted compactions don't fire.
275
+ */
276
+ onCompaction?: (info: {
277
+ reason: "manual" | "threshold" | "overflow";
278
+ tokensBefore: number;
279
+ }) => void;
280
+ /**
281
+ * Make this child report through a `StructuredOutput` tool built from this
282
+ * schema, and put the validated payload on {@link RunResult.structuredJson}.
283
+ *
284
+ * Already compiled by the caller, so a schema this runtime cannot validate
285
+ * fails at the call that wrote it rather than inside the child.
286
+ */
287
+ structuredOutput?: CompiledSchema;
288
+ /** Runtime bridge for opt-in child-safe nested delegation. */
289
+ nestedRuntime?: {
290
+ manager: NestedAgentManager;
291
+ parentAgentId: string;
292
+ depth: number;
293
+ maxSubagentDepth?: number;
294
+ /** Async parent-scoped durable-report reader for evicted children. */
295
+ archive?: ArchivedReportReader;
296
+ };
297
+ /** Stable root session id, pinned by the manager for ownership provenance. */
298
+ rootSessionId?: string;
299
+ /** Stable root project cwd — never the worktree copy. */
300
+ rootCwd?: string;
301
+ /** Root session's pi JSONL file, when it exists. */
302
+ rootSessionFile?: string;
303
+ }
304
+ export interface RunResult {
305
+ responseText: string;
306
+ session: AgentSession;
307
+ /** True if the agent was hard-aborted (max_turns + grace exceeded). */
308
+ aborted: boolean;
309
+ /** True if the agent was steered to wrap up (hit soft turn limit) but finished in time. */
310
+ steered: boolean;
311
+ /**
312
+ * A failure message for the run's FINAL assistant turn, when that turn failed:
313
+ * a provider error (stopReason "error"), or a "length" stop that produced no
314
+ * text (a silent max-token death). pi resolves an exhausted-retries failure
315
+ * normally instead of rejecting, so without this the manager would report such
316
+ * a run as completed — with an empty result, or worse, an earlier turn's text
317
+ * presented as the answer (#144). Undefined for a clean stop, or a "length"
318
+ * stop that produced text (a legitimate truncated answer).
319
+ */
320
+ failure?: string;
321
+ /**
322
+ * The validated `StructuredOutput` payload as canonical JSON, when the caller
323
+ * asked for a schema and the child produced one.
324
+ *
325
+ * Deliberately not folded into {@link responseText}: `record.result` picks up
326
+ * a worktree branch note on the way out, which would leave the caller with
327
+ * unparseable JSON, and merging the two would make "produced structured
328
+ * output" indistinguishable from "happened to answer in JSON".
329
+ */
330
+ structuredJson?: string;
331
+ /** Whether the extra structured-output prompt had to be sent. */
332
+ structuredRetried?: boolean;
333
+ }
334
+ export declare function runAgent(ctx: ExtensionContext, type: SubagentType, prompt: string, options: RunOptions): Promise<RunResult>;
335
+ /**
336
+ * Send a new prompt to an existing session (resume).
337
+ */
338
+ export declare function resumeAgent(session: AgentSession, prompt: string, options?: {
339
+ onToolActivity?: (activity: ToolActivity) => void;
340
+ onAssistantUsage?: (usage: LifetimeUsage) => void;
341
+ onCompaction?: (info: {
342
+ reason: "manual" | "threshold" | "overflow";
343
+ tokensBefore: number;
344
+ }) => void;
345
+ signal?: AbortSignal;
346
+ }): Promise<{
347
+ text: string;
348
+ failure?: string;
349
+ }>;
350
+ /**
351
+ * Send a steering message to a running subagent.
352
+ * The message will interrupt the agent after its current tool execution.
353
+ */
354
+ export declare function steerAgent(session: AgentSession, message: string): Promise<void>;
355
+ /**
356
+ * Get the subagent's conversation messages as formatted text.
357
+ */
358
+ export declare function getAgentConversation(session: AgentSession): string;