@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.
- package/.plans/PLAN-next-changes.md +183 -0
- package/.plans/README.md +14 -0
- package/AGENTS.md +31 -0
- package/CHANGELOG.md +583 -0
- package/CLAUDE.md +1 -0
- package/LICENSE +21 -0
- package/README.md +630 -0
- package/RELEASE.md +39 -0
- package/dist/abort-resend.d.ts +35 -0
- package/dist/abort-resend.js +71 -0
- package/dist/agent-details.d.ts +17 -0
- package/dist/agent-details.js +22 -0
- package/dist/agent-manager.d.ts +132 -0
- package/dist/agent-manager.js +493 -0
- package/dist/agent-runner.d.ts +165 -0
- package/dist/agent-runner.js +732 -0
- package/dist/agent-tool-description.d.ts +9 -0
- package/dist/agent-tool-description.js +147 -0
- package/dist/agent-types.d.ts +60 -0
- package/dist/agent-types.js +157 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +46 -0
- package/dist/cross-extension-rpc.js +76 -0
- package/dist/custom-agents.d.ts +14 -0
- package/dist/custom-agents.js +149 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +119 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +1918 -0
- package/dist/invocation-config.d.ts +25 -0
- package/dist/invocation-config.js +19 -0
- package/dist/memory.d.ts +49 -0
- package/dist/memory.js +151 -0
- package/dist/model-resolver.d.ts +19 -0
- package/dist/model-resolver.js +62 -0
- package/dist/notifications.d.ts +6 -0
- package/dist/notifications.js +107 -0
- package/dist/output-file.d.ts +24 -0
- package/dist/output-file.js +86 -0
- package/dist/peek.d.ts +37 -0
- package/dist/peek.js +121 -0
- package/dist/prompts.d.ts +40 -0
- package/dist/prompts.js +95 -0
- package/dist/schedule-store.d.ts +38 -0
- package/dist/schedule-store.js +155 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +338 -0
- package/dist/settings.d.ts +135 -0
- package/dist/settings.js +168 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/status-note.d.ts +13 -0
- package/dist/status-note.js +24 -0
- package/dist/types.d.ts +184 -0
- package/dist/types.js +7 -0
- package/dist/ui/agent-tool-rendering.d.ts +34 -0
- package/dist/ui/agent-tool-rendering.js +154 -0
- package/dist/ui/agent-widget-tree.d.ts +33 -0
- package/dist/ui/agent-widget-tree.js +130 -0
- package/dist/ui/agent-widget.d.ts +156 -0
- package/dist/ui/agent-widget.js +408 -0
- package/dist/ui/conversation-viewer.d.ts +47 -0
- package/dist/ui/conversation-viewer.js +290 -0
- package/dist/ui/menu-select.d.ts +20 -0
- package/dist/ui/menu-select.js +46 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +99 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/usage.d.ts +50 -0
- package/dist/usage.js +49 -0
- package/dist/wait.d.ts +10 -0
- package/dist/wait.js +37 -0
- package/dist/worktree.d.ts +45 -0
- package/dist/worktree.js +160 -0
- package/docs/design/default-extension-tool-exposure.md +56 -0
- package/docs/superpowers/plans/2026-06-19-recursive-subagent-widget.md +600 -0
- package/docs/superpowers/specs/2026-06-19-recursive-subagent-widget-design.md +189 -0
- package/examples/agent-tool-description.md +45 -0
- package/package.json +56 -0
- package/reviews/proposal-structured-output-schema.md +135 -0
- package/reviews/recursive-subagent-widget-preview-rev2.png +0 -0
- package/reviews/recursive-subagent-widget-preview.html +137 -0
- package/reviews/recursive-subagent-widget-preview.png +0 -0
- package/reviews/subagent-features-comparison.md +350 -0
- package/src/abort-resend.ts +75 -0
- package/src/agent-details.ts +31 -0
- package/src/agent-manager.ts +596 -0
- package/src/agent-runner.ts +872 -0
- package/src/agent-tool-description.ts +163 -0
- package/src/agent-types.ts +189 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +122 -0
- package/src/custom-agents.ts +160 -0
- package/src/default-agents.ts +123 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +2115 -0
- package/src/invocation-config.ts +42 -0
- package/src/memory.ts +165 -0
- package/src/model-resolver.ts +81 -0
- package/src/notifications.ts +120 -0
- package/src/output-file.ts +96 -0
- package/src/peek.ts +155 -0
- package/src/prompts.ts +129 -0
- package/src/schedule-store.ts +153 -0
- package/src/schedule.ts +365 -0
- package/src/settings.ts +289 -0
- package/src/skill-loader.ts +102 -0
- package/src/status-note.ts +25 -0
- package/src/types.ts +195 -0
- package/src/ui/agent-tool-rendering.ts +175 -0
- package/src/ui/agent-widget-tree.ts +169 -0
- package/src/ui/agent-widget.ts +497 -0
- package/src/ui/conversation-viewer.ts +297 -0
- package/src/ui/menu-select.ts +68 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/usage.ts +60 -0
- package/src/wait.ts +44 -0
- package/src/worktree.ts +191 -0
- package/vitest.config.ts +25 -0
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-manager.ts — Tracks agents, background execution, resume support.
|
|
3
|
+
*
|
|
4
|
+
* Background agents are subject to a configurable concurrency limit (default: 4).
|
|
5
|
+
* Excess agents are queued and auto-started as running agents complete.
|
|
6
|
+
* Foreground agents bypass the queue (they block the parent anyway).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
import { statSync } from "node:fs";
|
|
11
|
+
import { isAbsolute } from "node:path";
|
|
12
|
+
import type { Model } from "@earendil-works/pi-ai";
|
|
13
|
+
import type { AgentSession, ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { resumeAgent, runAgent, type ToolActivity } from "./agent-runner.js";
|
|
15
|
+
import { type AgentInvocation, type AgentRecord, type IsolationMode, MAX_RECURSIVE_DEPTH, type SubagentType, type ThinkingLevel } from "./types.js";
|
|
16
|
+
import { addUsage } from "./usage.js";
|
|
17
|
+
import { cleanupWorktree, createWorktree, pruneWorktrees, } from "./worktree.js";
|
|
18
|
+
|
|
19
|
+
export type OnAgentComplete = (record: AgentRecord) => void;
|
|
20
|
+
export type OnAgentStart = (record: AgentRecord) => void;
|
|
21
|
+
export type OnAgentCompact = (record: AgentRecord, info: CompactionInfo) => void;
|
|
22
|
+
export type CompactionInfo = { reason: "manual" | "threshold" | "overflow"; tokensBefore: number };
|
|
23
|
+
|
|
24
|
+
/** Default max concurrent background agents. */
|
|
25
|
+
const DEFAULT_MAX_CONCURRENT = 4;
|
|
26
|
+
/**
|
|
27
|
+
* Validate a caller-supplied SpawnOptions.cwd. `undefined`/`null` mean "unset"
|
|
28
|
+
* (parent cwd). Anything else must be an absolute path to an existing
|
|
29
|
+
* directory — curated errors instead of TypeErrors from path/fs internals
|
|
30
|
+
* (RPC callers send arbitrary JSON: null, numbers, file paths).
|
|
31
|
+
*/
|
|
32
|
+
function assertValidSpawnCwd(cwd: unknown): asserts cwd is string | undefined | null {
|
|
33
|
+
if (cwd == null) return;
|
|
34
|
+
if (typeof cwd !== "string" || !isAbsolute(cwd)) {
|
|
35
|
+
throw new Error(`SpawnOptions.cwd must be an absolute path: "${String(cwd)}"`);
|
|
36
|
+
}
|
|
37
|
+
let isDirectory = false;
|
|
38
|
+
try {
|
|
39
|
+
isDirectory = statSync(cwd).isDirectory();
|
|
40
|
+
} catch {
|
|
41
|
+
throw new Error(`SpawnOptions.cwd does not exist: "${cwd}"`);
|
|
42
|
+
}
|
|
43
|
+
if (!isDirectory) {
|
|
44
|
+
throw new Error(`SpawnOptions.cwd is not a directory: "${cwd}"`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface SpawnArgs {
|
|
49
|
+
pi: ExtensionAPI;
|
|
50
|
+
ctx: ExtensionContext;
|
|
51
|
+
type: SubagentType;
|
|
52
|
+
prompt: string;
|
|
53
|
+
options: SpawnOptions;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface SpawnOptions {
|
|
57
|
+
description: string;
|
|
58
|
+
model?: Model<any>;
|
|
59
|
+
maxTurns?: number;
|
|
60
|
+
isolated?: boolean;
|
|
61
|
+
inheritContext?: boolean;
|
|
62
|
+
thinkingLevel?: ThinkingLevel;
|
|
63
|
+
isBackground?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Skip the maxConcurrent queue check for this spawn — start immediately even
|
|
66
|
+
* if the configured concurrency limit would otherwise queue it. Used by the
|
|
67
|
+
* scheduler so a fired job can't be deferred past its trigger window.
|
|
68
|
+
*/
|
|
69
|
+
bypassQueue?: boolean;
|
|
70
|
+
/** Isolation mode — "worktree" creates a temp git worktree for the agent. */
|
|
71
|
+
isolation?: IsolationMode;
|
|
72
|
+
/**
|
|
73
|
+
* Working directory for the agent (absolute path). Default: parent session
|
|
74
|
+
* cwd. The agent's tools operate here, but .pi config (extensions, skills,
|
|
75
|
+
* settings, memory) still loads from the parent session's project — the
|
|
76
|
+
* target directory's `.pi` extensions never execute. With isolation:
|
|
77
|
+
* "worktree", the worktree is created FROM this directory and the result
|
|
78
|
+
* branch lands in that repo.
|
|
79
|
+
*/
|
|
80
|
+
cwd?: string;
|
|
81
|
+
/** Resolved invocation snapshot captured for UI display. */
|
|
82
|
+
invocation?: AgentInvocation;
|
|
83
|
+
/** Recursive subagent depth. Parent/orchestrator is 0; spawned agents are 1..4. */
|
|
84
|
+
depth?: number;
|
|
85
|
+
/** Parent subagent id when spawned recursively from another subagent. */
|
|
86
|
+
parentAgentId?: string;
|
|
87
|
+
/** Parent abort signal — when aborted, the subagent is also stopped. */
|
|
88
|
+
signal?: AbortSignal;
|
|
89
|
+
/** Called on tool start/end with activity info (for streaming progress to UI). */
|
|
90
|
+
onToolActivity?: (activity: ToolActivity) => void;
|
|
91
|
+
/** Called on streaming text deltas from the assistant response. */
|
|
92
|
+
onTextDelta?: (delta: string, fullText: string) => void;
|
|
93
|
+
/** Called when the agent session is created (for accessing session stats). */
|
|
94
|
+
onSessionCreated?: (session: AgentSession) => void;
|
|
95
|
+
/** Called at the end of each agentic turn with the cumulative count. */
|
|
96
|
+
onTurnEnd?: (turnCount: number) => void;
|
|
97
|
+
/** Called once per assistant message_end with that message's usage delta. */
|
|
98
|
+
onAssistantUsage?: (usage: { input: number; output: number; cacheWrite: number }) => void;
|
|
99
|
+
/** Called when the session successfully compacts. */
|
|
100
|
+
onCompaction?: (info: CompactionInfo) => void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface ResumeOptions {
|
|
104
|
+
signal?: AbortSignal;
|
|
105
|
+
onToolActivity?: (activity: ToolActivity) => void;
|
|
106
|
+
onAssistantUsage?: (usage: { input: number; output: number; cacheWrite: number }) => void;
|
|
107
|
+
onCompaction?: (info: CompactionInfo) => void;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class AgentManager {
|
|
111
|
+
private agents = new Map<string, AgentRecord>();
|
|
112
|
+
private cleanupInterval: ReturnType<typeof setInterval>;
|
|
113
|
+
private onComplete?: OnAgentComplete;
|
|
114
|
+
private onStart?: OnAgentStart;
|
|
115
|
+
private onCompact?: OnAgentCompact;
|
|
116
|
+
private maxConcurrent: number;
|
|
117
|
+
/** Base repos worktrees were created from — so dispose() can prune them all,
|
|
118
|
+
* not just the parent repo (caller-supplied cwd can target other repos). */
|
|
119
|
+
private worktreeRepos = new Set<string>();
|
|
120
|
+
|
|
121
|
+
/** Queue of background agents waiting to start. */
|
|
122
|
+
private queue: { id: string; args: SpawnArgs }[] = [];
|
|
123
|
+
/** Number of currently running background agents. */
|
|
124
|
+
private runningBackground = 0;
|
|
125
|
+
|
|
126
|
+
constructor(
|
|
127
|
+
onComplete?: OnAgentComplete,
|
|
128
|
+
maxConcurrent = DEFAULT_MAX_CONCURRENT,
|
|
129
|
+
onStart?: OnAgentStart,
|
|
130
|
+
onCompact?: OnAgentCompact,
|
|
131
|
+
) {
|
|
132
|
+
this.onComplete = onComplete;
|
|
133
|
+
this.onStart = onStart;
|
|
134
|
+
this.onCompact = onCompact;
|
|
135
|
+
this.maxConcurrent = maxConcurrent;
|
|
136
|
+
// Cleanup completed agents after 10 minutes (but keep sessions for resume)
|
|
137
|
+
this.cleanupInterval = setInterval(() => this.cleanup(), 60_000);
|
|
138
|
+
this.cleanupInterval.unref();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Update the max concurrent background agents limit. */
|
|
142
|
+
setMaxConcurrent(n: number) {
|
|
143
|
+
this.maxConcurrent = Math.max(1, n);
|
|
144
|
+
// Start queued agents if the new limit allows
|
|
145
|
+
this.drainQueue();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
getMaxConcurrent(): number {
|
|
149
|
+
return this.maxConcurrent;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Spawn an agent and return its ID immediately (for background use).
|
|
154
|
+
* If the concurrency limit is reached, the agent is queued.
|
|
155
|
+
*/
|
|
156
|
+
spawn(
|
|
157
|
+
pi: ExtensionAPI,
|
|
158
|
+
ctx: ExtensionContext,
|
|
159
|
+
type: SubagentType,
|
|
160
|
+
prompt: string,
|
|
161
|
+
options: SpawnOptions,
|
|
162
|
+
): string {
|
|
163
|
+
// Validate before the queue branch — a queued spawn should fail at the
|
|
164
|
+
// call, not minutes later at drain. Throw (not warn): programmatic callers
|
|
165
|
+
// can fix and retry; the RPC layer converts throws into error envelopes.
|
|
166
|
+
assertValidSpawnCwd(options.cwd);
|
|
167
|
+
const depth = options.depth ?? 1;
|
|
168
|
+
if (depth > MAX_RECURSIVE_DEPTH) {
|
|
169
|
+
throw new Error(`Cannot spawn agent: maximum recursive subagent depth is ${MAX_RECURSIVE_DEPTH}.`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const id = randomUUID().slice(0, 17);
|
|
173
|
+
const abortController = new AbortController();
|
|
174
|
+
const record: AgentRecord = {
|
|
175
|
+
id,
|
|
176
|
+
type,
|
|
177
|
+
description: options.description,
|
|
178
|
+
status: options.isBackground ? "queued" : "running",
|
|
179
|
+
toolUses: 0,
|
|
180
|
+
startedAt: Date.now(),
|
|
181
|
+
abortController,
|
|
182
|
+
lifetimeUsage: { input: 0, output: 0, cacheWrite: 0 },
|
|
183
|
+
compactionCount: 0,
|
|
184
|
+
invocation: options.invocation,
|
|
185
|
+
depth,
|
|
186
|
+
parentAgentId: options.parentAgentId,
|
|
187
|
+
};
|
|
188
|
+
this.agents.set(id, record);
|
|
189
|
+
|
|
190
|
+
const args: SpawnArgs = { pi, ctx, type, prompt, options };
|
|
191
|
+
|
|
192
|
+
if (options.isBackground && !options.bypassQueue && this.runningBackground >= this.maxConcurrent) {
|
|
193
|
+
// Queue it — will be started when a running agent completes
|
|
194
|
+
this.queue.push({ id, args });
|
|
195
|
+
return id;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// startAgent can throw (e.g. strict worktree-isolation failure) — clean
|
|
199
|
+
// up the record so callers don't see an orphan in `listAgents()`.
|
|
200
|
+
try {
|
|
201
|
+
this.startAgent(id, record, args);
|
|
202
|
+
} catch (err) {
|
|
203
|
+
this.agents.delete(id);
|
|
204
|
+
throw err;
|
|
205
|
+
}
|
|
206
|
+
return id;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Actually start an agent (called immediately or from queue drain). */
|
|
210
|
+
private startAgent(id: string, record: AgentRecord, { pi, ctx, type, prompt, options }: SpawnArgs) {
|
|
211
|
+
// Re-validate a caller-supplied cwd: queued spawns can start minutes after
|
|
212
|
+
// spawn()'s check, and the directory may be gone by then (TOCTOU). Same
|
|
213
|
+
// curated errors; drainQueue parks a throw on the record as an error.
|
|
214
|
+
assertValidSpawnCwd(options.cwd);
|
|
215
|
+
// Single resolution point for the caller-supplied cwd — the worktree base
|
|
216
|
+
// repo and both cleanup calls below MUST agree on this value forever.
|
|
217
|
+
const customCwd = options.cwd ?? undefined; // null (RPC "unset") → undefined
|
|
218
|
+
const baseCwd = customCwd ?? ctx.cwd;
|
|
219
|
+
|
|
220
|
+
// Worktree isolation: try to create a temporary git worktree. Strict —
|
|
221
|
+
// fail loud if not possible (no silent fallback to main tree). Done
|
|
222
|
+
// BEFORE state mutation so a throw doesn't leave the record half-running.
|
|
223
|
+
let worktreeCwd: string | undefined;
|
|
224
|
+
if (options.isolation === "worktree") {
|
|
225
|
+
const wt = createWorktree(baseCwd, id);
|
|
226
|
+
if (!wt) {
|
|
227
|
+
throw new Error(
|
|
228
|
+
'Cannot run with isolation: "worktree" — not a git repo, no commits yet, or `git worktree add` failed. ' +
|
|
229
|
+
'Initialize git and commit at least once, or omit `isolation`.',
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
record.worktree = wt;
|
|
233
|
+
// workPath preserves subdirectory scoping for caller-supplied cwds: a
|
|
234
|
+
// cwd deep in a monorepo maps to the same subdir inside the copy, not
|
|
235
|
+
// the copied repo's root. Plain worktree spawns keep the historical
|
|
236
|
+
// behavior (agent at the copy's root) — moving them to workPath would
|
|
237
|
+
// also move .pi config discovery when the parent session sits in a repo
|
|
238
|
+
// subdirectory, silently dropping extensions/skills.
|
|
239
|
+
worktreeCwd = customCwd !== undefined ? wt.workPath : wt.path;
|
|
240
|
+
this.worktreeRepos.add(baseCwd);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
record.status = "running";
|
|
244
|
+
record.startedAt = Date.now();
|
|
245
|
+
if (options.isBackground) this.runningBackground++;
|
|
246
|
+
this.onStart?.(record);
|
|
247
|
+
|
|
248
|
+
// Wire parent abort signal to stop the subagent when the parent is interrupted
|
|
249
|
+
let detachParentSignal: (() => void) | undefined;
|
|
250
|
+
if (options.signal) {
|
|
251
|
+
const onParentAbort = () => this.abort(id);
|
|
252
|
+
options.signal.addEventListener("abort", onParentAbort, { once: true });
|
|
253
|
+
detachParentSignal = () => options.signal!.removeEventListener("abort", onParentAbort);
|
|
254
|
+
}
|
|
255
|
+
const detach = () => { detachParentSignal?.(); detachParentSignal = undefined; };
|
|
256
|
+
|
|
257
|
+
const promise = runAgent(ctx, type, prompt, {
|
|
258
|
+
pi,
|
|
259
|
+
agentId: id,
|
|
260
|
+
model: options.model,
|
|
261
|
+
maxTurns: options.maxTurns,
|
|
262
|
+
isolated: options.isolated,
|
|
263
|
+
inheritContext: options.inheritContext,
|
|
264
|
+
thinkingLevel: options.thinkingLevel,
|
|
265
|
+
// Worktree wins for the working dir (the agent must run in the copy —
|
|
266
|
+
// which, with a custom cwd, was created from that target). Config stays
|
|
267
|
+
// with the parent project when a caller-supplied cwd is in play; it must
|
|
268
|
+
// stay undefined otherwise so plain worktree runs keep resolving config
|
|
269
|
+
// (incl. relative extension paths and memory) inside the worktree copy.
|
|
270
|
+
cwd: worktreeCwd ?? customCwd,
|
|
271
|
+
configCwd: customCwd !== undefined ? ctx.cwd : undefined,
|
|
272
|
+
signal: record.abortController!.signal,
|
|
273
|
+
onToolActivity: (activity) => {
|
|
274
|
+
if (activity.type === "end") record.toolUses++;
|
|
275
|
+
options.onToolActivity?.(activity);
|
|
276
|
+
},
|
|
277
|
+
onTurnEnd: options.onTurnEnd,
|
|
278
|
+
onTextDelta: options.onTextDelta,
|
|
279
|
+
onAssistantUsage: (usage) => {
|
|
280
|
+
addUsage(record.lifetimeUsage, usage);
|
|
281
|
+
options.onAssistantUsage?.(usage);
|
|
282
|
+
},
|
|
283
|
+
onCompaction: (info) => {
|
|
284
|
+
record.compactionCount++;
|
|
285
|
+
this.onCompact?.(record, info);
|
|
286
|
+
options.onCompaction?.(info);
|
|
287
|
+
},
|
|
288
|
+
depth: record.depth,
|
|
289
|
+
parentAgentId: record.parentAgentId,
|
|
290
|
+
onSessionCreated: (session) => {
|
|
291
|
+
record.session = session;
|
|
292
|
+
// Flush any steers that arrived before the session was ready
|
|
293
|
+
if (record.pendingSteers?.length) {
|
|
294
|
+
for (const msg of record.pendingSteers) {
|
|
295
|
+
session.steer(msg).catch(() => {});
|
|
296
|
+
}
|
|
297
|
+
record.pendingSteers = undefined;
|
|
298
|
+
}
|
|
299
|
+
options.onSessionCreated?.(session);
|
|
300
|
+
},
|
|
301
|
+
})
|
|
302
|
+
.then(({ responseText, session, aborted, steered }) => {
|
|
303
|
+
// Don't overwrite status if externally stopped via abort()
|
|
304
|
+
if (record.status !== "stopped") {
|
|
305
|
+
record.status = aborted ? "aborted" : steered ? "steered" : "completed";
|
|
306
|
+
}
|
|
307
|
+
record.result = responseText;
|
|
308
|
+
record.session = session;
|
|
309
|
+
record.completedAt ??= Date.now();
|
|
310
|
+
|
|
311
|
+
detach();
|
|
312
|
+
|
|
313
|
+
// Final flush of streaming output file
|
|
314
|
+
if (record.outputCleanup) {
|
|
315
|
+
try { record.outputCleanup(); } catch { /* ignore */ }
|
|
316
|
+
record.outputCleanup = undefined;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Clean up worktree if used
|
|
320
|
+
if (record.worktree) {
|
|
321
|
+
const wtResult = cleanupWorktree(baseCwd, record.worktree, options.description);
|
|
322
|
+
record.worktreeResult = wtResult;
|
|
323
|
+
if (wtResult.hasChanges && wtResult.branch) {
|
|
324
|
+
// With a caller-supplied cwd the branch lives in THAT repo, not the
|
|
325
|
+
// parent session's — say so, or the orchestrator merges in the wrong repo.
|
|
326
|
+
const repoNote = customCwd !== undefined ? ` in \`${baseCwd}\`` : "";
|
|
327
|
+
record.result = (record.result ?? "") +
|
|
328
|
+
`\n\n---\nChanges saved to branch \`${wtResult.branch}\`${repoNote}. Merge with: \`git merge ${wtResult.branch}\`${customCwd !== undefined ? ` (run in \`${baseCwd}\`)` : ""}`;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (options.isBackground) {
|
|
333
|
+
this.runningBackground--;
|
|
334
|
+
try { this.onComplete?.(record); } catch { /* ignore completion side-effect errors */ }
|
|
335
|
+
this.drainQueue();
|
|
336
|
+
}
|
|
337
|
+
return responseText;
|
|
338
|
+
})
|
|
339
|
+
.catch((err) => {
|
|
340
|
+
// Don't overwrite status if externally stopped via abort()
|
|
341
|
+
if (record.status !== "stopped") {
|
|
342
|
+
record.status = "error";
|
|
343
|
+
}
|
|
344
|
+
record.error = err instanceof Error ? err.message : String(err);
|
|
345
|
+
record.completedAt ??= Date.now();
|
|
346
|
+
|
|
347
|
+
detach();
|
|
348
|
+
|
|
349
|
+
// Final flush of streaming output file on error
|
|
350
|
+
if (record.outputCleanup) {
|
|
351
|
+
try { record.outputCleanup(); } catch { /* ignore */ }
|
|
352
|
+
record.outputCleanup = undefined;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Best-effort worktree cleanup on error
|
|
356
|
+
if (record.worktree) {
|
|
357
|
+
try {
|
|
358
|
+
const wtResult = cleanupWorktree(baseCwd, record.worktree, options.description);
|
|
359
|
+
record.worktreeResult = wtResult;
|
|
360
|
+
} catch { /* ignore cleanup errors */ }
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (options.isBackground) {
|
|
364
|
+
this.runningBackground--;
|
|
365
|
+
this.onComplete?.(record);
|
|
366
|
+
this.drainQueue();
|
|
367
|
+
}
|
|
368
|
+
return "";
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
record.promise = promise;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Start queued agents up to the concurrency limit. */
|
|
375
|
+
private drainQueue() {
|
|
376
|
+
while (this.queue.length > 0 && this.runningBackground < this.maxConcurrent) {
|
|
377
|
+
const next = this.queue.shift()!;
|
|
378
|
+
const record = this.agents.get(next.id);
|
|
379
|
+
if (!record || record.status !== "queued") continue;
|
|
380
|
+
try {
|
|
381
|
+
this.startAgent(next.id, record, next.args);
|
|
382
|
+
} catch (err) {
|
|
383
|
+
// Late failure (e.g. strict worktree-isolation) — surface on the record
|
|
384
|
+
// so the user/agent can see it via /agents, then keep draining.
|
|
385
|
+
record.status = "error";
|
|
386
|
+
record.error = err instanceof Error ? err.message : String(err);
|
|
387
|
+
record.completedAt = Date.now();
|
|
388
|
+
this.onComplete?.(record);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Spawn an agent and wait for completion (foreground use).
|
|
395
|
+
* Foreground agents bypass the concurrency queue.
|
|
396
|
+
*/
|
|
397
|
+
async spawnAndWait(
|
|
398
|
+
pi: ExtensionAPI,
|
|
399
|
+
ctx: ExtensionContext,
|
|
400
|
+
type: SubagentType,
|
|
401
|
+
prompt: string,
|
|
402
|
+
options: Omit<SpawnOptions, "isBackground">,
|
|
403
|
+
): Promise<AgentRecord> {
|
|
404
|
+
const id = this.spawn(pi, ctx, type, prompt, { ...options, isBackground: false });
|
|
405
|
+
const record = this.agents.get(id)!;
|
|
406
|
+
await record.promise;
|
|
407
|
+
return record;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Resume an existing agent session in the background. */
|
|
411
|
+
resume(
|
|
412
|
+
id: string,
|
|
413
|
+
prompt: string,
|
|
414
|
+
signalOrOptions?: AbortSignal | ResumeOptions,
|
|
415
|
+
): AgentRecord | undefined {
|
|
416
|
+
const record = this.agents.get(id);
|
|
417
|
+
if (!record?.session) return undefined;
|
|
418
|
+
const options = typeof (signalOrOptions as AbortSignal | undefined)?.addEventListener === "function"
|
|
419
|
+
? { signal: signalOrOptions as AbortSignal }
|
|
420
|
+
: (signalOrOptions ?? {}) as ResumeOptions;
|
|
421
|
+
|
|
422
|
+
record.status = "running";
|
|
423
|
+
record.startedAt = Date.now();
|
|
424
|
+
record.completedAt = undefined;
|
|
425
|
+
record.result = undefined;
|
|
426
|
+
record.error = undefined;
|
|
427
|
+
record.resultConsumed = false;
|
|
428
|
+
record.abortController = new AbortController();
|
|
429
|
+
this.runningBackground++;
|
|
430
|
+
this.onStart?.(record);
|
|
431
|
+
|
|
432
|
+
const onParentAbort = () => this.abort(id);
|
|
433
|
+
options.signal?.addEventListener("abort", onParentAbort, { once: true });
|
|
434
|
+
const detach = () => options.signal?.removeEventListener("abort", onParentAbort);
|
|
435
|
+
|
|
436
|
+
const promise = resumeAgent(record.session, prompt, {
|
|
437
|
+
onToolActivity: (activity) => {
|
|
438
|
+
if (activity.type === "end") record.toolUses++;
|
|
439
|
+
options.onToolActivity?.(activity);
|
|
440
|
+
},
|
|
441
|
+
onAssistantUsage: (usage) => {
|
|
442
|
+
addUsage(record.lifetimeUsage, usage);
|
|
443
|
+
options.onAssistantUsage?.(usage);
|
|
444
|
+
},
|
|
445
|
+
onCompaction: (info) => {
|
|
446
|
+
record.compactionCount++;
|
|
447
|
+
this.onCompact?.(record, info);
|
|
448
|
+
options.onCompaction?.(info);
|
|
449
|
+
},
|
|
450
|
+
signal: record.abortController.signal,
|
|
451
|
+
}).then((responseText) => {
|
|
452
|
+
if (record.status !== "stopped") {
|
|
453
|
+
record.status = "completed";
|
|
454
|
+
}
|
|
455
|
+
record.result = responseText;
|
|
456
|
+
record.completedAt = Date.now();
|
|
457
|
+
detach();
|
|
458
|
+
this.runningBackground--;
|
|
459
|
+
try { this.onComplete?.(record); } catch { /* ignore completion side-effect errors */ }
|
|
460
|
+
this.drainQueue();
|
|
461
|
+
return responseText;
|
|
462
|
+
}).catch((err) => {
|
|
463
|
+
if (record.status !== "stopped") record.status = "error";
|
|
464
|
+
record.error = err instanceof Error ? err.message : String(err);
|
|
465
|
+
record.completedAt = Date.now();
|
|
466
|
+
detach();
|
|
467
|
+
this.runningBackground--;
|
|
468
|
+
try { this.onComplete?.(record); } catch { /* ignore completion side-effect errors */ }
|
|
469
|
+
this.drainQueue();
|
|
470
|
+
return "";
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
record.promise = promise;
|
|
474
|
+
return record;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
getRecord(id: string): AgentRecord | undefined {
|
|
478
|
+
return this.agents.get(id);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
listAgents(): AgentRecord[] {
|
|
482
|
+
return [...this.agents.values()].sort(
|
|
483
|
+
(a, b) => b.startedAt - a.startedAt,
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
abort(id: string): boolean {
|
|
488
|
+
const record = this.agents.get(id);
|
|
489
|
+
if (!record) return false;
|
|
490
|
+
|
|
491
|
+
// Remove from queue if queued
|
|
492
|
+
if (record.status === "queued") {
|
|
493
|
+
this.queue = this.queue.filter(q => q.id !== id);
|
|
494
|
+
record.status = "stopped";
|
|
495
|
+
record.completedAt = Date.now();
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (record.status !== "running") return false;
|
|
500
|
+
record.abortController?.abort();
|
|
501
|
+
record.status = "stopped";
|
|
502
|
+
record.completedAt = Date.now();
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** Dispose a record's session and remove it from the map. */
|
|
507
|
+
private removeRecord(id: string, record: AgentRecord): void {
|
|
508
|
+
record.session?.dispose?.();
|
|
509
|
+
record.session = undefined;
|
|
510
|
+
this.agents.delete(id);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
private cleanup() {
|
|
514
|
+
const cutoff = Date.now() - 10 * 60_000;
|
|
515
|
+
for (const [id, record] of this.agents) {
|
|
516
|
+
if (record.status === "running" || record.status === "queued") continue;
|
|
517
|
+
if ((record.completedAt ?? 0) >= cutoff) continue;
|
|
518
|
+
this.removeRecord(id, record);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Remove all completed/stopped/errored records immediately.
|
|
524
|
+
* Called on session start/switch so tasks from a prior session don't persist.
|
|
525
|
+
*/
|
|
526
|
+
clearCompleted(): void {
|
|
527
|
+
for (const [id, record] of this.agents) {
|
|
528
|
+
if (record.status === "running" || record.status === "queued") continue;
|
|
529
|
+
this.removeRecord(id, record);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** Whether any agents are still running or queued. */
|
|
534
|
+
hasRunning(): boolean {
|
|
535
|
+
return [...this.agents.values()].some(
|
|
536
|
+
r => r.status === "running" || r.status === "queued",
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/** Abort all running and queued agents immediately. */
|
|
541
|
+
abortAll(): number {
|
|
542
|
+
let count = 0;
|
|
543
|
+
// Clear queued agents first
|
|
544
|
+
for (const queued of this.queue) {
|
|
545
|
+
const record = this.agents.get(queued.id);
|
|
546
|
+
if (record) {
|
|
547
|
+
record.status = "stopped";
|
|
548
|
+
record.completedAt = Date.now();
|
|
549
|
+
count++;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
this.queue = [];
|
|
553
|
+
// Abort running agents
|
|
554
|
+
for (const record of this.agents.values()) {
|
|
555
|
+
if (record.status === "running") {
|
|
556
|
+
record.abortController?.abort();
|
|
557
|
+
record.status = "stopped";
|
|
558
|
+
record.completedAt = Date.now();
|
|
559
|
+
count++;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return count;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** Wait for all running and queued agents to complete (including queued ones). */
|
|
566
|
+
async waitForAll(): Promise<void> {
|
|
567
|
+
// Loop because drainQueue respects the concurrency limit — as running
|
|
568
|
+
// agents finish they start queued ones, which need awaiting too.
|
|
569
|
+
while (true) {
|
|
570
|
+
this.drainQueue();
|
|
571
|
+
const pending = [...this.agents.values()]
|
|
572
|
+
.filter(r => r.status === "running" || r.status === "queued")
|
|
573
|
+
.map(r => r.promise)
|
|
574
|
+
.filter(Boolean);
|
|
575
|
+
if (pending.length === 0) break;
|
|
576
|
+
await Promise.allSettled(pending);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
dispose() {
|
|
581
|
+
clearInterval(this.cleanupInterval);
|
|
582
|
+
// Clear queue
|
|
583
|
+
this.queue = [];
|
|
584
|
+
for (const record of this.agents.values()) {
|
|
585
|
+
record.session?.dispose();
|
|
586
|
+
}
|
|
587
|
+
this.agents.clear();
|
|
588
|
+
// Prune any orphaned git worktrees (crash recovery)
|
|
589
|
+
try { pruneWorktrees(process.cwd()); } catch { /* ignore */ }
|
|
590
|
+
// Also prune repos that caller-supplied cwds created worktrees in — a clean
|
|
591
|
+
// exit with in-flight agents would otherwise leave stale registrations there.
|
|
592
|
+
for (const repo of this.worktreeRepos) {
|
|
593
|
+
try { pruneWorktrees(repo); } catch { /* ignore */ }
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|