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
FileSystemNotFounderror - If Node.js cannot be spawned, returns
IPCError - If gRPC connection fails, returns
IPCErrorwith context
§Module Contents
InitializeCocoon: Main entry point for Cocoon initializationLaunchAndManageCocoonSideCar: 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 🔒 - Cocoon
Process 🔒State - Global state for tracking Cocoon process lifecycle
Constants§
- BOOTSTRAP_
SCRIPT_ 🔒PATH - COCOON_
BUNDLE_ 🔒PROBE - Relative path from the resolved Cocoon package root to the bundled
entry module. Used by the pre-flight guard below to fail fast with
an actionable error when the bundle is missing (esbuild failure,
partial rm -rf, freshly cloned checkout without
pnpm run prepublishOnly, etc.) instead of spawning Node into a dying require() chain. - COCOON_
GRPC_ 🔒PORT - COCOON_
SIDE_ 🔒CAR_ IDENTIFIER - Configuration constants for Cocoon process management
- GRPC_
CONNECT_ 🔒BUDGET_ MS - GRPC_
CONNECT_ 🔒INITIAL_ MS - Exponential-backoff retry parameters for the Mountain → Cocoon gRPC handshake. Replaces the previous “20 × 1000 ms fixed poll” which under-probed the common race (Cocoon’s stage2 binds the port at ~200 ms so attempts 1-2 fail and we sat idle through 18 more whole- second sleeps) and over-waited the real failure (when Cocoon is genuinely dead, we wasted 20 s before reporting).
- GRPC_
CONNECT_ 🔒MAX_ DELAY_ MS - HANDSHAKE_
TIMEOUT_ 🔒MS - HEALTH_
CHECK_ 🔒INTERVAL_ SECONDS - MAX_
RESTART_ 🔒ATTEMPTS - MOUNTAIN_
GRPC_ 🔒PORT - RESTART_
WINDOW_ 🔒SECONDS
Statics§
- COCOON_
PID 🔒 - Last-known PID of the Cocoon child process. Mirrored here so callers can
read it without taking the async
COCOON_STATEmutex (e.g. from IPC handlers such asextensionHostStarter:start). Set after spawn and cleared on shutdown.0means “not running”.
Functions§
- Find
Matching 🔒Workspace Contains Patterns - Return the subset of
Patternsfor which at least one workspace folder contains a matching file or directory. Patterns are interpreted the same way VS Code does forworkspaceContains:<pattern>activation events: - GetCocoon
Pid - Return the Cocoon child process’s OS PID, or
Noneif Cocoon has not been spawned (or has exited). - Hard
Kill Cocoon - Atom I6: post-shutdown hard-kill. Called by RuntimeShutdown after the
$shutdowngRPC notification has been sent (and either succeeded or timed out). Grabs the storedChildhandle and force-terminates it if still alive, then resets COCOON_STATE. This plugs the “Mountain exits cleanly but child stays running” leak that leads to zombie-Cocoon zombies holding the gRPC port. - Initialize
Cocoon - The main entry point for initializing the Cocoon sidecar process manager.
- Launch
AndManage 🔒Cocoon Side Car - Spawns the Cocoon process, manages its communication channels, and performs the complete initialization handshake sequence.
- Pattern
Matches 🔒AnyEntry - Very small glob-matcher scoped to VS Code
workspaceContains:syntax. Supports literal paths,*(one path segment), and**(zero or more segments). Case-sensitive per the VS Code spec. - Segment
Match 🔒 - Single
Segment 🔒Match - Sweep
Stale 🔒Cocoon - Atom I6: pre-boot sweep. TCP-probe the Cocoon gRPC port and kill any stale process still bound to it. Prevents the EADDRINUSE cascade that leaves the extension host in degraded mode when a prior Mountain exited without cleaning up its child.
- monitor_
cocoon_ 🔒health_ task - Background task that monitors Cocoon process health and logs crashes.