All files / server/services/sandbox docker-manager.ts

80.76% Statements 126/156
72.91% Branches 70/96
86.95% Functions 20/23
82.19% Lines 120/146

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413                                    22x 22x         22x               1275x 1275x 933377x 933377x   932161x 933377x 933377x 59x   932102x                   59x 22x 37x 37x                                                                                         151x 151x           151x 151x 151x 151x 151x       1305x 1305x 1305x 2x 1305x 1305x 1305x               9x   9x 3x 3x 3x 3x     9x               7x 7x 2x 2x 2x 2x                         72x 72x   72x   72x 1303x   1303x   1302x         1275x   1275x 1275x 1216x 1216x 1216x     59x 59x 59x 59x 1275x 57x 57x   59x 59x     27x                 86x 86x 86x 86x 86x       69x 60x 69x                                       8x 8x 8x     8x 2x 1x 1x 1x       1x 1x 1x 1x   1x                     8x 2x 2x 2x                             8x 8x 8x     8x                 8x         8x     8x 5x       8x 3x     3x 5x 4x 4x       8x                           7x 7x 7x 3x 3x 3x 3x     7x         7x 7x   7x 5x 5x                           3x                                                                                  
/**
 * Docker-Manager: Manages Docker container lifecycle, setup, and event handling
 * Extracted from Etappe A: Docker-Lifecycle refactoring
 */
 
import type { IProcessController } from "../process-controller";
import type { ArduinoOutputParser, ParsedStderrOutput } from "../arduino-output-parser";
import { Logger } from "@shared/logger";
import type { SimulationTimeoutManager } from "../simulation-timeout-manager";
import { normalizeSimulationTimeout } from "@shared/input-limits";
import type { PinStateChange } from "@shared/types/arduino.types";
import { config } from "../../config";
import {
  appendCompilerDiagnostics,
  limitCompilerDiagnostics,
  type CompilerDiagnosticsBuffer,
} from "../compiler-diagnostics";
 
const RUNTIME_START_MARKER = "[[RUNTIME_START]]";
const IO_REGISTRY_MARKER = "[[IO_REGISTRY_START]]";
/**
 * Compile output kept between chunks for the marker search: enough for a marker
 * split across chunks plus the context that decides whether it counts.
 */
const MARKER_SCAN_CARRY = RUNTIME_START_MARKER.length - 1 + IO_REGISTRY_MARKER.length + 1;
 
/**
 * Finds the runtime marker in `output` starting at or after `minStart`; matches
 * before it were already rejected when their chunk arrived. Each call therefore
 * scans only new output plus a constant carry.
 */
function findRuntimeStartMarker(output: string, minStart = 0): { start: number; end: number } | null {
  let searchFrom = minStart;
  while (searchFrom < output.length) {
    const start = output.indexOf(RUNTIME_START_MARKER, searchFrom);
    if (start < 0) return null;
 
    const isLineStart = start === 0 || output[start - 1] === "\n";
    const followsRegistryMarker = output.startsWith(IO_REGISTRY_MARKER, start - IO_REGISTRY_MARKER.length);
    if (isLineStart || followsRegistryMarker) {
      return { start, end: start + RUNTIME_START_MARKER.length };
    }
    searchFrom = start + 1;
  }
  return null;
}
 
/**
 * Drops the part of the latest chunk that follows the marker start from the
 * diagnostics; a marker that began in the carry also removes its already-kept prefix.
 */
function keepDiagnosticsBeforeMarker(buffer: CompilerDiagnosticsBuffer, scanned: string, carryLength: number, markerStart: number): void {
  if (markerStart >= carryLength) {
    appendCompilerDiagnostics(buffer, scanned.slice(carryLength, markerStart));
  } else Eif (!buffer.omittedChars) {
    buffer.value = buffer.value.slice(0, buffer.value.length - (carryLength - markerStart));
  }
}
 
interface DockerManagerCallbacks {
  onOutput: (line: string, isComplete?: boolean) => void;
  onPinState: (pin: number, type: PinStateChange, value: number) => void;
  onError: (line: string) => void;
}
 
interface DockerProcessConfig {
  flushBatchers: () => void;
  flushMessageQueue: () => void;
  /** Use a getter so the guard reflects the live value, preventing stale-capture bugs. */
  getProcessKilled: () => boolean;
  executionTimeout?: number;
  onStateTransition?: (state: "running" | "stopped") => void;
}
 
interface DockerEventHandlers {
  onCompileError?: (error: string) => void;
  onCompileSuccess?: () => void;
  onRuntimeStart?: () => void;
  onExit?: (code: number | null) => void;
}
 
