@try-works/dsh-minimal-router 0.1.0 → 0.2.1

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/evidence.js CHANGED
@@ -129,11 +129,25 @@ export function createEvidenceAdapter(options = {}) {
129
129
  const getRemoteModels = typeof options.getRemoteModels === "function" ? options.getRemoteModels : null
130
130
  const getModelCapabilities =
131
131
  typeof options.getModelCapabilities === "function" ? options.getModelCapabilities : null
132
+ // Run 37 F-37-12: a request observation (engine id + prompt tokens) is recorded
133
+ // after the call returns, so a historical ledger row is joined at read time by
134
+ // its requestId. Absent observations simply leave the fields unknown.
135
+ const getObservations = typeof options.getObservations === "function" ? options.getObservations : null
136
+ // Run 37 DEC-37-09/14: the Overview's engine meter must show the FRESH speed the
137
+ // floor routes on, not the engine's lifetime average (which read 23.9 tps while
138
+ // the same model measured 230+ on real requests).
139
+ const getSpeed = typeof options.getSpeed === "function" ? options.getSpeed : null
140
+ // F-37-51: the recent-session rows the Overview's routing card lists (a cached
141
+ // read the host refreshes; the page never blocks on DSH session enumeration).
142
+ const readSessions = typeof options.readSessions === "function" ? options.readSessions : null
132
143
  // Run 30 (DEC-30-02): the host's build id is optional — an adapter without one
133
144
  // simply omits the field, so a pre-handshake host can never fail decodeHealth.
134
145
  const buildId = typeof options.buildId === "string" && options.buildId.length > 0 ? options.buildId : null
135
146
  const remoteModels = () =>
136
- normalizeRemoteModels(getRemoteModels !== null ? getRemoteModels() : options.remoteModels)
147
+ normalizeRemoteModels(getRemoteModels !== null ? getRemoteModels() : options.remoteModels).map((entry) => ({
148
+ ...entry,
149
+ roles: rolesForModel(entry.model)
150
+ }))
137
151
 
138
152
  const at = () => {
139
153
  const value = now()
@@ -249,6 +263,23 @@ export function createEvidenceAdapter(options = {}) {
249
263
  }
250
264
  }
251
265
 
266
+ /**
267
+ * F-37-48: which roles route to a model. Read from the settings document (the
268
+ * same one the Roles page writes), so the Models page can label a row by the
269
+ * roles it actually serves instead of a hardcoded literal.
270
+ */
271
+ const rolesForModel = (modelId) => {
272
+ if (typeof modelId !== "string" || modelId.length === 0) return []
273
+ const settings = readSettings()
274
+ const roles = settings !== null && isRecord(settings.roles) ? settings.roles : null
275
+ if (roles === null) return []
276
+ const out = []
277
+ for (const role of ["l1", "l2", "l3", "advisor"]) {
278
+ const entry = roles[role]
279
+ if (isRecord(entry) && entry.model === modelId && entry.enabled !== false) out.push(role)
280
+ }
281
+ return out
282
+ }
252
283
  /** The settings rev is the cache key the Overview projection follows. */
253
284
  const settingsRev = () => {
254
285
  const settings = readSettings()
@@ -325,8 +356,23 @@ export function createEvidenceAdapter(options = {}) {
325
356
  headroom: { used, atFloor, engineMax },
326
357
  // P2: the Overview's engine status Meter reads the same observed
327
358
  // throughput and memory headroom the Models/Routing evidence carries.
328
- engineTps: snapshot === null ? null : snapshot.observedTps,
329
- engineTpsSource: snapshot === null ? null : snapshot.observedTpsSource,
359
+ engineTps: (() => {
360
+ const fresh =
361
+ getSpeed !== null && snapshot !== null
362
+ ? getSpeed(typeof snapshot.preferredModel === "string" ? snapshot.preferredModel : null)
363
+ : null
364
+ if (fresh !== null && typeof fresh === "object" && Number.isFinite(fresh.tps)) return fresh.tps
365
+ return snapshot === null ? null : snapshot.observedTps
366
+ })(),
367
+ engineTpsSource: (() => {
368
+ const fresh =
369
+ getSpeed !== null && snapshot !== null
370
+ ? getSpeed(typeof snapshot.preferredModel === "string" ? snapshot.preferredModel : null)
371
+ : null
372
+ if (fresh !== null && typeof fresh === "object" && Number.isFinite(fresh.tps)) return "live"
373
+ if (snapshot === null) return null
374
+ return snapshot.observedTpsSource === "live" ? "live" : "historical"
375
+ })(),
330
376
  headroomRatio: snapshot === null ? null : snapshot.headroomRatio,
331
377
  ctxBucket,
332
378
  recent,
@@ -335,7 +381,9 @@ export function createEvidenceAdapter(options = {}) {
335
381
  // The four role chips read this document (roles.l1/l2/l3/advisor.model and
336
382
  // roles.l3.enabled); the local-engine line reads the adapter block below.
337
383
  settings: readSettings(),
338
- engine: engineProjection()
384
+ engine: engineProjection(),
385
+ // F-37-51: routing is per session - the recent sessions and their state.
386
+ sessions: readSessions !== null ? readSessions() : []
339
387
  }
340
388
  }
341
389
 
@@ -387,7 +435,15 @@ export function createEvidenceAdapter(options = {}) {
387
435
  withCapabilities(id, {
388
436
  id,
389
437
  preferred: id === snapshot.preferredModel,
390
- selected: id === selectedModel
438
+ selected: id === selectedModel,
439
+ roles: rolesForModel(id),
440
+ // F-37-48: the fresh probe/measured reading when the model is idle.
441
+ tps: (() => {
442
+ const live = snapshot.models.find((model) => model.id === id)?.tps
443
+ if (Number.isFinite(live)) return live
444
+ const fresh = getSpeed !== null ? getSpeed(id) : null
445
+ return fresh !== null && Number.isFinite(fresh.tps) ? fresh.tps : null
446
+ })()
391
447
  })
392
448
  ),
393
449
  remoteModels: remoteModels(),
@@ -434,7 +490,12 @@ export function createEvidenceAdapter(options = {}) {
434
490
  cacheEfficiency: snapshot.stats.cacheEfficiency,
435
491
  uptimeSeconds: snapshot.stats.uptimeSeconds,
436
492
  memoryUsedBytes: snapshot.activity.memoryUsedBytes,
437
- memoryMaxBytes: snapshot.activity.memoryMaxBytes
493
+ memoryMaxBytes: snapshot.activity.memoryMaxBytes,
494
+ // F-37-48: the engine's soft limit (the guard) and its scheduler
495
+ // ceiling, so the header can show real numbers instead of em dashes.
496
+ memorySoftBytes:
497
+ typeof snapshot.activity.memorySoftBytes === "number" ? snapshot.activity.memorySoftBytes : null,
498
+ engineMax: typeof snapshot.engineMax === "number" && Number.isFinite(snapshot.engineMax) ? snapshot.engineMax : null
438
499
  }
439
500
  }
440
501
  }
