@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.
- package/CHANGELOG.md +862 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/README.md +1125 -3
- package/SECURITY.md +97 -0
- package/dist/abortable.d.ts +12 -0
- package/dist/abortable.js +42 -0
- package/dist/active-agents-prompt.d.ts +40 -0
- package/dist/active-agents-prompt.js +98 -0
- package/dist/advisor-status.d.ts +16 -0
- package/dist/advisor-status.js +74 -0
- package/dist/agent-color.d.ts +35 -0
- package/dist/agent-color.js +131 -0
- package/dist/agent-file-toggle.d.ts +125 -0
- package/dist/agent-file-toggle.js +264 -0
- package/dist/agent-manager.d.ts +872 -0
- package/dist/agent-manager.js +2910 -0
- package/dist/agent-runner.d.ts +358 -0
- package/dist/agent-runner.js +1219 -0
- package/dist/agent-types.d.ts +142 -0
- package/dist/agent-types.js +315 -0
- package/dist/awaited-result.d.ts +37 -0
- package/dist/awaited-result.js +150 -0
- package/dist/child-context.d.ts +15 -0
- package/dist/child-context.js +181 -0
- package/dist/child-session-shutdown.d.ts +21 -0
- package/dist/child-session-shutdown.js +97 -0
- package/dist/cleanup-plans.d.ts +10 -0
- package/dist/cleanup-plans.js +112 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +73 -0
- package/dist/cross-extension-rpc.js +192 -0
- package/dist/custom-agents.d.ts +79 -0
- package/dist/custom-agents.js +583 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +125 -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/error-log.d.ts +64 -0
- package/dist/error-log.js +176 -0
- package/dist/force-main-model.d.ts +21 -0
- package/dist/force-main-model.js +28 -0
- package/dist/goal-bridge.d.ts +155 -0
- package/dist/goal-bridge.js +305 -0
- package/dist/group-join.d.ts +36 -0
- package/dist/group-join.js +161 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +6390 -0
- package/dist/invocation-config.d.ts +164 -0
- package/dist/invocation-config.js +158 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/mention-clone.d.ts +87 -0
- package/dist/mention-clone.js +178 -0
- package/dist/mention.d.ts +81 -0
- package/dist/mention.js +131 -0
- package/dist/model-resolver.d.ts +36 -0
- package/dist/model-resolver.js +95 -0
- package/dist/model-scope.d.ts +49 -0
- package/dist/model-scope.js +48 -0
- package/dist/nested-tools.d.ts +76 -0
- package/dist/nested-tools.js +393 -0
- package/dist/output-diagnostics.d.ts +11 -0
- package/dist/output-diagnostics.js +64 -0
- package/dist/output-file.d.ts +64 -0
- package/dist/output-file.js +351 -0
- package/dist/prompts.d.ts +61 -0
- package/dist/prompts.js +94 -0
- package/dist/recovery.d.ts +56 -0
- package/dist/recovery.js +341 -0
- package/dist/report-cleanup.d.ts +204 -0
- package/dist/report-cleanup.js +1632 -0
- package/dist/report-store.d.ts +738 -0
- package/dist/report-store.js +3612 -0
- package/dist/schedule-store.d.ts +52 -0
- package/dist/schedule-store.js +240 -0
- package/dist/schedule.d.ts +121 -0
- package/dist/schedule.js +420 -0
- package/dist/settings.d.ts +388 -0
- package/dist/settings.js +268 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/standalone-fallback.d.ts +30 -0
- package/dist/standalone-fallback.js +239 -0
- package/dist/status-note.d.ts +61 -0
- package/dist/status-note.js +85 -0
- package/dist/structured-output.d.ts +61 -0
- package/dist/structured-output.js +112 -0
- package/dist/terminal-progress.d.ts +115 -0
- package/dist/terminal-progress.js +206 -0
- package/dist/types.d.ts +411 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-mention.d.ts +82 -0
- package/dist/ui/agent-mention.js +187 -0
- package/dist/ui/agent-widget.d.ts +304 -0
- package/dist/ui/agent-widget.js +770 -0
- package/dist/ui/cleanup-preview.d.ts +59 -0
- package/dist/ui/cleanup-preview.js +466 -0
- package/dist/ui/conversation-viewer.d.ts +105 -0
- package/dist/ui/conversation-viewer.js +544 -0
- package/dist/ui/fleet-list.d.ts +183 -0
- package/dist/ui/fleet-list.js +494 -0
- package/dist/ui/fleet-overlay.d.ts +203 -0
- package/dist/ui/fleet-overlay.js +887 -0
- package/dist/ui/hub-feed.d.ts +52 -0
- package/dist/ui/hub-feed.js +148 -0
- package/dist/ui/native-transcript.d.ts +33 -0
- package/dist/ui/native-transcript.js +234 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +94 -0
- package/dist/ui/select-item.d.ts +27 -0
- package/dist/ui/select-item.js +34 -0
- package/dist/ui/tool-progress.d.ts +22 -0
- package/dist/ui/tool-progress.js +56 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/ui/workflow-card.d.ts +175 -0
- package/dist/ui/workflow-card.js +332 -0
- package/dist/ui/workflow-dialog.d.ts +305 -0
- package/dist/ui/workflow-dialog.js +848 -0
- package/dist/ui/workflow-menu.d.ts +60 -0
- package/dist/ui/workflow-menu.js +147 -0
- package/dist/usage.d.ts +147 -0
- package/dist/usage.js +147 -0
- package/dist/workflow/collisions.d.ts +95 -0
- package/dist/workflow/collisions.js +88 -0
- package/dist/workflow/entry.d.ts +32 -0
- package/dist/workflow/entry.js +29 -0
- package/dist/workflow/host.d.ts +65 -0
- package/dist/workflow/host.js +477 -0
- package/dist/workflow/journal.d.ts +113 -0
- package/dist/workflow/journal.js +177 -0
- package/dist/workflow/json-schema.d.ts +51 -0
- package/dist/workflow/json-schema.js +111 -0
- package/dist/workflow/meta.d.ts +67 -0
- package/dist/workflow/meta.js +317 -0
- package/dist/workflow/progress.d.ts +224 -0
- package/dist/workflow/progress.js +361 -0
- package/dist/workflow/runtime.d.ts +373 -0
- package/dist/workflow/runtime.js +919 -0
- package/dist/workflow/saved.d.ts +90 -0
- package/dist/workflow/saved.js +203 -0
- package/dist/workflow/task.d.ts +145 -0
- package/dist/workflow/task.js +213 -0
- package/dist/workflow/tool-description.d.ts +43 -0
- package/dist/workflow/tool-description.js +204 -0
- package/dist/workflow/worker-source.d.ts +47 -0
- package/dist/workflow/worker-source.js +778 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +164 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +12 -0
- package/docs/DEVELOPMENT.md +40 -0
- package/docs/artifacts.md +93 -0
- package/docs/recovery.md +62 -0
- package/docs/rpc.md +252 -0
- package/docs/runtime-performance.md +53 -0
- package/docs/workflows.md +467 -0
- package/examples/agent-tool-description.md +43 -0
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +66 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +75 -6
- package/src/abortable.ts +43 -0
- package/src/active-agents-prompt.ts +115 -0
- package/src/advisor-status.ts +74 -0
- package/src/agent-color.ts +169 -0
- package/src/agent-file-toggle.ts +273 -0
- package/src/agent-manager.ts +3312 -0
- package/src/agent-runner.ts +1500 -0
- package/src/agent-types.ts +390 -0
- package/src/awaited-result.ts +187 -0
- package/src/child-context.ts +173 -0
- package/src/child-session-shutdown.ts +114 -0
- package/src/cleanup-plans.ts +112 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +257 -0
- package/src/custom-agents.ts +689 -0
- package/src/default-agents.ts +129 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/error-log.ts +202 -0
- package/src/force-main-model.ts +29 -0
- package/src/goal-bridge.ts +468 -0
- package/src/group-join.ts +188 -0
- package/src/index.ts +6856 -0
- package/src/invocation-config.ts +276 -0
- package/src/memory.ts +179 -0
- package/src/mention-clone.ts +221 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +118 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +553 -0
- package/src/output-diagnostics.ts +68 -0
- package/src/output-file.ts +380 -0
- package/src/prompts.ts +151 -0
- package/src/recovery.ts +320 -0
- package/src/report-cleanup.ts +2044 -0
- package/src/report-store.ts +4130 -0
- package/src/schedule-store.ts +244 -0
- package/src/schedule.ts +450 -0
- package/src/settings.ts +632 -0
- package/src/skill-loader.ts +102 -0
- package/src/standalone-fallback.ts +195 -0
- package/src/status-note.ts +90 -0
- package/src/structured-output.ts +130 -0
- package/src/terminal-progress.ts +215 -0
- package/src/types.ts +424 -0
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +866 -0
- package/src/ui/cleanup-preview.ts +518 -0
- package/src/ui/conversation-viewer.ts +527 -0
- package/src/ui/fleet-list.ts +563 -0
- package/src/ui/fleet-overlay.ts +1048 -0
- package/src/ui/hub-feed.ts +179 -0
- package/src/ui/native-transcript.ts +224 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/select-item.ts +45 -0
- package/src/ui/tool-progress.ts +67 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1119 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +217 -0
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +524 -0
- package/src/workflow/journal.ts +221 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1342 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +313 -0
- package/src/workflow/tool-description.ts +205 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +205 -0
- package/src/xml.ts +13 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-extension RPC handlers for the subagents extension.
|
|
3
|
+
*
|
|
4
|
+
* Exposes ping, spawn, stop, and consume RPCs over the pi.events event bus,
|
|
5
|
+
* using per-request scoped reply channels.
|
|
6
|
+
*
|
|
7
|
+
* Reply envelope follows pi-mono convention:
|
|
8
|
+
* success → { success: true, data?: T }
|
|
9
|
+
* error → { success: false, error: string }
|
|
10
|
+
*
|
|
11
|
+
* @see docs/rpc.md — the caller-facing integration reference: spawn options
|
|
12
|
+
* (including the fields spawnTopLevel strips), every error string, the
|
|
13
|
+
* completion-notification race, and what protocol version 2 does not promise.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { isTopLevelAgent } from "./agent-manager.ts";
|
|
17
|
+
import { getAllowPerCallTurnLimits, resolveEffectiveMaxTurns } from "./agent-runner.ts";
|
|
18
|
+
import { cloneAgentDefinition, getAgentConfigIn, resolveSpawnTypeIn } from "./agent-types.ts";
|
|
19
|
+
import { isForceMainModel } from "./force-main-model.ts";
|
|
20
|
+
import { type ModelRegistry, resolveModel } from "./model-resolver.ts";
|
|
21
|
+
import { checkModelScope } from "./model-scope.ts";
|
|
22
|
+
import type { AgentConfig, AgentRecord } from "./types.ts";
|
|
23
|
+
|
|
24
|
+
/** Minimal event bus interface needed by the RPC handlers. */
|
|
25
|
+
export interface EventBus {
|
|
26
|
+
on(event: string, handler: (data: unknown) => void): () => void;
|
|
27
|
+
emit(event: string, data: unknown): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** RPC reply envelope — matches pi-mono's RpcResponse shape. */
|
|
31
|
+
export type RpcReply<T = void> =
|
|
32
|
+
| { success: true; data?: T }
|
|
33
|
+
| { success: false; error: string };
|
|
34
|
+
|
|
35
|
+
/** RPC protocol version — bumped when the envelope or method contracts change. */
|
|
36
|
+
export const PROTOCOL_VERSION = 2;
|
|
37
|
+
|
|
38
|
+
/** Minimal AgentManager interface needed by the spawn/stop/consume RPCs. */
|
|
39
|
+
export interface SpawnCapable {
|
|
40
|
+
spawn(pi: unknown, ctx: unknown, type: string, prompt: string, options: any): string;
|
|
41
|
+
/** Resolves once the spawned agent is running; rejects on a startup failure. */
|
|
42
|
+
awaitStartup(id: string): Promise<void>;
|
|
43
|
+
abort(id: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* The record behind an id, for the stop handler's ownership check. Narrowed
|
|
46
|
+
* to the two fields `isTopLevelAgent` reads, so the RPC layer keeps its
|
|
47
|
+
* deliberately shallow view of the manager.
|
|
48
|
+
*/
|
|
49
|
+
getRecord(id: string): Pick<AgentRecord, "parentAgentId" | "workflowId"> | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Mark a settled agent's result as read by the caller, suppressing the
|
|
52
|
+
* completion notification — what `get_subagent_result` does when it returns
|
|
53
|
+
* one. False when there is no such agent, or it has not settled yet.
|
|
54
|
+
*/
|
|
55
|
+
consumeResult(id: string): boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface RpcDeps {
|
|
59
|
+
events: EventBus;
|
|
60
|
+
pi: unknown; // passed through to manager.spawn
|
|
61
|
+
getCtx: () => unknown | undefined; // returns current ExtensionContext
|
|
62
|
+
manager: SpawnCapable;
|
|
63
|
+
/**
|
|
64
|
+
* Refreshes the caller's scoped registry for an ordinary spawn admission.
|
|
65
|
+
* When present, the requested type is resolved against this map and the
|
|
66
|
+
* admitted definition is cloned into the spawn options. Absent keeps the
|
|
67
|
+
* legacy pass-through behaviour for callers that wire no async preparation.
|
|
68
|
+
*/
|
|
69
|
+
prepare?: () => Promise<Map<string, AgentConfig>>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface RpcHandle {
|
|
73
|
+
unsubPing: () => void;
|
|
74
|
+
unsubSpawn: () => void;
|
|
75
|
+
unsubStop: () => void;
|
|
76
|
+
unsubConsume: () => void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Wire a single RPC handler: listen on `channel`, run `fn(params)`,
|
|
81
|
+
* emit the reply envelope on `channel:reply:${requestId}`.
|
|
82
|
+
*/
|
|
83
|
+
function handleRpc<P extends { requestId: string }>(
|
|
84
|
+
events: EventBus,
|
|
85
|
+
channel: string,
|
|
86
|
+
fn: (params: P) => unknown | Promise<unknown>,
|
|
87
|
+
): () => void {
|
|
88
|
+
return events.on(channel, async (raw: unknown) => {
|
|
89
|
+
const params = raw as P;
|
|
90
|
+
try {
|
|
91
|
+
const data = await fn(params);
|
|
92
|
+
const reply: { success: true; data?: unknown } = { success: true };
|
|
93
|
+
if (data !== undefined) reply.data = data;
|
|
94
|
+
events.emit(`${channel}:reply:${params.requestId}`, reply);
|
|
95
|
+
} catch (err: any) {
|
|
96
|
+
events.emit(`${channel}:reply:${params.requestId}`, {
|
|
97
|
+
success: false, error: err?.message ?? String(err),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Register ping, spawn, stop, and consume RPC handlers on the event bus.
|
|
105
|
+
* Returns unsub functions for cleanup.
|
|
106
|
+
*/
|
|
107
|
+
export function registerRpcHandlers(deps: RpcDeps): RpcHandle {
|
|
108
|
+
const { events, pi, getCtx, manager } = deps;
|
|
109
|
+
|
|
110
|
+
const unsubPing = handleRpc(events, "subagents:rpc:ping", () => {
|
|
111
|
+
return { version: PROTOCOL_VERSION };
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const unsubSpawn = handleRpc<{ requestId: string; type: string; prompt: string; options?: any }>(
|
|
115
|
+
events, "subagents:rpc:spawn", async ({ type, prompt, options }) => {
|
|
116
|
+
const ctx = getCtx();
|
|
117
|
+
if (!ctx) throw new Error("No active session");
|
|
118
|
+
|
|
119
|
+
// Refresh and resolve against the caller's OWN scoped registry before
|
|
120
|
+
// anything can suspend. The clone freezes prompt, tool permissions and
|
|
121
|
+
// model/session/nesting policy through queueing, worktree setup and the
|
|
122
|
+
// runner; a later process-wide reload cannot widen or narrow this child.
|
|
123
|
+
// No `prepare` means the legacy path: the manager receives the requested
|
|
124
|
+
// type and no definition snapshot, exactly as before.
|
|
125
|
+
const registry = deps.prepare === undefined ? undefined : await deps.prepare();
|
|
126
|
+
let spawnType = type;
|
|
127
|
+
let agentDefinition: AgentConfig | undefined;
|
|
128
|
+
if (registry !== undefined) {
|
|
129
|
+
const dispatch = resolveSpawnTypeIn(registry, type);
|
|
130
|
+
if (!dispatch.ok) throw new Error(dispatch.message);
|
|
131
|
+
spawnType = dispatch.type;
|
|
132
|
+
const config = getAgentConfigIn(registry, spawnType);
|
|
133
|
+
agentDefinition = config === undefined ? undefined : cloneAgentDefinition(config);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Cross-extension RPC callers (e.g. pi-tasks TaskExecute) naturally
|
|
137
|
+
// forward serializable values, so options.model can be a string like
|
|
138
|
+
// "openai-codex/gpt-5.5". Resolve it to a real Model instance here
|
|
139
|
+
// — same pattern the scheduler path already uses — so the spawned
|
|
140
|
+
// agent's auth lookup doesn't crash with "No API key found for
|
|
141
|
+
// undefined".
|
|
142
|
+
// Own caller options only: inherited admission fields must not reach a
|
|
143
|
+
// legacy manager when no other normalization happens to copy the object.
|
|
144
|
+
let normalizedOptions = { ...(options ?? {}) };
|
|
145
|
+
// Admission state comes only from this handler's prepared scoped registry.
|
|
146
|
+
// Never mutate the caller or trust its own/non-enumerable definition.
|
|
147
|
+
delete normalizedOptions.agentDefinition;
|
|
148
|
+
// Caller caps are a policy opt-in, and an RPC payload is unvalidated: a
|
|
149
|
+
// caller that bypasses the tool schema can still send `maxTurns`. Strip
|
|
150
|
+
// it here so the runner's low-level option contract cannot be reached
|
|
151
|
+
// around the switch. Copy first — never mutate the caller's own object.
|
|
152
|
+
if (!getAllowPerCallTurnLimits()) {
|
|
153
|
+
normalizedOptions = { ...normalizedOptions };
|
|
154
|
+
delete normalizedOptions.maxTurns;
|
|
155
|
+
const invocation = normalizedOptions.invocation;
|
|
156
|
+
if (invocation !== null && typeof invocation === "object") {
|
|
157
|
+
normalizedOptions.invocation = { ...invocation, maxTurns: resolveEffectiveMaxTurns(spawnType) };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// `!= null` on purpose: a JSON-forwarding caller can serialize an unset
|
|
161
|
+
// field as null, and the runner reads `options.model ?? default`, so null
|
|
162
|
+
// means "inherit" — not an override to resolve or scope-check.
|
|
163
|
+
const override = normalizedOptions.model;
|
|
164
|
+
if (override != null && isForceMainModel()) {
|
|
165
|
+
// Dropped, not deferred: a queued spawn reads the switch when it
|
|
166
|
+
// STARTS, and a raw string that reaches the runner after a mid-queue
|
|
167
|
+
// `/subagents on` is the "No API key found for undefined" crash this
|
|
168
|
+
// resolution exists to prevent.
|
|
169
|
+
normalizedOptions = { ...normalizedOptions, model: undefined };
|
|
170
|
+
} else if (override != null) {
|
|
171
|
+
const { modelRegistry, cwd } = ctx as { modelRegistry?: ModelRegistry; cwd?: string };
|
|
172
|
+
// Names the override the same way in both messages below; an object
|
|
173
|
+
// override would otherwise interpolate as "[object Object]".
|
|
174
|
+
const label = typeof override === "string" ? override : `${override.provider}/${override.id}`;
|
|
175
|
+
if (!modelRegistry) {
|
|
176
|
+
throw new Error(`Model override "${label}" provided but ctx.modelRegistry is unavailable`);
|
|
177
|
+
}
|
|
178
|
+
let model = override;
|
|
179
|
+
if (typeof override === "string") {
|
|
180
|
+
const resolved = resolveModel(override, modelRegistry);
|
|
181
|
+
if (typeof resolved === "string") {
|
|
182
|
+
// resolveModel returns a human-readable error string when the
|
|
183
|
+
// input doesn't match any available model. Surface it instead of
|
|
184
|
+
// silently falling back so the caller sees the auth/typo issue.
|
|
185
|
+
throw new Error(resolved);
|
|
186
|
+
}
|
|
187
|
+
model = resolved;
|
|
188
|
+
normalizedOptions = { ...normalizedOptions, model: resolved };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// A model on the RPC payload is an orchestrator-level choice, exactly
|
|
192
|
+
// like Agent({ model }) — so it gets the Agent tool's hard error, never
|
|
193
|
+
// the frontmatter warn (#240). The check reads the RESOLVED model:
|
|
194
|
+
// resolveModel is fuzzy, so a bare "sonnet" can land on a provider the
|
|
195
|
+
// caller never named. Frontmatter-pinned and parent-inherited models are
|
|
196
|
+
// resolved later, in agent-runner, and keep warn-and-proceed.
|
|
197
|
+
const verdict = checkModelScope({
|
|
198
|
+
model,
|
|
199
|
+
cwd: cwd ?? process.cwd(),
|
|
200
|
+
modelRegistry,
|
|
201
|
+
callerSupplied: true,
|
|
202
|
+
agentLabel: spawnType,
|
|
203
|
+
modelInput: label,
|
|
204
|
+
});
|
|
205
|
+
if (verdict.kind === "error") throw new Error(verdict.message);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const spawnOptions = agentDefinition === undefined
|
|
209
|
+
? normalizedOptions
|
|
210
|
+
: { ...normalizedOptions, agentDefinition };
|
|
211
|
+
const id = manager.spawn(pi, ctx, spawnType, prompt, spawnOptions);
|
|
212
|
+
// With isolation: "worktree" the agent starts asynchronously — wait for
|
|
213
|
+
// it, so a strict-isolation failure is still an error envelope rather
|
|
214
|
+
// than an id for an agent that never ran.
|
|
215
|
+
try {
|
|
216
|
+
await manager.awaitStartup(id);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
// The error envelope is the result; do not also wake the main model.
|
|
219
|
+
manager.consumeResult(id);
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
return { id };
|
|
223
|
+
},
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
const unsubStop = handleRpc<{ requestId: string; agentId: string }>(
|
|
227
|
+
events, "subagents:rpc:stop", ({ agentId }) => {
|
|
228
|
+
const record = manager.getRecord(agentId);
|
|
229
|
+
if (!record) throw new Error("Agent not found");
|
|
230
|
+
// Only the session's own agents are this RPC's to stop. A nested child or
|
|
231
|
+
// a workflow's agent is owned by something that is *waiting on it*, and
|
|
232
|
+
// aborting it out from under that owner turns another extension's stop
|
|
233
|
+
// into a failed step here. Defence in depth rather than a live hole: no
|
|
234
|
+
// RPC hands out agent ids, so a caller has no ordinary way to name one it
|
|
235
|
+
// does not own — but the guard is cheap and the id may leak some other
|
|
236
|
+
// way. Same refuse-what-we-should-not-touch stance as `consume` below.
|
|
237
|
+
if (!isTopLevelAgent(record)) throw new Error("Agent is owned by another agent or workflow");
|
|
238
|
+
// Not "not found" — the lookup above already proved it exists. `abort`
|
|
239
|
+
// returns false only for a record that is neither running nor queued,
|
|
240
|
+
// which is an agent that has already finished.
|
|
241
|
+
if (!manager.abort(agentId)) throw new Error("Agent is not running");
|
|
242
|
+
},
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
// A caller that has already shown the model an agent's result — pi-tasks'
|
|
246
|
+
// TaskOutput is the one in practice — says so here, so the completion
|
|
247
|
+
// notification for that same result is not delivered on top of it and does
|
|
248
|
+
// not cost the parent a turn. Deliberately outside the ping version
|
|
249
|
+
// handshake: an extension built against protocol v2 simply never calls it.
|
|
250
|
+
const unsubConsume = handleRpc<{ requestId: string; agentId: string }>(
|
|
251
|
+
events, "subagents:rpc:consume", ({ agentId }) => {
|
|
252
|
+
if (!manager.consumeResult(agentId)) throw new Error("Agent not found or still running");
|
|
253
|
+
},
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
return { unsubPing, unsubSpawn, unsubStop, unsubConsume };
|
|
257
|
+
}
|