interface DockerHandlerState {
  isCompilePhase: { value: boolean };
  compileErrorBuffer: CompilerDiagnosticsBuffer;
  compileSuccessSent: { value: boolean };
  totalOutputBytes: { value: number };
  processStartTime: number | null;
  stderrFallbackBuffer: string;
  runtimeOutputBuffer: { value: string };
  flushTimer: NodeJS.Timeout | null;
}
 
type HandleParsedLineDelegate = (parsed: ParsedStderrOutput, callbacks: DockerManagerCallbacks) => void;
type OutputBudgetState = Pick<DockerHandlerState, "totalOutputBytes">;
type StdoutHandlerState = Pick<DockerHandlerState, "isCompilePhase" | "compileErrorBuffer" | "compileSuccessSent" | "totalOutputBytes"> & Partial<Pick<DockerHandlerState, "processStartTime" | "runtimeOutputBuffer">>;
type StderrHandlerState = Pick<DockerHandlerState, "isCompilePhase" | "compileErrorBuffer" | "totalOutputBytes" | "stderrFallbackBuffer"> & Partial<Pick<DockerHandlerState, "processStartTime">>;
type DockerExitState = Pick<DockerHandlerState, "isCompilePhase" | "compileErrorBuffer" | "compileSuccessSent" | "stderrFallbackBuffer"> & Partial<Pick<DockerHandlerState, "processStartTime" | "runtimeOutputBuffer">>;
type DockerRuntimeState = Pick<DockerHandlerState, "isCompilePhase" | "compileErrorBuffer" | "compileSuccessSent" | "totalOutputBytes" | "stderrFallbackBuffer"> & Partial<Pick<DockerHandlerState, "processStartTime" | "flushTimer" | "runtimeOutputBuffer">>;
 
export class DockerManager {
  private readonly logger = new Logger("DockerManager");
  private readonly SANDBOX_CONFIG = {
    maxOutputBytes: config.sandbox.resources.maxOutputBytes, // Max 100MB output
    maxExecutionTimeSec: config.sandbox.resources.maxExecutionTimeSec, // Max 60 seconds runtime
  };
 
  constructor(
    private readonly processController: IProcessController,
    private readonly stderrParser: ArduinoOutputParser,
    private readonly timeoutManager: SimulationTimeoutManager,
    private readonly handleParsedLine: HandleParsedLineDelegate,
    private readonly onTerminationRequested?: () => void,
  ) {}
 
  private consumeOutputBudget(state: OutputBudgetState, data: Buffer | string, callbacks: DockerManagerCallbacks): boolean {
    const counter = state.totalOutputBytes;
    counter.value += Buffer.byteLength(data);
    if (counter.value <= this.SANDBOX_CONFIG.maxOutputBytes) return true;
    this.onTerminationRequested?.();
    this.processController.kill("SIGKILL");
    callbacks.onError("Output size limit exceeded");
    return false;
  }
 
  /**
   * Start the simulation runtime timeout after the RUNTIME_START marker.
   * Compilation and sandbox startup are protected by the separate startup watchdog.
   */
  setupDockerTimeout(executionTimeout: number | undefined, callbacks: DockerManagerCallbacks): void {
    const timeoutSec = normalizeSimulationTimeout(executionTimeout);
 
    const handleTimeout = () => {
      this.onTerminationRequested?.();
      this.processController.kill("SIGKILL");
      callbacks.onOutput(`--- Simulation timeout (${timeoutSec}s) ---`, true);
      this.logger.info(`Docker runtime timeout after ${timeoutSec}s`);
    };
 
    this.timeoutManager.schedule(timeoutSec * 1000, handleTimeout);
  }
 
  /**
   * Protect the compile/startup phase until RUNTIME_START is observed.
   * This is an internal guard and is intentionally independent of timeoutSec.
   */
  private setupDockerStartupTimeout(callbacks: DockerManagerCallbacks): void {
    const startupTimeoutSec = this.SANDBOX_CONFIG.maxExecutionTimeSec;
    this.timeoutManager.schedule(startupTimeoutSec * 1000, () => {
      this.onTerminationRequested?.();
      this.processController.kill("SIGKILL");
      callbacks.onOutput(`--- Sandbox startup timeout (${startupTimeoutSec}s) ---`, true);
      this.logger.warn(`Docker sandbox startup timeout after ${startupTimeoutSec}s`);
    });
  }
 
