11import { OpenCodeSettings , ProviderInstanceId , TextGenerationError } from "@t3tools/contracts" ;
22import * as NodeServices from "@effect/platform-node/NodeServices" ;
33import { it } from "@effect/vitest" ;
4+ import * as Cause from "effect/Cause" ;
45import * as Duration from "effect/Duration" ;
56import * as Effect from "effect/Effect" ;
7+ import * as Exit from "effect/Exit" ;
8+ import * as Fiber from "effect/Fiber" ;
69import * as Layer from "effect/Layer" ;
710import * as Schema from "effect/Schema" ;
811import * as TestClock from "effect/testing/TestClock" ;
@@ -23,6 +26,10 @@ const runtimeMock = {
2326 authHeaders : [ ] as Array < string | null > ,
2427 closeCalls : [ ] as string [ ] ,
2528 sessionCreateCalls : 0 ,
29+ abortCalls : [ ] as string [ ] ,
30+ onSessionCreate : undefined as ( ( signal ?: AbortSignal ) => Promise < never > ) | undefined ,
31+ onPrompt : undefined as ( ( signal ?: AbortSignal ) => Promise < never > ) | undefined ,
32+ onAbort : undefined as ( ( signal ?: AbortSignal ) => Promise < never > ) | undefined ,
2633 connectionError : undefined as Error | undefined ,
2734 sessionCreateError : undefined as unknown ,
2835 sessionResult : undefined as { data ?: { id : string } } | undefined ,
@@ -38,6 +45,10 @@ const runtimeMock = {
3845 this . state . authHeaders . length = 0 ;
3946 this . state . closeCalls . length = 0 ;
4047 this . state . sessionCreateCalls = 0 ;
48+ this . state . abortCalls . length = 0 ;
49+ this . state . onSessionCreate = undefined ;
50+ this . state . onPrompt = undefined ;
51+ this . state . onAbort = undefined ;
4152 this . state . connectionError = undefined ;
4253 this . state . sessionCreateError = undefined ;
4354 this . state . sessionResult = undefined ;
@@ -94,19 +105,34 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntime.OpenCodeRuntimeShape = {
94105 createOpenCodeSdkClient : ( { baseUrl, serverPassword } ) =>
95106 ( {
96107 session : {
97- create : async ( ) => {
108+ create : async ( _input : unknown , options ?: { readonly signal ?: AbortSignal } ) => {
98109 runtimeMock . state . sessionCreateCalls += 1 ;
110+ if ( runtimeMock . state . onSessionCreate ) {
111+ return runtimeMock . state . onSessionCreate ( options ?. signal ) ;
112+ }
99113 if ( runtimeMock . state . sessionCreateError !== undefined ) {
100114 throw runtimeMock . state . sessionCreateError ;
101115 }
102116 return runtimeMock . state . sessionResult ?? { data : { id : `${ baseUrl } /session` } } ;
103117 } ,
104- prompt : async ( input : { readonly parts : ReadonlyArray < unknown > } ) => {
118+ abort : async (
119+ input : { readonly sessionID : string } ,
120+ options ?: { readonly signal ?: AbortSignal } ,
121+ ) => {
122+ runtimeMock . state . abortCalls . push ( input . sessionID ) ;
123+ if ( runtimeMock . state . onAbort ) return runtimeMock . state . onAbort ( options ?. signal ) ;
124+ return { data : true } ;
125+ } ,
126+ prompt : async (
127+ input : { readonly parts : ReadonlyArray < unknown > } ,
128+ options ?: { readonly signal ?: AbortSignal } ,
129+ ) => {
105130 runtimeMock . state . promptUrls . push ( baseUrl ) ;
106131 runtimeMock . state . promptParts . push ( input . parts ) ;
107132 runtimeMock . state . authHeaders . push (
108133 serverPassword ? `Basic ${ btoa ( `opencode:${ serverPassword } ` ) } ` : null ,
109134 ) ;
135+ if ( runtimeMock . state . onPrompt ) return runtimeMock . state . onPrompt ( options ?. signal ) ;
110136 if ( runtimeMock . state . promptRequestError !== undefined ) {
111137 throw runtimeMock . state . promptRequestError ;
112138 }
@@ -235,6 +261,101 @@ const advanceIdleClock = Effect.gen(function* () {
235261} ) ;
236262
237263it . layer ( OpenCodeTextGenerationTestLayer ) ( "OpenCodeTextGeneration" , ( it ) => {
264+ it . effect . each ( [ "local" , "external" ] as const ) (
265+ "cancels an in-flight session creation on the %s server" ,
266+ ( server ) =>
267+ withOpenCodeTextGeneration (
268+ server === "local" ? DEFAULT_OPENCODE_SETTINGS : EXISTING_SERVER_OPENCODE_SETTINGS ,
269+ ( textGeneration ) =>
270+ Effect . gen ( function * ( ) {
271+ const started = Promise . withResolvers < AbortSignal | undefined > ( ) ;
272+ runtimeMock . state . onSessionCreate = ( signal ) => {
273+ started . resolve ( signal ) ;
274+ return new Promise < never > ( ( ) => { } ) ;
275+ } ;
276+ const fiber = yield * textGeneration
277+ . generateCommitMessage ( DEFAULT_COMMIT_MESSAGE_INPUT )
278+ . pipe ( Effect . forkChild ) ;
279+ const signal = yield * Effect . promise ( ( ) => started . promise ) ;
280+ yield * Fiber . interrupt ( fiber ) ;
281+
282+ expect ( signal ?. aborted ) . toBe ( true ) ;
283+ expect ( runtimeMock . state . promptUrls ) . toEqual ( [ ] ) ;
284+ expect ( runtimeMock . state . abortCalls ) . toEqual ( [ ] ) ;
285+ } ) ,
286+ ) ,
287+ ) ;
288+
289+ it . effect . each ( [ "local" , "external" ] as const ) (
290+ "cancels the prompt and stops only its session on the %s server" ,
291+ ( server ) =>
292+ withOpenCodeTextGeneration (
293+ server === "local" ? DEFAULT_OPENCODE_SETTINGS : EXISTING_SERVER_OPENCODE_SETTINGS ,
294+ ( textGeneration ) =>
295+ Effect . gen ( function * ( ) {
296+ const started = Promise . withResolvers < AbortSignal | undefined > ( ) ;
297+ runtimeMock . state . onPrompt = ( signal ) => {
298+ started . resolve ( signal ) ;
299+ return new Promise < never > ( ( ) => { } ) ;
300+ } ;
301+ const fiber = yield * textGeneration
302+ . generateCommitMessage ( DEFAULT_COMMIT_MESSAGE_INPUT )
303+ . pipe ( Effect . forkChild ) ;
304+ const signal = yield * Effect . promise ( ( ) => started . promise ) ;
305+ yield * Fiber . interrupt ( fiber ) ;
306+
307+ expect ( signal ?. aborted ) . toBe ( true ) ;
308+ expect ( runtimeMock . state . abortCalls ) . toEqual ( [
309+ `${ runtimeMock . state . promptUrls [ 0 ] } /session` ,
310+ ] ) ;
311+ expect ( runtimeMock . state . closeCalls ) . toEqual ( [ ] ) ;
312+
313+ runtimeMock . state . onPrompt = undefined ;
314+ const result = yield * textGeneration . generateCommitMessage (
315+ DEFAULT_COMMIT_MESSAGE_INPUT ,
316+ ) ;
317+ expect ( result . subject ) . toBe ( "Improve OpenCode reuse" ) ;
318+ expect ( runtimeMock . state . abortCalls ) . toHaveLength ( 1 ) ;
319+ } ) ,
320+ ) ,
321+ ) ;
322+
323+ it . effect . each ( [ "failure" , "timeout" ] as const ) (
324+ "preserves cancellation when the session abort ends in %s" ,
325+ ( outcome ) =>
326+ withOpenCodeTextGeneration ( EXISTING_SERVER_OPENCODE_SETTINGS , ( textGeneration ) =>
327+ Effect . gen ( function * ( ) {
328+ const promptStarted = Promise . withResolvers < void > ( ) ;
329+ const abortStarted = Promise . withResolvers < AbortSignal | undefined > ( ) ;
330+ runtimeMock . state . onPrompt = ( ) => {
331+ promptStarted . resolve ( ) ;
332+ return new Promise < never > ( ( ) => { } ) ;
333+ } ;
334+ runtimeMock . state . onAbort = ( signal ) => {
335+ abortStarted . resolve ( signal ) ;
336+ return outcome === "failure"
337+ ? Promise . reject ( new Error ( "Server disconnected" ) )
338+ : new Promise < never > ( ( ) => { } ) ;
339+ } ;
340+ const fiber = yield * textGeneration
341+ . generateCommitMessage ( DEFAULT_COMMIT_MESSAGE_INPUT )
342+ . pipe ( Effect . forkChild ) ;
343+ yield * Effect . promise ( ( ) => promptStarted . promise ) ;
344+ const cancellation = yield * Fiber . interrupt ( fiber ) . pipe ( Effect . forkChild ) ;
345+ const abortSignal = yield * Effect . promise ( ( ) => abortStarted . promise ) ;
346+ if ( outcome === "timeout" ) {
347+ yield * TestClock . adjust ( "10 seconds" ) ;
348+ }
349+ yield * Fiber . join ( cancellation ) ;
350+
351+ const exit = yield * Fiber . await ( fiber ) ;
352+ expect ( Exit . isFailure ( exit ) && Cause . hasInterruptsOnly ( exit . cause ) ) . toBe ( true ) ;
353+ expect ( runtimeMock . state . abortCalls ) . toEqual ( [ "http://127.0.0.1:9999/session" ] ) ;
354+ if ( outcome === "timeout" ) expect ( abortSignal ?. aborted ) . toBe ( true ) ;
355+ } ) ,
356+ ) ,
357+ ) ;
358+
238359 it . effect ( "excludes generic files from thread title generation" , ( ) =>
239360 withOpenCodeTextGeneration ( DEFAULT_OPENCODE_SETTINGS , ( textGeneration ) =>
240361 Effect . gen ( function * ( ) {
0 commit comments