@lmzhen/dsh-evolution-core 0.49.0 → 0.50.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.
Files changed (93) hide show
  1. package/lib/authority-plugin.js +2 -1
  2. package/lib/citations-CZKTUGQF.js +198 -0
  3. package/lib/composition/index.js +2 -2
  4. package/lib/{composition-Cqjbho0t.js → composition-DA8XPfMc.js} +1 -1
  5. package/lib/connection-DAJy7Xjm.js +28 -0
  6. package/lib/{ensure-CAjLGd9f.js → ensure-Bx4ksgJk.js} +15 -5
  7. package/lib/errors.js +2 -0
  8. package/lib/extract-plan-Bru3c9ec.js +271 -0
  9. package/lib/extract.js +240 -0
  10. package/lib/http-routes.js +27 -7
  11. package/lib/index.js +41 -800
  12. package/lib/{numeric-D8js3sqC.js → limits-C9jfvxBl.js} +1 -43
  13. package/lib/{mutation-authority-l57R626O.js → mutation-authority-DQfpXs1O.js} +2 -54
  14. package/lib/numeric-Dc53KQoe.js +43 -0
  15. package/lib/platform/index.js +4 -3
  16. package/lib/platform-DyHnHUmQ.js +251 -0
  17. package/lib/policy.js +3 -2
  18. package/lib/quality-WAU6YatF.js +554 -0
  19. package/lib/restrictor.js +1 -1
  20. package/lib/result.js +33 -0
  21. package/lib/runtime/plugin.js +97 -43
  22. package/lib/{session-projection-DSXnUNZG.js → session-projection-ByNxQi9O.js} +2 -198
  23. package/lib/skill-health-Bl-MdXZ4.js +1409 -0
  24. package/lib/skills.js +7 -4
  25. package/lib/state-store-Jpa-iYIZ.js +54 -0
  26. package/lib/subagent/index.js +214 -176
  27. package/lib/{tools-DzcNFex2.js → tools-BrUswg8v.js} +3 -1
  28. package/lib/types/composition/ensure.js +21 -7
  29. package/lib/types/extract-plan.d.ts +2 -0
  30. package/lib/types/extract.d.ts +12 -0
  31. package/lib/types/extract.js +12 -0
  32. package/lib/types/http-routes.js +32 -9
  33. package/lib/types/index.d.ts +0 -3
  34. package/lib/types/index.js +0 -3
  35. package/lib/types/opt-in.js +4 -3
  36. package/lib/types/params.js +2 -1
  37. package/lib/types/platform/agents.d.ts +32 -0
  38. package/lib/types/platform/agents.js +26 -0
  39. package/lib/types/platform/commands.d.ts +14 -0
  40. package/lib/types/platform/commands.js +9 -0
  41. package/lib/types/platform/connection.d.ts +26 -0
  42. package/lib/types/platform/connection.js +27 -0
  43. package/lib/types/platform/index.d.ts +26 -0
  44. package/lib/types/platform/index.js +13 -0
  45. package/lib/types/platform/llm.d.ts +3 -1
  46. package/lib/types/platform/llm.js +1 -0
  47. package/lib/types/platform/platformView.d.ts +21 -0
  48. package/lib/types/platform/platformView.js +29 -0
  49. package/lib/types/platform/profileContext.d.ts +16 -0
  50. package/lib/types/platform/profileContext.js +14 -0
  51. package/lib/types/platform/sessionQuery.d.ts +14 -0
  52. package/lib/types/platform/sessionQuery.js +9 -0
  53. package/lib/types/platform/settings.d.ts +22 -0
  54. package/lib/types/platform/settings.js +14 -0
  55. package/lib/types/platform/skills.d.ts +16 -0
  56. package/lib/types/platform/skills.js +9 -0
  57. package/lib/types/platform/storageDomain.d.ts +13 -0
  58. package/lib/types/platform/storageDomain.js +9 -0
  59. package/lib/types/platform/subagents.d.ts +25 -0
  60. package/lib/types/platform/subagents.js +17 -0
  61. package/lib/types/platform/systemPrompt.d.ts +16 -0
  62. package/lib/types/platform/systemPrompt.js +13 -0
  63. package/lib/types/platform/tools.d.ts +4 -0
  64. package/lib/types/platform/tools.js +2 -0
  65. package/lib/types/platform/userQuestions.d.ts +33 -0
  66. package/lib/types/platform/userQuestions.js +9 -0
  67. package/lib/types/platform/workingDirectory.d.ts +14 -0
  68. package/lib/types/platform/workingDirectory.js +9 -0
  69. package/lib/types/platform-line.generated.d.ts +5 -5
  70. package/lib/types/platform-line.generated.js +5 -5
  71. package/lib/types/platform-services.js +3 -0
  72. package/lib/types/result.d.ts +35 -0
  73. package/lib/types/result.js +25 -0
  74. package/lib/types/runtime/llm.d.ts +2 -2
  75. package/lib/types/runtime/llm.js +5 -8
  76. package/lib/types/runtime/platform.d.ts +37 -4
  77. package/lib/types/runtime/platform.js +40 -5
  78. package/lib/types/runtime/projections.d.ts +9 -4
  79. package/lib/types/runtime/projections.js +64 -28
  80. package/lib/types/subagent/channel.d.ts +1 -3
  81. package/lib/types/subagent/channel.js +2 -332
  82. package/lib/types/subagent/index.d.ts +2 -0
  83. package/lib/types/subagent/index.js +2 -0
  84. package/lib/types/subagent/kernel.d.ts +4 -0
  85. package/lib/types/subagent/kernel.js +234 -0
  86. package/lib/types/subagent/readiness.js +5 -7
  87. package/lib/types/subagent/run.d.ts +53 -0
  88. package/lib/types/subagent/run.js +125 -0
  89. package/lib/types/subagent/types.d.ts +2 -2
  90. package/lib/usage-DdjIBAkm.js +1252 -0
  91. package/package.json +37 -25
  92. package/lib/platform-Cc9cNt5j.js +0 -111
  93. package/lib/skill-health-DP8z9TPf.js +0 -2923