  /**
   * Setup Docker stdout handler (detects end of compile phase, parses output)
   */
  setupStdoutHandler(
    callbacks: DockerManagerCallbacks,
    state: StdoutHandlerState,
    onCompileSuccess?: () => void,
    onRuntimeStart?: () => void,
  ): void {
    const isCompilePhase = state.isCompilePhase;
    const compileSuccessSent = state.compileSuccessSent;
    // The last compile output, kept so a marker split across chunks is still found.
    let scanCarry = "";
 
    this.processController.onStdout((data) => {
      const str = data.toString();
 
      if (!this.consumeOutputBudget(state, data, callbacks)) return;
 
      if (isCompilePhase.value) {
        // g++ stderr is redirected to stdout by the Docker command. Keep all
        // compile output out of the runtime stream and wait for the explicit
        // sentinel before declaring compilation successful. Diagnostics are
        // bounded and the marker search only looks at new output.
        const scanned = scanCarry + str;
        // Only a marker that ends in the new chunk can be new.
        const markerMatch = findRuntimeStartMarker(scanned, Math.max(0, scanCarry.length - RUNTIME_START_MARKER.length + 1));
        if (!markerMatch) {
          appendCompilerDiagnostics(state.compileErrorBuffer, str);
          scanCarry = scanned.slice(-MARKER_SCAN_CARRY);
          return;
        }
 
        const runtimeOutput = scanned.slice(markerMatch.end).replace(/^\r?\n/, "");
        keepDiagnosticsBeforeMarker(state.compileErrorBuffer, scanned, scanCarry.length, markerMatch.start);
        isCompilePhase.value = false;
        onRuntimeStart?.();
        if (!compileSuccessSent.value && onCompileSuccess) {
          compileSuccessSent.value = true;
          onCompileSuccess();
        }
        this.forwardRuntimeStdout(runtimeOutput, state, callbacks);
        return;
      }
 
      this.forwardRuntimeStdout(str, state, callbacks);
    });
  }
 
  private forwardRuntimeStdout(
    output: string,
    state: Pick<StdoutHandlerState, "processStartTime" | "runtimeOutputBuffer">,
    callbacks: DockerManagerCallbacks,
  ): void {
    const runtimeOutputBuffer = state.runtimeOutputBuffer ?? { value: "" };
    const lines = `${runtimeOutputBuffer.value}${output}`.split(/\r?\n/);
    runtimeOutputBuffer.value = lines.pop() ?? "";
    state.runtimeOutputBuffer = runtimeOutputBuffer;
    lines.forEach((line) => {
      // Filter the compile-phase sentinel added by buildCompileAndRunCommand.
      // Its sole purpose is to trigger the isCompilePhase reset above and
      // must not be forwarded to the protocol parser or the client.
      if (!line || line === RUNTIME_START_MARKER) return;
      const parsed = this.stderrParser.parseStderrLine(line, state.processStartTime || 0);
      this.handleParsedLine(parsed, callbacks);
    });
  }
 
  private flushRuntimeStdout(
    state: Pick<StdoutHandlerState, "processStartTime" | "runtimeOutputBuffer">,
    callbacks: DockerManagerCallbacks,
  ): void {
    const runtimeOutputBuffer = state.runtimeOutputBuffer;
    if (!runtimeOutputBuffer?.value) return;
    this.forwardRuntimeStdout("\n", state, callbacks);
  }
 
  /**
   * Setup Docker stderr handlers (raw + fallback + readline)
   */
  setupStderrHandlers(
    callbacks: DockerManagerCallbacks,
    state: StderrHandlerState,
  ): void {
    const isCompilePhase = state.isCompilePhase;
    const compileErrorBuffer = state.compileErrorBuffer;
    const useFallbackParser = !this.processController.supportsStderrLineStreaming();
 
    // Raw stderr stream for compile aggregation
    this.processController.onStderr((data) => {
      if (!this.consumeOutputBudget(state, data, callbacks)) return;
      const chunk = data.toString();
      Eif (isCompilePhase.value && useFallbackParser) {
        appendCompilerDiagnostics(compileErrorBuffer, chunk);
      }
 
      // Fallback parsing when readline is unavailable
      Eif (useFallbackParser) {
        state.stderrFallbackBuffer = (state.stderrFallbackBuffer || "") + chunk;
        const lines = state.stderrFallbackBuffer.split(/\r?\n/);
        state.stderrFallbackBuffer = lines.pop() || "";
 
        Iif (!isCompilePhase.value) {
          for (const line of lines) {
            if (!line) continue;
            const parsed = this.stderrParser.parseStderrLine(line, state.processStartTime || 0);
            this.handleParsedLine(parsed, callbacks);
          }
        }
      }
    });
 
    // Readline-based stderr line stream (preferred when available)
    this.processController.onStderrLine((line) => {
      Iif (line.length === 0 || isCompilePhase.value) return;
      const parsed = this.stderrParser.parseStderrLine(line, state.processStartTime || 0);
      this.handleParsedLine(parsed, callbacks);
    });
  }
 
  /**
   * Handle Docker process exit (cleanup, final parsing, callbacks)
   */
   
