@dsmm/dsmm 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/README.md +26 -40
  2. package/cordis.patch.yml +9 -13
  3. package/docs/agent-presets.md +26 -14
  4. package/docs/compatibility.md +24 -68
  5. package/docs/deferred-native-approval-risk.md +8 -12
  6. package/docs/design.md +25 -254
  7. package/docs/lsp.md +2 -2
  8. package/docs/migration-from-ocmm.md +38 -51
  9. package/docs/model-routing.md +21 -21
  10. package/docs/native-permissions.md +17 -7
  11. package/docs/profiles.md +13 -9
  12. package/docs/releasing.md +25 -311
  13. package/docs/roadmap.md +52 -241
  14. package/docs/runtime-recovery.md +29 -91
  15. package/docs/safety-guards.md +19 -16
  16. package/docs/settings-status.md +23 -11
  17. package/docs/skill-sync.md +17 -17
  18. package/docs/web-fetch-fakeip.md +82 -0
  19. package/lib/child-capture.d.ts +12 -2
  20. package/lib/child-capture.js +22 -5
  21. package/lib/client/ChildPermissions.d.ts +43 -0
  22. package/lib/client/ChildPermissions.js +100 -0
  23. package/lib/client/ChildrenPanel.d.ts +5 -5
  24. package/lib/client/ChildrenPanel.js +6 -18
  25. package/lib/client/DeploymentFields.d.ts +32 -0
  26. package/lib/client/DeploymentFields.js +70 -0
  27. package/lib/client/DeploymentPage.d.ts +21 -6
  28. package/lib/client/DeploymentPage.js +80 -112
  29. package/lib/client/HistoryContinuation.d.ts +7 -0
  30. package/lib/client/HistoryContinuation.js +22 -0
  31. package/lib/client/NativeCommandEntries.d.ts +17 -0
  32. package/lib/client/NativeCommandEntries.js +68 -0
  33. package/lib/client/PermissionPanelEntry.d.ts +8 -0
  34. package/lib/client/PermissionPanelEntry.js +11 -0
  35. package/lib/client/PermissionsPanel.d.ts +3 -1
  36. package/lib/client/PermissionsPanel.js +10 -9
  37. package/lib/client/ProfilesSection.js +8 -6
  38. package/lib/client/RoleAvailability.js +1 -1
  39. package/lib/client/RoleToolView-data.d.ts +35 -0
  40. package/lib/client/RoleToolView-data.js +52 -0
  41. package/lib/client/RoleToolView-locales.d.ts +36 -0
  42. package/lib/client/RoleToolView-locales.js +20 -0
  43. package/lib/client/RoleToolView.d.ts +10 -0
  44. package/lib/client/RoleToolView.js +47 -0
  45. package/lib/client/SessionProfiles.js +51 -24
  46. package/lib/client/TemporaryPermissions.d.ts +21 -0
  47. package/lib/client/TemporaryPermissions.js +94 -0
  48. package/lib/client/children-controller.d.ts +2 -8
  49. package/lib/client/children-controller.js +1 -26
  50. package/lib/client/children-locales.d.ts +27 -52
  51. package/lib/client/children-locales.js +8 -12
  52. package/lib/client/configuration-display.d.ts +5 -0
  53. package/lib/client/configuration-display.js +11 -0
  54. package/lib/client/controller.d.ts +18 -0
  55. package/lib/client/controller.js +118 -28
  56. package/lib/client/deployment-controller.d.ts +13 -1
  57. package/lib/client/deployment-controller.js +84 -24
  58. package/lib/client/deployment-data.d.ts +1 -0
  59. package/lib/client/deployment-data.js +4 -1
  60. package/lib/client/deployment-locales.d.ts +43 -0
  61. package/lib/client/deployment-locales.js +45 -19
  62. package/lib/client/deployment-modules.d.ts +9 -0
  63. package/lib/client/deployment-modules.js +24 -0
  64. package/lib/client/deployment-workspace.d.ts +48 -0
  65. package/lib/client/deployment-workspace.js +123 -0
  66. package/lib/client/history-controller.d.ts +29 -0
  67. package/lib/client/history-controller.js +111 -0
  68. package/lib/client/history-entry.d.ts +11 -0
  69. package/lib/client/history-entry.js +106 -0
  70. package/lib/client/history-locales.d.ts +95 -0
  71. package/lib/client/history-locales.js +41 -0
  72. package/lib/client/index.js +26 -76
  73. package/lib/client/locales.d.ts +13 -0
  74. package/lib/client/locales.js +104 -86
  75. package/lib/client/permission-controller.d.ts +1 -0
  76. package/lib/client/permission-controller.js +3 -2
  77. package/lib/client/permission-locales.d.ts +51 -0
  78. package/lib/client/permission-locales.js +59 -19
  79. package/lib/client/session-labels.js +4 -3
  80. package/lib/client/styles.d.ts +1 -1
  81. package/lib/client/styles.js +9 -8
  82. package/lib/client/temporary-permission-client.d.ts +20 -0
  83. package/lib/client/temporary-permission-client.js +52 -0
  84. package/lib/client/temporary-permission-entry.d.ts +6 -0
  85. package/lib/client/temporary-permission-entry.js +56 -0
  86. package/lib/client.js +5691 -3439
  87. package/lib/commands.js +2 -0
  88. package/lib/config-file-store.d.ts +3 -0
  89. package/lib/config-file-store.js +4 -2
  90. package/lib/config-revision.d.ts +5 -0
  91. package/lib/config-revision.js +11 -0
  92. package/lib/core-permission-authority.js +11 -41
  93. package/lib/deployment-config.d.ts +9 -2
  94. package/lib/deployment-config.js +35 -8
  95. package/lib/deployment-permission-rules.js +19 -7
  96. package/lib/dsh-types.d.ts +1 -1
  97. package/lib/history-candidates.d.ts +30 -0
  98. package/lib/history-candidates.js +176 -0
  99. package/lib/history-codec.d.ts +11 -0
  100. package/lib/history-codec.js +102 -0
  101. package/lib/history-contract.d.ts +44 -0
  102. package/lib/history-contract.js +2 -0
  103. package/lib/history-controller.d.ts +42 -0
  104. package/lib/history-controller.js +329 -0
  105. package/lib/history-policy.d.ts +4 -0
  106. package/lib/history-policy.js +8 -0
  107. package/lib/history-remote.d.ts +28 -0
  108. package/lib/history-remote.js +17 -0
  109. package/lib/history-rpc.d.ts +24 -0
  110. package/lib/history-rpc.js +136 -0
  111. package/lib/history-support.d.ts +15 -0
  112. package/lib/history-support.js +21 -0
  113. package/lib/history-types.d.ts +67 -0
  114. package/lib/history-types.js +2 -0
  115. package/lib/host-llm-startup.js +3 -3
  116. package/lib/host-llm.d.ts +9 -3
  117. package/lib/host-llm.js +48 -8
  118. package/lib/index.d.ts +18 -20
  119. package/lib/index.js +147 -415
  120. package/lib/lifetime.d.ts +4 -0
  121. package/lib/lifetime.js +22 -0
  122. package/lib/live-pi-ai-owner.d.ts +1 -0
  123. package/lib/live-pi-ai-owner.js +8 -4
  124. package/lib/mode.d.ts +3 -2
  125. package/lib/mode.js +28 -41
  126. package/lib/model-routing.d.ts +2 -8
  127. package/lib/model-routing.js +59 -175
  128. package/lib/native-admission.d.ts +77 -0
  129. package/lib/native-admission.js +503 -0
  130. package/lib/native-pi-ai-wire.d.ts +3 -1
  131. package/lib/native-pi-ai-wire.js +47 -13
  132. package/lib/permission-child-control.d.ts +36 -0
  133. package/lib/permission-child-control.js +164 -0
  134. package/lib/permission-control.d.ts +2 -3
  135. package/lib/permission-control.js +7 -0
  136. package/lib/permission-controller.js +2 -2
  137. package/lib/permission-exec-argv.d.ts +32 -0
  138. package/lib/permission-exec-argv.js +171 -0
  139. package/lib/permission-fs-search.js +2 -1
  140. package/lib/permission-fs.js +2 -1
  141. package/lib/permission-native-rules.js +3 -3
  142. package/lib/permission-temporary-files.d.ts +19 -0
  143. package/lib/permission-temporary-files.js +248 -0
  144. package/lib/permission-temporary-grants.d.ts +106 -0
  145. package/lib/permission-temporary-grants.js +198 -0
  146. package/lib/permission-temporary-install.d.ts +25 -0
  147. package/lib/permission-temporary-install.js +86 -0
  148. package/lib/permission-temporary-remote.d.ts +19 -0
  149. package/lib/permission-temporary-remote.js +75 -0
  150. package/lib/permission-temporary-rpc.d.ts +37 -0
  151. package/lib/permission-temporary-rpc.js +154 -0
  152. package/lib/permission-temporary-types.d.ts +47 -0
  153. package/lib/permission-temporary-types.js +2 -0
  154. package/lib/permission-temporary-web.d.ts +11 -0
  155. package/lib/permission-temporary-web.js +154 -0
  156. package/lib/permission-web.js +2 -1
  157. package/lib/preset-registry.js +3 -2
  158. package/lib/preset-skills.d.ts +3 -12
  159. package/lib/preset-skills.js +28 -58
  160. package/lib/profile-diagnostics.d.ts +1 -1
  161. package/lib/profile-diagnostics.js +2 -2
  162. package/lib/profile-remote.js +61 -22
  163. package/lib/profile-rpc.d.ts +4 -3
  164. package/lib/profile-rpc.js +35 -37
  165. package/lib/profile-runtime.d.ts +42 -74
  166. package/lib/profile-runtime.js +314 -617
  167. package/lib/profile-store.d.ts +20 -8
  168. package/lib/profile-store.js +112 -63
  169. package/lib/profile-types.d.ts +19 -4
  170. package/lib/profiles.d.ts +3 -1
  171. package/lib/profiles.js +9 -7
  172. package/lib/prompt-content.d.ts +1 -0
  173. package/lib/prompt-content.js +4 -1
  174. package/lib/prompts.d.ts +2 -12
  175. package/lib/prompts.js +11 -66
  176. package/lib/role-control-remote.d.ts +1 -4
  177. package/lib/role-control-remote.js +8 -36
  178. package/lib/role-control-rpc.d.ts +10 -36
  179. package/lib/role-control-rpc.js +112 -269
  180. package/lib/role-control-types.d.ts +3 -26
  181. package/lib/role-control-types.js +3 -4
  182. package/lib/role-identity.d.ts +5 -0
  183. package/lib/role-identity.js +10 -0
  184. package/lib/role-invocation.d.ts +3 -3
  185. package/lib/role-invocation.js +2 -2
  186. package/lib/role-llm-runtime.js +5 -11
  187. package/lib/role-policy.d.ts +19 -50
  188. package/lib/role-policy.js +136 -508
  189. package/lib/role-providers.d.ts +5 -8
  190. package/lib/role-providers.js +20 -86
  191. package/lib/role-routing.d.ts +1 -3
  192. package/lib/role-routing.js +24 -34
  193. package/lib/role-subagents.d.ts +5 -14
  194. package/lib/role-subagents.js +163 -108
  195. package/lib/runtime-integration.d.ts +5 -78
  196. package/lib/runtime-integration.js +11 -697
  197. package/lib/session-metadata.d.ts +13 -12
  198. package/lib/session-metadata.js +6 -31
  199. package/lib/session-scope.js +2 -1
  200. package/lib/settings.d.ts +5 -18
  201. package/lib/settings.js +19 -43
  202. package/lib/skills.d.ts +1 -0
  203. package/lib/skills.js +5 -1
  204. package/lib/state.d.ts +3 -2
  205. package/lib/state.js +26 -10
  206. package/lib/status.d.ts +1 -9
  207. package/lib/status.js +3 -55
  208. package/lib/web-fetch-address-policy.d.ts +24 -0
  209. package/lib/web-fetch-address-policy.js +189 -0
  210. package/lib/web-fetch-fakeip.d.ts +25 -0
  211. package/lib/web-fetch-fakeip.js +107 -0
  212. package/package.json +124 -106
  213. package/patches/dsh-approval-settlement/@deepseek-ai__dsh-api-gateway@0.2.1-rc.2.patch +13291 -0
  214. package/patches/dsh-approval-settlement/@deepseek-ai__dsh-api-remotes@0.2.1-rc.2.patch +9133 -0
  215. package/patches/dsh-approval-settlement/@deepseek-ai__dsh-client-ui-approval@0.2.1-rc.2.patch +156 -0
  216. package/patches/dsh-approval-settlement/@deepseek-ai__dsh-subagent@0.2.1-rc.2.patch +3546 -0
  217. package/patches/dsh-approval-settlement/@deepseek-ai__dsh-user-approval@0.2.1-rc.2.patch +431 -0
  218. package/patches/dsh-approval-settlement/@deepseek-ai__dsh-web@0.2.1-rc.2.patch +119 -0
  219. package/patches/dsh-approval-settlement/README.md +41 -0
  220. package/patches/dsh-approval-settlement/metadata.json +10 -0
  221. package/patches/dsh-session-activation/@deepseek-ai__dsh-agent-loop@0.2.1-rc.2.patch +383 -0
  222. package/patches/dsh-session-activation/@deepseek-ai__dsh-agent-preset-registry@0.2.1-rc.2.patch +55 -0
  223. package/patches/dsh-session-activation/@deepseek-ai__dsh-api-session-controller@0.2.1-rc.2.patch +16673 -0
  224. package/patches/dsh-session-activation/@deepseek-ai__dsh-client-ui-subagent@0.2.1-rc.2.patch +29 -0
  225. package/patches/dsh-session-activation/@deepseek-ai__dsh-client-ui-workspace@0.2.1-rc.2.patch +196 -0
  226. package/patches/dsh-session-activation/@deepseek-ai__dsh-session-persistence-jsonl@0.2.1-rc.2.patch +35 -0
  227. package/patches/dsh-session-activation/@deepseek-ai__dsh-session-persistence@0.2.1-rc.2.patch +40 -0
  228. package/docs/research/deepseek-v4-pro.md +0 -74
  229. package/docs/research/oh-dsh.md +0 -96
  230. package/lib/agent-registry.d.ts +0 -36
  231. package/lib/agent-registry.js +0 -75
  232. package/lib/child-admission.d.ts +0 -40
  233. package/lib/child-admission.js +0 -151
  234. package/lib/delivery-authority.d.ts +0 -83
  235. package/lib/delivery-authority.js +0 -397
  236. package/lib/native-child-evidence.d.ts +0 -40
  237. package/lib/native-child-evidence.js +0 -104
  238. package/lib/role-runs.d.ts +0 -138
  239. package/lib/role-runs.js +0 -335
  240. package/lib/session-persistence.d.ts +0 -36
  241. package/lib/session-persistence.js +0 -304
  242. package/lib/subagent-runtime.d.ts +0 -57
  243. package/lib/subagent-runtime.js +0 -257
  244. package/prompts/deepseek-flash.md +0 -11
  245. package/prompts/deepseek-v4-pro.md +0 -14
