All files / server/services arduino-compiler.ts

68.6% Statements 201/293
64.49% Branches 89/138
52.38% Functions 22/42
70.6% Lines 197/279

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    13x 13x 13x                                                                               66x 66x 66x 66x 66x   66x     66x 66x 66x             388x 388x   388x 392x   392x 386x 386x   6x         6x 4x   4x 4x     4x                     4x             6x 4x           6x           21x 21x 21x       21x 21x 20x 20x 20x   1x                   194x       194x       194x       180x 180x 180x 360x 360x               180x                                                         180x 180x 180x         180x                                                                                                                           194x   194x 13x 13x 13x   13x               181x                         180x 180x               180x 180x             180x                         180x 180x   180x 169x     11x   11x   13x     13x   13x 13x 14x 13x     13x 13x 13x 13x           13x 13x   13x 13x 13x       13x               11x 11x 11x 13x 13x 13x       11x                                 166x 166x 166x   166x                                         166x                                 13x 13x 13x     13x 5x 7x 7x         13x 1x     13x                   194x   194x 194x   194x                           194x     194x           194x   194x 194x     194x 194x 194x 194x 194x 194x 194x 194x   194x   194x 194x 13x                                 181x 181x                           181x 180x     180x       180x         180x     179x             179x 179x 179x 179x   179x 166x 166x         166x 166x 166x   13x         13x 13x 13x     179x                     2x                   194x                   194x 194x       194x 194x 194x                           1x                                   174x 174x   174x   174x 174x           174x 11x 11x 11x                           163x 174x 174x   174x 163x                                                           174x             174x     174x 174x   174x 174x                           163x 163x   163x 163x   163x 163x 143x   20x     163x 163x   163x               163x 163x 145x   145x 163x       18x   163x                                                                          
//arduino-compiler.ts
 
import { writeFile, mkdir, rm, readFile, readdir, stat, utimes, rename, mkdtemp } from "node:fs/promises";
import { join } from "node:path";
import { randomUUID, createHash } from "node:crypto";
import { Logger } from "@shared/logger";
import { ParserMessage, IOPinRecord } from "@shared/schema";
import { CodeParser } from "@shared/code-parser";
import { detectSketchEntrypoints } from "@shared/utils/sketch-validation";
import { getFastTmpBaseDir } from "@shared/utils/temp-paths";
import { reservedNamesValidator } from "@shared/reserved-names-validator";
import { getCompileGatekeeper } from "./compile-gatekeeper";
import { ProcessExecutor } from "./process-executor";
import { CompilationError, CompilerOutputParser } from "./compiler/compiler-output-parser";
// Removed unused mock imports to satisfy TypeScript
 
// Re-export for backwards compatibility
export type { CompilationError } from "./compiler/compiler-output-parser";
 
export interface CompilationResult {
  success: boolean;
  output: string;
  // raw stderr text for backwards compatibility and debugging
  stderr?: string;
  // structured list of errors/warnings from the compiler
  errors: CompilationError[];
  binary?: Buffer;
  arduinoCliStatus: "idle" | "compiling" | "success" | "error";
  // gccStatus removed – it was deprecated and is no longer populated
  parserMessages?: ParserMessage[]; // Parser validation messages
  ioRegistry?: IOPinRecord[]; // I/O Registry for visualization
}
 
export interface CompileRequestOptions {
  fqbn?: string;
  libraries?: string[];
  sketchHash?: string;
  coreFingerprint?: string;
  buildPath?: string;
  buildCachePath?: string;
  hexCacheDir?: string;
}
 
