@fishwithcat/dsh-subagent 0.2.2-alpha.2 → 0.2.2-alpha.3

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.
@@ -1,24 +1,35 @@
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, parent relationships, and resource release across all providers.
4
+ * capacity, runtime ownership, and resource release across all providers.
5
5
  *
6
6
  * @module @deepseek-ai/dsh-subagent/manager
7
7
  */
8
8
  import type { Context } from '@deepseek-ai/cordis';
9
9
  import type { Agent } from '@deepseek-ai/dsh-agent';
10
10
  import type { ContentBlock, MessageId, MessageSource } from '@deepseek-ai/dsh-llm';
11
- import type { SessionId } from '@deepseek-ai/dsh-session';
11
+ import type { Session, SessionId } from '@deepseek-ai/dsh-session';
12
12
  import type { DelegatedApprovalPolicy } from './child-agent.ts';
13
- import type { ActivationObserver } from './lifecycle.ts';
13
+ import type { LifecycleEmitter } from './lifecycle.ts';
14
+ import type { SubagentIdentityProjection } from './projection-types.ts';
14
15
  import type { ContinuableCreateRequest, ContinuableCreateSpec, SubagentInterruptAuthority, SubagentActivationSpec, SubagentActivation, SubagentRun, SubagentSendMessageOptions, ResolvedSubagentStartRequest } from './types.ts';
16
+ import type { SubagentDelivery } from './control-types.ts';
17
+ /** Inputs shared by model steering and human prompt delivery. */
18
+ type ChildDeliveryOptions = {
19
+ readonly delivery: SubagentDelivery;
20
+ readonly source: MessageSource;
21
+ readonly signal: AbortSignal;
22
+ /** Recheck the original model caller at final inbox admission. */
23
+ readonly authorize?: () => void;
24
+ /** Record successful inbox admission before another asynchronous operation can intervene. */
25
+ readonly onAccepted?: () => void;
26
+ };
15
27
  /** Package-private hooks supplied by the owning service. */
16
28
  interface SubagentHost {
17
- startExternal(name: string, request: ResolvedSubagentStartRequest): Promise<SubagentRun>;
18
- /** Resolve one provider's detached continuable-creation contribution. */
19
- prepareContinuable(name: string, request: ContinuableCreateRequest): Promise<ContinuableCreateSpec>;
20
- /** Build the lifecycle observer for one Activation residency epoch. */
21
- observeActivation(provider: string, childId: SessionId, parent: Agent): ActivationObserver;
29
+ /** Read the current descriptor identity through the service-owned projection. */
30
+ identityOf(session: Session): SubagentIdentityProjection | undefined;
31
+ /** Publish lifecycle events through the owning service's scoped dispatcher. */
32
+ readonly emitLifecycle: LifecycleEmitter;
22
33
  }
23
34
  /** Own subagent requests and the resources of every live activation. */
