11import * as NodeServices from "@effect/platform-node/NodeServices" ;
22import { assert , describe , it } from "@effect/vitest" ;
33import * as Cause from "effect/Cause" ;
4+ import * as Deferred from "effect/Deferred" ;
5+ import * as Duration from "effect/Duration" ;
46import * as Effect from "effect/Effect" ;
7+ import * as Fiber from "effect/Fiber" ;
58import * as Queue from "effect/Queue" ;
69import * as Schema from "effect/Schema" ;
710import * as Sink from "effect/Sink" ;
811import * as Stream from "effect/Stream" ;
912import * as ChildProcess from "effect/process/ChildProcess" ;
1013import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
14+ import * as TestClock from "effect/testing/TestClock" ;
1115
1216import { checkPiProviderStatus , MINIMUM_PI_VERSION } from "./status.ts" ;
1317
@@ -52,7 +56,13 @@ function piProbeSpawner(version: string) {
5256 } ) ;
5357}
5458
55- const piDiscoverySpawner = ( inventory : unknown ) =>
59+ const piDiscoverySpawner = (
60+ inventory : unknown ,
61+ slowStartup ?: {
62+ readonly delay : Duration . Input ;
63+ readonly getStateReceived : Deferred . Deferred < void > ;
64+ } ,
65+ ) =>
5666 Effect . gen ( function * ( ) {
5767 const stdout = yield * Queue . unbounded < Uint8Array , Cause . Done > ( ) ;
5868 let stdinBuffer = "" ;
@@ -66,6 +76,10 @@ const piDiscoverySpawner = (inventory: unknown) =>
6676 stdinBuffer = stdinBuffer . slice ( newline + 1 ) ;
6777 if ( line . length === 0 ) continue ;
6878 const request = decodeRpcRequest ( line ) ;
79+ if ( request . type === "get_state" && slowStartup !== undefined ) {
80+ yield * Deferred . succeed ( slowStartup . getStateReceived , undefined ) ;
81+ yield * Effect . sleep ( slowStartup . delay ) ;
82+ }
6983 const data =
7084 request . type === "get_available_models"
7185 ? inventory
@@ -131,14 +145,20 @@ describe("PiProvider", () => {
131145 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
132146 ) ;
133147
134- it . effect ( "reports only the models returned by successful Pi discovery " , ( ) =>
148+ it . effect ( "reports discovered models when Pi startup takes 20 seconds " , ( ) =>
135149 Effect . gen ( function * ( ) {
136- const spawner = yield * piDiscoverySpawner ( {
137- models : [ { provider : "anthropic" , id : "claude-sonnet-4" } ] ,
138- } ) ;
139- const snapshot = yield * checkPiProviderStatus ( settings ) . pipe (
150+ const getStateReceived = yield * Deferred . make < void > ( ) ;
151+ const spawner = yield * piDiscoverySpawner (
152+ { models : [ { provider : "anthropic" , id : "claude-sonnet-4" } ] } ,
153+ { delay : "20 seconds" , getStateReceived } ,
154+ ) ;
155+ const probe = yield * checkPiProviderStatus ( settings ) . pipe (
140156 Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
157+ Effect . forkChild ,
141158 ) ;
159+ yield * Deferred . await ( getStateReceived ) ;
160+ yield * TestClock . adjust ( "20 seconds" ) ;
161+ const snapshot = yield * Fiber . join ( probe ) ;
142162 assert . equal ( snapshot . status , "ready" ) ;
143163 assert . equal ( snapshot . auth . status , "authenticated" ) ;
144164 assert . deepEqual (
@@ -149,6 +169,29 @@ describe("PiProvider", () => {
149169 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
150170 ) ;
151171
172+ it . effect ( "keeps compatible Pi selectable when startup outlasts the discovery deadline" , ( ) =>
173+ Effect . gen ( function * ( ) {
174+ const getStateReceived = yield * Deferred . make < void > ( ) ;
175+ const spawner = yield * piDiscoverySpawner (
176+ { models : [ { provider : "anthropic" , id : "claude-sonnet-4" } ] } ,
177+ { delay : "1 hour" , getStateReceived } ,
178+ ) ;
179+ const probe = yield * checkPiProviderStatus ( settings ) . pipe (
180+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
181+ Effect . forkChild ,
182+ ) ;
183+ yield * Deferred . await ( getStateReceived ) ;
184+ yield * TestClock . adjust ( "30 seconds" ) ;
185+ const snapshot = yield * Fiber . join ( probe ) ;
186+ assert . equal ( snapshot . status , "ready" ) ;
187+ assert . equal ( snapshot . auth . status , "unknown" ) ;
188+ assert . deepEqual (
189+ snapshot . models . map ( ( model ) => model . slug ) ,
190+ [ "default" ] ,
191+ ) ;
192+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
193+ ) ;
194+
152195 it . effect ( "reports a known unauthenticated inventory when Pi returns no usable models" , ( ) =>
153196 Effect . gen ( function * ( ) {
154197 const spawner = yield * piDiscoverySpawner ( { models : [ ] } ) ;
0 commit comments