export class ArduinoCompiler {
  private readonly tempDir = join(process.cwd(), "temp");
  private readonly logger = new Logger("ArduinoCompiler");
  private readonly gatekeeper = getCompileGatekeeper();
  private readonly processExecutor = new ProcessExecutor();
  private readonly defaultFqbn = process.env.ARDUINO_FQBN || "arduino:avr:uno";
  private readonly defaultBuildCacheDir =
    process.env.ARDUINO_CACHE_DIR ||
    process.env.BUILD_CACHE_DIR ||
    join(process.cwd(), "server", "arduino-cache");
  private readonly defaultBinaryStorageDir = join(this.defaultBuildCacheDir, "binaries");
  private readonly defaultHexCacheDir = join(this.defaultBuildCacheDir, "hex-cache");
  private readonly defaultBuildCachePath = join(this.defaultBuildCacheDir, "build-cache");
 
  /**
   * Robust cleanup function that handles file locking on Windows/Unix.
   * Uses rename-before-delete to work around EPERM and EBUSY errors.
   */
  private async robustCleanupDir(dirPath: string): Promise<void> {
    const maxRetries = 3;
    const retryDelayMs = 100;
 
    for (let attempt = 0; attempt < maxRetries; attempt++) {
      try {
        // Fast path: try direct deletion first
        await rm(dirPath, { recursive: true, force: true });
        this.logger.debug(`Successfully deleted ${dirPath}`);
        return;
      } catch (directError) {
        this.logger.debug(
          `Direct delete failed (attempt ${attempt + 1}/${maxRetries}): ${directError}`,
        );
 
        // If not the last attempt, try the rename-trick
        if (attempt < maxRetries - 1) {
          try {
            // Rename to a trash path to work around file locks
            const trashPath = `${dirPath}.trash.${randomUUID()}`;
            this.logger.debug(
              `Attempting rename-before-delete: ${dirPath} -> ${trashPath}`,
            );
            await rename(dirPath, trashPath);
 
            // Try to delete the trash path in the background (non-blocking)
            rm(trashPath, { recursive: true, force: true }).catch((trashError) => {
              this.logger.warn(
                `Failed to delete trash directory ${trashPath}: ${trashError}`,
              );
              // This is non-critical; we got the original dir out of the way
            });
            return;
          } catch (renameError) {
            this.logger.debug(`Rename-trick failed: ${renameError}`);
            // Fall through to next retry or throw
          }
        }
      }
 
      // If not the last attempt, wait before retrying
      if (attempt < maxRetries - 1) {
        await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
      }
    }
 
    // Last resort: log a warning but don't throw
    // The OS will clean up temp directories eventually
    this.logger.warn(
      `Failed to clean up ${dirPath} after ${maxRetries} attempts. It will be cleaned up by the OS.`,
    );
  }
 
  static async create(): Promise<ArduinoCompiler> {
    const instance = new ArduinoCompiler();
    await instance.ensureTempDir();
    return instance;
  }
 
  private async ensureTempDir() {
    try {
      await mkdir(this.tempDir, { recursive: true });
      await mkdir(this.defaultHexCacheDir, { recursive: true });
      await mkdir(this.defaultBuildCachePath, { recursive: true });
      await mkdir(this.defaultBinaryStorageDir, { recursive: true });
    } catch (error) {
      this.logger.warn(
        `Failed to create temp directory: ${error instanceof Error ? error.message : error}`,
      );
    }
  }
 
  private buildSketchHash(
    code: string,
    options?: CompileRequestOptions,
  ): string {
    Iif (options?.sketchHash) {
      return options.sketchHash;
    }
 
    const payload = JSON.stringify({
      code,
      fqbn: options?.fqbn || this.defaultFqbn,
    });
    return createHash("sha256").update(payload).digest("hex");
  }
 
  private async readBinaryFromStorage(sketchHash: string): Promise<Buffer | null> {
    const hexPath = join(this.defaultBinaryStorageDir, `${sketchHash}.hex`);
    const elfPath = join(this.defaultBinaryStorageDir, `${sketchHash}.elf`);
    for (const path of [hexPath, elfPath]) {
      try {
        const binary = await readFile(path);
        const now = new Date();
        await utimes(path, now, now).catch(() => undefined);
        return binary;
      } catch {
        // continue probing next extension
      }
    }
    return null;
  }
 
