@@ -5,11 +5,12 @@ import * as NodePath from "node:path";
55
66import * as NodeServices from "@effect/platform-node/NodeServices" ;
77import { assert , it } from "@effect/vitest" ;
8- import { ProviderInstanceId } from "@t3tools/contracts" ;
8+ import { DEFAULT_SERVER_SETTINGS , ProviderInstanceId } from "@t3tools/contracts" ;
99import type { OpenCodeSettings } from "../settings.ts" ;
1010import * as HostProcess from "@t3tools/shared/HostProcess" ;
1111import * as Deferred from "effect/Deferred" ;
1212import * as Effect from "effect/Effect" ;
13+ import * as Duration from "effect/Duration" ;
1314import * as Fiber from "effect/Fiber" ;
1415import * as Layer from "effect/Layer" ;
1516import * as Logger from "effect/Logger" ;
@@ -264,6 +265,193 @@ it.layer(layer)("OpenCodeDriver runtime selection", (it) => {
264265 assert . strictEqual ( requested . length , readsAfterRejection ) ;
265266 } ) . pipe ( Effect . scoped , Effect . provide ( TestClock . layer ( ) ) ) ,
266267 ) ;
268+
269+ it . effect . each ( [
270+ { name : "recovers" , enabled : true , demand : true , intervalOff : false , unsupported : false } ,
271+ {
272+ name : "skips without demand" ,
273+ enabled : true ,
274+ demand : false ,
275+ intervalOff : false ,
276+ unsupported : false ,
277+ } ,
278+ {
279+ name : "respects a disabled interval" ,
280+ enabled : true ,
281+ demand : true ,
282+ intervalOff : true ,
283+ unsupported : false ,
284+ } ,
285+ {
286+ name : "skips disabled instances" ,
287+ enabled : false ,
288+ demand : true ,
289+ intervalOff : false ,
290+ unsupported : false ,
291+ } ,
292+ {
293+ name : "skips unsupported usage" ,
294+ enabled : true ,
295+ demand : true ,
296+ intervalOff : false ,
297+ unsupported : true ,
298+ } ,
299+ ] ) ( "Go usage recovery $name without restarting a ready catalog" , ( scenario ) =>
300+ Effect . gen ( function * ( ) {
301+ const usageStarted = yield * Deferred . make < void > ( ) ;
302+ let usageReads = 0 ;
303+ let serverStarts = 0 ;
304+ let modelReads = 0 ;
305+ const server = HttpClient . make ( ( request ) => {
306+ if ( request . url === "https://opencode.ai/zen/go/v1/usage" ) {
307+ usageReads += 1 ;
308+ assert . strictEqual ( request . headers . authorization , "Bearer test-go-key" ) ;
309+ if ( scenario . unsupported )
310+ return Effect . succeed (
311+ HttpClientResponse . fromWeb ( request , new Response ( null , { status : 403 } ) ) ,
312+ ) ;
313+ if ( usageReads === 1 )
314+ return Deferred . succeed ( usageStarted , undefined ) . pipe ( Effect . andThen ( Effect . never ) ) ;
315+ const window = { percent : 10 , resetsAt : "2026-11-01T00:00:00.000Z" } ;
316+ return Effect . succeed (
317+ HttpClientResponse . fromWeb (
318+ request ,
319+ Response . json ( { usage : { rolling : window , weekly : window , monthly : window } } ) ,
320+ ) ,
321+ ) ;
322+ }
323+ const path = new URL ( request . url ) . pathname ;
324+ if ( path === "/api/model" ) modelReads += 1 ;
325+ const body =
326+ path === "/api/info"
327+ ? JSON . parse ( OPENCODE_2_RESPONSES [ "/api/info" ] . body )
328+ : {
329+ location : { directory : "/work" } ,
330+ data :
331+ path === "/api/model"
332+ ? [
333+ {
334+ id : "big-pickle" ,
335+ modelID : "big-pickle" ,
336+ providerID : "opencode" ,
337+ name : "Big Pickle" ,
338+ package : "@opencode/ai/providers/openai-compatible" ,
339+ settings : { } ,
340+ compatibility : { } ,
341+ capabilities : { tools : true , input : [ "text" ] , output : [ "text" ] } ,
342+ variants : [ ] ,
343+ time : { released : 1789516800000 } ,
344+ cost : [ ] ,
345+ status : "active" ,
346+ enabled : true ,
347+ limit : { context : 1000000 , output : 65536 } ,
348+ } ,
349+ ]
350+ : [ ] ,
351+ } ;
352+ return Effect . succeed ( HttpClientResponse . fromWeb ( request , Response . json ( body ) ) ) ;
353+ } ) ;
354+ const runtime = {
355+ ...openCode2Runtime ,
356+ startOpenCodeServerProcess : ( {
357+ verify,
358+ } : Parameters <
359+ OpenCodeRuntime . OpenCodeRuntime [ "Service" ] [ "startOpenCodeServerProcess" ]
360+ > [ 0 ] ) =>
361+ Effect . gen ( function * ( ) {
362+ serverStarts += 1 ;
363+ const url = "http://127.0.0.1:4096" ;
364+ const version = verify ? yield * verify ( url ) : "2.0.18" ;
365+ return { url, version, isRunning : Effect . succeed ( true ) , exitCode : Effect . never } ;
366+ } ) ,
367+ } ;
368+ const instance = yield * OpenCodeDriver . create ( {
369+ instanceId : ProviderInstanceId . make ( "opencode-usage-recovery" ) ,
370+ displayName : undefined ,
371+ environment : [
372+ {
373+ name : "OPENCODE_AUTH_CONTENT" ,
374+ sensitive : true ,
375+ value : JSON . stringify ( {
376+ "opencode-go" : { type : "api" , key : "test-go-key" } ,
377+ } ) ,
378+ } ,
379+ ] ,
380+ enabled : scenario . enabled ,
381+ config : OpenCodeDriver . defaultConfig ( ) ,
382+ } ) . pipe (
383+ Effect . provide (
384+ TestProviderHost . layer ( {
385+ runBackgroundWork : scenario . demand ,
386+ settings : scenario . intervalOff
387+ ? {
388+ ...DEFAULT_SERVER_SETTINGS ,
389+ backgroundActivity : {
390+ ...DEFAULT_SERVER_SETTINGS . backgroundActivity ,
391+ profile : "custom" ,
392+ overrides : { providerHealthRefreshInterval : Duration . zero } ,
393+ } ,
394+ }
395+ : DEFAULT_SERVER_SETTINGS ,
396+ } ) ,
397+ ) ,
398+ Effect . provideService ( HttpClient . HttpClient , server ) ,
399+ Effect . provideService ( OpenCodeRuntime . OpenCodeRuntime , runtime ) ,
400+ ) ;
401+ const initial = scenario . enabled
402+ ? yield * instance . snapshot . streamChanges . pipe (
403+ Stream . filter ( ( snapshot ) => snapshot . usageLimits !== undefined ) ,
404+ Stream . take ( 1 ) ,
405+ Stream . runCollect ,
406+ Effect . forkChild ,
407+ )
408+ : undefined ;
409+ if ( scenario . enabled && ! scenario . unsupported ) yield * Deferred . await ( usageStarted ) ;
410+ yield * TestClock . adjust ( "5 seconds" ) ;
411+ const failed = initial
412+ ? Array . from ( yield * Fiber . join ( initial ) ) [ 0 ]
413+ : yield * instance . snapshot . getSnapshot ;
414+ if ( scenario . enabled ) {
415+ assert . strictEqual ( failed ?. status , "ready" ) ;
416+ assert . strictEqual ( failed ?. auth . status , "authenticated" ) ;
417+ }
418+ const initialReason =
419+ scenario . enabled && ! scenario . unsupported ? "probeFailed" : "unsupported" ;
420+ assert . strictEqual ( failed ?. usageLimits ?. unavailable ?. reason , initialReason ) ;
421+ const catalogReads = modelReads ;
422+ const starts = serverStarts ;
423+
424+ const shouldRecover =
425+ scenario . enabled && scenario . demand && ! scenario . intervalOff && ! scenario . unsupported ;
426+ const recovery = shouldRecover
427+ ? yield * instance . snapshot . streamChanges . pipe (
428+ Stream . filter ( ( snapshot ) => snapshot . usageLimits ?. windows . length === 3 ) ,
429+ Stream . take ( 1 ) ,
430+ Stream . runCollect ,
431+ Effect . forkChild ,
432+ )
433+ : undefined ;
434+ yield * TestClock . adjust ( "5 minutes" ) ;
435+ const recovered = yield * instance . snapshot . getSnapshot ;
436+ const expectedReads = ! scenario . enabled ? 0 : shouldRecover ? 2 : 1 ;
437+ assert . strictEqual ( usageReads , expectedReads ) ;
438+ if ( recovery ) assert . deepStrictEqual ( Array . from ( yield * Fiber . join ( recovery ) ) , [ recovered ] ) ;
439+ assert . strictEqual (
440+ recovered . usageLimits ?. unavailable ?. reason ,
441+ shouldRecover ? undefined : initialReason ,
442+ ) ;
443+ assert . deepStrictEqual (
444+ recovered . usageLimits ?. windows . map ( ( window ) => window . kind ) ,
445+ shouldRecover ? [ "session" , "weekly" , "monthly" ] : [ ] ,
446+ ) ;
447+ assert . strictEqual ( modelReads , catalogReads ) ;
448+ assert . strictEqual ( serverStarts , starts ) ;
449+ yield * TestClock . adjust ( "15 minutes" ) ;
450+ assert . strictEqual ( usageReads , expectedReads ) ;
451+ assert . strictEqual ( modelReads , catalogReads ) ;
452+ assert . strictEqual ( serverStarts , starts ) ;
453+ } ) . pipe ( Effect . scoped , Effect . provide ( TestClock . layer ( ) ) ) ,
454+ ) ;
267455} ) ;
268456
269457// npm links into opencode-ai for 1.x and @opencode/cli for 2.x.
0 commit comments