@@ -0,0 +1,503 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+ import { getTraceable, symbols } from "@deepseek-ai/cordis";
4
+ import { SessionId } from "@deepseek-ai/dsh-session";
5
+ import { SubagentError, delegationDepthOf } from "@deepseek-ai/dsh-subagent";
6
+ import { freezeSettings } from "./settings.js";
7
+ import { scopeLifetime } from "./lifetime.js";
8
+ import { sessionEvents } from "./session-scope.js";
9
+ import { scopeParentOf } from "./native-scope.js";
10
+ import { DsmmProfileError } from "./profiles.js";
11
+ import { roleProviderName } from "./roles.js";
12
+ import { resolveRoleInvocation } from "./role-invocation.js";
13
+ import { resolvedModelParameterPolicy } from "./model-parameter-policy.js";
14
+ /** Short-lived creation correlations and exact live bindings, never an execution queue. */
15
+ export class NativeAdmission {
16
+ ctx;
17
+ profiles;
18
+ policy;
19
+ lifetime = new AbortController();
20
+ pending = new Map();
21
+ live = new WeakMap();
22
+ candidates = new WeakMap();
23
+ historyPrepared = new WeakMap();
24
+ agents = new Set();
25
+ activations = new Set();
26
+ activities = new Map();
27
+ calls = new Map();
28
+ rebindings = new WeakMap();
29
+ guardedTools = new WeakMap();
30
+ agentLifetimes = new WeakMap();
31
+ retirements = new WeakMap();
32
+ installing = new AsyncLocalStorage();
33
+ selecting = new WeakSet();
34
+ failedInstallations = new WeakSet();
35
+ blankUpdates = new WeakMap();
36
+ getters = new WeakMap();
37
+ executions = new WeakMap();
38
+ ready = false;
39
+ install;
40
+ constructor(ctx, profiles, policy) {
41
+ this.ctx = ctx;
42
+ this.profiles = profiles;
43
+ this.policy = policy;
44
+ scopeLifetime(ctx, "dsmm: close admission before owner teardown", () => {
45
+ this.lifetime.abort();
46
+ this.pending.clear();
47
+ for (const agent of this.agents)
48
+ agent.cancel({ kind: "disposed" });
49
+ });
50
+ ctx.effect(() => async () => {
51
+ const results = await Promise.allSettled([...this.activations].map(activation => activation.dispose()));
52
+ await Promise.allSettled(this.activities.keys());
53
+ const errors = results.flatMap(result => result.status === "rejected" ? [result.reason] : []);
54
+ if (errors.length)
55
+ throw new AggregateError(errors, "Native activation teardown failed");
56
+ });
57
+ ctx.on("agent/created", async (frame) => { await this.created(frame.agent, frame.source, frame.signal); }, { global: true, prepend: true });
58
+ ctx.on("agent/disposed", ({ agent }) => {
59
+ void this.retireAgent(agent).catch(() => { });
60
+ this.live.delete(agent);
61
+ this.candidates.delete(agent);
62
+ this.agents.delete(agent);
63
+ for (const activation of this.activations)
64
+ if (activation.childId === agent.id)
65
+ this.activations.delete(activation);
66
+ profiles.release(agent);
67
+ policy.release(agent);
68
+ }, { global: true });
69
+ // Unbound retained Agents are not enrolled by listing. These guards also cover startup failures.
70
+ ctx.on("agent/pre-step", async (frame, next) => { await this.ensure(frame.agent); return next(); }, { global: true, prepend: true });
71
+ ctx.on("agent/request", async (frame, next) => { await this.ensure(frame.agent); return next(); }, { global: true, prepend: true });
72
+ ctx.on("tools/execute", async (exec, next) => { if (exec.agent)
73
+ await this.ensure(exec.agent); return policy.duringToolExecution(exec, next); }, { global: true, prepend: true });
74
+ ctx.on("tools/pre-execute", async (exec, next) => {
75
+ if (exec.agent) {
76
+ const active = this.executions.get(exec.agent) ?? new Set();
77
+ active.add(exec.token);
78
+ this.executions.set(exec.agent, active);
79
+ await this.ensure(exec.agent);
80
+ }
81
+ return next();
82
+ }, { global: true, prepend: true });
83
+ ctx.on("tools/result", exec => { if (exec.agent)
84
+ this.executions.get(exec.agent)?.delete(exec.token); }, { global: true });
85
+ ctx.on("agent-preset/selected", id => {
86
+ const agent = ctx.agents.get(id);
87
+ if (!agent || !this.live.has(agent))
88
+ return;
89
+ const binding = this.rebindBlank(agent);
90
+ this.rebindings.set(agent, binding);
91
+ void binding.catch(() => { });
92
+ }, { global: true });
93
+ }
94
+ activate(install) { this.lifetime.signal.throwIfAborted(); this.install = install; this.ready = true; }
95
+ get isReady() { return this.ready && !this.lifetime.signal.aborted; }
96
+ /** Install the current configuration before publication, without enrolling a historical Agent as blank. */
97
+ async prepareHistory(agent, admission, signal) {
98
+ const check = () => {
99
+ signal.throwIfAborted();
100
+ this.lifetime.signal.throwIfAborted();
101
+ if (!this.isReady || !this.install || this.ctx.agents.get(agent.id) !== undefined || agent.session.header.origin === "subagent"
102
+ || this.ctx.get("dsmmHistoryContinuation")?.admissionForCreated(agent) !== admission)
103
+ throw new SubagentError("Exact unpublished history candidate required", "UNAUTHORIZED");
104
+ };
105
+ check();
106
+ const prepared = { admission, preset: agent.ctx.get("agentPresets")?.composedPreset(agent.ctx), scopeParent: scopeParentOf(agent), dependencies: this.dependencies(agent) };
107
+ if (prepared.dependencies.some(value => value === undefined))
108
+ throw new SubagentError("History composition is unavailable", "UNAUTHORIZED");
109
+ this.candidates.set(agent, admission);
110
+ scopeLifetime(agent.ctx, "dsmm: release unpublished history configuration", () => { this.historyPrepared.delete(agent); this.candidates.delete(agent); });
111
+ this.guardTools(agent);
112
+ await this.install(agent, this.getter(agent), "history-setup");
113
+ check();
114
+ if (prepared.dependencies.some((value, index) => value !== this.dependencies(agent)[index]) || prepared.scopeParent !== scopeParentOf(agent))
115
+ throw new SubagentError("History composition changed during setup", "UNAUTHORIZED");
116
+ this.historyPrepared.set(agent, prepared);
117
+ }
118
+ isSelecting(agent) { return this.selecting.has(agent); }
119
+ get(agent) { return this.live.get(agent)?.admission; }
120
+ configuration(agent) {
121
+ const preview = this.installing.getStore();
122
+ const value = preview?.agent === agent ? preview.admission : this.candidates.get(agent) ?? this.get(agent);
123
+ if (!value)
124
+ throw new SubagentError("No trusted live admission", "UNAUTHORIZED");
125
+ return value;
126
+ }
127
+ getter(agent) {
128
+ let getter = this.getters.get(agent);
129
+ if (getter === undefined) {
130
+ getter = () => this.configuration(agent).settings;
131
+ getter.admission = () => this.configuration(agent);
132
+ this.getters.set(agent, getter);
133
+ }
134
+ return getter;
135
+ }
136
+ dependencies(agent) {
137
+ const presets = agent.ctx.get("agentPresets");
138
+ return [presets, agent.ctx.get("subagents"), agent.ctx.get("llm"),
139
+ presets?.serviceFor(agent, "tools") ?? agent.ctx.get("tools"),
140
+ presets?.serviceFor(agent, "systemPrompt") ?? agent.ctx.get("systemPrompt"),
141
+ presets?.serviceFor(agent, "skills") ?? agent.ctx.get("skills")].map(service => service === undefined ? undefined : original(service));
142
+ }
143
+ invocation(agent) { return this.live.get(agent)?.invocation; }
144
+ async ensure(agent) { await this.rebindings.get(agent); return this.assert(agent); }
145
+ assert(agent) {
146
+ this.lifetime.signal.throwIfAborted();
147
+ this.agentLifetimes.get(agent)?.signal.throwIfAborted();
148
+ const live = this.live.get(agent);
149
+ if (this.failedInstallations.has(agent))
150
+ throw new SubagentError("Session configuration restoration failed; create a new session", "UNAUTHORIZED");
151
+ if (this.selecting.has(agent))
152
+ throw new SubagentError("Session configuration maintenance is in progress", "UNAUTHORIZED");
153
+ if (!this.ready || !live || this.ctx.agents.get(agent.id) !== agent)
154
+ throw new SubagentError("No trusted live admission; create a new session", "UNAUTHORIZED");
155
+ if (live.preset !== agent.ctx.get("agentPresets")?.composedPreset(agent.ctx) || live.scopeParent !== scopeParentOf(agent))
156
+ throw new SubagentError("Preset composition changed; create a new session", "UNAUTHORIZED");
157
+ if (live.parent && (this.ctx.agents.get(live.parent.id) !== live.parent || !this.ctx.agents.isOwnedBy(agent.id, live.parent)))
158
+ throw new SubagentError("Parent residency changed", "UNAUTHORIZED");
159
+ if (live.parent)
160
+ this.agentLifetimes.get(live.parent)?.signal.throwIfAborted();
161
+ this.policy.assertModuleAdmission(agent);
162
+ return live.admission;
163
+ }
164
+ blankReason(agent, allowQueued = false) {
165
+ if (!this.isReady || this.failedInstallations.has(agent) || this.ctx.agents.get(agent.id) !== agent || !this.ctx.agents.roots().includes(agent) || this.dependencies(agent).some(value => value === undefined))
166
+ return "runtime-unavailable";
167
+ const live = this.live.get(agent);
168
+ if (!live || live.parent || agent.session.header.origin === "subagent" || agent.session.header.isSeeded
169
+ || sessionEvents(agent.session).some(event => ["turn/start", "step/start", "request/header", "assistant/message", "tool/call"].includes(event.type)))
170
+ return "new-session-required";
171
+ if (agent.status !== "idle" || (this.calls.get(agent) ?? 0) !== 0 || (this.executions.get(agent)?.size ?? 0) !== 0 || [...this.pending.values()].some(row => row.parent === agent)
172
+ || this.ctx.agents.list().some(child => this.ctx.agents.isOwnedBy(child.id, agent))
173
+ || this.selecting.has(agent) && !allowQueued
174
+ || !allowQueued && (agent.inbox.nextTurn.length !== 0 || agent.inbox.nextStep.length !== 0))
175
+ return "busy";
176
+ if (agent.runMaintenance === undefined)
177
+ return "maintenance";
178
+ if (live.preset !== agent.ctx.get("agentPresets")?.composedPreset(agent.ctx) || live.scopeParent !== scopeParentOf(agent)
179
+ || this.agentLifetimes.get(agent)?.signal.aborted)
180
+ return "runtime-unavailable";
181
+ return undefined;
182
+ }
183
+ /** Reserved by runMaintenance; newly queued input belongs to the accepted choice. */
184
+ beginBlank(agent, signal, assertAuthority) {
185
+ const reason = this.blankReason(agent);
186
+ if (reason !== undefined)
187
+ throw new DsmmProfileError(reason === "busy" ? "busy" : "unavailable", "Only an unstarted live session can select its configuration.");
188
+ const previous = this.live.get(agent);
189
+ this.assert(agent);
190
+ const captured = this.dependencies(agent);
191
+ const preparationSignal = AbortSignal.any([signal, this.lifetime.signal, this.agentLifetimes.get(agent).signal]);
192
+ this.selecting.add(agent);
193
+ let finish;
194
+ const completion = new Promise(resolve => { finish = resolve; });
195
+ this.blankUpdates.set(agent, completion);
196
+ let prepared = false, committed = false;
197
+ let publication;
198
+ let providers = new Map();
199
+ const assertCurrent = () => {
200
+ assertAuthority();
201
+ preparationSignal.throwIfAborted();
202
+ if (this.live.get(agent) !== previous || this.blankReason(agent, true) !== undefined || this.dependencies(agent).some((value, index) => value !== captured[index])) {
203
+ throw new DsmmProfileError("conflict", "The session owner or composition changed before configuration commit.");
204
+ }
205
+ for (const [name, expected] of providers) {
206
+ const current = agent.ctx.get("subagents")?.getProvider(name);
207
+ if (current === undefined || original(current) !== original(expected) || current.capabilities.depthLimit !== true) {
208
+ throw new DsmmProfileError("conflict", "A required native provider changed before configuration commit.");
209
+ }
210
+ }
211
+ };
212
+ const install = (admission) => this.installing.run({ agent, admission }, () => this.install(agent, this.getter(agent)));
213
+ return {
214
+ assertCurrent,
215
+ prepare: async (admission, describe) => {
216
+ assertCurrent();
217
+ this.policy.validateRootRole(agent, admission);
218
+ await this.installing.run({ agent, admission }, async () => {
219
+ const targets = this.policy.targets(agent);
220
+ providers = new Map([...targets.map(roleProviderName), ...targets.length === 0 ? [] : ["spawn"]].map(name => {
221
+ const provider = agent.ctx.get("subagents")?.getProvider(name);
222
+ if (provider?.capabilities.depthLimit !== true)
223
+ throw new DsmmProfileError("activation", "An enabled role has no admitted native provider.");
224
+ return [name, provider];
225
+ }));
226
+ for (const role of targets) {
227
+ const preset = await agent.ctx.get("agentPresets").resolve(role);
228
+ assertCurrent();
229
+ if (preset.broken !== undefined)
230
+ throw new DsmmProfileError("activation", "An enabled role preset is unavailable.", role);
231
+ const routing = admission.settings.roleRouting[role];
232
+ if (routing?.primary !== undefined || routing?.fallbackRoutes !== undefined) {
233
+ await resolveRoleInvocation(agent, role, admission.settings, preparationSignal, {}, undefined, resolvedModelParameterPolicy(admission));
234
+ assertCurrent();
235
+ }
236
+ for (const level of ["low", "high", "max"])
237
+ if (routing?.levels?.[level] !== undefined) {
238
+ await resolveRoleInvocation(agent, role, admission.settings, preparationSignal, { level }, undefined, resolvedModelParameterPolicy(admission));
239
+ assertCurrent();
240
+ }
241
+ }
242
+ });
243
+ assertCurrent();
244
+ prepared = true;
245
+ await install(admission);
246
+ assertCurrent();
247
+ this.installing.run({ agent, admission }, describe);
248
+ publication = Object.freeze({ ...previous, admission });
249
+ },
250
+ committed: () => { this.live.set(agent, publication); committed = true; },
251
+ close: async () => {
252
+ try {
253
+ if (prepared && !committed && this.ctx.agents.get(agent.id) === agent && !this.lifetime.signal.aborted)
254
+ await install(previous.admission);
255
+ }
256
+ catch (error) {
257
+ this.failedInstallations.add(agent);
258
+ const refusal = new DsmmProfileError("activation", "Session configuration restoration failed. Execution is disabled; create a new session.");
259
+ refusal.cause = error;
260
+ throw refusal;
261
+ }
262
+ finally {
263
+ this.selecting.delete(agent);
264
+ this.blankUpdates.delete(agent);
265
+ finish();
266
+ }
267
+ },
268
+ };
269
+ }
270
+ async reserve(exec, invocation, provider, maxDepth) {
271
+ const parent = exec.agent;
272
+ if (!parent)
273
+ throw new SubagentError("Calling Agent required", "UNAUTHORIZED");
274
+ const parentAdmission = this.assert(parent);
275
+ this.policy.assertDelegation(parent, invocation.target);
276
+ const toolFilter = this.policy.delegationToolFilter(exec);
277
+ const presets = this.ctx.get("agentPresets");
278
+ if (!presets)
279
+ throw new SubagentError("Native preset registry unavailable", "UNSUPPORTED_CAPABILITY");
280
+ const lease = await presets.acquireScope(invocation.target);
281
+ try {
282
+ exec.signal.throwIfAborted();
283
+ this.assert(parent);
284
+ const currentProvider = this.ctx.get("subagents")?.getProvider(provider.name);
285
+ if (!currentProvider || original(currentProvider) !== original(provider) || this.get(parent) !== parentAdmission)
286
+ throw new SubagentError("Creation dependencies changed", "UNAUTHORIZED");
287
+ const childId = SessionId(randomUUID()), signal = AbortSignal.any([exec.signal, this.lifetime.signal, this.agentLifetimes.get(parent).signal]);
288
+ const admission = freezeSettings({ ...parentAdmission, settings: structuredClone(parentAdmission.settings) });
289
+ const pending = Object.freeze({ childId, parent, parentAdmission, callId: exec.callId, invocation, admission, provider, presetScope: lease.key, signal, maxDepth, toolFilter });
290
+ this.pending.set(childId, pending);
291
+ const revoke = () => { if (this.pending.get(childId) === pending)
292
+ this.pending.delete(childId); };
293
+ signal.addEventListener("abort", revoke, { once: true });
294
+ return { childId, signal, release: async () => { revoke(); signal.removeEventListener("abort", revoke); await lease[Symbol.asyncDispose](); } };
295
+ }
296
+ catch (error) {
297
+ await lease[Symbol.asyncDispose]();
298
+ throw error;
299
+ }
300
+ }
301
+ async duringCall(agent, signal, operation) {
302
+ this.assert(agent);
303
+ const merged = AbortSignal.any([signal, this.lifetime.signal, this.agentLifetimes.get(agent).signal]);
304
+ const task = Promise.resolve().then(() => operation(merged));
305
+ this.calls.set(agent, (this.calls.get(agent) ?? 0) + 1);
306
+ this.activities.set(task, agent);
307
+ try {
308
+ return await task;
309
+ }
310
+ finally {
311
+ this.activities.delete(task);
312
+ const remaining = this.calls.get(agent) - 1;
313
+ if (remaining)
314
+ this.calls.set(agent, remaining);
315
+ else
316
+ this.calls.delete(agent);
317
+ }
318
+ }
319
+ prepare(request, provider) {
320
+ const pending = this.pending.get(request.sessionId);
321
+ if (!pending || pending.parent !== request.parent || original(pending.provider) !== original(provider))
322
+ throw new SubagentError("Role creation requires its exact pending call", "UNAUTHORIZED");
323
+ this.check(pending);
324
+ request.signal.throwIfAborted();
325
+ }
326
+ check(pending) {
327
+ pending.signal.throwIfAborted();
328
+ this.lifetime.signal.throwIfAborted();
329
+ const provider = this.ctx.get("subagents")?.getProvider(pending.provider.name);
330
+ if (this.pending.get(pending.childId) !== pending || this.assert(pending.parent) !== pending.parentAdmission || provider === undefined || original(provider) !== original(pending.provider))
331
+ throw new SubagentError("Pending call lost its owner", "UNAUTHORIZED");
332
+ }
333
+ track(activation) {
334
+ this.activations.add(activation);
335
+ void activation.result.finally(() => { if (!this.ctx.agents.get(activation.childId))
336
+ this.activations.delete(activation); }).catch(() => { });
337
+ if (this.lifetime.signal.aborted)
338
+ void activation.dispose().catch(error => this.ctx.logger.error(error));
339
+ }
340
+ guardTools(agent) {
341
+ const tools = agent.ctx.get("agentPresets")?.serviceFor(agent, "tools") ?? agent.ctx.get("tools");
342
+ if (!tools)
343
+ return;
344
+ const identity = original(tools), installed = this.guardedTools.get(agent) ?? new Set();
345
+ if (installed.has(identity))
346
+ return;
347
+ installed.add(identity);
348
+ this.guardedTools.set(agent, installed);
349
+ getTraceable(agent.ctx, tools).guard(exec => {
350
+ if (exec.agent !== agent)
351
+ return;
352
+ this.assert(agent);
353
+ const current = agent.ctx.get("agentPresets")?.serviceFor(agent, "tools") ?? agent.ctx.get("tools");
354
+ if (!current || original(current) !== identity)
355
+ return "The tool realm is no longer the admitted composition";
356
+ this.policy.assertToolExecution(exec);
357
+ return this.policy.preToolDenial(agent, exec.name, undefined, exec);
358
+ });
359
+ }
360
+ retireAgent(agent) {
361
+ const existing = this.retirements.get(agent);
362
+ if (existing)
363
+ return existing;
364
+ this.agentLifetimes.get(agent)?.abort();
365
+ const children = new Set([...this.agents].filter(child => this.live.get(child)?.parent === agent));
366
+ for (const child of children)
367
+ child.cancel({ kind: "disposed" });
368
+ const receipts = [...this.activations].filter(activation => [...children].some(child => child.id === activation.childId));
369
+ const retirement = Promise.allSettled(receipts.map(activation => activation.dispose())).then(results => {
370
+ const errors = results.flatMap(result => result.status === "rejected" ? [result.reason] : []);
371
+ if (errors.length)
372
+ throw new AggregateError(errors, "Native descendants failed to retire");
373
+ });
374
+ const settled = retirement.then(async () => { await Promise.allSettled([...this.activities].filter(([, actor]) => actor === agent).map(([activity]) => activity)); });
375
+ this.retirements.set(agent, settled);
376
+ return settled;
377
+ }
378
+ async rebindBlank(agent) {
379
+ await this.blankUpdates.get(agent);
380
+ this.lifetime.signal.throwIfAborted();
381
+ const previous = this.live.get(agent);
382
+ if (previous === undefined)
383
+ throw new SubagentError("The blank Agent owner was retired", "UNAUTHORIZED");
384
+ if (previous.preset === agent.ctx.get("agentPresets")?.composedPreset(agent.ctx) && previous.scopeParent === scopeParentOf(agent))
385
+ return;
386
+ if (this.selecting.has(agent))
387
+ throw new SubagentError("Session configuration maintenance owns the blank composition", "UNAUTHORIZED");
388
+ if (previous.parent || (this.calls.get(agent) ?? 0) !== 0 || [...this.pending.values()].some(row => row.parent === agent)
389
+ || this.ctx.agents.list().some(child => this.ctx.agents.isOwnedBy(child.id, agent)) || sessionEvents(agent.session).some(event => event.type === "turn/start"))
390
+ throw new SubagentError("Preset change requires a new session", "UNAUTHORIZED");
391
+ this.policy.validateRootRole(agent, previous.admission);
392
+ const preset = agent.ctx.get("agentPresets")?.composedPreset(agent.ctx), scopeParent = scopeParentOf(agent);
393
+ this.guardTools(agent);
394
+ await this.install(agent, this.getter(agent));
395
+ this.lifetime.signal.throwIfAborted();
396
+ this.agentLifetimes.get(agent).signal.throwIfAborted();
397
+ if (this.ctx.agents.get(agent.id) !== agent || this.live.get(agent) !== previous || scopeParentOf(agent) !== scopeParent)
398
+ throw new SubagentError("Composition changed during blank admission", "UNAUTHORIZED");
399
+ this.live.set(agent, Object.freeze({ ...previous, preset, scopeParent }));
400
+ }
401
+ async created(agent, source, creationSignal) {
402
+ if (this.live.has(agent)) {
403
+ this.assert(agent);
404
+ return;
405
+ }
406
+ if (!this.ready || !this.install)
407
+ throw new SubagentError("DSMM admission is initializing", "UNAUTHORIZED");
408
+ const signal = AbortSignal.any([...creationSignal ? [creationSignal] : [], this.lifetime.signal]);
409
+ signal.throwIfAborted();
410
+ const parents = this.ctx.agents.list().filter(parent => parent !== agent && this.ctx.agents.isOwnedBy(agent.id, parent));
411
+ const parent = parents[0], preset = agent.ctx.get("agentPresets")?.composedPreset(agent.ctx), presetScope = scopeParentOf(agent), pending = this.pending.get(agent.id);
412
+ if (parents.length > 1 || this.ctx.agents.get(agent.id) !== agent)
413
+ throw new SubagentError("Invalid native ownership", "UNAUTHORIZED");
414
+ let admission;
415
+ if (parent) {
416
+ const parentAdmission = this.assert(parent), controlled = this.policy.identity(parent).role !== undefined;
417
+ if (agent.session.header.parentSession !== parent.id || agent.session.header.origin !== "subagent")
418
+ throw new SubagentError("Invalid native child header", "UNAUTHORIZED");
419
+ if (pending) {
420
+ this.check(pending);
421
+ if (pending.parent !== parent || pending.parentAdmission !== parentAdmission || preset !== pending.invocation.target || agent.session.header.isSeeded
422
+ || scopeParentOf(agent) !== pending.presetScope)
423
+ throw new SubagentError("Child does not match the pending invocation", "UNAUTHORIZED");
424
+ if (pending.maxDepth !== undefined && delegationDepthOf(agent) > pending.maxDepth)
425
+ throw new SubagentError("Child exceeds depth ceiling", "UNAUTHORIZED");
426
+ admission = pending.admission;
427
+ }
428
+ else {
429
+ if (controlled || preset?.startsWith("dsmm-"))
430
+ throw new SubagentError("Use an admitted DSMM role tool; cold recovery is unavailable", "UNAUTHORIZED");
431
+ admission = parentAdmission;
432
+ }
433
+ }
434
+ else {
435
+ if (agent.session.header.origin === "subagent" || pending)
436
+ throw new SubagentError("Missing exact parent", "UNAUTHORIZED");
437
+ const history = source === "resume" ? this.ctx.get("dsmmHistoryContinuation")?.admissionForCreated(agent) : undefined;
438
+ if (source === "resume") {
439
+ const prepared = this.historyPrepared.get(agent);
440
+ if (!history || prepared?.admission !== history || prepared.preset !== preset || prepared.scopeParent !== presetScope
441
+ || prepared.dependencies.some((value, index) => value !== this.dependencies(agent)[index]))
442
+ throw new SubagentError("History resume requires its exact prepared candidate", "UNAUTHORIZED");
443
+ admission = history;
444
+ this.policy.validateRootRole(agent, admission);
445
+ }
446
+ else {
447
+ // A resume may have opened the native log, but cannot recover DSMM authorization.
448
+ if ((source !== "startup" || agent.session.header.isSeeded) && (preset?.startsWith("dsmm-") || sessionEvents(agent.session).some(event => ["deepwork/mode", "dsmm/role-policy"].includes(event.type))))
449
+ throw new SubagentError("DSMM cold recovery is unavailable", "UNAUTHORIZED");
450
+ admission = await this.profiles.prepareRoot(agent, signal);
451
+ this.policy.validateRootRole(agent, admission);
452
+ if ((source !== "startup" || agent.session.header.isSeeded) && admission.settings.defaultActive)
453
+ throw new SubagentError("Historical sessions are not automatically admitted to Deepwork", "UNAUTHORIZED");
454
+ }
455
+ }
456
+ this.agentLifetimes.set(agent, new AbortController());
457
+ scopeLifetime(agent.ctx, "dsmm: retire exact Agent work", () => { void this.retireAgent(agent).catch(() => { }); });
458
+ agent.ctx.effect(() => () => this.retireAgent(agent));
459
+ this.candidates.set(agent, admission);
460
+ this.agents.add(agent);
461
+ // These registrations belong to the Agent, not the plugin owner. Revocation remains effective after unload.
462
+ agent.ctx.on("agent/pre-step", async (frame, next) => { if (frame.agent === agent)
463
+ await this.ensure(agent); return next(); }, { prepend: true });
464
+ agent.ctx.on("agent/request", async (frame, next) => { if (frame.agent === agent)
465
+ await this.ensure(agent); return next(); }, { prepend: true });
466
+ agent.ctx.on("tools/pre-execute", async (exec, next) => { if (exec.agent === agent) {
467
+ await this.ensure(agent);
468
+ const denial = this.policy.preToolDenial(agent, exec.name, undefined, exec);
469
+ if (denial)
470
+ return { kind: "deny", reason: denial };
471
+ } return next(); }, { prepend: true });
472
+ this.guardTools(agent);
473
+ try {
474
+ if (pending)
475
+ this.policy.bindChild(agent, parent, admission, pending.invocation, pending.maxDepth, pending.toolFilter);
476
+ await this.install(agent, this.getter(agent));
477
+ signal.throwIfAborted();
478
+ this.agentLifetimes.get(agent).signal.throwIfAborted();
479
+ if (pending)
480
+ this.check(pending);
481
+ if (parent)
482
+ this.assert(parent);
483
+ if (this.ctx.agents.get(agent.id) !== agent || preset !== agent.ctx.get("agentPresets")?.composedPreset(agent.ctx)
484
+ || presetScope !== scopeParentOf(agent) || parent && !this.ctx.agents.isOwnedBy(agent.id, parent))
485
+ throw new SubagentError("Agent changed during initialization", "UNAUTHORIZED");
486
+ this.profiles.publish(agent, admission);
487
+ const published = this.profiles.admission(agent);
488
+ this.live.set(agent, Object.freeze({ admission: published, preset, scopeParent: scopeParentOf(agent), parent, invocation: pending?.invocation }));
489
+ if (pending)
490
+ this.pending.delete(agent.id);
491
+ }
492
+ catch (error) {
493
+ this.policy.release(agent);
494
+ throw error;
495
+ }
496
+ finally {
497
+ this.candidates.delete(agent);
498
+ this.historyPrepared.delete(agent);
499
+ }
500
+ }
501
+ }
502
+ function original(value) { return Reflect.get(value, symbols.original) ?? value; }
503
+ //# sourceMappingURL=native-admission.js.map
@@ -2,7 +2,7 @@ import type { Context } from "@deepseek-ai/cordis";
2
2
  import { PiAiAdapter, Config } from "@deepseek-ai/dsh-llm-pi-ai";
3
3
  import type { Options, PiAiAdapterOptions } from "@deepseek-ai/dsh-llm-pi-ai";
4
4
  import type { LlmAdapter } from "@deepseek-ai/dsh-llm";
5
- import type { DsmmLlmGeneration } from "./host-llm.js";
5
+ import type { DsmmLlmCallConfig, DsmmLlmGeneration } from "./host-llm.js";
6
6
  import type { DsmmLlmRouteResolver } from "./role-llm.js";
7
7
  export interface DsmmPiAiOwnerOptions {
8
8
  readonly config: Options;
@@ -14,6 +14,8 @@ export interface DsmmPiAiOwnerOptions {
14
14
  export interface DsmmPiAiProviderOwner extends DsmmLlmRouteResolver {
15
15
  readonly stockAdapter: PiAiAdapter;
16
16
  readonly providers: readonly string[];
17
+ requiresRaw(config: DsmmLlmCallConfig): boolean;
18
+ supportsNativeStop(provider: string): boolean;
17
19
  generation(provider: string): DsmmLlmGeneration;
18
20
  }
19
21
  declare module "@deepseek-ai/cordis" {