Skip to content

Commit 31ccbef

Browse files
committed
Check llama.cpp, Splash and oMLX windows against the turn before showing them
1 parent 4767af3 commit 31ccbef

9 files changed

Lines changed: 212 additions & 24 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
count. KoboldCpp's `N generations this turn (last shown only)` no longer
1212
appears on turns read step by step, since nothing is dropped. Built from
1313
live captures; not yet run step by step against a live server.
14-
- The `mtplx`, `koboldcpp` and `mlxserve` totals are OpenCode's -- what you
15-
waited, retries named -- rather than the engine's duration for its latest
16-
request.
14+
- `llamacpp`, `llamafile`, `splash` and `omlx` check that a window is this
15+
turn's before showing it: its tokens must equal OpenCode's count for the
16+
turn and, for Splash and oMLX, its requests must equal the turn's steps.
17+
A window that fails shows the universal line with the overlapping-requests
18+
notice. Splash's `N requests this turn` no longer appears once the
19+
requests are accounted for. On a multi-step turn oMLX shows OpenCode's
20+
generation rate instead of the server's all-time average. Built from live
21+
captures; not yet run against a live server.
22+
- Every engine line's total is OpenCode's -- what you waited, retries named
23+
-- rather than an engine's own request or phase timings.
1724

1825
## [0.2.3] – 2026-09-23
1926
### Changed