@@ -1,333 +1,3 @@
1
- import { ensureRestrictedPreset } from "../composition/ensure.js";
2
- import { probeChannelState } from "./readiness.js";
3
- function serviceOf(ctx, name) {
4
- return ctx.get(name);
5
- }
6
- function messageOf(error) {
7
- return error instanceof Error ? error.message : String(error);
8
- }
9
- function textOfValue(value) {
10
- if (typeof value === 'string')
11
- return value;
12
- if (!Array.isArray(value))
13
- return '';
14
- return value
15
- .filter(block => typeof block === 'object' && block !== null && block.type === 'text')
16
- .map((block) => {
17
- const text = block.text;
18
- return typeof text === 'string' ? text : '';
19
- })
20
- .join('');
21
- }
22
- function abortLink(signal, timeoutMs) {
23
- const controller = new AbortController();
24
- const timer = setTimeout(() => { controller.abort(new Error('subagent turn timeout')); }, timeoutMs);
25
- const onAbort = () => { controller.abort(signal?.reason); };
26
- signal?.addEventListener('abort', onAbort, { once: true });
27
- return {
28
- controller,
29
- cleanup: () => {
30
- clearTimeout(timer);
31
- signal?.removeEventListener('abort', onAbort);
32
- },
33
- };
34
- }
35
- function waitForSubagentEnd(ctx, childId, signal, timeoutMs) {
36
- return new Promise((resolve, reject) => {
37
- let settled = false;
38
- const finish = (fn) => {
39
- if (settled)
40
- return;
41
- settled = true;
42
- cleanup();
43
- fn();
44
- };
45
- const eventCtx = ctx;
46
- const off = typeof eventCtx.on === 'function'
47
- ? eventCtx.on('subagent/end', (info) => {
48
- if (info.id !== childId)
49
- return;
50
- finish(() => { resolve(info); });
51
- })
52
- : () => { };
53
- const timer = setTimeout(() => {
54
- finish(() => { reject(new Error('timed out waiting for subagent/end: ' + childId)); });
55
- }, timeoutMs);
56
- const onAbort = () => {
57
- finish(() => { reject(signal.reason instanceof Error ? signal.reason : new Error('conversation turn aborted')); });
58
- };
59
- signal.addEventListener('abort', onAbort, { once: true });
60
- function cleanup() {
61
- clearTimeout(timer);
62
- signal.removeEventListener('abort', onAbort);
63
- off();
64
- }
65
- if (signal.aborted)
66
- onAbort();
67
- });
68
- }
69
- export function createSubagentChannel(ctx) {
70
- let lastKind = 'reviewer';
71
- const active = new Map();
72
- const disposed = new Set();
73
- const channel = {
74
- get state() {
75
- return probeChannelState(ctx, lastKind);
76
- },
77
- prepare(spec) {
78
- lastKind = spec.kind;
79
- const state = probeChannelState(ctx, spec.kind);
80
- if (state.kind === 'unavailable')
81
- return Promise.resolve(state);
82
- if (spec.allow.length === 0)
83
- return Promise.resolve({ kind: 'unavailable', code: 'E-324', reason: 'allow set is empty' });
84
- return Promise.resolve(state);
85
- },
86
- async run(spec) {
87
- lastKind = spec.kind;
88
- const probe = probeChannelState(ctx, spec.kind);
89
- if (probe.kind === 'unavailable')
90
- return probe;
91
- const subagents = serviceOf(ctx, 'subagents');
92
- const presets = serviceOf(ctx, 'agentPresets');
93
- if (subagents === undefined || presets === undefined) {
94
- return { kind: 'unavailable', code: 'E-323', reason: 'subagents/agentPresets service unavailable' };
95
- }
96
- const parentPresetId = presets.composedPreset(spec.parent.ctx);
97
- if (parentPresetId === undefined) {
98
- return { kind: 'unavailable', code: 'E-323', reason: 'parent preset could not be resolved' };
99
- }
100
- const ensured = await ensureRestrictedPreset(ctx, { kind: spec.kind, parentPresetId, allow: spec.allow });
101
- if (ensured.kind === 'failed') {
102
- return { kind: 'unavailable', code: 'E-324', reason: ensured.reason };
103
- }
104
- const controller = new AbortController();
105
- const timer = setTimeout(() => { controller.abort(new Error('subagent timeout')); }, spec.timeoutMs);
106
- const onAbort = () => { controller.abort(spec.signal?.reason); };
107
- spec.signal?.addEventListener('abort', onAbort, { once: true });
108
- let activation;
109
- try {
110
- activation = await subagents.startActivation({
111
- provider: 'spawn',
112
- label: spec.label,
113
- delivery: 'caller',
114
- request: {
115
- parent: spec.parent,
116
- prompt: [{ type: 'text', text: spec.promptText }],
117
- agentPreset: ensured.presetId,
118
- ...(spec.outputSchema !== undefined ? { outputSchema: spec.outputSchema } : {}),
119
- ...(spec.maxDepth !== undefined ? { maxDepth: spec.maxDepth } : {}),
120
- ...(spec.agentOptions !== undefined ? { agentOptions: spec.agentOptions } : {}),
121
- },
122
- signal: controller.signal,
123
- });
124
- let result;
125
- try {
126
- result = await activation.result;
127
- }
128
- catch (error) {
129
- if (controller.signal.aborted)
130
- return { kind: 'aborted' };
131
- return { kind: 'unavailable', code: 'E-323', reason: messageOf(error) };
132
- }
133
- let evidence;
134
- if (spec.afterResult !== undefined) {
135
- try {
136
- const agents = serviceOf(ctx, 'agents');
137
- const child = agents?.get(activation.childId);
138
- if (child !== undefined)
139
- evidence = spec.afterResult(child);
140
- }
141
- catch {
142
- evidence = undefined;
143
- }
144
- }
145
- const stopReason = result.stopReason;
146
- // W5 §3: OK requires BOTH a completed stop and a structured payload; a completed run that
147
- // produced nothing must stay a no-structured outcome so the business layer cannot treat an
148
- // empty result as a plan.
149
- if (stopReason === 'completed' && result.structured !== undefined && result.structured !== null) {
150
- return {
151
- kind: 'ok',
152
- structured: result.structured,
153
- childId: activation.childId,
154
- stopReason: 'completed',
155
- ...(evidence !== undefined ? { evidence } : {}),
156
- };
157
- }
158
- if (stopReason === 'aborted')
159
- return { kind: 'aborted' };
160
- return {
161
- kind: 'no-structured',
162
- childId: activation.childId,
163
- stopReason,
164
- ...(result.diagnostic !== undefined ? { diagnostic: result.diagnostic } : {}),
165
- ...(evidence !== undefined ? { evidence } : {}),
166
- };
167
- }
168
- catch (error) {
169
- if (controller.signal.aborted)
170
- return { kind: 'aborted' };
171
- return { kind: 'unavailable', code: 'E-323', reason: messageOf(error) };
172
- }
173
- finally {
174
- clearTimeout(timer);
175
- spec.signal?.removeEventListener('abort', onAbort);
176
- if (activation !== undefined) {
177
- try {
178
- await activation.dispose();
179
- }
180
- catch (error) {
181
- // W5 §6: teardown failures are observable but must NOT replace the captured outcome.
182
- try {
183
- ctx.logger.warn('subagent channel: activation dispose failed: ' + messageOf(error));
184
- }
185
- catch { /* logging must not mask teardown */ }
186
- }
187
- }
188
- }
189
- },
190
- async open(spec) {
191
- lastKind = spec.kind;
192
- const probe = probeChannelState(ctx, spec.kind);
193
- if (probe.kind === 'unavailable')
194
- return probe;
195
- if (spec.allow.length === 0)
196
- return { kind: 'unavailable', code: 'E-324', reason: 'allow set is empty' };
197
- const subagents = serviceOf(ctx, 'subagents');
198
- const presets = serviceOf(ctx, 'agentPresets');
199
- if (subagents === undefined || presets === undefined) {
200
- return { kind: 'unavailable', code: 'E-323', reason: 'subagents/agentPresets service unavailable' };
201
- }
202
- const parentPresetId = presets.composedPreset(spec.parent.ctx);
203
- if (parentPresetId === undefined) {
204
- return { kind: 'unavailable', code: 'E-323', reason: 'parent preset could not be resolved' };
205
- }
206
- const ensured = await ensureRestrictedPreset(ctx, { kind: spec.kind, parentPresetId, allow: spec.allow });
207
- if (ensured.kind === 'failed')
208
- return { kind: 'unavailable', code: 'E-324', reason: ensured.reason };
209
- const link = abortLink(spec.signal, spec.timeoutMs);
210
- let activation;
211
- try {
212
- activation = await subagents.startActivation({
213
- provider: 'spawn',
214
- label: spec.label,
215
- delivery: 'caller',
216
- request: {
217
- parent: spec.parent,
218
- prompt: [{ type: 'text', text: spec.promptText }],
219
- agentPreset: ensured.presetId,
220
- ...(spec.cwd !== undefined ? { cwd: spec.cwd } : {}),
221
- ...(spec.maxDepth !== undefined ? { maxDepth: spec.maxDepth } : {}),
222
- ...(spec.agentOptions !== undefined ? { agentOptions: spec.agentOptions } : {}),
223
- },
224
- signal: link.controller.signal,
225
- });
226
- const result = await activation.result;
227
- if (result.stopReason === 'aborted') {
228
- try {
229
- await activation.dispose();
230
- }
231
- catch { /* teardown must not replace the outcome */ }
232
- return { kind: 'aborted' };
233
- }
234
- if (result.stopReason !== 'completed') {
235
- try {
236
- await activation.dispose();
237
- }
238
- catch { /* teardown must not replace the outcome */ }
239
- return { kind: 'unavailable', code: 'E-323', reason: 'round 1 did not complete: ' + result.stopReason };
240
- }
241
- const turn = {
242
- childId: activation.childId,
243
- stopReason: result.stopReason,
244
- text: textOfValue(result.output),
245
- ...(result.structured !== undefined ? { structured: result.structured } : {}),
246
- ...(result.diagnostic !== undefined ? { diagnostic: result.diagnostic } : {}),
247
- };
248
- active.set(activation.childId, { childId: activation.childId, parent: spec.parent, activation, disposed: false });
249
- disposed.delete(activation.childId);
250
- return { kind: 'ok', childId: activation.childId, turn };
251
- }
252
- catch (error) {
253
- if (activation !== undefined) {
254
- try {
255
- await activation.dispose();
256
- }
257
- catch { /* teardown must not replace the outcome */ }
258
- }
259
- if (link.controller.signal.aborted)
260
- return { kind: 'aborted' };
261
- return { kind: 'unavailable', code: 'E-323', reason: messageOf(error) };
262
- }
263
- finally {
264
- link.cleanup();
265
- }
266
- },
267
- async continueTurn(childId, promptText, options) {
268
- if (disposed.has(childId))
269
- return { kind: 'unavailable', code: 'E-324', reason: 'conversation is disposed' };
270
- const current = active.get(childId);
271
- if (current === undefined || current.disposed) {
272
- return { kind: 'unavailable', code: 'E-323', reason: 'no open conversation for ' + childId };
273
- }
274
- const subagents = serviceOf(ctx, 'subagents');
275
- if (subagents?.sendMessage === undefined) {
276
- return { kind: 'unavailable', code: 'E-323', reason: 'subagents.sendMessage unavailable' };
277
- }
278
- const link = abortLink(options?.signal, options?.timeoutMs ?? 120_000);
279
- let pending;
280
- try {
281
- pending = waitForSubagentEnd(ctx, childId, link.controller.signal, options?.timeoutMs ?? 120_000);
282
- await subagents.sendMessage(current.parent, childId, [{ type: 'text', text: promptText }], { signal: link.controller.signal });
283
- }
284
- catch (error) {
285
- link.cleanup();
286
- if (options?.signal?.aborted === true)
287
- return { kind: 'aborted' };
288
- return { kind: 'unavailable', code: 'E-323', reason: messageOf(error) };
289
- }
290
- try {
291
- const info = await pending;
292
- if (info.stopReason === 'aborted')
293
- return { kind: 'aborted' };
294
- const turn = {
295
- childId,
296
- stopReason: info.stopReason ?? 'completed',
297
- text: textOfValue(info.lastAssistantMessage),
298
- ...(info.diagnostic !== undefined ? { diagnostic: info.diagnostic } : {}),
299
- };
300
- return { kind: 'ok', childId, turn };
301
- }
302
- catch (error) {
303
- if (options?.signal?.aborted === true)
304
- return { kind: 'aborted' };
305
- return { kind: 'unavailable', code: 'E-323', reason: messageOf(error) };
306
- }
307
- finally {
308
- link.cleanup();
309
- }
310
- },
311
- async dispose(childId) {
312
- const current = active.get(childId);
313
- if (current === undefined || current.disposed) {
314
- disposed.add(childId);
315
- return;
316
- }
317
- current.disposed = true;
318
- active.delete(childId);
319
- disposed.add(childId);
320
- try {
321
- await current.activation.dispose();
322
- }
323
- catch (error) {
324
- try {
325
- ctx.logger.warn('subagent conversation: dispose failed: ' + messageOf(error));
326
- }
327
- catch { /* logging must not mask teardown */ }
328
- }
329
- },
330
- };
331
- return channel;
332
- }
1
+ // Thin compatibility adapter: the execution kernel owns one implementation.
2
+ export { createSubagentKernel as createSubagentChannel } from "./kernel.js";
333
3
  //# sourceMappingURL=channel.js.map