  private async writeBinaryToStorage(sketchHash: string, binary: Buffer): Promise<void> {
    const targetPath = join(this.defaultBinaryStorageDir, `${sketchHash}.hex`);
    const tmpPath = `${targetPath}.tmp-${process.pid}-${Date.now()}`;
    await mkdir(this.defaultBinaryStorageDir, { recursive: true });
    await writeFile(tmpPath, binary);
    await rename(tmpPath, targetPath);
  }
 
  private async _writeOutputToCache(storageDir: string, sketchHash: string, output: string): Promise<void> {
    const outputPath = join(storageDir, `${sketchHash}.output.txt`);
    await writeFile(outputPath, output, "utf8");
  }
 
  private async _readOutputFromCache(storageDir: string, sketchHash: string): Promise<string | null> {
    const outputPath = join(storageDir, `${sketchHash}.output.txt`);
    try {
      return await readFile(outputPath, "utf8");
    } catch {
      return null;
    }
  }
 
  private async readHexFromCache(
    sketchHash: string,
    hexCacheDir: string,
  ): Promise<Buffer | null> {
    const cachePath = join(hexCacheDir, `${sketchHash}.hex`);
    try {
      const binary = await readFile(cachePath);
      const now = new Date();
      await utimes(cachePath, now, now).catch(() => undefined);
      return binary;
    } catch {
      return null;
    }
  }
 
  private async writeHexToCache(
    sketchHash: string,
    hexCacheDir: string,
    binary: Buffer,
  ): Promise<void> {
    const cachePath = join(hexCacheDir, `${sketchHash}.hex`);
    const tmpPath = `${cachePath}.tmp-${process.pid}-${Date.now()}`;
    await mkdir(hexCacheDir, { recursive: true });
    await writeFile(tmpPath, binary);
    await rename(tmpPath, cachePath);
  }
 
  private async runHexCacheCleanup(hexCacheDir: string, maxBytes: number = 512 * 1024 * 1024): Promise<void> {
    try {
      const entries = await readdir(hexCacheDir);
      const files: Array<{ path: string; size: number; atimeMs: number }> = [];
      let totalSize = 0;
 
      for (const entry of entries) {
        if (!entry.endsWith(".hex")) continue;
        const fullPath = join(hexCacheDir, entry);
        try {
          const fileStat = await stat(fullPath);
          if (!fileStat.isFile()) continue;
          totalSize += fileStat.size;
          files.push({
            path: fullPath,
            size: fileStat.size,
            atimeMs: fileStat.atimeMs || fileStat.mtimeMs,
          });
        } catch {
          // ignore disappearing files
        }
      }
 
      if (totalSize <= maxBytes) return;
 
      files.sort((a, b) => a.atimeMs - b.atimeMs);
      for (const file of files) {
        if (totalSize <= maxBytes) break;
        await rm(file.path, { force: true });
        totalSize -= file.size;
      }
    } catch (error) {
      this.logger.debug(`[CacheCleanup] skipped: ${error instanceof Error ? error.message : String(error)}`);
    }
  }
 
  /**
   * Validates that the sketch contains required entry points (setup and loop).
   * Returns { hasSetup, hasLoop } and error message if validation fails.
   */
  private validateSketchEntrypoints(code: string): {
    valid: boolean;
    hasSetup: boolean;
    hasLoop: boolean;
    errorMessage?: string;
  } {
    const { hasSetup, hasLoop } = detectSketchEntrypoints(code);
 
    if (!hasSetup || !hasLoop) {
      const missingFunctions = [];
      if (!hasSetup) missingFunctions.push("setup()");
      if (!hasLoop) missingFunctions.push("loop()");
 
      return {
        valid: false,
        hasSetup,
        hasLoop,
        errorMessage: `Missing Arduino functions: ${missingFunctions.join(" and ")}\n\nArduino sketches require:\n- void setup() { }\n- void loop() { }`,
      };
    }
 
    return { valid: true, hasSetup, hasLoop };
  }
 