‎README.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,13 @@ its token count equals OpenCode's for the turn. OpenCode's own background
416416
work (a new session's title, compaction), a turn you interrupted that kept
417417
generating, or another tab or client sharing the server all break that, and
418418
no engine here labels its counters by request or session to separate them
419-
again. So for the Prometheus engines, a turn that shared its window shows
420-
the universal line with `engine data skipped: overlapping requests` rather
421-
than figures that describe several requests at once.
419+
again. So a turn that shared its window shows the universal line with
420+
`engine data skipped: overlapping requests` rather than figures that
421+
describe several requests at once. This applies to every engine that
422+
differences counters: the Prometheus engines, `llamacpp`, `llamafile`,
423+
`splash` and `omlx`, checked against the turn's tokens and, where the
424+
engine counts requests, against its steps. Verified live on vllm-mlx; the
425+
others are built from their live captures.
422426

423427
`mtplx`, `koboldcpp` and `mlxserve` report only the engine's latest
424428
request, so they are read at the end of every step and the steps' receipts

‎adapters/llamacpp.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,27 @@ export function diffLlamaCppCounters(
109109
* prefill, where an engine stamps from the request reaching it. Same word,
110110
* different span, so it says which.
111111
*/
112+
/**
113+
* Whether a window is this turn's. llama.cpp has no request counter, so the
114+
* token count is the whole check: the window's generated tokens must equal
115+
* OpenCode's own count for the turn, summed over its steps. A title request,
116+
* another client, or an interrupted turn still generating all add tokens.
117+
* 0 or absent means OpenCode has no count, and the check is skipped.
118+
*/
119+
export function llamaCppIsThisTurn(t: LlamaCppTurn, hostTokens: number | undefined): boolean {
120+
return hostTokens === undefined || hostTokens <= 0 || t.completionTokens === hostTokens
121+
}
122+
123+
/**
124+
* `host.total` is the turn's total from OpenCode -- what the user waited,
125+
* retries included -- and wins over the engine's own timings.
126+
*/
112127
export function formatLlamaCppLine(
113128
t: LlamaCppTurn,
114129
label: string,
115130
model: string,
116-
hostTtft?: number
131+
hostTtft?: number,
132+
host: { total?: number; retries?: number } = {}
117133
): string {
118134
// Host-derived, and labelled as such. No derived figure on this line takes
119135
// its numerator from one source and its denominator from the other -- ttft
@@ -123,7 +139,9 @@ export function formatLlamaCppLine(
123139
`${label} ${short(model)}`,
124140
t.decodeTokS !== undefined ? `${nn(t.decodeTokS)} tok/s${ttftLabel}` : ttftLabel.trim(),
125141
t.prefillTokS !== undefined ? `prefill ${ni(t.prefillTokS)} tok/s` : "",
126-
`${ni(t.completionTokens)} tok ${nn(t.decodeS + t.prefillS, 2)}s`,
142+
`${ni(t.completionTokens)} tok ${nn(host.total ?? t.decodeS + t.prefillS, 2)}s${
143+
(host.retries ?? 0) > 0 ? ` (${host.retries} ${host.retries === 1 ? "retry" : "retries"})` : ""
144+
}`,
127145
]
128146
.filter(Boolean)
129147
.join("\n")

‎adapters/omlx.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,32 @@ export function recoverLatest(
8989
* prefill, where an engine stamps from the request reaching it. Same word,
9090
* different span, so it says which.
9191
*/
92+
/**
93+
* Whether a window is this turn's: as many requests as the turn had steps,
94+
* and the window's completion tokens equal to OpenCode's own count for the
95+
* turn. A host token count of 0 or absent skips the token check.
96+
*/
97+
export function omlxIsThisTurn(
98+
prev: OmlxSample,
99+
now: OmlxSample,
100+
host: { tokens?: number; steps?: number }
101+
): boolean {
102+
if (prev.model !== now.model) return false
103+
if (now.requests - prev.requests !== (host.steps ?? 1)) return false
104+
return host.tokens === undefined || host.tokens <= 0 || now.completion - prev.completion === host.tokens
105+
}
106+
107+
/**
108+
* `host.decodeTokS` is OpenCode's generation rate for the turn, used when
109+
* the running mean cannot recover this turn's own: over several requests it
110+
* yields only the server's all-time average, which is not this turn's speed.
111+
* `host.total` is the turn's total from OpenCode, retries included.
112+
*/
92113
export function formatOmlxLine(
93114
now: OmlxSample,
94115
prev: OmlxSample | undefined,
95-
hostTtft?: number
116+
hostTtft?: number,
117+
host: { decodeTokS?: number; total?: number; retries?: number } = {}
96118
): string {
97119
const header = `oMLX ${short(now.model ?? "")}`
98120
// Host-derived, and labelled as such. No derived figure on this line takes
@@ -123,9 +145,10 @@ export function formatOmlxLine(
123145
// from the window.
124146
const recoveredDecode = recoverLatest(prev.avgGen, prev.requests, now.avgGen, now.requests)
125147
const recoveredPrefill = recoverLatest(prev.avgPrefill, prev.requests, now.avgPrefill, now.requests)
126-
const decodeLabel = recoveredDecode === undefined ? " (avg)" : ""
148+
const useHostRate = recoveredDecode === undefined && host.decodeTokS !== undefined
149+
const decodeLabel = recoveredDecode === undefined && !useHostRate ? " (avg)" : ""
127150
const prefillLabel = recoveredPrefill === undefined ? " (avg)" : ""
128-
const decode = recoveredDecode ?? now.avgGen
151+
const decode = recoveredDecode ?? (useHostRate ? (host.decodeTokS as number) : now.avgGen)
129152
const prefill = recoveredPrefill ?? now.avgPrefill
130153

131154
const completion = now.completion - prev.completion
@@ -136,7 +159,11 @@ export function formatOmlxLine(
136159
header,
137160
`${nn(decode)} tok/s${decodeLabel}${ttftLabel}`,
138161
`prefill ${ni(prefill)} tok/s${prefillLabel}`,
139-
`${ni(completion)} tok (${ni(promptTokens)} prompt${cached > 0 ? `, ${ni(cached)} cached` : ""})`,
162+
`${ni(completion)} tok (${ni(promptTokens)} prompt${cached > 0 ? `, ${ni(cached)} cached` : ""})${
163+
host.total !== undefined ? ` ${nn(host.total, 2)}s` : ""
164+
}${
165+
(host.retries ?? 0) > 0 ? ` (${host.retries} ${host.retries === 1 ? "retry" : "retries"})` : ""
166+
}`,
140167
].join("\n")
141168
}
142169

‎adapters/splash.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,30 @@ export async function fetchSplashSample(
180180
* prefill, where an engine stamps from the request reaching it. Same word,
181181
* different span, so it says which.
182182
*/
183-
export function formatSplashLine(t: SplashTurn, model: string, hostTtft?: number): string {
183+
/**
184+
* Whether a window is this turn's: as many requests as the turn had steps
185+
* (one request per step), and the window's tokens equal to OpenCode's own
186+
* count for the turn. `splash opencode` routes OpenCode's title and
187+
* compaction requests to Splash, so a spare request is the expected way
188+
* this fails. A host token count of 0 or absent skips the token check.
189+
*/
190+
export function splashIsThisTurn(t: SplashTurn, host: { tokens?: number; steps?: number }): boolean {
191+
if (t.requests !== (host.steps ?? 1)) return false
192+
return host.tokens === undefined || host.tokens <= 0 || t.completionTokens === host.tokens
193+
}
194+
195+
/**
196+
* `host.total` is the turn's total from OpenCode -- what the user waited,
197+
* retries included -- and wins over the engine's phase times. `host.steps`
198+
* set means the window was checked against the turn's steps, so its
199+
* requests are all accounted for and need no "requests this turn" note.
200+
*/
201+
export function formatSplashLine(
202+
t: SplashTurn,
203+
model: string,
204+
hostTtft?: number,
205+
host: { total?: number; retries?: number; steps?: number } = {}
206+
): string {
184207
// Host-derived, and labelled as such. No derived figure on this line takes
185208
// its numerator from one source and its denominator from the other -- ttft
186209
// is measured directly, so nothing crosses the seam.
@@ -190,12 +213,14 @@ export function formatSplashLine(t: SplashTurn, model: string, hostTtft?: number
190213
`Splash ${short(model)}`,
191214
t.decodeTokS !== undefined ? `${nn(t.decodeTokS)} tok/s${ttftLabel}` : ttftLabel.trim(),
192215
t.prefillTokS !== undefined ? `prefill ${ni(t.prefillTokS)} tok/s` : "",
193-
`${ni(t.completionTokens)} tok ${nn(t.prefillS + t.decodeS, 2)}s`,
216+
`${ni(t.completionTokens)} tok ${nn(host.total ?? t.prefillS + t.decodeS, 2)}s${
217+
(host.retries ?? 0) > 0 ? ` (${host.retries} ${host.retries === 1 ? "retry" : "retries"})` : ""
218+
}`,
194219
`${ni(prompt)} prompt${t.cachedTokens > 0 ? `, ${ni(t.cachedTokens)} cached` : ""}`,
195220
t.draftAcceptRate !== undefined ? `draft ${ni(t.draftAcceptRate * 100)}% accepted` : "",
196221
// Only when a turn spanned several requests (tool round trips), so the
197222
// figures above read as sums rather than as one reply.
198-
t.requests > 1 ? `${ni(t.requests)} requests this turn` : "",
223+
t.requests > 1 && host.steps === undefined ? `${ni(t.requests)} requests this turn` : "",
199224
]
200225
.filter(Boolean)
201226
.join("\n")

‎test/llamacpp.test.mjs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
parseLlamaCppCounters,
1414
diffLlamaCppCounters,
1515
formatLlamaCppLine,
16+
llamaCppIsThisTurn,
1617
} from "../adapters/llamacpp.ts"
1718

1819
const dir = path.dirname(fileURLToPath(import.meta.url))
@@ -142,4 +143,30 @@ test("missing rates are omitted, never rendered as placeholders", () => {
142143
assert.equal(out.split("\n").length, 2)
143144
})
144145

146+
// ---- is the window this turn's? ---------------------------------------------
147+
// llama.cpp has no request counter, so the token count is the whole check:
148+
// the window's generated tokens must equal OpenCode's count for the turn,
149+
// summed over its steps. Anything else -- a title request, another client,
150+
// an interrupted turn still generating -- declines the engine line.
151+
test("a window whose tokens equal the turn's is this turn's", () => {
152+
const t = diffLlamaCppCounters(before, after)
153+
assert.equal(llamaCppIsThisTurn(t, USAGE.completion_tokens), true)
154+
})
155+
156+
test("a window holding more tokens than the turn made is declined", () => {
157+
const t = diffLlamaCppCounters(before, after)
158+
assert.equal(llamaCppIsThisTurn(t, 20), false)
159+
})
160+
161+
test("with no host count to compare, the check does not apply", () => {
162+
const t = diffLlamaCppCounters(before, after)
163+
assert.equal(llamaCppIsThisTurn(t, 0), true)
164+
})
165+
166+
test("the total is OpenCode's -- what you waited -- with retries named", () => {
167+
const t = diffLlamaCppCounters(before, after)
168+
const out = formatLlamaCppLine(t, "llama.cpp", "m", 0.3, { total: 7.0, retries: 1 })
169+
assert.ok(out.includes("32 tok 7.00s (1 retry)"), out)
170+
})
171+
145172
console.log(`\n${passed} passed`)

‎test/omlx.test.mjs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { strict as assert } from "node:assert"
2727
import { readFileSync } from "node:fs"
2828
import { fileURLToPath } from "node:url"
2929
import path from "node:path"
30-
import { recoverLatest, formatOmlxLine, toOmlxSample } from "../adapters/omlx.ts"
30+
import { recoverLatest, formatOmlxLine, toOmlxSample, omlxIsThisTurn } from "../adapters/omlx.ts"
3131

3232
const dir = path.dirname(fileURLToPath(import.meta.url))
3333
const fixture = (name) => JSON.parse(readFileSync(path.join(dir, "..", "fixtures", name), "utf8"))
@@ -154,4 +154,27 @@ test("the loaded model wins over the configured default", () => {
154154
assert.equal(toOmlxSample({ default_model: "b" }).model, "b")
155155
})
156156

157+
// ---- is the window this turn's? ---------------------------------------------
158+
// oMLX counts requests and tokens, so a window is this turn's when its
159+
// requests equal the turn's steps and its tokens equal the turn's.
160+
test("a one-request window with matching tokens is this turn's", () => {
161+
assert.equal(omlxIsThisTurn(before, afterOne, { tokens: 100, steps: 1 }), true)
162+
assert.equal(omlxIsThisTurn(before, afterOne, { tokens: 80, steps: 1 }), false)
163+
assert.equal(omlxIsThisTurn(before, afterOne, { tokens: 100, steps: 2 }), false)
164+
})
165+
166+
test("a two-step turn with two requests and matching tokens is this turn's", () => {
167+
// after-one -> after-two: two requests, 120 tokens, captured live.
168+
assert.equal(omlxIsThisTurn(afterOne, afterTwo, { tokens: 120, steps: 2 }), true)
169+
})
170+
171+
test("a multi-step turn uses OpenCode's generation rate, not the server's all-time average", () => {
172+
// The running mean can only recover one request's rate; over two it is
173+
// the server's lifetime average, which is not this turn's speed.
174+
const out = formatOmlxLine(afterTwo, afterOne, 0.5, { decodeTokS: 42.5, total: 12.0 })
175+
assert.ok(out.includes("42.5 tok/s"), out)
176+
assert.ok(!out.includes("(avg) ttft") && !/tok\/s \(avg\)/.test(out.split("\n")[1]), out)
177+
assert.ok(out.includes("12.00s"), out)
178+
})
179+
157180
console.log(`\n${passed} passed`)

‎test/splash.test.mjs‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { strict as assert } from "node:assert"
77
import { readFileSync } from "node:fs"
88
import { fileURLToPath } from "node:url"
99
import path from "node:path"
10-
import { parseSplashSample, diffSplashSamples, formatSplashLine } from "../adapters/splash.ts"
10+
import { parseSplashSample, diffSplashSamples, formatSplashLine, splashIsThisTurn } from "../adapters/splash.ts"
1111

1212
const dir = path.dirname(fileURLToPath(import.meta.url))
1313
const fixture = (name) => readFileSync(path.join(dir, "..", "fixtures", name), "utf8")
@@ -256,4 +256,37 @@ test("Splash: no host ttft means no ttft line, not an empty one", () => {
256256
assert.deepEqual(out.split("\n").length, formatSplashLine(t, "m").split("\n").length)
257257
})
258258

259+
// ---- is the window this turn's? ---------------------------------------------
260+
// Splash counts requests, so a window is this turn's when its requests equal
261+
// the turn's steps (one request per step) and its tokens equal the turn's.
262+
// `splash opencode` routes OpenCode's title and compaction requests to
263+
// Splash, so a spare request in the window is the expected failure.
264+
test("Splash: one request and matching tokens is this turn's", () => {
265+
const [b, a] = pair("splash")
266+
const t = diffSplashSamples(b, a)
267+
assert.equal(splashIsThisTurn(t, { tokens: USAGE.completion_tokens, steps: 1 }), true)
268+
})
269+
270+
test("Splash: tokens that don't match the turn's are declined", () => {
271+
const [b, a] = pair("splash")
272+
assert.equal(splashIsThisTurn(diffSplashSamples(b, a), { tokens: 150, steps: 1 }), false)
273+
})
274+
275+
test("Splash: more requests than steps means another request landed", () => {
276+
const [b, a] = pair("splash")
277+
const t = { ...diffSplashSamples(b, a), requests: 2 }
278+
assert.equal(splashIsThisTurn(t, { tokens: USAGE.completion_tokens, steps: 1 }), false)
279+
assert.equal(splashIsThisTurn(t, { tokens: USAGE.completion_tokens, steps: 2 }), true)
280+
})
281+
282+
test("Splash: a verified multi-step turn needs no 'requests this turn' note", () => {
283+
const [b, a] = pair("splash")
284+
const t = { ...diffSplashSamples(b, a), requests: 2 }
285+
const verified = formatSplashLine(t, "m", 0.4, { total: 9.0, steps: 2 })
286+
assert.ok(!verified.includes("requests this turn"), verified)
287+
assert.ok(verified.includes("200 tok 9.00s"), verified)
288+
// Unverified (no step count), the note still says the figures are sums.
289+
assert.ok(formatSplashLine(t, "m").includes("2 requests this turn"))
290+
})
291+
259292
console.log(`\n${passed} passed`)

‎tui.tsx‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ import { record, formatHistory, formatCollapsedLine, latestFor, type History, ty
3434
import { emptyPanels, lineFor, keyFor, setLine, LatestPerKey, type Panels } from "./panels"
3535

3636
import { fetchMtplxLatest, formatMtplxLine, combineMtplxSteps, type MtplxLatest } from "./adapters/mtplx"
37-
import { fetchOmlxSample, formatOmlxLine, type OmlxSample } from "./adapters/omlx"
37+
import { fetchOmlxSample, formatOmlxLine, omlxIsThisTurn, type OmlxSample } from "./adapters/omlx"
3838
import {
3939
fetchLlamaCppCounters,
4040
diffLlamaCppCounters,
4141
formatLlamaCppLine,
42+
llamaCppIsThisTurn,
4243
type LlamaCppCounters,
4344
} from "./adapters/llamacpp"
4445
import { fetchMlxServeRequests, mlxServeTurn, formatMlxServeLine, combineMlxServeSteps, type MlxServeRequest } from "./adapters/mlxserve"
4546
import {
4647
fetchSplashSample,
4748
diffSplashSamples,
4849
formatSplashLine,
50+
splashIsThisTurn,
4951
type SplashSample,
5052
} from "./adapters/splash"
5153
import { fetchKoboldPerf, koboldTurn, formatKoboldLine, combineKoboldSteps, type KoboldPerf } from "./adapters/koboldcpp"
@@ -376,8 +378,20 @@ export default Plugin.define({
376378
setBase((d) => {
377379
d.omlx = now
378380
})
379-
if (prev) match(now.completion - prev.completion, `; requests ${now.requests - prev.requests}`)
380-
return formatOmlxLine(now, prev, hostTtft)
381+
if (prev) {
382+
match(now.completion - prev.completion, `; requests ${now.requests - prev.requests}, steps ${steps.length}`)
383+
// A window that isn't this turn's -- a spare request, or tokens
384+
// that don't match -- is declined. The no-baseline render below
385+
// is labelled as the server's averages and needs no check.
386+
if (now.requests > prev.requests && !omlxIsThisTurn(prev, now, { tokens: hostTokens, steps: steps.length })) {
387+
tier2.sharedWindow = true
388+
return null
389+
}
390+
}
391+
return formatOmlxLine(now, prev, hostTtft, {
392+
...hostFigures,
393+
decodeTokS: turnRate(hostTokens, info, turn).decodeTokS,
394+
})
381395
}
382396

383397
// llamafile is llama.cpp-derived and publishes identical metric names,
@@ -398,8 +412,13 @@ export default Plugin.define({
398412
return null
399413
}
400414
const t = diffLlamaCppCounters(prev, now)
401-
if (t) match(t.completionTokens)
402-
return t ? formatLlamaCppLine(t, label, model, hostTtft) : null
415+
if (!t) return null
416+
match(t.completionTokens, `; steps ${steps.length}`)
417+
if (!llamaCppIsThisTurn(t, hostTokens)) {
418+
tier2.sharedWindow = true
419+
return null
420+
}
421+
return formatLlamaCppLine(t, label, model, hostTtft, hostFigures)
403422
}
404423

405424
case "splash": {
@@ -414,8 +433,13 @@ export default Plugin.define({
414433
return null
415434
}
416435
const t = diffSplashSamples(prev, now)
417-
if (t) match(t.completionTokens, `; requests ${t.requests}`)
418-
return t ? formatSplashLine(t, model, hostTtft) : null
436+
if (!t) return null
437+
match(t.completionTokens, `; requests ${t.requests}, steps ${steps.length}`)
438+
if (!splashIsThisTurn(t, { tokens: hostTokens, steps: steps.length })) {
439+
tier2.sharedWindow = true
440+
return null
441+
}
442+
return formatSplashLine(t, model, hostTtft, { ...hostFigures, steps: steps.length })
419443
}
420444

421445
case "koboldcpp":

0 commit comments

Comments
 (0)