Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Binary/Main/
AppLifecycle.rs

1//! # AppLifecycle (Binary/Main)
2//!
3//! ## RESPONSIBILITIES
4//!
5//! Application lifecycle management for the Tauri application setup and
6//! initialization. This module handles the complete setup process during the
7//! Tauri setup hook, including tray initialization, command registration, IPC
8//! server setup, window creation, environment configuration, and async service
9//! initialization.
10//!
11//! ## ARCHITECTURAL ROLE
12//!
13//! The AppLifecycle module is the **initialization layer** in Mountain's
14//! architecture:
15//!
16//! ```text
17//! Tauri Builder Setup ──► AppLifecycle::AppLifecycleSetup()
18//!                              │
19//!                              ├─► Tray Initialization
20//!                              ├─► Command Registration
21//!                              ├─► IPC Server Setup
22//!                              ├─► Window Building
23//!                              ├─► Environment Setup
24//!                              ├─► Runtime Setup
25//!                              └─► Async Service Initialization
26//! ```
27//!
28//! ## KEY COMPONENTS
29//!
30//! - **AppLifecycleSetup()**: Main setup function orchestrating all
31//!   initialization
32//! - **Tray Initialization**: System tray icon with Dark/Light mode support
33//! - **Command Registration**: Native command registration with application
34//!   state
35//! - **IPC Server**: Mountain IPC server for frontend-backend communication
36//! - **Window Building**: Main application window configuration
37//! - **MountainEnvironment**: Environment context for application services
38//! - **ApplicationRunTime**: Runtime context with scheduler and environment
39//! - **Status Reporter**: IPC status reporting initialization
40//! - **Advanced Features**: Advanced IPC features initialization
41//! - **Wind Sync**: Wind advanced sync initialization
42//! - **Async Initialization**: Post-setup async service initialization
43//!
44//! ## ERROR HANDLING
45//!
46//! Returns `Result<(), Box<dyn std::error::Error>>` for setup errors.
47//! Non-critical failures are logged but don't prevent application startup.
48//! Critical failures are propagated to prevent incomplete startup.
49//!
50//! ## LOGGING
51//!
52//! Comprehensive logging at INFO level for major setup steps,
53//! DEBUG level for detailed processing, and ERROR for failures.
54//! All logs are prefixed with `[Lifecycle] [ComponentName]`.
55//!
56//! ## PERFORMANCE CONSIDERATIONS
57//!
58//! - Async initialization spawned after main setup to avoid blocking
59//! - Services initialized only when needed
60//! - Clone operations minimized for Arc-wrapped shared state
61//!
62//! ## TODO
63//! - [ ] Add setup progress tracking
64//! - [ ] Implement setup timeout handling
65//! - [ ] Add setup rollback mechanism on failure
66
67use std::sync::Arc;
68
69use tauri::Manager;
70use Echo::Scheduler::Scheduler::Scheduler;
71
72use crate::dev_log;
73#[cfg(debug_assertions)]
74use crate::Binary::Debug::WebkitServer;
75
76/// Master "disable Land customisations" gate. Returns `true` when the
77/// `Disable=true` env var is set (PascalCase, single-word, matching
78/// the rest of Land's env surface in `.env.Land.Diagnostics`). When
79/// enabled, Mountain skips:
80///   - `WindowEvent::CloseRequested` intercept (Cmd+W routes natively)
81///   - Cocoon + Air sidecar spawn
82///   - The Wind / SkyBridge advanced-features registration
83///   - The smoke-test gating that would otherwise activate via Sky
84///
85/// Code paths are NOT removed - just skipped at runtime so a clean
86/// `Disable=` env var (or `Disable=false`) restores stock behaviour.
87fn IsLandDisabled() -> bool {
88	std::env::var("Disable")
89		.map(|Value| Value.eq_ignore_ascii_case("true"))
90		.unwrap_or(false)
91}
92
93use crate::{
94	// Crate root imports
95	ApplicationState::State::ApplicationState::ApplicationState,
96	// Binary submodule imports
97	Binary::Build::WindowBuild::WindowBuild as WindowBuildFn,
98	Binary::Extension::ExtensionPopulate::ExtensionPopulate as ExtensionPopulateFn,
99	Binary::Extension::ScanPathConfigure::ScanPathConfigure as ScanPathConfigureFn,
100	Binary::Register::AdvancedFeaturesRegister::AdvancedFeaturesRegister as AdvancedFeaturesRegisterFn,
101	Binary::Register::CommandRegister::CommandRegister as CommandRegisterFn,
102	Binary::Register::IPCServerRegister::IPCServerRegister as IPCServerRegisterFn,
103	Binary::Register::StatusReporterRegister::StatusReporterRegister as StatusReporterRegisterFn,
104	Binary::Register::WindSyncRegister::WindSyncRegister as WindSyncRegisterFn,
105	Binary::Service::AirStart::AirStart as AirStartFn,
106	Binary::Service::CocoonStart::CocoonStart as CocoonStartFn,
107	Binary::Service::ConfigurationInitialize::ConfigurationInitialize as ConfigurationInitializeFn,
108	Binary::Service::VineStart::VineStart as VineStartFn,
109	Binary::Tray::EnableTray as EnableTrayFn,
110	Environment::MountainEnvironment::MountainEnvironment,
111	RunTime::ApplicationRunTime::ApplicationRunTime,
112};
113
114/// Logs a checkpoint message at TRACE level.
115macro_rules! TraceStep {
116
117	($($arg:tt)*) => {{
118
119		dev_log!("lifecycle", $($arg)*);
120	}};
121}
122
123/// Sets up the application lifecycle during Tauri initialization.
124///
125/// This function coordinates all setup operations:
126/// 1. System tray initialization
127/// 2. Native command registration
128/// 3. IPC server initialization
129/// 4. Main window creation
130/// 5. Mountain environment setup
131/// 6. Application runtime setup
132/// 7. Status reporter initialization
133/// 8. Advanced features initialization
134/// 9. Wind advanced sync initialization
135/// 10. Async post-setup initialization
136///
137/// # Parameters
138///
139/// * `app` - Mutable reference to Tauri App instance
140/// * `app_handle` - Cloned Tauri AppHandle for async operations
141/// * `localhost_url` - URL for the development server
142/// * `scheduler` - Arc-wrapped Echo Scheduler
143/// * `app_state` - Application state clone
144///
145/// # Returns
146///
147/// `Result<(), Box<dyn std::error::Error>>` - Ok on success, Err on critical
148/// failure
149pub fn AppLifecycleSetup(
150	app:&mut tauri::App,
151
152	app_handle:tauri::AppHandle,
153
154	localhost_url:String,
155
156	scheduler:Arc<Scheduler>,
157
158	app_state:Arc<ApplicationState>,
159) -> Result<(), Box<dyn std::error::Error>> {
160	dev_log!("lifecycle", "[Lifecycle] [Setup] Setup hook started.");
161
162	dev_log!("lifecycle", "[Lifecycle] [Setup] LocalhostUrl={}", localhost_url);
163
164	let app_handle_for_setup = app_handle.clone();
165
166	TraceStep!("[Lifecycle] [Setup] AppHandle acquired.");
167
168	// -------------------------------------------------------------------------
169	// [UI] [Tray] Initialize System Tray
170	// -------------------------------------------------------------------------
171	dev_log!("lifecycle", "[UI] [Tray] Initializing system tray...");
172
173	if let Err(Error) = EnableTrayFn::enable_tray(app) {
174		dev_log!("lifecycle", "error: [UI] [Tray] Failed to enable tray: {}", Error);
175	}
176
177	// -------------------------------------------------------------------------
178	// [Lifecycle] [Commands] Register native commands
179	// -------------------------------------------------------------------------
180	dev_log!("lifecycle", "[Lifecycle] [Commands] Registering native commands...");
181
182	if let Err(e) = CommandRegisterFn(&app_handle_for_setup, &app_state) {
183		dev_log!("lifecycle", "error: [Lifecycle] [Commands] Failed to register commands: {}", e);
184	}
185
186	dev_log!("lifecycle", "[Lifecycle] [Commands] Native commands registered.");
187
188	// -------------------------------------------------------------------------
189	// [Lifecycle] [IPC] Initialize IPC Server
190	// -------------------------------------------------------------------------
191	dev_log!("lifecycle", "[Lifecycle] [IPC] Initializing Mountain IPC Server...");
192
193	if let Err(e) = IPCServerRegisterFn(&app_handle_for_setup) {
194		dev_log!("lifecycle", "error: [Lifecycle] [IPC] Failed to register IPC server: {}", e);
195	}
196
197	// -------------------------------------------------------------------------
198	// [UI] [Window] Build main window
199	// -------------------------------------------------------------------------
200	dev_log!("lifecycle", "[UI] [Window] Building main window...");
201
202	let MainWindow = WindowBuildFn(app, localhost_url.clone());
203
204	dev_log!("lifecycle", "[UI] [Window] Main window ready.");
205
206	// DevTools auto-open is opt-in via the PascalCase env var
207	// `Inspect=1` (or any non-empty value other than `0`). Naming
208	// follows Land's single-word PascalCase verb convention -
209	// see `.env.Land.Diagnostics` for the documented set.
210	//
211	// Auto-opening DevTools on every debug launch was the direct
212	// cause of "I can't type or fire keybindings": the DevTools
213	// window steals macOS keyboard focus the moment it appears, so
214	// the main webview never becomes first responder and every
215	// keystroke goes to DevTools (or the system menu) instead of
216	// the workbench. The keybinding shortcut `Cmd+Alt+I` (Tauri's
217	// default) and the right-click "Inspect" entry both still
218	// work when needed.
219	#[cfg(debug_assertions)]
220	{
221		let WantDevTools = std::env::var("Inspect")
222			.map(|Value| !Value.is_empty() && Value != "0")
223			.unwrap_or(false);
224
225		if WantDevTools {
226			dev_log!("lifecycle", "[UI] [Window] Inspect=1 set: opening DevTools.");
227
228			MainWindow.open_devtools();
229		} else {
230			dev_log!(
231				"lifecycle",
232				"[UI] [Window] Debug build: DevTools auto-open suppressed (export Inspect=1 to override)."
233			);
234		}
235	}
236
237	#[cfg(debug_assertions)]
238	{
239		let enable_debug_server = std::env::var("DebugServer").map(|v| v != "0" && !v.is_empty()).unwrap_or(false);
240		if enable_debug_server {
241			// DebugServer values: mountain | cocoon | both | 1 (= mountain, legacy).
242			// Mountain port: DebugServerPort or DebugServerPortMountain (default 9933).
243			// Cocoon port: DebugServerPortCocoon (default 9934) - started inside the
244			// Cocoon extension-host process from its own bootstrap path.
245			dev_log!(
246				"lifecycle",
247				"[Debug] [Webkit] DebugServer mode={} Mountain-port={} Cocoon-port={}",
248				std::env::var("DebugServer").unwrap_or_else(|_| "(unset)".into()),
249				std::env::var("DebugServerPortMountain")
250					.or_else(|_| std::env::var("DebugServerPort"))
251					.unwrap_or_else(|_| "9933".into()),
252				std::env::var("DebugServerPortCocoon").unwrap_or_else(|_| "9934".into())
253			);
254			WebkitServer::install(&MainWindow);
255		}
256	}
257
258	// -------------------------------------------------------------------------
259	// [UI] [Window] Intercept CloseRequested so Cmd+W (and the macOS app
260	// menu's Window > Close item) routes through the workbench instead of
261	// killing the whole window.
262	//
263	// On macOS, Tauri 2.x installs a default app menu that maps Cmd+W to
264	// NSWindow's `performClose:`. The webview's keydown handler never gets
265	// the event because the menu wins the responder chain. The result the
266	// user sees: hitting Cmd+W to close a tab nukes the entire editor.
267	//
268	// The fix is the standard Electron-style handshake:
269	//   1. Mountain prevents the close.
270	//   2. Mountain emits `sky://window/close-requested` to the webview.
271	//   3. Sky listens, asks the workbench to close the active editor; if there is
272	//      no active editor (or the workbench refuses), Sky calls
273	//      `nativeHost:closeWindow`, which uses `WebviewWindow::destroy()` to tear
274	//      the window down without re-firing CloseRequested.
275	if IsLandDisabled() {
276		dev_log!(
277			"window",
278			"[UI] [Window] Disable=true: CloseRequested intercept SKIPPED (Cmd+W will close window natively)"
279		);
280	} else {
281		use tauri::Emitter;
282
283		let CloseEmitter = MainWindow.clone();
284
285		MainWindow.on_window_event(move |Event| {
286			if let tauri::WindowEvent::CloseRequested { api, .. } = Event {
287				api.prevent_close();
288				let _ = CloseEmitter.emit("sky://window/close-requested", ());
289				dev_log!("window", "[UI] [Window] CloseRequested intercepted; forwarded to webview");
290			}
291		});
292	}
293
294	// -------------------------------------------------------------------------
295	// [Backend] [Dirs] Ensure userdata directories exist
296	// -------------------------------------------------------------------------
297	{
298		let PathResolver = app.path();
299
300		let AppDataDir = PathResolver.app_data_dir().unwrap_or_default();
301
302		let LogDir = PathResolver.app_log_dir().unwrap_or_default();
303
304		let HomeDir = PathResolver.home_dir().unwrap_or_default();
305
306		// Set the canonical userdata base so WindServiceHandlers resolves
307		// /User/... paths to the real Tauri app_data_dir (not hardcoded "FIDDEE").
308		crate::IPC::WindServiceHandlers::Utilities::UserdataDir::set_userdata_base_dir(
309			AppDataDir.to_string_lossy().to_string(),
310		);
311
312		// Set the real filesystem root for /Static/Application/ path mapping.
313		// In dev mode, Tauri serves from ../Sky/Target relative to Mountain.
314		// Tauri's resource_dir gives us the frontendDist path.
315		// Resolve Sky/Target via Tauri first; fall back to executable-
316		// relative bundle and monorepo layouts so raw-binary launches
317		// (e.g. running `Target/release/<bin>` directly from a terminal)
318		// still resolve `STATIC_APPLICATION_ROOT` correctly. Without this
319		// fallback, release binaries launched outside `.app` had an
320		// empty static root, causing extension-contributed icons served
321		// via `vscode-file://` to 404 (GitLens / Roo / Claude side bar
322		// icons missing).
323		let SkyTargetDir = PathResolver
324			.resource_dir()
325			.ok()
326			.filter(|P| !P.as_os_str().is_empty() && P.exists())
327			.unwrap_or_else(|| {
328				let ExeParent = std::env::current_exe()
329					.ok()
330					.and_then(|Exe| Exe.parent().map(|P| P.to_path_buf()))
331					.unwrap_or_default();
332
333				// `.app/Contents/MacOS/<bin>` → `Contents/Resources/`
334				let BundleResources = ExeParent.join("../Resources");
335				if BundleResources.exists() {
336					return BundleResources;
337				}
338
339				// Monorepo layout: `Element/Mountain/Target/<profile>/<bin>` →
340				// `Element/Sky/Target/`. Used by both debug runs and raw-
341				// release launches from inside the repo.
342				let RepoSky = ExeParent.join("../../../Sky/Target");
343				if RepoSky.exists() {
344					return RepoSky;
345				}
346
347				// Last resort: alongside the binary. A broken bundle layout
348				// then surfaces as visible "asset not found" 404s instead of
349				// silent empty-string joins.
350				ExeParent
351			});
352
353		crate::IPC::WindServiceHandlers::Utilities::ApplicationRoot::set_static_application_root(
354			SkyTargetDir.to_string_lossy().to_string(),
355		);
356
357		dev_log!(
358			"lifecycle",
359			"[Lifecycle] [Dirs] Static application root: {}",
360			SkyTargetDir.display()
361		);
362
363		// Every directory VS Code may stat or readdir during startup
364		let Dirs = [
365			// User profile directories
366			AppDataDir.join("User"),
367			AppDataDir.join("User/globalStorage"),
368			AppDataDir.join("User/workspaceStorage"),
369			AppDataDir.join("User/workspaceStorage/vscode-chat-images"),
370			AppDataDir.join("User/extensions"),
371			AppDataDir.join("User/profiles/__default__profile__"),
372			AppDataDir.join("User/snippets"),
373			AppDataDir.join("User/prompts"),
374			AppDataDir.join("User/caches"),
375			// Configuration cache
376			AppDataDir.join("CachedConfigurations/defaults/__default__profile__-configurationDefaultsOverrides"),
377			// Log directories - VS Code stats {logsPath}/window1/output_{timestamp}
378			LogDir.join("window1"),
379			// System extensions directory - VS Code scans appRoot/../extensions
380			// which resolves to /Static/Application/extensions (mapped to Sky Target).
381			SkyTargetDir.join("Static/Application/extensions"),
382			// Agent directories VS Code probes for (create to avoid stat errors)
383			HomeDir.join(".claude/agents"),
384			HomeDir.join(".copilot/agents"),
385		];
386
387		for Dir in &Dirs {
388			if let Err(Error) = std::fs::create_dir_all(Dir) {
389				dev_log!(
390					"lifecycle",
391					"warn: [Lifecycle] [Dirs] Failed to create {}: {}",
392					Dir.display(),
393					Error
394				);
395			}
396		}
397
398		// Default empty files VS Code reads on startup
399		let DefaultFiles:&[(&std::path::Path, &str)] = &[
400			(&AppDataDir.join("User/settings.json"), "{}"),
401			(&AppDataDir.join("User/keybindings.json"), "[]"),
402			(&AppDataDir.join("User/tasks.json"), "{}"),
403			(&AppDataDir.join("User/extensions.json"), "[]"),
404			(&AppDataDir.join("User/mcp.json"), "{}"),
405		];
406
407		for (FilePath, DefaultContent) in DefaultFiles {
408			if !FilePath.exists() {
409				let _ = std::fs::write(FilePath, DefaultContent);
410			}
411		}
412
413		// Atom I7: ensure `security.workspace.trust.enabled: false` lives
414		// in User/settings.json. Without it, opening the Land repo as a
415		// workspace triggers VS Code's workspace-trust gate: built-in
416		// extensions whose `location` is inside the picked folder are
417		// marked `DisabledByTrustRequirement` (see
418		// `extensionEnablementService.ts:549`). Since our built-ins ship
419		// under `Element/Sky/Target/Static/Application/extensions/` -
420		// which IS inside the repo - any user picking the repo as a
421		// workspace hits this filter for every extension. Disabling the
422		// trust system wholesale is the correct Land-level policy; we're
423		// a personal editor, not a multi-user sandbox. Users can opt
424		// back in by flipping this key in their User/settings.json.
425		{
426			let SettingsPath = AppDataDir.join("User/settings.json");
427
428			let Current = std::fs::read_to_string(&SettingsPath).unwrap_or_else(|_| "{}".to_string());
429
430			if !Current.contains("\"security.workspace.trust.enabled\"") {
431				if let Ok(mut Parsed) = serde_json::from_str::<serde_json::Value>(&Current) {
432					if !Parsed.is_object() {
433						Parsed = serde_json::json!({});
434					}
435
436					if let Some(Obj) = Parsed.as_object_mut() {
437						Obj.insert("security.workspace.trust.enabled".to_string(), serde_json::Value::Bool(false));
438					}
439
440					if let Ok(Serialized) = serde_json::to_string_pretty(&Parsed) {
441						let _ = std::fs::write(&SettingsPath, Serialized);
442
443						dev_log!(
444							"lifecycle",
445							"[Lifecycle] [Dirs] Injected default 'security.workspace.trust.enabled=false' into {}",
446							SettingsPath.display()
447						);
448					}
449				}
450			}
451		}
452
453		// Set GlobalMementoPath now that we know the real Tauri app data dir
454		if let Ok(mut Path) = app_state.GlobalMementoPath.lock() {
455			*Path = AppDataDir.join("User/globalStorage/global.json");
456			dev_log!("lifecycle", "[Lifecycle] [Dirs] GlobalMementoPath: {}", Path.display());
457		}
458
459		dev_log!(
460			"lifecycle",
461			"[Lifecycle] [Dirs] Userdata directories ensured at {}",
462			AppDataDir.display()
463		);
464	}
465
466	// -------------------------------------------------------------------------
467	// [Backend] [Env] Mountain environment
468	// -------------------------------------------------------------------------
469	dev_log!("lifecycle", "[Backend] [Env] Creating MountainEnvironment...");
470
471	let Environment = Arc::new(MountainEnvironment::Create(app_handle_for_setup.clone(), app_state.clone()));
472
473	dev_log!("lifecycle", "[Backend] [Env] MountainEnvironment ready.");
474
475	// -------------------------------------------------------------------------
476	// [Backend] [Runtime] ApplicationRunTime
477	// -------------------------------------------------------------------------
478	dev_log!("lifecycle", "[Backend] [Runtime] Creating ApplicationRunTime...");
479
480	let Runtime = Arc::new(ApplicationRunTime::Create(scheduler.clone(), Environment.clone()));
481
482	app_handle_for_setup.manage(Runtime.clone());
483
484	dev_log!("lifecycle", "[Backend] [Runtime] ApplicationRunTime managed.");
485
486	// -------------------------------------------------------------------------
487	// [Lifecycle] [IPC] Initialize Status Reporter
488	// -------------------------------------------------------------------------
489	if let Err(e) = StatusReporterRegisterFn(&app_handle_for_setup, Runtime.clone()) {
490		dev_log!(
491			"lifecycle",
492			"error: [Lifecycle] [IPC] Failed to initialize status reporter: {}",
493			e
494		);
495	}
496
497	// -------------------------------------------------------------------------
498	// [Lifecycle] [IPC] Initialize Advanced Features
499	// -------------------------------------------------------------------------
500	if let Err(e) = AdvancedFeaturesRegisterFn(&app_handle_for_setup, Runtime.clone()) {
501		dev_log!(
502			"lifecycle",
503			"error: [Lifecycle] [IPC] Failed to initialize advanced features: {}",
504			e
505		);
506	}
507
508	// -------------------------------------------------------------------------
509	// [Lifecycle] [IPC] Initialize Wind Advanced Sync
510	// -------------------------------------------------------------------------
511	if let Err(e) = WindSyncRegisterFn(&app_handle_for_setup, Runtime.clone()) {
512		dev_log!(
513			"lifecycle",
514			"error: [Lifecycle] [IPC] Failed to initialize wind advanced sync: {}",
515			e
516		);
517	}
518
519	// -------------------------------------------------------------------------
520	// [Lifecycle] [PostSetup] Async initialization work
521	// -------------------------------------------------------------------------
522	let PostSetupAppHandle = app_handle_for_setup.clone();
523
524	let PostSetupEnvironment = Environment.clone();
525
526	tauri::async_runtime::spawn(async move {
527		dev_log!("lifecycle", "[Lifecycle] [PostSetup] Starting...");
528		let PostSetupStart = crate::IPC::DevLog::NowNano::Fn();
529		let AppStateForSetup = PostSetupEnvironment.ApplicationState.clone();
530		TraceStep!("[Lifecycle] [PostSetup] AppState cloned.");
531
532		// [Config]
533		// First-pass merge runs against the empty `ScannedExtensions`
534		// map (the scan happens later in this lifecycle). User /
535		// workspace `settings.json` overrides land here, but extension
536		// `contributes.configuration.properties[*].default` keys cannot
537		// be collected yet. Without a second pass after the scan,
538		// `getConfiguration('git').get('enabled')` returns undefined,
539		// vscode.git's `_activate` takes the `if (!enabled) return;`
540		// short-circuit, and the SCM viewlet stays empty even though
541		// Cocoon successfully activated the extension. The second pass
542		// below repairs this without disturbing the existing initial
543		// merge that the rest of bootstrap depends on.
544		let ConfigStart = crate::IPC::DevLog::NowNano::Fn();
545		let _ = ConfigurationInitializeFn(&PostSetupEnvironment).await;
546		crate::otel_span!("lifecycle:config:initialize", ConfigStart);
547
548		// [Workspace] [Trust] Desktop app - trust local workspace by default
549		AppStateForSetup.Workspace.SetTrustStatus(true);
550
551		// [Extensions] [ScanPaths]
552		let ExtScanStart = crate::IPC::DevLog::NowNano::Fn();
553		let _ = ScanPathConfigureFn(&AppStateForSetup);
554
555		// [Extensions] [Scan]
556		let _ = ExtensionPopulateFn(PostSetupAppHandle.clone(), &AppStateForSetup).await;
557		crate::otel_span!("lifecycle:extensions:scan", ExtScanStart);
558
559		// [Config] [Re-merge] - now that ScannedExtensions is populated,
560		// run the merge a second time so `collect_default_configurations`
561		// can walk extension manifests and seed `git.enabled = true`,
562		// `git.path = null`, `git.autoRepositoryDetection = true`, plus
563		// every other `contributes.configuration.properties[*].default`
564		// the 113 scanned extensions declare. The first-pass merge logged
565		// "0 top-level keys"; this pass should log a much larger count.
566		// User / workspace overrides applied during the first pass are
567		// preserved because the merge order is Default → User → Workspace
568		// and the cached User/Workspace JSON files are re-read each call.
569		let ConfigRemergeStart = crate::IPC::DevLog::NowNano::Fn();
570		let _ = ConfigurationInitializeFn(&PostSetupEnvironment).await;
571		crate::otel_span!("lifecycle:config:remerge-after-extension-scan", ConfigRemergeStart);
572
573		// [Vine] [gRPC]
574		let VineStart = crate::IPC::DevLog::NowNano::Fn();
575		let _ = VineStartFn(
576			PostSetupAppHandle.clone(),
577			"127.0.0.1:50051".to_string(),
578			"127.0.0.1:50052".to_string(),
579		)
580		.await;
581		crate::otel_span!("lifecycle:vine:start", VineStart);
582
583		// [Cocoon] [Sidecar] - skipped when Disable=true so the
584		// workbench loads without an extension host. Useful for
585		// bisecting whether typing-input regressions originate in
586		// Cocoon's gRPC handlers or upstream / Tauri / WKWebView.
587		if IsLandDisabled() {
588			dev_log!(
589				"cocoon",
590				"[Cocoon] [Start] Disable=true: Cocoon spawn SKIPPED (workbench will run without extensions)"
591			);
592		} else {
593			let CocoonStart = crate::IPC::DevLog::NowNano::Fn();
594			let _ = CocoonStartFn(&PostSetupAppHandle, &PostSetupEnvironment).await;
595			crate::otel_span!("lifecycle:cocoon:start", CocoonStart);
596		}
597
598		// [Air] [Sidecar] - daemon for updates / downloads / signing /
599		// indexing / system monitoring. Spawn parallel to Cocoon; both
600		// are sidecars in the Vine pool. AirStart returns Ok(()) even
601		// on spawn failure (graceful degradation - workbench works
602		// without Air, just without those background capabilities).
603		// Skipped under `Disable=true` for parity with Cocoon.
604		if IsLandDisabled() {
605			dev_log!("grpc", "[Air] [Start] Disable=true: Air spawn SKIPPED");
606		} else {
607			let AirStartT0 = crate::IPC::DevLog::NowNano::Fn();
608			let _ = AirStartFn(&PostSetupAppHandle, &PostSetupEnvironment).await;
609			crate::otel_span!("lifecycle:air:start", AirStartT0);
610		}
611
612		// [Lifecycle] [Phase] Advance Starting → Ready now that the gRPC
613		// server + Cocoon sidecar + extension scan have all finished. Wind's
614		// `TauriChannel("lifecycle").listen("onDidChangePhase")` subscribers
615		// fire so long-running services can start pulling.
616		AppStateForSetup.Feature.Lifecycle.AdvanceAndBroadcast(2, &PostSetupAppHandle);
617
618		// Schedule a background transition to Restored (3), then Eventually
619		// (4). Sky/Wind are the authoritative signal - they call
620		// `lifecycle:advancePhase` over Tauri IPC when the workbench is
621		// truly interactive (`Restored`) and when late-binding extensions
622		// should stop blocking (`Eventually`). `AdvanceAndBroadcast`
623		// rejects backwards/same-phase advances (LifecyclePhaseState.rs:53),
624		// so the timers below are pure fallbacks: if Sky has already driven
625		// the phase, these become no-ops and log nothing visible.
626		//
627		// The windows are deliberately generous - a debug-electron cold
628		// boot with 98 extensions has been observed to finish its
629		// `$activateByEvent("*")` burst at ~3.5 s on an M4 mini and
630		// noticeably later on older hardware. The previous 2 s / 5 s
631		// timings ran the risk of flipping Restored while the burst was
632		// still in flight, which prematurely unblocked services gated on
633		// "the editor is interactive". 8 s / 15 s keeps a safety margin
634		// without visibly delaying late-binding extensions that legitimately
635		// need Eventually to fire.
636		let LifecycleStateClone = AppStateForSetup.Feature.Lifecycle.clone();
637		let AppHandleForPhase = PostSetupAppHandle.clone();
638		tauri::async_runtime::spawn(async move {
639			tokio::time::sleep(tokio::time::Duration::from_millis(8_000)).await;
640			if LifecycleStateClone.GetPhase() < 3 {
641				dev_log!(
642					"lifecycle",
643					"[Lifecycle] [Fallback] Sky did not advance to Restored within 8s; Mountain auto-advancing \
644					 (current phase={})",
645					LifecycleStateClone.GetPhase()
646				);
647				LifecycleStateClone.AdvanceAndBroadcast(3, &AppHandleForPhase);
648			}
649			tokio::time::sleep(tokio::time::Duration::from_millis(15_000)).await;
650			if LifecycleStateClone.GetPhase() < 4 {
651				dev_log!(
652					"lifecycle",
653					"[Lifecycle] [Fallback] Sky did not advance to Eventually within 23s total; Mountain \
654					 auto-advancing (current phase={})",
655					LifecycleStateClone.GetPhase()
656				);
657				LifecycleStateClone.AdvanceAndBroadcast(4, &AppHandleForPhase);
658			}
659		});
660
661		// Hidden-until-ready safety timer: `WindowBuild.rs` creates the main
662		// window with `.visible(false)` and the `lifecycle:advancePhase(3)`
663		// handler reveals it once Sky reports the workbench DOM is attached.
664		// If Sky crashes before phase 3 reaches Mountain, the window would
665		// stay invisible forever. Force-reveal after 3 s so the user always
666		// sees SOMETHING even on a completely broken Sky. 3 s matches the
667		// observed p95 of `[Lifecycle] [Phase] Advance Ready` on a cold
668		// M-series boot, so the timer rarely fires on a healthy path.
669		let AppHandleForEmergencyShow = PostSetupAppHandle.clone();
670		tauri::async_runtime::spawn(async move {
671			tokio::time::sleep(tokio::time::Duration::from_millis(3_000)).await;
672			if let Some(MainWindow) = AppHandleForEmergencyShow.get_webview_window("main") {
673				if let Ok(false) = MainWindow.is_visible() {
674					dev_log!(
675						"lifecycle",
676						"warn: [Lifecycle] [Fallback] main window hidden at +3s; force-revealing to avoid an \
677						 invisible-window lockup (Sky never reached phase 3)"
678					);
679					let _ = MainWindow.show();
680					let _ = MainWindow.set_focus();
681				}
682			}
683		});
684
685		crate::otel_span!("lifecycle:postsetup:complete", PostSetupStart);
686		dev_log!("lifecycle", "[Lifecycle] [PostSetup] Complete. System ready.");
687	});
688
689	Ok(())
690}