@@ -1,4 +1,6 @@
1
1
  export * from './types.ts';
2
2
  export * from './readiness.ts';
3
+ export * from './run.ts';
4
+ export * from './kernel.ts';
3
5
  export * from './channel.ts';
4
6
  //# sourceMappingURL=index.d.ts.map
@@ -1,5 +1,7 @@
1
1
  // Subagent channel contract + implementation (types/readiness/channel split for one owner each).
2
2
  export * from "./types.js";
3
3
  export * from "./readiness.js";
4
+ export * from "./run.js";
5
+ export * from "./kernel.js";
4
6
  export * from "./channel.js";
5
7
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,4 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import type { SubagentChannel } from './types.ts';
3
+ export declare function createSubagentKernel(ctx: Context): SubagentChannel;
4
+ //# sourceMappingURL=kernel.d.ts.map
@@ -0,0 +1,234 @@
1
+ import { ensureRestrictedPreset } from "../composition/ensure.js";
2
+ import { probeChannelState } from "./readiness.js";
3
+ import { abortLink, createSubagentRun, textOfValue } from "./run.js";
4
+ function platformOf(ctx) {
5
+ return ctx.get('evolutionPlatform');
6
+ }
7
+ function messageOf(error) {
8
+ return error instanceof Error ? error.message : String(error);
9
+ }
10
+ export function createSubagentKernel(ctx) {
11
+ let lastKind = 'reviewer';
12
+ const active = new Map();
13
+ const disposed = new Set();
14
+ /**
15
+ * The one policy/probe/preset preparation shared by run and open. Starting the
16
+ * activation stays separate because run disposes and open keeps the handle.
17
+ */
18
+ async function prepare(spec) {
19
+ lastKind = spec.kind;
20
+ const probe = probeChannelState(ctx, spec.kind);
21
+ if (probe.kind === 'unavailable')
22
+ return { ok: false, outcome: probe };
23
+ const platform = platformOf(ctx);
24
+ const subagents = platform?.ports.subagents;
25
+ const presets = platform?.ports.presets;
26
+ if (platform === undefined || subagents === undefined || presets === undefined) {
27
+ return { ok: false, outcome: { kind: 'unavailable', code: 'E-323', reason: 'subagents/agentPresets service unavailable' } };
28
+ }
29
+ const parentPresetId = presets.composedPreset(spec.parent.ctx);
30
+ if (parentPresetId === undefined) {
31
+ return { ok: false, outcome: { kind: 'unavailable', code: 'E-323', reason: 'parent preset could not be resolved' } };
32
+ }
33
+ const ensured = await ensureRestrictedPreset(ctx, { kind: spec.kind, parentPresetId, allow: spec.allow });
34
+ if (ensured.kind === 'failed') {
35
+ return { ok: false, outcome: { kind: 'unavailable', code: 'E-324', reason: ensured.reason } };
36
+ }
37
+ return { ok: true, platform, subagents, ensured };
38
+ }
39
+ /** Start one activation after the shared policy/prepare step; the caller owns its link/run. */
40
+ async function startRun(spec, options) {
41
+ const prepared = await prepare(spec);
42
+ if (!prepared.ok)
43
+ return { ok: false, outcome: prepared.outcome };
44
+ const link = abortLink(spec.signal, spec.timeoutMs);
45
+ try {
46
+ const activation = await prepared.subagents.start({
47
+ provider: 'spawn',
48
+ label: spec.label,
49
+ delivery: 'caller',
50
+ parent: spec.parent,
51
+ prompt: [{ type: 'text', text: spec.promptText }],
52
+ agentPreset: prepared.ensured.presetId,
53
+ ...(options.outputSchema !== undefined ? { outputSchema: options.outputSchema } : {}),
54
+ ...(options.cwd !== undefined ? { cwd: options.cwd } : {}),
55
+ ...(spec.maxDepth !== undefined ? { maxDepth: spec.maxDepth } : {}),
56
+ ...(spec.agentOptions !== undefined ? { agentOptions: spec.agentOptions } : {}),
57
+ signal: link.controller.signal,
58
+ });
59
+ return { ok: true, run: createSubagentRun(ctx, spec.parent, spec.label, activation), link, platform: prepared.platform };
60
+ }
61
+ catch (error) {
62
+ link.cleanup();
63
+ if (link.controller.signal.aborted)
64
+ return { ok: false, outcome: { kind: 'aborted' } };
65
+ return { ok: false, outcome: { kind: 'unavailable', code: 'E-323', reason: messageOf(error) } };
66
+ }
67
+ }
68
+ const channel = {
69
+ get state() {
70
+ return probeChannelState(ctx, lastKind);
71
+ },
72
+ prepare(spec) {
73
+ lastKind = spec.kind;
74
+ const state = probeChannelState(ctx, spec.kind);
75
+ if (state.kind === 'unavailable')
76
+ return Promise.resolve(state);
77
+ if (spec.allow.length === 0)
78
+ return Promise.resolve({ kind: 'unavailable', code: 'E-324', reason: 'allow set is empty' });
79
+ return Promise.resolve(state);
80
+ },
81
+ async run(spec) {
82
+ const started = await startRun(spec, spec.outputSchema === undefined ? {} : { outputSchema: spec.outputSchema });
83
+ if (!started.ok)
84
+ return started.outcome;
85
+ const { run, link, platform } = started;
86
+ try {
87
+ const result = await run.result;
88
+ let evidence;
89
+ if (spec.afterResult !== undefined) {
90
+ try {
91
+ const child = platform.ports.agents?.get(run.id);
92
+ if (child !== undefined)
93
+ evidence = spec.afterResult(child);
94
+ }
95
+ catch {
96
+ evidence = undefined;
97
+ }
98
+ }
99
+ const stopReason = result.stopReason;
100
+ if (stopReason === 'completed' && result.structured !== undefined && result.structured !== null) {
101
+ return {
102
+ kind: 'ok',
103
+ structured: result.structured,
104
+ childId: run.id,
105
+ stopReason: 'completed',
106
+ ...(evidence !== undefined ? { evidence } : {}),
107
+ };
108
+ }
109
+ if (stopReason === 'aborted')
110
+ return { kind: 'aborted' };
111
+ return {
112
+ kind: 'no-structured',
113
+ childId: run.id,
114
+ stopReason,
115
+ ...(result.diagnostic !== undefined ? { diagnostic: result.diagnostic } : {}),
116
+ ...(evidence !== undefined ? { evidence } : {}),
117
+ };
118
+ }
119
+ catch (error) {
120
+ if (link.controller.signal.aborted)
121
+ return { kind: 'aborted' };
122
+ return { kind: 'unavailable', code: 'E-323', reason: messageOf(error) };
123
+ }
124
+ finally {
125
+ link.cleanup();
126
+ try {
127
+ await run.dispose();
128
+ }
129
+ catch (error) {
130
+ try {
131
+ ctx.logger.warn('subagent channel: activation dispose failed: ' + messageOf(error));
132
+ }
133
+ catch { /* logging must not mask teardown */ }
134
+ }
135
+ }
136
+ },
137
+ async open(spec) {
138
+ const started = await startRun(spec, spec.cwd === undefined ? {} : { cwd: spec.cwd });
139
+ if (!started.ok)
140
+ return started.outcome;
141
+ const { run, link } = started;
142
+ try {
143
+ const result = await run.result;
144
+ if (result.stopReason === 'aborted') {
145
+ try {
146
+ await run.dispose();
147
+ }
148
+ catch { /* teardown must not replace the outcome */ }
149
+ return { kind: 'aborted' };
150
+ }
151
+ if (result.stopReason !== 'completed') {
152
+ try {
153
+ await run.dispose();
154
+ }
155
+ catch { /* teardown must not replace the outcome */ }
156
+ return { kind: 'unavailable', code: 'E-323', reason: 'round 1 did not complete: ' + result.stopReason };
157
+ }
158
+ const turn = {
159
+ childId: run.id,
160
+ stopReason: result.stopReason,
161
+ text: textOfValue(result.output),
162
+ ...(result.structured !== undefined ? { structured: result.structured } : {}),
163
+ ...(result.diagnostic !== undefined ? { diagnostic: result.diagnostic } : {}),
164
+ };
165
+ active.set(run.id, { childId: run.id, run, disposed: false });
166
+ disposed.delete(run.id);
167
+ return { kind: 'ok', childId: run.id, turn };
168
+ }
169
+ catch (error) {
170
+ try {
171
+ await run.dispose();
172
+ }
173
+ catch { /* teardown must not replace the outcome */ }
174
+ if (link.controller.signal.aborted)
175
+ return { kind: 'aborted' };
176
+ return { kind: 'unavailable', code: 'E-323', reason: messageOf(error) };
177
+ }
178
+ finally {
179
+ link.cleanup();
180
+ }
181
+ },
182
+ async continueTurn(childId, promptText, options) {
183
+ if (disposed.has(childId))
184
+ return { kind: 'unavailable', code: 'E-324', reason: 'conversation is disposed' };
185
+ const current = active.get(childId);
186
+ if (current === undefined || current.disposed) {
187
+ return { kind: 'unavailable', code: 'E-323', reason: 'no open conversation for ' + childId };
188
+ }
189
+ try {
190
+ await current.run.send([{ type: 'text', text: promptText }], options);
191
+ const result = await current.run.next(options);
192
+ if (result.stopReason === 'aborted')
193
+ return { kind: 'aborted' };
194
+ if (result.stopReason !== 'completed') {
195
+ return { kind: 'unavailable', code: 'E-323', reason: 'round 2 did not complete: ' + result.stopReason };
196
+ }
197
+ const turn = {
198
+ childId,
199
+ stopReason: 'completed',
200
+ text: textOfValue(result.output),
201
+ ...(result.structured !== undefined ? { structured: result.structured } : {}),
202
+ ...(result.diagnostic !== undefined ? { diagnostic: result.diagnostic } : {}),
203
+ };
204
+ return { kind: 'ok', childId, turn };
205
+ }
206
+ catch (error) {
207
+ if (options?.signal?.aborted === true)
208
+ return { kind: 'aborted' };
209
+ return { kind: 'unavailable', code: 'E-323', reason: messageOf(error) };
210
+ }
211
+ },
212
+ async dispose(childId) {
213
+ const current = active.get(childId);
214
+ if (current === undefined || current.disposed) {
215
+ disposed.add(childId);
216
+ return;
217
+ }
218
+ current.disposed = true;
219
+ active.delete(childId);
220
+ disposed.add(childId);
221
+ try {
222
+ await current.run.dispose();
223
+ }
224
+ catch (error) {
225
+ try {
226
+ ctx.logger.warn('subagent conversation: dispose failed: ' + messageOf(error));
227
+ }
228
+ catch { /* logging must not mask teardown */ }
229
+ }
230
+ },
231
+ };
232
+ return channel;
233
+ }
234
+ //# sourceMappingURL=kernel.js.map
@@ -1,12 +1,10 @@
1
1
  import { restrictedPresetState } from "../composition/ensure.js";