  /**
   * Checks both the instant binary cache and hex cache for a compiled sketch.
   * Returns the first available cached binary, or null if no cache hit.
   */
  private async checkCacheHits(
    sketchHash: string,
    hexCacheDir: string,
    compileStartedAt: bigint,
  ): Promise<{ cached: boolean; binary: Buffer | null; cacheType: string; cachedOutput: string | null }> {
    // Check instant binary cache first (most recent)
    const instantBinary = await this.readBinaryFromStorage(sketchHash);
    Iif (instantBinary) {
      const elapsedMs = Number((process.hrtime.bigint() - compileStartedAt) / BigInt(1_000_000));
      this.logger.info(`[Cache] Hit for hash ${sketchHash} (${elapsedMs}ms)`);
      const cachedOutput = await this._readOutputFromCache(this.defaultBinaryStorageDir, sketchHash);
      return { cached: true, binary: instantBinary, cacheType: "instant", cachedOutput };
    }
 
    // Check hex cache (persistent, shared across sessions)
    const cachedBinary = await this.readHexFromCache(sketchHash, hexCacheDir);
    Iif (cachedBinary) {
      const elapsedMs = Number((process.hrtime.bigint() - compileStartedAt) / BigInt(1_000_000));
      this.logger.info(`[Cache] Hit for hash ${sketchHash} (${elapsedMs}ms)`);
      const cachedOutput = await this._readOutputFromCache(hexCacheDir, sketchHash);
      return { cached: true, binary: cachedBinary, cacheType: "hex", cachedOutput };
    }
 
    return { cached: false, binary: null, cacheType: "none", cachedOutput: null };
  }
 
  /**
   * Processes header includes by replacing #include statements with actual header content.
   * Tracks line offset for later error correction.
   * Returns { processedCode, lineOffset }.
   */
  private async processHeaderIncludes(
    code: string,
    headers?: Array<{ name: string; content: string }>,
    sketchDir?: string,
  ): Promise<{ processedCode: string; lineOffset: number }> {
    let processedCode = code;
    let lineOffset = 0;
 
    if (!headers || headers.length === 0) {
      return { processedCode, lineOffset };
    }
 
    this.logger.debug(`Processing ${headers.length} header includes`);
 
    for (const header of headers) {
      // Try to find includes with both the full name (header_1.h) and without extension (header_1)
      const headerWithoutExt = header.name.replace(/\.[^/.]+$/, "");
 
      // Search for both variants: #include "header_1.h" and #include "header_1"
      const includeVariants = [`#include "${header.name}"`, `#include "${headerWithoutExt}"`];
 
      let found = false;
      for (const includeStatement of includeVariants) {
        if (processedCode.includes(includeStatement)) {
          this.logger.debug(`Found include for: ${header.name} (pattern: ${includeStatement})`);
          
          // Replace the #include with the actual header content
          const replacement = `// --- Start of ${header.name} ---\n${header.content}\n// --- End of ${header.name} ---`;
          const escapedInclude = includeStatement.split('"')[1].replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
          const patternString = String.raw`#include\s*"${escapedInclude}"`;
          processedCode = processedCode.replaceAll(
            new RegExp(patternString, "g"),
            replacement,
          );
 
          // Calculate line offset by counting newlines in replacement
          const newlinesInReplacement = (replacement.match(/\n/g) || []).length;
          lineOffset += newlinesInReplacement;
 
          found = true;
          this.logger.debug(`Replaced include for: ${header.name}, line offset now: ${lineOffset}`);
          break;
        }
      }
 
      Iif (!found) {
        this.logger.debug(
          `Include not found for: ${header.name} (tried: ${includeVariants.join(", ")})`,
        );
      }
    }
 
    // Write header files to disk as separate files
    Eif (sketchDir) {
      this.logger.debug(`Writing ${headers.length} header files to ${sketchDir}`);
      for (const header of headers) {
        const headerPath = join(sketchDir, header.name);
        this.logger.debug(`Writing header: ${headerPath}`);
        await writeFile(headerPath, header.content);
      }
    }
 
    return { processedCode, lineOffset };
  }
 
