@dsmm/dsmm 0.3.30 → 0.3.32

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 (48) hide show
  1. package/agent-presets/dsmm-oracle/agent.cordis.yml +2 -0
  2. package/agent-presets/dsmm-oracle-2nd/agent.cordis.yml +2 -0
  3. package/agent-presets/dsmm-plan-critic/agent.cordis.yml +2 -0
  4. package/agent-presets/dsmm-planner/agent.cordis.yml +1 -0
  5. package/agent-presets/dsmm-reviewer/agent.cordis.yml +2 -0
  6. package/docs/compatibility.md +2 -2
  7. package/dsh-plugin.json +1 -1
  8. package/lib/agent-prompt-scope.d.ts +3 -2
  9. package/lib/agent-prompt-scope.js +11 -16
  10. package/lib/core-permission-authority.js +4 -0
  11. package/lib/host-llm.d.ts +5 -0
  12. package/lib/host-llm.js +9 -0
  13. package/lib/index.js +13 -16
  14. package/lib/live-pi-ai-owner.d.ts +1 -0
  15. package/lib/live-pi-ai-owner.js +2 -1
  16. package/lib/mode.d.ts +3 -2
  17. package/lib/mode.js +8 -12
  18. package/lib/model-routing.js +2 -2
  19. package/lib/native-admission.d.ts +2 -0
  20. package/lib/native-admission.js +80 -24
  21. package/lib/native-pi-ai-wire.js +2 -2
  22. package/lib/native-scope.d.ts +3 -3
  23. package/lib/native-scope.js +5 -6
  24. package/lib/preset-skills.d.ts +3 -2
  25. package/lib/preset-skills.js +12 -14
  26. package/lib/resident-root.d.ts +10 -0
  27. package/lib/resident-root.js +151 -0
  28. package/lib/role-identity.d.ts +2 -2
  29. package/lib/role-identity.js +1 -5
  30. package/lib/role-llm-runtime.d.ts +12 -2
  31. package/lib/role-llm-runtime.js +28 -18
  32. package/lib/role-llm.d.ts +2 -0
  33. package/lib/role-llm.js +1 -0
  34. package/lib/role-policy.d.ts +2 -0
  35. package/lib/role-policy.js +10 -10
  36. package/lib/role-routing.d.ts +1 -1
  37. package/lib/role-routing.js +4 -3
  38. package/lib/role-subagents.d.ts +2 -2
  39. package/lib/role-subagents.js +43 -63
  40. package/package.json +1 -1
  41. package/prompts/source/agents/plan-critic.md +2 -0
  42. package/prompts/source/agents/planner.md +1 -0
  43. package/prompts/source/agents/reviewer.md +2 -0
  44. package/skills/avoid-ai-slop/SKILL.md +1 -1
  45. package/skills/brainstorming/SKILL.md +1 -1
  46. package/skills/receiving-code-review/SKILL.md +2 -2
  47. package/skills/requesting-code-review/SKILL.md +1 -1
  48. package/skills/writing-plans/SKILL.md +1 -1
@@ -7,6 +7,7 @@ import { SubagentError, delegationDepthOf, foldSubagentDescriptor } from "@deeps
7
7
  import { freezeSettings } from "./settings.js";
8
8
  import { invocationView } from "./role-invocation.js";
9
9
  import { scopeLifetime } from "./lifetime.js";
10
+ import { assertResidentRootCurrent, captureResidentTool, guardResidentRealm, markResidentAgent, publishResidentAgent, restoreResidentRoot } from "./resident-root.js";
10
11
  import { sessionEvents, childOwnedSessionEvents } from "./session-scope.js";
11
12
  import { scopeParentOf, scopeOf } from "./native-scope.js";
12
13
  import { bindNativeParent, nativeIsOwnedBy, nativeRoots, nativeTreeRoot } from "./native-ownership.js";
