Module CocoonManagement

Module CocoonManagement 

Source
Expand description

§Cocoon Management

This module provides comprehensive lifecycle management for the Cocoon sidecar process, which serves as the VS Code extension host within the Mountain editor.

§Overview

Cocoon is a Node.js-based process that provides compatibility with VS Code extensions. This module handles:

  • Process Spawning: Launching Node.js with the Cocoon bootstrap script
  • Environment Configuration: Setting up environment variables for IPC and logging
  • Communication Setup: Establishing gRPC/Vine connections on port 50052
  • Health Monitoring: Tracking process state and handling failures
  • Lifecycle Management: Graceful shutdown and restart capabilities
  • IO Redirection: Capturing stdout/stderr for logging and debugging

§Process Communication

The Cocoon process communicates via:

  • gRPC on port 50052 (configured via MOUNTAIN_GRPC_PORT/COCOON_GRPC_PORT)
  • Vine protocol for cross-process messaging
  • Standard streams for logging (VSCODE_PIPE_LOGGING)

§Dependencies

  • scripts/cocoon/bootstrap-fork.js: Bootstrap script for launching Cocoon
  • Node.js runtime: Required for executing Cocoon
  • Vine gRPC server: Must be running on port 50051 for handshake

§Error Handling

The module provides graceful degradation:

  • If the bootstrap script is missing, returns FileSystemNotFound error
  • If Node.js cannot be spawned, returns IPCError
  • If gRPC connection fails, returns IPCError with context

§Module Contents

  • InitializeCocoon: Main entry point for Cocoon initialization
  • LaunchAndManageCocoonSideCar: Process spawning and lifecycle management

§Example

use crate::Source::ProcessManagement::CocoonManagement::InitializeCocoon;

// Initialize Cocoon with application handle and environment
match InitializeCocoon(&app_handle, &environment).await {
	Ok(()) => println!("Cocoon initialized successfully"),
	Err(e) => eprintln!("Cocoon initialization failed: {:?}", e),
}

Structs§

COCOON_HEALTH 🔒
COCOON_STATE 🔒
CocoonProcessState 🔒
Global state for tracking Cocoon process lifecycle

Constants§

BOOTSTRAP_SCRIPT_PATH 🔒
COCOON_GRPC_PORT 🔒
COCOON_SIDE_CAR_IDENTIFIER 🔒
Configuration constants for Cocoon process management
GRPC_SERVER_READY_DELAY_MS 🔒
HANDSHAKE_TIMEOUT_MS 🔒
HEALTH_CHECK_INTERVAL_SECONDS 🔒
MAX_RESTART_ATTEMPTS 🔒
MOUNTAIN_GRPC_PORT 🔒
RESTART_WINDOW_SECONDS 🔒

Functions§

InitializeCocoon
The main entry point for initializing the Cocoon sidecar process manager.
LaunchAndManageCocoonSideCar 🔒
Spawns the Cocoon process, manages its communication channels, and performs the complete initialization handshake sequence.
monitor_cocoon_health_task 🔒
Background task that monitors Cocoon process health and logs crashes