@deepseek-ai/dsh-subagent 0.2.2-rc.1 → 0.2.2-rc.2
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/README.i18n.yaml +46 -28
- package/README.md +52 -23
- package/README.zh.md +52 -23
- package/lib/index.js +845 -902
- package/lib/typert.host.js +91 -125
- package/lib/typert.remote-client.d.ts +3 -3
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/typert.remote-client.js +17 -63
- package/lib/types/activation.d.ts +17 -25
- package/lib/types/activation.d.ts.map +1 -0
- package/lib/types/activation.js +1 -1
- package/lib/types/activation.js.map +1 -0
- package/lib/types/archive-admission.d.ts +11 -4
- package/lib/types/archive-admission.d.ts.map +1 -0
- package/lib/types/archive-admission.js +39 -115
- package/lib/types/archive-admission.js.map +1 -0
- package/lib/types/assistant-output.d.ts.map +1 -0
- package/lib/types/assistant-output.js.map +1 -0
- package/lib/types/catalog.d.ts +27 -7
- package/lib/types/catalog.d.ts.map +1 -0
- package/lib/types/catalog.js +31 -8
- package/lib/types/catalog.js.map +1 -0
- package/lib/types/child-agent.d.ts +7 -17
- package/lib/types/child-agent.d.ts.map +1 -0
- package/lib/types/child-agent.js +6 -4
- package/lib/types/child-agent.js.map +1 -0
- package/lib/types/client.d.ts.map +1 -0
- package/lib/types/client.js.map +1 -0
- package/lib/types/continuation-messages.d.ts +13 -10
- package/lib/types/continuation-messages.d.ts.map +1 -0
- package/lib/types/continuation-messages.js +22 -22
- package/lib/types/continuation-messages.js.map +1 -0
- package/lib/types/control-types.d.ts +7 -28
- package/lib/types/control-types.d.ts.map +1 -0
- package/lib/types/control-types.js.map +1 -0
- package/lib/types/control.d.ts +0 -4
- package/lib/types/control.d.ts.map +1 -0
- package/lib/types/control.js +1 -5
- package/lib/types/control.js.map +1 -0
- package/lib/types/depth.d.ts.map +1 -0
- package/lib/types/depth.js.map +1 -0
- package/lib/types/descriptor.d.ts +1 -1
- package/lib/types/descriptor.d.ts.map +1 -0
- package/lib/types/descriptor.js +1 -1
- package/lib/types/descriptor.js.map +1 -0
- package/lib/types/error.d.ts.map +1 -0
- package/lib/types/error.js.map +1 -0
- package/lib/types/index.d.ts +74 -97
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +125 -146
- package/lib/types/index.js.map +1 -0
- package/lib/types/lifecycle.d.ts +9 -38
- package/lib/types/lifecycle.d.ts.map +1 -0
- package/lib/types/lifecycle.js +5 -31
- package/lib/types/lifecycle.js.map +1 -0
- package/lib/types/list-children.d.ts +12 -0
- package/lib/types/list-children.d.ts.map +1 -0
- package/lib/types/list-children.js +35 -6
- package/lib/types/list-children.js.map +1 -0
- package/lib/types/manager.d.ts +96 -112
- package/lib/types/manager.d.ts.map +1 -0
- package/lib/types/manager.js +471 -568
- package/lib/types/manager.js.map +1 -0
- package/lib/types/names.d.ts +56 -0
- package/lib/types/names.d.ts.map +1 -0
- package/lib/types/names.js +96 -0
- package/lib/types/names.js.map +1 -0
- package/lib/types/out-of-process.d.ts.map +1 -0
- package/lib/types/out-of-process.js.map +1 -0
- package/lib/types/projection-types.d.ts +18 -6
- package/lib/types/projection-types.d.ts.map +1 -0
- package/lib/types/projection-types.js.map +1 -0
- package/lib/types/projection.d.ts +2 -2
- package/lib/types/projection.d.ts.map +1 -0
- package/lib/types/projection.js.map +1 -0
- package/lib/types/structured.d.ts +1 -1
- package/lib/types/structured.d.ts.map +1 -0
- package/lib/types/structured.js +2 -2
- package/lib/types/structured.js.map +1 -0
- package/lib/types/types.d.ts +34 -28
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +2 -4
- package/lib/types/types.js.map +1 -0
- package/package.json +54 -54
- package/src/activation.ts +184 -0
- package/src/archive-admission.ts +84 -0
- package/src/assistant-output.ts +75 -0
- package/src/catalog.ts +194 -0
- package/src/child-agent.ts +280 -0
- package/src/client.ts +8 -0
- package/src/continuation-messages.ts +176 -0
- package/src/control-types.ts +127 -0
- package/src/control.ts +103 -0
- package/src/depth.ts +51 -0
- package/src/descriptor.ts +269 -0
- package/src/error.ts +15 -0
- package/src/index.ts +604 -0
- package/src/lifecycle.ts +116 -0
- package/src/list-children.ts +168 -0
- package/src/manager.ts +1359 -0
- package/src/names.ts +113 -0
- package/src/out-of-process.ts +217 -0
- package/src/projection-types.ts +109 -0
- package/src/projection.ts +184 -0
- package/src/structured.ts +155 -0
- package/src/types.ts +364 -0
- package/lib/types/internal.d.ts +0 -58
- package/lib/types/internal.js +0 -58
package/lib/types/manager.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Owns subagent startup, message admission, cold resume, and execution lifetimes.
|
|
3
3
|
* One child lock orders input acceptance and closure; one activation map owns
|
|
4
|
-
* capacity,
|
|
4
|
+
* capacity, runtime ownership, and resource release across all providers.
|
|
5
5
|
*
|
|
6
6
|
* @module @deepseek-ai/dsh-subagent/manager
|
|
7
7
|
*/
|
|
@@ -63,12 +63,13 @@ import { brandString } from '@deepseek-ai/dsh-brand';
|
|
|
63
63
|
import { ReasoningEffortId, contentHasImage, createUserMessage, errorChain } from '@deepseek-ai/dsh-llm';
|
|
64
64
|
import { SessionLogOffset } from '@deepseek-ai/dsh-session';
|
|
65
65
|
import { appendDelegatedPolicyOverrides, applyChildComposition, captureDelegatedPolicyOverrides, childSessionMeta, resolveChildAgentOptions, resolveChildDepth, } from "./child-agent.js";
|
|
66
|
-
import { createAgentMessage,
|
|
67
|
-
import {
|
|
66
|
+
import { createAgentMessage, createSettlementMessage, withResultDeliveryGuidance, } from "./continuation-messages.js";
|
|
67
|
+
import { installSubagentArchiveAdmission } from "./archive-admission.js";
|
|
68
68
|
import { foldSubagentDescriptor, snapshotSubagentDescriptor } from "./descriptor.js";
|
|
69
|
-
import { establishCatalogChild, establishExternalCatalogChild } from "./catalog.js";
|
|
69
|
+
import { catalogNames, establishCatalogChild, establishExternalCatalogChild } from "./catalog.js";
|
|
70
70
|
import { SubagentError } from "./error.js";
|
|
71
|
-
import {
|
|
71
|
+
import { ROOT_AGENT_NAME, SiblingNameReservations, agentAddress, joinAgentPath, parentAgentPath } from "./names.js";
|
|
72
|
+
import { SubagentRunId } from "./types.js";
|
|
72
73
|
import { attachStructuredRuntime } from "./structured.js";
|
|
73
74
|
import { ActivationPool, ChildLock, activationAgent, captureLocalResult, observeLocalFailure, requireLocalActivation } from "./activation.js";
|
|
74
75
|
/** Render only the safe outer message from execution or teardown failures. */
|
|
@@ -90,34 +91,36 @@ export class SubagentManager {
|
|
|
90
91
|
locks = new ChildLock();
|
|
91
92
|
/** Structural Cordis owner of every Activation handle. */
|
|
92
93
|
ownerCtx;
|
|
93
|
-
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
* poisoning a later same-id replacement.
|
|
98
|
-
*/
|
|
99
|
-
closingScopes = new Map();
|
|
94
|
+
/** Exact subagent tree roots whose host teardown has closed admission. */
|
|
95
|
+
closingRoots = new Set();
|
|
96
|
+
/** Sibling names held by creations that have not committed their catalog entry. */
|
|
97
|
+
names = new SiblingNameReservations();
|
|
100
98
|
draining = false;
|
|
101
99
|
/**
|
|
102
100
|
* Build one manager inside the service's Agent-injected context.
|
|
103
101
|
* @param ctx - context providing Agents, Sessions, and teardown ownership.
|
|
104
|
-
* @param host -
|
|
102
|
+
* @param host - descriptor reads and lifecycle publication owned by the service.
|
|
105
103
|
* @param maxActiveSubagents - the current configured child capacity.
|
|
106
104
|
*/
|
|
107
105
|
constructor(ctx, host, maxActiveSubagents) {
|
|
108
106
|
this.ctx = ctx;
|
|
109
107
|
this.host = host;
|
|
110
108
|
this.maxActiveSubagents = maxActiveSubagents;
|
|
111
|
-
//
|
|
112
|
-
// cannot express the dynamic child graph. Register the private scope's
|
|
113
|
-
// structural disposer FIRST and the drain SECOND, so reverse unwind invokes
|
|
114
|
-
// the drain before releasing the scope; a cleanup effect on the same scope
|
|
115
|
-
// as the Agent handles would let structural handle disposal bypass
|
|
116
|
-
// child-first ordering.
|
|
109
|
+
// Drain managed instances before Cordis releases their owning scope.
|
|
117
110
|
const scope = ctx.plugin(function activationOwner() { });
|
|
118
111
|
this.ownerCtx = scope.ctx;
|
|
112
|
+
installSubagentArchiveAdmission(ctx, (agent) => {
|
|
113
|
+
const activation = this.resident.get(agent.id);
|
|
114
|
+
return activation !== undefined && activationAgent(activation) === agent ? activation.ancestors : undefined;
|
|
115
|
+
}, session => host.identityOf(session));
|
|
119
116
|
ctx.on('agent/disposed', ({ agent }) => {
|
|
120
|
-
this.
|
|
117
|
+
this.closingRoots.delete(agent);
|
|
118
|
+
const activation = this.resident.get(agent.id);
|
|
119
|
+
if (activation?.kind === 'local' && activation.handle.agent === agent) {
|
|
120
|
+
void this.close(activation, () => this.finishDisposal(activation, false)).catch((error) => {
|
|
121
|
+
this.reportTeardownFailure(activation, error);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
121
124
|
});
|
|
122
125
|
ctx.effect(function* () {
|
|
123
126
|
yield scope.dispose;
|
|
@@ -127,7 +130,7 @@ export class SubagentManager {
|
|
|
127
130
|
/**
|
|
128
131
|
* Interrupt one live continuable child's current turn under the supplied authority.
|
|
129
132
|
* @param targetSessionId - the durable child session id to interrupt.
|
|
130
|
-
* @param authority -
|
|
133
|
+
* @param authority - a trusted user operation or exact live ancestor Agent.
|
|
131
134
|
*/
|
|
132
135
|
interrupt(targetSessionId, authority) {
|
|
133
136
|
if (authority.kind === 'ancestor') {
|
|
@@ -142,12 +145,8 @@ export class SubagentManager {
|
|
|
142
145
|
const activation = this.resident.get(targetSessionId);
|
|
143
146
|
if (activation === undefined)
|
|
144
147
|
return;
|
|
145
|
-
if (authority.kind === '
|
|
146
|
-
|
|
147
|
-
throw new SubagentError(`subagent "${targetSessionId}" belongs to another parent session`, 'UNAUTHORIZED');
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
else if (!activation.ancestry.has(authority.agent)) {
|
|
148
|
+
if (authority.kind === 'ancestor'
|
|
149
|
+
&& (this.rootOf(authority.agent) !== activation.root || !activation.ancestors.includes(authority.agent.id))) {
|
|
151
150
|
throw new SubagentError(`subagent "${targetSessionId}" is not a live descendant of agent "${authority.agent.id}"`, 'UNAUTHORIZED');
|
|
152
151
|
}
|
|
153
152
|
// Disposal already stopped the target with a whole-Activation teardown;
|
|
@@ -166,32 +165,27 @@ export class SubagentManager {
|
|
|
166
165
|
}
|
|
167
166
|
/**
|
|
168
167
|
* Close admission, await every already-admitted materialization through
|
|
169
|
-
* publication or rollback, then dispose
|
|
170
|
-
* child-first.
|
|
168
|
+
* publication or rollback, then dispose every agent in the subagent tree.
|
|
171
169
|
*/
|
|
172
170
|
async drain() {
|
|
173
171
|
this.draining = true;
|
|
174
172
|
for (const pending of this.materializations)
|
|
175
173
|
pending.controller.abort();
|
|
176
174
|
await Promise.all([...this.materializations].map(materialization => materialization.settled));
|
|
177
|
-
|
|
178
|
-
for (const activation of this.resident.values()) {
|
|
179
|
-
for (const child of activation.ownedChildren)
|
|
180
|
-
owned.add(child);
|
|
181
|
-
}
|
|
182
|
-
const roots = [...this.resident.values()].filter(activation => !owned.has(activation.childId));
|
|
183
|
-
await this.disposeRoots(roots, 'activation(s)');
|
|
175
|
+
await this.disposeRoots([...this.resident.values()], 'activation(s)');
|
|
184
176
|
}
|
|
185
177
|
/**
|
|
186
|
-
* Join progressing
|
|
187
|
-
* @param
|
|
188
|
-
* @returns whether work was joined and the host must recheck
|
|
178
|
+
* Join progressing subagent tree work for its root, leaving idle parked inboxes resident.
|
|
179
|
+
* @param root - exact live subagent tree root; ordinary members return false.
|
|
180
|
+
* @returns whether work was joined and the host must recheck root completion.
|
|
189
181
|
*/
|
|
190
|
-
async
|
|
182
|
+
async waitForTreeWork(root) {
|
|
183
|
+
if (this.rootOf(root) !== root)
|
|
184
|
+
return false;
|
|
191
185
|
let observed = false;
|
|
192
186
|
while (true) {
|
|
193
|
-
const pending = [...this.materializations].filter(item => item.
|
|
194
|
-
const children = [...this.resident.values()].filter(item =>
|
|
187
|
+
const pending = [...this.materializations].filter(item => item.root === root);
|
|
188
|
+
const children = [...this.resident.values()].filter(item => item.root === root);
|
|
195
189
|
observed ||= children.some(item => item.kind === 'local' && item.handle.agent.status !== 'idle');
|
|
196
190
|
await Promise.all(children.flatMap(item => item.kind === 'local' ? [item.handle.agent.whenIdle()] : []));
|
|
197
191
|
const waits = pending.map(item => item.settled);
|
|
@@ -204,13 +198,6 @@ export class SubagentManager {
|
|
|
204
198
|
waits.push(item.handle.agent.whenIdle());
|
|
205
199
|
continue;
|
|
206
200
|
}
|
|
207
|
-
if (item.ownedChildren.size > 0) {
|
|
208
|
-
// Published descendants are observed separately; preparation holds
|
|
209
|
-
// keep the owner pending until publication or rollback wakes it.
|
|
210
|
-
if ([...item.ownedChildren].some(id => !this.resident.has(id)))
|
|
211
|
-
waits.push(item.poke.promise);
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
201
|
const inbox = item.handle.agent.inbox;
|
|
215
202
|
if (item.failureAt === undefined && (inbox.nextTurn.length > 0 || inbox.nextStep.length > 0))
|
|
216
203
|
continue;
|
|
@@ -222,147 +209,94 @@ export class SubagentManager {
|
|
|
222
209
|
continue;
|
|
223
210
|
}
|
|
224
211
|
// A descendant can be published while whenIdle follows another child.
|
|
225
|
-
if ([...this.materializations].some(item => item.
|
|
226
|
-
|| [...this.resident.values()].some(item =>
|
|
227
|
-
&& item.ancestry.has(parent) && !children.includes(item)))
|
|
212
|
+
if ([...this.materializations].some(item => item.root === root && !pending.includes(item))
|
|
213
|
+
|| [...this.resident.values()].some(item => item.root === root && !children.includes(item)))
|
|
228
214
|
continue;
|
|
229
215
|
return observed;
|
|
230
216
|
}
|
|
231
217
|
}
|
|
232
218
|
/**
|
|
233
|
-
* Stop the
|
|
234
|
-
* @param parents - exact live roots whose
|
|
219
|
+
* Stop every member of the selected subagent trees; ordinary members do not close a subagent tree.
|
|
220
|
+
* @param parents - exact live subagent tree roots whose instances must stop.
|
|
235
221
|
*/
|
|
236
222
|
async drainDescendants(parents) {
|
|
237
|
-
const roots = new Set(parents.filter(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
const targets = [];
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
&& activation.ancestry.has(root));
|
|
249
|
-
if (owners.length === 0)
|
|
250
|
-
continue;
|
|
251
|
-
targets.push(activation);
|
|
252
|
-
for (const owner of owners) {
|
|
253
|
-
const members = this.closingMembers(owner);
|
|
254
|
-
if (child !== undefined)
|
|
255
|
-
members.add(child);
|
|
256
|
-
for (const agent of lineage)
|
|
257
|
-
members.add(agent);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
const materializations = [...this.materializations].filter((materialization) => {
|
|
261
|
-
const owners = [...roots].filter(root => materialization.lineage.includes(root));
|
|
262
|
-
for (const owner of owners) {
|
|
263
|
-
const members = this.closingMembers(owner);
|
|
264
|
-
for (const agent of materialization.lineage)
|
|
265
|
-
members.add(agent);
|
|
266
|
-
}
|
|
267
|
-
if (owners.length > 0)
|
|
268
|
-
materialization.controller.abort();
|
|
269
|
-
return owners.length > 0;
|
|
270
|
-
});
|
|
271
|
-
const ownedTargets = new Set();
|
|
272
|
-
for (const activation of targets) {
|
|
273
|
-
for (const child of activation.ownedChildren)
|
|
274
|
-
ownedTargets.add(child);
|
|
275
|
-
}
|
|
276
|
-
const targetRoots = targets.filter(activation => !ownedTargets.has(activation.childId));
|
|
277
|
-
for (const activation of targets) {
|
|
278
|
-
const disposal = this.dispose(activation);
|
|
279
|
-
void disposal.catch(() => undefined);
|
|
280
|
-
}
|
|
281
|
-
await Promise.all(materializations.map(materialization => materialization.settled));
|
|
282
|
-
await this.disposeRoots(targetRoots, 'scoped activation(s)');
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Release selected resident direct children of one exact live parent.
|
|
286
|
-
* @param parent - exact live direct parent authorizing the selected release.
|
|
287
|
-
* @param childIds - durable direct-child ids to release when resident.
|
|
288
|
-
*/
|
|
289
|
-
async drainChildren(parent, childIds) {
|
|
290
|
-
if (this.ctx.agents.get(parent.id) !== parent) {
|
|
291
|
-
throw new SubagentError('selected child teardown requires the exact live parent agent', 'UNAUTHORIZED');
|
|
292
|
-
}
|
|
293
|
-
const targets = [];
|
|
294
|
-
for (const childId of new Set(childIds)) {
|
|
295
|
-
const activation = this.resident.get(childId);
|
|
296
|
-
if (activation === undefined)
|
|
297
|
-
continue;
|
|
298
|
-
if (activation.parent.id !== parent.id || !activation.ancestry.has(parent)) {
|
|
299
|
-
throw new SubagentError(`subagent "${childId}" is not a direct child of agent "${parent.id}"`, 'UNAUTHORIZED');
|
|
300
|
-
}
|
|
301
|
-
targets.push(activation);
|
|
302
|
-
}
|
|
303
|
-
for (const activation of targets) {
|
|
304
|
-
const disposal = this.dispose(activation);
|
|
305
|
-
void disposal.catch(() => undefined);
|
|
306
|
-
}
|
|
307
|
-
await this.disposeRoots(targets, 'selected activation(s)');
|
|
223
|
+
const roots = new Set(parents.filter(root => this.ctx.agents.get(root.id) === root && this.rootOf(root) === root));
|
|
224
|
+
for (const root of roots)
|
|
225
|
+
this.closingRoots.add(root);
|
|
226
|
+
const pending = [...this.materializations].filter(item => roots.has(item.root));
|
|
227
|
+
for (const item of pending)
|
|
228
|
+
item.controller.abort();
|
|
229
|
+
const targets = [...this.resident.values()].filter(item => roots.has(item.root));
|
|
230
|
+
const disposal = this.disposeRoots(targets, 'scoped activation(s)');
|
|
231
|
+
const [disposed] = await Promise.allSettled([disposal, ...pending.map(item => item.settled)]);
|
|
232
|
+
if (disposed.status === 'rejected')
|
|
233
|
+
throw disposed.reason;
|
|
308
234
|
}
|
|
309
235
|
/**
|
|
310
236
|
* Start one continuable background child and resolve at initial inbox acceptance.
|
|
311
237
|
* Every earlier failure disposes any created handle and rolls back Activation
|
|
312
|
-
* and
|
|
238
|
+
* and its capacity slot without returning either id.
|
|
313
239
|
* @param spec - provider, delegation request, and caller cancellation.
|
|
240
|
+
* @param prepare - creation contribution bound to the checked provider.
|
|
314
241
|
* @returns the durable child id and accepted initial prompt message id.
|
|
315
242
|
*/
|
|
316
|
-
async startLocal(spec) {
|
|
317
|
-
const
|
|
318
|
-
const parent = request.parent;
|
|
243
|
+
async startLocal(spec, prepare) {
|
|
244
|
+
const parent = spec.parent;
|
|
319
245
|
this.assertAdmitting(parent);
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
const
|
|
323
|
-
this.assertChildIdAvailable(childId);
|
|
324
|
-
const childDepth = resolveChildDepth(parent, request.maxDepth);
|
|
325
|
-
// Snapshot before any await: invalid descriptor JSON rejects the call
|
|
326
|
-
// before a child exists, and the detached value is what reaches the log.
|
|
327
|
-
const agentOptions = resolveChildAgentOptions(parent, request.agentOptions, childDepth);
|
|
328
|
-
const agentProvider = agentOptions.provider;
|
|
329
|
-
const agentModel = agentOptions.model;
|
|
330
|
-
const agentReasoningEffort = agentOptions.reasoningEffort;
|
|
331
|
-
const descriptor = snapshotSubagentDescriptor({
|
|
332
|
-
mode: 'continuable',
|
|
333
|
-
provider: spec.provider,
|
|
334
|
-
label: spec.label,
|
|
335
|
-
...agentProvider !== undefined ? { agentProvider } : {},
|
|
336
|
-
...agentModel !== undefined ? { agentModel } : {},
|
|
337
|
-
...agentReasoningEffort !== undefined ? { agentReasoningEffort } : {},
|
|
338
|
-
});
|
|
339
|
-
// Capture before the first await: a later parent switch belongs to the
|
|
340
|
-
// parent's future, not to this child.
|
|
341
|
-
const delegatedPolicies = captureDelegatedPolicyOverrides(parent);
|
|
342
|
-
// An idle continuation-managed parent must not settle while a caller is
|
|
343
|
-
// still creating its child. A turn-scoped delegation does not need this,
|
|
344
|
-
// but the service is also callable outside a turn.
|
|
345
|
-
const releaseHold = this.holdOwnership(parent, childId);
|
|
246
|
+
const ancestry = this.ancestryForChild(parent);
|
|
247
|
+
const reservation = this.reserveName(parent, spec.name);
|
|
248
|
+
const path = joinAgentPath(this.pathOf(parent, ancestry.root), reservation.name);
|
|
346
249
|
try {
|
|
347
|
-
|
|
348
|
-
|
|
250
|
+
return await this.startLocalNamed(spec, prepare, ancestry, reservation, path);
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
reservation.release();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** Create one local child under a name reserved before any await. */
|
|
257
|
+
startLocalNamed(spec, prepare, ancestry, reservation, path) {
|
|
258
|
+
const parent = spec.parent;
|
|
259
|
+
return this.trackStartup(ancestry.root, spec.signal, async (signal) => {
|
|
260
|
+
const persistence = spec.delivery === 'caller' ? this.ctx.get('sessionPersistence') : this.requirePersistence();
|
|
261
|
+
const childId = spec.childId ?? brandString(randomUUID());
|
|
262
|
+
this.assertChildIdAvailable(childId);
|
|
263
|
+
const childDepth = resolveChildDepth(parent, spec.maxDepth);
|
|
264
|
+
// Snapshot before any await: invalid descriptor JSON rejects the call
|
|
265
|
+
// before a child exists, and the detached value is what reaches the log.
|
|
266
|
+
const agentOptions = resolveChildAgentOptions(parent, spec.agentOptions, childDepth);
|
|
267
|
+
const agentProvider = agentOptions.provider;
|
|
268
|
+
const agentModel = agentOptions.model;
|
|
269
|
+
const agentReasoningEffort = agentOptions.reasoningEffort;
|
|
270
|
+
const descriptor = snapshotSubagentDescriptor({
|
|
271
|
+
mode: 'continuable',
|
|
272
|
+
provider: spec.provider,
|
|
273
|
+
label: spec.label,
|
|
274
|
+
...agentProvider !== undefined ? { agentProvider } : {},
|
|
275
|
+
...agentModel !== undefined ? { agentModel } : {},
|
|
276
|
+
...agentReasoningEffort !== undefined ? { agentReasoningEffort } : {},
|
|
277
|
+
});
|
|
278
|
+
// Capture before the first await: a later parent switch belongs to the
|
|
279
|
+
// parent's future, not to this child.
|
|
280
|
+
const delegatedPolicies = captureDelegatedPolicyOverrides(parent);
|
|
281
|
+
const cwd = await this.resolveDirectory(spec, signal);
|
|
282
|
+
const prepared = await prepare({
|
|
349
283
|
sessionId: childId,
|
|
350
284
|
cwd,
|
|
351
285
|
parent,
|
|
352
|
-
signal
|
|
286
|
+
signal,
|
|
353
287
|
});
|
|
354
|
-
|
|
288
|
+
signal.throwIfAborted();
|
|
355
289
|
this.assertAdmitting(parent);
|
|
356
290
|
const inheritedEventCount = SessionLogOffset(prepared.seed?.length ?? 0);
|
|
357
291
|
const seed = prepared.seed;
|
|
358
292
|
let established;
|
|
359
293
|
const messageId = await this.locks.run(childId, async () => {
|
|
360
|
-
|
|
294
|
+
signal.throwIfAborted();
|
|
361
295
|
this.assertAdmitting(parent);
|
|
362
296
|
this.assertChildIdAvailable(childId);
|
|
363
297
|
if (spec.childId !== undefined) {
|
|
364
|
-
const persisted = await persistence?.stat(childId, { signal:
|
|
365
|
-
|
|
298
|
+
const persisted = await persistence?.stat(childId, { signal: signal });
|
|
299
|
+
signal.throwIfAborted();
|
|
366
300
|
this.assertAdmitting(parent);
|
|
367
301
|
this.assertChildIdAvailable(childId);
|
|
368
302
|
if (persisted !== undefined) {
|
|
@@ -373,84 +307,98 @@ export class SubagentManager {
|
|
|
373
307
|
kind: 'local',
|
|
374
308
|
childId,
|
|
375
309
|
provider: spec.provider,
|
|
376
|
-
|
|
310
|
+
...ancestry,
|
|
311
|
+
path,
|
|
377
312
|
create: {
|
|
313
|
+
parent,
|
|
378
314
|
cwd,
|
|
379
315
|
seed,
|
|
380
|
-
meta: childSessionMeta(parent, childDepth, prepared.seed !== undefined,
|
|
316
|
+
meta: childSessionMeta(parent, childDepth, prepared.seed !== undefined, spec.agentPreset),
|
|
381
317
|
inheritedEventCount,
|
|
382
318
|
delegatedPolicies,
|
|
383
319
|
descriptor,
|
|
384
320
|
},
|
|
385
321
|
agentOptions,
|
|
386
|
-
composition: { agentPreset:
|
|
387
|
-
signal
|
|
322
|
+
composition: { agentPreset: spec.agentPreset },
|
|
323
|
+
signal,
|
|
388
324
|
delivery: spec.delivery,
|
|
389
|
-
outputSchema: spec.
|
|
325
|
+
outputSchema: spec.outputSchema,
|
|
390
326
|
});
|
|
391
327
|
established = activation;
|
|
392
328
|
const child = requireLocalActivation(activation).handle.agent;
|
|
393
329
|
const childHeader = child.session.header;
|
|
394
|
-
return await this.submitMaterialized(activation, spec.delivery !== 'caller'
|
|
395
|
-
?
|
|
396
|
-
:
|
|
397
|
-
|
|
330
|
+
return await this.submitMaterialized(activation, spec.delivery !== 'caller'
|
|
331
|
+
? withResultDeliveryGuidance(spec.prompt)
|
|
332
|
+
: spec.prompt, { source: { kind: 'user' }, signal, delivery: 'queue', authorize: () => { this.assertAdmitting(parent); } }, ancestry.root, () => {
|
|
333
|
+
this.assertAdmitting(parent);
|
|
334
|
+
establishCatalogChild(parent.session, childHeader, descriptor, reservation.name);
|
|
398
335
|
});
|
|
399
336
|
});
|
|
400
337
|
return { ...this.receipt(established), childId, messageId };
|
|
401
|
-
}
|
|
402
|
-
catch (error) {
|
|
403
|
-
releaseHold();
|
|
404
|
-
throw error;
|
|
405
|
-
}
|
|
338
|
+
});
|
|
406
339
|
}
|
|
407
340
|
/**
|
|
408
341
|
* Start an external backend under activation ownership.
|
|
409
342
|
* @param spec - task, provider, cancellation, and delivery policy.
|
|
343
|
+
* @param start - execution bound to the checked provider.
|
|
410
344
|
* @returns the accepted activation and its result/disposal capabilities.
|
|
411
345
|
*/
|
|
412
|
-
async startExternal(spec) {
|
|
413
|
-
const parent = spec
|
|
346
|
+
async startExternal(spec, start) {
|
|
347
|
+
const { delivery, name: requestedName, parent } = spec;
|
|
414
348
|
this.assertAdmitting(parent);
|
|
415
|
-
const
|
|
416
|
-
|
|
417
|
-
|
|
349
|
+
const ancestry = this.ancestryForChild(parent);
|
|
350
|
+
if (delivery === 'caller' && requestedName !== undefined) {
|
|
351
|
+
throw new SubagentError('agent names require a cataloged child; caller-delivered external runs are not cataloged', 'UNSUPPORTED_CAPABILITY');
|
|
352
|
+
}
|
|
353
|
+
// Only parent-delivered external runs enter the catalog, so only they hold a name.
|
|
354
|
+
const reservation = delivery === 'parent' ? this.reserveName(parent, requestedName) : undefined;
|
|
355
|
+
const path = joinAgentPath(this.pathOf(parent, ancestry.root), reservation?.name);
|
|
418
356
|
try {
|
|
419
|
-
|
|
420
|
-
activation = await this.materialize({
|
|
421
|
-
kind: 'external',
|
|
422
|
-
childId: pendingId,
|
|
423
|
-
provider: spec.provider,
|
|
424
|
-
parent,
|
|
425
|
-
signal: spec.signal,
|
|
426
|
-
delivery: spec.delivery,
|
|
427
|
-
start: signal => this.host.startExternal(spec.provider, {
|
|
428
|
-
...spec.request, cwd, label: spec.label, signal,
|
|
429
|
-
}),
|
|
430
|
-
});
|
|
431
|
-
this.assertAdmitting(parent);
|
|
432
|
-
this.assertLiveParent(parent, activation.childId);
|
|
433
|
-
spec.signal.throwIfAborted();
|
|
434
|
-
if (spec.delivery === 'parent') {
|
|
435
|
-
establishExternalCatalogChild(parent.session, activation.childId, spec.label);
|
|
436
|
-
}
|
|
437
|
-
this.announce(activation);
|
|
438
|
-
return this.receipt(activation);
|
|
357
|
+
return await this.startExternalNamed(spec, start, ancestry, reservation, path);
|
|
439
358
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
359
|
+
finally {
|
|
360
|
+
reservation?.release();
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
/** Start one external execution, cataloging parent-delivered runs under their reserved name. */
|
|
364
|
+
startExternalNamed(spec, start, ancestry, reservation, path) {
|
|
365
|
+
const { provider, delivery, childId: _childId, name: _name, ...request } = spec;
|
|
366
|
+
const parent = request.parent;
|
|
367
|
+
return this.trackStartup(ancestry.root, spec.signal, async (signal) => {
|
|
368
|
+
let activation;
|
|
369
|
+
try {
|
|
370
|
+
const cwd = await this.resolveDirectory(spec, signal);
|
|
371
|
+
activation = await this.materialize({
|
|
372
|
+
kind: 'external',
|
|
373
|
+
provider,
|
|
374
|
+
...ancestry,
|
|
375
|
+
path,
|
|
376
|
+
signal,
|
|
377
|
+
delivery,
|
|
378
|
+
start: signal => start({
|
|
379
|
+
...request, cwd, signal,
|
|
380
|
+
}),
|
|
381
|
+
});
|
|
382
|
+
this.assertAdmitting(parent);
|
|
383
|
+
signal.throwIfAborted();
|
|
384
|
+
if (reservation !== undefined) {
|
|
385
|
+
establishExternalCatalogChild(parent.session, activation.childId, spec.label, reservation.name);
|
|
444
386
|
}
|
|
445
|
-
|
|
446
|
-
|
|
387
|
+
this.accept(activation);
|
|
388
|
+
return this.receipt(activation);
|
|
389
|
+
}
|
|
390
|
+
catch (error) {
|
|
391
|
+
if (activation !== undefined) {
|
|
392
|
+
try {
|
|
393
|
+
await this.dispose(activation);
|
|
394
|
+
}
|
|
395
|
+
catch (cleanupError) {
|
|
396
|
+
this.ctx.logger.warn(`subagent "${activation.childId}" admission rollback failed: ${failureMessage(cleanupError)}`);
|
|
397
|
+
}
|
|
447
398
|
}
|
|
399
|
+
throw error;
|
|
448
400
|
}
|
|
449
|
-
|
|
450
|
-
}
|
|
451
|
-
finally {
|
|
452
|
-
releaseHold();
|
|
453
|
-
}
|
|
401
|
+
});
|
|
454
402
|
}
|
|
455
403
|
/** Capture the explicit or inherited directory while the parent still owns startup. */
|
|
456
404
|
async resolveDirectory(request, signal) {
|
|
@@ -461,59 +409,156 @@ export class SubagentManager {
|
|
|
461
409
|
return cwd;
|
|
462
410
|
}
|
|
463
411
|
/**
|
|
464
|
-
*
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
* @
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
412
|
+
* Resolve the live subagent tree root from runtime ownership, independent of creator residency.
|
|
413
|
+
* @param caller - exact live Agent selecting the subagent tree.
|
|
414
|
+
* @returns the live subagent tree root.
|
|
415
|
+
* @throws when the caller has no active runtime owner.
|
|
416
|
+
*/
|
|
417
|
+
rootOf(caller) {
|
|
418
|
+
if (this.ctx.agents.get(caller.id) !== caller) {
|
|
419
|
+
throw new SubagentError('subagent tree access requires the exact live caller', 'UNAUTHORIZED');
|
|
420
|
+
}
|
|
421
|
+
const identity = this.host.identityOf(caller.session);
|
|
422
|
+
const delegated = caller.session.header.origin === 'subagent' || identity != null && caller.session.isOwnSeq(identity.seq);
|
|
423
|
+
const activation = this.resident.get(caller.id);
|
|
424
|
+
if (activation !== undefined && activationAgent(activation) === caller
|
|
425
|
+
&& this.ctx.agents.get(activation.root.id) === activation.root)
|
|
426
|
+
return activation.root;
|
|
427
|
+
if (delegated || activation !== undefined) {
|
|
428
|
+
throw new SubagentError('agent has no active runtime owner', 'UNAUTHORIZED');
|
|
429
|
+
}
|
|
430
|
+
return caller;
|
|
431
|
+
}
|
|
432
|
+
/** Capture creation ancestry separately from the root that owns the new instance. */
|
|
433
|
+
ancestryForChild(creator) {
|
|
434
|
+
const root = this.rootOf(creator);
|
|
435
|
+
const ancestors = this.resident.get(creator.id)?.ancestors ?? [];
|
|
436
|
+
return { root, ancestors: [...ancestors, creator.id] };
|
|
437
|
+
}
|
|
438
|
+
/** Claim a sibling name against the creator's committed catalog and in-flight siblings. */
|
|
439
|
+
reserveName(parent, requested) {
|
|
440
|
+
return this.names.reserve(parent, catalogNames(this.host.catalogOf(parent.session)), requested);
|
|
441
|
+
}
|
|
442
|
+
/** Path of an exact live tree member: `''` for the root, undefined for unnamed history. */
|
|
443
|
+
pathOf(member, root) {
|
|
444
|
+
return member === root ? '' : this.resident.get(member.id)?.path;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Report the model-facing names of a live member and its direct creator.
|
|
448
|
+
* @param agent - exact live root or resident tree member.
|
|
449
|
+
* @returns `lead` for the root, otherwise the member's and creator's names or Session ids.
|
|
450
|
+
* @throws when the caller has no active runtime owner.
|
|
451
|
+
*/
|
|
452
|
+
namesOf(agent) {
|
|
453
|
+
const root = this.rootOf(agent);
|
|
454
|
+
if (root === agent)
|
|
455
|
+
return { self: ROOT_AGENT_NAME };
|
|
456
|
+
// rootOf returns another root only for the caller's own resident Activation.
|
|
457
|
+
const activation = this.resident.get(agent.id);
|
|
458
|
+
const parentId = activation.ancestors.at(-1);
|
|
459
|
+
const parentPath = activation.path === undefined
|
|
460
|
+
? parentId === root.id ? '' : this.resident.get(parentId)?.path
|
|
461
|
+
: parentAgentPath(activation.path);
|
|
462
|
+
return { self: agentAddress(activation.path, agent.id), parent: agentAddress(parentPath, parentId) };
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Deliver to another agent in the subagent tree, restoring only the target when absent.
|
|
466
|
+
* @param sender - exact live root or resident continuable Agent.
|
|
467
|
+
* @param targetId - another member's durable Session id.
|
|
468
|
+
* @param content - model-authored message blocks.
|
|
469
|
+
* @param options - cancellation before inbox acceptance.
|
|
470
|
+
* @returns the accepted inbox message id.
|
|
472
471
|
*/
|
|
473
472
|
async sendMessage(sender, targetId, content, options) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
473
|
+
options.signal.throwIfAborted();
|
|
474
|
+
const root = this.rootOf(sender);
|
|
475
|
+
if (sender.id === targetId)
|
|
476
|
+
throw new SubagentError('send_message requires another agent in the subagent tree', 'UNAUTHORIZED');
|
|
477
477
|
this.assertAdmitting(sender);
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
489
|
-
return this.deliverToChild(sender, targetId, content, {
|
|
490
|
-
signal: options.signal,
|
|
491
|
-
delivery: 'steer',
|
|
478
|
+
const message = createAgentMessage(sender, agentAddress(this.pathOf(sender, root), sender.id), content);
|
|
479
|
+
const activation = this.resident.get(sender.id);
|
|
480
|
+
return this.deliverToMember(root, targetId, [...message.content], {
|
|
481
|
+
signal: options.signal, delivery: 'steer', source: message.source,
|
|
482
|
+
authorize: () => { this.assertAdmitting(sender); },
|
|
483
|
+
onAccepted: () => {
|
|
484
|
+
if (activation?.kind === 'local' && activation.ancestors.at(-1) === targetId) {
|
|
485
|
+
activation.omitClosingText = true;
|
|
486
|
+
}
|
|
487
|
+
},
|
|
492
488
|
});
|
|
493
489
|
}
|
|
494
|
-
/**
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
490
|
+
/** Resolve the live owner through verified creation edges without activating ancestors. */
|
|
491
|
+
async resolveCreationAncestry(source, signal, root) {
|
|
492
|
+
const ancestors = [];
|
|
493
|
+
const names = [];
|
|
494
|
+
const seen = new Set([source.header.id]);
|
|
495
|
+
let { header, projections, inheritedEventCount } = source;
|
|
496
|
+
const pathFromRoot = () => names.reduce(joinAgentPath, '');
|
|
497
|
+
while (true) {
|
|
498
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
499
|
+
try {
|
|
500
|
+
signal.throwIfAborted();
|
|
501
|
+
const parentId = header.parentSession;
|
|
502
|
+
const identity = projections?.values.subagent;
|
|
503
|
+
if (parentId === undefined || identity == null || identity.seq < inheritedEventCount)
|
|
504
|
+
break;
|
|
505
|
+
if (seen.has(parentId))
|
|
506
|
+
throw new SubagentError('cyclic subagent creation ancestry', 'UNAUTHORIZED');
|
|
507
|
+
seen.add(parentId);
|
|
508
|
+
const parent = __addDisposableResource(env_1, await this.observeContinuation(parentId, signal), false);
|
|
509
|
+
const entry = parent.projections?.values.subagentCatalog?.find(entry => entry.id === header.id);
|
|
510
|
+
if (entry === undefined) {
|
|
511
|
+
throw new SubagentError('subagent is absent from its creator catalog', 'UNAUTHORIZED');
|
|
512
|
+
}
|
|
513
|
+
ancestors.unshift(parentId);
|
|
514
|
+
names.unshift('name' in entry ? entry.name : undefined);
|
|
515
|
+
if (parentId === root?.id)
|
|
516
|
+
return { root, ancestors, path: pathFromRoot() };
|
|
517
|
+
({ header, projections, inheritedEventCount } = parent);
|
|
518
|
+
}
|
|
519
|
+
catch (e_1) {
|
|
520
|
+
env_1.error = e_1;
|
|
521
|
+
env_1.hasError = true;
|
|
522
|
+
}
|
|
523
|
+
finally {
|
|
524
|
+
__disposeResources(env_1);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
if (root === undefined && ancestors.length > 0 && header.origin !== 'subagent') {
|
|
528
|
+
const owner = this.ctx.agents.get(header.id);
|
|
529
|
+
if (owner === undefined || this.rootOf(owner) !== owner) {
|
|
530
|
+
throw new SubagentError('subagent tree root is not live', 'CONTINUATION_UNAVAILABLE');
|
|
531
|
+
}
|
|
532
|
+
return { root: owner, ancestors, path: pathFromRoot() };
|
|
533
|
+
}
|
|
534
|
+
throw new SubagentError(`agent "${source.header.id}" is not a member of this subagent tree`, 'UNAUTHORIZED');
|
|
505
535
|
}
|
|
506
536
|
/**
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
* @param childId - durable
|
|
537
|
+
* Deliver one host-authored prompt through the target's live subagent tree root.
|
|
538
|
+
* Remote callers must be authorized for the target Session before invoking this operation.
|
|
539
|
+
* @param childId - durable continuable subagent session id.
|
|
510
540
|
* @param content - model-visible prompt blocks.
|
|
511
|
-
* @param
|
|
512
|
-
* @param signal - caller cancellation before inbox acceptance.
|
|
541
|
+
* @param options - inbox destination, durable attribution, and cancellation before acceptance.
|
|
513
542
|
* @returns the accepted durable message id.
|
|
514
543
|
*/
|
|
515
|
-
async
|
|
516
|
-
|
|
544
|
+
async deliverPrompt(childId, content, options) {
|
|
545
|
+
options.signal.throwIfAborted();
|
|
546
|
+
let root = this.resident.get(childId)?.root;
|
|
547
|
+
if (root === undefined) {
|
|
548
|
+
const env_2 = { stack: [], error: void 0, hasError: false };
|
|
549
|
+
try {
|
|
550
|
+
const source = __addDisposableResource(env_2, await this.observeContinuation(childId, options.signal), false);
|
|
551
|
+
({ root } = await this.resolveCreationAncestry(source, options.signal));
|
|
552
|
+
}
|
|
553
|
+
catch (e_2) {
|
|
554
|
+
env_2.error = e_2;
|
|
555
|
+
env_2.hasError = true;
|
|
556
|
+
}
|
|
557
|
+
finally {
|
|
558
|
+
__disposeResources(env_2);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return this.deliverToMember(root, childId, content, options);
|
|
517
562
|
}
|
|
518
563
|
/** Admit local input synchronously before any teardown can begin. */
|
|
519
564
|
deliver(activation, message, delivery) {
|
|
@@ -538,38 +583,6 @@ export class SubagentManager {
|
|
|
538
583
|
throw new SubagentError(`subagent "${childId}" already exists`, 'DUPLICATE_CHILD');
|
|
539
584
|
}
|
|
540
585
|
}
|
|
541
|
-
/**
|
|
542
|
-
* Pre-register `childId` in a continuation-managed parent's owned set so the
|
|
543
|
-
* parent cannot settle while a caller is still establishing or resuming that
|
|
544
|
-
* child. Returns a releaser for the failure path; it removes only a hold
|
|
545
|
-
* this call added, and leaves ownership in place once a live Activation for
|
|
546
|
-
* the child exists.
|
|
547
|
-
* @param parent - the live direct parent the operation is admitted under.
|
|
548
|
-
* @param childId - the durable child the operation addresses.
|
|
549
|
-
* @returns the failure-path releaser; a no-op when nothing was added.
|
|
550
|
-
*/
|
|
551
|
-
holdOwnership(parent, childId) {
|
|
552
|
-
const parentActivation = this.resident.get(parent.id);
|
|
553
|
-
if (parentActivation === undefined || activationAgent(parentActivation) !== parent)
|
|
554
|
-
return () => { };
|
|
555
|
-
if (parentActivation.closing !== undefined) {
|
|
556
|
-
throw new SubagentError(`subagent parent "${parent.id}" is being disposed; the child was not established`, 'ACTIVATION_CLOSING');
|
|
557
|
-
}
|
|
558
|
-
if (parentActivation.ownedChildren.has(childId))
|
|
559
|
-
return () => { };
|
|
560
|
-
parentActivation.ownedChildren.add(childId);
|
|
561
|
-
return () => {
|
|
562
|
-
const live = this.resident.get(childId);
|
|
563
|
-
/* v8 ignore next 4 -- reaching this arm needs another delivery to establish the child
|
|
564
|
-
* between this operation's failure and its releaser running, which no test can schedule
|
|
565
|
-
* deterministically: the ownership edge then belongs to that live Activation, so the
|
|
566
|
-
* conservative keep leaves it for finishDisposal's releaseOwnership. */
|
|
567
|
-
if (live !== undefined && live.closing === undefined)
|
|
568
|
-
return;
|
|
569
|
-
if (parentActivation.ownedChildren.delete(childId))
|
|
570
|
-
this.wake(parentActivation);
|
|
571
|
-
};
|
|
572
|
-
}
|
|
573
586
|
/**
|
|
574
587
|
* Apply activation admission to a resident parent before delivering input.
|
|
575
588
|
* @param parent - exact live Agent receiving the message.
|
|
@@ -593,34 +606,20 @@ export class SubagentManager {
|
|
|
593
606
|
parent.followup(message);
|
|
594
607
|
}
|
|
595
608
|
/**
|
|
596
|
-
* Reject new
|
|
597
|
-
* @param agent - exact live
|
|
609
|
+
* Reject new work from closing members or subagent trees.
|
|
610
|
+
* @param agent - exact live caller whose instance and subagent tree must admit work.
|
|
598
611
|
*/
|
|
599
612
|
assertAdmitting(agent) {
|
|
600
613
|
const closing = this.closingTeardownFor(agent);
|
|
601
|
-
if (closing === undefined)
|
|
614
|
+
if (closing === undefined) {
|
|
615
|
+
if (this.resident.get(agent.id)?.closing !== undefined) {
|
|
616
|
+
throw new SubagentError(`subagent "${agent.id}" is closing`, 'ACTIVATION_CLOSING');
|
|
617
|
+
}
|
|
602
618
|
return;
|
|
619
|
+
}
|
|
603
620
|
throw new SubagentError(closing === 'manager'
|
|
604
621
|
? 'subagents are draining; the operation was not admitted'
|
|
605
|
-
: `
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Authorize one operation against the durable direct-parent lineage.
|
|
609
|
-
* @param parent - exact live Agent claiming direct-parent authority.
|
|
610
|
-
* @param childId - durable child session id addressed by the operation.
|
|
611
|
-
* @param parentSession - durable direct-parent id recorded by the child.
|
|
612
|
-
*/
|
|
613
|
-
authorizeLineage(parent, childId, parentSession) {
|
|
614
|
-
this.assertLiveParent(parent, childId);
|
|
615
|
-
if (parentSession !== parent.id) {
|
|
616
|
-
throw new SubagentError(`subagent "${childId}" belongs to another parent session`, 'UNAUTHORIZED');
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
/** Reject parent authority after the live registry entry changes. */
|
|
620
|
-
assertLiveParent(parent, childId) {
|
|
621
|
-
if (this.ctx.agents.get(parent.id) !== parent) {
|
|
622
|
-
throw new SubagentError(`subagent "${childId}" delivery requires the exact live parent agent`, 'UNAUTHORIZED');
|
|
623
|
-
}
|
|
622
|
+
: `subagent tree "${closing.id}" is draining; the operation was not admitted`, 'DRAINING');
|
|
624
623
|
}
|
|
625
624
|
/**
|
|
626
625
|
* Create or resume one child Agent and publish its Activation.
|
|
@@ -628,23 +627,26 @@ export class SubagentManager {
|
|
|
628
627
|
* @returns the published process-local Activation.
|
|
629
628
|
*/
|
|
630
629
|
materialize(inputs) {
|
|
631
|
-
this.assertAdmitting(inputs.
|
|
630
|
+
this.assertAdmitting(inputs.root);
|
|
632
631
|
inputs.signal.throwIfAborted();
|
|
633
|
-
const
|
|
634
|
-
const pool = this.resident.get(inputs.parent.id)?.pool ?? this.rootPool(inputs.parent);
|
|
632
|
+
const pool = this.rootPool(inputs.root);
|
|
635
633
|
const releaseSlot = pool.reserve(this.maxActiveSubagents());
|
|
634
|
+
return this.materializeTracked(inputs, releaseSlot).catch((error) => {
|
|
635
|
+
releaseSlot();
|
|
636
|
+
throw error;
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
/** Keep subagent tree shutdown and host waits joined to preparation, publication, and rollback. */
|
|
640
|
+
trackStartup(root, signal, start) {
|
|
641
|
+
this.assertAdmitting(root);
|
|
642
|
+
signal.throwIfAborted();
|
|
636
643
|
const settled = Promise.withResolvers();
|
|
637
644
|
const controller = new AbortController();
|
|
638
|
-
const materialization = {
|
|
639
|
-
lineage,
|
|
640
|
-
settled: settled.promise,
|
|
641
|
-
controller,
|
|
642
|
-
};
|
|
645
|
+
const materialization = { root, settled: settled.promise, controller };
|
|
643
646
|
this.materializations.add(materialization);
|
|
644
|
-
return
|
|
645
|
-
releaseSlot();
|
|
647
|
+
return start(AbortSignal.any([signal, controller.signal])).catch((error) => {
|
|
646
648
|
if (error instanceof Error && error.name === 'AbortError')
|
|
647
|
-
this.assertAdmitting(
|
|
649
|
+
this.assertAdmitting(root);
|
|
648
650
|
throw error;
|
|
649
651
|
}).finally(() => {
|
|
650
652
|
this.materializations.delete(materialization);
|
|
@@ -685,42 +687,14 @@ export class SubagentManager {
|
|
|
685
687
|
+ reasons.map(reason => failureMessage(reason)).join('; '), 'ACTIVATION_TEARDOWN_FAILED');
|
|
686
688
|
}
|
|
687
689
|
}
|
|
688
|
-
/** Return the
|
|
689
|
-
closingMembers(root) {
|
|
690
|
-
const existing = this.closingScopes.get(root);
|
|
691
|
-
if (existing !== undefined)
|
|
692
|
-
return existing;
|
|
693
|
-
const members = new Set();
|
|
694
|
-
this.closingScopes.set(root, members);
|
|
695
|
-
return members;
|
|
696
|
-
}
|
|
697
|
-
/** Return the exact currently resolvable ancestry from `agent` upward. */
|
|
698
|
-
liveLineage(agent) {
|
|
699
|
-
const lineage = [agent];
|
|
700
|
-
const seen = new Set([agent.id]);
|
|
701
|
-
let parentSession = agent.session.header.parentSession;
|
|
702
|
-
while (parentSession !== undefined) {
|
|
703
|
-
const parent = this.ctx.agents.get(parentSession);
|
|
704
|
-
if (parent === undefined || seen.has(parent.id))
|
|
705
|
-
break;
|
|
706
|
-
lineage.push(parent);
|
|
707
|
-
seen.add(parent.id);
|
|
708
|
-
parentSession = parent.session.header.parentSession;
|
|
709
|
-
}
|
|
710
|
-
return lineage;
|
|
711
|
-
}
|
|
712
|
-
/** Return the teardown that closed continuable admission for this agent's lineage. */
|
|
690
|
+
/** Return the subagent tree teardown that closed admission for this exact Agent. */
|
|
713
691
|
closingTeardownFor(agent) {
|
|
714
692
|
if (this.draining)
|
|
715
693
|
return 'manager';
|
|
716
|
-
const
|
|
717
|
-
|
|
718
|
-
if (members.has(agent) || lineage.includes(root))
|
|
719
|
-
return root;
|
|
720
|
-
}
|
|
721
|
-
return undefined;
|
|
694
|
+
const root = this.rootOf(agent);
|
|
695
|
+
return this.closingRoots.has(root) ? root : undefined;
|
|
722
696
|
}
|
|
723
|
-
/**
|
|
697
|
+
/** Share one capacity pool across every instance in a subagent tree. */
|
|
724
698
|
rootPool(parent) {
|
|
725
699
|
let pool = this.rootPools.get(parent);
|
|
726
700
|
if (pool === undefined) {
|
|
@@ -730,9 +704,8 @@ export class SubagentManager {
|
|
|
730
704
|
return pool;
|
|
731
705
|
}
|
|
732
706
|
/** Perform one tracked materialization through publication or rollback. */
|
|
733
|
-
async materializeTracked(inputs,
|
|
734
|
-
const { provider,
|
|
735
|
-
let childId = inputs.childId;
|
|
707
|
+
async materializeTracked(inputs, releaseSlot) {
|
|
708
|
+
const { provider, root } = inputs;
|
|
736
709
|
inputs.signal.throwIfAborted();
|
|
737
710
|
let resource;
|
|
738
711
|
if (inputs.kind === 'external') {
|
|
@@ -743,9 +716,8 @@ export class SubagentManager {
|
|
|
743
716
|
const run = await inputs.start(controller.signal);
|
|
744
717
|
resource = { kind: 'external', run, controller };
|
|
745
718
|
void run.result.catch(() => undefined);
|
|
746
|
-
childId = run.id;
|
|
747
719
|
try {
|
|
748
|
-
this.assertChildIdAvailable(
|
|
720
|
+
this.assertChildIdAvailable(run.id);
|
|
749
721
|
}
|
|
750
722
|
catch (error) {
|
|
751
723
|
await run.dispose();
|
|
@@ -757,13 +729,13 @@ export class SubagentManager {
|
|
|
757
729
|
}
|
|
758
730
|
}
|
|
759
731
|
else {
|
|
760
|
-
const { create } = inputs;
|
|
732
|
+
const { create, childId } = inputs;
|
|
761
733
|
let structured;
|
|
762
734
|
const setup = async (childCtx, child) => {
|
|
763
735
|
// Only fresh creation appends the descriptor and delegated policy after
|
|
764
736
|
// the inherited marker; a cold resume replays those persisted events.
|
|
765
737
|
if (inputs.composition.agentPreset !== undefined) {
|
|
766
|
-
await applyChildComposition(childCtx, parent, inputs.composition);
|
|
738
|
+
await applyChildComposition(childCtx, create?.parent, inputs.composition);
|
|
767
739
|
}
|
|
768
740
|
if (create !== undefined) {
|
|
769
741
|
if (inputs.composition.agentPreset !== undefined) {
|
|
@@ -777,24 +749,21 @@ export class SubagentManager {
|
|
|
777
749
|
await workingDirectory.set(child, create.cwd, inputs.signal);
|
|
778
750
|
}
|
|
779
751
|
if (inputs.composition.agentPreset === undefined)
|
|
780
|
-
await applyChildComposition(childCtx, parent, inputs.composition);
|
|
752
|
+
await applyChildComposition(childCtx, create?.parent, inputs.composition);
|
|
781
753
|
if (inputs.outputSchema !== undefined) {
|
|
782
|
-
structured = attachStructuredRuntime(childCtx, inputs.outputSchema, () =>
|
|
783
|
-
&& child.inbox.nextStep.length === 0
|
|
754
|
+
structured = attachStructuredRuntime(childCtx, inputs.outputSchema, () => child.inbox.nextStep.length === 0
|
|
784
755
|
&& child.inbox.nextTurn.length === 0);
|
|
785
756
|
}
|
|
786
757
|
};
|
|
787
758
|
const handle = create === undefined
|
|
788
759
|
? await this.ownerCtx.agents.resume({
|
|
789
760
|
resumeSessionId: childId,
|
|
790
|
-
parentAgent: parent,
|
|
791
761
|
agentOptions: inputs.agentOptions,
|
|
792
762
|
signal: inputs.signal,
|
|
793
763
|
setup,
|
|
794
764
|
})
|
|
795
765
|
: await this.ownerCtx.agents.create({
|
|
796
766
|
sessionId: childId,
|
|
797
|
-
parentAgent: parent,
|
|
798
767
|
meta: create.meta,
|
|
799
768
|
...(create.seed === undefined ? {} : { seed: create.seed }),
|
|
800
769
|
inheritedEventCount: create.inheritedEventCount,
|
|
@@ -812,29 +781,26 @@ export class SubagentManager {
|
|
|
812
781
|
omitClosingText: false,
|
|
813
782
|
};
|
|
814
783
|
}
|
|
815
|
-
const
|
|
784
|
+
const childId = resource.kind === 'local' ? resource.handle.agent.id : resource.run.id;
|
|
816
785
|
const activation = {
|
|
817
|
-
pool,
|
|
818
786
|
releaseSlot,
|
|
819
787
|
childId,
|
|
788
|
+
runId: SubagentRunId(randomUUID()),
|
|
789
|
+
provider,
|
|
820
790
|
...resource,
|
|
821
|
-
|
|
791
|
+
root,
|
|
792
|
+
ancestors: inputs.ancestors,
|
|
793
|
+
path: inputs.path,
|
|
822
794
|
delivery: inputs.delivery ?? 'parent',
|
|
823
795
|
result: Promise.withResolvers(),
|
|
824
796
|
closing: undefined,
|
|
825
|
-
|
|
826
|
-
ownedChildren: new Set(),
|
|
827
|
-
observer,
|
|
828
|
-
announced: false,
|
|
797
|
+
accepted: false,
|
|
829
798
|
};
|
|
830
799
|
void activation.result.promise.catch(() => undefined);
|
|
831
800
|
this.resident.set(childId, activation);
|
|
832
801
|
try {
|
|
833
|
-
this.assertAdmitting(
|
|
802
|
+
this.assertAdmitting(root);
|
|
834
803
|
inputs.signal.throwIfAborted();
|
|
835
|
-
if (this.ctx.agents.get(parent.id) !== parent)
|
|
836
|
-
throw new SubagentError('subagent parent is no longer live', 'UNAUTHORIZED');
|
|
837
|
-
this.acquireOwnership(parent, childId);
|
|
838
804
|
const wakeSettlement = () => { this.wake(activation); };
|
|
839
805
|
if (activation.kind === 'local') {
|
|
840
806
|
observeLocalFailure(activation, wakeSettlement);
|
|
@@ -854,7 +820,9 @@ export class SubagentManager {
|
|
|
854
820
|
activation.omitClosingText = false;
|
|
855
821
|
});
|
|
856
822
|
}
|
|
857
|
-
|
|
823
|
+
// Listeners may read the resident Agent or drain it immediately. Publish
|
|
824
|
+
// after residency and inbox listeners, as the last rollback-protected step.
|
|
825
|
+
this.host.emitLifecycle('subagent/start', this.runInfo(activation), root);
|
|
858
826
|
}
|
|
859
827
|
catch (error) {
|
|
860
828
|
/* v8 ignore next -- rollback failure must not mask the admission failure
|
|
@@ -864,14 +832,19 @@ export class SubagentManager {
|
|
|
864
832
|
}
|
|
865
833
|
return activation;
|
|
866
834
|
}
|
|
867
|
-
/**
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
835
|
+
/** Identity shared by one residency epoch's start/end pair, including cold resumes. */
|
|
836
|
+
runInfo(activation) {
|
|
837
|
+
return {
|
|
838
|
+
runId: activation.runId,
|
|
839
|
+
provider: activation.provider,
|
|
840
|
+
parentSessionId: activation.ancestors.at(-1),
|
|
841
|
+
id: activation.childId,
|
|
842
|
+
local: activation.kind === 'local',
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
/** Record acceptance and begin settlement observation before returning the receipt. */
|
|
846
|
+
accept(activation) {
|
|
847
|
+
activation.accepted = true;
|
|
875
848
|
if (activation.kind === 'local') {
|
|
876
849
|
this.watchLocalSettlement(activation);
|
|
877
850
|
}
|
|
@@ -889,29 +862,10 @@ export class SubagentManager {
|
|
|
889
862
|
finally {
|
|
890
863
|
this.resident.delete(activation.childId);
|
|
891
864
|
activation.releaseSlot();
|
|
892
|
-
this.releaseOwnership(activation.childId);
|
|
893
865
|
activation.result.reject(error);
|
|
894
866
|
}
|
|
895
867
|
});
|
|
896
868
|
}
|
|
897
|
-
/** Register the child in a continuation-managed parent's owned set. */
|
|
898
|
-
acquireOwnership(parent, childId) {
|
|
899
|
-
const parentActivation = this.resident.get(parent.id);
|
|
900
|
-
if (parentActivation === undefined)
|
|
901
|
-
return;
|
|
902
|
-
if (parentActivation.closing !== undefined) {
|
|
903
|
-
throw new SubagentError(`subagent parent "${parent.id}" is being disposed; the child was not established`, 'ACTIVATION_CLOSING');
|
|
904
|
-
}
|
|
905
|
-
parentActivation.ownedChildren.add(childId);
|
|
906
|
-
this.wake(parentActivation);
|
|
907
|
-
}
|
|
908
|
-
/** Remove one child from its live owner's set and let that owner re-check settlement. */
|
|
909
|
-
releaseOwnership(childId) {
|
|
910
|
-
for (const candidate of this.resident.values()) {
|
|
911
|
-
if (candidate.ownedChildren.delete(childId))
|
|
912
|
-
this.wake(candidate);
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
869
|
/** Let a settlement watcher re-check residency after relevant state changes. */
|
|
916
870
|
wake(activation) {
|
|
917
871
|
if (activation.kind === 'external')
|
|
@@ -977,20 +931,18 @@ export class SubagentManager {
|
|
|
977
931
|
reportTeardownFailure(activation, error) {
|
|
978
932
|
this.ctx.logger.warn(`subagent "${activation.childId}" activation teardown failed: ${failureMessage(error)}`);
|
|
979
933
|
}
|
|
980
|
-
/** Check pending input
|
|
934
|
+
/** Check pending input before closing admission. */
|
|
981
935
|
settlementState(activation, observation) {
|
|
982
936
|
if (activation.closing !== undefined)
|
|
983
937
|
return 'closed';
|
|
984
938
|
if (activation.poke !== observation)
|
|
985
939
|
return 'retry';
|
|
986
940
|
const inbox = activation.handle.agent.inbox;
|
|
987
|
-
if (activation.ownedChildren.size > 0)
|
|
988
|
-
return 'wait';
|
|
989
941
|
if (activation.failureAt === undefined && (inbox.nextTurn.length > 0 || inbox.nextStep.length > 0))
|
|
990
942
|
return 'wait';
|
|
991
943
|
return 'ready';
|
|
992
944
|
}
|
|
993
|
-
/**
|
|
945
|
+
/** Stop and release one instance without affecting its creation descendants. */
|
|
994
946
|
async finishDisposal(activation, stop) {
|
|
995
947
|
this.wake(activation);
|
|
996
948
|
const { childId } = activation;
|
|
@@ -1016,27 +968,6 @@ export class SubagentManager {
|
|
|
1016
968
|
if (stop) {
|
|
1017
969
|
activation.handle.agent.cancel({ kind: 'parent' });
|
|
1018
970
|
const idle = activation.handle.agent.whenIdle();
|
|
1019
|
-
const children = [...activation.ownedChildren]
|
|
1020
|
-
.map(child => this.resident.get(child))
|
|
1021
|
-
.filter((child) => child !== undefined);
|
|
1022
|
-
const pending = [...this.materializations].filter(item => item.lineage.includes(activation.handle.agent));
|
|
1023
|
-
const childDisposals = children.map(child => this.dispose(child));
|
|
1024
|
-
for (const item of pending)
|
|
1025
|
-
item.controller.abort();
|
|
1026
|
-
childDisposals.push(...pending.map(item => item.settled));
|
|
1027
|
-
const childFailures = await Promise.all(childDisposals.map(async (disposal) => {
|
|
1028
|
-
try {
|
|
1029
|
-
await disposal;
|
|
1030
|
-
return undefined;
|
|
1031
|
-
}
|
|
1032
|
-
catch (error) {
|
|
1033
|
-
return error;
|
|
1034
|
-
}
|
|
1035
|
-
}));
|
|
1036
|
-
const reasons = childFailures.filter(reason => reason !== undefined);
|
|
1037
|
-
if (reasons.length > 0) {
|
|
1038
|
-
failures.push(new SubagentError(`subagent "${childId}" child teardown failed: ${reasons.map(reason => failureMessage(reason)).join('; ')}`, 'ACTIVATION_TEARDOWN_FAILED'));
|
|
1039
|
-
}
|
|
1040
971
|
await idle;
|
|
1041
972
|
await this.flushFinalState(activation);
|
|
1042
973
|
}
|
|
@@ -1064,12 +995,21 @@ export class SubagentManager {
|
|
|
1064
995
|
failure = new SubagentError(`subagent "${childId}" activation teardown failed at ${failures.length} boundaries: `
|
|
1065
996
|
+ failures.map(item => detail(item)).join('; '), 'ACTIVATION_TEARDOWN_FAILED', { cause: new AggregateError(failures) });
|
|
1066
997
|
}
|
|
998
|
+
if (failure !== undefined && result.stopReason === 'completed' && child !== undefined
|
|
999
|
+
&& (child.inbox.nextStep.length > 0 || child.inbox.nextTurn.length > 0)) {
|
|
1000
|
+
result = { ...result, stopReason: 'error' };
|
|
1001
|
+
}
|
|
1067
1002
|
this.resident.delete(childId);
|
|
1068
1003
|
activation.releaseSlot();
|
|
1069
1004
|
const terminal = failure === undefined ? result : { output: [], stopReason: 'error' };
|
|
1070
1005
|
this.notifySettlement(activation, result);
|
|
1071
|
-
|
|
1072
|
-
|
|
1006
|
+
// The sole end publication follows resource release and parent notification,
|
|
1007
|
+
// before result delivery.
|
|
1008
|
+
this.host.emitLifecycle('subagent/end', {
|
|
1009
|
+
...this.runInfo(activation),
|
|
1010
|
+
stopReason: terminal.stopReason,
|
|
1011
|
+
...terminal.output.length === 0 ? {} : { lastAssistantMessage: terminal.output },
|
|
1012
|
+
}, activation.root);
|
|
1073
1013
|
if (resultFailure === undefined)
|
|
1074
1014
|
activation.result.resolve(result);
|
|
1075
1015
|
else
|
|
@@ -1079,13 +1019,15 @@ export class SubagentManager {
|
|
|
1079
1019
|
}
|
|
1080
1020
|
/** Deliver the captured execution result to the durable direct parent after cleanup. */
|
|
1081
1021
|
notifySettlement(activation, terminal) {
|
|
1082
|
-
if (!activation.
|
|
1022
|
+
if (!activation.accepted || activation.delivery === 'caller')
|
|
1083
1023
|
return;
|
|
1084
1024
|
try {
|
|
1085
|
-
const
|
|
1086
|
-
|
|
1025
|
+
const parentId = activation.ancestors.at(-1);
|
|
1026
|
+
const parent = this.ctx.agents.get(parentId);
|
|
1027
|
+
if (parent === undefined || parent.ctx.fiber.state !== 2 /* FiberState.ACTIVE */
|
|
1028
|
+
|| this.rootOf(parent) !== activation.root)
|
|
1087
1029
|
return;
|
|
1088
|
-
const message = createSettlementMessage(activation.childId, terminal, activation.kind === 'local', activation.kind === 'local' && activation.omitClosingText);
|
|
1030
|
+
const message = createSettlementMessage(activation.childId, agentAddress(activation.path, activation.childId), terminal, activation.kind === 'local', activation.kind === 'local' && activation.omitClosingText);
|
|
1089
1031
|
if (this.closingTeardownFor(parent) !== undefined) {
|
|
1090
1032
|
parent.inject(message);
|
|
1091
1033
|
return;
|
|
@@ -1111,144 +1053,118 @@ export class SubagentManager {
|
|
|
1111
1053
|
receipt(activation) {
|
|
1112
1054
|
return {
|
|
1113
1055
|
childId: activation.childId,
|
|
1056
|
+
name: agentAddress(activation.path, activation.childId),
|
|
1114
1057
|
result: activation.result.promise,
|
|
1115
1058
|
dispose: () => this.dispose(activation),
|
|
1116
1059
|
};
|
|
1117
1060
|
}
|
|
1118
|
-
/**
|
|
1119
|
-
async
|
|
1120
|
-
this.assertAdmitting(parent);
|
|
1121
|
-
const releaseHold = this.holdOwnership(parent, childId);
|
|
1122
|
-
try {
|
|
1123
|
-
return await this.deliverFollowup(parent, childId, content, options);
|
|
1124
|
-
}
|
|
1125
|
-
catch (error) {
|
|
1126
|
-
releaseHold();
|
|
1127
|
-
throw error;
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
/** The delivery loop behind {@link deliverToChild}, run under the parent hold. */
|
|
1131
|
-
async deliverFollowup(parent, childId, content, options) {
|
|
1061
|
+
/** Serialize target restoration and inbox admission; closure is awaited outside the lock. */
|
|
1062
|
+
async deliverToMember(root, childId, content, options) {
|
|
1132
1063
|
while (true) {
|
|
1133
|
-
|
|
1064
|
+
this.assertAdmitting(root);
|
|
1065
|
+
options.signal.throwIfAborted();
|
|
1066
|
+
const result = await this.locks.run(childId, async () => {
|
|
1067
|
+
if (childId === root.id) {
|
|
1068
|
+
if (contentHasImage(content))
|
|
1069
|
+
await this.assertImageCapable(root, options.signal);
|
|
1070
|
+
options.signal.throwIfAborted();
|
|
1071
|
+
options.authorize?.();
|
|
1072
|
+
this.assertAdmitting(root);
|
|
1073
|
+
const message = createUserMessage({ content, source: options.source });
|
|
1074
|
+
this.sendWaking(root, message, options.delivery);
|
|
1075
|
+
options.onAccepted?.();
|
|
1076
|
+
return { messageId: message.id };
|
|
1077
|
+
}
|
|
1134
1078
|
const activation = this.resident.get(childId);
|
|
1135
|
-
if (activation === undefined)
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
const disposal = activation.closing;
|
|
1139
|
-
/* v8 ignore next 3 -- the send-versus-dispose cutoff needs a delivery to
|
|
1140
|
-
* observe the transaction inside the same critical section that opened it. */
|
|
1141
|
-
if (disposal !== undefined) {
|
|
1142
|
-
return disposal.then(() => undefined, () => undefined);
|
|
1079
|
+
if (activation === undefined) {
|
|
1080
|
+
const restored = await this.restoreTarget(root, childId, options.signal);
|
|
1081
|
+
return { messageId: await this.submitMaterialized(restored, content, options, root) };
|
|
1143
1082
|
}
|
|
1144
|
-
if (
|
|
1083
|
+
if (activation.root !== root)
|
|
1084
|
+
throw new SubagentError('target belongs to another subagent tree', 'UNAUTHORIZED');
|
|
1085
|
+
requireLocalActivation(activation);
|
|
1086
|
+
if (activation.closing === undefined && contentHasImage(content)) {
|
|
1145
1087
|
await this.assertImageCapable(requireLocalActivation(activation).handle.agent, options.signal);
|
|
1146
|
-
if (activation.closing !== undefined) {
|
|
1147
|
-
await Promise.allSettled([activation.closing]);
|
|
1148
|
-
return undefined;
|
|
1149
|
-
}
|
|
1150
1088
|
}
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1089
|
+
if (activation.closing !== undefined)
|
|
1090
|
+
return { closing: activation.closing };
|
|
1091
|
+
const messageId = this.submitAdmitted(activation, content, options, root);
|
|
1092
|
+
return { messageId };
|
|
1154
1093
|
});
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
this.assertAdmitting(parent);
|
|
1159
|
-
options.signal.throwIfAborted();
|
|
1160
|
-
/* v8 ignore stop */
|
|
1094
|
+
if ('messageId' in result)
|
|
1095
|
+
return result.messageId;
|
|
1096
|
+
await Promise.allSettled([result.closing]);
|
|
1161
1097
|
}
|
|
1162
1098
|
}
|
|
1163
|
-
/**
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
* transaction between exact-agent authorization and this no-await span. */
|
|
1167
|
-
if (activation.closing !== undefined) {
|
|
1168
|
-
throw new SubagentError(`subagent "${sender.id}" activation is being disposed; the message was not delivered`, 'ACTIVATION_CLOSING');
|
|
1169
|
-
}
|
|
1170
|
-
const parent = this.ctx.agents.get(activation.parent.id);
|
|
1171
|
-
if (parent !== activation.parent) {
|
|
1172
|
-
throw new SubagentError('direct parent is not live; the message was not delivered', 'PARENT_UNAVAILABLE');
|
|
1173
|
-
}
|
|
1174
|
-
const message = createAgentMessage(sender, content);
|
|
1175
|
-
this.sendAgentMessage(parent, message);
|
|
1176
|
-
activation.omitClosingText = true;
|
|
1177
|
-
return message.id;
|
|
1178
|
-
}
|
|
1179
|
-
/** Send one Agent message while translating only the target's own rejection. */
|
|
1180
|
-
sendAgentMessage(parent, message) {
|
|
1099
|
+
/** Read continuation history, preserving cancellation and reporting unavailable storage. */
|
|
1100
|
+
async observeContinuation(childId, signal) {
|
|
1101
|
+
const query = this.requireSessionQuery();
|
|
1181
1102
|
try {
|
|
1182
|
-
|
|
1103
|
+
return await query.observeSession(childId, { signal });
|
|
1183
1104
|
}
|
|
1184
1105
|
catch (error) {
|
|
1185
|
-
|
|
1106
|
+
signal.throwIfAborted();
|
|
1107
|
+
throw new SubagentError(`subagent "${childId}" is unavailable`, 'NOT_RESUMABLE', { cause: error });
|
|
1186
1108
|
}
|
|
1187
1109
|
}
|
|
1188
|
-
/**
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
async coldResume(parent, childId, content, options) {
|
|
1193
|
-
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
1194
|
-
try {
|
|
1195
|
-
const query = this.requireSessionQuery();
|
|
1196
|
-
let observation;
|
|
1197
|
-
try {
|
|
1198
|
-
observation = await query.observeSession(childId, {
|
|
1199
|
-
signal: options.signal,
|
|
1200
|
-
});
|
|
1201
|
-
}
|
|
1202
|
-
catch (error) {
|
|
1203
|
-
options.signal.throwIfAborted();
|
|
1204
|
-
throw new SubagentError(`subagent "${childId}" is unavailable`, 'NOT_RESUMABLE', { cause: error });
|
|
1205
|
-
}
|
|
1206
|
-
const source = __addDisposableResource(env_1, observation, false);
|
|
1207
|
-
this.assertAdmitting(parent);
|
|
1208
|
-
this.authorizeLineage(parent, childId, source.header.parentSession);
|
|
1209
|
-
const ownEvents = source.events.slice(source.inheritedEventCount);
|
|
1210
|
-
const descriptor = foldSubagentDescriptor(ownEvents);
|
|
1211
|
-
if (descriptor === undefined || descriptor.mode !== 'continuable') {
|
|
1212
|
-
throw new SubagentError(`subagent "${childId}" has no supported continuation state and cannot be resumed; choose a different target`, 'NOT_RESUMABLE');
|
|
1213
|
-
}
|
|
1214
|
-
let activation;
|
|
1110
|
+
/** Reconstruct only the target under its subagent tree root; the caller owns acceptance or rollback. */
|
|
1111
|
+
async restoreTarget(root, childId, signal) {
|
|
1112
|
+
return this.trackStartup(root, signal, async (signal) => {
|
|
1113
|
+
const env_3 = { stack: [], error: void 0, hasError: false };
|
|
1215
1114
|
try {
|
|
1216
|
-
const
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1115
|
+
const source = __addDisposableResource(env_3, await this.observeContinuation(childId, signal), false);
|
|
1116
|
+
this.assertAdmitting(root);
|
|
1117
|
+
const ownEvents = source.events.slice(source.inheritedEventCount);
|
|
1118
|
+
const descriptor = foldSubagentDescriptor(ownEvents);
|
|
1119
|
+
if (descriptor === undefined || descriptor.mode !== 'continuable') {
|
|
1120
|
+
throw new SubagentError(`subagent "${childId}" has no supported continuation state and cannot be resumed; choose a different target`, 'NOT_RESUMABLE');
|
|
1220
1121
|
}
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1122
|
+
const { ancestors, path } = await this.resolveCreationAncestry(source, signal, root);
|
|
1123
|
+
let activation;
|
|
1124
|
+
try {
|
|
1125
|
+
const projectedPreset = source.projections?.values.agentPreset;
|
|
1126
|
+
if (projectedPreset === undefined && source.events.some(event => event.type === 'agent-preset/selected')) {
|
|
1127
|
+
throw new SubagentError(`subagent "${childId}" has no agent preset projection for restoration`, 'NOT_RESUMABLE');
|
|
1128
|
+
}
|
|
1129
|
+
const agentPreset = projectedPreset ?? source.header.agentPreset;
|
|
1130
|
+
activation = await this.materialize({
|
|
1131
|
+
kind: 'local',
|
|
1132
|
+
childId,
|
|
1133
|
+
provider: descriptor.provider,
|
|
1134
|
+
root,
|
|
1135
|
+
ancestors,
|
|
1136
|
+
path,
|
|
1137
|
+
agentOptions: {
|
|
1138
|
+
...descriptor.agentProvider !== undefined ? { provider: descriptor.agentProvider } : {},
|
|
1139
|
+
...descriptor.agentModel !== undefined ? { model: descriptor.agentModel } : {},
|
|
1140
|
+
...descriptor.agentReasoningEffort !== undefined
|
|
1141
|
+
? { reasoningEffort: ReasoningEffortId(descriptor.agentReasoningEffort) }
|
|
1142
|
+
: {},
|
|
1143
|
+
},
|
|
1144
|
+
composition: {
|
|
1145
|
+
agentPreset: agentPreset ?? undefined,
|
|
1146
|
+
persona: descriptor.persona,
|
|
1147
|
+
toolFilter: descriptor.toolFilter,
|
|
1148
|
+
},
|
|
1149
|
+
signal,
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
catch (error) {
|
|
1153
|
+
signal.throwIfAborted();
|
|
1154
|
+
if (error instanceof SubagentError)
|
|
1155
|
+
throw error;
|
|
1156
|
+
throw new SubagentError(`subagent "${childId}" is unavailable`, 'NOT_RESUMABLE', { cause: error });
|
|
1157
|
+
}
|
|
1158
|
+
return activation;
|
|
1236
1159
|
}
|
|
1237
|
-
catch (
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
throw error;
|
|
1241
|
-
throw new SubagentError(`subagent "${childId}" is unavailable`, 'NOT_RESUMABLE', { cause: error });
|
|
1160
|
+
catch (e_3) {
|
|
1161
|
+
env_3.error = e_3;
|
|
1162
|
+
env_3.hasError = true;
|
|
1242
1163
|
}
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
env_1.hasError = true;
|
|
1248
|
-
}
|
|
1249
|
-
finally {
|
|
1250
|
-
__disposeResources(env_1);
|
|
1251
|
-
}
|
|
1164
|
+
finally {
|
|
1165
|
+
__disposeResources(env_3);
|
|
1166
|
+
}
|
|
1167
|
+
});
|
|
1252
1168
|
}
|
|
1253
1169
|
/** Admit a materialized child, commit its creation fact, and release it on failure. */
|
|
1254
1170
|
async submitMaterialized(activation, content, options, parent, commit) {
|
|
@@ -1261,7 +1177,7 @@ export class SubagentManager {
|
|
|
1261
1177
|
}
|
|
1262
1178
|
const messageId = this.submitAdmitted(activation, content, options, parent);
|
|
1263
1179
|
commit?.();
|
|
1264
|
-
this.
|
|
1180
|
+
this.accept(activation);
|
|
1265
1181
|
return messageId;
|
|
1266
1182
|
}
|
|
1267
1183
|
catch (error) {
|
|
@@ -1276,19 +1192,17 @@ export class SubagentManager {
|
|
|
1276
1192
|
}
|
|
1277
1193
|
/** Build and submit one message across the final synchronous admission cutoff. */
|
|
1278
1194
|
submitAdmitted(activation, content, options, parent) {
|
|
1279
|
-
const message = options.source
|
|
1280
|
-
? createAgentMessage(parent, content)
|
|
1281
|
-
: createUserMessage({ content, source: options.source });
|
|
1195
|
+
const message = createUserMessage({ content, source: options.source });
|
|
1282
1196
|
options.signal.throwIfAborted();
|
|
1197
|
+
options.authorize?.();
|
|
1283
1198
|
this.assertAdmitting(parent);
|
|
1284
|
-
this.authorizeLineage(parent, activation.childId, activation.parent.id);
|
|
1285
|
-
this.acquireOwnership(parent, activation.childId);
|
|
1286
1199
|
try {
|
|
1287
1200
|
this.deliver(activation, message, options.delivery);
|
|
1288
1201
|
}
|
|
1289
1202
|
finally {
|
|
1290
1203
|
this.wake(activation);
|
|
1291
1204
|
}
|
|
1205
|
+
options.onAccepted?.();
|
|
1292
1206
|
return message.id;
|
|
1293
1207
|
}
|
|
1294
1208
|
/** Refuse image content for a child whose fixed model accepts text only. */
|
|
@@ -1323,15 +1237,4 @@ export class SubagentManager {
|
|
|
1323
1237
|
}
|
|
1324
1238
|
}
|
|
1325
1239
|
export default SubagentManager;
|
|
1326
|
-
/** Creation-time selection precedes the own descriptor; historical forks can select after it. */
|
|
1327
|
-
function hasIndependentPresetSelection(events) {
|
|
1328
|
-
for (const event of events) {
|
|
1329
|
-
if (event.type === 'subagent/descriptor')
|
|
1330
|
-
return false;
|
|
1331
|
-
if (event.type === 'agent-preset/selected')
|
|
1332
|
-
return true;
|
|
1333
|
-
}
|
|
1334
|
-
/* v8 ignore next -- coldResume rejects logs without a supported own descriptor before this lookup. */
|
|
1335
|
-
return false;
|
|
1336
|
-
}
|
|
1337
1240
|
//# sourceMappingURL=manager.js.map
|