@linxin666/dsh-liangshen 0.4.21 → 0.4.23

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.
@@ -11,9 +11,11 @@
11
11
  * nobody named one — or, for `subagent_fork`, because the tool cannot name one
12
12
  * at all.
13
13
  *
14
- * This row closes that gap from the preset plane. Whenever the calling Session
15
- * (or the nearest ancestor that recorded one) carries an allowlist, every
16
- * delegation the row polices must resolve to an authorized route:
14
+ * This row closes that gap from the preset plane. Starting in 0.2.3-alpha.2,
15
+ * the live `subagentModelSelection` service exposes `allowedRoutes()` directly;
16
+ * on older Hosts without that service it falls back to the recorded
17
+ * `subagent/model-selection-policy` Session event. Whenever an allowlist is in
18
+ * force, every delegation the row polices must resolve to an authorized route:
17
19
  *
18
20
  * - `explicit` tools publish `provider`/`model`; the call must name a complete
19
21
  * pair, and that pair must be on the list;
@@ -140,6 +142,66 @@ export function policyRoutesOf(session) {
140
142
  return undefined
141
143
  }
142
144
 
145
+ /**
146
+ * Normalize a raw route array returned by a live `subagentModelSelection` service.
147
+ * @param entries - Raw route entries.
148
+ * @returns normalized routes, or undefined when invalid.
149
+ */
150
+ function normalizeRoutes(entries) {
151
+ if (!Array.isArray(entries)) return undefined
152
+ const routes = []
153
+ for (const entry of entries) {
154
+ const route = asRecord(entry)
155
+ const provider = asString(route?.['provider'])
156
+ const model = asString(route?.['model'])
157
+ if (provider === undefined || model === undefined) return undefined
158
+ routes.push({ provider, model })
159
+ }
160
+ return routes
161
+ }
162
+
163
+ /**
164
+ * Resolve authorized routes when the optional live `subagentModelSelection`
165
+ * service is mounted; when mounted, it is authoritative (returning `undefined`
166
+ * or throwing fails open without consulting stale session events).
167
+ * @param service - Optional `ctx.get('subagentModelSelection')` service instance.
168
+ * @returns `{ handled: true, routes }` when the live service is mounted, or
169
+ * `{ handled: false, routes: undefined }` when absent.
170
+ */
171
+ export function liveServiceRoutes(service) {
172
+ if (typeof service?.allowedRoutes !== 'function') {
173
+ return { handled: false, routes: undefined }
174
+ }
175
+ let raw
176
+ try {
177
+ raw = service.allowedRoutes()
178
+ } catch {
179
+ return { handled: true, routes: undefined }
180
+ }
181
+ if (raw === undefined) return { handled: true, routes: undefined }
182
+ return { handled: true, routes: normalizeRoutes(raw) }
183
+ }
184
+
185
+ /**
186
+ * Resolve the optional live default child route (`subagentModelSelection.defaultRoute()`,
187
+ * added in DSH 0.2.3-alpha.3 #9373). Returns `{ provider, model }` when a valid
188
+ * default route is configured, or undefined when absent / unsupported / throwing.
189
+ * @param service - Optional `ctx.get('subagentModelSelection')` service instance.
190
+ */
191
+ export function liveServiceDefaultRoute(service) {
192
+ if (typeof service?.defaultRoute !== 'function') return undefined
193
+ let raw
194
+ try {
195
+ raw = service.defaultRoute()
196
+ } catch {
197
+ return undefined
198
+ }
199
+ const route = asRecord(raw)
200
+ const provider = asString(route?.['provider'])
201
+ const model = asString(route?.['model'])
202
+ return provider !== undefined && model !== undefined ? { provider, model } : undefined
203
+ }
204
+
143
205
  /**
144
206
  * Resolve the allowlist a delegation from this agent must respect: the agent's
145
207
  * own recorded policy, else the nearest ancestor Session that recorded one.
@@ -218,18 +280,48 @@ export function delegationModeOf(toolName, explicitToolNames, inheritToolNames)
218
280
  return undefined
219
281
  }
220
282
 
283
+ /** Read whether one tool definition declares `provider` or `model` parameter fields. */
284
+ function definitionDeclaresRouteFields(definition) {
285
+ const parameters = asRecord(definition?.parameters)
286
+ if (parameters === undefined) return undefined
287
+ const properties = asRecord(parameters['properties'])
288
+ if (properties !== undefined) {
289
+ return properties['provider'] !== undefined || properties['model'] !== undefined
290
+ }
291
+ return parameters['provider'] !== undefined || parameters['model'] !== undefined
292
+ }
293
+
221
294
  /**
222
- * Ask the tool registry whether one delegation tool currently declares the
223
- * route fields. The answer is read per call: an HMR replacement or a preset
295
+ * Ask the tool execution or registry whether one delegation tool currently declares
296
+ * the route fields. The answer is read per call: an HMR replacement or a preset
224
297
  * change can remount the tool, and a stale answer would put the guard back on
225
298
  * the branch no call can satisfy.
299
+ *
300
+ * In DSH 0.2.3-alpha.3+ (`tool-registry-roots`), `tools.definitions()` was removed
301
+ * and every `ToolExecution` carries its resolved `definition` directly (including
302
+ * nested PTC calls); fall back to `tools.get(toolName, agent)` and finally legacy
303
+ * `tools.definitions(agent)` for older Hosts and test stubs.
226
304
  * @param tools - Tool registry from the Host context.
227
305
  * @param agent - Calling agent, whose scope selects the visible definitions.
228
306
  * @param toolName - Tool being dispatched.
307
+ * @param executionDefinition - Resolved `ToolExecution.definition` when available.
229
308
  * @returns whether the tool declares a route field, or undefined when the
230
309
  * registry cannot answer (which keeps the configured mode in charge).
231
310
  */
