@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.
@@ -174,7 +174,17 @@ export const createRouteMapper = (options = {}) => {
174
174
  if (typeof options.localAvailable === "function" && options.localAvailable() === false) return null
175
175
  if (typeof localSelectionProvider !== "string" || localSelectionProvider.length === 0) return null
176
176
  if (typeof decision.model !== "string" || decision.model.length === 0) return null
177
- return { provider: localSelectionProvider, model: decision.model }
177
+ // DEC-37-11: a role with a finite context window is served through its own
178
+ // provider route, whose adapter resolveModel() carries that window - so the
179
+ // child's recorded window and compaction budget are the role's, at spawn.
180
+ const roleProvider =
181
+ typeof options.roleProviderFor === "function" && typeof decision.role === "string"
182
+ ? options.roleProviderFor(decision.role)
183
+ : null
184
+ return {
185
+ provider: typeof roleProvider === "string" && roleProvider.length > 0 ? roleProvider : localSelectionProvider,
186
+ model: decision.model
187
+ }
178
188
  }
179
189
  // A remote decision applies exactly the model's provider/model, carried on
180
190
  // the decision by decide(). There is no config pin, session preserve, or
@@ -689,6 +699,7 @@ export const createRouteApplication = (options = {}) => {
689
699
  const row = buildDecisionRow({
690
700
  id: newId(),
691
701
  requestId: pending.requestId ?? `${sessionId}:${turn}`,
702
+ sessionId,
692
703
  agentId: sessionId,
693
704
  decision: pending.decision,
694
705
  contextTokens: lastContextTokens.get(sessionId) ?? 0,
@@ -722,22 +733,25 @@ export const createRouteApplication = (options = {}) => {
722
733
  }
723
734
  }
724
735
 
725
- // R-37-03/DEC-37-05: cap the prepared call's context window to the role's
726
- // window, applied to whatever model the role uses. A null or absent window
727
- // (the Advisor's "no cap", or an unknown depth) injects nothing, so the
728
- // model's own window stands. The loop reads preparedCall.context.contextWindow
729
- // (dsh-agent-loop:1079) for the request/context record and compaction budget.
730
- const roles = typeof options.readRoles === "function" ? options.readRoles() : null
731
- const role = roleOf(agent)
732
- const roleWindow =
733
- roles !== null && typeof roles === "object" && !Array.isArray(roles) &&
734
- roles[role] !== null && typeof roles[role] === "object" && Number.isFinite(roles[role].contextWindow)
735
- ? Math.trunc(roles[role].contextWindow)
736
- : null
737
- const prepared = stepDecision !== null && typeof stepDecision === "object" && !Array.isArray(stepDecision) ? stepDecision : {}
738
- const context = { ...(prepared.context ?? {}) }
739
- if (roleWindow !== null) context.contextWindow = roleWindow
740
- return { ...prepared, context }
736
+ // Run 37 (F-37-04, PROVEN LIMITATION): the step decision is passed through
737
+ // UNCHANGED. DSH records the request contextWindow exclusively from the LLM
738
+ // adapter's model metadata, and no plugin surface can cap it below the
739
+ // model's window:
740
+ // - dsh-agent-loop buildRequest reads preparedCall.context.contextWindow
741
+ // (dsh-agent-loop/lib/index.js:1079) and appends the request/context
742
+ // record at :1088 - the only writer of that record.
743
+ // - preparedCall is llm.prepareCall's return (dsh-agent-loop:1018), NOT the
744
+ // pre-step waterfall result; the pre-step decision carries only
745
+ // kind/messages (dsh-agent-loop:706-721).
746
+ // - preparedCall.context comes from resolveCallWithInfo's info.context
747
+ // (dsh-llm/lib/index.js:2315) - the adapter's declared window. The
748
+ // agent/request config's context field is never read there.
749
+ // - compaction prices its budget from ctx.llm.resolveModelInfo
750
+ // (compaction-basic:955-959), the same adapter path.
751
+ // The role's contextWindow therefore stays an advisory Roles-page setting
752
+ // (null = no cap, DEC-37-05) and the Roles page carries the limitation note;
753
+ // injecting it anywhere would be a dead field, not a cap.
754
+ return stepDecision
741
755
  }
742
756
 
743
757
  const handleSessionDisposed = (session) => {
package/lib/routing.js CHANGED
@@ -287,11 +287,14 @@ const telemetrySpeed = (telemetry) => {
287
287
  return { tps, source: tpsSourceOf(telemetry, tps) }
288
288
  }
289
289
 
290
- // A fresh probe sample first; telemetry.observedTps only when none exists.
290
+ // A fresh probe/measured sample first; telemetry.observedTps only when none exists.
291
+ // `fresh` marks the store source, because a fresh per-request observation already
292
+ // reflects contention and must not be divided again by the session count.
291
293
  const resolveFloorSpeed = (model, telemetry, speedFor) => {
292
294
  const fresh = freshSpeedSample(speedFor, model)
293
- if (fresh !== null) return fresh
294
- return telemetrySpeed(telemetry)
295
+ if (fresh !== null) return { ...fresh, fresh: true }
296
+ const fallback = telemetrySpeed(telemetry)
297
+ return fallback === null ? null : { ...fallback, fresh: false }
295
298
  }
296
299
 
297
300
  /**
@@ -342,7 +345,12 @@ const localGateFailure = (candidate, telemetry, floorTps, speedFor, evidence, lo
342
345
  // a lone session keeps the measurement unchanged. The floor gate's evidence
343
346
  // is whichever sample it used: probe or telemetry.
344
347
  const active = loadFacts.activeLocalSessions
345
- const contended = loadFacts.poolTps !== null && active > 1
348
+ // DEC-37-14 (Owner): dividing the engine's pool figure is only honest when the
349
+ // only evidence is that aggregate. A fresh per-request observation was measured
350
+ // under the current load, so it IS the per-session speed (live: four concurrent
351
+ // requests each decoded at 46.5 tok/s while the engine lifetime average read
352
+ // 20.3 tps).
353
+ const contended = speed.fresh !== true && loadFacts.poolTps !== null && active > 1
346
354
  const effectiveTps = contended ? round1(loadFacts.poolTps / active) : speed.tps
347
355
  evidence.observedTps = effectiveTps
348
356
  evidence.tpsSource = speed.source
@@ -615,6 +623,9 @@ export const buildDecisionRow = (fields) => {
615
623
  const row = {
616
624
  id: fields.id,
617
625
  requestId: fields.requestId,
626
+ // F-37-49: the deciding session, so a ledger row can be attributed without
627
+ // parsing the requestId prefix.
628
+ sessionId: typeof fields.sessionId === "string" && fields.sessionId.length > 0 ? fields.sessionId : null,
618
629
  omlxRequestId: fields.omlxRequestId ?? null,
619
630
  agentId: fields.agentId ?? null,
620
631
  decision: decision.target,
package/lib/telemetry.js CHANGED
@@ -252,7 +252,13 @@ const buildSnapshot = (source, fetchedAt, stats, activity, models, device, setti
252
252
  activeRequests: active.total_active_requests,
253
253
  waitingRequests: active.total_waiting_requests,
254
254
  memoryUsedBytes,
255
- memoryMaxBytes
255
+ memoryMaxBytes,
256
+ // F-37-48: the engine's own soft limit is the memory guard the page shows
257
+ // (memory_pressure.soft_bytes); absent stays null, never invented.
258
+ memorySoftBytes:
259
+ typeof active.memory_pressure?.soft_bytes === "number" && active.memory_pressure.soft_bytes > 0
260
+ ? active.memory_pressure.soft_bytes
261
+ : null
256
262
  },
257
263
  device: {
258
264
  chipName: device.chip_name,
@@ -409,11 +415,20 @@ export const createOmlxTelemetry = (options = {}) => {
409
415
  // request path and never against a remote model. Routing reads sampleFor(),
410
416
  // which is synchronous and never waits on the network.
411
417
 
412
- export const SPEED_SAMPLE_TTL_MS = 15 * 60 * 1000
413
- export const BELOW_FLOOR_REPROBE_MS = 10 * 60 * 1000
418
+ // DEC-37-09 (Owner): the probe reading is the router's floor evidence and the
419
+ // engine's own average is a lagging, outdated indicator. A probe result therefore
420
+ // stays valid much longer (30 min) and the sweep re-probes on a short cycle, so a
421
+ // fresh probe reading is essentially always present at decision time.
422
+ export const SPEED_SAMPLE_TTL_MS = 30 * 60 * 1000
423
+ export const BELOW_FLOOR_REPROBE_MS = 2 * 60 * 1000
414
424
  export const SPEED_SOURCES = ["measured", "probe", "unknown"]
415
- export const PROBE_MAX_TOKENS = 8
416
- export const PROBE_PROMPT = "Reply with the single word pong."
425
+ // DEC-37-13: an 8-token completion is too short to measure decode speed - the
426
+ // engine's own usage for the same model reports 189.9 tps over 64 tokens while an
427
+ // 8-token probe window can read 9-19 tps, because token startup and scheduling
428
+ // dominate a few-token window. The probe therefore asks for a run of tokens and
429
+ // prefers the engine's own generation_tokens_per_second.
430
+ export const PROBE_MAX_TOKENS = 64
431
+ export const PROBE_PROMPT = "Count from 1 to 40, separated by commas, digits only."
417
432
  export const DEFAULT_PROBE_TIMEOUT_MS = 20_000
418
433
 
419
434
  const roundTps = (value) => Math.round(value * 10) / 10
@@ -683,6 +698,12 @@ const readSseStream = async (stream, now) => {
683
698
  if (typeof usageTokens === "number" && Number.isFinite(usageTokens) && usageTokens > 0) {
684
699
  state.usageTokens = Math.trunc(usageTokens)
685
700
  }
701
+ // The engine reports the decode-only speed of this very request; prefer it over
702
+ // our own delta window, which is sensitive to a short completion.
703
+ const reportedTps = payload?.usage?.generation_tokens_per_second
704
+ if (typeof reportedTps === "number" && Number.isFinite(reportedTps) && reportedTps > 0) {
705
+ state.reportedTps = reportedTps
706
+ }
686
707
  }
687
708
  for await (const chunk of stream) {
688
709
  const atMs = now().getTime()
@@ -713,7 +734,7 @@ export const createModelProbe = (options = {}) => {
713
734
  const request = options.request ?? probeFetch
714
735
  const now = options.now ?? (() => new Date())
715
736
  const timeoutMs = normalizeInteger(options.timeoutMs, DEFAULT_PROBE_TIMEOUT_MS, 1, 120_000)
716
- const maxTokens = normalizeInteger(options.maxTokens, PROBE_MAX_TOKENS, 1, 16)
737
+ const maxTokens = normalizeInteger(options.maxTokens, PROBE_MAX_TOKENS, 1, 128)
717
738
  const prompt =
718
739
  typeof options.prompt === "string" && options.prompt.length > 0 ? options.prompt : PROBE_PROMPT
719
740
  const headers = { Accept: "application/json", "Content-Type": "application/json" }
@@ -786,6 +807,11 @@ export const createModelProbe = (options = {}) => {
786
807
  } else if (response !== null && typeof response?.text === "function") {
787
808
  text = await Promise.race([Promise.resolve(response.text()), deadline])
788
809
  }
810
+ if (sse !== null && typeof sse.reportedTps === "number" && Number.isFinite(sse.reportedTps) && sse.reportedTps > 0) {
811
+ // DEC-37-13: the engine's own per-request decode speed wins.
812
+ const tokens = sse.usageTokens !== null ? sse.usageTokens : estimateTokens(sse.content)
813
+ return makeSample(tokens, Math.max(1, Math.round((tokens / sse.reportedTps) * 1000)))
814
+ }
789
815
  if (sse !== null && sse.firstDeltaAt !== null && sse.finishAt !== null) {
790
816
  // completion_tokens comes from the stream's usage when the engine sends it;
791
817
  // otherwise the accumulated content is estimated as before.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@try-works/dsh-minimal-router",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "DSH local router plugin: per-session auto-routing between a local engine and remote models, evidence pages, and a plugin-owned engine provider.",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",