All files / server/routes simulation.ws.ts

85.89% Statements 274/319
74.58% Branches 135/181
83.01% Functions 44/53
88.17% Lines 261/296

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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971                                                                                        6x         6x                   2x 1x         1x                             92x 92x 3x   92x       5x                 5x 5x 3x 5x                 57x               1x       1x 1x   1x 1x                     9x 9x 9x 9x 9x 9x 8x 1x 1x   7x 7x 7x 7x       7x                               2x 2x 2x 2x 2x 2x 2x       2x 2x 2x 2x 2x       2x                               3x         3x                       20x       20x                                                                     47x 47x 47x     47x                                 47x 47x 26x 12x 1x 1x   11x 11x     47x 47x 47x         47x         47x           19x                       78x   78x 52x         78x 10000x 10000x 10000x           78x     28x   28x 28x 28x 28x 28x           28x     28x   28x         28x         28x                 78x 1x 1x       1x       1x       1x 1x 1x                   1x     78x 26x           78x         78x                 78x                                                                                     78x                               78x                     78x                                                   81x 81x 81x 81x 79x       81x 81x   2x   2x   1x 2x           1x 1x         1x         1x   79x 79x     1x 1x   78x 78x     78x                       78x           78x                         92x     90x 5x 5x       85x 85x     92x 1x 1x       1x               1x             84x         92x 1x       1x       1x     83x 83x 2x 2x   81x 81x                       92x 92x 81x     81x   81x   92x 14x     14x                 81x 78x     78x       78x                 78x     78x 78x 78x 78x 26x     26x     78x 92x     92x     92x 92x                                 75x   3x   3x 2x         2x             2x     57x   56x 56x       56x 56x 26x 26x                     2x 2x 1x 1x 1x       1x                           19x 19x 19x 17x   19x       19x           47x   47x   147x     2x 19x 9x 2x           47x 85x 85x       85x 85x 85x 85x 85x   85x 85x     85x 2x         85x                     85x   85x   85x     85x   85x         85x               85x 85x   85x 85x 85x 329x 181x 181x 1x   181x   148x     85x 85x     85x   85x         85x 1x     1x   1x                         2x 2x 3x 3x 3x     3x 3x 3x       3x   3x 2x       2x 3x 3x 2x     47x    
import { WebSocketServer, WebSocket } from "ws";
import { createHash } from "node:crypto";
import type { Server } from "node:http";
import type { SandboxRunner } from "../services/sandbox-runner";
import {
  type IOPinRecord,
  type ClientToServerWSMessage,
  type WSMessage,
  WSMessageType,
} from "@shared/schema";
import type { Logger } from "@shared/logger";
import type { PinStateChange } from "@shared/types/arduino.types";
import { getSandboxRunnerPool } from "../services/sandbox-runner-pool";
import path from "node:path";
import { writeFile, access } from "node:fs/promises";
import {
  createWebSocketAuthorizationVerifier,
  getRequestAuthorization,
  type TrustConfig,
} from "../security/access-control";
import { INPUT_LIMITS } from "@shared/input-limits";
import { WsMessageRouter } from "./simulation/ws-message-router";
import {
  type ClientState,
  hasSimulationLifecycle,
  WsSessionManager,
} from "./simulation/ws-session-manager";
import { sendMessageToClient, WsOutputBuffer } from "./simulation/ws-output-buffer";
import { WEBSOCKET_PROTOCOL_VERSION } from "../services/protocol-version";
import {
  AdmissionResult,
  SimulationAdmissionController,
  SimulationReservation,
} from "../services/simulation-admission-controller";
import { operationError, SYSTEM_BUSY_MESSAGE } from "@shared/operation-errors";
import { config } from "../config";
import { InboundMessageLimiter } from "./simulation/ws-inbound-limiter";
import { safeErrorLogMetadata } from "../services/safe-error-log-metadata";
import { mapWithConcurrency, settleWithin } from "../services/concurrency";
 
function sendStartError(
  ws: WebSocket,
  error: ReturnType<typeof operationError>,
): void {
  sendMessageToClient(ws, {
    type: WSMessageType.OPERATION_ERROR,
    operation: "start_simulation",
    ...error,
  });
  sendMessageToClient(ws, {
    type: WSMessageType.SIMULATION_STATUS,
    status: "stopped",
  });
}
 
