@@ -5,10 +5,13 @@ import {
55 type ServerSelfUpdateProgressStage ,
66 type ServerSelfUpdateResult ,
77} from "@t3tools/contracts" ;
8+ import * as Cause from "effect/Cause" ;
9+ import * as Clock from "effect/Clock" ;
810import * as Context from "effect/Context" ;
911import * as Crypto from "effect/Crypto" ;
1012import * as Duration from "effect/Duration" ;
1113import * as Effect from "effect/Effect" ;
14+ import * as HashMap from "effect/HashMap" ;
1215import * as Layer from "effect/Layer" ;
1316import * as Option from "effect/Option" ;
1417import * as Ref from "effect/Ref" ;
@@ -46,6 +49,8 @@ export class DesktopAppUpdate extends Context.Service<
4649 /** True when this server was spawned by a desktop app that can be
4750 driven over the telemetry control channel. */
4851 readonly available : boolean ;
52+ /** Keeps the managed tunnel while an accepted remote install restarts this server. */
53+ readonly isRestartPending : Effect . Effect < boolean > ;
4954 /** Checks and downloads through the desktop app, then returns a token
5055 while this server is still connected. `commit` starts installation. */
5156 readonly run : (
@@ -67,6 +72,27 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* (
6772 const crypto = yield * Crypto . Crypto ;
6873 const receiver = yield * DesktopTelemetryReceiver . DesktopTelemetryReceiver ;
6974 const inFlight = yield * Ref . make ( false ) ;
75+ // The RPC can disappear before shutdown cleanup runs. Retain accepted
76+ // handoffs across that interruption, but bound them by the install deadline.
77+ const pendingRestarts = yield * Ref . make ( HashMap . empty < string , number > ( ) ) ;
78+
79+ const isRestartPending = Effect . scoped (
80+ Effect . gen ( function * ( ) {
81+ const now = yield * Clock . currentTimeMillis ;
82+ const { latest } = yield * receiver . desktopUpdates ;
83+ const failedRequestId =
84+ Option . isSome ( latest ) && latest . value . outcome === "failed"
85+ ? latest . value . requestId
86+ : undefined ;
87+ const pending = yield * Ref . updateAndGet ( pendingRestarts , ( requests ) =>
88+ HashMap . filter (
89+ requests ,
90+ ( deadline , requestId ) => deadline > now && requestId !== failedRequestId ,
91+ ) ,
92+ ) ;
93+ return HashMap . size ( pending ) > 0 ;
94+ } ) ,
95+ ) ;
7096
7197 const available = config . mode === "desktop" && config . desktopTelemetryControlFd !== undefined ;
7298 const failWith = ( reason : string , cause ?: unknown ) =>
@@ -191,41 +217,62 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* (
191217 if ( ! available ) {
192218 return yield * failWith ( "This server cannot commit a desktop app update." ) ;
193219 }
194- const terminal = yield * Effect . scoped (
195- Effect . gen ( function * ( ) {
196- const { latest, changes } = yield * receiver . desktopUpdates ;
197- const reports = Option . match ( latest , {
198- onNone : ( ) => changes ,
199- onSome : ( report ) => Stream . concat ( Stream . make ( report ) , changes ) ,
200- } ) ;
201- yield * Effect . uninterruptible (
202- receiver . commitDesktopUpdate ( requestId ) . pipe (
203- Effect . mapError ( ( error ) => failWith ( "Could not reach the T3 Code desktop app." , error ) ) ,
204- Effect . tap ( ( ) => onHandoffAccepted ( ) ) ,
205- ) ,
206- ) ;
207- return yield * reports . pipe (
208- Stream . filter ( ( report ) => report . requestId === requestId && report . outcome === "failed" ) ,
209- Stream . runHead ,
210- Effect . timeout ( DESKTOP_INSTALL_TIMEOUT ) ,
211- Effect . catchTags ( {
212- TimeoutError : ( ) =>
213- failWith ( "The desktop app did not report an install result in time." ) ,
214- } ) ,
215- ) ;
216- } ) ,
217- ) ;
218- if ( Option . isNone ( terminal ) ) {
219- return yield * failWith ( "The desktop app stopped reporting the install." ) ;
220- }
221- return yield * failWith (
222- terminal . value . reason ??
223- terminal . value . state . message ??
224- "The desktop app failed to install the update." ,
220+ let handoffAccepted = false ;
221+ return yield * Effect . gen ( function * ( ) {
222+ const terminal = yield * Effect . scoped (
223+ Effect . gen ( function * ( ) {
224+ const { latest, changes } = yield * receiver . desktopUpdates ;
225+ const reports = Option . match ( latest , {
226+ onNone : ( ) => changes ,
227+ onSome : ( report ) => Stream . concat ( Stream . make ( report ) , changes ) ,
228+ } ) ;
229+ yield * Effect . uninterruptible (
230+ Effect . gen ( function * ( ) {
231+ const now = yield * Clock . currentTimeMillis ;
232+ yield * Ref . update (
233+ pendingRestarts ,
234+ HashMap . set ( requestId , now + Duration . toMillis ( DESKTOP_INSTALL_TIMEOUT ) ) ,
235+ ) ;
236+ yield * receiver . commitDesktopUpdate ( requestId ) ;
237+ handoffAccepted = true ;
238+ } ) . pipe (
239+ Effect . mapError ( ( error ) =>
240+ failWith ( "Could not reach the T3 Code desktop app." , error ) ,
241+ ) ,
242+ Effect . tap ( ( ) => onHandoffAccepted ( ) ) ,
243+ ) ,
244+ ) ;
245+ return yield * reports . pipe (
246+ Stream . filter (
247+ ( report ) => report . requestId === requestId && report . outcome === "failed" ,
248+ ) ,
249+ Stream . runHead ,
250+ Effect . timeout ( DESKTOP_INSTALL_TIMEOUT ) ,
251+ Effect . catchTags ( {
252+ TimeoutError : ( ) =>
253+ failWith ( "The desktop app did not report an install result in time." ) ,
254+ } ) ,
255+ ) ;
256+ } ) ,
257+ ) ;
258+ if ( Option . isNone ( terminal ) ) {
259+ return yield * failWith ( "The desktop app stopped reporting the install." ) ;
260+ }
261+ return yield * failWith (
262+ terminal . value . reason ??
263+ terminal . value . state . message ??
264+ "The desktop app failed to install the update." ,
265+ ) ;
266+ } ) . pipe (
267+ Effect . onExit ( ( exit ) =>
268+ exit . _tag === "Failure" && handoffAccepted && Cause . hasInterruptsOnly ( exit . cause )
269+ ? Effect . void
270+ : Ref . update ( pendingRestarts , HashMap . remove ( requestId ) ) ,
271+ ) ,
225272 ) ;
226273 } ) ;
227274
228- return DesktopAppUpdate . of ( { available, run, commit } ) ;
275+ return DesktopAppUpdate . of ( { available, isRestartPending , run, commit } ) ;
229276} ) ;
230277
231278export const layer = Layer . effect ( DesktopAppUpdate , make ( ) ) ;
0 commit comments