232
- export function routeParameterProbe(tools, agent, toolName) {
311
+ export function routeParameterProbe(tools, agent, toolName, executionDefinition) {
312
+ if (executionDefinition !== undefined) {
313
+ const fromExec = definitionDeclaresRouteFields(executionDefinition)
314
+ if (fromExec !== undefined) return fromExec
315
+ }
316
+ if (typeof tools?.get === 'function') {
317
+ try {
318
+ const definition = tools.get(toolName, agent)
319
+ const fromGet = definitionDeclaresRouteFields(definition)
320
+ if (fromGet !== undefined) return fromGet
321
+ } catch {
322
+ // Fall through to legacy probe when root lookup throws.
323
+ }
324
+ }
233
325
  if (typeof tools?.definitions !== 'function') return undefined
234
326
  let definitions
235
327
  try {
@@ -238,9 +330,7 @@ export function routeParameterProbe(tools, agent, toolName) {
238
330
  return undefined
239
331
  }
240
332
  const definition = (Array.isArray(definitions) ? definitions : []).find(entry => entry?.name === toolName)
241
- const parameters = asRecord(definition?.parameters)
242
- if (parameters === undefined) return undefined
243
- return parameters['provider'] !== undefined || parameters['model'] !== undefined
333
+ return definitionDeclaresRouteFields(definition)
244
334
  }
245
335
 
246
336
  /** Denial reason for a delegation that named a route on an inherit-only tool. */
@@ -306,13 +396,17 @@ export function unauthorizedReason(toolName, provider, model, allowed) {
306
396
  export function delegationDenialReason(input) {
307
397
  const {
308
398
  agent, toolName, args, sessions,
399
+ modelSelection,
400
+ getModelSelection,
309
401
  explicitToolNames = DEFAULT_EXPLICIT_TOOLS,
310
402
  inheritToolNames = DEFAULT_INHERIT_TOOLS,
311
403
  probe,
312
404
  } = input ?? {}
313
405
  const mode = delegationModeOf(toolName, explicitToolNames, inheritToolNames)
314
406
  if (mode === undefined) return undefined
315
- const allowed = authorizedRoutesFor(agent, sessions)
407
+ const liveService = typeof getModelSelection === 'function' ? getModelSelection() : modelSelection
408
+ const live = liveServiceRoutes(liveService)
409
+ const allowed = live.handled ? live.routes : authorizedRoutesFor(agent, sessions)
316
410
  if (allowed === undefined) return undefined
317
411
  const named = namedRouteOf(args)
318
412
  const complete = named.provider !== undefined && named.model !== undefined
@@ -338,7 +432,20 @@ export function delegationDenialReason(input) {
338
432
  // child would inherit. Only the COMPLETE pair falls through.
339
433
  if (!complete) return inheritedRouteDenial(agent, toolName, allowed)
340
434
  }
341
- if (!complete) return missingPairReason(toolName, named, inheritedRouteOf(agent), allowed)
435
+ if (!complete) {
436
+ // DSH 0.2.3-alpha.3 (#9373): when the operator configured a default child
437
+ // model on the Plugins page (`subagentModelSelection.defaultRoute()`), a
438
+ // call that omits both `provider` and `model` runs on that default route
439
+ // instead of inheriting the parent route. Allow it whenever that configured
440
+ // default route is itself authorized by the active allowlist.
441
+ if (named.provider === undefined && named.model === undefined) {
442
+ const defaultRoute = liveServiceDefaultRoute(liveService)
443
+ if (defaultRoute !== undefined && routesInclude(allowed, defaultRoute.provider, defaultRoute.model)) {
444
+ return undefined
445
+ }
446
+ }
447
+ return missingPairReason(toolName, named, inheritedRouteOf(agent), allowed)
448
+ }
342
449
  if (!routesInclude(allowed, named.provider, named.model)) {
343
450
  return unauthorizedReason(toolName, named.provider, named.model, allowed)
344
451
  }
@@ -358,14 +465,16 @@ export function installSubagentRoutes(ctx, config = {}) {
358
465
  const explicitToolNames = Array.isArray(config.explicitTools) ? config.explicitTools : DEFAULT_EXPLICIT_TOOLS
359
466
  const inheritToolNames = Array.isArray(config.inheritTools) ? config.inheritTools : DEFAULT_INHERIT_TOOLS
360
467
  const sessions = typeof ctx?.get === 'function' ? ctx.get('sessions') : undefined
468
+ const getModelSelection = typeof ctx?.get === 'function' ? () => ctx.get('subagentModelSelection') : undefined
361
469
  return tools.guard(execution => delegationDenialReason({
362
470
  agent: execution?.agent,
363
471
  toolName: execution?.name,
364
472
  args: execution?.arguments,
365
473
  sessions,
474
+ getModelSelection,
366
475
  explicitToolNames,
367
476
  inheritToolNames,
368
- probe: (agent, toolName) => routeParameterProbe(tools, agent, toolName),
477
+ probe: (agent, toolName) => routeParameterProbe(tools, agent, toolName, execution?.definition),
369
478
  }))
370
479
  }
371
480
 
@@ -67,14 +67,51 @@ export function apply(ctx, config) {
67
67
  DEFAULT_IDLE_RECLAIM_STEPS,
68
68
  )
69
69
 
70
+ const scopedToolsOf = (agent) => {
71
+ const agentCtx = agent?.ctx
72
+ if (agentCtx === undefined || agentCtx === null) return undefined
73
+ if (typeof agentCtx.get === 'function') {
74
+ const bound = agentCtx.get('tools')
75
+ if (bound !== undefined) return bound
76
+ }
77
+ try {
78
+ return agentCtx.tools
79
+ } catch {
80
+ return undefined
81
+ }
82
+ }
83
+
70
84
  /**
71
- * Read the session's visible tool schemas from the public API, tolerating
72
- * test stubs that only provide sdkSchemas.
85
+ * Read the session's visible tool schemas from the public API.
86
+ * Supports 0.2.3-alpha.3 `tools.getAll(agent)` (falling back to unscoped
87
+ * `tools.getAll()` when a PTC root collapses scoped reads to `run_code`),
88
+ * and tolerates legacy `schemas` / `sdkSchemas` test stubs.
73
89
  */
74
90
  const schemasFor = (agent) => {
75
- const candidates = [agent?.ctx?.tools, ctx.tools, ctx.get('tools')]
91
+ const candidates = [scopedToolsOf(agent), ctx.tools, ctx.get('tools')]
76
92
  for (const tools of candidates) {
77
93
  if (tools === undefined) continue
94
+ if (typeof tools?.getAll === 'function') {
95
+ try {
96
+ const scoped = tools.getAll(agent)
97
+ if (Array.isArray(scoped)) {
98
+ const nonTransport = scoped.filter(entry => entry?.name && entry.name !== 'run_code')
99
+ if (nonTransport.length > 0) return nonTransport
100
+ // Under a 0.2.3-alpha.3 PTC root registry, scoped getAll(agent)
101
+ // returns only [run_code]; the unscoped root stays native and holds
102
+ // the global/plugin/MCP registrations that paging covers.
103
+ if (scoped.some(entry => entry?.name === 'run_code')) {
104
+ const globalEntries = tools.getAll()
105
+ if (Array.isArray(globalEntries)) {
106
+ return globalEntries.filter(entry => entry?.name && entry.name !== 'run_code')
107
+ }
108
+ }
109
+ return []
110
+ }
111
+ } catch {
112
+ // A hostile or unavailable projection falls through to the next source.
113
+ }
114
+ }
78
115
  for (const method of ['schemas', 'sdkSchemas']) {
79
116
  if (typeof tools?.[method] !== 'function') continue
80
117
  try {
@@ -129,6 +166,9 @@ export function apply(ctx, config) {
129
166
 
130
167
  ctx.tools.register({
131
168
  name: 'tool_activate',
169
+ // Session-log-only meta-tool: never touches the execution machine, so it
170
+ // stays callable inside remote SSH execution environments (DSH 0.2.3-alpha.4+).
171
+ environment: 'independent',
132
172
  description: [
133
173
  `Load one paged tool namespace back into this session's reach — onto the wire, and the generated SDK along with it. The session catalog summarizes the paged namespaces; activating one makes its tools callable from the next request on.`,
134
174
  `At most ${maxActiveNamespaces} namespaces stay active at once: activating beyond the cap evicts the least recently used namespace back to the catalog summary.`,