function rejectAdmission(
  ws: WebSocket,
  admission: Extract<AdmissionResult, { admitted: false }>,
): void {
  if (admission.reason === "capacity") {
    sendStartError(
      ws,
      operationError("SYSTEM_BUSY", SYSTEM_BUSY_MESSAGE, 5),
    );
  } else {
    sendStartError(
      ws,
      operationError(
        "RATE_LIMITED",
        "Rate-Limit erreicht: Pro Nutzer sind höchstens 5 laufende oder wartende Simulationen erlaubt.",
      ),
    );
  }
}
 
/**
 * Waits until no earlier lifecycle of this connection is being released.
 * Returns false when a stop, code change or disconnect arrived meanwhile.
 */
async function waitForPreviousRelease(clientState: ClientState): Promise<boolean> {
  const generation = clientState.startGeneration ?? 0;
  while (clientState.releasing) {
    await clientState.releasing.catch(() => undefined);
  }
  return !clientState.closed && (clientState.startGeneration ?? 0) === generation;
}
 
function rejectActiveLifecycle(ws: WebSocket, clientState: ClientState): void {
  sendMessageToClient(ws, {
    type: WSMessageType.OPERATION_ERROR,
    operation: "start_simulation",
    ...operationError(
      "SIMULATION_ALREADY_ACTIVE",
      "In dieser Sitzung läuft bereits eine Simulation oder wartet auf den Start. Bitte zuerst stoppen.",
    ),
  });
  // Resynchronise the client with the run that keeps going.
  let status: "paused" | "running" | "queued" = "queued";
  if (clientState.isPaused) status = "paused";
  else if (clientState.isRunning) status = "running";
  sendMessageToClient(ws, { type: WSMessageType.SIMULATION_STATUS, status });
}
 
/** Whether a start continuation still owns the connection's lifecycle. */
function ownsLifecycle(
  clientState: ClientState,
  reservation: SimulationReservation,
  runner: SandboxRunner,
): boolean {
  return clientState.reservation === reservation && clientState.runner === runner;
}
 
/** Preserve the existing serial diagnostic and stopped-status protocol. */
async function releaseFailedControl(
  ws: WebSocket, clientState: ClientState, sessionManager: WsSessionManager,
  operation: "pause" | "resume",
): Promise<void> {
  sendMessageToClient(ws, {
    type: WSMessageType.SERIAL_OUTPUT,
    data: `[ERR] Docker ${operation} failed; simulation stopped.\n`,
  });
  const reservation = clientState.reservation;
  await sessionManager.safeReleaseRunner(clientState, `${operation}_failed`, reservation);
  // A release can finish after a new run has already reserved this session.
  Iif (clientState.reservation && clientState.reservation !== reservation) return;
  sendMessageToClient(ws, { type: WSMessageType.SIMULATION_STATUS, status: "stopped" });
}
 
/**
 * Handle "pause_simulation" WebSocket message
 */
async function handlePauseSimulation(
  _ws: WebSocket,
  clientState: ClientState,
  sessionManager: WsSessionManager,
): Promise<void> {
  Eif (clientState?.runner && clientState.isRunning) {
    const runner = clientState.runner;
    const reservation = clientState.reservation;
    Iif (!runner.pause()) return;
    const paused = runner.controlResult ? await runner.controlResult : true;
    if (clientState.runner !== runner || clientState.reservation !== reservation) return;
    if (!paused) {
      await releaseFailedControl(_ws, clientState, sessionManager, "pause");
      return;
    }
    Eif (paused) {
      clientState.isPaused = true;
      sessionManager.markSessionPaused(clientState);
      sendMessageToClient(_ws, {
        type: WSMessageType.SIMULATION_STATUS,
        status: "paused",
      });
      sendMessageToClient(_ws, {
        type: WSMessageType.SERIAL_OUTPUT,
        data: "--- Simulation paused ---\n",
      });
    }
  }
}
 
/**
 * Handle "resume_simulation" WebSocket message
 */
async function handleResumeSimulation(
  _ws: WebSocket,
  clientState: ClientState,
  sessionManager: WsSessionManager,
): Promise<void> {
  Eif (clientState?.runner && clientState.isPaused) {
    const runner = clientState.runner;
    const reservation = clientState.reservation;
    Iif (!runner.resume()) return;
    const resumed = runner.controlResult ? await runner.controlResult : true;
    Iif (clientState.runner !== runner || clientState.reservation !== reservation) return;
    Iif (!resumed) {
      await releaseFailedControl(_ws, clientState, sessionManager, "resume");
      return;
    }
    Eif (resumed) {
      clientState.isPaused = false;
      clientState.isRunning = true;
      sessionManager.markSessionRunning(clientState);
      sendMessageToClient(_ws, {
        type: WSMessageType.SIMULATION_STATUS,
        status: "running",
      });
      sendMessageToClient(_ws, {
        type: WSMessageType.SERIAL_OUTPUT,
        data: "--- Simulation resumed ---\n",
      });
    }
  }
}
 