2
2
  import { restrictedPresetId } from "../composition/preset-ids.js";
3
- function serviceOf(ctx, name) {
4
- return ctx.get(name);
5
- }
6
3
  /** Pure derivation (no side effects): the doctor reads this, the channel re-probes on every run. */
7
4
  export function probeChannelState(ctx, kind) {
8
- const subagents = serviceOf(ctx, 'subagents');
9
- if (subagents === undefined || typeof subagents.getProvider !== 'function') {
5
+ const platform = ctx.get('evolutionPlatform');
6
+ const subagents = platform?.ports.subagents;
7
+ if (subagents === undefined) {
10
8
  return { kind: 'unavailable', code: 'E-323', reason: 'subagents service unavailable' };
11
9
  }
12
10
  const provider = subagents.getProvider('spawn');
@@ -18,8 +16,8 @@ export function probeChannelState(ctx, kind) {
18
16
  if (provider.inheritsParentContext === true) {
19
17
  return { kind: 'unavailable', code: 'E-323', reason: 'spawn provider inherits the parent context (independent preset required)' };
20
18
  }
21
- const agentPresets = serviceOf(ctx, 'agentPresets');
22
- if (agentPresets === undefined || typeof agentPresets.readDocument !== 'function' || typeof agentPresets.register !== 'function') {
19
+ const presets = platform?.ports.presets;
20
+ if (presets === undefined) {
23
21
  return { kind: 'unavailable', code: 'E-323', reason: 'agentPresets service unavailable or incomplete' };
24
22
  }
25
23
  const id = restrictedPresetId(kind);
@@ -0,0 +1,53 @@
1
+ /**
2
+ * One handle over a started subagent activation: the attach point for
3
+ * multi-turn conversations. The kernel owns policy/preset resolution; this
4
+ * handle owns the continuation lifecycle and the end-event wait.
5
+ */
6
+ import type { Context } from '@deepseek-ai/cordis';
7
+ import type { Agent, ContentBlock, SessionId, SubagentStopReason } from '../platform/index.ts';
8
+ export interface ActivationResultLike {
9
+ readonly output?: unknown;
10
+ readonly structured?: unknown;
11
+ readonly diagnostic?: string;
12
+ readonly stopReason: SubagentStopReason;
13
+ }
14
+ export interface ActivationLike {
15
+ readonly childId: SessionId;
16
+ readonly result: Promise<ActivationResultLike>;
17
+ dispose(): Promise<void>;
18
+ }
19
+ export interface SubagentResult {
20
+ readonly childId: SessionId;
21
+ readonly stopReason: SubagentStopReason;
22
+ readonly output?: unknown;
23
+ readonly structured?: unknown;
24
+ readonly diagnostic?: string;
25
+ }
26
+ export interface SubagentTurnOptions {
27
+ readonly timeoutMs?: number;
28
+ readonly signal?: AbortSignal;
29
+ }
30
+ export interface SubagentRun {
31
+ readonly id: SessionId;
32
+ readonly name: string;
33
+ readonly result: Promise<SubagentResult>;
34
+ send(prompt: readonly ContentBlock[], options?: SubagentTurnOptions): Promise<void>;
35
+ next(options?: SubagentTurnOptions): Promise<SubagentResult>;
36
+ dispose(): Promise<void>;
37
+ }
38
+ interface SubagentEndInfo {
39
+ readonly id?: string;
40
+ readonly stopReason?: SubagentStopReason;
41
+ readonly lastAssistantMessage?: unknown;
42
+ readonly diagnostic?: string;
43
+ }
44
+ export declare function textOfValue(value: unknown): string;
45
+ export declare function abortLink(signal: AbortSignal | undefined, timeoutMs: number): {
46
+ controller: AbortController;
47
+ cleanup: () => void;
48
+ };
49
+ export declare function waitForSubagentEnd(ctx: Context, childId: SessionId, signal: AbortSignal, timeoutMs: number): Promise<SubagentEndInfo>;
50
+ /** Wrap one started activation in the single continuation handle. */
51
+ export declare function createSubagentRun(ctx: Context, parent: Agent, name: string, activation: ActivationLike): SubagentRun;
52
+ export {};
53
+ //# sourceMappingURL=run.d.ts.map