@@ -29,7 +29,7 @@ import { appendFileSync } from "node:fs"
2929
3030import { short } from "./format"
3131import 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"
3333import { record , historyLines , type History , type TurnRecord } from "./history"
3434import { emptyPanels , lineFor , keyFor , setLine , LatestPerKey , PLACEHOLDER , type Panels } from "./panels"
3535import { 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
0 commit comments