/**
 * Handle "serial_input" WebSocket message
 */
function handleSerialInput(
  _ws: WebSocket,
  data: Extract<ClientToServerWSMessage, { type: "serial_input" }>,
  clientState: ClientState,
): void {
  Eif (
    clientState?.runner &&
    clientState?.isRunning &&
    !clientState.isPaused
  ) {
    clientState.runner.sendSerialInput(data.data);
  }
}
 
/**
 * Handle "set_pin_value" WebSocket message
 */
function handleSetPinValue(
  _ws: WebSocket,
  data: Extract<ClientToServerWSMessage, { type: "set_pin_value" }>,
  clientState: ClientState,
): void {
  Eif (
    clientState?.runner &&
    (clientState.isRunning || clientState.isPaused)
  ) {
    clientState.runner.setPinValue(data.pin, data.value);
  }
}
 
type SimulationDeps = {
  SandboxRunner: typeof SandboxRunner;
  getSimulationRateLimiter: () => {
    checkLimit: (identity: string) => {
      allowed: boolean;
      retryAfter?: number;
    };
  };
  getSimulationAdmissionController?: () => SimulationAdmissionController;
  shouldSendSimulationEndMessage: (compileFailed: boolean) => boolean;
  getLastCompiledCode: (subject: string) => string | null;
  logger: Logger;
  runnerPool?: ReturnType<typeof getSandboxRunnerPool>;
  trust: TrustConfig;
  allowedWebSocketOrigins: readonly string[];
  disableRateLimit: boolean;
  heartbeatIntervalMs?: number;
  inboundMessageLimit?: { perSecond: number; burst: number };
};
 
