@@ -53,7 +53,7 @@ pub struct MoonSession {
5353
5454 // Lazy components
5555 pub ( crate ) cache_engine : OnceLock < Arc < CacheEngine > > ,
56- // pub(crate) daemon_client: OnceCell<Option< DaemonClient> >,
56+ pub ( crate ) daemon_client : OnceLock < DaemonClient > ,
5757 pub ( crate ) extension_registry : OnceCell < Arc < ExtensionRegistry > > ,
5858 pub ( crate ) project_graph : OnceLock < Arc < ProjectGraph > > ,
5959 pub ( crate ) task_graph : OnceLock < Arc < TaskGraph > > ,
@@ -83,7 +83,7 @@ impl MoonSession {
8383 config_dir : PathBuf :: new ( ) ,
8484 config_loader : ConfigLoader :: default ( ) ,
8585 console : Console :: new ( cli. quiet || is_formatted_output ( ) ) ,
86- // daemon_client: OnceCell ::new(),
86+ daemon_client : OnceLock :: new ( ) ,
8787 extensions_config : Arc :: new ( ExtensionsConfig :: default ( ) ) ,
8888 extension_registry : OnceCell :: new ( ) ,
8989 moon_env : Arc :: new ( MoonEnvironment :: default ( ) ) ,
@@ -139,16 +139,19 @@ impl MoonSession {
139139 return Ok ( None ) ;
140140 }
141141
142- // let client = self
143- // .daemon_client
144- // .get_or_try_init(async move || self.get_daemon_connector()?.acquire().await)
145- // .await?;
142+ if let Some ( client) = self . daemon_client . get ( ) {
143+ return Ok ( Some ( client. to_owned ( ) ) ) ;
144+ }
145+
146+ let client = self . get_daemon_connector ( ) ?. acquire ( ) . await ?;
146147
147- // Ok(client.clone())
148+ // Only cache the client if we successfully connected to a daemon.
149+ // If we failed to connect, we don't want to cache so that we try again.
150+ if let Some ( client) = & client {
151+ let _ = self . daemon_client . set ( client. to_owned ( ) ) ;
152+ }
148153
149- // `acquire` connects to a running daemon or starts one, degrading to
150- // `None` on failure, so there's nothing to handle here.
151- self . get_daemon_connector ( ) ?. acquire ( ) . await
154+ Ok ( client)
152155 }
153156
154157 pub async fn create_workspace_graph_context ( & self ) -> miette:: Result < WorkspaceBuilderContext > {
@@ -476,7 +479,9 @@ impl AppSession for MoonSession {
476479 // `connect_to_daemon`, so the two coordinate and at most one spawns —
477480 // and a spawn failure degrades to `None` instead of failing the run.
478481 if self . is_daemon_allowed ( ) {
479- self . get_daemon_connector ( ) ?. acquire ( ) . await ?;
482+ if let Some ( client) = self . get_daemon_connector ( ) ?. acquire ( ) . await ? {
483+ let _ = self . daemon_client . set ( client) ;
484+ }
480485 }
481486
482487 Ok ( None )
0 commit comments