24
35
  export declare class SubagentManager {
@@ -36,18 +47,13 @@ export declare class SubagentManager {
36
47
  private readonly locks;
37
48
  /** Structural Cordis owner of every Activation handle. */
38
49
  private readonly ownerCtx;
39
- /**
40
- * Exact roots whose host teardown has begun, with the live lineage members
41
- * observed under each root. Entries remain until that exact root leaves the
42
- * Agent registry, closing admission throughout its host's teardown without
43
- * poisoning a later same-id replacement.
44
- */
45
- private readonly closingScopes;
50
+ /** Exact subagent tree roots whose host teardown has closed admission. */
51
+ private readonly closingRoots;
46
52
  private draining;
47
53
  /**
48
54
  * Build one manager inside the service's Agent-injected context.
49
55
  * @param ctx - context providing Agents, Sessions, and teardown ownership.
50
- * @param host - provider dispatch and lifecycle observation owned by the service.
56
+ * @param host - descriptor reads and lifecycle publication owned by the service.
51
57
  * @param maxActiveSubagents - the current configured child capacity.
52
58
  * @param delegatedApproval - the configured approval policy seed for each local child.
53
59
  */
@@ -55,81 +61,74 @@ export declare class SubagentManager {
55
61
  /**
56
62
  * Interrupt one live continuable child's current turn under the supplied authority.
57
63
  * @param targetSessionId - the durable child session id to interrupt.
58
- * @param authority - the human parent address or exact live ancestor Agent.
64
+ * @param authority - a trusted user operation or exact live ancestor Agent.
59
65
  */
60
66
  interrupt(targetSessionId: SessionId, authority: SubagentInterruptAuthority): void;
61
67
  /**
62
68
  * Close admission, await every already-admitted materialization through
63
- * publication or rollback, then dispose the stable live Activation graph
64
- * child-first.
69
+ * publication or rollback, then dispose every agent in the subagent tree.
65
70
  */
66
71
  drain(): Promise<void>;
67
72
  /**
68
- * Join progressing child work, leaving idle parked inboxes resident.
69
- * @param parent - exact live parent whose descendants are observed.
70
- * @returns whether work was joined and the host must recheck parent completion.
73
+ * Join progressing subagent tree work for its root, leaving idle parked inboxes resident.
74
+ * @param root - exact live subagent tree root; ordinary members return false.
75
+ * @returns whether work was joined and the host must recheck root completion.
71
76
  */
72
- waitForChildren(parent: Agent): Promise<boolean>;
77
+ waitForTreeWork(root: Agent): Promise<boolean>;
73
78
  /**
74
- * Stop the managed descendants of exact live host-owned parents.
75
- * @param parents - exact live roots whose managed descendants must stop.
79
+ * Stop every member of the selected subagent trees; ordinary members do not close a subagent tree.
80
+ * @param parents - exact live subagent tree roots whose instances must stop.
76
81
  */
77
82
  drainDescendants(parents: readonly Agent[]): Promise<void>;
78
- /**
79
- * Release selected resident direct children of one exact live parent.
80
- * @param parent - exact live direct parent authorizing the selected release.
81
- * @param childIds - durable direct-child ids to release when resident.
82
- */
83
- drainChildren(parent: Agent, childIds: readonly SessionId[]): Promise<void>;
84
83
  /**
85
84
  * Start one continuable background child and resolve at initial inbox acceptance.
86
85
  * Every earlier failure disposes any created handle and rolls back Activation
87
- * and parent ownership without returning either id.
86
+ * and its capacity slot without returning either id.
88
87
  * @param spec - provider, delegation request, and caller cancellation.
88
+ * @param prepare - creation contribution bound to the checked provider.
89
89
  * @returns the durable child id and accepted initial prompt message id.
90
90
  */
91
- startLocal(spec: SubagentActivationSpec): Promise<SubagentActivation & {
91
+ startLocal(spec: SubagentActivationSpec, prepare: (request: ContinuableCreateRequest) => Promise<ContinuableCreateSpec>): Promise<SubagentActivation & {
92
92
  messageId: MessageId;
93
93
  }>;
94
94
  /**
95
95
  * Start an external backend under activation ownership.
96
96
  * @param spec - task, provider, cancellation, and delivery policy.
97
+ * @param start - execution bound to the checked provider.
97
98
  * @returns the accepted activation and its result/disposal capabilities.
98
99
  */
99
- startExternal(spec: SubagentActivationSpec): Promise<SubagentActivation>;
100
+ startExternal(spec: SubagentActivationSpec, start: (request: ResolvedSubagentStartRequest) => Promise<SubagentRun>): Promise<SubagentActivation>;
100
101
  /** Capture the explicit or inherited directory while the parent still owns startup. */
101
102
  private resolveDirectory;
102
103
  /**
103
- * Deliver one model-authored message to a direct continuable child or to the
104
- * sender's direct parent. A missing direct child cold-resumes through the
105
- * ordinary continuation lifecycle.
106
- * @param sender - exact live Agent authorizing and originating the message.
107
- * @param targetId - durable direct-parent or direct-child session id.
108
- * @param content - model-authored content to deliver.
109
- * @param options - caller cancellation before acceptance.
110
- * @returns the accepted message's inbox id.
104
+ * Resolve the live subagent tree root from runtime ownership, independent of creator residency.
105
+ * @param caller - exact live Agent selecting the subagent tree.
106
+ * @returns the live subagent tree root.
107
+ * @throws when the caller has no active runtime owner.
111
108
  */
112
- sendMessage(sender: Agent, targetId: SessionId, content: ContentBlock[], options: SubagentSendMessageOptions): Promise<MessageId>;
109
+ rootOf(caller: Agent): Agent;
110
+ /** Capture creation ancestry separately from the root that owns the new instance. */
111
+ private ancestryForChild;
113
112
  /**
114
- * Queue one human-authored prompt as a distinct direct-child turn.
115
- * @param parent - exact live direct parent authorizing delivery.
116
- * @param childId - durable direct-child session id.
117
- * @param content - model-visible prompt blocks.
118
- * @param source - durable attribution for the human prompt.
119
- * @param signal - caller cancellation before inbox acceptance.
120
- * @returns the accepted durable message id.
113
+ * Deliver to another agent in the subagent tree, restoring only the target when absent.
114
+ * @param sender - exact live root or resident continuable Agent.
115
+ * @param targetId - another member's durable Session id.
116
+ * @param content - model-authored message blocks.
117
+ * @param options - cancellation before inbox acceptance.
118
+ * @returns the accepted inbox message id.
121
119
  */
122
- queuePrompt(parent: Agent, childId: SessionId, content: ContentBlock[], source: MessageSource, signal: AbortSignal): Promise<MessageId>;
120
+ sendMessage(sender: Agent, targetId: SessionId, content: ContentBlock[], options: SubagentSendMessageOptions): Promise<MessageId>;
121
+ /** Resolve the live owner through verified creation edges without activating ancestors. */
122
+ private resolveCreationAncestry;
123
123
  /**
124
- * Steer one host-authored prompt to a direct continuable child.
125
- * @param parent - exact live direct parent authorizing delivery.
126
- * @param childId - durable direct-child session id.
124
+ * Deliver one host-authored prompt through the target's live subagent tree root.
125
+ * Remote callers must be authorized for the target Session before invoking this operation.
126
+ * @param childId - durable continuable subagent session id.
127
127
  * @param content - model-visible prompt blocks.
128
- * @param source - durable attribution for the host prompt.
129
- * @param signal - caller cancellation before inbox acceptance.
128
+ * @param options - inbox destination, durable attribution, and cancellation before acceptance.
130
129
  * @returns the accepted durable message id.
131
130
  */
132
- steerPrompt(parent: Agent, childId: SessionId, content: ContentBlock[], source: MessageSource, signal: AbortSignal): Promise<MessageId>;
131
+ deliverPrompt(childId: SessionId, content: ContentBlock[], options: Pick<ChildDeliveryOptions, 'delivery' | 'source' | 'signal'>): Promise<MessageId>;
133
132
  /** Admit local input synchronously before any teardown can begin. */
134
133
  private deliver;
135
134
  /**
@@ -137,17 +136,6 @@ export declare class SubagentManager {
137
136
  * @param childId - proposed durable child session id.
138
137
  */
139
138
  private assertChildIdAvailable;
140
- /**
141
- * Pre-register `childId` in a continuation-managed parent's owned set so the
142
- * parent cannot settle while a caller is still establishing or resuming that
143
- * child. Returns a releaser for the failure path; it removes only a hold
144
- * this call added, and leaves ownership in place once a live Activation for
145
- * the child exists.
146
- * @param parent - the live direct parent the operation is admitted under.
147
- * @param childId - the durable child the operation addresses.
148
- * @returns the failure-path releaser; a no-op when nothing was added.
149
- */
150
- private holdOwnership;
151
139
  /**
152
140
  * Apply activation admission to a resident parent before delivering input.
153
141
  * @param parent - exact live Agent receiving the message.
@@ -156,25 +144,18 @@ export declare class SubagentManager {
156
144
  */
157
145
  private sendWaking;
158
146
  /**
159
- * Reject new admission once the manager or this exact parent tree began draining.
160
- * @param agent - exact live Agent whose lineage determines admission.
147
+ * Reject new work from closing members or subagent trees.
148
+ * @param agent - exact live caller whose instance and subagent tree must admit work.
161
149
  */
162
150
  private assertAdmitting;
163
- /**
164
- * Authorize one operation against the durable direct-parent lineage.
165
- * @param parent - exact live Agent claiming direct-parent authority.
166
- * @param childId - durable child session id addressed by the operation.
167
- * @param parentSession - durable direct-parent id recorded by the child.
168
- */
169
- private authorizeLineage;
170
- /** Reject parent authority after the live registry entry changes. */
171
- private assertLiveParent;
172
151
  /**
173
152
  * Create or resume one child Agent and publish its Activation.
174
153
  * @param inputs - reconstruction and admission inputs for the residency epoch.
175
154
  * @returns the published process-local Activation.
176
155
  */
177
156
  private materialize;
157
+ /** Keep subagent tree shutdown and host waits joined to preparation, publication, and rollback. */
158
+ private trackStartup;
178
159
  /**
179
160
  * Stop and release one Activation through its memoized close transaction.
180
161
  * @param activation - exact residency epoch to close.
@@ -185,36 +166,27 @@ export declare class SubagentManager {
185
166
  private close;
186
167
  /** Dispose independent roots and report every branch failure after all settle. */
187
168
  private disposeRoots;
188
- /** Return the retained member set for one exact scoped-teardown root. */
189
- private closingMembers;
190
- /** Return the exact currently resolvable ancestry from `agent` upward. */
191
- private liveLineage;
192
- /** Return the teardown that closed continuable admission for this agent's lineage. */
169
+ /** Return the subagent tree teardown that closed admission for this exact Agent. */
193
170
  private closingTeardownFor;
194
- /** Resolve a root's pool once; descendants inherit their resident parent's pool directly. */
171
+ /** Share one capacity pool across every instance in a subagent tree. */
195
172
  private rootPool;
196
173
  /** Perform one tracked materialization through publication or rollback. */
197
174
  private materializeTracked;
198
- /**
199
- * Begin settlement observation after initial acceptance is recorded.
200
- * @param activation - accepted activation to observe.
201
- */
202
- private announce;
175
+ /** Identity shared by one residency epoch's start/end pair, including cold resumes. */
176
+ private runInfo;
177
+ /** Record acceptance and begin settlement observation before returning the receipt. */
178
+ private accept;
203
179
  /** Release an Activation whose start edge was not published. */
204
180
  private rollbackUnpublished;
205
- /** Register the child in a continuation-managed parent's owned set. */
206
- private acquireOwnership;
207
- /** Remove one child from its live owner's set and let that owner re-check settlement. */
208
- private releaseOwnership;
209
181
  /** Let a settlement watcher re-check residency after relevant state changes. */
210
182
  private wake;
211
183
  /** Follow one Activation to natural settlement. */
212
184
  private watchLocalSettlement;
213
185
  /** Report disposal failure once after the natural settlement observer finishes. */
214
186
  private reportTeardownFailure;
215
- /** Check pending input and owned children before closing admission. */
187
+ /** Check pending input before closing admission. */
216
188
  private settlementState;
217
- /** Propagate stop synchronously and await descendant startup rollback before release. */
189
+ /** Stop and release one instance without affecting its creation descendants. */
218
190
  private finishDisposal;
219
191
  /** Deliver the captured execution result to the durable direct parent after cleanup. */
220
192
  private notifySettlement;
@@ -222,19 +194,12 @@ export declare class SubagentManager {
222
194
  private flushFinalState;
223
195
  /** Return capabilities bound to an exact activation instead of a future same-id instance. */
224
196
  private receipt;
225
- /** Route one parent-originated delivery through residency and cold resume. */
226
- private deliverToChild;
227
- /** The delivery loop behind {@link deliverToChild}, run under the parent hold. */
228
- private deliverFollowup;
229
- /** Deliver one resident continuable child's message to its live direct parent. */
230
- private sendToParent;
231
- /** Send one Agent message while translating only the target's own rejection. */
232
- private sendAgentMessage;
233
- /**
234
- * Cold-resume a persisted child and submit the waiting turn. The descriptor
235
- * supplies every reconstruction input; no subagent provider is dispatched.
236
- */
237
- private coldResume;
197
+ /** Serialize target restoration and inbox admission; closure is awaited outside the lock. */
198
+ private deliverToMember;
199
+ /** Read continuation history, preserving cancellation and reporting unavailable storage. */
200
+ private observeContinuation;
201
+ /** Reconstruct only the target under its subagent tree root; the caller owns acceptance or rollback. */
202
+ private restoreTarget;
238
203
  /** Admit a materialized child, commit its creation fact, and release it on failure. */
239
204
  private submitMaterialized;
240
205
  /** Build and submit one message across the final synchronous admission cutoff. */