// Return type exposes a small API used by other modules (test-reset)
export function registerSimulationWebSocket(
  httpServer: Server,
  deps: SimulationDeps,
) {
  const {
    getSimulationRateLimiter,
    shouldSendSimulationEndMessage,
    getLastCompiledCode,
    logger,
    runnerPool,
  } = deps;
  const pool = runnerPool ?? getSandboxRunnerPool();
  const admissionController = deps.getSimulationAdmissionController?.() ??
    new SimulationAdmissionController();
 
  const wss = new WebSocketServer({
    server: httpServer,
    path: "/ws",
    maxPayload: INPUT_LIMITS.webSocket.maxPayloadBytes,
    // Disable per-message compression to eliminate zlib concurrency bottleneck.
    // With 200+ simultaneous clients, the default concurrencyLimit:10 caused CPU
    // starvation during pin-state bursts; disabling deflate entirely removes that
    // constraint at the cost of slightly higher bandwidth (tolerable on LAN).
    perMessageDeflate: false,
    verifyClient: createWebSocketAuthorizationVerifier(
      deps.trust,
      deps.allowedWebSocketOrigins,
    ),
  });
 
  // Half-open connections (closed laptop, dropped Wi-Fi) would otherwise keep
  // their runner and admission until the simulation timeout.
  const answeredPing = new WeakMap<WebSocket, boolean>();
  const heartbeat = setInterval(() => {
    for (const client of wss.clients) {
      if (answeredPing.get(client) === false) {
        client.terminate();
        continue;
      }
      answeredPing.set(client, false);
      client.ping();
    }
  }, deps.heartbeatIntervalMs ?? config.server.webSocketHeartbeatIntervalMs);
  heartbeat.unref?.();
  wss.on("close", () => clearInterval(heartbeat));
  const inboundLimit = deps.inboundMessageLimit ?? {
    perSecond: config.server.webSocketInboundMessagesPerSecond,
    burst: config.server.webSocketInboundMessageBurst,
  };
 
  const sessionManager = new WsSessionManager({
    pool,
    logger,
    admissionController,
  });
  const outputBuffer = new WsOutputBuffer();
 
  function safeReleaseRunner(
    state: ClientState,
    reason: string,
  ): Promise<void> {
    return sessionManager.safeReleaseRunner(state, reason);
  }
 
  /**
   * Build all callback functions for sketch execution (onOutput, onError, etc.)
   * Extracted to reduce cognitive complexity of message handler.
   */
  function buildRunSketchCallbacks(
    ws: WebSocket,
    clientState: ClientState,
    reservation: SimulationReservation,
  ) {
    let compileFailed = false;
 
    const onOutput = (line: string, isComplete?: boolean) => {
      outputBuffer.sendSerialOutputBatched(ws, line, isComplete);
    };
 
    // Runtime diagnostics are sketch-controlled and already bounded per run;
    // a log entry per line would let a sketch flood the backend log.
    const onError = (err: string) => {
      logger.debug(`[Client WS][ERR] diagnostic received (${Buffer.byteLength(err)} bytes)`);
      outputBuffer.flushSerialOutputBuffer(ws);
      sendMessageToClient(ws, {
        type: WSMessageType.SERIAL_OUTPUT,
        data: "[ERR] " + err,
      });
    };
 
    const onExit = (_exitCode: number | null) => {
      // Capture client state immediately — the session entry
      // may be deleted by the ws "close" handler before the setTimeout fires.
      const capturedCs = sessionManager.get(ws);
 
      setTimeout(async () => {
        try {
          outputBuffer.flushSerialOutputBuffer(ws);
          Eif (capturedCs) {
            await sessionManager.safeReleaseRunner(
              capturedCs,
              "onExit",
              reservation,
            );
            // A start waiting for this release may already own the session.
            Iif (capturedCs.reservation && capturedCs.reservation !== reservation) return;
          }
 
          Iif (!shouldSendSimulationEndMessage(compileFailed)) return;
 
          sendMessageToClient(ws, {
            type: WSMessageType.SERIAL_OUTPUT,
            data: "--- Simulation ended: Loop cycles completed ---\n",
            isComplete: true,
          });
          sendMessageToClient(ws, {
            type: WSMessageType.SIMULATION_STATUS,
            status: "stopped",
          });
 
          outputBuffer.clearClient(ws);
        } catch (err) {
          logger.error(
            `Error sending stop message: ${err instanceof Error ? err.message : String(err)}`,
          );
        }
      }, 100);
    };
 
    const onCompileError = (compileErr: string) => {
      compileFailed = true;
      sendMessageToClient(ws, {
        type: WSMessageType.COMPILATION_ERROR,
        data: compileErr,
      });
      sendMessageToClient(ws, {
        type: WSMessageType.COMPILATION_STATUS,
        arduinoCliStatus: "error",
      });
      sendMessageToClient(ws, {
        type: WSMessageType.SIMULATION_STATUS,
        status: "stopped",
      });
      const cs = sessionManager.get(ws);
      Eif (cs) {
        sessionManager.safeReleaseRunner(
          cs,
          "onCompileError",
          reservation,
        ).catch((error) => {
          logger.warn(
            `[SandboxRunnerPool] safeReleaseRunner failed in onCompileError: ${error}`,
          );
        });
      }
      logger.error(`[Client Compile Error] diagnostic received (${Buffer.byteLength(compileErr)} bytes)`);
    };
 
    const onCompileSuccess = () => {
      sendMessageToClient(ws, {
        type: WSMessageType.COMPILATION_STATUS,
        arduinoCliStatus: "success",
      });
    };
 
    const onCompileQueued = () => {
      // gccStatus:queued removed in Phase 3.3.6 - queue state no longer emitted via WS
      // arduinoCliStatus does not have a "queued" state
    };
 
    const onPinState = (pin: number, type: PinStateChange, value: number) => {
      sendMessageToClient(ws, {
        type: WSMessageType.PIN_STATE,
        pin,
        stateType: type,
        value,
      });
    };
 
    const onIORegistry = (
      registry: IOPinRecord[],
      baudrate: number | undefined,
      reason?: string,
    ) => {
      const message: Extract<WSMessage, { type: "io_registry" }> = {
        type: WSMessageType.IO_REGISTRY,
        registry,
      };
      if (baudrate !== undefined) message.baudrate = baudrate;
      if (reason !== undefined) message.reason = reason;
      sendMessageToClient(ws, message);
      const baudSuffix = baudrate === undefined ? "" : `, baud=${baudrate}`;
      logger.info(`[io_registry] ${registry.length} pins${baudSuffix}`);
 
      // Async save without blocking — fire-and-forget with error handling
      (async () => {
        try {
          const sketchDir = clientState?.runner?.getSketchDir();
          if (!sketchDir) return;
 
          try {
            await access(sketchDir);
          } catch {
            return;
          }
 
          const registryFile = path.join(
            sketchDir,
            `io-registry-${Date.now()}.pending.json`,
          );
          await writeFile(registryFile, JSON.stringify(registry, null, 2));
          logger.debug(`Registry saved: ${path.basename(registryFile)}`);
          if (clientState.runner)
            clientState.runner.setRegistryFile(registryFile);
        } catch (err) {
          logger.warn(
            `Failed to save I/O Registry file: ${err instanceof Error ? err.message : String(err)}`,
          );
        }
      })();
    };
 
    const onTelemetry = (metrics: {
      timestamp: number;
      intendedPinChangesPerSecond: number;
      actualPinChangesPerSecond: number;
      droppedPinChangesPerSecond: number;
      batchesPerSecond: number;
      avgStatesPerBatch: number;
      serialOutputPerSecond: number;
      serialBytesPerSecond: number;
      serialBytesTotal: number;
      serialIntendedBytesPerSecond: number;
      serialDroppedBytesPerSecond: number;
    }) => {
      sendMessageToClient(ws, { type: WSMessageType.SIM_TELEMETRY, metrics });
    };
 
    const onPinStateBatch = (batch: {
      states: Array<{ pin: number; stateType: PinStateChange; value: number }>;
      timestamp: number;
    }) => {
      sendMessageToClient(ws, {
        type: WSMessageType.PIN_STATE_BATCH,
        states: batch.states,
        timestamp: batch.timestamp,
      });
    };
 
    return {
      onOutput,
      onError,
      onExit,
      onCompileError,
      onCompileSuccess,
      onCompileQueued,
      onPinState,
      onIORegistry,
      onTelemetry,
      onPinStateBatch,
      compileFailed: () => compileFailed,
    };
  }
 
  /**
   * Acquires a runner from the pool for the given client.
   * Manages the AbortController and handles pool-exhaustion / cancel errors.
   * Returns false when the caller should return early.
   * Extracted to keep handleStartSimulation below cognitive complexity threshold.
   */
  async function acquireRunnerForClient(
    ws: WebSocket,
    clientState: ClientState,
    reservation: SimulationReservation,
  ): Promise<boolean> {
    const acquireAbort = new AbortController();
    clientState.queueAbortController = acquireAbort;
    const clearOwnAbort = () => {
      if (clientState.queueAbortController === acquireAbort) {
        clientState.queueAbortController = null;
      }
    };
    let runner: SandboxRunner;
    try {
      runner = await pool.acquireRunner(acquireAbort.signal);
    } catch (error) {
      clearOwnAbort();
      // Stop, code change or disconnect already ended this lifecycle.
      if (clientState.reservation !== reservation) return false;
      const isCancelled =
        error instanceof Error && error.message.includes("cancelled");
      Iif (isCancelled) {
        // Client disconnected while waiting — nothing to send, WS is already closed
        logger.debug(
          `[SandboxRunnerPool] Acquire cancelled because WS closed while queued`,
        );
      } else {
        logger.error(`[SandboxRunnerPool] Failed to acquire runner: ${error}`);
        sendStartError(
          ws,
          operationError("SYSTEM_BUSY", SYSTEM_BUSY_MESSAGE, 5),
        );
      }
      await sessionManager.safeReleaseRunner(
        clientState,
        "runner-acquire-failed",
        reservation,
      );
      return false;
    }
    clearOwnAbort();
    if (clientState.reservation !== reservation) {
      // The lifecycle ended while the grant was in flight: the runner was
      // never attached to the session, so nothing else would return it.
      await pool.releaseRunner(runner);
      return false;
    }
    clientState.runner = runner;
    logger.debug(
      `[SandboxRunnerPool] Acquired runner for client. Pool stats: ${JSON.stringify(pool.getStats())}`,
    );
    return true;
  }
 
  /**
   * Log the run request for audit/evidence. Sketch source is never logged
   * (SECURITY.md); its length and a short digest identify the run.
   */
  function logRunPayloadAudit(
    code: string,
    timeoutSec: number | undefined,
    sessionId: string | undefined,
  ): void {
    const payload = {
      codeLength: code.length,
      codeSha256: createHash("sha256").update(code).digest("hex").slice(0, 12),
      timeoutSec,
      context: { sessionId, label: "default-ws" },
    };
    logger.debug(`[B1-Evidence] Payload: ${JSON.stringify(payload)}`);
  }
 
  /**
   * Admission phase of a start: waits for this connection's previous release,
   * then checks lifecycle, rate limit and code and reserves admission. Returns
   * null when the start was rejected or cancelled.
   */
  async function admitStart(
    ws: WebSocket,
    data: Extract<ClientToServerWSMessage, { type: "start_simulation" }>,
    clientState: ClientState,
  ): Promise<{ code: string; reservation: SimulationReservation } | null> {
    if (!(await waitForPreviousRelease(clientState))) return null;
    // From here to the reservation everything runs synchronously, so the
    // lifecycle check and the reservation are atomic for this connection.
    if (hasSimulationLifecycle(clientState)) {
      rejectActiveLifecycle(ws, clientState);
      return null;
    }
 
    // Rate limiting check
    const rateLimiter = getSimulationRateLimiter();
    const limitCheck = deps.disableRateLimit
      ? { allowed: true }
      : rateLimiter.checkLimit(clientState.subject);
    if (!limitCheck.allowed) {
      const retryAfter = limitCheck.retryAfter || 30;
      logger.warn(
        `[RateLimit] Simulation start rejected. Retry after ${retryAfter}s`,
      );
 
      sendStartError(
        ws,
        operationError(
          "RATE_LIMITED",
          `Rate-Limit: Bitte warte ${retryAfter} Sekunden, bevor du eine weitere Simulation startest.`,
          retryAfter,
        ),
      );
      return null;
    }
 
    // Use per-client code from the WS message if provided (multi-client isolation),
    // Deprecated compatibility fallback. New clients send code per session;
    // remove this branch at the next protocol-major release.
    const code =
      "code" in data &&
      typeof data.code === "string" &&
      data.code.trim().length > 0
        ? data.code
        : getLastCompiledCode(clientState.subject);
    if (!code) {
      sendMessageToClient(ws, {
        type: WSMessageType.SERIAL_OUTPUT,
        data: "[ERR] No compiled code available. Please compile first.\n",
      });
      sendMessageToClient(ws, {
        type: WSMessageType.SIMULATION_STATUS,
        status: "stopped",
      });
      return null;
    }
 
    const admission = admissionController.reserve(clientState.subject);
    if (!admission.admitted) {
      rejectAdmission(ws, admission);
      return null;
    }
    clientState.reservation = admission.reservation;
    return { code, reservation: admission.reservation };
  }
 
  /**
   * Handle "start_simulation" WebSocket message
   * Checks rate limits, acquires runner, and starts sketch execution.
   */
  async function handleStartSimulation(
    ws: WebSocket,
    data: Extract<ClientToServerWSMessage, { type: "start_simulation" }>,
    clientState: ClientState,
  ): Promise<void> {
    const admitted = await admitStart(ws, data, clientState);
    if (!admitted) return;
    const { code, reservation } = admitted;
 
    // If the pool is saturated, notify client it is queued and wait for a slot
    const statsBeforeAcquire = pool.getStats();
    const willQueue =
      statsBeforeAcquire.availableRunners === 0 &&
      statsBeforeAcquire.totalRunners >= statsBeforeAcquire.maxRunners;
    if (willQueue) {
      logger.debug(
        `[SandboxRunnerPool] Pool saturated — client queued (queue length: ${statsBeforeAcquire.queuedRequests + 1})`,
      );
      sendMessageToClient(ws, {
        type: WSMessageType.SIMULATION_STATUS,
        status: "queued",
      });
    }
 
    // Acquire new runner from pool (may block until a slot is released).
    // The AbortController is managed inside acquireRunnerForClient; it is set on
    // clientState so the WS-close handler can cancel the wait on disconnect.
    if (!(await acquireRunnerForClient(ws, clientState, reservation))) return;
    const acquiredRunner = clientState.runner!; // non-null: acquireRunnerForClient returned true
 
    // Slot assignment: tell client which runner slot they own immediately
    const acquiredWorkerIndex = pool.getRunnerIndex(acquiredRunner);
 
    // Keep the client in STARTING until runSketch confirms process readiness.
    // The lifecycle manager performs the actual running transition atomically.
    sendMessageToClient(ws, {
      type: WSMessageType.COMPILATION_STATUS,
      arduinoCliStatus: "compiling",
      workerIndex: acquiredWorkerIndex,
      workerTotal: sessionManager.countRunningClients() + 1,
    });
 
    // Broadcast updated count to all OTHER running clients (ws excluded because
    // it just received the full workerIndex+workerTotal message above).
    sessionManager.broadcastWorkerTotal(ws);
 
    // Build callbacks
    const callbacks = buildRunSketchCallbacks(ws, clientState, reservation);
    let runnerReady = false;
    let pendingCompileSuccesses = 0;
    const onCompileSuccess = () => {
      Iif (runnerReady) {
        callbacks.onCompileSuccess();
      } else {
        pendingCompileSuccesses += 1;
      }
    };
    const timeoutValue = "timeout" in data ? data.timeout : undefined;
    logger.info(`[Simulation] Starting with timeout: ${timeoutValue}s`);
 
    // Log consolidated payload for audit
    logRunPayloadAudit(code, timeoutValue, clientState.testRunId);
 
    // Start sketch execution once the runner has resolved.
    try {
      const processReady = await acquiredRunner.runSketch({
        code,
        headers: data.headers,
        entryFile: data.entryFile,
        onOutput: callbacks.onOutput,
        onError: callbacks.onError,
        onExit: callbacks.onExit,
        onCompileError: callbacks.onCompileError,
        onCompileSuccess,
        onCompileQueued: callbacks.onCompileQueued,
        onPinState: callbacks.onPinState,
        timeoutSec: timeoutValue,
        onIORegistry: callbacks.onIORegistry,
        onTelemetry: callbacks.onTelemetry,
        onPinStateBatch: callbacks.onPinStateBatch,
        context: { sessionId: clientState.testRunId, label: "default-ws", subject: clientState.subject },
      });
      if (!processReady) return;
    } catch (error) {
      logger.error(`[Simulation] runSketch failed (${safeErrorLogMetadata(error)})`);
      // A stop or disconnect during startup already released and reported.
      if (clientState.reservation !== reservation) return;
      await sessionManager.safeReleaseRunner(
        clientState,
        "runSketch-error",
        reservation,
      );
      sendStartError(
        ws,
        operationError(
          "SIMULATION_START_FAILED",
          "Simulation could not be started.",
        ),
      );
      return;
    }
    // Ended during startup: never mark a released session as running.
    if (!ownsLifecycle(clientState, reservation, acquiredRunner)) return;
 
    sessionManager.markSessionRunning(clientState);
    sendMessageToClient(ws, {
      type: WSMessageType.SIMULATION_STATUS,
      status: "running",
    });
    runnerReady = true;
    while (pendingCompileSuccesses > 0) {
      callbacks.onCompileSuccess();
      pendingCompileSuccesses -= 1;
    }
  }
 
  /**
   * Handle "code_changed" WebSocket message
   */
  async function handleCodeChanged(
    _ws: WebSocket,
    clientState: ClientState,
  ): Promise<void> {
    sessionManager.cancelPendingStart(clientState);
    if (clientState?.runner || clientState?.reservation) {
      sessionManager.abortQueuedAcquire(clientState);
      await safeReleaseRunner(clientState, "code_changed");
      sendMessageToClient(_ws, {
        type: WSMessageType.SIMULATION_STATUS,
        status: "stopped",
      });
      sendMessageToClient(_ws, {
        type: WSMessageType.SERIAL_OUTPUT,
        data: "--- Simulation stopped due to code change ---\n",
      });
    }
  }
 
  /**
   * Handle "stop_simulation" WebSocket message
   */
  async function handleStopSimulation(
    _ws: WebSocket,
    clientState: ClientState,
  ): Promise<void> {
    sessionManager.cancelPendingStart(clientState);
    sessionManager.abortQueuedAcquire(clientState);
    if (clientState?.runner || clientState?.reservation) {
      await safeReleaseRunner(clientState, "stop_simulation");
    }
    sendMessageToClient(_ws, {
      type: WSMessageType.SIMULATION_STATUS,
      status: "stopped",
    });
    sendMessageToClient(_ws, {
      type: WSMessageType.SERIAL_OUTPUT,
      data: "--- Simulation stopped ---\n",
    });
  }
 
  let _wsConnectAttempts = 0;
 
  const messageRouter = new WsMessageRouter({
    logger,
    getClientState: (ws) => sessionManager.get(ws),
    handlers: {
      startSimulation: handleStartSimulation,
      codeChanged: (ws, _data, clientState) => handleCodeChanged(ws, clientState),
      stopSimulation: (ws, _data, clientState) => handleStopSimulation(ws, clientState),
      pauseSimulation: (ws, _data, clientState) => handlePauseSimulation(ws, clientState, sessionManager),
      resumeSimulation: (ws, _data, clientState) => handleResumeSimulation(ws, clientState, sessionManager),
      serialInput: handleSerialInput,
      setPinValue: handleSetPinValue,
    },
  });
 
  wss.on("connection", (ws, req) => {
    const authorization = getRequestAuthorization(req, deps.trust);
    Iif (!authorization.allowed) {
      ws.close(1008, "Unauthorized");
      return;
    }
    const identity = authorization.identity;
    const url = req.url || "";
    const urlParams = new URLSearchParams(url.split("?")[1] || "");
    const testRunId = urlParams.get("testRunId") || undefined;
    const testRunIdSuffix = testRunId ? ` [testRunId: ${testRunId}]` : "";
 
    _wsConnectAttempts++;
    logger.warn(
      `New WebSocket client connected for subject ${identity.subject}${testRunIdSuffix}. Total clients: ${wss.clients.size} (attempt #${_wsConnectAttempts})`,
    );
    if (_wsConnectAttempts % 10 === 0) {
      logger.warn(
        `[WS milestone] ${_wsConnectAttempts} total connect attempts, ${wss.clients.size} currently open`,
      );
    }
 
    sessionManager.register(ws, {
      subject: identity.subject,
      runner: null,
      isRunning: false,
      isPaused: false,
      testRunId,
      queueAbortController: null,
      reservation: null,
      metricsState: null,
    });
 
    const clientState = sessionManager.get(ws);
    let simStatus: "paused" | "running" | "stopped";
    Iif (clientState?.isPaused) {
      simStatus = "paused";
    } else Iif (clientState?.isRunning) {
      simStatus = "running";
    } else {
      simStatus = "stopped";
    }
    sendMessageToClient(ws, {
      type: WSMessageType.SIMULATION_STATUS,
      status: simStatus,
    });
 
    Iif (testRunId) {
      sendMessageToClient(ws, {
        type: WSMessageType.HANDSHAKE,
        testRunId,
        protocolVersion: WEBSOCKET_PROTOCOL_VERSION,
      });
    }
 
    answeredPing.set(ws, true);
    ws.on("pong", () => answeredPing.set(ws, true));
 
    const inbound = new InboundMessageLimiter(inboundLimit.perSecond, inboundLimit.burst);
    let droppedMessages = 0;
    ws.on("message", async (message) => {
      if (!inbound.tryTake()) {
        droppedMessages += 1;
        if (droppedMessages === 1) {
          logger.warn(`[WS] Inbound message rate exceeded for subject ${identity.subject}; dropping excess messages`);
        }
        return;
      }
      await messageRouter.route(ws, message);
    });
 
    ws.on("close", async (code: number, reason: Buffer) => {
      await sessionManager.cleanupClient(ws, "ws-close");
 
      // Clean up serial output buffer and timer
      outputBuffer.clearClient(ws);
 
      logger.warn(
        `Client disconnected (code=${code}, reason=${reason.toString() || "—"}). Remaining clients: ${wss.clients.size}`,
      );
    });
 
    ws.on("error", async (error) => {
      await sessionManager.cleanupClient(ws, "ws-error");
 
      // Clean up serial output buffer and timer
      outputBuffer.clearClient(ws);
 
      logger.error(
        `WebSocket error: ${error instanceof Error ? error.message : String(error)}`,
      );
    });
  });
 
  /**
   * Ends every session's lifecycle (shutdown and test reset). Releases run in
   * parallel with bounded concurrency, and a release that hangs stops being
   * awaited after the shutdown stop timeout: the pool shutdown and the final
   * owner sweep remove what is left.
   */
  async function stopAllRunnersAndNotify() {
    const sessions = [...sessionManager.entries()];
    const settled = await mapWithConcurrency(sessions, config.sandbox.pool.shutdownConcurrency, async ([ws, clientState]) => {
      sessionManager.cancelPendingStart(clientState);
      sessionManager.abortQueuedAcquire(clientState);
      const released = clientState.runner || clientState.reservation
        ? await settleWithin(safeReleaseRunner(clientState, "test-reset"), config.sandbox.pool.shutdownStopTimeoutMs)
        : true;
      clientState.isRunning = false;
      clientState.isPaused = false;
      sendMessageToClient(ws, {
        type: WSMessageType.SIMULATION_STATUS,
        status: "stopped",
      });
      return released;
    });
    const unfinished = settled.filter((released) => !released).length;
    Iif (unfinished > 0) {
      logger.warn(`[Simulation] ${unfinished} session release(s) exceeded the shutdown stop timeout`);
    }
 
    const cleanedTestRunIds = sessions
      .map(([, clientState]) => clientState.testRunId)
      .filter((id): id is string => Boolean(id));
    return { cleanedUpCount: sessions.length, cleanedTestRunIds };
  }
 
  return { wss, stopAllRunnersAndNotify };
}