  /**
   * Handles successful compilation: writes caches and formats output.
   */
  private async handleCompilationSuccess(
    sketchHash: string,
    hexCacheDir: string,
    cliResult: {
      success: boolean;
      output?: string;
      errors?: string;
      parsedErrors?: CompilationError[];
      binary?: Buffer;
    },
  ): Promise<{ cliOutput: string; cliErrors: string; parsedErrors: CompilationError[] }> {
    const cliOutput = cliResult.output || "";
    let cliErrors = cliResult.errors || "";
    const parsedErrors = cliResult.parsedErrors || [];
 
    Iif (cliResult.binary) {
      // Write to both instant cache and persistent hex cache
      await this.writeHexToCache(sketchHash, hexCacheDir, cliResult.binary).catch((error) => {
        this.logger.debug(
          `[CompileCache] failed to write HEX cache: ${error instanceof Error ? error.message : String(error)}`,
        );
      });
      await this.writeBinaryToStorage(sketchHash, cliResult.binary).catch((error) => {
        this.logger.debug(
          `[CompileCache] failed to write binary storage cache: ${error instanceof Error ? error.message : String(error)}`,
        );
      });
      // Store the formatted output alongside both cache locations so cache hits
      // can reproduce the full compiler output (sketch size, RAM usage, etc.)
      if (cliOutput) {
        await this._writeOutputToCache(hexCacheDir, sketchHash, cliOutput).catch(() => undefined);
        await this._writeOutputToCache(this.defaultBinaryStorageDir, sketchHash, cliOutput).catch(() => undefined);
      }
      await this.runHexCacheCleanup(hexCacheDir);
    }
 
    return { cliOutput, cliErrors, parsedErrors };
  }
 
  /**
   * Handles compilation errors: cleans error messages and parses them into structured errors.
   */
  private handleCompilationError(
    cliErrors: string,
    lineOffset: number,
    cliResult: {
      success: boolean;
      output?: string;
      errors?: string;
      parsedErrors?: CompilationError[];
      binary?: Buffer;
    },
  ): { cliOutput: string; cliErrors: string; parsedErrors: CompilationError[] } {
    const cliOutput = "";
    let cleanedErrors = cliErrors;
    let parsedErrors = cliResult.parsedErrors || [];
 
    // Correct stderr text for offset so UI shows original line numbers
    if (lineOffset > 0 && cleanedErrors) {
      cleanedErrors = cleanedErrors.replaceAll(/sketch\.ino:(\d+):/g, (_m, n) => {
        const corrected = Math.max(1, Number.parseInt(n, 10) - lineOffset);
        return `sketch.ino:${corrected}:`;
      });
    }
 
    // Backstop: if the caller didn't supply parsedErrors, run parser ourselves
    if (parsedErrors.length === 0 && cleanedErrors) {
      parsedErrors = this.parseCompilerErrors(cleanedErrors, lineOffset);
    }
 
    return { cliOutput, cliErrors: cleanedErrors, parsedErrors };
  }
 
  async compile(
    code: string,
    headers?: Array<{ name: string; content: string }>,
    tempRoot?: string,
    options?: CompileRequestOptions,
  ): Promise<CompilationResult> {
    // GATEKEEPER: Acquire a compile slot to prevent race conditions
    const release = await this.gatekeeper.acquire();
 
    try {
      return await this.compileInternal(code, headers, tempRoot, options);
    } finally {
      release();
    }
  }
 
