@dsmm/dsmm 0.3.26 → 0.3.27

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 (47) hide show
  1. package/README.md +21 -8
  2. package/docs/agent-presets.md +7 -7
  3. package/docs/agent-teams.md +27 -0
  4. package/docs/compatibility.md +6 -6
  5. package/docs/lsp.md +10 -12
  6. package/docs/maintenance.md +29 -0
  7. package/docs/roadmap.md +25 -9
  8. package/docs/runtime-recovery.md +3 -3
  9. package/docs/testing.md +2 -1
  10. package/dsh-plugin.json +1 -1
  11. package/lib/client/NativeToolView-data.d.ts +8 -0
  12. package/lib/client/NativeToolView-data.js +19 -0
  13. package/lib/client/NativeToolView.d.ts +19 -0
  14. package/lib/client/NativeToolView.js +46 -0
  15. package/lib/client/RoleToolView-data.js +1 -1
  16. package/lib/client/RoleToolView-locales.d.ts +7 -0
  17. package/lib/client/RoleToolView-locales.js +6 -0
  18. package/lib/client/RoleToolView.js +2 -0
  19. package/lib/client/children-locales.d.ts +3 -3
  20. package/lib/client/controller.js +20 -0
  21. package/lib/client.js +347 -186
  22. package/lib/core-permission-authority.js +14 -4
  23. package/lib/mode.js +2 -2
  24. package/lib/native-admission.d.ts +5 -0
  25. package/lib/native-admission.js +218 -30
  26. package/lib/native-ownership.d.ts +3 -0
  27. package/lib/native-ownership.js +19 -0
  28. package/lib/native-subagents.d.ts +2 -1
  29. package/lib/native-subagents.js +4 -3
  30. package/lib/profile-runtime.d.ts +1 -1
  31. package/lib/prompts.d.ts +2 -0
  32. package/lib/prompts.js +14 -8
  33. package/lib/role-control-remote.js +19 -8
  34. package/lib/role-control-rpc.js +1 -1
  35. package/lib/role-control-types.d.ts +18 -5
  36. package/lib/role-identity.js +3 -19
  37. package/lib/role-invocation.d.ts +1 -1
  38. package/lib/role-policy.d.ts +11 -3
  39. package/lib/role-policy.js +61 -21
  40. package/lib/role-subagents.js +12 -5
  41. package/lib/session-metadata.d.ts +27 -1
  42. package/lib/session-metadata.js +46 -0
  43. package/lib/session-scope.d.ts +1 -1
  44. package/lib/session-scope.js +2 -2
  45. package/package.json +3 -1
  46. package/prompts/source/deepwork/general.md +19 -0
  47. package/prompts/source/shared/native-teams.md +11 -0
@@ -1,4 +1,4 @@
1
- import { nativeRoots, nativeIsOwnedBy } from "./native-ownership.js";
1
+ import { nativeRoots, nativeIsOwnedBy, nativeTreeRoot } from "./native-ownership.js";
2
2
  import { randomUUID } from "node:crypto";
3
3
  import { scopeLifetime } from "./lifetime.js";
4
4
  import { PermissionRefusal, permissionOriginal } from "./permission-control.js";
@@ -24,13 +24,23 @@ export function createCorePermissionAuthority(ctx, options) {
24
24
  throw new PermissionRefusal("cancelled");
25
25
  return ctx.get("dsmmNativeAdmission");
26
26
  };
