@troed/oc-ls-stats 1.6.1 → 1.6.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.
package/README.md CHANGED
@@ -81,6 +81,8 @@ Every 500ms, the plugin polls each discovered server, always carrying the select
81
81
 
82
82
  When a server returns an error (e.g. HTTP 502) or is unreachable, the plugin backs off with an exponentially increasing delay per server (1s, 2s, 4s, ...) capped at 10s, with a small random jitter to stagger multiple servers. Backoff resets as soon as the server responds successfully again.
83
83
 
84
+ A single failed poll is not treated as an outage: a routing layer in front of a single-slot backend briefly answers 503 (or holds `/slots` open) while the backend switches tasks or loads a model. Requests carry a 5s timeout, an in-flight poll is never started twice, and failures inside a 10s grace window keep the last reading on screen while retrying at the normal 500ms cadence. Only once the window outlives the grace does the readout switch to `n/a` and the backoff kick in.
85
+
84
86
  ### State Classification
85
87
 
86
88
  Each processing slot is classified as prefill (PP) or generation (TG) by comparing `n_decoded` and `n_prompt_tokens` against per-slot/per-task baselines. The plugin tracks two baseline maps keyed by slot ID:
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@troed/oc-ls-stats",
4
- "version": "1.6.1",
4
+ "version": "1.6.2",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "tui.tsx",
package/src/stats.ts CHANGED
@@ -2,6 +2,47 @@ export function backoffDelayMs(failures: number, baseMs: number = 1000, maxMs: n
2
2
  return Math.min(maxMs, baseMs * 2 ** (failures - 1))
3
3
  }
4
4
 