  /**
   * Internal compile implementation (wrapped by compile with gatekeeper)
   * Orchestrates compilation by delegating to helper functions for clarity.
   */
  private async compileInternal(
    code: string,
    headers?: Array<{ name: string; content: string }>,
    tempRoot?: string,
    options?: CompileRequestOptions,
  ): Promise<CompilationResult> {
    const sketchId = randomUUID();
 
    // Ensure provided tempRoot exists (important for Worker pool and deterministic tests)
    Iif (tempRoot) {
      await mkdir(tempRoot, { recursive: true }).catch(() => {});
    }
 
    // use a unique temporary directory per-call to avoid state conflicts
    const baseTempDir =
      tempRoot || (await mkdtemp(join(getFastTmpBaseDir(), "unosim-")));
 
    const sketchDir = join(baseTempDir, sketchId);
    const sketchFile = join(sketchDir, `${sketchId}.ino`);
 
    // Pre-compilation validation and parsing
    const parser = new CodeParser();
    const parserMessages = parser.parseAll(code);
    const reservedNameMessages = reservedNamesValidator.validateReservedNames(code);
    const allParserMessages = [...parserMessages, ...reservedNameMessages];
    const ioRegistry: IOPinRecord[] = [];
    const sketchHash = this.buildSketchHash(code, options);
    const hexCacheDir = options?.hexCacheDir || this.defaultHexCacheDir;
    const compileStartedAt = process.hrtime.bigint();
 
    try {
      // 1. Validate sketch has required entry points
      const validation = this.validateSketchEntrypoints(code);
      if (!validation.valid) {
        return {
          success: false,
          output: "",
          stderr: validation.errorMessage,
          errors: [],
          arduinoCliStatus: "error",
          parserMessages: allParserMessages,
          ioRegistry,
        };
      }
 
      // 2. Check both instant and hex caches.
      // Only use the cache when the output sidecar (.output.txt) also exists so
      // the full "Sketch uses X bytes … Board: Arduino UNO" message is returned.
      // If cachedOutput is null (e.g. old cache entry written before the sidecar
      // was introduced) we fall through to a fresh compile so the sidecar gets
      // written and the user always sees the complete output.
      const cacheResult = await this.checkCacheHits(sketchHash, hexCacheDir, compileStartedAt);
      Iif (cacheResult.cached && cacheResult.binary && cacheResult.cachedOutput !== null) {
        return {
          success: true,
          output: cacheResult.cachedOutput,
          stderr: undefined,
          errors: [],
          binary: cacheResult.binary,
          arduinoCliStatus: "success",
          parserMessages: allParserMessages,
          ioRegistry,
        };
      }
 
      // 3. Create directories and process headers
      await mkdir(sketchDir, { recursive: true });
      Iif (options?.buildPath) {
        await mkdir(options.buildPath, { recursive: true }).catch(() => {});
      }
      Iif (options?.buildCachePath) {
        await mkdir(options.buildCachePath, { recursive: true }).catch(() => {});
      }
 
      const { processedCode, lineOffset } = await this.processHeaderIncludes(
        code,
        headers,
        sketchDir,
      );
      await writeFile(sketchFile, processedCode);
 
      // 4. Run Arduino CLI compilation
      const cliResult = await this.compileWithArduinoCli(sketchFile, {
        fqbn: options?.fqbn || this.defaultFqbn,
        buildPath: options?.buildPath,
        buildCachePath: options?.buildCachePath || this.defaultBuildCachePath,
      });
 
      // 5. Handle result (success or error)
      let cliOutput = "";
      let cliErrors = "";
      let parsedErrors: CompilationError[] = [];
      let arduinoCliStatus: "success" | "error" = "error";
 
      if (cliResult.success) {
        arduinoCliStatus = "success";
        const successResult = await this.handleCompilationSuccess(
          sketchHash,
          hexCacheDir,
          cliResult,
        );
        cliOutput = successResult.cliOutput;
        cliErrors = successResult.cliErrors;
        parsedErrors = successResult.parsedErrors;
      } else {
        const errorResult = this.handleCompilationError(
          cliResult.errors || "Compilation failed",
          lineOffset,
          cliResult,
        );
        cliOutput = errorResult.cliOutput;
        cliErrors = errorResult.cliErrors;
        parsedErrors = errorResult.parsedErrors;
      }
 
      return {
        success: cliResult.success,
        output: cliOutput,
        stderr: cliErrors || undefined,
        errors: parsedErrors,
        binary: cliResult.binary,
        arduinoCliStatus,
        parserMessages: allParserMessages,
        ioRegistry,
      };
    } catch (error) {
      return {
        success: false,
        output: "",
        stderr: `Compilation failed: ${error instanceof Error ? error.message : String(error)}`,
        errors: [],
        arduinoCliStatus: "error",
        parserMessages: allParserMessages,
        ioRegistry,
      };
    } finally {
      await this._cleanupSketchDirs(sketchDir, baseTempDir, tempRoot);
    }
  }
 
