@@ -29,7 +29,7 @@ import { appendFileSync } from "node:fs"
2929
3030import { short } from "./format"
3131import type { HttpOptions } from "./http"
32- import { universalView , turnRate , turnSteps , lastModel , aggregateTurn , type Turn , type Display , DEFAULT_DISPLAY } from "./universal"
32+ import { universalView , turnRate , turnSteps , turnUserAt , 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"
@@ -311,7 +311,9 @@ export default Plugin.define({
311311 const turns = new Map < string , Turn > ( )
312312 // When each session's current execution started: the start of what the
313313 // user waits for, which the turn's total runs from (retries included).
314- const execStart = new Map < string , number > ( )
314+ // When each session's current reply started: the execution starting,
315+ // then each reply ending, since one execution can hold several replies.
316+ const replyStart = new Map < string , number > ( )
315317 // Per step: its provider and model (from session.step.started), and for
316318 // engines that report their latest request -- MTPLX, KoboldCpp, mlx-serve
317319 // -- a read taken at that step's end. A tool-using turn is one request
@@ -772,8 +774,19 @@ export default Plugin.define({
772774 // a turn in one tab suppress another tab's line (see panels.ts).
773775 const latest = new LatestPerKey ( )
774776
775- async function report ( sessionID : string ) : Promise < void > {
776- const seq = latest . begin ( sessionID )
777+ // Replies already reported, by their last step's id. A reply is reported
778+ // when its last step ends, and again asked for when the execution ends;
779+ // it must render once.
780+ const reported = new Set < string > ( )
781+ /**
782+ * One reply: the user message's steps, ending at `endID` (the step that
783+ * ended the reply) or at the latest step. `outcome` is set when the
784+ * execution was interrupted or failed before the reply finished.
785+ */
786+ async function report (
787+ sessionID : string ,
788+ opts : { endID ?: string ; outcome ?: "interrupted" | "failed" } = { }
789+ ) : Promise < void > {
777790 // `message.list()` is a union of message kinds and its tail after a turn
778791 // is an "idle" marker, not the reply — measured, see audit P1. Scan
779792 // backwards for the assistant message.
@@ -782,12 +795,30 @@ export default Plugin.define({
782795 // The turn is every assistant message since the last user message: one
783796 // per step when it calls tools. Reading only the last one showed
784797 // `140 tok 10.20s` for a 316-token, 37s turn (measured, vllm-mlx).
785- const steps = turnSteps ( msgs )
786- const agg = aggregateTurn ( steps , turns , { execStart : execStart . get ( sessionID ) } )
787- execStart . delete ( sessionID )
798+ const steps = turnSteps ( msgs , opts . endID )
799+ const lastID = steps [ steps . length - 1 ] ?. id
800+ if ( ! lastID || reported . has ( lastID ) ) return
801+ reported . add ( lastID )
802+ if ( reported . size > 64 ) {
803+ const oldest = reported . values ( ) . next ( ) . value
804+ if ( oldest !== undefined ) reported . delete ( oldest )
805+ }
806+ const seq = latest . begin ( sessionID )
807+ // The reply started when the execution did -- or, for a message queued
808+ // behind an earlier reply in the same execution, when that reply ended,
809+ // or when the message was sent if that is later.
810+ const userAt = turnUserAt ( msgs , opts . endID )
811+ const since = replyStart . get ( sessionID )
812+ const start = since !== undefined && userAt !== undefined ? Math . max ( since , userAt ) : ( since ?? userAt )
813+ const agg = aggregateTurn ( steps , turns , {
814+ execStart : start ,
815+ endAt : opts . outcome ? Date . now ( ) : undefined ,
816+ } )
817+ replyStart . set ( sessionID , Date . now ( ) )
788818 const info = agg . info
789819 const turn = agg . turn
790820 if ( ! info ) return
821+ dbg ( `report: ${ sessionID } ending ${ lastID } ${ opts . outcome ? ` (${ opts . outcome } )` : "" } ; ${ steps . length } step(s)` )
791822 dbg (
792823 `turn: ${ steps . length } assistant message(s) [${ steps
793824 . map ( ( m ) => `${ ( m . tokens ?. output ?? 0 ) + ( m . tokens ?. reasoning ?? 0 ) } ${ m . finish ? `/${ m . finish } ` : "" } ` )
@@ -860,7 +891,9 @@ export default Plugin.define({
860891 }
861892 let line : TurnView | null = null
862893 try {
863- line = await enrich ( provider , model , info , turn , http , tier2 , steps , sameEngine )
894+ // An unfinished reply's last step never completed, so the engine has
895+ // no reading of it to check against: OpenCode's figures only.
896+ if ( ! opts . outcome ) line = await enrich ( provider , model , info , turn , http , tier2 , steps , sameEngine )
864897 // Recorded before the fallback overwrites it, so history knows which
865898 // tier the figures actually came from.
866899 } catch ( e : unknown ) {
@@ -884,7 +917,15 @@ export default Plugin.define({
884917 // above are measured and complete; only their SOURCE changes once a
885918 // baseline exists, and the rate in particular can move an order of
886919 // magnitude when it does. Split to fit the box's 32 cells.
887- if ( tier2 . pendingBaseline ) line . notes . push ( "engine telemetry" , "from the next turn" )
920+ if ( opts . outcome ) {
921+ // OpenCode records no tokens for a step it stopped mid-stream
922+ // (measured: an interrupted reply's step came back 0/error after 7s
923+ // of thinking), so a 0 here is unknown, not none.
924+ const out0 = ( info . tokens ?. output ?? 0 ) + ( info . tokens ?. reasoning ?? 0 )
925+ if ( out0 === 0 ) line . rows = line . rows . filter ( ( [ label ] ) => label !== "tokens" && label !== "speed" )
926+ line . notes . push ( opts . outcome )
927+ }
928+ else if ( tier2 . pendingBaseline ) line . notes . push ( "engine telemetry" , "from the next turn" )
888929 else if ( tier2 . sharedWindow ) line . notes . push ( "engine data skipped:" , "overlapping requests" )
889930 }
890931
@@ -919,6 +960,7 @@ export default Plugin.define({
919960 steps : turn ?. steps ,
920961 engine : enriched ? tier2 . engine : undefined ,
921962 subagents,
963+ outcome : opts . outcome ,
922964 }
923965 setHistory ( ( d ) => {
924966 d . turns = record ( { turns : d . turns } , rec ) . turns
@@ -992,7 +1034,7 @@ export default Plugin.define({
9921034 ctx . data . on ( "session.execution.started" , ( evt ) => {
9931035 const sid = ( evt as { data ?: { sessionID ?: string } } ) . data ?. sessionID
9941036 if ( typeof sid !== "string" ) return
995- execStart . set ( sid , Date . now ( ) )
1037+ replyStart . set ( sid , Date . now ( ) )
9961038 // The engine this turn will use: the session's last model, or one
9971039 // just selected. A new session on the default model has neither,
9981040 // and its first turn goes unprimed, as before.
@@ -1131,6 +1173,49 @@ export default Plugin.define({
11311173 }
11321174 } )
11331175 )
1176+ // A reply ends when a step ends with a final answer. That is
1177+ // its own turn even if the execution goes on: a message sent while it
1178+ // ran is queued into the same execution, which then only ends -- or is
1179+ // interrupted -- after the next reply (measured: a 19m 53s reply never
1180+ // reported, because its execution ended interrupted 20m in).
1181+ off . push (
1182+ ctx . data . on ( "session.step.ended" , ( evt ) => {
1183+ const d = ( evt as { data ?: { sessionID ?: string ; assistantMessageID ?: string ; finish ?: string } } ) . data
1184+ if ( typeof d ?. sessionID !== "string" || typeof d . assistantMessageID !== "string" ) return
1185+ // Not "error" or "unknown": OpenCode retries a step under the same
1186+ // message id, and a failed attempt must not end the reply before
1187+ // the retry does. The execution's own end catches those.
1188+ if ( d . finish !== "stop" && d . finish !== "length" && d . finish !== "content-filter" ) return
1189+ const sid = d . sessionID
1190+ const id = d . assistantMessageID
1191+ // After the host has applied the step to the message it ends.
1192+ setTimeout ( ( ) => {
1193+ const m = ctx . data . session . message . get ( sid , id ) as SessionMessageAssistant | undefined
1194+ dbg ( `reply ended ${ id } (${ d . finish } ); completed ${ m ?. time ?. completed !== undefined } , tokens ${ m ?. tokens ?. output ?? "?" } ` )
1195+ report ( sid , { endID : id } ) . catch ( ( e : unknown ) => dbg ( `report threw: ${ String ( e ) } ` ) )
1196+ } , 50 )
1197+ } )
1198+ )
1199+ // An execution stopped before its reply finished still used the engine;
1200+ // the reply is shown, marked, rather than leaving the previous turn up.
1201+ off . push (
1202+ ctx . data . on ( "session.execution.interrupted" , ( evt ) => {
1203+ const sid = ( evt as { data ?: { sessionID ?: string } } ) . data ?. sessionID
1204+ if ( typeof sid === "string" ) {
1205+ dbg ( `event execution.interrupted ${ sid } ` )
1206+ report ( sid , { outcome : "interrupted" } ) . catch ( ( e : unknown ) => dbg ( `report threw: ${ String ( e ) } ` ) )
1207+ }
1208+ } )
1209+ )
1210+ off . push (
1211+ ctx . data . on ( "session.execution.failed" , ( evt ) => {
1212+ const sid = ( evt as { data ?: { sessionID ?: string } } ) . data ?. sessionID
1213+ if ( typeof sid === "string" ) {
1214+ dbg ( `event execution.failed ${ sid } ` )
1215+ report ( sid , { outcome : "failed" } ) . catch ( ( e : unknown ) => dbg ( `report threw: ${ String ( e ) } ` ) )
1216+ }
1217+ } )
1218+ )
11341219 } catch ( e : unknown ) {
11351220 dbg ( `subscribe failed: ${ String ( e ) } ` )
11361221 }
0 commit comments