5
+ // --- Transient stats failures ------------------------------------------------
6
+ // A stats request should never hang the poll loop: a routing layer can hold a
7
+ // /slots request open while the backend switches tasks or loads a model.
8
+ // Conduct's own /health handler can spend ~4s probing a slow node, so the
9
+ // timeout must sit above that or the plugin gives up on answers it would get.
10
+ export const STATS_REQUEST_TIMEOUT_MS = 5_000
11
+
12
+ // A single failed poll is not necessarily an outage. A router in front of a
13
+ // single-slot backend briefly answers 503 (or hangs) while the backend switches
14
+ // tasks, so the readout keeps its last reading and retries at the normal poll
15
+ // cadence until this window expires.
16
+ export const STATS_STALE_GRACE_MS = 10_000
17
+
18
+ /**
19
+ * Tracks consecutive failed stats polls so a short router hiccup (a slot
20
+ * handover) does not blank the readout and grow the retry backoff. The window
21
+ * opens on the first failure; failures inside the grace are reported as stale
22
+ * (keep the last reading), and only a window that outlives the grace counts as
23
+ * an outage. A successful poll resets the window.
24
+ */
25
+ export class PollFailureWindow {
26
+ private startedAt: number | null = null
27
+ private count = 0
28
+
29
+ constructor(private readonly graceMs: number = STATS_STALE_GRACE_MS) {}
30
+
31
+ failure(now: number = Date.now()): { consecutive: number; expired: boolean } {
32
+ if (this.startedAt === null) {
33
+ this.startedAt = now
34
+ this.count = 0
35
+ }
36
+ this.count += 1
37
+ return { consecutive: this.count, expired: now - this.startedAt >= this.graceMs }
38
+ }
39
+
40
+ success(): void {
41
+ this.startedAt = null
42
+ this.count = 0
43
+ }
44
+ }
45
+
5
46
  export interface SlotState {
6
47
  id: number
7
48
  is_processing: boolean
package/tui.tsx CHANGED
@@ -13,6 +13,8 @@ import {
13
13
  statsUrl,
14
14
  serverKindKey,
15
15
  ServerKindCache,
16
+ PollFailureWindow,
17
+ STATS_REQUEST_TIMEOUT_MS,
16
18
  selectPollModel,
17
19
  type ServerType,
18
20
  } from "./src/stats.ts"
@@ -57,8 +59,9 @@ function estimateStreamTokens(delta: string) {
57
59
  }
58
60
 
59
61
  async function fetchSlots(baseUrl: string, model?: string): Promise<unknown | null> {
62
+ const url = statsUrl(baseUrl, "/slots", model)
60
63
  try {
61
- const resp = await fetch(statsUrl(baseUrl, "/slots", model))
64
+ const resp = await fetch(url, { signal: AbortSignal.timeout(STATS_REQUEST_TIMEOUT_MS) })
62
65
  if (!resp.ok) return null
63
66
  return await resp.json()
64
67
  } catch {
@@ -67,8 +70,9 @@ async function fetchSlots(baseUrl: string, model?: string): Promise<unknown | nu
67
70
  }
68
71
 
69
72
  async function fetchHealth(baseUrl: string, model?: string): Promise<unknown | null> {
73
+ const url = statsUrl(baseUrl, "/health", model)
70
74
  try {
71
- const resp = await fetch(statsUrl(baseUrl, "/health", model))
75
+ const resp = await fetch(url, { signal: AbortSignal.timeout(STATS_REQUEST_TIMEOUT_MS) })
72
76
  if (!resp.ok) return null
73
77
  return await resp.json()
74
78
  } catch {
@@ -82,8 +86,9 @@ async function fetchHealth(baseUrl: string, model?: string): Promise<unknown | n
82
86
  // the backend can forward the call; plain llama.cpp / Strata / Basalt servers
83
87
  // ignore the parameter.
84
88
  async function fetchLiveMetrics(baseUrl: string, model?: string): Promise<unknown | null> {
89
+ const url = statsUrl(baseUrl, "/metrics", model)
85
90
  try {
86
- const resp = await fetch(statsUrl(baseUrl, "/metrics", model))
91
+ const resp = await fetch(url, { signal: AbortSignal.timeout(STATS_REQUEST_TIMEOUT_MS) })
87
92
  if (!resp.ok) return null
88
93
  const body = await resp.json()
89
94
  if (body && typeof body === "object" && "live" in body) return (body as { live: unknown }).live
@@ -218,6 +223,7 @@ const tui: TuiPlugin = async (api, options) => {
218
223
  let llamaServerUrls: string[] = []
219
224
  let llamaServerModel: string | undefined
220
225
  const backoffByUrl = new Map<string, { failures: number; nextRetryAt: number }>()
226
+ const outage = new PollFailureWindow()
221
227
  const serverKindByUrl = new Map<string, ServerType>()
222
228
  const forcedServerType = serverTypeOverride((options as Record<string, unknown> | undefined)?.serverType)
223
229
 
@@ -281,9 +287,10 @@ const tui: TuiPlugin = async (api, options) => {
281
287
  if ((await resolveServerKind(baseUrl, model)) !== "llama") {
282
288
  const live = await fetchLiveMetrics(baseUrl, model)
283
289
  if (live == null) {
284
- noteServerFailure(baseUrl)
290
+ if (outage.failure().expired) noteServerFailure(baseUrl)
285
291
  continue
286
292
  }
293
+ outage.success()
287
294
  backoffByUrl.delete(baseUrl)
288
295
  tracker.failure = null
289
296
  mapLiveMetrics(live, tracker)
@@ -293,9 +300,13 @@ const tui: TuiPlugin = async (api, options) => {
293
300
 
294
301
  const slots = await fetchSlots(baseUrl, model)
295
302
  if (slots == null) {
296
- noteServerFailure(baseUrl)
303
+ // A handover on a single-slot backend briefly 503s or stalls /slots.
304
+ // Keep the last reading inside the grace and only back off once the
305
+ // outage outlives it.
306
+ if (outage.failure().expired) noteServerFailure(baseUrl)
297
307
  continue
298
308
  }
309
+ outage.success()
299
310
 
300
311
  backoffByUrl.delete(baseUrl)
301
312
  const slotList = Array.isArray(slots) ? slots : Object.values(slots)
@@ -372,8 +383,15 @@ const tui: TuiPlugin = async (api, options) => {
372
383
  pruneSamples()
373
384
  }, 1000)
374
385
 
386
+ let polling = false
375
387
  const slotTimer = setInterval(async () => {
376
- await pollMetrics()
388
+ if (polling) return
389
+ polling = true
390
+ try {
391
+ await pollMetrics()
392
+ } finally {
393
+ polling = false
394
+ }
377
395
  }, SLOT_POLL_MS)
378
396
 
379
397
  api.lifecycle.onDispose(() => {
@@ -508,6 +526,7 @@ const v2Setup = async (context: any) => {
508
526
  let llamaServerUrls: string[] = []
509
527
  let providerList: unknown[] = []
510
528
  const backoffByUrl = new Map<string, { failures: number; nextRetryAt: number }>()
529
+ const outage = new PollFailureWindow()
511
530
  const serverKindCache = new ServerKindCache()
512
531
  const forcedServerType = serverTypeOverride((context.options as Record<string, unknown> | undefined)?.serverType)
513
532
 
@@ -571,9 +590,10 @@ const v2Setup = async (context: any) => {
571
590
  // swap), and /health then reports the new one. Drop the cached kind
572
591
  // so the next poll re-detects instead of polling /metrics forever.
573
592
  serverKindCache.invalidate(baseUrl, model)
574
- noteServerFailure(baseUrl)
593
+ if (outage.failure().expired) noteServerFailure(baseUrl)
575
594
  continue
576
595
  }
596
+ outage.success()
577
597
  backoffByUrl.delete(baseUrl)
578
598
  tracker.failure = null
579
599
  mapLiveMetrics(live, tracker)
@@ -583,9 +603,13 @@ const v2Setup = async (context: any) => {
583
603
 
584
604
  const slots = await fetchSlots(baseUrl, model)
585
605
  if (slots == null) {
586
- noteServerFailure(baseUrl)
606
+ // A handover on a single-slot backend briefly 503s or stalls /slots.
607
+ // Keep the last reading inside the grace and only back off once the
608
+ // outage outlives it.
609
+ if (outage.failure().expired) noteServerFailure(baseUrl)
587
610
  continue
588
611
  }
612
+ outage.success()
589
613
 
590
614
  backoffByUrl.delete(baseUrl)
591
615
  const slotList = Array.isArray(slots) ? slots : Object.values(slots)
@@ -669,8 +693,15 @@ const v2Setup = async (context: any) => {
669
693
  pruneSamples()
670
694
  }, 1000)
671
695
 
672
- const slotTimer = setInterval(() => {
673
- void pollMetrics()
696
+ let polling = false
697
+ const slotTimer = setInterval(async () => {
698
+ if (polling) return
699
+ polling = true
700
+ try {
701
+ await pollMetrics()
702
+ } finally {
703
+ polling = false
704
+ }
674
705
  }, SLOT_POLL_MS)
675
706
 
676
707
  try {