  /** Remove sketch-specific temporary directories created during compilation. */
  private async _cleanupSketchDirs(
    sketchDir: string,
    baseTempDir: string,
    tempRoot?: string,
  ): Promise<void> {
    try {
      await this.robustCleanupDir(sketchDir);
    } catch (error) {
      this.logger.warn(`Failed to clean up sketch directory: ${error}`);
    }
    Eif (!tempRoot) {
      try {
        await this.robustCleanupDir(baseTempDir);
      } catch (error) {
        this.logger.warn(`Failed to remove base temp directory: ${error}`);
      }
    }
  }
 
  // Parses stderr text into structured error objects.
  // lineOffset can be provided to adjust line numbers when headers were
  // inlined; callers should pass the same offset that was used during
  // header embedding so that the final error objects refer to the original
  // sketch lines.  This parameter is _used_ below to mutate parsed line
  // numbers, satisfying the TypeScript checker.
  private parseCompilerErrors(stderr: string, lineOffset: number = 0): CompilationError[] {
    return CompilerOutputParser.parseErrors(stderr, lineOffset);
  }
 
  private async compileWithArduinoCli(
    sketchFile: string,
    config: {
      fqbn: string;
      buildPath?: string;
      buildCachePath?: string;
    },
  ): Promise<{
    success: boolean;
    output: string;
    errors?: string;
    parsedErrors?: CompilationError[];
    binary?: Buffer;
  }> {
    // Arduino CLI expects the sketch DIRECTORY, not the file
    const sketchDir = sketchFile.slice(0, Math.max(0, sketchFile.lastIndexOf("/")));
    const args = this._buildCompileArgs(config, sketchDir);
 
    this.logger.info(`Executing arduino-cli ${args.join(" ")}`);
 
    try {
      const result = await this.processExecutor.execute("arduino-cli", args, {
        timeout: 60000, // 60s timeout for compilation
        stdio: "pipe",
      });
 
      // Check for spawn/execution errors
      if (result.error) {
        const errorMessage = `Failed to execute arduino-cli: ${result.error.message}. Make sure arduino-cli is installed and in PATH.`;
        this.logger.error(errorMessage);
        return {
          success: false,
          output: "",
          errors: errorMessage,
          parsedErrors: [{
            file: "system",
            line: 0,
            column: 0,
            type: "error",
            message: errorMessage,
          }],
        };
      }
 
      const output = result.stdout || "";
      const errors = result.stderr || "";
      const code = result.code;
 
      if (code === 0) {
        return await this._handleSuccessfulCompile(output, config, sketchDir);
      } else E{
        return this._handleFailedCompile(errors, sketchFile);
      }
    } catch (error) {
      const errorMessage = `Failed to execute arduino-cli: ${error instanceof Error ? error.message : String(error)}. Make sure arduino-cli is installed and in PATH.`;
      this.logger.error(errorMessage);
      return {
        success: false,
        output: "",
        errors: errorMessage,
        parsedErrors: [{
          file: "system",
          line: 0,
          column: 0,
          type: "error",
          message: errorMessage,
        }],
      };
    }
  }
 
