@@ -87,3 +87,63 @@ cliIt.live(
8787 } ) ,
8888 60_000 ,
8989)
90+
91+ cliIt . live (
92+ "model preference survives promoted input and process restart without replay" ,
93+ ( { opencode, home, llm } ) =>
94+ Effect . gen ( function * ( ) {
95+ const client = yield * HttpClient . HttpClient
96+ const env = {
97+ OPENCODE_DB : `${ home } /model-preference-restart.sqlite` ,
98+ OPENCODE_TEST_MANAGED_CONFIG_DIR : `${ home } /managed` ,
99+ OPENCODE_CONFIG : "" ,
100+ OPENCODE_CONFIG_DIR : `${ home } /.config/opencode` ,
101+ OPENCODE_PERMISSION : "{}" ,
102+ OPENCODE_EXPERIMENTAL : "false" ,
103+ OPENCODE_EXPERIMENTAL_EVENT_SYSTEM : "true" ,
104+ }
105+ const first = yield * opencode . serve ( { env, hostname : "127.0.0.1" , port : yield * freePort } )
106+ const created = yield * client . post ( `${ first . url } /session` , {
107+ body : HttpBody . jsonUnsafe ( { title : "Preference restart" , agent : "build" , model : { id : "test-model" , providerID : "test" } } ) ,
108+ } )
109+ expect ( created . status ) . toBe ( 200 )
110+ const session = yield * created . json . pipe ( Effect . flatMap ( Schema . decodeUnknownEffect ( SessionV1 . SessionInfo ) ) )
111+ const path = `/session/${ session . id } `
112+ const preference = yield * client . patch ( `${ first . url } ${ path } ` , {
113+ body : HttpBody . jsonUnsafe ( { model : { providerID : "test" , modelID : "test-model" } } ) ,
114+ } )
115+ expect ( preference . status ) . toBe ( 200 )
116+ expect ( yield * preference . json ) . toMatchObject ( { preferredModel : { id : "test-model" } } )
117+ yield * llm . text ( "synthetic turn" )
118+ const payload = { requestID : "model-preference-restart" , delivery : "queue" , text : "synthetic input" }
119+ expect ( ( yield * client . post ( `${ first . url } ${ path } /input` , { body : HttpBody . jsonUnsafe ( payload ) } ) ) . status ) . toBe ( 200 )
120+ yield * llm . wait ( 1 )
121+ const receipt = yield * pollWithTimeout (
122+ Effect . gen ( function * ( ) {
123+ const response = yield * client . get ( `${ first . url } ${ path } /input/${ payload . requestID } ` )
124+ const row = yield * response . json . pipe ( Effect . flatMap ( Schema . decodeUnknownEffect ( SessionV1 . InputReceipt ) ) )
125+ return row . state === "promoted" ? row : undefined
126+ } ) ,
127+ "input did not promote" ,
128+ "15 seconds" ,
129+ )
130+ yield * pollWithTimeout (
131+ Effect . gen ( function * ( ) {
132+ const response = yield * client . get ( `${ first . url } ${ path } /message` )
133+ const messages = yield * response . json . pipe ( Effect . flatMap ( Schema . decodeUnknownEffect ( Schema . Array ( SessionV1 . WithParts ) ) ) )
134+ return messages . some ( ( item ) => item . info . role === "assistant" && item . info . finish === "stop" ) ? true : undefined
135+ } ) ,
136+ "input turn did not finish" ,
137+ "15 seconds" ,
138+ )
139+ expect ( yield * ( yield * client . get ( `${ first . url } ${ path } ` ) ) . json ) . toMatchObject ( { preferredModel : { id : "test-model" } } )
140+ first . kill ( )
141+ yield * Effect . promise ( ( ) => first . exited )
142+ const second = yield * opencode . serve ( { env, hostname : "127.0.0.1" , port : yield * freePort } )
143+ expect ( yield * ( yield * client . get ( `${ second . url } ${ path } ` ) ) . json ) . toMatchObject ( { preferredModel : { id : "test-model" } } )
144+ expect ( yield * ( yield * client . get ( `${ second . url } ${ path } /input/${ payload . requestID } ` ) ) . json ) . toEqual ( receipt )
145+ expect ( yield * ( yield * client . post ( `${ second . url } ${ path } /input` , { body : HttpBody . jsonUnsafe ( payload ) } ) ) . json ) . toEqual ( receipt )
146+ expect ( yield * llm . inputs ) . toHaveLength ( 1 )
147+ } ) ,
148+ 60_000 ,
149+ )
0 commit comments