@try-works/dsh-minimal-router 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/host.js ADDED
@@ -0,0 +1,554 @@
1
+ // @dsh-local-router/plugin — host runtime (plain JS, zero external deps).
2
+ //
3
+ // This module is the single source of truth the DSH host tools reach. It is a
4
+ // faithful port of packages/plugin/host/policy-api.ts and ledger-store.ts,
5
+ // which remain the Effect-typed, unit-tested canonical implementation. Keeping
6
+ // this file dependency-free means the DSH host can load it without a TypeScript
7
+ // build and without `effect` in the profile node_modules.
8
+
9
+ import { GATES, LOAD_GATES, ROUTER_ROUTE_LIMITATION, TPS_SOURCES, roleOf } from "./routing.js"
10
+
11
+ export { LOAD_GATES }
12
+
13
+ export const SURFACES = ["pre-step", "router_route", "agent/status", "agent/inbox/claimed", "turn/start"]
14
+ export const DECISIONS = ["local", "remote"]
15
+ // Run 36 (R-36-03, DEC-36-05): the policy is scopeless. A new write may only
16
+ // name these fields; a legacy document that still carries a scope is stripped.
17
+ export const PATCH_KEYS = ["floorTps", "mainModel", "advisoryModel"]
18
+ // Rows already on disk may carry the removed scope gate. New rows may not.
19
+ // LOAD_GATES comes from routing.js so host and decision engine share one set.
20
+ // Role attribution (R-36-08): an unreadable delegation depth records "unknown"
21
+ // rather than a silent l1; the Advisor is a spawn role, not a depth.
22
+ export const ROW_ROLES = ["l1", "l2", "l3", "advisor", "unknown"]
23
+
24
+ /** The single writable policy, seeded with the M0 fixture values (rev 1). */
25
+ export const initialPolicy = () => ({
26
+ floorTps: 50,
27
+ mainModel: null,
28
+ advisoryModel: null,
29
+ rev: 1
30
+ })
31
+
32
+ const validFloor = (n) => Number.isFinite(n) && Number.isInteger(n) && n >= 0 && n <= 100
33
+
34
+ /**
35
+ * A mainModel is null (no model chosen) or a complete provider/model/label
36
+ * reference. A partial object or a legacy bare string is invalid: the host must
37
+ * not route on a guess.
38
+ */
39
+ export const validMainModel = (value) =>
40
+ value === null ||
41
+ (typeof value === "object" &&
42
+ value !== null &&
43
+ !Array.isArray(value) &&
44
+ typeof value.provider === "string" && value.provider.length > 0 &&
45
+ typeof value.model === "string" && value.model.length > 0 &&
46
+ typeof value.label === "string" && value.label.length > 0)
47
+
48
+ const sameMainModel = (a, b) => {
49
+ if (a === null || b === null) return a === b
50
+ return a.provider === b.provider && a.model === b.model && a.label === b.label
51
+ }
52
+
53
+ const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value)
54
+
55
+ /**
56
+ * Resolve a stored mainModel against the discovered remote catalogue.
57
+ * A legacy bare-label string resolves when exactly one discovered model matches
58
+ * by label or model id; otherwise the field reports that it needs a choice and
59
+ * the host must not route on a guess.
60
+ *
61
+ * NOT WIRED in the shipped plugin (R-23-05 retired): the plugin has no persisted
62
+ * policy store and the config seed is dropped, so a legacy value never reaches
63
+ * this helper at boot. It remains as the tested, documented migration contract
64
+ * for a future persisted store.
65
+ */
66
+ export const resolveMainModel = (mainModel, discovered = []) => {
67
+ if (mainModel === null || mainModel === undefined) return { ok: false, reason: "unknown", ref: null }
68
+ if (validMainModel(mainModel)) return { ok: true, ref: { provider: mainModel.provider, model: mainModel.model, label: mainModel.label } }
69
+ if (typeof mainModel !== "string" || mainModel.length === 0) return { ok: false, reason: "unknown", ref: null }
70
+ const label = mainModel
71
+ const list = Array.isArray(discovered) ? discovered : []
72
+ const matches = list.filter((m) => isRecord(m) && (m.label === label || m.model === label))
73
+ if (matches.length === 1) {
74
+ const m = matches[0]
75
+ return { ok: true, ref: { provider: m.provider, model: m.model, label: typeof m.label === "string" && m.label.length > 0 ? m.label : label } }
76
+ }
77
+ return { ok: false, reason: matches.length > 1 ? "ambiguous" : "unknown", ref: null }
78
+ }
79
+
80
+ /**
81
+ * One in-memory policy + ledger runtime. apply() constructs exactly one per
82
+ * host process; tests construct fresh instances. The policy is the only
83
+ * writable object: writes are guarded by expectedRev (If-Match) and a
84
+ * conflict returns the current policy so the caller can retry.
85
+ */
86
+ export function createRouterRuntime(initial = initialPolicy()) {
87
+ const seed = initialPolicy()
88
+ const raw = initial != null && typeof initial === "object" && !Array.isArray(initial) ? { ...initial } : {}
89
+ // Run 36 (R-36-03): a seed that still names the removed scope is stripped. It
90
+ // is a dead transport fact, never a policy field.
91
+ delete raw.subagentScope
92
+ delete raw.mainScope
93
+ // A profile-provided policy may omit rev; seed defaults rather than treating
94
+ // the whole policy as invalid and silently routing every decision remote.
95
+ let policy = {
96
+ ...seed,
97
+ ...raw,
98
+ rev: Number.isInteger(raw.rev) ? raw.rev : seed.rev
99
+ }
100
+ // A non-applicable mainModel (a legacy bare string, a partial reference, or any
101
+ // malformed value) is normalised to null at the boundary so no serialisation
102
+ // path ever emits a non-applicable string. There is no legacy migration wiring:
103
+ // the plugin has no persisted policy store and the config seed is dropped
104
+ // (index.js), so a legacy value never reaches this boundary in the shipped host.
105
+ if (!validMainModel(policy.mainModel)) policy.mainModel = null
106
+ let rows = []
107
+ // Routing is opt-in: it never selects a model until the user enables it.
108
+ let enabled = true
109
+
110
+ const get = () => policy
111
+ const isEnabled = () => enabled
112
+ const setEnabled = (value) => {
113
+ enabled = value === true
114
+ return { enabled }
115
+ }
116
+
117
+ const write = (patch, expectedRev) => {
118
+ if (policy.rev !== expectedRev) return { conflict: true, current: policy }
119
+ const source = patch != null && typeof patch === "object" && !Array.isArray(patch) ? patch : {}
120
+ const unknown = Object.keys(source).find((key) => !PATCH_KEYS.includes(key))
121
+ if (unknown !== undefined) return { invalid: true, field: unknown, value: source[unknown] }
122
+ if (source.floorTps !== undefined && !validFloor(source.floorTps)) {
123
+ return { invalid: true, field: "floorTps", value: source.floorTps }
124
+ }
125
+ if (source.mainModel !== undefined && !validMainModel(source.mainModel)) {
126
+ return { invalid: true, field: "mainModel", value: source.mainModel }
127
+ }
128
+ if (source.advisoryModel !== undefined && source.advisoryModel !== null && typeof source.advisoryModel !== "string") {
129
+ return { invalid: true, field: "advisoryModel", value: source.advisoryModel }
130
+ }
131
+ const changed = PATCH_KEYS.some((key) => {
132
+ if (source[key] === undefined) return false
133
+ if (key === "mainModel") return !sameMainModel(source[key], policy[key])
134
+ return source[key] !== policy[key]
135
+ })
136
+ if (!changed) return { policy }
137
+ policy = {
138
+ floorTps: source.floorTps ?? policy.floorTps,
139
+ mainModel: source.mainModel === undefined ? policy.mainModel : source.mainModel,
140
+ advisoryModel: source.advisoryModel === undefined ? policy.advisoryModel : source.advisoryModel,
141
+ rev: policy.rev + 1
142
+ }
143
+ return { policy }
144
+ }
145
+
146
+ const validateRow = (row, options = {}) => {
147
+ if (row == null || typeof row !== "object" || Array.isArray(row)) return { field: "row", value: row }
148
+ for (const key of ["id", "requestId", "model", "reason", "at"]) {
149
+ if (typeof row[key] !== "string") return { field: key, value: row[key] }
150
+ }
151
+ for (const key of ["omlxRequestId", "agentId"]) {
152
+ if (row[key] !== null && typeof row[key] !== "string") return { field: key, value: row[key] }
153
+ }
154
+ if (!DECISIONS.includes(row.decision)) return { field: "decision", value: row.decision }
155
+ for (const key of ["contextTokens", "floorTps", "latencyMs"]) {
156
+ if (typeof row[key] !== "number" || !Number.isFinite(row[key])) return { field: key, value: row[key] }
157
+ }
158
+ for (const key of ["gate", "scope", "surface", "limitation", "notAppliedReason", "appliedBy", "provider", "selectedModel", "role", "roleReason", "tpsSource"]) {
159
+ if (row[key] !== undefined && typeof row[key] !== "string") return { field: key, value: row[key] }
160
+ }
161
+ // Run 25 T2 / Run 36 R-36-08: the role that governed the floor/model; an
162
+ // unreadable depth is recorded as "unknown", never silently rewritten to l1.
163
+ if (row.role !== undefined && !ROW_ROLES.includes(row.role)) return { field: "role", value: row.role }
164
+ // F-36-V4: tpsSource is the advertised enum, enforced on record. A legacy
165
+ // row written before the field existed (undefined) still loads.
166
+ if (row.tpsSource !== undefined && !TPS_SOURCES.includes(row.tpsSource)) {
167
+ return { field: "tpsSource", value: row.tpsSource }
168
+ }
169
+ // F-36-V2: the binding ceiling is a positive number or null (no ceiling).
170
+ if (
171
+ row.concurrencyCeiling !== undefined &&
172
+ row.concurrencyCeiling !== null &&
173
+ (typeof row.concurrencyCeiling !== "number" || !Number.isFinite(row.concurrencyCeiling))
174
+ ) {
175
+ return { field: "concurrencyCeiling", value: row.concurrencyCeiling }
176
+ }
177
+ for (const key of ["turn", "step", "policyRev"]) {
178
+ if (row[key] !== undefined && (!Number.isInteger(row[key]) || row[key] < 0)) {
179
+ return { field: key, value: row[key] }
180
+ }
181
+ }
182
+ if (row.applied !== undefined && typeof row.applied !== "boolean") {
183
+ return { field: "applied", value: row.applied }
184
+ }
185
+ // Run 36 DEC-36-05: a row read from disk may carry the removed scope gate.
186
+ const gateLoad = options.load === true
187
+ if (row.gate !== undefined && !(gateLoad ? LOAD_GATES : GATES).includes(row.gate)) {
188
+ return { field: "gate", value: row.gate }
189
+ }
190
+ if (row.scope !== undefined && !["localAndRemote", "localOnly", "remoteOnly"].includes(row.scope)) {
191
+ return { field: "scope", value: row.scope }
192
+ }
193
+ if (row.surface !== undefined && !SURFACES.includes(row.surface)) {
194
+ return { field: "surface", value: row.surface }
195
+ }
196
+ return undefined
197
+ }
198
+
199
+ const record = (row, options = {}) => {
200
+ const invalid = validateRow(row, options)
201
+ if (invalid !== undefined) return { invalid: true, ...invalid }
202
+ rows = [row, ...rows]
203
+ return { row }
204
+ }
205
+
206
+ const list = () => ({ rows: rows.slice() })
207
+
208
+ // Rows read from disk may carry the removed scope gate; new records may not.
209
+ const load = (row) => record(row, { load: true })
210
+
211
+ return { get, write, record, load, list, isEnabled, setEnabled }
212
+ }
213
+
214
+ const renderJson = (_args, value) => [{ type: "text", text: JSON.stringify(value, null, 2) }]
215
+
216
+ const badArgs = (value) => ({
217
+ ok: false,
218
+ invalid: true,
219
+ field: "args",
220
+ value,
221
+ message: "arguments must be an object"
222
+ })
223
+
224
+ export function createToolDefinitions(runtime, options = {}) {
225
+ const routing = options.routing
226
+
227
+ const router_policy = {
228
+ name: "router_policy",
229
+ description:
230
+ "Read or write the dsh-local-router routing policy (the single writable object). " +
231
+ 'action "get" returns the current policy. action "write" applies patch atomically guarded ' +
232
+ "by expectedRev (If-Match): if the policy rev no longer equals expectedRev the write is " +
233
+ "rejected and the current policy is returned so the caller can retry. patch may set " +
234
+ "floorTps (integer 0..100), mainModel ({provider, model, label} | null), advisoryModel (string|null). Unknown fields are rejected.",
235
+ parameters: {
236
+ type: "object",
237
+ properties: {
238
+ action: {
239
+ type: "string",
240
+ enum: ["get", "write"],
241
+ description: "get reads the current policy; write applies a patch guarded by expectedRev"
242
+ },
243
+ expectedRev: {
244
+ type: "integer",
245
+ description: "For write: the rev the patch is based on (If-Match). A mismatch rejects and returns the current policy."
246
+ },
247
+ patch: {
248
+ type: "object",
249
+ description: "For write: the policy fields to change (unknown fields are rejected).",
250
+ properties: {
251
+ floorTps: { type: "integer", minimum: 0, maximum: 100 },
252
+ mainModel: {
253
+ type: ["object", "null"],
254
+ properties: {
255
+ provider: { type: "string" },
256
+ model: { type: "string" },
257
+ label: { type: "string" }
258
+ },
259
+ required: ["provider", "model", "label"]
260
+ },
261
+ advisoryModel: { type: ["string", "null"] }
262
+ }
263
+ }
264
+ },
265
+ required: ["action"]
266
+ },
267
+ output: { schema: { type: "object" }, render: renderJson },
268
+ async execute(args) {
269
+ if (args == null || typeof args !== "object" || Array.isArray(args)) return badArgs(args)
270
+ if (args.action === "get") return { policy: runtime.get() }
271
+ if (args.action === "write") {
272
+ if (typeof args.expectedRev !== "number" || !Number.isInteger(args.expectedRev)) {
273
+ return {
274
+ ok: false,
275
+ invalid: true,
276
+ field: "expectedRev",
277
+ value: args.expectedRev,
278
+ message: "expectedRev is required and must be an integer for write"
279
+ }
280
+ }
281
+ const result = runtime.write(args.patch, args.expectedRev)
282
+ if (result.conflict === true) {
283
+ return {
284
+ ok: false,
285
+ conflict: true,
286
+ message: "policy revision changed; retry against the returned current policy",
287
+ current: result.current
288
+ }
289
+ }
290
+ if (result.invalid === true) {
291
+ return {
292
+ ok: false,
293
+ invalid: true,
294
+ field: result.field,
295
+ value: result.value,
296
+ message: 'invalid policy field "' + result.field + '"'
297
+ }
298
+ }
299
+ return { ok: true, policy: result.policy }
300
+ }
301
+ return {
302
+ ok: false,
303
+ invalid: true,
304
+ field: "action",
305
+ value: args.action,
306
+ message: 'action must be "get" or "write"'
307
+ }
308
+ }
309
+ }
310
+
311
+ const router_ledger = {
312
+ name: "router_ledger",
313
+ description:
314
+ "Record or list routing decisions in the dsh-local-router ledger. action \"record\" prepends " +
315
+ "one routing decision row (newest first); action \"list\" returns every recorded row newest first. " +
316
+ "A row requires id, requestId, omlxRequestId (string|null), agentId (string|null), " +
317
+ "decision (local|remote), model, reason, contextTokens, floorTps, latencyMs (numbers) and " +
318
+ "at (ISO timestamp string). Decision rows also carry gate (residency|headroom|floor|fallback), " +
319
+ "policyRev, turn, step, applied, surface, role (l1|l2|l3|advisor|unknown) and optional notAppliedReason/appliedBy/provider/selectedModel/limitation/roleReason strings. " +
320
+ "A legacy row read from disk may still carry a removed gate value; it loads and keeps its recorded gate.",
321
+ parameters: {
322
+ type: "object",
323
+ properties: {
324
+ action: {
325
+ type: "string",
326
+ enum: ["record", "list"],
327
+ description: "record prepends a routing decision; list returns all rows newest first"
328
+ },
329
+ row: {
330
+ type: "object",
331
+ description: "For record: the routing decision to store.",
332
+ properties: {
333
+ id: { type: "string" },
334
+ requestId: { type: "string" },
335
+ omlxRequestId: { type: ["string", "null"] },
336
+ agentId: { type: ["string", "null"] },
337
+ decision: { type: "string", enum: ["local", "remote"] },
338
+ model: { type: "string" },
339
+ reason: { type: "string" },
340
+ contextTokens: { type: "number" },
341
+ floorTps: { type: "number" },
342
+ latencyMs: { type: "number" },
343
+ at: { type: "string" },
344
+ gate: { type: "string", enum: GATES },
345
+ policyRev: { type: "integer", minimum: 0 },
346
+ turn: { type: "integer", minimum: 0 },
347
+ step: { type: "integer", minimum: 0 },
348
+ applied: { type: "boolean" },
349
+ surface: { type: "string", enum: SURFACES },
350
+ limitation: { type: "string" },
351
+ notAppliedReason: { type: "string" },
352
+ tpsSource: { type: "string", enum: TPS_SOURCES },
353
+ observedTps: { type: "number" },
354
+ concurrencyCeiling: { type: ["number", "null"] },
355
+ skips: { type: "array", items: { type: "object" } },
356
+ appliedBy: { type: "string" },
357
+ provider: { type: "string" },
358
+ selectedModel: { type: "string" },
359
+ role: { type: "string", enum: ROW_ROLES },
360
+ roleReason: { type: "string" }
361
+ },
362
+ required: ["id", "requestId", "omlxRequestId", "agentId", "decision", "model", "reason", "contextTokens", "floorTps", "latencyMs", "at"]
363
+ }
364
+ },
365
+ required: ["action"]
366
+ },
367
+ output: { schema: { type: "object" }, render: renderJson },
368
+ async execute(args) {
369
+ if (args == null || typeof args !== "object" || Array.isArray(args)) return badArgs(args)
370
+ if (args.action === "list") return runtime.list()
371
+ if (args.action === "record") {
372
+ const result = runtime.record(args.row)
373
+ if (result.invalid === true) {
374
+ return {
375
+ ok: false,
376
+ invalid: true,
377
+ field: result.field,
378
+ value: result.value,
379
+ message: 'invalid ledger row field "' + result.field + '"'
380
+ }
381
+ }
382
+ return { ok: true, row: result.row }
383
+ }
384
+ return {
385
+ ok: false,
386
+ invalid: true,
387
+ field: "action",
388
+ value: args.action,
389
+ message: 'action must be "record" or "list"'
390
+ }
391
+ }
392
+ }
393
+
394
+ const router_route = {
395
+ name: "router_route",
396
+ description:
397
+ "Compute the dsh-local-router decision for the current agent step and record exactly one ledger row. " +
398
+ "Reads the policy, the role's configured chain and oMLX telemetry, applies residency -> headroom -> floor -> fallback, and " +
399
+ "returns the decision, the written row, and any telemetry/policy/ledger error. " +
400
+ "This tool reports the decision only: DSH fixes the step's provider/model before agent/pre-step, so it " +
401
+ "cannot change the current step's model. It does not reroute.",
402
+ parameters: {
403
+ type: "object",
404
+ properties: {
405
+ requestId: {
406
+ type: "string",
407
+ description: "Optional join key for the decision row; defaults to the tool call id."
408
+ },
409
+ turn: { type: "integer", minimum: 0, description: "Optional turn number for the row." },
410
+ step: { type: "integer", minimum: 0, description: "Optional step number for the row." },
411
+ contextTokens: {
412
+ type: "number",
413
+ minimum: 0,
414
+ description: "Optional context size for the row; the host cannot tokenize, so 0 means not captured."
415
+ }
416
+ },
417
+ required: []
418
+ },
419
+ output: { schema: { type: "object" }, render: renderJson },
420
+ async execute(args, exec) {
421
+ if (args == null || typeof args !== "object" || Array.isArray(args)) return badArgs(args)
422
+ if (routing === undefined) {
423
+ return {
424
+ ok: false,
425
+ code: "ROUTING_UNAVAILABLE",
426
+ message: "router_route is registered without a routing controller"
427
+ }
428
+ }
429
+ if (args.requestId !== undefined && typeof args.requestId !== "string") {
430
+ return { ok: false, invalid: true, field: "requestId", value: args.requestId, message: "requestId must be a string" }
431
+ }
432
+ for (const key of ["turn", "step"]) {
433
+ if (args[key] !== undefined && (!Number.isInteger(args[key]) || args[key] < 0)) {
434
+ return { ok: false, invalid: true, field: key, value: args[key], message: key + " must be a non-negative integer" }
435
+ }
436
+ }
437
+ if (
438
+ args.contextTokens !== undefined &&
439
+ (typeof args.contextTokens !== "number" || !Number.isFinite(args.contextTokens) || args.contextTokens < 0)
440
+ ) {
441
+ return {
442
+ ok: false,
443
+ invalid: true,
444
+ field: "contextTokens",
445
+ value: args.contextTokens,
446
+ message: "contextTokens must be a non-negative number"
447
+ }
448
+ }
449
+
450
+ const agent = exec !== undefined && exec.agent !== undefined ? exec.agent : undefined
451
+ const agentId = agent?.id ?? null
452
+ const sessionId = agent?.session?.id ?? agent?.session?.header?.id ?? null
453
+ const requestId =
454
+ typeof args.requestId === "string" && args.requestId.length > 0
455
+ ? args.requestId
456
+ : exec?.callId !== undefined
457
+ ? "tool:" + exec.callId
458
+ : `${sessionId ?? agentId ?? "agent"}:${args.turn ?? "?"}:${args.step ?? "?"}`
459
+
460
+ try {
461
+ const result = await routing.route({
462
+ requestId,
463
+ agentId,
464
+ // F-36-V1 (DEC-36-12): the role is the caller's delegation depth. An
465
+ // unreadable depth is "unknown" on the decision and the row.
466
+ role: roleOf(agent),
467
+ scope: agent?.session?.header?.origin === "subagent" ? "subagent" : "main",
468
+ turn: args.turn,
469
+ step: args.step,
470
+ contextTokens: args.contextTokens,
471
+ surface: "router_route",
472
+ applied: false,
473
+ limitation: ROUTER_ROUTE_LIMITATION,
474
+ signal: exec?.signal
475
+ })
476
+ return { ...result, tool: "router_route" }
477
+ } catch (error) {
478
+ return {
479
+ ok: false,
480
+ code: "ROUTE_FAILED",
481
+ message: error instanceof Error ? error.message : String(error)
482
+ }
483
+ }
484
+ }
485
+ }
486
+
487
+ const router_routing = {
488
+ name: "router_routing",
489
+ description:
490
+ "Enable or disable dsh-local-router model routing. Routing defaults OFF: " +
491
+ "until it is enabled the router never selects a model and leaves the " +
492
+ "agent's existing model selection untouched. action \"enable\" turns routing on, " +
493
+ '"disable" turns it off, and "status" reports whether it is on.',
494
+ parameters: {
495
+ type: "object",
496
+ properties: {
497
+ action: {
498
+ type: "string",
499
+ enum: ["enable", "disable", "status"],
500
+ description: "enable turns routing on, disable turns it off, status reports the current state"
501
+ }
502
+ },
503
+ required: ["action"]
504
+ },
505
+ output: { schema: { type: "object" }, render: renderJson },
506
+ async execute(args) {
507
+ if (args == null || typeof args !== "object" || Array.isArray(args)) return badArgs(args)
508
+ if (args.action === "enable") {
509
+ runtime.setEnabled(true)
510
+ return { enabled: runtime.isEnabled() }
511
+ }
512
+ if (args.action === "disable") {
513
+ runtime.setEnabled(false)
514
+ return { enabled: runtime.isEnabled() }
515
+ }
516
+ if (args.action === "status") {
517
+ return { enabled: runtime.isEnabled() }
518
+ }
519
+ return {
520
+ ok: false,
521
+ invalid: true,
522
+ field: "action",
523
+ value: args.action,
524
+ message: 'action must be "enable", "disable" or "status"'
525
+ }
526
+ }
527
+ }
528
+
529
+ // R-37-02: discovery without schema injection. Returns the full tool-name
530
+ // inventory (the global registry view), so a restricted child can still list
531
+ // every tool the host knows without any of their schemas in its context.
532
+ const list_tools = {
533
+ name: "list_tools",
534
+ description:
535
+ "List every tool name this host knows (the full inventory, not just this agent's own restricted set). " +
536
+ "Returns names only - no tool schemas are injected - so a subagent can discover tools without paying for their schemas in context. " +
537
+ "Call tools with `await tools.<name>(args)` inside a run_code program.",
538
+ parameters: { type: "object", properties: {}, additionalProperties: false },
539
+ output: { schema: { type: "object" }, render: renderJson },
540
+ async execute(_args, execCtx) {
541
+ const names =
542
+ typeof options.listTools === "function" ? options.listTools(execCtx !== null && typeof execCtx === "object" ? execCtx.agent : undefined) : null
543
+ const list = names instanceof Set ? [...names] : Array.isArray(names) ? names : null
544
+ if (list === null || list.length === 0) {
545
+ return { ok: false, tools: [], message: "the tool inventory is not available to this host" }
546
+ }
547
+ return { ok: true, tools: [...list].sort() }
548
+ }
549
+ }
550
+
551
+ // F-37-39: router_routing is defined below but never registered by the host, so it
552
+ // is not returned - a dead definition must not look like a shipped tool.
553
+ return { router_policy, router_ledger, router_route, list_tools }
554
+ }