@@ -98,6 +99,7 @@ export class NativeAdmission {
98
99
  admissionRouteFailures = new WeakMap();
99
100
  setupSelectionCursors = new WeakMap();
100
101
  ready = false;
102
+ scopes = new Map();
101
103
  install;
102
104
  admissionModelSelection;
103
105
  constructor(ctx, profiles, policy, configurationCommitted = () => { }) {
@@ -119,6 +121,8 @@ export class NativeAdmission {
119
121
  });
120
122
  ctx.effect(() => async () => {
121
123
  const results = await Promise.allSettled([...this.activations].map(activation => activation.dispose()));
124
+ const scopes = await Promise.allSettled([...this.scopes.values()].map(scope => scope.fiber.dispose()));
125
+ results.push(...scopes);
122
126
  await Promise.allSettled(this.activities.keys());
123
127
  const errors = results.flatMap(result => result.status === "rejected" ? [result.reason] : []);
124
128
  if (errors.length)
@@ -180,10 +184,21 @@ export class NativeAdmission {
180
184
  ctx.on("agent/request", async (frame, next) => { await this.guardExecution(frame.agent); return next(); }, { global: true, prepend: true });
181
185
  ctx.on("tools/execute", async (exec, next) => {
182
186
  const native = exec.agent && await this.guardExecution(exec.agent) === "native";
183
- return this.nativeToolExecution.run(exec.token, () => native ? next() : policy.duringToolExecution(exec, next));
187
+ if (native)
188
+ return this.nativeToolExecution.run(exec.token, next);
189
+ const caller = exec.signal;
190
+ if (exec.agent)
191
+ exec.signal = AbortSignal.any([caller, this.lifetime.signal, this.agentLifetimes.get(exec.agent).signal]);
192
+ try {
193
+ return await this.nativeToolExecution.run(exec.token, () => policy.duringToolExecution(exec, next));
194
+ }
195
+ finally {
196
+ exec.signal = caller;
197
+ }
184
198
  }, { global: true, prepend: true });
185
199
  ctx.on("tools/pre-execute", async (exec, next) => {
186
200
  if (exec.agent) {
201
+ captureResidentTool(exec.agent, this, exec.token);
187
202
  this.trackExecution(exec);
188
203
  await this.guardExecution(exec.agent);
189
204
  this.beginNativeToolObservation(exec);
@@ -209,6 +224,30 @@ export class NativeAdmission {
209
224
  this.install = install;
210
225
  this.ready = true;
211
226
  }
227
+ async restoreResidentRoots() {
228
+ if (this.ctx.fiber.state === 1) {
229
+ // Own services become visible on ACTIVE, after the apply callback returns.
230
+ // Claim idle roots' native maintenance slots in that same status dispatch.
231
+ const detach = this.ctx.on("internal/status", fiber => {
232
+ if (fiber.ctx !== this.ctx.fiber.ctx || fiber.state !== 2)
233
+ return;
234
+ detach();
235
+ return this.restoreResidentRoots();
236
+ }, { global: true });
237
+ return;
238
+ }
239
+ if (!this.isReady)
240
+ throw new SubagentError("DSMM owner is not ready for resident admission", "UNAUTHORIZED");
241
+ await Promise.all(nativeRoots(this.ctx.agents).map(async (agent) => {
242
+ try {
243
+ await restoreResidentRoot(agent, this, signal => this.created(agent, "startup", signal, true));
244
+ }
245
+ catch (error) {
246
+ if (!this.lifetime.signal.aborted)
247
+ this.ctx.logger.warn("Resident root %s remains revoked: %s", agent.id, error);
248
+ }
249
+ }));
250
+ }
212
251
  /** Bind asynchronous metadata preparation; later lifecycle callbacks only install its immutable result. */
213
252
  bindAdmissionModelSelection(hook) {
214
253
  this.admissionModelSelection = hook;
@@ -577,6 +616,7 @@ export class NativeAdmission {
577
616
  /** Exact process-local capture, retained after retirement so stale objects never become native. */
578
617
  owns(agent) { return this.agentLifetimes.has(agent); }
579
618
  async guardExecution(agent) {
619
+ assertResidentRootCurrent(agent, this);
580
620
  await this.awaitingPublication.get(agent)?.promise;
581
621
  if (!this.owns(agent)) {
582
622
  const registry = agent.ctx.get("agents");
@@ -741,7 +781,7 @@ export class NativeAdmission {
741
781
  }
742
782
  }
743
783
  };
744
- const install = (admission) => this.installing.run({ agent, admission }, () => this.install(agent, this.getter(agent)));
784
+ const install = (admission) => this.installing.run({ agent, admission }, () => this.install(agent, this.getter(agent), this.scopes.get(agent)));
745
785
  return {
746
786
  assertCurrent,
747
787
  prepare: async (admission, describe) => {
@@ -935,7 +975,7 @@ export class NativeAdmission {
935
975
  if (this.lifetime.signal.aborted)
936
976
  void activation.dispose().catch(error => this.ctx.logger.error(error));
937
977
  }
938
- guardTools(agent) {
978
+ guardTools(agent, scope) {
939
979
  const tools = agent.ctx.get("agentPresets")?.serviceFor(agent, "tools") ?? agent.ctx.get("tools");
940
980
  if (!tools)
941
981
  return;
@@ -944,7 +984,7 @@ export class NativeAdmission {
944
984
  return;
945
985
  installed.add(identity);
946
986
  this.guardedTools.set(agent, installed);
947
- getTraceable(agent.ctx, tools).guard(exec => {
987
+ getTraceable(scope, tools).guard(exec => {
948
988
  if (exec.agent !== agent)
949
989
  return;
950
990
  this.assert(agent);
@@ -988,20 +1028,23 @@ export class NativeAdmission {
988
1028
  return;
989
1029
  if (this.selecting.has(agent))
990
1030
  throw new SubagentError("Session configuration maintenance owns the blank composition", "UNAUTHORIZED");
991
- if (previous.parent || (this.calls.get(agent) ?? 0) !== 0 || [...this.pending.values()].some(row => row.parent === agent)
1031
+ if (previous.parent || previous.owner || (this.calls.get(agent) ?? 0) !== 0 || [...this.pending.values()].some(row => row.parent === agent)
992
1032
  || this.ctx.agents.list().some(child => nativeIsOwnedBy(this.ctx.agents, child.id, agent)) || sessionEvents(agent.session).some(event => event.type === "turn/start"))
993
1033
  throw new SubagentError("Preset change requires a new session", "UNAUTHORIZED");
994
1034
  this.policy.validateRootRole(agent, previous.admission);
995
1035
  const preset = agent.ctx.get("agentPresets")?.composedPreset(agent.ctx), scopeParent = scopeParentOf(agent);
996
- this.guardTools(agent);
997
- await this.install(agent, this.getter(agent));
1036
+ const scope = this.scopes.get(agent);
1037
+ guardResidentRealm(agent);
1038
+ this.guardTools(agent, scope);
1039
+ await this.install(agent, this.getter(agent), scope);
998
1040
  this.lifetime.signal.throwIfAborted();
999
1041
  this.agentLifetimes.get(agent).signal.throwIfAborted();
1000
1042
  if (!this.isReady || this.ctx.agents.get(agent.id) !== agent || this.live.get(agent) !== previous || scopeParentOf(agent) !== scopeParent)
1001
1043
  throw new SubagentError("Composition changed during blank admission", "UNAUTHORIZED");
1002
1044
  this.live.set(agent, Object.freeze({ ...previous, preset, scopeParent, dependencies: this.dependencies(agent) }));
1045
+ publishResidentAgent(agent, this);
1003
1046
  }
1004
- async created(agent, source, creationSignal) {
1047
+ async created(agent, source, creationSignal, restoring = false) {
1005
1048
  if (this.owns(agent)) {
1006
1049
  this.assert(agent);
1007
1050
  return;
@@ -1040,16 +1083,26 @@ export class NativeAdmission {
1040
1083
  throw new SubagentError("Delegation ceiling identity mismatch", "UNAUTHORIZED");
1041
1084
  if (!parent && !owner && (!nativeRoots(this.ctx.agents).includes(agent) || agent.session.header.origin === "subagent" || descriptor !== undefined))
1042
1085
  throw new SubagentError("Child requires its native creator or ceiling", "UNAUTHORIZED");
1086
+ const isRoot = !owner && !parent;
1087
+ if (restoring && !isRoot)
1088
+ throw new SubagentError("Resident root ownership changed", "UNAUTHORIZED");
1043
1089
  if (owner)
1044
1090
  this.assert(owner);
1091
+ if (restoring)
1092
+ guardResidentRealm(agent);
1093
+ else
1094
+ markResidentAgent(agent, this, isRoot);
1095
+ const scope = agent.ctx.plugin(function dsmmAgentGeneration() { }).ctx;
1096
+ this.scopes.set(agent, scope);
1045
1097
  this.agentLifetimes.set(agent, new AbortController());
1046
1098
  signal = AbortSignal.any([signal, this.agentLifetimes.get(agent).signal, ...owner ? [this.agentLifetimes.get(owner).signal] : []]);
1047
- scopeLifetime(agent.ctx, "dsmm: retire exact Agent work", () => { void this.retireAgent(agent).catch(() => { }); });
1048
- agent.ctx.effect(() => async () => {
1099
+ scopeLifetime(scope, "dsmm: retire exact Agent work", () => { void this.retireAgent(agent).catch(() => { }); });
1100
+ scope.effect(() => async () => {
1049
1101
  try {
1050
1102
  await this.retireAgent(agent);
1051
1103
  }
1052
1104
  finally {
1105
+ this.scopes.delete(agent);
1053
1106
  this.live.delete(agent);
1054
1107
  this.candidates.delete(agent);
1055
1108
  this.admissionRoutes.delete(agent);
@@ -1068,6 +1121,7 @@ export class NativeAdmission {
1068
1121
  if (!this.isReady || this.ctx.agents.get(agent.id) !== agent || this.ctx.get("sessions")?.get(agent.id) !== agent.session
1069
1122
  || scopeOf(agent.ctx) !== agent || original(agent.ctx.get("agents")) !== original(this.ctx.agents)
1070
1123
  || this.dependencies(agent).some((value, index) => value !== dependencies[index]) || scopeParentOf(agent) !== presetScope || preset !== agent.ctx.get("agentPresets")?.composedPreset(agent.ctx)
1124
+ || isRoot && !nativeRoots(this.ctx.agents).includes(agent)
1071
1125
  || owner && (this.assert(owner) !== ownerAdmission || (root === undefined ? !nativeIsOwnedBy(this.ctx.agents, agent.id, owner) : nativeTreeRoot(this.ctx.agents, agent) !== owner))
1072
1126
  || pending && parent && (this.assert(parent) !== parentAdmission || !nativeIsOwnedBy(this.ctx.agents, agent.id, parent)))
1073
1127
  throw new SubagentError("Activation owner changed", "UNAUTHORIZED");
@@ -1177,12 +1231,7 @@ export class NativeAdmission {
1177
1231
  }
1178
1232
  this.candidates.set(agent, admission);
1179
1233
  this.agents.add(agent);
1180
- // These registrations belong to the Agent, not the plugin owner. Revocation remains effective after unload.
1181
- agent.ctx.on("agent/pre-step", async (frame, next) => { if (frame.agent === agent)
1182
- await this.ensure(agent); return next(); }, { prepend: true });
1183
- agent.ctx.on("agent/request", async (frame, next) => { if (frame.agent === agent)
1184
- await this.ensure(agent); return next(); }, { prepend: true });
1185
- this.guardTools(agent);
1234
+ this.guardTools(agent, scope);
1186
1235
  try {
1187
1236
  if (pending)
1188
1237
  this.policy.bindChild(agent, parent, admission, pending.invocation, pending.maxDepth, pending.toolFilter);
@@ -1202,7 +1251,7 @@ export class NativeAdmission {
1202
1251
  provider: descriptor.provider, ...(pending ? { role: pending.invocation.target, level: pending.invocation.effectiveLevel } : { role: null, level: null }),
1203
1252
  profileId: admission.profile?.id ?? null, ...this.policy.delegationCeiling(agent) });
1204
1253
  }
1205
- await this.install(agent, this.getter(agent));
1254
+ await this.install(agent, this.getter(agent), scope);
1206
1255
  check();
1207
1256
  await this.profiles.revalidateActivation(admission);
1208
1257
  check();
@@ -1211,13 +1260,17 @@ export class NativeAdmission {
1211
1260
  if (continuationProvider && this.ctx.get("subagents").getProvider(continuationProvider.name) !== continuationProvider)
1212
1261
  throw new SubagentError("Role provider changed", "UNAUTHORIZED");
1213
1262
  // Occupancy precedes the installed guards' async policy and approval work.
1214
- agent.ctx.on("tools/pre-execute", async (exec, next) => { if (exec.agent === agent) {
1215
- this.trackExecution(exec);
1216
- await this.ensure(agent);
1217
- const denial = this.policy.preToolDenial(agent, exec.name, undefined, exec);
1218
- if (denial)
1219
- return { kind: "deny", reason: denial };
1220
- } return next(); }, { prepend: true });
1263
+ scope.on("tools/pre-execute", async (exec, next) => {
1264
+ if (exec.agent === agent) {
1265
+ captureResidentTool(agent, this, exec.token);
1266
+ this.trackExecution(exec);
1267
+ await this.ensure(agent);
1268
+ const denial = this.policy.preToolDenial(agent, exec.name, undefined, exec);
1269
+ if (denial)
1270
+ return { kind: "deny", reason: denial };
1271
+ }
1272
+ return next();
1273
+ }, { prepend: true });
1221
1274
  const routeCandidate = await this.admissionModelSelection?.(agent, {
1222
1275
  settings: admission.settings, epoch: admission.epoch, admission, signal, assertCurrent: check,
1223
1276
  });
@@ -1231,15 +1284,18 @@ export class NativeAdmission {
1231
1284
  const published = this.profiles.admission(agent);
1232
1285
  this.live.set(agent, Object.freeze({ admission: published, preset, scopeParent: scopeParentOf(agent), parent, owner, creatorId: agent.session.header.parentSession,
1233
1286
  invocation: pending?.invocation ?? continuation, dependencies, provider: pending?.provider ?? continuationProvider }));
1287
+ publishResidentAgent(agent, this);
1234
1288
  if (pending) {
1235
1289
  this.bindObservedChild(pending.observation, agent, published);
1236
1290
  this.pending.delete(agent.id);
1237
1291
  }
1238
1292
  }
1239
1293
  catch (error) {
1294
+ this.failedInstallations.add(agent);
1240
1295
  this.admissionRoutes.delete(agent);
1241
1296
  this.admissionRouteFailures.delete(agent);
1242
1297
  this.policy.release(agent);
1298
+ await scope.fiber.dispose();
1243
1299
  throw error;
1244
1300
  }
1245
1301
  finally {
@@ -7,7 +7,7 @@ import { getBuiltinModels, getBuiltinProviders } from "@earendil-works/pi-ai/pro
7
7
  import { buildBaseOptions } from "@earendil-works/pi-ai/api/simple-options";
8
8
  import { LlmError, ReasoningEffortId, assertUsableApiKey, attributionHeaders, freezeMessage, resolveImageAttachmentAccess, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
9
9
  import { singleUsePreparedStream, snapshotDsmmCall } from "./host-llm.js";
10
- import { DsmmMissingProviderError, modelRouteToCall } from "./role-llm.js";
10
+ import { DsmmMissingProviderError, isMissingProviderError, modelRouteToCall } from "./role-llm.js";
11
11
  import { normalizeModelRouteJson } from "./route-json.js";
12
12
  function imageOffloadProfileFields(profile) {
13
13
  const extended = profile;
@@ -215,7 +215,7 @@ export function createDsmmPiAiProviderOwner(ctx, options, borrowed) {
215
215
  const models = createModels(options.auth);
216
216
  for (const profile of profiles.values())
217
217
  models.setProvider(profile.piProvider);
218
- const owner = Object.freeze({ stockAdapter, providers,
218
+ const owner = Object.freeze({ stockAdapter, providers, isMissingProviderError,
219
219
  requiresRaw: (config) => config.customParameters !== undefined || config.stop !== undefined && profiles.has(config.provider),
220
220
  supportsNativeStop: (provider) => !profiles.has(provider),
221
221
  async resolveRoute(input, signal) {
@@ -1,9 +1,9 @@
1
1
  import { Context } from "@deepseek-ai/cordis";
2
- import type { DshAgent, DshContext, DshSystemPromptContext } from "./dsh-types.js";
2
+ import type { DshAgent, DshSystemPromptContext } from "./dsh-types.js";
3
3
  type NativeScopeKey = NonNullable<DshSystemPromptContext["scope"]>;
4
4
  export declare const scopeOf: (ctx: Context) => NativeScopeKey | undefined, scopeParentOf: (key: NativeScopeKey) => NativeScopeKey | undefined;
5
5
  export declare function nativeAgentContext(agent: DshAgent): Context;
6
- /** A scope key is opaque; resolve identity against live Agents, not its fields. */
7
- export declare function agentForScope(ctx: DshContext, key: object | undefined): DshAgent | undefined;
6
+ /** Select the retained preset's implementation, but register effects on the caller's Agent layer. */
7
+ export declare function agentScopeService<K extends "systemPrompt" | "skills" | "tools">(ctx: Context, name: K): Context[K] | undefined;
8
8
  export {};
9
9
  //# sourceMappingURL=native-scope.d.ts.map
@@ -1,4 +1,4 @@
1
- import { Context } from "@deepseek-ai/cordis";
1
+ import { Context, getTraceable, symbols } from "@deepseek-ai/cordis";
2
2
  import { createRequire } from "node:module";
3
3
  // Resolve through the declared SDK peer, not a developer/global installation.
4
4
  const require = createRequire(import.meta.url);
@@ -11,10 +11,9 @@ export function nativeAgentContext(agent) {
11
11
  }
12
12
  return agent.ctx;
13
13
  }
14
- /** A scope key is opaque; resolve identity against live Agents, not its fields. */
15
- export function agentForScope(ctx, key) {
16
- if (key === undefined)
17
- return undefined;
18
- return ctx.get?.("agents")?.list().find((agent) => agent === key);
14
+ /** Select the retained preset's implementation, but register effects on the caller's Agent layer. */
15
+ export function agentScopeService(ctx, name) {
16
+ const preset = ctx.get("agentPresets")?.serviceFor({ ctx }, name);
17
+ return preset === undefined ? ctx.get(name) : getTraceable(ctx, Reflect.get(preset, symbols.original) ?? preset);
19
18
  }
20
19
  //# sourceMappingURL=native-scope.js.map
@@ -1,6 +1,7 @@
1
- import type { DshAgent, DshContext } from "./dsh-types.js";
1
+ import { type Context } from "@deepseek-ai/cordis";
2
+ import type { DshAgent } from "./dsh-types.js";
2
3
  import type { DsmmSettingsGetter } from "./settings.js";
3
4
  import type { DeepworkModeController } from "./state.js";
4
5
  /** Admission owns installation; mode changes only invalidate this Agent's lazy provider. */
5
- export declare function registerAgentSkills(ctx: DshContext, controller: DeepworkModeController, getSettings: DsmmSettingsGetter, agent: DshAgent, lifetime: AbortSignal, catalogReady?: () => boolean): void;
6
+ export declare function registerAgentSkills(ctx: Context, controller: DeepworkModeController, getSettings: DsmmSettingsGetter, agent: DshAgent, catalogReady?: () => boolean): void;
6
7
  //# sourceMappingURL=preset-skills.d.ts.map
@@ -1,10 +1,10 @@
1
- import { getTraceable } from "@deepseek-ai/cordis";
1
+ import { symbols } from "@deepseek-ai/cordis";
2
2
  import { registerBundledSkills } from "./skills.js";
3
- import { nativeAgentContext, scopeParentOf } from "./native-scope.js";
3
+ import { agentScopeService, scopeParentOf } from "./native-scope.js";
4
4
  const bindings = new WeakMap();
5
5
  /** Admission owns installation; mode changes only invalidate this Agent's lazy provider. */
6
- export function registerAgentSkills(ctx, controller, getSettings, agent, lifetime, catalogReady) {
7
- const refresh = bindings.get(agent);
6
+ export function registerAgentSkills(ctx, controller, getSettings, agent, catalogReady) {
7
+ const refresh = bindings.get(ctx);
8
8
  if (refresh) {
9
9
  refresh();
10
10
  return;
@@ -12,28 +12,26 @@ export function registerAgentSkills(ctx, controller, getSettings, agent, lifetim
12
12
  let mounted;
13
13
  const remove = () => { mounted?.provider.dispose(); mounted = undefined; };
14
14
  const reconcile = () => {
15
- const agentCtx = nativeAgentContext(agent), settings = getSettings(agent);
15
+ const settings = getSettings(agent);
16
16
  if (!settings.modules.deepwork.enabled || !controller.active(agent, settings.defaultActive)) {
17
17
  remove();
18
18
  return;
19
19
  }
20
- const presets = ctx.get?.("agentPresets");
21
- const raw = presets?.serviceFor(agent, "skills") ?? agentCtx.get("skills"), parent = scopeParentOf(agent);
22
- if (raw === undefined)
20
+ const skills = agentScopeService(ctx, "skills"), parent = scopeParentOf(agent);
21
+ if (skills === undefined)
23
22
  throw new Error("Active skills require the native skill registry");
24
- if (mounted?.parent === parent && mounted?.registry === raw) {
23
+ const realm = Reflect.get(skills, symbols.original) ?? skills;
24
+ if (mounted?.parent === parent && mounted?.realm === realm) {
25
25
  mounted.provider.invalidate();
26
26
  return;
27
27
  }
28
28
  remove();
29
- mounted = { parent, registry: raw, provider: registerBundledSkills(agentCtx, getTraceable(agentCtx, raw), { agent, controller, getSettings, catalogReady }) };
29
+ mounted = { parent, realm, provider: registerBundledSkills(ctx, skills, { agent, controller, getSettings, catalogReady }) };
30
30
  };
31
31
  const stopMode = controller.watch(changed => { if (changed === agent)
32
32
  reconcile(); });
33
- bindings.set(agent, reconcile);
34
- const dispose = () => { stopMode(); remove(); bindings.delete(agent); lifetime.removeEventListener("abort", dispose); };
35
- lifetime.addEventListener("abort", dispose, { once: true });
36
- ctx.effect?.(() => dispose);
33
+ bindings.set(ctx, reconcile);
34
+ ctx.effect(() => () => { stopMode(); remove(); bindings.delete(ctx); });
37
35
  reconcile();
38
36
  }
39
37
  //# sourceMappingURL=preset-skills.js.map
@@ -0,0 +1,10 @@
1
+ import type { Agent } from "@deepseek-ai/dsh-agent";
2
+ import type { ToolExecution } from "@deepseek-ai/dsh-tools";
3
+ import type { NativeAdmission } from "./native-admission.js";
4
+ export declare function assertResidentRootCurrent(agent: Agent, owner: NativeAdmission | undefined): void;
5
+ export declare function markResidentAgent(agent: Agent, owner: NativeAdmission, root: boolean): void;
6
+ export declare function captureResidentTool(agent: Agent, owner: NativeAdmission, token: ToolExecution["token"]): void;
7
+ export declare function guardResidentRealm(agent: Agent): void;
8
+ export declare function publishResidentAgent(agent: Agent, owner: NativeAdmission): void;
9
+ export declare function restoreResidentRoot(agent: Agent, owner: NativeAdmission, install: (signal: AbortSignal) => Promise<void>): Promise<void> | undefined;
10
+ //# sourceMappingURL=resident-root.d.ts.map
@@ -0,0 +1,151 @@
1
+ import { getTraceable, symbols } from "@deepseek-ai/cordis";
2
+ import { SubagentError } from "@deepseek-ai/dsh-subagent";
3
+ import { scopeLifetime } from "./lifetime.js";
4
+ import { scopeOf, scopeParentOf } from "./native-scope.js";
5
+ // Reloaded ESM graphs share only exact resident objects, never session-id authority.
6
+ const key = Symbol.for("@dsmm/dsmm/resident-roots");
7
+ const records = Reflect.get(globalThis, key) ?? new WeakMap();
8
+ if (!Reflect.has(globalThis, key))
9
+ Object.defineProperty(globalThis, key, { value: records });
10
+ const original = (value) => value && (Reflect.get(value, symbols.original) ?? value);
11
+ const deny = () => { throw new SubagentError("Resident root has no current DSMM admission", "UNAUTHORIZED"); };
12
+ function current(record) {
13
+ if (record.version !== 1 || !record.active || !record.owner?.isReady)
14
+ return deny();
15
+ return record.owner;
16
+ }
17
+ export function assertResidentRootCurrent(agent, owner) {
18
+ const record = records.get(agent);
19
+ if (record && original(current(record)) !== original(owner))
20
+ deny();
21
+ }
22
+ function claim(record, owner) {
23
+ record.owner = owner;
24
+ const revoke = () => {
25
+ if (record.owner !== owner)
26
+ return;
27
+ record.active = false;
28
+ record.owner = undefined;
29
+ record.afterDrain = undefined;
30
+ record.detachOwner = undefined;
31
+ };
32
+ owner.lifetime.signal.addEventListener("abort", revoke, { once: true });
33
+ record.detachOwner = () => owner.lifetime.signal.removeEventListener("abort", revoke);
34
+ }
35
+ export function markResidentAgent(agent, owner, root) {
36
+ if (records.has(agent))
37
+ deny();
38
+ const deployment = owner.ctx.get("dsmmDeploymentConfig");
39
+ const record = { version: 1, root, registry: original(owner.ctx.agents), sessions: original(owner.ctx.get("sessions")),
40
+ deploymentKey: deployment?.hostProfileKey, home: deployment?.home,
41
+ session: agent.session, presets: original(agent.ctx.get("agentPresets")), profile: original(owner.ctx.get("profileContext")),
42
+ active: false, published: false, disposed: false,
43
+ tools: new WeakSet(), tokens: new Set() };
44
+ records.set(agent, record);
45
+ scopeLifetime(agent.ctx, "dsmm: revoke resident root", () => {
46
+ record.detachOwner?.();
47
+ record.detachOwner = undefined;
48
+ record.active = false;
49
+ record.disposed = true;
50
+ record.owner = undefined;
51
+ record.afterDrain = undefined;
52
+ });
53
+ agent.ctx.on("agent/pre-step", async (frame, next) => { if (frame.agent === agent)
54
+ await current(record).ensure(agent); return next(); }, { prepend: true });
55
+ agent.ctx.on("agent/request", async (frame, next) => { if (frame.agent === agent)
56
+ await current(record).ensure(agent); return next(); }, { prepend: true });
57
+ agent.ctx.on("tools/pre-execute", async (exec, next) => {
58
+ if (exec.agent === agent) {
59
+ const owner = current(record);
60
+ record.tokens.add(exec.token);
61
+ await owner.ensure(agent);
62
+ }
63
+ return next();
64
+ }, { prepend: true });
65
+ agent.ctx.on("tools/execute", async (exec, next) => {
66
+ if (exec.agent === agent) {
67
+ current(record).assert(agent);
68
+ if (!record.tokens.has(exec.token))
69
+ deny();
70
+ }
71
+ return next();
72
+ }, { prepend: true });
73
+ agent.ctx.on("tools/result", exec => {
74
+ if (exec.agent === agent && record.tokens.delete(exec.token) && !record.tokens.size)
75
+ record.afterDrain?.();
76
+ });
77
+ claim(record, owner);
78
+ guardResidentRealm(agent);
79
+ }
80
+ export function captureResidentTool(agent, owner, token) {
81
+ const record = records.get(agent);
82
+ if (!record)
83
+ return;
84
+ assertResidentRootCurrent(agent, owner);
85
+ record.tokens.add(token);
86
+ }
87
+ export function guardResidentRealm(agent) {
88
+ const record = records.get(agent);
89
+ const tools = agent.ctx.get("agentPresets")?.serviceFor(agent, "tools") ?? agent.ctx.get("tools");
90
+ if (!record || !tools)
91
+ return;
92
+ const realm = original(tools);
93
+ if (record.tools.has(realm))
94
+ return;
95
+ getTraceable(agent.ctx, tools).guard(exec => {
96
+ if (exec.agent !== agent)
97
+ return;
98
+ current(record).assert(agent);
99
+ if (!record.tokens.has(exec.token) || realm !== original(agent.ctx.get("agentPresets")?.serviceFor(agent, "tools") ?? agent.ctx.get("tools")))
100
+ deny();
101
+ });
102
+ record.tools.add(realm);
103
+ }
104
+ export function publishResidentAgent(agent, owner) {
105
+ const record = records.get(agent);
106
+ if (record.disposed || original(record.owner) !== original(owner) || !owner.isReady)
107
+ deny();
108
+ record.scopeParent = scopeParentOf(agent);
109
+ record.preset = agent.ctx.get("agentPresets")?.composedPreset(agent.ctx);
110
+ record.published = record.active = true;
111
+ }
112
+ export function restoreResidentRoot(agent, owner, install) {
113
+ const record = records.get(agent);
114
+ if (!record || record.version !== 1 || !record.root || record.disposed || !record.published || record.owner)
115
+ return;
116
+ const valid = () => record.profile === original(owner.ctx.get("profileContext")) && record.registry === original(owner.ctx.agents) && record.registry === original(agent.ctx.get("agents"))
117
+ && record.deploymentKey === owner.ctx.get("dsmmDeploymentConfig")?.hostProfileKey && record.home === owner.ctx.get("dsmmDeploymentConfig")?.home
118
+ && record.sessions === original(owner.ctx.get("sessions")) && record.session === agent.session && owner.ctx.get("sessions")?.get(agent.id) === record.session
119
+ && record.presets === original(agent.ctx.get("agentPresets")) && record.preset === agent.ctx.get("agentPresets")?.composedPreset(agent.ctx)
120
+ && record.scopeParent === scopeParentOf(agent) && scopeOf(agent.ctx) === agent && owner.ctx.agents.get(agent.id) === agent;
121
+ if (!valid())
122
+ deny();
123
+ claim(record, owner);
124
+ const admit = (signal) => {
125
+ if (record.owner !== owner || !owner.isReady || !valid())
126
+ deny();
127
+ return install(signal);
128
+ };
129
+ const report = (error) => { if (!owner.lifetime.signal.aborted)
130
+ owner.ctx.logger.warn("Resident root %s remains revoked: %s", agent.id, error); };
131
+ const start = () => {
132
+ record.afterDrain = undefined;
133
+ if (record.disposed || record.owner !== owner)
134
+ return;
135
+ // Ordinary tool calls can outlive both the turn and the retiring plugin.
136
+ // A busy Agent/maintenance task also has to converge before taking its place.
137
+ try {
138
+ return agent.runMaintenance(admit);
139
+ }
140
+ catch {
141
+ void agent.whenIdle().then(() => {
142
+ if (!record.disposed && record.owner === owner)
143
+ return agent.runMaintenance(admit);
144
+ }).catch(report);
145
+ }
146
+ };
147
+ if (!record.tokens.size)
148
+ return start();
149
+ record.afterDrain = () => { void start()?.catch(report); };
150
+ }
151
+ //# sourceMappingURL=resident-root.js.map
@@ -1,5 +1,5 @@
1
- import type { DshAgent } from "./dsh-types.js";
1
+ import type { SystemPrompt } from "@deepseek-ai/dsh-system-prompt";
2
2
  import type { DsmmRoleId } from "./roles.js";
3
3
  /** The caller supplies the policy-admitted identity; only its Agent layer is changed. */
4
- export declare function registerRoleHarnessIdentity(agent: DshAgent, role: DsmmRoleId, child?: boolean): () => void;
4
+ export declare function registerRoleHarnessIdentity(prompt: SystemPrompt, role: DsmmRoleId, child?: boolean): () => void;
5
5
  //# sourceMappingURL=role-identity.d.ts.map
@@ -1,11 +1,7 @@
1
- import { getTraceable } from "@deepseek-ai/cordis";
2
- import { nativeAgentContext } from "./native-scope.js";
3
1
  import { buildRoleIdentity } from "./prompt-content.js";
4
2
  import { DSMM_ROLES } from "./roles.js";
5
3
  /** The caller supplies the policy-admitted identity; only its Agent layer is changed. */
6
- export function registerRoleHarnessIdentity(agent, role, child = false) {
7
- const ctx = nativeAgentContext(agent);
8
- const prompt = getTraceable(ctx, ctx.get("agentPresets")?.serviceFor({ ctx }, "systemPrompt") ?? ctx.get("systemPrompt"));
4
+ export function registerRoleHarnessIdentity(prompt, role, child = false) {
9
5
  const definition = DSMM_ROLES.find(row => row.id === role);
10
6
  const identity = prompt.section({ name: "harness:identity", order: prompt.getSectionOrder("HARNESS_IDENTITY"),
11
7
  text: buildRoleIdentity(definition) });
@@ -1,10 +1,19 @@
1
1
  import type { Context } from "@deepseek-ai/cordis";
2
- import type { DsmmLlmHostOwner, DsmmLlmAdmission, DsmmLlmRequestFrame, DsmmLlmCallConfig } from "./host-llm.js";
2
+ import type { DsmmEffortSource, DsmmLlmHostOwner, DsmmLlmAdmission, DsmmLlmRequestFrame, DsmmLlmCallConfig } from "./host-llm.js";
3
3
  import type { DsmmPiAiProviderOwner } from "./native-pi-ai-wire.js";
4
+ interface CoreLlmAdmission extends DsmmLlmAdmission {
5
+ prepare(proposal: DsmmLlmCallConfig): Promise<{
6
+ admission: DsmmLlmAdmission;
7
+ config: DsmmLlmCallConfig;
8
+ effortSource: DsmmEffortSource;
9
+ }>;
10
+ }
4
11
  export interface DsmmCoreLlmOwner {
5
12
  /** Undefined is only the physically owned ordinary-native child path. */
6
- admit(frame: DsmmLlmRequestFrame): Promise<DsmmLlmAdmission | undefined>;
13
+ admit(frame: DsmmLlmRequestFrame): Promise<CoreLlmAdmission | undefined>;
7
14
  }
15
+ /** Request preparations, route locks and invocation effort belong to the admitting Core's module instance. */
16
+ export declare function bindCoreLlmAdmission(frame: DsmmLlmRequestFrame, admission: DsmmLlmAdmission): CoreLlmAdmission;
8
17
  declare module "@deepseek-ai/cordis" {
9
18
  interface Context {
10
19
  dsmmCoreLlmOwner: DsmmCoreLlmOwner;
@@ -14,4 +23,5 @@ declare module "@deepseek-ai/cordis" {
14
23
  export declare function createDsmmRoleLlmOwner(ctx: Context, providers: Pick<DsmmPiAiProviderOwner, "generation"> & {
15
24
  requiresRaw?(config: DsmmLlmCallConfig): boolean;
16
25
  }): DsmmLlmHostOwner;
26
+ export {};
17
27
  //# sourceMappingURL=role-llm-runtime.d.ts.map