27
- /** Registry ownership is direct, so each physical edge must be walked independently. */
27
+ /** Tree admission has a live root owner; older registries retain direct-edge ancestry. */
28
28
  const ancestry = (exec) => {
29
29
  const agents = ctx.get("agents"), agent = exec.agent;
30
30
  const realmAgents = agent?.ctx.get("agents");
31
31
  if (agents === undefined || agent === undefined || agents.get(agent.id) !== agent
32
32
  || realmAgents === undefined || permissionOriginal(realmAgents) !== permissionOriginal(agents))
33
33
  throw new PermissionRefusal("not-owned");
34
+ const root = nativeTreeRoot(agents, agent);
35
+ if (root !== undefined) {
36
+ const host = integration();
37
+ if (host?.owns(agent)) {
38
+ host.assert(agent);
39
+ if (agent !== root && rolePolicy.identity(agent).owner !== root)
40
+ throw new PermissionRefusal("not-owned");
41
+ }
42
+ return agent === root ? [root] : [agent, root];
43
+ }
34
44
  const chain = [], seen = new Set(), residents = agents.list();
35
45
  let current = agent;
36
46
  while (true) {
@@ -84,7 +94,7 @@ export function createCorePermissionAuthority(ctx, options) {
84
94
  const identity = native ? undefined : rolePolicy.identity(structural);
85
95
  const tools = agent.ctx.get("tools"), approval = agent.ctx.get("approval");
86
96
  if (tools === undefined || !native && (identity?.epoch !== admission.epoch
87
- || (parent === undefined ? agent !== root || identity.child : identity.parent !== parent.authority.agent || !identity.child)))
97
+ || (parent === undefined ? agent !== root || identity.child : (identity.owner ?? identity.parent) !== parent.authority.agent || !identity.child)))
88
98
  throw new PermissionRefusal("not-owned");
89
99
  const nativePolicy = approval === undefined ? undefined : approval.overrideOf(agent.session) ?? approval.config.policy;
90
100
  if (approval !== undefined && nativePolicy !== "ask" && nativePolicy !== "never")
@@ -97,7 +107,7 @@ export function createCorePermissionAuthority(ctx, options) {
97
107
  const facts = [root, agent.ctx, parent?.authority.signal, permissionOriginal(ctx.get("dsmmCoreLlmOwner")),
98
108
  permissionOriginal(ctx.get("dsmmProfileRuntime")), permissionOriginal(tools),
99
109
  approval === undefined ? undefined : permissionOriginal(approval),
100
- admission?.settings, native, captured, identity?.role, identity?.child, identity?.parent, identity?.epoch, identity?.readOnly,
110
+ admission?.settings, native, captured, identity?.role, identity?.child, identity?.owner ?? identity?.parent, identity?.creatorId, identity?.epoch, identity?.readOnly,
101
111
  JSON.stringify(identity?.toolFilter)];
102
112
  if (previous !== undefined && !previous.authority.signal.aborted && previous.integration === entry
103
113
  && previous.authority.owner === owner && previous.admission === admission && sameFacts(previous.facts, facts)) {
package/lib/mode.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { getTraceable } from "@deepseek-ai/cordis";
2
2
  import { agentForScope, nativeAgentContext } from "./native-scope.js";
3
3
  import { buildDeepworkPrompt } from "./prompts.js";
4
- import { resolveEffectiveDsmmRole, resolveSelectedAgentPreset } from "./session-scope.js";
4
+ import { resolveAdmittedDsmmRole } from "./role-policy.js";
5
5
  const bindings = new WeakMap();
6
6
  function routeFromAgent(agent) {
7
7
  const header = agent.session.requestHeader?.();
@@ -31,7 +31,7 @@ export function registerDeepworkPrompt(readyCtx, controller, getSettings, config
31
31
  const settings = getSettings(agent);
32
32
  if (!settings.modules.deepwork.enabled || !controller.active(agent, settings.defaultActive))
33
33
  return "";
34
- return buildDeepworkPrompt(settings, { route: routeFromAgent(agent), selectedPreset: resolveSelectedAgentPreset(agent.session), roleId: resolveEffectiveDsmmRole(agent, settings, true) ?? null, overrideSection: config.section });
34
+ return buildDeepworkPrompt(settings, { route: routeFromAgent(agent), roleId: resolveAdmittedDsmmRole(agent, settings, true) ?? null, overrideSection: config.section });
35
35
  }
36
36
  });
37
37
  };
@@ -48,6 +48,8 @@ export declare class NativeAdmission {
48
48
  private readonly ambiguousCallIds;
49
49
  private readonly observedCallTokens;
50
50
  private readonly observedCallChildren;
51
+ private readonly nativeToolObservations;
52
+ private readonly nativeToolExecution;
51
53
  private readonly observedCallRuns;
52
54
  private readonly callSubscribers;
53
55
  private readonly providerScopes;
@@ -76,6 +78,9 @@ export declare class NativeAdmission {
76
78
  private settleObservedActivation;
77
79
  private bindObservedChild;
78
80
  private observeCallRequestHeader;
81
+ private setObservedRoute;
82
+ private beginNativeToolObservation;
83
+ private finishNativeToolObservation;
79
84
  private finishObservedTool;
80
85
  private retireObservedParent;
81
86
  observeRoleCalls(parent: Agent, admission: DsmmProfileAdmission, signal: AbortSignal, check: () => void): AsyncIterable<RoleCallObservationFrame>;
@@ -9,7 +9,8 @@ import { invocationView } from "./role-invocation.js";
9
9
  import { scopeLifetime } from "./lifetime.js";
10
10
  import { sessionEvents, childOwnedSessionEvents } from "./session-scope.js";
11
11
  import { scopeParentOf, scopeOf } from "./native-scope.js";
12
- import { bindNativeParent, nativeIsOwnedBy, nativeRoots } from "./native-ownership.js";
12
+ import { bindNativeParent, nativeIsOwnedBy, nativeRoots, nativeTreeRoot } from "./native-ownership.js";
13
+ import { appendDelegationCeiling, readDelegationCeiling } from "./session-metadata.js";
13
14
  import { listNativeChildren } from "./native-subagents.js";
14
15
  import { DsmmProfileError } from "./profiles.js";
15
16
  import { roleProviderName } from "./roles.js";
@@ -89,6 +90,8 @@ export class NativeAdmission {
89
90
  ambiguousCallIds = new WeakMap();
90
91
  observedCallTokens = new Map();
91
92
  observedCallChildren = new Map();
93
+ nativeToolObservations = new Map();
94
+ nativeToolExecution = new AsyncLocalStorage();
92
95
  observedCallRuns = new Map();
93
96
  callSubscribers = new Set();
94
97
  providerScopes = new Map();
@@ -112,6 +115,7 @@ export class NativeAdmission {
112
115
  this.observedCallTokens.clear();
113
116
  this.observedCallChildren.clear();
114
117
  this.observedCallRuns.clear();
118
+ this.nativeToolObservations.clear();
115
119
  for (const agent of this.agents)
116
120
  agent.cancel({ kind: "disposed" });
117
121
  });
@@ -192,15 +196,14 @@ export class NativeAdmission {
192
196
  ctx.on("agent/pre-step", async (frame, next) => { await this.guardExecution(frame.agent); return next(); }, { global: true, prepend: true });
193
197
  ctx.on("agent/request", async (frame, next) => { await this.guardExecution(frame.agent); return next(); }, { global: true, prepend: true });
194
198
  ctx.on("tools/execute", async (exec, next) => {
195
- if (exec.agent && await this.guardExecution(exec.agent) === "native")
196
- return next();
197
- return policy.duringToolExecution(exec, next);
199
+ const native = exec.agent && await this.guardExecution(exec.agent) === "native";
200
+ return this.nativeToolExecution.run(exec.token, () => native ? next() : policy.duringToolExecution(exec, next));
198
201
  }, { global: true, prepend: true });
199
202
  ctx.on("tools/pre-execute", async (exec, next) => {
200
203
  if (exec.agent) {
201
204
  this.trackExecution(exec);
202
- if (await this.guardExecution(exec.agent) === "native")
203
- return next();
205
+ await this.guardExecution(exec.agent);
206
+ this.beginNativeToolObservation(exec);
204
207
  }
205
208
  return next();
206
209
  }, { global: true, prepend: true });
@@ -248,14 +251,18 @@ export class NativeAdmission {
248
251
  ...(route.reasoningEffort === undefined ? {} : { reasoningEffort: route.reasoningEffort }) });
249
252
  }
250
253
  observedCall(record) {
251
- return Object.freeze({ parentSessionId: record.parent.id, parentAdmissionEpoch: record.parentAdmission.epoch,
254
+ const common = { parentSessionId: record.parent.id, parentAdmissionEpoch: record.parentAdmission.epoch,
252
255
  ...(record.childAdmission === undefined ? {} : { childAdmissionEpoch: record.childAdmission.epoch }),
253
256
  callId: record.callId, rootCallId: record.rootCallId, childId: record.childId,
254
- ...(record.runId === undefined ? {} : { runId: record.runId }), role: record.role,
255
- level: record.invocation.effectiveLevel, routeSource: record.invocation.routeSource,
256
- initialRoute: Object.freeze({ status: "known", source: "invocation-capture", route: this.observationRoute(record.invocation.initialRoute) }),
257
+ ...(record.runId === undefined ? {} : { runId: record.runId }),
257
258
  actualRoute: record.actualRoute, phase: record.phase, delivery: record.delivery,
258
- ...(record.stopReason === undefined ? {} : { stopReason: record.stopReason }) });
259
+ ...(record.stopReason === undefined ? {} : { stopReason: record.stopReason }) };
260
+ return record.kind === "native"
261
+ ? Object.freeze({ ...common, kind: "native", toolName: record.toolName, provider: record.provider,
262
+ ...(record.nativeName === undefined ? {} : { nativeName: record.nativeName }),
263
+ initialRoute: Object.freeze({ status: "unknown", reason: "missing-invocation-capture" }) })
264
+ : Object.freeze({ ...common, role: record.role, level: record.invocation.effectiveLevel, routeSource: record.invocation.routeSource,
265
+ initialRoute: Object.freeze({ status: "known", source: "invocation-capture", route: this.observationRoute(record.invocation.initialRoute) }) });
259
266
  }
260
267
  publishObservedCall(record) {
261
268
  if (this.observedCalls.get(record.parent)?.get(record.callId) !== record || this.ambiguousCallIds.get(record.parent)?.has(record.callId))
@@ -314,12 +321,17 @@ export class NativeAdmission {
314
321
  this.lifetime.signal.throwIfAborted();
315
322
  waiting.signal?.throwIfAborted();
316
323
  const parent = info.parentSessionId === undefined ? undefined : this.ctx.agents.get(info.parentSessionId);
317
- if (parent === undefined)
318
- throw new SubagentError("Child requires its exact live creator", "UNAUTHORIZED");
319
324
  const pending = this.pending.get(child.id);
320
325
  if (pending && (pending.parent !== parent || pending.provider.name !== info.provider))
321
326
  throw new SubagentError("Published child differs from its pending invocation", "UNAUTHORIZED");
322
- bindNativeParent(this.ctx.agents, child, parent);
327
+ if (parent !== undefined)
328
+ bindNativeParent(this.ctx.agents, child, parent);
329
+ else {
330
+ const ceiling = readDelegationCeiling(child.session);
331
+ if (waiting.source !== "resume" || ceiling === undefined || ceiling.creatorId !== info.parentSessionId
332
+ || ceiling.provider !== info.provider)
333
+ throw new SubagentError("Child requires its verified creator evidence", "UNAUTHORIZED");
334
+ }
323
335
  await this.created(child, waiting.source, waiting.signal);
324
336
  };
325
337
  void admission().then(waiting.resolve, error => {
@@ -329,6 +341,15 @@ export class NativeAdmission {
329
341
  });
330
342
  }
331
343
  startObservedActivation(info) {
344
+ const child = this.ctx.agents.get(info.id), token = this.nativeToolExecution.getStore();
345
+ const pending = token === undefined ? undefined : this.nativeToolObservations.get(token);
346
+ const parentId = info.parentSessionId;
347
+ if (info.local && child !== undefined && pending !== undefined && pending.admission === this.get(pending.parent)
348
+ && (parentId === undefined || parentId === pending.parent.id)
349
+ && child.session.header.origin === "subagent" && child.session.header.parentSession === pending.parent.id
350
+ && nativeIsOwnedBy(this.ctx.agents, child.id, pending.parent) && !pending.activations.has(info.id)) {
351
+ pending.activations.set(info.id, { info, child, childAdmission: this.get(child) });
352
+ }
332
353
  const record = this.observedCallChildren.get(info.id);
333
354
  if (record === undefined || info.provider !== record.provider || !info.local || record.phase === "settled")
334
355
  return;
@@ -340,8 +361,16 @@ export class NativeAdmission {
340
361
  this.publishObservedCall(record);
341
362
  }
342
363
  endObservedActivation(info) {
364
+ const parentId = info.parentSessionId;
365
+ for (const pending of this.nativeToolObservations.values()) {
366
+ const evidence = pending.activations.get(info.id);
367
+ if (evidence?.info.runId === info.runId && evidence.info.provider === info.provider && info.local
368
+ && (parentId === undefined || parentId === pending.parent.id))
369
+ evidence.end = info;
370
+ }
343
371
  const record = this.observedCallRuns.get(info.runId);
344
- if (record === undefined || record.childId !== info.id || record.provider !== info.provider || !info.local)
372
+ if (record === undefined || record.childId !== info.id || record.provider !== info.provider || !info.local
373
+ || parentId !== undefined && parentId !== record.parent.id)
345
374
  return;
346
375
  this.settleObservedActivation(record, info.stopReason);
347
376
  }
@@ -369,9 +398,19 @@ export class NativeAdmission {
369
398
  }
370
399
  observeCallRequestHeader(session, value) {
371
400
  const record = this.observedCallChildren.get(session.id), child = this.ctx.agents.get(session.id);
401
+ for (const pending of this.nativeToolObservations.values()) {
402
+ const evidence = pending.activations.get(session.id);
403
+ if (evidence !== undefined && evidence.end === undefined && evidence.child === child && child.session === session
404
+ && pending.admission === this.get(pending.parent))
405
+ evidence.header = { config: value };
406
+ }
372
407
  if (record === undefined || child === undefined || child !== record.child || child.session !== session
373
408
  || record.parentAdmission !== this.get(record.parent) || record.childAdmission !== this.get(child) || record.phase === "settled")
374
409
  return;
410
+ this.setObservedRoute(record, value);
411
+ this.publishObservedCall(record);
412
+ }
413
+ setObservedRoute(record, value) {
375
414
  const config = typeof value === "object" && value !== null ? value : undefined;
376
415
  if (typeof config?.provider !== "string" || typeof config.model !== "string"
377
416
  || config.reasoningEffort !== undefined && typeof config.reasoningEffort !== "string") {
@@ -387,9 +426,84 @@ export class NativeAdmission {
387
426
  record.actualRoute = Object.freeze({ status: "unknown", reason: "invalid-own-request-header" });
388
427
  }
389
428
  }
390
- this.publishObservedCall(record);
429
+ }
430
+ beginNativeToolObservation(exec) {
431
+ const parent = exec.agent, admission = parent && this.get(parent);
432
+ if (parent === undefined || admission === undefined)
433
+ return;
434
+ const tools = this.ctx.get("agentPresets")?.serviceFor(parent, "tools") ?? parent.ctx.tools;
435
+ const definition = tools.get(exec.name, parent), schema = definition?.output.schema;
436
+ const identity = schema?.properties?.name !== undefined ? "name" : "subagentId";
437
+ if (definition === undefined || schema?.type !== "object" || schema.additionalProperties !== false
438
+ || schema.properties?.kind?.const !== "activation" || schema.properties[identity]?.type !== "string"
439
+ || !schema.required?.includes("kind") || !schema.required.includes(identity)
440
+ || Object.keys(schema.properties).length !== 2)
441
+ return;
442
+ this.nativeToolObservations.set(exec.token, { parent, admission, definition, execute: definition.execute, activations: new Map() });
443
+ }
444
+ async finishNativeToolObservation(exec, result, pending) {
445
+ try {
446
+ if (pending.activations.size === 0 || result.isError || typeof result.value !== "object" || result.value === null || Array.isArray(result.value)
447
+ || result.value.kind !== "activation")
448
+ return;
449
+ const nativeName = typeof result.value.name === "string" && result.value.name.length > 0 ? result.value.name : undefined;
450
+ const runtime = this.ctx.get("subagents");
451
+ const childId = nativeName === undefined
452
+ ? typeof result.value.subagentId === "string" && result.value.subagentId.length > 0 ? SessionId(result.value.subagentId) : undefined
453
+ : await runtime?.resolveName?.(pending.parent, nativeName, this.lifetime.signal);
454
+ const tools = this.ctx.get("agentPresets")?.serviceFor(pending.parent, "tools") ?? pending.parent.ctx.tools;
455
+ const evidence = childId === undefined ? undefined : pending.activations.get(childId);
456
+ if (this.nativeToolObservations.get(exec.token) !== pending || this.lifetime.signal.aborted
457
+ || this.get(pending.parent) !== pending.admission || this.ctx.agents.get(pending.parent.id) !== pending.parent
458
+ || exec.agent !== pending.parent || tools.get(exec.name, pending.parent) !== pending.definition
459
+ || pending.definition.execute !== pending.execute || evidence === undefined || childId === undefined)
460
+ return;
461
+ if (evidence.end === undefined && (this.ctx.agents.get(childId) !== evidence.child
462
+ || !nativeIsOwnedBy(this.ctx.agents, childId, pending.parent)))
463
+ return;
464
+ const record = { kind: "native", parent: pending.parent, parentAdmission: pending.admission,
465
+ token: exec.token, callId: String(exec.callId), rootCallId: String(exec.rootCallId), childId,
466
+ toolName: exec.name, provider: evidence.info.provider, ...(nativeName === undefined ? {} : { nativeName }),
467
+ child: evidence.child, childAdmission: this.get(evidence.child) ?? evidence.childAdmission,
468
+ runId: evidence.info.runId, phase: "active", delivery: "parent", actualRoute: { status: "unknown", reason: "no-own-request-header" } };
469
+ if (evidence.header !== undefined)
470
+ this.setObservedRoute(record, evidence.header.config);
471
+ const calls = this.callsFor(pending.parent);
472
+ let ambiguous = this.ambiguousCallIds.get(pending.parent);
473
+ if (ambiguous === undefined) {
474
+ ambiguous = new Set();
475
+ this.ambiguousCallIds.set(pending.parent, ambiguous);
476
+ }
477
+ const duplicate = calls.has(record.callId) || ambiguous.has(record.callId);
478
+ this.recordsFor(pending.parent).add(record);
479
+ if (duplicate) {
480
+ ambiguous.add(record.callId);
481
+ calls.delete(record.callId);
482
+ this.publishObservedRemoval(pending.parent, pending.admission, record.callId);
483
+ }
484
+ else
485
+ calls.set(record.callId, record);
486
+ if (evidence.end !== undefined)
487
+ this.settleObservedActivation(record, evidence.end.stopReason);
488
+ else {
489
+ this.observedCallChildren.set(childId, record);
490
+ this.observedCallRuns.set(evidence.info.runId, record);
491
+ this.publishObservedCall(record);
492
+ }
493
+ }
494
+ catch (error) {
495
+ if (!this.lifetime.signal.aborted)
496
+ this.ctx.logger.debug("Native call observation unavailable: %s", error instanceof Error ? error.message : String(error));
497
+ }
498
+ finally {
499
+ if (this.nativeToolObservations.get(exec.token) === pending)
500
+ this.nativeToolObservations.delete(exec.token);
501
+ }
391
502
  }
392
503
  finishObservedTool(exec, result) {
504
+ const native = this.nativeToolObservations.get(exec.token);
505
+ if (native !== undefined)
506
+ void this.finishNativeToolObservation(exec, result, native);
393
507
  const record = this.observedCallTokens.get(exec.token);
394
508
  if (record === undefined)
395
509
  return;
@@ -400,6 +514,9 @@ export class NativeAdmission {
400
514
  }
401
515
  }
402
516
  retireObservedParent(parent) {
517
+ for (const [token, pending] of this.nativeToolObservations)
518
+ if (pending.parent === parent)
519
+ this.nativeToolObservations.delete(token);
403
520
  for (const record of this.observedCallRecords.get(parent) ?? []) {
404
521
  this.observedCallTokens.delete(record.token);
405
522
  if (this.observedCallChildren.get(record.childId) === record)
@@ -540,15 +657,23 @@ export class NativeAdmission {
540
657
  throw new SubagentError("Native admission dependencies changed", "UNAUTHORIZED");
541
658
  if (live.preset !== agent.ctx.get("agentPresets")?.composedPreset(agent.ctx) || live.scopeParent !== scopeParentOf(agent))
542
659
  throw new SubagentError("Preset composition changed; create a new session", "UNAUTHORIZED");
543
- if (live.parent && (this.ctx.agents.get(live.parent.id) !== live.parent || !nativeIsOwnedBy(this.ctx.agents, agent.id, live.parent)))
544
- throw new SubagentError("Parent residency changed", "UNAUTHORIZED");
545
- if (live.parent)
546
- this.assertBinding(live.parent, maintenanceRoot);
660
+ const owner = live.owner ?? live.parent;
661
+ if (owner) {
662
+ const root = nativeTreeRoot(this.ctx.agents, agent);
663
+ if (this.ctx.agents.get(owner.id) !== owner || (root === undefined ? !nativeIsOwnedBy(this.ctx.agents, agent.id, owner) : root !== owner)
664
+ || agent.session.header.parentSession !== live.creatorId)
665
+ throw new SubagentError("Admission owner changed", "UNAUTHORIZED");
666
+ this.assertBinding(owner, maintenanceRoot);
667
+ }
547
668
  this.policy.assertModuleAdmission(agent);
548
669
  return live.admission;
549
670
  }
550
671
  residentTree(agent) {
551
- const tree = [agent], residents = this.ctx.agents.list();
672
+ const residents = this.ctx.agents.list();
673
+ if (nativeTreeRoot(this.ctx.agents, agent) === agent) {
674
+ return [agent, ...residents.filter(member => member !== agent && nativeTreeRoot(this.ctx.agents, member) === agent).sort((a, b) => a.id.localeCompare(b.id))];
675
+ }
676
+ const tree = [agent];
552
677
  for (let index = 0; index < tree.length; index++) {
553
678
  for (const child of residents.filter(child => nativeIsOwnedBy(this.ctx.agents, child.id, tree[index])).sort((a, b) => a.id.localeCompare(b.id))) {
554
679
  if (tree.includes(child))
@@ -788,7 +913,7 @@ export class NativeAdmission {
788
913
  ambiguous.add(callId);
789
914
  calls.delete(callId);
790
915
  }
791
- const observation = { parent, parentAdmission, token: exec.token, callId, rootCallId, childId, invocation, role,
916
+ const observation = { kind: "role", parent, parentAdmission, token: exec.token, callId, rootCallId, childId, invocation, role,
792
917
  label, provider: provider.name, delivery, phase: "reserved", actualRoute: Object.freeze({ status: "unknown", reason: "not-observed" }) };
793
918
  const pending = Object.freeze({ childId, parent, parentAdmission, callId: exec.callId, invocation, admission, provider, expectedPreset, presetScope, signal, maxDepth, toolFilter, observation });
794
919
  this.recordsFor(parent).add(observation);
@@ -881,7 +1006,10 @@ export class NativeAdmission {
881
1006
  if (existing)
882
1007
  return existing;
883
1008
  this.agentLifetimes.get(agent)?.abort();
884
- const children = new Set([...this.agents].filter(child => this.live.get(child)?.parent === agent));
1009
+ const children = new Set([...this.agents].filter(child => {
1010
+ const live = this.live.get(child);
1011
+ return (live?.owner ?? live?.parent) === agent;
1012
+ }));
885
1013
  for (const child of children)
886
1014
  child.cancel({ kind: "disposed" });
887
1015
  const receipts = [...this.activations].filter(activation => [...children].some(child => child.id === activation.childId));
@@ -950,10 +1078,19 @@ export class NativeAdmission {
950
1078
  throw new SubagentError("Role continuation requires an admitted parent", "UNAUTHORIZED");
951
1079
  return;
952
1080
  }
953
- if (!parent && (!nativeRoots(this.ctx.agents).includes(agent) || agent.session.header.origin === "subagent" || descriptor !== undefined))
954
- throw new SubagentError("Child requires its native parent", "UNAUTHORIZED");
1081
+ const ceiling = source === "resume" ? readDelegationCeiling(agent.session) : undefined;
1082
+ const root = nativeTreeRoot(this.ctx.agents, agent);
1083
+ const owner = parent ? root ?? parent : ceiling !== undefined && root !== agent ? root : undefined;
1084
+ if (ceiling !== undefined && (root?.id !== ceiling.rootId || agent.id !== ceiling.childId
1085
+ || agent.session.header.parentSession !== ceiling.creatorId || descriptor?.provider !== ceiling.provider
1086
+ || (roleFromProviderName(descriptor.provider) ?? null) !== ceiling.role))
1087
+ throw new SubagentError("Delegation ceiling identity mismatch", "UNAUTHORIZED");
1088
+ if (!parent && !owner && (!nativeRoots(this.ctx.agents).includes(agent) || agent.session.header.origin === "subagent" || descriptor !== undefined))
1089
+ throw new SubagentError("Child requires its native creator or ceiling", "UNAUTHORIZED");
1090
+ if (owner)
1091
+ this.assert(owner);
955
1092
  this.agentLifetimes.set(agent, new AbortController());
956
- signal = AbortSignal.any([signal, this.agentLifetimes.get(agent).signal, ...parent ? [this.agentLifetimes.get(parent).signal] : []]);
1093
+ signal = AbortSignal.any([signal, this.agentLifetimes.get(agent).signal, ...owner ? [this.agentLifetimes.get(owner).signal] : []]);
957
1094
  scopeLifetime(agent.ctx, "dsmm: retire exact Agent work", () => { void this.retireAgent(agent).catch(() => { }); });
958
1095
  agent.ctx.effect(() => async () => {
959
1096
  try {
@@ -971,18 +1108,56 @@ export class NativeAdmission {
971
1108
  });
972
1109
  const dependencies = this.dependencies(agent);
973
1110
  const parentAdmission = parent ? this.assert(parent) : undefined;
1111
+ const ownerAdmission = owner ? this.assert(owner) : undefined;
974
1112
  const check = () => {
975
1113
  signal.throwIfAborted();
976
1114
  this.agentLifetimes.get(agent).signal.throwIfAborted();
977
1115
  if (!this.isReady || this.ctx.agents.get(agent.id) !== agent || this.ctx.get("sessions")?.get(agent.id) !== agent.session
978
1116
  || scopeOf(agent.ctx) !== agent || original(agent.ctx.get("agents")) !== original(this.ctx.agents)
979
1117
  || this.dependencies(agent).some((value, index) => value !== dependencies[index]) || scopeParentOf(agent) !== presetScope || preset !== agent.ctx.get("agentPresets")?.composedPreset(agent.ctx)
980
- || parent && (this.assert(parent) !== parentAdmission || !nativeIsOwnedBy(this.ctx.agents, agent.id, parent)))
1118
+ || owner && (this.assert(owner) !== ownerAdmission || (root === undefined ? !nativeIsOwnedBy(this.ctx.agents, agent.id, owner) : nativeTreeRoot(this.ctx.agents, agent) !== owner))
1119
+ || pending && parent && (this.assert(parent) !== parentAdmission || !nativeIsOwnedBy(this.ctx.agents, agent.id, parent)))
981
1120
  throw new SubagentError("Activation owner changed", "UNAUTHORIZED");
982
1121
  };
983
1122
  let continuation, independent = false, continuationProvider;
984
1123
  let admission;
985
- if (parent) {
1124
+ if (ceiling !== undefined) {
1125
+ if (!owner || descriptor?.mode !== "continuable" || agent.session.header.origin !== "subagent")
1126
+ throw new SubagentError("Cold child is not continuable", "NOT_RESUMABLE");
1127
+ const subagents = this.ctx.get("subagents");
1128
+ const rows = await subagents.listDescendants(owner.id, signal);
1129
+ check();
1130
+ const row = rows.find(row => row.id === agent.id);
1131
+ if (!row || row.kind === "diagnostic" || row.parentId !== ceiling.creatorId || row.mode !== "continuable")
1132
+ throw new SubagentError("Native child catalog mismatch", "UNAUTHORIZED");
1133
+ const metadata = { childSessionId: ceiling.childId, parentSessionId: ceiling.creatorId, role: ceiling.role,
1134
+ profileId: ceiling.profileId, level: ceiling.level };
1135
+ const previous = await this.profiles.readChildIdentity(agent.id, ceiling.creatorId);
1136
+ if (previous && (previous.role !== metadata.role || previous.profileId !== metadata.profileId || previous.level !== metadata.level))
1137
+ throw new SubagentError("Child selection identity conflict", "UNAUTHORIZED");
1138
+ const ownPreset = own.find(event => event.type === "agent-preset/selected" || event.type === "subagent/descriptor");
1139
+ independent = ownPreset?.type === "agent-preset/selected";
1140
+ if (!independent && (preset !== owner.ctx.get("agentPresets")?.composedPreset(owner.ctx) || presetScope !== scopeParentOf(owner)))
1141
+ throw new SubagentError("Inherited composition changed", "UNAUTHORIZED");
1142
+ continuationProvider = ceiling.role === null ? subagents.getProvider(ceiling.provider) : this.providers.get(roleProviderName(ceiling.role));
1143
+ const currentProvider = continuationProvider && subagents.getProvider(continuationProvider.name);
1144
+ if (!continuationProvider || !currentProvider || original(currentProvider) !== original(continuationProvider))
1145
+ throw new SubagentError("Native provider is unavailable", "UNAUTHORIZED");
1146
+ admission = await this.profiles.prepareChild(agent, ownerAdmission, signal, { profileId: ceiling.profileId });
1147
+ check();
1148
+ if (ceiling.role !== null) {
1149
+ if (!admission.settings.modules.deepwork.enabled || !admission.settings.roles[ceiling.role])
1150
+ throw new SubagentError("Role is disabled", "UNAUTHORIZED");
1151
+ this.policy.assertDelegation(owner, ceiling.role);
1152
+ const manual = latestNativeModelSelection(agent)?.route;
1153
+ continuation = await resolveRoleInvocation(owner, ceiling.role, admission.settings, signal, { level: ceiling.level }, manual === undefined ? undefined : { provider: manual.provider, model: manual.model,
1154
+ ...(manual.reasoningEffort === undefined ? {} : { reasoningEffort: ReasoningEffortId(manual.reasoningEffort) }) }, resolvedModelParameterPolicy(admission));
1155
+ check();
1156
+ }
1157
+ else if (isDsmmRoleId(preset))
1158
+ throw new SubagentError("Native continuation cannot acquire a DSMM role", "UNAUTHORIZED");
1159
+ }
1160
+ else if (parent) {
986
1161
  const parentAdmission = this.assert(parent), controlled = this.policy.identity(parent).role !== undefined;
987
1162
  if (agent.session.header.parentSession !== parent.id || agent.session.header.origin !== "subagent")
988
1163
  throw new SubagentError("Invalid native child header", "UNAUTHORIZED");
@@ -1058,10 +1233,22 @@ export class NativeAdmission {
1058
1233
  try {
1059
1234
  if (pending)
1060
1235
  this.policy.bindChild(agent, parent, admission, pending.invocation, pending.maxDepth, pending.toolFilter);
1236
+ else if (ceiling)
1237
+ this.policy.bindContinuedChild(agent, owner, admission, continuation, ceiling, ceiling.role === null ? admission.settings.subagents.maxDepth : this.ctx.get("subagents").resolveMaxDepth(admission.settings.subagents.maxDepth), independent);
1061
1238
  else if (continuation)
1062
1239
  this.policy.bindChild(agent, parent, admission, continuation, this.ctx.get("subagents").resolveMaxDepth(admission.settings.subagents.maxDepth), this.policy.visibleToolCeiling(parent), independent);
1063
1240
  else if (parent)
1064
- this.policy.bindInheritedChild(agent, parent, admission, source === "resume");
1241
+ this.policy.bindInheritedChild(agent, parent, admission, source === "resume", admission.settings.subagents.maxDepth);
1242
+ if (parent && root && source !== "resume" && descriptor) {
1243
+ const provider = this.ctx.get("subagents").getProvider(descriptor.provider);
1244
+ if (!provider)
1245
+ throw new SubagentError("Native provider is unavailable", "UNAUTHORIZED");
1246
+ if (!pending)
1247
+ continuationProvider = provider;
1248
+ appendDelegationCeiling(agent.session, { version: 1, childId: agent.id, creatorId: parent.id, rootId: root.id,
1249
+ provider: descriptor.provider, ...(pending ? { role: pending.invocation.target, level: pending.invocation.effectiveLevel } : { role: null, level: null }),
1250
+ profileId: admission.profile?.id ?? null, ...this.policy.delegationCeiling(agent) });
1251
+ }
1065
1252
  await this.install(agent, this.getter(agent));
1066
1253
  check();
1067
1254
  await this.profiles.revalidateActivation(admission);
@@ -1089,7 +1276,8 @@ export class NativeAdmission {
1089
1276
  check();
1090
1277
  this.profiles.publish(agent, admission);
1091
1278
  const published = this.profiles.admission(agent);
1092
- this.live.set(agent, Object.freeze({ admission: published, preset, scopeParent: scopeParentOf(agent), parent, invocation: pending?.invocation ?? continuation, dependencies, provider: pending?.provider ?? continuationProvider }));
1279
+ this.live.set(agent, Object.freeze({ admission: published, preset, scopeParent: scopeParentOf(agent), parent, owner, creatorId: agent.session.header.parentSession,
1280
+ invocation: pending?.invocation ?? continuation, dependencies, provider: pending?.provider ?? continuationProvider }));
1093
1281
  if (pending) {
1094
1282
  this.bindObservedChild(pending.observation, agent, published);
1095
1283
  this.pending.delete(agent.id);
@@ -9,6 +9,9 @@ export interface NativeOwnershipRegistry<A extends OwnershipAgent> {
9
9
  }
10
10
  /** Root membership is native runtime evidence, never durable session lineage. */
11
11
  export declare function nativeRoots<A extends OwnershipAgent>(registry: NativeOwnershipRegistry<A>): A[];
12
+ /** A tree root is authority only while both exact objects remain registered. */
13
+ export declare function nativeTreeRoot<A extends OwnershipAgent>(registry: NativeOwnershipRegistry<A>, agent: A): A | undefined;
14
+ export declare function nativeSameTree<A extends OwnershipAgent>(registry: NativeOwnershipRegistry<A>, left: A, right: A): boolean;
12
15
  /** New runtimes require an authenticated creation edge in addition to native tree residency. */
13
16
  export declare function nativeIsOwnedBy<A extends OwnershipAgent>(registry: NativeOwnershipRegistry<A> | undefined, childId: string, parent: A): boolean;
14
17
  /** Call only after authenticating the native start's direct creator, not from a session header. */
@@ -22,6 +22,25 @@ export function nativeRoots(registry) {
22
22
  }
23
23
  });
24
24
  }
25
+ /** A tree root is authority only while both exact objects remain registered. */
26
+ export function nativeTreeRoot(registry, agent) {
27
+ if (agent.id === undefined || registry.get(agent.id) !== agent)
28
+ return undefined;
29
+ try {
30
+ const root = runtimeOf(agent)?.rootOf(agent);
31
+ if (root?.id === undefined)
32
+ return undefined;
33
+ const live = registry.get(root.id);
34
+ return live === root && runtimeOf(live)?.rootOf(live) === live ? live : undefined;
35
+ }
36
+ catch {
37
+ return undefined;
38
+ }
39
+ }
40
+ export function nativeSameTree(registry, left, right) {
41
+ const root = nativeTreeRoot(registry, left);
42
+ return root !== undefined && nativeTreeRoot(registry, right) === root;
43
+ }
25
44
  /** New runtimes require an authenticated creation edge in addition to native tree residency. */
26
45
  export function nativeIsOwnedBy(registry, childId, parent) {
27
46
  if (registry === undefined)
@@ -1,3 +1,4 @@
1
+ import type { Agent } from "@deepseek-ai/dsh-agent";
1
2
  import type { SessionId } from "@deepseek-ai/dsh-session";
2
3
  import type { SubagentActivation, SubagentCatalogEntry, SubagentDescendantListEntry, SubagentInterruptReceipt, SubagentPromptReceipt, SubagentPromptRequest, SubagentRuntime } from "@deepseek-ai/dsh-subagent";
3
4
  type ActivationSpec = Parameters<SubagentRuntime["startActivation"]>[0];
@@ -15,6 +16,6 @@ export declare function listNativeChildren(runtime: ChildDiscovery, parentId: Se
15
16
  /** Transport an already-authorized direct-child prompt through the Host's control contract. */
16
17
  export declare function promptNativeChild(runtime: SubagentRuntime, request: SubagentPromptRequest, signal: AbortSignal): Promise<SubagentPromptReceipt>;
17
18
  /** Caller-side catalog and exact-parent checks remain authoritative for this interrupt. */
18
- export declare function interruptNativeChild(runtime: SubagentRuntime, childId: SessionId, parentId: SessionId): SubagentInterruptReceipt;
19
+ export declare function interruptNativeChild(runtime: SubagentRuntime, childId: SessionId, caller: Agent): SubagentInterruptReceipt;
19
20
  export {};
20
21
  //# sourceMappingURL=native-subagents.d.ts.map
@@ -22,9 +22,10 @@ export function promptNativeChild(runtime, request, signal) {
22
22
  return runtime.prompt(prompt, signal);
23
23
  }
24
24
  /** Caller-side catalog and exact-parent checks remain authoritative for this interrupt. */
25
- export function interruptNativeChild(runtime, childId, parentId) {
25
+ export function interruptNativeChild(runtime, childId, caller) {
26
26
  if (typeof runtime.rootOf !== "function")
27
- return runtime.interruptByParent(childId, parentId, "continuable");
28
- return runtime.interruptByUser(childId);
27
+ return runtime.interruptByParent(childId, caller.id, "continuable");
28
+ runtime.interrupt(childId, { kind: "ancestor", agent: caller });
29
+ return { accepted: true };
29
30
  }
30
31
  //# sourceMappingURL=native-subagents.js.map
@@ -76,7 +76,7 @@ export declare class DsmmProfileRuntime {
76
76
  prepareHistory(profileId: string | null, signal: AbortSignal): Promise<PreparedHistoryProfile>;
77
77
  prepareRoot(agent: DshAgent, signal: AbortSignal): Promise<AdmittedProfile>;
78
78
  readChildIdentity(childId: string, parentId: string): Promise<ChildAdmissionIdentity | null>;
79
- prepareChild(agent: DshAgent, parent: DsmmProfileAdmission, signal: AbortSignal, identity?: ChildAdmissionIdentity | null): Promise<AdmittedProfile>;
79
+ prepareChild(agent: DshAgent, parent: DsmmProfileAdmission, signal: AbortSignal, identity?: Pick<ChildAdmissionIdentity, "profileId"> | null): Promise<AdmittedProfile>;
80
80
  private currentSelection;
81
81
  private prepareActivation;
82
82
  revalidateActivation(admission: DsmmProfileAdmission): Promise<void>;
package/lib/prompts.d.ts CHANGED
@@ -33,6 +33,7 @@ export interface DsmmPromptObservation {
33
33
  continuable?: boolean;
34
34
  /** Existing exact live child delivery; never a cold-recovery claim. */
35
35
  nativeChildDelivery?: boolean;
36
+ nativeTree?: boolean;
36
37
  nativeBrowserPrompt?: {
37
38
  surface: string;
38
39
  };
@@ -40,6 +41,7 @@ export interface DsmmPromptObservation {
40
41
  }
41
42
  /** Render callable delegation and lifecycle observations without repeating Agent locale. */
42
43
  export declare function buildDsmmDelegationGuidance(settings: DsmmSettings, observed: DsmmPromptObservation): string;
44
+ export declare function buildDsmmTeamGuidance(admittedNames: readonly string[]): string;
43
45
  export declare function buildDsmmAgentGuidance(settings: DsmmSettings, observed: DsmmPromptObservation): string;
44
46
  export declare function buildDeepworkPrompt(settings: DsmmSettings, options?: DeepworkPromptOptions): string;
45
47
  //# sourceMappingURL=prompts.d.ts.map