@@ -7805,6 +7805,108 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
78057805 } ) ,
78067806 ) ;
78077807
7808+ it . effect ( "keeps emitting usage snapshots after a snapshot emission fails" , ( ) =>
7809+ Effect . gen ( function * ( ) {
7810+ const threadId = asThreadId ( "thread-opencode-token-usage-worker-survives-failure" ) ;
7811+ runtimeMock . state . configProviders = {
7812+ providers : [
7813+ {
7814+ id : "opencode" ,
7815+ models : {
7816+ "gpt-5.4" : { limit : { context : 200_000 , output : 16_384 } } ,
7817+ } ,
7818+ } ,
7819+ ] ,
7820+ } ;
7821+ runtimeMock . state . subscribedEvents = [
7822+ {
7823+ type : "message.updated" ,
7824+ properties : {
7825+ sessionID : "http://127.0.0.1:9999/session" ,
7826+ info : {
7827+ id : "msg-worker-survives-a" ,
7828+ role : "assistant" ,
7829+ providerID : "opencode" ,
7830+ modelID : "gpt-5.4" ,
7831+ tokens : {
7832+ input : 1000 ,
7833+ output : 100 ,
7834+ reasoning : 10 ,
7835+ cache : { read : 200 , write : 50 } ,
7836+ } ,
7837+ } ,
7838+ } ,
7839+ } ,
7840+ {
7841+ type : "message.updated" ,
7842+ properties : {
7843+ sessionID : "http://127.0.0.1:9999/session" ,
7844+ info : {
7845+ id : "msg-worker-survives-b" ,
7846+ role : "assistant" ,
7847+ providerID : "opencode" ,
7848+ modelID : "gpt-5.4" ,
7849+ tokens : {
7850+ input : 2000 ,
7851+ output : 100 ,
7852+ reasoning : 10 ,
7853+ cache : { read : 200 , write : 50 } ,
7854+ } ,
7855+ } ,
7856+ } ,
7857+ } ,
7858+ ] ;
7859+
7860+ // Fail the third event-id allocation: the first two belong to
7861+ // session.started/thread.started, so the first usage snapshot's
7862+ // buildEventBase dies while the second snapshot must still flow.
7863+ let uuidCalls = 0 ;
7864+ const nodeCrypto = yield * Crypto . Crypto ;
7865+ const gatedCrypto = {
7866+ ...nodeCrypto ,
7867+ randomUUIDv4 : Effect . suspend ( ( ) => {
7868+ uuidCalls += 1 ;
7869+ if ( uuidCalls === 3 ) {
7870+ return Effect . die ( new Error ( "worker-survival-test injected uuid failure" ) ) ;
7871+ }
7872+ return nodeCrypto . randomUUIDv4 ;
7873+ } ) ,
7874+ } satisfies Crypto . Crypto ;
7875+ const adapter = yield * makeOpenCodeAdapter ( openCodeAdapterTestSettings ) . pipe (
7876+ Effect . provideService ( Crypto . Crypto , gatedCrypto ) ,
7877+ ) ;
7878+
7879+ const eventsFiber = yield * adapter . streamEvents . pipe (
7880+ Stream . filter ( ( event ) => event . threadId === threadId ) ,
7881+ Stream . take ( 3 ) ,
7882+ Stream . runCollect ,
7883+ Effect . forkChild ,
7884+ ) ;
7885+
7886+ yield * adapter . startSession ( {
7887+ provider : ProviderDriverKind . make ( "opencode" ) ,
7888+ threadId,
7889+ runtimeMode : "full-access" ,
7890+ } ) ;
7891+
7892+ // Without per-iteration recovery the worker dies on the first failure
7893+ // and the second snapshot never arrives, so take(3) never completes.
7894+ const events = Array . from ( yield * Fiber . join ( eventsFiber ) . pipe ( Effect . timeout ( "1 second" ) ) ) ;
7895+ NodeAssert . deepEqual (
7896+ events . map ( ( event ) => event . type ) ,
7897+ [ "session.started" , "thread.started" , "thread.token-usage.updated" ] ,
7898+ ) ;
7899+ const usageEvent = events [ 2 ] ;
7900+ NodeAssert . ok ( usageEvent ) ;
7901+ if ( usageEvent . type === "thread.token-usage.updated" ) {
7902+ NodeAssert . equal ( usageEvent . payload . usage . usedTokens , 2360 ) ;
7903+ NodeAssert . equal ( usageEvent . payload . usage . maxTokens , 200_000 ) ;
7904+ } else {
7905+ NodeAssert . fail ( `expected thread.token-usage.updated, got ${ usageEvent . type } ` ) ;
7906+ }
7907+ } ) ,
7908+ ) ;
7909+
78087910 it . effect ( "skips metadata probes for zero-token assistant broadcasts" , ( ) =>
78097911 Effect . gen ( function * ( ) {
78107912 const adapter = yield * OpenCodeAdapter ;
0 commit comments