  private _buildCompileArgs(
    config: {
      fqbn: string;
      buildPath?: string;
      buildCachePath?: string;
    },
    sketchDir: string,
  ): string[] {
    const args = [
      "compile",
      "--fqbn",
      config.fqbn,
      "--verbose",
    ];
 
    Iif (config.buildPath) {
      args.push("--build-path", config.buildPath);
    }
    Eif (config.buildCachePath) {
      args.push("--build-cache-path", config.buildCachePath);
    }
    args.push(sketchDir);
    return args;
  }
 
  private async _handleSuccessfulCompile(
    output: string,
    config: { buildPath?: string },
    sketchDir: string,
  ): Promise<{
    success: boolean;
    output: string;
    errors?: string;
    parsedErrors?: CompilationError[];
    binary?: Buffer;
  }> {
    const progSizeRegex = /(Sketch uses[^\n]*\.|Der Sketch verwendet[^\n]*\.)/;
    const ramSizeRegex = /(Global variables use[^\n]*\.|Globale Variablen verwenden[^\n]*\.)/;
 
    const progSizeMatch = progSizeRegex.exec(output);
    const ramSizeMatch = ramSizeRegex.exec(output);
 
    let parsedOutput = "";
    if (progSizeMatch && ramSizeMatch) {
      parsedOutput = `${progSizeMatch[0]}\n${ramSizeMatch[0]}\n\nBoard: Arduino UNO`;
    } else {
      parsedOutput = `Board: Arduino UNO (Simulation)`;
    }
 
    const buildOutputDir = config.buildPath || sketchDir;
    const binary = await this._discoverBuildBinary(buildOutputDir);
 
    return {
      success: true,
      output: parsedOutput,
      binary,
    };
  }
 
  private async _discoverBuildBinary(buildOutputDir: string): Promise<Buffer | undefined> {
    try {
      const hexCandidates = (await readdir(buildOutputDir))
        .filter((entry) => entry.endsWith(".hex"))
        .sort((a, b) => a.localeCompare(b));
      const preferred = hexCandidates.find((entry) => !entry.includes("with_bootloader")) || hexCandidates[0];
      Iif (preferred) {
        return await readFile(join(buildOutputDir, preferred));
      }
    } catch (error) {
      this.logger.debug(`[CompileCache] failed to read build hex output: ${error instanceof Error ? error.message : String(error)}`);
    }
    return undefined;
  }
 
  private _handleFailedCompile(
    errors: string,
    sketchFile: string,
  ): {
    success: boolean;
    output: string;
    errors: string;
    parsedErrors: CompilationError[];
    binary?: Buffer;
  } {
    this.logger.error(`arduino-cli compilation failed`);
    this.logger.error(`Full stderr output:\n${errors}`);
 
    const cleanedErrors = this._cleanCompilerErrors(errors, sketchFile);
    const structured = this.parseCompilerErrors(cleanedErrors || "");
    return {
      success: false,
      output: "",
      errors: cleanedErrors || "Compilation failed",
      parsedErrors: structured,
    };
  }
 
  private _cleanCompilerErrors(errors: string, sketchFile: string): string {
    const escapedPath = sketchFile.replaceAll(/[-/\\^$*+?.()|[\]{}]/g, String.raw`\$&`);
    return errors
      .replaceAll(new RegExp(escapedPath, "g"), "sketch.ino")
      .replaceAll(/\/[^\s:/]+\/temp\/[a-f0-9-]+\/[a-f0-9-]+\.ino/gi, "sketch.ino")
      .replaceAll(/Error during build: exit status \d+\s*/g, "")
      .trim();
  }
}
 
// singleton instance removed, not used anywhere