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