@@ -22,6 +22,7 @@ import * as PreviewPasskeys from "../preview/Passkeys.ts";
2222import * as DesktopBackendManager from "../backend/DesktopBackendManager.ts" ;
2323import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts" ;
2424import * as DesktopHomeOwner from "../backend/DesktopHomeOwner.ts" ;
25+ import * as DesktopLocalEnvironmentAuth from "../backend/DesktopLocalEnvironmentAuth.ts" ;
2526import * as DesktopEnvironment from "./DesktopEnvironment.ts" ;
2627import * as DesktopLegacyLocalStorage from "./DesktopLegacyLocalStorage.ts" ;
2728import * as DesktopLifecycle from "./DesktopLifecycle.ts" ;
@@ -168,13 +169,12 @@ const stopAllPoolInstances = Effect.fn("desktop.app.stopAllPoolInstances")(
168169 } ,
169170) ;
170171
171- const bootstrap = Effect . gen ( function * ( ) {
172+ // Spawns the local backend as soon as the protocol, IPC, and legacy storage that every window needs exist.
173+ const startLocalBackend = Effect . gen ( function * ( ) {
172174 const state = yield * DesktopState . DesktopState ;
173175 const environment = yield * DesktopEnvironment . DesktopEnvironment ;
174176 const desktopSettings = yield * DesktopAppSettings . DesktopAppSettings ;
175177 const desktopWindow = yield * DesktopWindow . DesktopWindow ;
176- const snapShot = yield * DesktopSnapShot . DesktopSnapShot ;
177- const appActivation = yield * DesktopAppActivation . DesktopAppActivation ;
178178 yield * logBootstrapInfo ( "bootstrap start" ) ;
179179
180180 const settings = yield * desktopSettings . get ;
@@ -193,32 +193,13 @@ const bootstrap = Effect.gen(function* () {
193193 yield * ( yield * DesktopAppIdentity . DesktopAppIdentity ) . resolveUserDataPath ,
194194 ) ;
195195
196- yield * snapShot . initialize ;
197- // Only the user's own login shell PATH shows every t3 a new terminal runs; a fresh install has
198- // no cached one. Without it, the command is left to Settings.
199- const installCli = ( yield * DesktopCliCommand . DesktopCliCommand ) . installOnFirstLaunch ;
200- yield * Effect . forkScoped (
201- ( yield * DesktopShellEnvironment . DesktopShellEnvironment ) . awaitUserShellDiscovery . pipe (
202- Effect . flatMap ( ( found ) =>
203- found || environment . platform === "win32"
204- ? installCli
205- : logBootstrapInfo ( "skipped installing the t3 command without the shell PATH" ) ,
206- ) ,
207- ) ,
208- ) ;
209-
210196 if ( ! settings . localEnvironmentEnabled ) {
211197 yield * logBootstrapInfo ( "bootstrap skipping local environment (disabled in settings)" ) ;
212- if ( ! ( yield * Ref . get ( state . quitting ) ) ) {
213- yield * desktopWindow . createMainIfBackendReady ;
214- }
215- return ;
198+ return false ;
216199 }
217200
218- const pool = yield * DesktopBackendPool . DesktopBackendPool ;
219- const primaryBackend = yield * pool . primary ;
201+ const primaryBackend = yield * ( yield * DesktopBackendPool . DesktopBackendPool ) . primary ;
220202 const serverExposure = yield * DesktopServerExposure . DesktopServerExposure ;
221- const wslBackend = yield * DesktopWslBackend . DesktopWslBackend ;
222203
223204 if ( environment . isDevelopment && Option . isNone ( environment . configuredBackendPort ) ) {
224205 return yield * new DesktopDevelopmentBackendPortRequiredError ( ) ;
@@ -237,7 +218,8 @@ const bootstrap = Effect.gen(function* () {
237218 "This T3 home is unavailable" ,
238219 DesktopHomeOwner . homeOwnedMessage ( owner ) ,
239220 ) ;
240- return yield * ( yield * ElectronApp . ElectronApp ) . quit ;
221+ yield * ( yield * ElectronApp . ElectronApp ) . quit ;
222+ return false ;
241223 }
242224
243225 const backendPortSelection = yield * resolveDesktopBackendPort ( environment . configuredBackendPort ) ;
@@ -275,29 +257,73 @@ const bootstrap = Effect.gen(function* () {
275257 ) ;
276258 }
277259
278- if ( ! ( yield * Ref . get ( state . quitting ) ) ) {
279- if ( settings . wslOnly === true && settings . wslBackendEnabled === true ) {
280- yield * desktopWindow . showConnectingSplash ;
281- }
282- yield * primaryBackend . start ;
283- yield * logBootstrapInfo ( "bootstrap backend start requested" ) ;
284- // WSL-only fallback can change the primary endpoint before readiness.
285- const config = yield * primaryBackend . currentConfig ;
286- if (
287- ! environment . isDevelopment &&
288- ! settings . wslOnly &&
289- Option . isSome ( config ) &&
290- Option . isNone ( config . value . preflightFailure )
291- ) {
292- yield * desktopWindow . ensureMain ;
260+ if ( yield * Ref . get ( state . quitting ) ) {
261+ return false ;
262+ }
263+ if ( settings . wslOnly === true && settings . wslBackendEnabled === true ) {
264+ yield * desktopWindow . showConnectingSplash ;
265+ }
266+ yield * primaryBackend . start ;
267+ yield * logBootstrapInfo ( "bootstrap backend start requested" ) ;
268+ // The renderer asks for this token first; minting it at readiness saves a round trip.
269+ yield * Effect . forkScoped (
270+ ( yield * DesktopLocalEnvironmentAuth . DesktopLocalEnvironmentAuth ) . getBearerToken . pipe (
271+ Effect . ignore ,
272+ ) ,
273+ ) ;
274+ return true ;
275+ } ) . pipe ( Effect . withSpan ( "desktop.startLocalBackend" ) ) ;
276+
277+ const bootstrap = Effect . fn ( "desktop.bootstrap" ) ( function * ( backendStarted : boolean ) {
278+ const state = yield * DesktopState . DesktopState ;
279+ const environment = yield * DesktopEnvironment . DesktopEnvironment ;
280+ const desktopSettings = yield * DesktopAppSettings . DesktopAppSettings ;
281+ const desktopWindow = yield * DesktopWindow . DesktopWindow ;
282+ const snapShot = yield * DesktopSnapShot . DesktopSnapShot ;
283+ const appActivation = yield * DesktopAppActivation . DesktopAppActivation ;
284+ const settings = yield * desktopSettings . get ;
285+ if ( settings . localEnvironmentEnabled && ( ! backendStarted || ( yield * Ref . get ( state . quitting ) ) ) ) {
286+ return ;
287+ }
288+
289+ yield * snapShot . initialize ;
290+ // Only the user's own login shell PATH shows every t3 a new terminal runs; a fresh install has
291+ // no cached one. Without it, the command is left to Settings.
292+ const installCli = ( yield * DesktopCliCommand . DesktopCliCommand ) . installOnFirstLaunch ;
293+ yield * Effect . forkScoped (
294+ ( yield * DesktopShellEnvironment . DesktopShellEnvironment ) . awaitUserShellDiscovery . pipe (
295+ Effect . flatMap ( ( found ) =>
296+ found || environment . platform === "win32"
297+ ? installCli
298+ : logBootstrapInfo ( "skipped installing the t3 command without the shell PATH" ) ,
299+ ) ,
300+ ) ,
301+ ) ;
302+
303+ if ( ! settings . localEnvironmentEnabled ) {
304+ if ( ! ( yield * Ref . get ( state . quitting ) ) ) {
305+ yield * desktopWindow . createMainIfBackendReady ;
293306 }
294- yield * appActivation . start . pipe (
295- Effect . tap ( ( ) => logBootstrapInfo ( "desktop app control socket ready" ) ) ,
296- Effect . catch ( ( error ) => logStartupError ( "desktop app control socket unavailable" , { error } ) ) ,
297- ) ;
298- yield * Effect . forkScoped ( wslBackend . reconcile ) ;
307+ return ;
299308 }
300- } ) . pipe ( Effect . withSpan ( "desktop.bootstrap" ) ) ;
309+
310+ const primaryBackend = yield * ( yield * DesktopBackendPool . DesktopBackendPool ) . primary ;
311+ // WSL-only fallback can change the primary endpoint before readiness.
312+ const config = yield * primaryBackend . currentConfig ;
313+ if (
314+ ! environment . isDevelopment &&
315+ ! settings . wslOnly &&
316+ Option . isSome ( config ) &&
317+ Option . isNone ( config . value . preflightFailure )
318+ ) {
319+ yield * desktopWindow . ensureMain ;
320+ }
321+ yield * appActivation . start . pipe (
322+ Effect . tap ( ( ) => logBootstrapInfo ( "desktop app control socket ready" ) ) ,
323+ Effect . catch ( ( error ) => logStartupError ( "desktop app control socket unavailable" , { error } ) ) ,
324+ ) ;
325+ yield * Effect . forkScoped ( ( yield * DesktopWslBackend . DesktopWslBackend ) . reconcile ) ;
326+ } ) ;
301327
302328const startup = Effect . gen ( function * ( ) {
303329 const appIdentity = yield * DesktopAppIdentity . DesktopAppIdentity ;
@@ -369,14 +395,20 @@ const startup = Effect.gen(function* () {
369395 } ) ;
370396 }
371397 yield * appIdentity . configure ;
372- yield * previewPasskeys . configure ;
373- yield * applicationMenu . configure ;
398+ // Sets canInstallUpdates, which the backend bootstrap payload reads.
374399 yield * updates . configure ;
375400 yield * DesktopRemoteUpdates . listen ;
376401 yield * DesktopStartupRecovery . listen ;
402+ const backendStarted = yield * startLocalBackend . pipe (
403+ Effect . catchCause ( ( cause ) => fatalStartupCause ( "bootstrap" , cause ) ) ,
404+ ) ;
405+ yield * previewPasskeys . configure ;
406+ yield * applicationMenu . configure ;
377407 // Portal identity is prepared pre-ready; MIME helpers must not gate the backend or window.
378408 yield * linuxUrlHandler . register . pipe ( Effect . forkScoped ) ;
379- yield * bootstrap . pipe ( Effect . catchCause ( ( cause ) => fatalStartupCause ( "bootstrap" , cause ) ) ) ;
409+ yield * bootstrap ( backendStarted ) . pipe (
410+ Effect . catchCause ( ( cause ) => fatalStartupCause ( "bootstrap" , cause ) ) ,
411+ ) ;
380412} ) . pipe ( Effect . withSpan ( "desktop.startup" ) ) ;
381413
382414const scopedProgram = Effect . scoped (
0 commit comments