@@ -76,7 +76,7 @@ import { SessionSystemPrompt } from "@opencode/core/session/system-prompt"
7676import { ID , Model } from "@opencode/core/model"
7777import { Location } from "@opencode/core/location"
7878import { Provider } from "@opencode/core/provider"
79- import { Cause , Context , Deferred , Effect , Exit , Fiber , Layer , Queue , Schema , Scope , Stream } from "effect"
79+ import { Cause , Context , DateTime , Deferred , Effect , Exit , Fiber , Layer , Queue , Schema , Scope , Stream } from "effect"
8080import { TestClock } from "effect/testing"
8181import { asc , desc , eq , sql } from "drizzle-orm"
8282import { testEffect } from "./lib/effect"
@@ -3391,11 +3391,17 @@ describe("SessionRunnerLLM", () => {
33913391
33923392 scenario ( "consumes the full provider stream before recording its boundary and settling local tools" , function * ( s ) {
33933393 yield * s . admit ( "Echo this" )
3394+ const request = yield * s . llm . gate
33943395 const tail = yield * Deferred . make < void > ( )
33953396 const complete = yield * Deferred . make < void > ( )
33963397 const finished = yield * Deferred . make < void > ( )
33973398 yield * s . llm . push (
3398- Stream . fromIterable ( TestLLM . tool ( "call-streamed" , "echo" , { text : "hello" } ) ) . pipe (
3399+ Stream . fromIterable (
3400+ TestLLM . complete (
3401+ { reason : { normalized : "tool-calls" } , usage : { outputTokens : 100 , reasoningTokens : 80 } } ,
3402+ LLMEvent . toolCall ( { id : "call-streamed" , name : "echo" , input : { text : "hello" } } ) ,
3403+ ) ,
3404+ ) . pipe (
33993405 Stream . concat (
34003406 Stream . fromEffect ( Deferred . succeed ( tail , undefined ) . pipe ( Effect . andThen ( Deferred . await ( complete ) ) ) ) . pipe (
34013407 Stream . drain ,
@@ -3413,25 +3419,39 @@ describe("SessionRunnerLLM", () => {
34133419 )
34143420 const run = yield * Effect . forkChild ( s . resume )
34153421
3422+ yield * request . started
3423+ yield * TestClock . adjust ( "2 seconds" )
3424+ yield * request . release
34163425 yield * tools . started
34173426 yield * Deferred . await ( tail )
34183427 expect ( s . requests ) . toHaveLength ( 1 )
34193428 expect ( yield * recordedEventTypes ( sessionID ) ) . not . toContain ( "session.step.streamed.1" )
34203429 expect ( requireAssistant ( yield * s . context ) . time . completed ) . toBeUndefined ( )
3430+ yield * TestClock . adjust ( "3 seconds" )
34213431 yield * Deferred . succeed ( complete , undefined )
34223432 yield * Fiber . join ( streamed )
34233433 expect ( yield * Deferred . isDone ( finished ) ) . toBe ( true )
34243434 const assistant = requireAssistant ( yield * s . context )
34253435 expect ( assistant . time . streamed ) . toBeDefined ( )
3436+ expect ( DateTime . toEpochMillis ( assistant . time . streamed ! ) - DateTime . toEpochMillis ( assistant . time . created ) ) . toBe (
3437+ 5_000 ,
3438+ )
34263439 expect ( assistant . time . completed ) . toBeUndefined ( )
34273440 expect ( assistant . content ) . toMatchObject ( [ { type : "tool" , state : { status : "running" } } ] )
34283441
3442+ yield * TestClock . adjust ( "10 seconds" )
34293443 yield * tools . release
34303444 yield * Fiber . join ( run )
34313445 const events = yield * recordedEventTypes ( sessionID )
34323446 expect ( events . indexOf ( "session.step.streamed.1" ) ) . toBeLessThan ( events . indexOf ( "session.tool.success.2" ) )
34333447 expect ( events . indexOf ( "session.tool.success.2" ) ) . toBeLessThan ( events . indexOf ( "session.step.ended.1" ) )
34343448 expect ( events . filter ( ( type ) => type === "session.step.streamed.1" ) ) . toHaveLength ( 2 )
3449+ yield * replaySessionProjection ( sessionID )
3450+ const replayed = ( yield * s . context ) . find ( ( message ) => message . id === assistant . id )
3451+ expect ( replayed ) . toMatchObject ( {
3452+ time : { created : assistant . time . created , streamed : assistant . time . streamed } ,
3453+ tokens : { output : 20 , reasoning : 80 } ,
3454+ } )
34353455 } )
34363456
34373457 scenario ( "restores durable reasoning provider metadata in the next request" , function * ( s ) {
@@ -5037,14 +5057,18 @@ describe("SessionRunnerLLM", () => {
50375057 scenario ( `bounds jittered exponential backoff before output for ${ failure . name } ` , function * ( s ) {
50385058 yield * s . admit ( "Retry transport" )
50395059 yield * s . llm . push ( TestLLM . failAfter ( failure ( ) , LLMEvent . stepStart ( { index : 0 } ) ) )
5040- yield * s . llm . push ( TestLLM . text ( "Recovered" , "retry-success" ) )
5060+ yield * s . llm . push (
5061+ Stream . fromEffect ( Effect . sleep ( 400 ) ) . pipe (
5062+ Stream . flatMap ( ( ) => Stream . fromIterable ( TestLLM . text ( "Recovered" , "retry-success" ) ) ) ,
5063+ ) ,
5064+ )
50415065
50425066 const scheduled = yield * subscribeRetries ( s )
50435067 const run = yield * s . resume . pipe ( Effect . forkChild )
50445068 yield * Queue . take ( scheduled )
50455069 yield * TestClock . adjust ( "1599 millis" )
50465070 expect ( s . requests ) . toHaveLength ( 1 )
5047- yield * TestClock . adjust ( "801 millis" )
5071+ yield * TestClock . adjust ( "1201 millis" )
50485072 yield * Fiber . join ( run )
50495073
50505074 expect ( s . requests ) . toHaveLength ( 2 )
@@ -5058,6 +5082,10 @@ describe("SessionRunnerLLM", () => {
50585082 ] )
50595083 yield * replaySessionProjection ( sessionID )
50605084 expect ( ( yield * s . context ) . filter ( ( message ) => message . type === "assistant" ) ) . toHaveLength ( 1 )
5085+ const assistant = requireAssistant ( yield * s . context )
5086+ expect ( DateTime . toEpochMillis ( assistant . time . streamed ! ) - DateTime . toEpochMillis ( assistant . time . created ) ) . toBe (
5087+ 400 ,
5088+ )
50615089 } )
50625090 }
50635091
0 commit comments