Skip to content

Commit 6fe8557

Browse files
Merge pull request #2 from charlesnutter/baseline-priming
Prime engine baselines when a turn starts
2 parents 9ffa69f + 761d126 commit 6fe8557

4 files changed

Lines changed: 168 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## [Unreleased]
2+
### Changed
3+
- The first turn after OpenCode starts can show engine figures on
4+
`vllm`, `sglang`, `vllmmlx`, `aphrodite`, `lmdeploy`, `llamacpp`,
5+
`llamafile`, `splash` and `omlx`, instead of `engine telemetry from the
6+
next turn`. When a turn starts on one of these engines with no reading
7+
yet, that one engine is read once. Nothing is read at startup, and no
8+
other engine is read. A new session that has not used or selected a
9+
model yet is not primed, as before.
10+
- A turn shown with OpenCode's own figures is headed by the engine's name,
11+
as its engine figures are (`vllm-mlx`, not `vllmmlx`).
12+
113
## [0.3.0] – 2026-09-23
214
### Added
315
- A **Session** section below the per-turn figures, collapsed by default:

‎test/universal.test.mjs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// counts never disagreed — only the rate's numerator did.
99
// Run with: bun test/universal.test.mjs
1010
import { strict as assert } from "node:assert"
11-
import { turnRate, universalLine, universalView, turnSteps, aggregateTurn, DEFAULT_DISPLAY } from "../universal.ts"
11+
import { turnRate, universalLine, universalView, turnSteps, lastModel, aggregateTurn, DEFAULT_DISPLAY } from "../universal.ts"
1212

1313
let passed = 0
1414
function test(name, fn) {
@@ -364,4 +364,19 @@ test("a step with no first token contributes no waiting time and marks it incomp
364364
assert.equal(turn.waitMs, undefined)
365365
})
366366

367+
// ---- the model a turn is about to use (for priming a baseline) --------------
368+
test("the last model is the newest assistant's or model switch's", () => {
369+
const a = (providerID) => ({ type: "assistant", model: { providerID, id: "m" } })
370+
assert.deepEqual(lastModel([a("mtplx"), { type: "user" }]), { providerID: "mtplx", id: "m" })
371+
assert.deepEqual(
372+
lastModel([a("mtplx"), { type: "model-switched", model: { providerID: "vllmmlx", id: "q" } }, { type: "user" }]),
373+
{ providerID: "vllmmlx", id: "q" }
374+
)
375+
})
376+
377+
test("a session with no model named yet gives none, not a guess", () => {
378+
assert.equal(lastModel([{ type: "user" }]), undefined)
379+
assert.equal(lastModel([]), undefined)
380+
})
381+
367382
console.log(`\n${passed} passed`)

‎tui.tsx‎

Lines changed: 124 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { appendFileSync } from "node:fs"
2929

