11import * as Brand from "effect/Brand" ;
22import * as Cause from "effect/Cause" ;
3+ import * as Context from "effect/Context" ;
34import * as Duration from "effect/Duration" ;
45import * as Effect from "effect/Effect" ;
56import * as Exit from "effect/Exit" ;
@@ -13,7 +14,7 @@ import * as Schema from "effect/Schema";
1314import * as Semaphore from "effect/Semaphore" ;
1415import * as Scope from "effect/Scope" ;
1516import * as Stream from "effect/Stream" ;
16- import { HttpClient } from "effect/http" ;
17+ import { HttpClient , HttpClientRequest } from "effect/http" ;
1718import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
1819
1920import {
@@ -39,6 +40,7 @@ const MAX_PREFLIGHT_FAILURE_ATTEMPTS = 5;
3940const DEFAULT_BACKEND_READINESS_TIMEOUT = Duration . minutes ( 3 ) ;
4041const DEFAULT_BACKEND_READINESS_INTERVAL = Duration . millis ( 100 ) ;
4142const DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT = Duration . seconds ( 1 ) ;
43+ const EXTERNAL_LIVENESS_INTERVAL = Duration . seconds ( 1 ) ;
4244// Match the service launcher's grace period so backends can stop provider sessions.
4345const DEFAULT_BACKEND_TERMINATE_GRACE = Duration . seconds ( 5 ) ;
4446const DEFAULT_BACKEND_OUTPUT_DRAIN_TIMEOUT = Duration . seconds ( 5 ) ;
@@ -49,6 +51,30 @@ const STARTUP_FAILURE_OUTPUT_CHARS = 4_000;
4951const { logWarning : logBackendProcessWarning } =
5052 DesktopObservability . makeComponentLogger ( "desktop-backend-process" ) ;
5153
54+ const isExternalPidAlive = ( pid : number ) : boolean => {
55+ if ( pid <= 0 ) {
56+ return false ;
57+ }
58+ try {
59+ process . kill ( pid , 0 ) ;
60+ return true ;
61+ } catch ( error ) {
62+ return (
63+ typeof error === "object" &&
64+ error !== null &&
65+ "code" in error &&
66+ ( error . code === "EPERM" || error . code === "EACCES" )
67+ ) ;
68+ }
69+ } ;
70+
71+ const ProcessLivenessProbe = Context . Reference < ( pid : number ) => Effect . Effect < boolean > > (
72+ "@t3tools/desktop/backend/ProcessLivenessProbe" ,
73+ {
74+ defaultValue : ( ) => ( pid : number ) => Effect . sync ( ( ) => isExternalPidAlive ( pid ) ) ,
75+ } ,
76+ ) ;
77+
5278type BackendProcessLayerServices = ChildProcessSpawner . ChildProcessSpawner | HttpClient . HttpClient ;
5379
5480type BackendProcessRunRequirements = BackendProcessLayerServices | Scope . Scope ;
@@ -278,6 +304,7 @@ interface BackendManagerState {
278304 readonly nextRunId : number ;
279305 readonly isExternal ?: boolean ;
280306 readonly externalPid ?: Option . Option < number > ;
307+ readonly externalWatcherFiber : Option . Option < Fiber . Fiber < void , never > > ;
281308}
282309
283310const initialState : BackendManagerState = {
@@ -291,6 +318,7 @@ const initialState: BackendManagerState = {
291318 nextRunId : 1 ,
292319 isExternal : false ,
293320 externalPid : Option . none ( ) ,
321+ externalWatcherFiber : Option . none ( ) ,
294322} ;
295323
296324const activePid = ( active : Option . Option < ActiveBackendRun > ) : Option . Option < number > =>
@@ -689,6 +717,21 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
689717 } ) ;
690718 } ) ;
691719
720+ const cancelExternalWatcher = Effect . gen ( function * ( ) {
721+ const externalWatcherFiber = yield * SubscriptionRef . modify ( state , ( current ) => [
722+ current . externalWatcherFiber ,
723+ {
724+ ...current ,
725+ externalWatcherFiber : Option . none ( ) ,
726+ } ,
727+ ] ) ;
728+
729+ yield * Option . match ( externalWatcherFiber , {
730+ onNone : ( ) => Effect . void ,
731+ onSome : ( fiber ) => Fiber . interrupt ( fiber ) . pipe ( Effect . asVoid ) ,
732+ } ) ;
733+ } ) ;
734+
692735 const start : Effect . Effect < void > = Effect . suspend ( ( ) =>
693736 mutex . withPermits ( 1 ) (
694737 Effect . gen ( function * ( ) {
@@ -740,6 +783,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
740783 const resetFatalPreflightCounter =
741784 ! current . desiredRunning && current . preflightFailureAttempt > 0 ;
742785 yield * cancelRestart ;
786+ yield * cancelExternalWatcher ;
743787 yield * SubscriptionRef . update ( state , ( latest ) => ( {
744788 ...latest ,
745789 desiredRunning : true ,
@@ -748,6 +792,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
748792 preflightFailureAttempt : resetFatalPreflightCounter ? 0 : latest . preflightFailureAttempt ,
749793 isExternal : false ,
750794 externalPid : Option . none < number > ( ) ,
795+ externalWatcherFiber : Option . none ( ) ,
751796 } ) ) ;
752797
753798 const preflightFailure = config . value . preflightFailure ;
@@ -1076,7 +1121,9 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
10761121 const stop = Effect . fn ( "desktop.backendInstance.stop" ) ( function * ( options ?: {
10771122 readonly timeout ?: Duration . Duration ;
10781123 } ) {
1079- const { active, restartFiber, notifyShutdown } = yield * mutex . withPermits ( 1 ) (
1124+ const { active, restartFiber, externalWatcherFiber, notifyShutdown } = yield * mutex . withPermits (
1125+ 1 ,
1126+ ) (
10801127 Effect . gen ( function * ( ) {
10811128 const result = yield * SubscriptionRef . modify ( state , ( latest ) => {
10821129 const active = Option . map ( latest . active , ( run ) =>
@@ -1086,6 +1133,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
10861133 {
10871134 active,
10881135 restartFiber : latest . restartFiber ,
1136+ externalWatcherFiber : latest . externalWatcherFiber ,
10891137 notifyShutdown : latest . ready ,
10901138 } ,
10911139 {
@@ -1094,6 +1142,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
10941142 ready : false ,
10951143 active,
10961144 restartFiber : Option . none < Fiber . Fiber < void , never > > ( ) ,
1145+ externalWatcherFiber : Option . none < Fiber . Fiber < void , never > > ( ) ,
10971146 isExternal : false ,
10981147 externalPid : Option . none < number > ( ) ,
10991148 } ,
@@ -1110,6 +1159,10 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
11101159 onNone : ( ) => Effect . void ,
11111160 onSome : ( fiber ) => Fiber . interrupt ( fiber ) . pipe ( Effect . asVoid ) ,
11121161 } ) ;
1162+ yield * Option . match ( externalWatcherFiber , {
1163+ onNone : ( ) => Effect . void ,
1164+ onSome : ( fiber ) => Fiber . interrupt ( fiber ) . pipe ( Effect . asVoid ) ,
1165+ } ) ;
11131166 yield * Option . match ( active , {
11141167 onNone : ( ) => Effect . void ,
11151168 // The enclosing operation owns the span for this inline handler.
@@ -1167,11 +1220,90 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
11671220 } ) ;
11681221 } ) ;
11691222
1223+ const probeHttpReady = Effect . fn ( "desktop.backendInstance.probeHttpReady" ) ( function * (
1224+ httpBaseUrl : URL ,
1225+ ) {
1226+ const readinessUrl = new URL ( BACKEND_READINESS_PATH , httpBaseUrl ) . toString ( ) ;
1227+ const isReady = yield * httpClient . execute ( HttpClientRequest . get ( readinessUrl ) ) . pipe (
1228+ Effect . timeoutOption ( DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT ) ,
1229+ Effect . flatMap ( ( responseOption ) =>
1230+ Option . match ( responseOption , {
1231+ onNone : ( ) => Effect . succeed ( false ) ,
1232+ onSome : ( res ) => Effect . succeed ( res . status >= 200 && res . status < 300 ) ,
1233+ } ) ,
1234+ ) ,
1235+ Effect . orElseSucceed ( ( ) => false ) ,
1236+ ) ;
1237+ return isReady ;
1238+ } ) ;
1239+
1240+ const watchExternal = Effect . fn ( "desktop.backendInstance.watchExternal" ) ( function * (
1241+ config : DesktopBackendStartConfig ,
1242+ pid : Option . Option < number > ,
1243+ ) {
1244+ const isProcessAlive = yield * ProcessLivenessProbe ;
1245+ while ( true ) {
1246+ yield * Effect . sleep ( EXTERNAL_LIVENESS_INTERVAL ) ;
1247+ const current = yield * SubscriptionRef . get ( state ) ;
1248+ if ( ! current . isExternal || ! current . ready ) {
1249+ return ;
1250+ }
1251+
1252+ const pidAlive = yield * Option . match ( pid , {
1253+ onSome : ( p ) => isProcessAlive ( p ) ,
1254+ onNone : ( ) => Effect . succeed ( true ) ,
1255+ } ) ;
1256+
1257+ const isAlive = pidAlive ? yield * probeHttpReady ( config . httpBaseUrl ) : false ;
1258+
1259+ if ( ! isAlive ) {
1260+ yield * logInstanceWarning ( "attached external backend is no longer running" ) ;
1261+ yield * mutex . withPermits ( 1 ) (
1262+ Effect . gen ( function * ( ) {
1263+ const { shouldRecover, wasReady } = yield * SubscriptionRef . modify (
1264+ state ,
1265+ (
1266+ latest ,
1267+ ) : readonly [
1268+ { readonly shouldRecover : boolean ; readonly wasReady : boolean } ,
1269+ BackendManagerState ,
1270+ ] => {
1271+ if ( ! latest . isExternal || ! latest . ready ) {
1272+ return [ { shouldRecover : false , wasReady : false } , latest ] ;
1273+ }
1274+ return [
1275+ { shouldRecover : latest . desiredRunning , wasReady : latest . ready } ,
1276+ {
1277+ ...latest ,
1278+ ready : false ,
1279+ isExternal : false ,
1280+ externalPid : Option . none ( ) ,
1281+ externalWatcherFiber : Option . none ( ) ,
1282+ } ,
1283+ ] ;
1284+ } ,
1285+ ) ;
1286+
1287+ if ( wasReady ) {
1288+ yield * ( spec . onShutdown ?.( ) ?? Effect . void ) . pipe ( Effect . ignore ) ;
1289+ }
1290+
1291+ if ( shouldRecover ) {
1292+ yield * scheduleRestart ( "attached external server exited" ) ;
1293+ }
1294+ } ) ,
1295+ ) ;
1296+ return ;
1297+ }
1298+ }
1299+ } ) ;
1300+
11701301 const attachExternal = Effect . fn ( "desktop.backendInstance.attachExternal" ) ( function * (
11711302 config : DesktopBackendStartConfig ,
11721303 pid : Option . Option < number > = Option . none ( ) ,
11731304 ) {
11741305 yield * cancelRestart ;
1306+ yield * cancelExternalWatcher ;
11751307 yield * SubscriptionRef . update ( state , ( latest ) => ( {
11761308 ...latest ,
11771309 desiredRunning : true ,
@@ -1181,8 +1313,25 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
11811313 restartAttempt : 0 ,
11821314 isExternal : true ,
11831315 externalPid : pid ,
1316+ externalWatcherFiber : Option . none ( ) ,
11841317 } ) ) ;
11851318 yield * ( spec . onReady ?.( config . httpBaseUrl ) ?? Effect . void ) . pipe ( Effect . ignore ) ;
1319+
1320+ const watcherFiber = yield * Effect . forkIn (
1321+ watchExternal ( config , pid ) . pipe (
1322+ Effect . catchCause ( ( cause ) =>
1323+ logInstanceError ( "desktop backend external watcher failed" , {
1324+ cause : Cause . pretty ( cause ) ,
1325+ } ) ,
1326+ ) ,
1327+ ) ,
1328+ parentScope ,
1329+ ) ;
1330+ yield * SubscriptionRef . update ( state , ( latest ) =>
1331+ latest . isExternal && Option . isNone ( latest . externalWatcherFiber )
1332+ ? { ...latest , externalWatcherFiber : Option . some ( watcherFiber ) }
1333+ : latest ,
1334+ ) ;
11861335 } ) ;
11871336
11881337 const waitForReady = Effect . fn ( "DesktopBackendManager.waitForReady" ) (
0 commit comments