  handleDockerExit(
    callbacks: DockerManagerCallbacks,
    state: DockerExitState,
    code: number | null,
    config: DockerProcessConfig,
    handlers: DockerEventHandlers,
  ): void {
    const isCompilePhase = state.isCompilePhase;
    const compileErrorBuffer = state.compileErrorBuffer;
    const useFallbackParser = !this.processController.supportsStderrLineStreaming();
 
    // Flush any remaining data in stderr fallback buffer
    Iif (state.stderrFallbackBuffer && useFallbackParser && !isCompilePhase.value) {
      const buffered = state.stderrFallbackBuffer;
      state.stderrFallbackBuffer = "";
      if (buffered.trim()) {
        const parsed = this.stderrParser.parseStderrLine(buffered, state.processStartTime || 0);
        this.handleParsedLine(parsed, callbacks);
      }
    }
 
    Iif (!isCompilePhase.value) {
      this.flushRuntimeStdout(state, callbacks);
    }
 
    // Flush message queue before exit
    config.flushMessageQueue();
 
    // Flush batchers if not still in compile phase
    if (!isCompilePhase.value || code === 0) {
      config.flushBatchers();
    }
 
    // Report compile errors or success
    if (code !== 0 && isCompilePhase.value && handlers.onCompileError) {
      const compileError = compileErrorBuffer.value
        ? limitCompilerDiagnostics(compileErrorBuffer.value, compileErrorBuffer.omittedChars)
        : `Simulation build failed (exit code ${code}).`;
      handlers.onCompileError(this.cleanCompilerErrors(compileError));
    } else if (code === 0 && handlers.onCompileSuccess && !state.compileSuccessSent.value) {
      state.compileSuccessSent.value = true;
      handlers.onCompileSuccess();
    }
 
    // Call exit callback (guard: only if process wasn't terminated by stop())
    if (!config.getProcessKilled() && handlers.onExit) handlers.onExit(code);
  }
 
  /**
   * Setup all Docker handlers (timeout, stdout, stderr, close)
   */
   
  setupDockerHandlers(
    callbacks: DockerManagerCallbacks,
    state: DockerRuntimeState,
    config: DockerProcessConfig,
    handlers: DockerEventHandlers,
  ): void {
    // Keep startup protection active until the first valid runtime marker.
    this.setupDockerStartupTimeout(callbacks);
    let runtimeTimeoutStarted = false;
    const handleRuntimeStart = () => {
      Iif (runtimeTimeoutStarted) return;
      runtimeTimeoutStarted = true;
      this.setupDockerTimeout(config.executionTimeout, callbacks);
      handlers.onRuntimeStart?.();
    };
 
    this.processController.onError((err) => {
      this.logger.error(`Docker process error: ${err.message}`);
      callbacks.onError(`Docker process failed: ${err.message}`);
    });
 
    this.setupStdoutHandler(callbacks, state, handlers.onCompileSuccess, handleRuntimeStart);
    this.setupStderrHandlers(callbacks, state);
 
    this.processController.onClose((code) => {
      this.timeoutManager.clear();
      this.handleDockerExit(
        callbacks,
        state,
        code,
        config,
        handlers,
      );
    });
  }
 
  /**
   * Clean up compiler error messages
   */
  private cleanCompilerErrors(errors: string): string {
    return errors.replaceAll("/sandbox/sketch.cpp", "sketch.ino").replaceAll(/(?:\/[^\s:/]+)+\/temp\/[a-f0-9-]+\/sketch\.cpp/gi, "sketch.ino").trim();
  }
 
  /**
   * Complete Docker orchestration: spawn process and setup all handlers
   * This consolidates runInDocker + setupDockerHandlers into a single delegated call
   */
   
  async runInDockerWithHandlers(
    dockerArgs: string[],
    callbacks: DockerManagerCallbacks,
    state: DockerRuntimeState,
    config: DockerProcessConfig,
    handlers: DockerEventHandlers,
  ): Promise<void> {
    try {
      // Clear listeners from previous run before spawning new process
      this.processController.clearListeners();
 
      // Spawn Docker process
      await this.processController.spawn("docker", dockerArgs);
      this.logger.info("🚀 Docker: Compile + Run in single container");
      
      // Record process start time and transition to running
      state.processStartTime = Date.now();
      config.onStateTransition?.("running");
 
      // Setup all handlers for Docker process
      this.setupDockerHandlers(
        callbacks,
        state,
        config,
        handlers,
      );
    } catch (err) {
      this.logger.error(`Docker process spawn failed: ${err instanceof Error ? err.message : String(err)}`);
      config.onStateTransition?.("stopped");
      throw err;
    }
  }
}