3030
import { short } from "./format"
3131
import type { HttpOptions } from "./http"
32-
import { universalView, turnRate, turnSteps, aggregateTurn, type Turn, type Display, DEFAULT_DISPLAY } from "./universal"
32+
import { universalView, turnRate, turnSteps, lastModel, aggregateTurn, type Turn, type Display, DEFAULT_DISPLAY } from "./universal"
3333
import { record, historyLines, type History, type TurnRecord } from "./history"
3434
import { emptyPanels, lineFor, keyFor, setLine, LatestPerKey, PLACEHOLDER, type Panels } from "./panels"
3535
import { encodeView, decodeView, LABEL_WIDTH, type TurnView } from "./rows"
@@ -322,6 +322,9 @@ export default Plugin.define({
322322
const stepProvider = new Map<string, string>()
323323
const stepModel = new Map<string, string>()
324324
const stepReads = new Map<string, Promise<unknown>>()
325+
// Per session: a model selected in it, for priming a session whose
326+
// messages don't name one yet.
327+
const selectedModel = new Map<string, { providerID: string; id: string }>()
325328
const bound = <V,>(m: Map<string, V>): void => {
326329
if (m.size > 64) {
327330
const oldest = m.keys().next().value
@@ -634,20 +637,114 @@ export default Plugin.define({
634637
return mlxServeView(t, hostFigures)
635638
}
636639

640+
default: {
641+
const p = promTarget(provider)
642+
return p ? prom(p.id, p.spec, p.url, p.label) : null // no adapter: Tier 1 handles it
643+
}
644+
}
645+
}
646+
647+
/** The Prometheus engines: baseline key, metric names, URL and label. */
648+
function promTarget(provider: string): { id: string; spec: PromSpec; url: string; label: string } | undefined {
649+
switch (provider) {
637650
case "vllm":
638-
return prom("vllm", VLLM_SPEC, cfg.vllmBase, "vLLM")
651+
return { id: "vllm", spec: VLLM_SPEC, url: cfg.vllmBase, label: "vLLM" }
639652
case "sglang":
640-
return prom("sglang", SGLANG_SPEC, cfg.sglangBase, "SGLang")
653+
return { id: "sglang", spec: SGLANG_SPEC, url: cfg.sglangBase, label: "SGLang" }
641654
case "vllmmlx":
642655
case "vllm-mlx":
643-
return prom("vllmmlx", VLLM_MLX_SPEC, cfg.vllmMlxBase, "vllm-mlx")
656+
return { id: "vllmmlx", spec: VLLM_MLX_SPEC, url: cfg.vllmMlxBase, label: "vllm-mlx" }
644657
case "aphrodite":
645-
return prom("aphrodite", APHRODITE_SPEC, cfg.aphroditeBase, "Aphrodite")
658+
return { id: "aphrodite", spec: APHRODITE_SPEC, url: cfg.aphroditeBase, label: "Aphrodite" }
646659
case "lmdeploy":
647-
return prom("lmdeploy", LMDEPLOY_SPEC, cfg.lmdeployBase, "LMDeploy")
648-
660+
return { id: "lmdeploy", spec: LMDEPLOY_SPEC, url: cfg.lmdeployBase, label: "LMDeploy" }
649661
default:
650-
return null // no adapter: Tier 1 handles it
662+
return undefined
663+
}
664+
}
665+
666+
/**
667+
* An engine's name as its own view heads it, so a turn that falls back to
668+
* OpenCode's figures keeps the same heading (`vllm-mlx`, not `vllmmlx`).
669+
* Providers with no adapter keep their id.
670+
*/
671+
function engineLabel(provider: string): string {
672+
const known: Record<string, string> = {
673+
mtplx: "MTPLX",
674+
omlx: "oMLX",
675+
llamacpp: "llama.cpp",
676+
llamafile: "llamafile",
677+
splash: "Splash",
678+
koboldcpp: "KoboldCpp",
679+
kobold: "KoboldCpp",
680+
mlxserve: "mlx-serve",
681+
"mlx-serve": "mlx-serve",
682+
}
683+
return promTarget(provider)?.label ?? known[provider] ?? provider
684+
}
685+
686+
// ---- baseline priming ---------------------------------------------------
687+
// A counter-difference engine is read at each turn's end, and that reading
688+
// is the next turn's baseline -- so the first turn after launch had none
689+
// and showed "engine telemetry from the next turn". Priming reads the one
690+
// engine a turn is about to use, when the turn starts, only if it has no
691+
// baseline yet: one localhost request per engine per run, never a sweep
692+
// of every configured engine. The engine counts nothing until prefill is
693+
// done, so the read should land first; if it lands late, the turn's token
694+
// check declines the window, which is no worse than having no baseline.
695+
const priming = new Set<string>()
696+
async function prime(provider: string): Promise<void> {
697+
if (priming.has(provider)) return
698+
const http: HttpOptions = { signal: life.signal }
699+
const t0 = Date.now()
700+
const done = (what: string): void => dbg(`prime ${provider}: ${what} after ${Date.now() - t0}ms`)
701+
priming.add(provider)
702+
try {
703+
const p = promTarget(provider)
704+
if (p) {
705+
if (base.prom[p.id]) return
706+
const now = await fetchPromSample(p.url, p.spec, http)
707+
if (!now) return done("no reading")
708+
// A turn's end may have set one meanwhile; that one is newer.
709+
setBase((d) => {
710+
if (!d.prom[p.id]) d.prom[p.id] = now
711+
})
712+
return done("baseline set")
713+
}
714+
switch (provider) {
715+
case "llamacpp":
716+
case "llamafile": {
717+
if (base.llamacpp[provider]) return
718+
const now = await fetchLlamaCppCounters(provider === "llamacpp" ? cfg.llamacppBase : cfg.llamafileBase, http)
719+
if (!now) return done("no reading")
720+
setBase((d) => {
721+
if (!d.llamacpp[provider]) d.llamacpp[provider] = now
722+
})
723+
return done("baseline set")
724+
}
725+
case "splash": {
726+
if (base.splash[cfg.splashBase]) return
727+
const now = await fetchSplashSample(cfg.splashBase, http)
728+
if (!now) return done("no reading")
729+
setBase((d) => {
730+
if (!d.splash[cfg.splashBase]) d.splash[cfg.splashBase] = now
731+
})
732+
return done("baseline set")
733+
}
734+
case "omlx": {
735+
if (base.omlx) return
736+
const now = await fetchOmlxSample(cfg.omlxBase, cfg.omlxKey, http)
737+
if (!now) return done("no reading")
738+
setBase((d) => {
739+
if (!d.omlx) d.omlx = now
740+
})
741+
return done("baseline set")
742+
}
743+
}
744+
} catch (e: unknown) {
745+
dbg(`prime ${provider} threw: ${String(e)}`)
746+
} finally {
747+
priming.delete(provider)
651748
}
652749
}
653750

@@ -719,7 +816,7 @@ export default Plugin.define({
719816
// A model or provider switch replaces this session's line rather than
720817
// blending two engines' figures into one reading. Per session, so a
721818
// different model in another tab is not a switch here.
722-
show(encodeView({ engine: provider, rows: [], notes: ["…"] }), sessionID, key)
819+
show(encodeView({ engine: engineLabel(provider), rows: [], notes: ["…"] }), sessionID, key)
723820
}
724821

725822
// One signal for every fetch this turn. Each request still gets its own
@@ -777,7 +874,7 @@ export default Plugin.define({
777874
const enriched = line !== null
778875
if (!line) {
779876
line = universalView(
780-
provider,
877+
engineLabel(provider),
781878
info,
782879
turn,
783880
cfg.display,
@@ -894,7 +991,23 @@ export default Plugin.define({
894991
off.push(
895992
ctx.data.on("session.execution.started", (evt) => {
896993
const sid = (evt as { data?: { sessionID?: string } }).data?.sessionID
897-
if (typeof sid === "string") execStart.set(sid, Date.now())
994+
if (typeof sid !== "string") return
995+
execStart.set(sid, Date.now())
996+
// The engine this turn will use: the session's last model, or one
997+
// just selected. A new session on the default model has neither,
998+
// and its first turn goes unprimed, as before.
999+
const m = selectedModel.get(sid) ?? lastModel(ctx.data.session.message.list(sid) ?? [])
1000+
dbg(`prime lookup ${sid}: ${m ? `${m.providerID}/${m.id}` : "no model known"}`)
1001+
if (m) void prime(m.providerID)
1002+
})
1003+
)
1004+
off.push(
1005+
ctx.data.on("session.model.selected", (evt) => {
1006+
const d = (evt as { data?: { sessionID?: string; model?: { providerID?: string; id?: string } } }).data
1007+
if (typeof d?.sessionID === "string" && d.model?.providerID && d.model.id) {
1008+
selectedModel.set(d.sessionID, { providerID: d.model.providerID, id: d.model.id })
1009+
bound(selectedModel)
1010+
}
8981011
})
8991012
)
9001013
// Read engines that keep only their latest request when a step's

‎universal.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ export function turnSteps(
124124
return steps
125125
}
126126

127+
/**
128+
* The model a session last used or selected, newest first: an assistant
129+
* message's model, or a `model-switched` entry. Undefined for a session with
130+
* neither, e.g. a new one on the default model.
131+
*/
132+
export function lastModel(
133+
msgs: readonly ({ type?: string; model?: { providerID?: string; id?: string } } | undefined)[]
134+
): { providerID: string; id: string } | undefined {
135+
for (let i = msgs.length - 1; i >= 0; i--) {
136+
const m = msgs[i]
137+
if (!m || (m.type !== "assistant" && m.type !== "model-switched")) continue
138+
if (m.model?.providerID && m.model.id) return { providerID: m.model.providerID, id: m.model.id }
139+
}
140+
return undefined
141+
}
142+
127143
/**
128144
* One turn's figures from its steps, shaped like a single message so the
129145
* universal line and `turnRate` need no second code path.

0 commit comments

Comments
 (0)