@xynogen/pix-subagent 0.1.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.
- package/LICENSE +27 -0
- package/README.md +114 -0
- package/package.json +46 -0
- package/src/agent-manager.ts +526 -0
- package/src/agent-runner.ts +849 -0
- package/src/agent-types.ts +152 -0
- package/src/context.ts +59 -0
- package/src/custom-agents.ts +165 -0
- package/src/default-agents.ts +126 -0
- package/src/env.ts +43 -0
- package/src/extension.ts +9 -0
- package/src/index.ts +216 -0
- package/src/invocation-config.ts +43 -0
- package/src/model-resolver.ts +98 -0
- package/src/once.ts +26 -0
- package/src/prompts.ts +111 -0
- package/src/tools.ts +822 -0
- package/src/types.ts +126 -0
- package/src/ui/notification.ts +75 -0
- package/src/ui/widget.ts +490 -0
- package/src/usage.ts +71 -0
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-runner.ts — Core execution engine: creates sessions, runs agents, collects results.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { basename, dirname, isAbsolute, resolve } from "node:path";
|
|
7
|
+
import type { Model } from "@earendil-works/pi-ai";
|
|
8
|
+
import type {
|
|
9
|
+
ExtensionContext,
|
|
10
|
+
LoadExtensionsResult,
|
|
11
|
+
} from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import {
|
|
13
|
+
type AgentSession,
|
|
14
|
+
type AgentSessionEvent,
|
|
15
|
+
createAgentSession,
|
|
16
|
+
DefaultResourceLoader,
|
|
17
|
+
type ExtensionAPI,
|
|
18
|
+
getAgentDir,
|
|
19
|
+
SessionManager,
|
|
20
|
+
SettingsManager,
|
|
21
|
+
} from "@earendil-works/pi-coding-agent";
|
|
22
|
+
import {
|
|
23
|
+
BUILTIN_TOOL_NAMES,
|
|
24
|
+
getAgentConfig,
|
|
25
|
+
getConfig,
|
|
26
|
+
getToolNamesForType,
|
|
27
|
+
} from "./agent-types.ts";
|
|
28
|
+
import { buildParentContext, extractText } from "./context.ts";
|
|
29
|
+
import { DEFAULT_AGENTS } from "./default-agents.ts";
|
|
30
|
+
import { detectEnv } from "./env.ts";
|
|
31
|
+
import { buildAgentPrompt, type PromptExtras } from "./prompts.ts";
|
|
32
|
+
import type { SubagentType, ThinkingLevel } from "./types.ts";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Tool names registered by THIS extension. Single source of truth so the
|
|
36
|
+
* registration sites (index.ts) and the subagent exclusion list below can't
|
|
37
|
+
* drift apart. These are our own tools, not pi built-ins, so they can't be
|
|
38
|
+
* derived from pi — but they only need defining once.
|
|
39
|
+
*/
|
|
40
|
+
export const SUBAGENT_TOOL_NAMES = {
|
|
41
|
+
AGENT: "agent",
|
|
42
|
+
GET_RESULT: "agent_result",
|
|
43
|
+
STEER: "agent_steer",
|
|
44
|
+
} as const;
|
|
45
|
+
|
|
46
|
+
/** Names of tools registered by this extension that subagents must NOT inherit. */
|
|
47
|
+
const EXCLUDED_TOOL_NAMES: string[] = Object.values(SUBAGENT_TOOL_NAMES);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Canonical name of an extension for `extensions: [...]` allowlist matching.
|
|
51
|
+
* Lowercased — extension names match case-insensitively so `extensions: [Mcp]`
|
|
52
|
+
* resolves the same as `[mcp]`. Tool names within `ext:foo/bar` are not affected.
|
|
53
|
+
* Directory extensions (`foo/index.ts`) resolve to the parent directory name;
|
|
54
|
+
* single-file extensions to the basename minus `.ts`/`.js`.
|
|
55
|
+
*/
|
|
56
|
+
export function extensionCanonicalName(extPath: string): string {
|
|
57
|
+
const base = basename(extPath);
|
|
58
|
+
const name =
|
|
59
|
+
base === "index.ts" || base === "index.js"
|
|
60
|
+
? basename(dirname(extPath))
|
|
61
|
+
: base.replace(/\.(ts|js)$/, "");
|
|
62
|
+
return name.toLowerCase();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Classify `extensions: string[]` frontmatter entries for the loader-level filter.
|
|
67
|
+
*
|
|
68
|
+
* An entry is a PATH iff it contains a path separator or starts with `~`; otherwise
|
|
69
|
+
* it is a NAME. `"*"` sets the wildcard flag (keep all default-discovered extensions).
|
|
70
|
+
*
|
|
71
|
+
* Path entries are resolved (`~` expanded, made absolute against `cwd`) into `paths`
|
|
72
|
+
* — and their canonical name is also added to `names`. The loader override matches
|
|
73
|
+
* everything by canonical name, so path-loaded extensions are matched via their name
|
|
74
|
+
* rather than their post-staging `Extension.path`.
|
|
75
|
+
*/
|
|
76
|
+
export function parseExtensionsSpec(
|
|
77
|
+
entries: string[],
|
|
78
|
+
cwd: string,
|
|
79
|
+
): { names: Set<string>; paths: string[]; wildcard: boolean } {
|
|
80
|
+
const names = new Set<string>();
|
|
81
|
+
const paths: string[] = [];
|
|
82
|
+
let wildcard = false;
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
if (!entry) continue;
|
|
85
|
+
if (entry === "*") {
|
|
86
|
+
wildcard = true;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const isPathEntry =
|
|
90
|
+
entry.includes("/") || entry.includes("\\") || entry.startsWith("~");
|
|
91
|
+
if (!isPathEntry) {
|
|
92
|
+
names.add(entry.toLowerCase());
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
let p = entry;
|
|
96
|
+
if (p === "~" || p.startsWith("~/") || p.startsWith("~\\")) {
|
|
97
|
+
p = homedir() + p.slice(1);
|
|
98
|
+
}
|
|
99
|
+
const abs = isAbsolute(p) ? p : resolve(cwd, p);
|
|
100
|
+
paths.push(abs);
|
|
101
|
+
names.add(extensionCanonicalName(abs));
|
|
102
|
+
}
|
|
103
|
+
return { names, paths, wildcard };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Parse raw `ext:` selector strings (from the `tools:` CSV) into the set of
|
|
108
|
+
* extension names to keep loaded and a per-extension tool-narrowing map.
|
|
109
|
+
*
|
|
110
|
+
* `ext:foo` → `extNames` has `foo`, no narrowing entry (all of foo's tools).
|
|
111
|
+
* `ext:foo/bar` → `extNames` has `foo`, `narrowing.foo` has `bar` (only `bar`).
|
|
112
|
+
* A name lands in `narrowing` only when a `/tool` form is seen, so a bare
|
|
113
|
+
* `ext:foo` alongside `ext:foo/bar` leaves narrowing in effect (narrowing wins).
|
|
114
|
+
* The split is on the first `/`; extension canonical names never contain `/`.
|
|
115
|
+
*/
|
|
116
|
+
export function parseExtSelectors(entries: string[]): {
|
|
117
|
+
extNames: Set<string>;
|
|
118
|
+
narrowing: Map<string, Set<string>>;
|
|
119
|
+
} {
|
|
120
|
+
const extNames = new Set<string>();
|
|
121
|
+
const narrowing = new Map<string, Set<string>>();
|
|
122
|
+
for (const raw of entries) {
|
|
123
|
+
if (!raw) continue;
|
|
124
|
+
const body = raw.slice("ext:".length);
|
|
125
|
+
const slash = body.indexOf("/");
|
|
126
|
+
// Extension name matches case-insensitively (matches the loader-side canonical
|
|
127
|
+
// name). Tool names are case-preserved — they're matched against pi-mono's
|
|
128
|
+
// registered identifiers, which are case-sensitive.
|
|
129
|
+
const name = (slash === -1 ? body : body.slice(0, slash))
|
|
130
|
+
.trim()
|
|
131
|
+
.toLowerCase();
|
|
132
|
+
if (!name) continue;
|
|
133
|
+
extNames.add(name);
|
|
134
|
+
if (slash === -1) continue;
|
|
135
|
+
const tool = body.slice(slash + 1).trim();
|
|
136
|
+
if (!tool) continue;
|
|
137
|
+
let set = narrowing.get(name);
|
|
138
|
+
if (!set) {
|
|
139
|
+
set = new Set();
|
|
140
|
+
narrowing.set(name, set);
|
|
141
|
+
}
|
|
142
|
+
set.add(tool);
|
|
143
|
+
}
|
|
144
|
+
return { extNames, narrowing };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Default max turns. undefined = unlimited (no turn limit). */
|
|
148
|
+
let defaultMaxTurns: number | undefined;
|
|
149
|
+
|
|
150
|
+
/** Normalize max turns. undefined or 0 = unlimited, otherwise minimum 1. */
|
|
151
|
+
export function normalizeMaxTurns(n: number | undefined): number | undefined {
|
|
152
|
+
if (n == null || n === 0) return undefined;
|
|
153
|
+
return Math.max(1, n);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Get the default max turns value. undefined = unlimited. */
|
|
157
|
+
export function getDefaultMaxTurns(): number | undefined {
|
|
158
|
+
return defaultMaxTurns;
|
|
159
|
+
}
|
|
160
|
+
/** Set the default max turns value. undefined or 0 = unlimited, otherwise minimum 1. */
|
|
161
|
+
export function setDefaultMaxTurns(n: number | undefined): void {
|
|
162
|
+
defaultMaxTurns = normalizeMaxTurns(n);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Additional turns allowed after the soft limit steer message. */
|
|
166
|
+
let graceTurns = 5;
|
|
167
|
+
|
|
168
|
+
/** Get the grace turns value. */
|
|
169
|
+
export function getGraceTurns(): number {
|
|
170
|
+
return graceTurns;
|
|
171
|
+
}
|
|
172
|
+
/** Set the grace turns value (minimum 1). */
|
|
173
|
+
export function setGraceTurns(n: number): void {
|
|
174
|
+
graceTurns = Math.max(1, n);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Try to find the right model for an agent type.
|
|
179
|
+
* Priority: explicit option > config.model > parent model.
|
|
180
|
+
*/
|
|
181
|
+
function resolveDefaultModel(
|
|
182
|
+
parentModel: Model<any> | undefined,
|
|
183
|
+
registry: {
|
|
184
|
+
find(provider: string, modelId: string): Model<any> | undefined;
|
|
185
|
+
getAvailable?(): Model<any>[];
|
|
186
|
+
},
|
|
187
|
+
configModel?: string,
|
|
188
|
+
): Model<any> | undefined {
|
|
189
|
+
if (configModel) {
|
|
190
|
+
const slashIdx = configModel.indexOf("/");
|
|
191
|
+
if (slashIdx !== -1) {
|
|
192
|
+
const provider = configModel.slice(0, slashIdx);
|
|
193
|
+
const modelId = configModel.slice(slashIdx + 1);
|
|
194
|
+
|
|
195
|
+
// Build a set of available model keys for fast lookup
|
|
196
|
+
const available = registry.getAvailable?.();
|
|
197
|
+
const availableKeys = available
|
|
198
|
+
? new Set(available.map((m: any) => `${m.provider}/${m.id}`))
|
|
199
|
+
: undefined;
|
|
200
|
+
const isAvailable = (p: string, id: string) =>
|
|
201
|
+
!availableKeys || availableKeys.has(`${p}/${id}`);
|
|
202
|
+
|
|
203
|
+
const found = registry.find(provider, modelId);
|
|
204
|
+
if (found && isAvailable(provider, modelId)) return found;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return parentModel;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Info about a tool event in the subagent. */
|
|
212
|
+
export interface ToolActivity {
|
|
213
|
+
type: "start" | "end";
|
|
214
|
+
toolName: string;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface RunOptions {
|
|
218
|
+
/** ExtensionAPI instance — used for pi.exec() instead of execSync. */
|
|
219
|
+
pi: ExtensionAPI;
|
|
220
|
+
/** Manager-assigned id; suffixes session name to disambiguate parallel spawns (e.g. `Explore#a1b2c3d4`). */
|
|
221
|
+
agentId?: string;
|
|
222
|
+
model?: Model<any>;
|
|
223
|
+
maxTurns?: number;
|
|
224
|
+
signal?: AbortSignal;
|
|
225
|
+
isolated?: boolean;
|
|
226
|
+
inheritContext?: boolean;
|
|
227
|
+
thinkingLevel?: ThinkingLevel;
|
|
228
|
+
/** Override working directory (e.g. for worktree isolation). */
|
|
229
|
+
cwd?: string;
|
|
230
|
+
/**
|
|
231
|
+
* Where .pi config is discovered (project extensions, skills, pi settings,
|
|
232
|
+
* agent memory). Default: same as the working directory. The manager sets
|
|
233
|
+
* this to the parent session's cwd when `SpawnOptions.cwd` points the
|
|
234
|
+
* working directory elsewhere — the agent works *there* but carries the
|
|
235
|
+
* parent project's config (the target's `.pi` extensions never execute).
|
|
236
|
+
*
|
|
237
|
+
* WARNING for future callers: if you pass `cwd` pointing at a directory the
|
|
238
|
+
* user didn't open, you almost certainly must pass `configCwd` too —
|
|
239
|
+
* omitting it makes the target's `.pi` extensions execute in this process.
|
|
240
|
+
* (Worktree isolation is the one intentional exception: its copy IS the
|
|
241
|
+
* parent's repo, so config resolving inside it is correct.)
|
|
242
|
+
*/
|
|
243
|
+
configCwd?: string;
|
|
244
|
+
/** Called on tool start/end with activity info. */
|
|
245
|
+
onToolActivity?: (activity: ToolActivity) => void;
|
|
246
|
+
/** Called on streaming text deltas from the assistant response. */
|
|
247
|
+
onTextDelta?: (delta: string, fullText: string) => void;
|
|
248
|
+
onSessionCreated?: (session: AgentSession) => void;
|
|
249
|
+
/** Called at the end of each agentic turn with the cumulative count. */
|
|
250
|
+
onTurnEnd?: (turnCount: number) => void;
|
|
251
|
+
/**
|
|
252
|
+
* Called once per assistant message_end with that message's usage delta.
|
|
253
|
+
* Lets callers maintain a lifetime accumulator that survives compaction
|
|
254
|
+
* (which replaces session.state.messages and resets stats-derived sums).
|
|
255
|
+
*/
|
|
256
|
+
onAssistantUsage?: (usage: {
|
|
257
|
+
input: number;
|
|
258
|
+
output: number;
|
|
259
|
+
cacheWrite: number;
|
|
260
|
+
}) => void;
|
|
261
|
+
/**
|
|
262
|
+
* Called when the session successfully compacts. `tokensBefore` is upstream's
|
|
263
|
+
* pre-compaction context size estimate. Aborted compactions don't fire.
|
|
264
|
+
*/
|
|
265
|
+
onCompaction?: (info: {
|
|
266
|
+
reason: "manual" | "threshold" | "overflow";
|
|
267
|
+
tokensBefore: number;
|
|
268
|
+
}) => void;
|
|
269
|
+
/**
|
|
270
|
+
* Caller-supplied tool-name subset — intersected with the resolved builtin+extension
|
|
271
|
+
* set (never widens). Omit to use the agent type's full default set.
|
|
272
|
+
*/
|
|
273
|
+
allowedToolNames?: string[];
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Intersect resolved tools with caller allowlist. Omitting allow → resolved unchanged. */
|
|
277
|
+
export function narrowTools(resolved: string[], allow?: string[]): string[] {
|
|
278
|
+
if (!allow) return resolved;
|
|
279
|
+
const allowed = new Set(allow);
|
|
280
|
+
return resolved.filter((t) => allowed.has(t));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export interface RunResult {
|
|
284
|
+
responseText: string;
|
|
285
|
+
session: AgentSession;
|
|
286
|
+
/** True if the agent was hard-aborted (max_turns + grace exceeded). */
|
|
287
|
+
aborted: boolean;
|
|
288
|
+
/** True if the agent was steered to wrap up (hit soft turn limit) but finished in time. */
|
|
289
|
+
steered: boolean;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Subscribe to a session and collect the last assistant message text.
|
|
294
|
+
* Returns an object with a `getText()` getter and an `unsubscribe` function.
|
|
295
|
+
*/
|
|
296
|
+
function collectResponseText(session: AgentSession) {
|
|
297
|
+
let text = "";
|
|
298
|
+
const unsubscribe = session.subscribe((event: AgentSessionEvent) => {
|
|
299
|
+
if (event.type === "message_start") {
|
|
300
|
+
text = "";
|
|
301
|
+
}
|
|
302
|
+
if (
|
|
303
|
+
event.type === "message_update" &&
|
|
304
|
+
event.assistantMessageEvent.type === "text_delta"
|
|
305
|
+
) {
|
|
306
|
+
text += event.assistantMessageEvent.delta;
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
return { getText: () => text, unsubscribe };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Get the last assistant text from the completed session history. */
|
|
313
|
+
function getLastAssistantText(session: AgentSession): string {
|
|
314
|
+
for (let i = session.messages.length - 1; i >= 0; i--) {
|
|
315
|
+
const msg = session.messages[i];
|
|
316
|
+
if (msg.role !== "assistant") continue;
|
|
317
|
+
const text = extractText(msg.content).trim();
|
|
318
|
+
if (text) return text;
|
|
319
|
+
}
|
|
320
|
+
return "";
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Wire an AbortSignal to abort a session.
|
|
325
|
+
* Returns a cleanup function to remove the listener.
|
|
326
|
+
*/
|
|
327
|
+
function forwardAbortSignal(
|
|
328
|
+
session: AgentSession,
|
|
329
|
+
signal?: AbortSignal,
|
|
330
|
+
): () => void {
|
|
331
|
+
if (!signal) return () => {};
|
|
332
|
+
const onAbort = () => session.abort();
|
|
333
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
334
|
+
return () => signal.removeEventListener("abort", onAbort);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export async function runAgent(
|
|
338
|
+
ctx: ExtensionContext,
|
|
339
|
+
type: SubagentType,
|
|
340
|
+
prompt: string,
|
|
341
|
+
options: RunOptions,
|
|
342
|
+
): Promise<RunResult> {
|
|
343
|
+
const config = getConfig(type);
|
|
344
|
+
const agentConfig = getAgentConfig(type);
|
|
345
|
+
|
|
346
|
+
// Resolve working directory: worktree override > parent cwd
|
|
347
|
+
const effectiveCwd = options.cwd ?? ctx.cwd;
|
|
348
|
+
// Filesystem work happens in effectiveCwd; config discovery in configCwd.
|
|
349
|
+
// They differ only for SpawnOptions.cwd spawns (config stays with the parent).
|
|
350
|
+
const configCwd = options.configCwd ?? effectiveCwd;
|
|
351
|
+
|
|
352
|
+
const env = await detectEnv(options.pi, effectiveCwd);
|
|
353
|
+
|
|
354
|
+
// Get parent system prompt for append-mode agents
|
|
355
|
+
const parentSystemPrompt = ctx.getSystemPrompt();
|
|
356
|
+
|
|
357
|
+
// Build prompt extras (memory, skill preloading)
|
|
358
|
+
const extras: PromptExtras = {};
|
|
359
|
+
|
|
360
|
+
// Resolve extensions/skills: isolated overrides to false
|
|
361
|
+
const extensions = options.isolated ? false : config.extensions;
|
|
362
|
+
// Nulling excludes under isolated also suppresses the orphaned-exclude warning —
|
|
363
|
+
// isolation is an intentional override, not a misconfiguration.
|
|
364
|
+
const excludeExtensions = options.isolated
|
|
365
|
+
? undefined
|
|
366
|
+
: config.excludeExtensions;
|
|
367
|
+
const skills = options.isolated ? false : config.skills;
|
|
368
|
+
|
|
369
|
+
// ponytail: skill-preload + persistent memory deferred to v2
|
|
370
|
+
let toolNames = getToolNamesForType(type);
|
|
371
|
+
|
|
372
|
+
// allowed_tools[] narrowing: caller-supplied subset is intersected (never widens)
|
|
373
|
+
if (options.allowedToolNames) {
|
|
374
|
+
toolNames = narrowTools(toolNames, options.allowedToolNames);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Build system prompt from agent config
|
|
378
|
+
let systemPrompt: string;
|
|
379
|
+
if (agentConfig) {
|
|
380
|
+
systemPrompt = buildAgentPrompt(
|
|
381
|
+
agentConfig,
|
|
382
|
+
effectiveCwd,
|
|
383
|
+
env,
|
|
384
|
+
parentSystemPrompt,
|
|
385
|
+
extras,
|
|
386
|
+
);
|
|
387
|
+
} else {
|
|
388
|
+
// Unknown type fallback: spread the canonical general-purpose config (defensive —
|
|
389
|
+
// unreachable in practice since index.ts resolves unknown types before calling runAgent).
|
|
390
|
+
const fallback = DEFAULT_AGENTS.get("general-purpose");
|
|
391
|
+
if (!fallback)
|
|
392
|
+
throw new Error(
|
|
393
|
+
`No fallback config available for unknown type "${type}"`,
|
|
394
|
+
);
|
|
395
|
+
systemPrompt = buildAgentPrompt(
|
|
396
|
+
{ ...fallback, name: type },
|
|
397
|
+
effectiveCwd,
|
|
398
|
+
env,
|
|
399
|
+
parentSystemPrompt,
|
|
400
|
+
extras,
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// When skills is string[], we've already preloaded them into the prompt.
|
|
405
|
+
// Still pass noSkills: true since we don't need the skill loader to load them again.
|
|
406
|
+
const noSkills = skills === false || Array.isArray(skills);
|
|
407
|
+
|
|
408
|
+
const agentDir = getAgentDir();
|
|
409
|
+
|
|
410
|
+
// Extension loading:
|
|
411
|
+
// - true → all default-discovered extensions
|
|
412
|
+
// - false → none (noExtensions)
|
|
413
|
+
// - string[] → loader-level allowlist. Bare names keep the matching
|
|
414
|
+
// default-discovered extension; path entries load that extension fresh;
|
|
415
|
+
// "*" keeps all default-discovered extensions. Excluded extensions never
|
|
416
|
+
// bind handlers or register tools (their factory still runs once).
|
|
417
|
+
//
|
|
418
|
+
// Suppress AGENTS.md/CLAUDE.md and APPEND_SYSTEM.md — upstream's
|
|
419
|
+
// buildSystemPrompt() re-appends both AFTER systemPromptOverride, which
|
|
420
|
+
// would defeat prompt_mode: replace and isolated: true. Parent context, if
|
|
421
|
+
// wanted, reaches the subagent via prompt_mode: append (parentSystemPrompt
|
|
422
|
+
// is embedded in systemPromptOverride) or inherit_context (conversation).
|
|
423
|
+
// `ext:` selectors from the `tools:` CSV narrow which extension tools surface to
|
|
424
|
+
// the LLM. They do NOT control loading — `extensions:` is the sole authority for
|
|
425
|
+
// which extensions load. `ext:foo` against an extension that `extensions:` excluded
|
|
426
|
+
// is an orphan and warns after reload. `isolated` means no extension tools at all.
|
|
427
|
+
const { extNames, narrowing } = parseExtSelectors(
|
|
428
|
+
options.isolated ? [] : (agentConfig?.extSelectors ?? []),
|
|
429
|
+
);
|
|
430
|
+
const noExtensions = extensions === false;
|
|
431
|
+
|
|
432
|
+
const extensionsSpec = Array.isArray(extensions)
|
|
433
|
+
? parseExtensionsSpec(extensions, configCwd)
|
|
434
|
+
: undefined;
|
|
435
|
+
const keepNames = extensionsSpec?.names ?? new Set<string>();
|
|
436
|
+
// `exclude_extensions:` is a denylist applied AFTER the include set — exclude wins.
|
|
437
|
+
// Plain canonical names only (case-insensitive). Note: excluded extensions'
|
|
438
|
+
// factories still run once during reload() (see comment above) — exclusion
|
|
439
|
+
// suppresses handler binding and tool registration; it is not a sandbox.
|
|
440
|
+
const excludeNames = new Set(
|
|
441
|
+
(excludeExtensions ?? []).map((n) => n.toLowerCase()),
|
|
442
|
+
);
|
|
443
|
+
const hasExcludes = excludeNames.size > 0;
|
|
444
|
+
// The override filters loaded extensions down to `keepNames` minus `excludeNames`.
|
|
445
|
+
// It's only needed when we're neither loading everything without excludes
|
|
446
|
+
// (`extensions: true` or a `"*"` wildcard) nor nothing (`noExtensions`).
|
|
447
|
+
const loadAll = extensions === true || extensionsSpec?.wildcard === true;
|
|
448
|
+
const additionalExtensionPaths = extensionsSpec?.paths.length
|
|
449
|
+
? extensionsSpec.paths
|
|
450
|
+
: undefined;
|
|
451
|
+
// Pre-filter discovered set, captured by the override — the exclude-typo warning
|
|
452
|
+
// must compare against this, not the surviving set (absence from survivors is
|
|
453
|
+
// an exclude *succeeding*).
|
|
454
|
+
let discoveredNames: Set<string> | undefined;
|
|
455
|
+
const extensionsOverride:
|
|
456
|
+
| ((base: LoadExtensionsResult) => LoadExtensionsResult)
|
|
457
|
+
| undefined =
|
|
458
|
+
noExtensions || (loadAll && !hasExcludes)
|
|
459
|
+
? undefined
|
|
460
|
+
: (base) => {
|
|
461
|
+
discoveredNames = new Set(
|
|
462
|
+
base.extensions.map((e) => extensionCanonicalName(e.path)),
|
|
463
|
+
);
|
|
464
|
+
return {
|
|
465
|
+
...base,
|
|
466
|
+
extensions: base.extensions.filter((e) => {
|
|
467
|
+
const name = extensionCanonicalName(e.path);
|
|
468
|
+
if (excludeNames.has(name)) return false; // exclude wins
|
|
469
|
+
return loadAll || keepNames.has(name);
|
|
470
|
+
}),
|
|
471
|
+
};
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
const loader = new DefaultResourceLoader({
|
|
475
|
+
cwd: configCwd,
|
|
476
|
+
agentDir,
|
|
477
|
+
noExtensions,
|
|
478
|
+
additionalExtensionPaths,
|
|
479
|
+
extensionsOverride,
|
|
480
|
+
noSkills,
|
|
481
|
+
noPromptTemplates: true,
|
|
482
|
+
noThemes: true,
|
|
483
|
+
noContextFiles: true,
|
|
484
|
+
systemPromptOverride: () => systemPrompt,
|
|
485
|
+
appendSystemPromptOverride: () => [],
|
|
486
|
+
});
|
|
487
|
+
await loader.reload();
|
|
488
|
+
|
|
489
|
+
// Plain entries in `tools:` are expected to be built-in names (extension tools
|
|
490
|
+
// go through `ext:`), so an unknown name there is unambiguously a typo. Previously
|
|
491
|
+
// this produced a silently broken agent (#75) — pi-mono accepted the bogus name
|
|
492
|
+
// into the allowlist, then dropped it at registration with no signal back.
|
|
493
|
+
if (agentConfig?.builtinToolNames?.length) {
|
|
494
|
+
const knownBuiltins = new Set(BUILTIN_TOOL_NAMES);
|
|
495
|
+
for (const name of agentConfig.builtinToolNames) {
|
|
496
|
+
if (!knownBuiltins.has(name)) {
|
|
497
|
+
options.onToolActivity?.({
|
|
498
|
+
type: "end",
|
|
499
|
+
toolName: `tools-error:tool "${name}" requested by agent "${type}" is not a known built-in`,
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// A subagent spawns mid-task, so a bad `extensions:`/`ext:` entry warns rather
|
|
506
|
+
// than aborts. Two distinct misconfigurations to catch:
|
|
507
|
+
// - `extensions: [foo]` but no extension named foo was discovered (typo or
|
|
508
|
+
// path that failed to load — path entries fold their canonical name into
|
|
509
|
+
// `keepNames`, so this covers them too).
|
|
510
|
+
// - `tools: ext:foo` but foo isn't in the loaded set (because `extensions:`
|
|
511
|
+
// didn't include it). Since v0.9, `ext:` no longer pulls extensions in;
|
|
512
|
+
// loading is `extensions:`-authoritative.
|
|
513
|
+
// An exclude_extensions: alongside extensions: false is contradictory — nothing
|
|
514
|
+
// loads, so there is nothing to exclude.
|
|
515
|
+
if (hasExcludes && noExtensions) {
|
|
516
|
+
options.onToolActivity?.({
|
|
517
|
+
type: "end",
|
|
518
|
+
toolName: `extension-error:exclude_extensions has no effect for agent "${type}" — extensions: false loads nothing`,
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
// Exclude typo check: compares against the PRE-filter discovered set (an excluded
|
|
522
|
+
// name absent from the surviving set is the exclude working as intended). Also
|
|
523
|
+
// flags path-like and "*" entries — excludes are plain names only.
|
|
524
|
+
if (hasExcludes && discoveredNames) {
|
|
525
|
+
for (const name of excludeNames) {
|
|
526
|
+
if (!discoveredNames.has(name)) {
|
|
527
|
+
options.onToolActivity?.({
|
|
528
|
+
type: "end",
|
|
529
|
+
toolName: `extension-error:exclude_extensions: "${name}" for agent "${type}" did not match any discovered extension`,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (keepNames.size > 0 || extNames.size > 0) {
|
|
535
|
+
const survivingNames = new Set(
|
|
536
|
+
loader
|
|
537
|
+
.getExtensions()
|
|
538
|
+
.extensions.map((e) => extensionCanonicalName(e.path)),
|
|
539
|
+
);
|
|
540
|
+
for (const name of keepNames) {
|
|
541
|
+
if (!survivingNames.has(name)) {
|
|
542
|
+
options.onToolActivity?.({
|
|
543
|
+
type: "end",
|
|
544
|
+
toolName: excludeNames.has(name)
|
|
545
|
+
? `extension-error:extension "${name}" is in both extensions: and exclude_extensions: for agent "${type}" — exclude wins`
|
|
546
|
+
: `extension-error:extension "${name}" requested by agent "${type}" was not loaded`,
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
for (const name of extNames) {
|
|
551
|
+
if (!survivingNames.has(name)) {
|
|
552
|
+
options.onToolActivity?.({
|
|
553
|
+
type: "end",
|
|
554
|
+
toolName: `extension-error:ext:${name} referenced by agent "${type}" but extension "${name}" is not loaded (check extensions:/exclude_extensions:)`,
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// Resolve model: explicit option > config.model > parent model
|
|
561
|
+
const model =
|
|
562
|
+
options.model ??
|
|
563
|
+
resolveDefaultModel(ctx.model, ctx.modelRegistry, agentConfig?.model);
|
|
564
|
+
|
|
565
|
+
// Resolve thinking level: explicit option > agent config > undefined (inherit)
|
|
566
|
+
const thinkingLevel = options.thinkingLevel ?? agentConfig?.thinking;
|
|
567
|
+
|
|
568
|
+
const disallowedSet = agentConfig?.disallowedTools
|
|
569
|
+
? new Set(agentConfig.disallowedTools)
|
|
570
|
+
: undefined;
|
|
571
|
+
|
|
572
|
+
// Enumerate extension-registered tool names from the loaded resource loader.
|
|
573
|
+
// Extensions populate `extension.tools` during `loader.reload()` and the set
|
|
574
|
+
// is stable afterwards — `bindExtensions` does not register new tools.
|
|
575
|
+
//
|
|
576
|
+
// Opt-in flip: when any `ext:` selector is present, extension tools become an
|
|
577
|
+
// explicit allowlist — a loaded extension not named by a selector contributes
|
|
578
|
+
// no tools (its handlers still ran), and `ext:foo/bar` narrows `foo` to `bar`.
|
|
579
|
+
const extensionToolNames: string[] = [];
|
|
580
|
+
if (!noExtensions) {
|
|
581
|
+
const optInActive = extNames.size > 0;
|
|
582
|
+
for (const extension of loader.getExtensions().extensions) {
|
|
583
|
+
const canon = extensionCanonicalName(extension.path);
|
|
584
|
+
if (optInActive && !extNames.has(canon)) continue;
|
|
585
|
+
const narrowed = narrowing.get(canon);
|
|
586
|
+
for (const toolName of extension.tools.keys()) {
|
|
587
|
+
if (narrowed && !narrowed.has(toolName)) continue;
|
|
588
|
+
extensionToolNames.push(toolName);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Build the master tool allowlist applied at session construction.
|
|
594
|
+
// pi-mono's `allowedToolNames` gates BOTH registration and the initial active
|
|
595
|
+
// set, so listing the exact final set here means the session is correctly
|
|
596
|
+
// scoped from the first instant — no post-construction narrowing required.
|
|
597
|
+
const builtinToolNameSet = new Set(toolNames);
|
|
598
|
+
const allowedToolNamesSet = options.allowedToolNames
|
|
599
|
+
? new Set(options.allowedToolNames)
|
|
600
|
+
: undefined;
|
|
601
|
+
const allowedTools = [...toolNames, ...extensionToolNames].filter((t) => {
|
|
602
|
+
if (EXCLUDED_TOOL_NAMES.includes(t)) return false;
|
|
603
|
+
if (disallowedSet?.has(t)) return false;
|
|
604
|
+
// allowed_tools[] intersection: extension tools are also subject to the caller allowlist
|
|
605
|
+
if (allowedToolNamesSet && !allowedToolNamesSet.has(t)) return false;
|
|
606
|
+
if (builtinToolNameSet.has(t)) return true;
|
|
607
|
+
return !noExtensions;
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
const sessionOpts: Parameters<typeof createAgentSession>[0] = {
|
|
611
|
+
cwd: effectiveCwd,
|
|
612
|
+
agentDir,
|
|
613
|
+
sessionManager: SessionManager.inMemory(effectiveCwd),
|
|
614
|
+
settingsManager: SettingsManager.create(configCwd, agentDir),
|
|
615
|
+
modelRegistry: ctx.modelRegistry,
|
|
616
|
+
model,
|
|
617
|
+
tools: allowedTools,
|
|
618
|
+
resourceLoader: loader,
|
|
619
|
+
};
|
|
620
|
+
if (thinkingLevel) {
|
|
621
|
+
sessionOpts.thinkingLevel = thinkingLevel;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const { session } = await createAgentSession(sessionOpts);
|
|
625
|
+
|
|
626
|
+
const baseSessionName = agentConfig?.name ?? type;
|
|
627
|
+
session.setSessionName(
|
|
628
|
+
options.agentId
|
|
629
|
+
? `${baseSessionName}#${options.agentId.slice(0, 8)}`
|
|
630
|
+
: baseSessionName,
|
|
631
|
+
);
|
|
632
|
+
|
|
633
|
+
// Bind extensions so that session_start fires and extensions can initialize
|
|
634
|
+
// (e.g. loading credentials, setting up state). Tool gating already happened
|
|
635
|
+
// at session construction via the `tools:` allowlist above — no separate
|
|
636
|
+
// post-bind filter is needed. All ExtensionBindings fields are optional.
|
|
637
|
+
await session.bindExtensions({
|
|
638
|
+
onError: (err) => {
|
|
639
|
+
options.onToolActivity?.({
|
|
640
|
+
type: "end",
|
|
641
|
+
toolName: `extension-error:${err.extensionPath}`,
|
|
642
|
+
});
|
|
643
|
+
},
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
options.onSessionCreated?.(session);
|
|
647
|
+
|
|
648
|
+
// Track turns for graceful max_turns enforcement
|
|
649
|
+
let turnCount = 0;
|
|
650
|
+
const maxTurns = normalizeMaxTurns(
|
|
651
|
+
options.maxTurns ?? agentConfig?.maxTurns ?? defaultMaxTurns,
|
|
652
|
+
);
|
|
653
|
+
let softLimitReached = false;
|
|
654
|
+
let aborted = false;
|
|
655
|
+
|
|
656
|
+
let currentMessageText = "";
|
|
657
|
+
const unsubTurns = session.subscribe((event: AgentSessionEvent) => {
|
|
658
|
+
if (event.type === "turn_end") {
|
|
659
|
+
turnCount++;
|
|
660
|
+
options.onTurnEnd?.(turnCount);
|
|
661
|
+
if (maxTurns != null) {
|
|
662
|
+
if (!softLimitReached && turnCount >= maxTurns) {
|
|
663
|
+
softLimitReached = true;
|
|
664
|
+
session.steer(
|
|
665
|
+
"You have reached your turn limit. Wrap up immediately — provide your final answer now.",
|
|
666
|
+
);
|
|
667
|
+
} else if (softLimitReached && turnCount >= maxTurns + graceTurns) {
|
|
668
|
+
aborted = true;
|
|
669
|
+
session.abort();
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (event.type === "message_start") {
|
|
674
|
+
currentMessageText = "";
|
|
675
|
+
}
|
|
676
|
+
if (
|
|
677
|
+
event.type === "message_update" &&
|
|
678
|
+
event.assistantMessageEvent.type === "text_delta"
|
|
679
|
+
) {
|
|
680
|
+
currentMessageText += event.assistantMessageEvent.delta;
|
|
681
|
+
options.onTextDelta?.(
|
|
682
|
+
event.assistantMessageEvent.delta,
|
|
683
|
+
currentMessageText,
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
if (event.type === "tool_execution_start") {
|
|
687
|
+
options.onToolActivity?.({ type: "start", toolName: event.toolName });
|
|
688
|
+
}
|
|
689
|
+
if (event.type === "tool_execution_end") {
|
|
690
|
+
options.onToolActivity?.({ type: "end", toolName: event.toolName });
|
|
691
|
+
}
|
|
692
|
+
if (event.type === "message_end" && event.message.role === "assistant") {
|
|
693
|
+
const u = (event.message as any).usage;
|
|
694
|
+
if (u)
|
|
695
|
+
options.onAssistantUsage?.({
|
|
696
|
+
input: u.input ?? 0,
|
|
697
|
+
output: u.output ?? 0,
|
|
698
|
+
cacheWrite: u.cacheWrite ?? 0,
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
if (event.type === "compaction_end" && !event.aborted && event.result) {
|
|
702
|
+
options.onCompaction?.({
|
|
703
|
+
reason: event.reason,
|
|
704
|
+
tokensBefore: event.result.tokensBefore,
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
const collector = collectResponseText(session);
|
|
710
|
+
const cleanupAbort = forwardAbortSignal(session, options.signal);
|
|
711
|
+
|
|
712
|
+
// Build the effective prompt: optionally prepend parent context
|
|
713
|
+
let effectivePrompt = prompt;
|
|
714
|
+
if (options.inheritContext) {
|
|
715
|
+
const parentContext = buildParentContext(ctx);
|
|
716
|
+
if (parentContext) {
|
|
717
|
+
effectivePrompt = parentContext + prompt;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
try {
|
|
722
|
+
await session.prompt(effectivePrompt);
|
|
723
|
+
} finally {
|
|
724
|
+
unsubTurns();
|
|
725
|
+
collector.unsubscribe();
|
|
726
|
+
cleanupAbort();
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
const responseText =
|
|
730
|
+
collector.getText().trim() || getLastAssistantText(session);
|
|
731
|
+
return { responseText, session, aborted, steered: softLimitReached };
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Send a new prompt to an existing session (resume).
|
|
736
|
+
*/
|
|
737
|
+
export async function resumeAgent(
|
|
738
|
+
session: AgentSession,
|
|
739
|
+
prompt: string,
|
|
740
|
+
options: {
|
|
741
|
+
onToolActivity?: (activity: ToolActivity) => void;
|
|
742
|
+
onAssistantUsage?: (usage: {
|
|
743
|
+
input: number;
|
|
744
|
+
output: number;
|
|
745
|
+
cacheWrite: number;
|
|
746
|
+
}) => void;
|
|
747
|
+
onCompaction?: (info: {
|
|
748
|
+
reason: "manual" | "threshold" | "overflow";
|
|
749
|
+
tokensBefore: number;
|
|
750
|
+
}) => void;
|
|
751
|
+
signal?: AbortSignal;
|
|
752
|
+
} = {},
|
|
753
|
+
): Promise<string> {
|
|
754
|
+
const collector = collectResponseText(session);
|
|
755
|
+
const cleanupAbort = forwardAbortSignal(session, options.signal);
|
|
756
|
+
|
|
757
|
+
const unsubEvents =
|
|
758
|
+
options.onToolActivity || options.onAssistantUsage || options.onCompaction
|
|
759
|
+
? session.subscribe((event: AgentSessionEvent) => {
|
|
760
|
+
if (event.type === "tool_execution_start")
|
|
761
|
+
options.onToolActivity?.({
|
|
762
|
+
type: "start",
|
|
763
|
+
toolName: event.toolName,
|
|
764
|
+
});
|
|
765
|
+
if (event.type === "tool_execution_end")
|
|
766
|
+
options.onToolActivity?.({ type: "end", toolName: event.toolName });
|
|
767
|
+
if (
|
|
768
|
+
event.type === "message_end" &&
|
|
769
|
+
event.message.role === "assistant"
|
|
770
|
+
) {
|
|
771
|
+
const u = (event.message as any).usage;
|
|
772
|
+
if (u)
|
|
773
|
+
options.onAssistantUsage?.({
|
|
774
|
+
input: u.input ?? 0,
|
|
775
|
+
output: u.output ?? 0,
|
|
776
|
+
cacheWrite: u.cacheWrite ?? 0,
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
if (
|
|
780
|
+
event.type === "compaction_end" &&
|
|
781
|
+
!event.aborted &&
|
|
782
|
+
event.result
|
|
783
|
+
) {
|
|
784
|
+
options.onCompaction?.({
|
|
785
|
+
reason: event.reason,
|
|
786
|
+
tokensBefore: event.result.tokensBefore,
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
})
|
|
790
|
+
: () => {};
|
|
791
|
+
|
|
792
|
+
try {
|
|
793
|
+
await session.prompt(prompt);
|
|
794
|
+
} finally {
|
|
795
|
+
collector.unsubscribe();
|
|
796
|
+
unsubEvents();
|
|
797
|
+
cleanupAbort();
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
return collector.getText().trim() || getLastAssistantText(session);
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/**
|
|
804
|
+
* Send a steering message to a running subagent.
|
|
805
|
+
* The message will interrupt the agent after its current tool execution.
|
|
806
|
+
*/
|
|
807
|
+
export async function steerAgent(
|
|
808
|
+
session: AgentSession,
|
|
809
|
+
message: string,
|
|
810
|
+
): Promise<void> {
|
|
811
|
+
await session.steer(message);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Get the subagent's conversation messages as formatted text.
|
|
816
|
+
*/
|
|
817
|
+
export function getAgentConversation(session: AgentSession): string {
|
|
818
|
+
const parts: string[] = [];
|
|
819
|
+
|
|
820
|
+
for (const msg of session.messages) {
|
|
821
|
+
if (msg.role === "user") {
|
|
822
|
+
const text =
|
|
823
|
+
typeof msg.content === "string"
|
|
824
|
+
? msg.content
|
|
825
|
+
: extractText(msg.content);
|
|
826
|
+
if (text.trim()) parts.push(`[User]: ${text.trim()}`);
|
|
827
|
+
} else if (msg.role === "assistant") {
|
|
828
|
+
const textParts: string[] = [];
|
|
829
|
+
const toolCalls: string[] = [];
|
|
830
|
+
for (const c of msg.content) {
|
|
831
|
+
if (c.type === "text" && c.text) textParts.push(c.text);
|
|
832
|
+
else if (c.type === "toolCall")
|
|
833
|
+
toolCalls.push(
|
|
834
|
+
` Tool: ${(c as any).name ?? (c as any).toolName ?? "unknown"}`,
|
|
835
|
+
);
|
|
836
|
+
}
|
|
837
|
+
if (textParts.length > 0)
|
|
838
|
+
parts.push(`[Assistant]: ${textParts.join("\n")}`);
|
|
839
|
+
if (toolCalls.length > 0)
|
|
840
|
+
parts.push(`[Tool Calls]:\n${toolCalls.join("\n")}`);
|
|
841
|
+
} else if (msg.role === "toolResult") {
|
|
842
|
+
const text = extractText(msg.content);
|
|
843
|
+
const truncated = text.length > 200 ? `${text.slice(0, 200)}...` : text;
|
|
844
|
+
parts.push(`[Tool Result (${msg.toolName})]: ${truncated}`);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
return parts.join("\n\n");
|
|
849
|
+
}
|