@@ -524,16 +585,46 @@ export function createEvidenceAdapter(options = {}) {
524
585
  }
525
586
  }
526
587
 
588
+ // Run 37 (F-37-12): the Activity row's local per-request detail is the joined
589
+ // observation, which index.js attaches to the ledger row once the engine
590
+ // completes the request. contextTokens is the observation's prompt tokens and
591
+ // omlxRequestId is its engine id. Both are surfaced exactly as recorded and
592
+ // never invented: an unattributed row stays 0 / null rather than a guess.
593
+ const observationFor = (row) => {
594
+ if (getObservations === null) return null
595
+ if (typeof row.requestId !== "string" || row.requestId.length === 0) return null
596
+ try {
597
+ const found = getObservations(row.requestId)
598
+ return isRecord(found) ? found : null
599
+ } catch {
600
+ return null
601
+ }
602
+ }
603
+ const activityRow = (row) => {
604
+ const observation = observationFor(row)
605
+ const promptTokens =
606
+ typeof row.contextTokens === "number" && Number.isFinite(row.contextTokens) && row.contextTokens > 0
607
+ ? row.contextTokens
608
+ : observation !== null && typeof observation.promptTokens === "number" && Number.isFinite(observation.promptTokens)
609
+ ? observation.promptTokens
610
+ : 0
611
+ const joined = typeof row.omlxRequestId === "string" && row.omlxRequestId.length > 0 ? row.omlxRequestId : null
612
+ const engineId =
613
+ joined ?? (observation !== null && typeof observation.engineRequestId === "string" && observation.engineRequestId.length > 0
614
+ ? observation.engineRequestId
615
+ : null)
616
+ return { ...row, contextTokens: promptTokens, omlxRequestId: engineId }
617
+ }
527
618
  const activityView = (model) => {
528
- const rows = model.ledger.rows
619
+ const rows = model.ledger.rows.map(activityRow)
529
620
  return {
530
621
  // S4 contract: the endpoint returns the full ledger, so there is no next page yet.
531
622
  nextCursor: null,
532
- rows: rows,
623
+ rows,
533
624
  joined: {
534
625
  total: rows.length,
535
- linked: rows.filter((row) => row.omlxRequestId !== null && row.omlxRequestId !== undefined).length,
536
- missing: rows.filter((row) => row.omlxRequestId === null || row.omlxRequestId === undefined).length
626
+ linked: rows.filter((row) => row.omlxRequestId !== null).length,
627
+ missing: rows.filter((row) => row.omlxRequestId === null).length
537
628
  },
538
629
  kpis: {
539
630
  total: rows.length,
package/lib/host.js CHANGED
@@ -537,8 +537,9 @@ export function createToolDefinitions(runtime, options = {}) {
537
537
  "Call tools with `await tools.<name>(args)` inside a run_code program.",
538
538
  parameters: { type: "object", properties: {}, additionalProperties: false },
539
539
  output: { schema: { type: "object" }, render: renderJson },
540
- async execute() {
541
- const names = typeof options.listTools === "function" ? options.listTools() : null
540
+ async execute(_args, execCtx) {
541
+ const names =
542
+ typeof options.listTools === "function" ? options.listTools(execCtx !== null && typeof execCtx === "object" ? execCtx.agent : undefined) : null
542
543
  const list = names instanceof Set ? [...names] : Array.isArray(names) ? names : null
543
544
  if (list === null || list.length === 0) {
544
545
  return { ok: false, tools: [], message: "the tool inventory is not available to this host" }
@@ -547,5 +548,7 @@ export function createToolDefinitions(runtime, options = {}) {
547
548
  }
548
549
  }
549
550
 
550
- return { router_policy, router_ledger, router_route, router_routing, list_tools }
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 }
551
554
  }