All files / server/services registry-manager.ts

83.21% Statements 243/292
71.42% Branches 135/189
86.04% Functions 37/43
86.13% Lines 236/274

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                19x 19x                                                                     340x     340x             340x 89x 102x     89x         340x             3x 3x   1x 1x 1x 1x 1x     1x                 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x             139x 139x       139x 139x     139x                       139x 139x 139x                       61x       61x 51x               52x   52x 44x               99x     99x       99x 623x 623x 623x 623x                   176x 77x 77x                 53x 53x                                         1x 1x   1x 1x                           636x 636x 636x 636x 636x   636x 623x   623x       623x       623x   623x       623x         636x                                       636x 636x 636x 636x 636x 636x   636x 624x     624x         624x         624x         624x         624x     636x                           636x 636x 636x     636x 636x               636x                 636x   636x                             636x 636x 636x 636x 636x   636x 636x       636x                                                           636x                     38x         38x                     38x 38x     38x 38x 38x 38x 38x 38x   38x 3x 3x                         74x 74x 74x             75x 75x 1x 1x     296x 74x 3x 3x             71x   74x 74x                                                     35x 35x     35x     35x 74x     35x 35x 35x 2x   33x   35x 35x 4x 4x                 48x 48x     46x     44x   2x 2x                 33x 33x             33x 33x             33x     33x         33x 103x 33x 33x   33x         33x   33x 33x     33x     33x             33x 18x     33x 33x 33x     33x 22x 22x                   33x 33x 33x 33x     33x 30x 30x 30x       3x 2x 2x     1x                                     51x 51x     51x 22x 22x 22x 1x 1x 1x     22x                 5x             75x 75x 75x 75x 75x 75x         75x 75x 75x   75x         75x 23x 23x                     45x     45x         45x         45x     45x           45x 45x 45x 45x 45x             26x                 56x         56x 56x 56x   56x   340x 256x 56x 56x 56x         56x   56x         342x           674x 58x              
// registry-manager.ts
// Manages I/O Registry for Arduino pin states with debouncing and change detection
 
import type { IOPinRecord } from "@shared/schema";
import type { PinStateBatcher } from "./pin-state-batcher";
import type { SerialOutputBatcher, SerialOutputTelemetry } from "./serial-output-batcher";
import { Logger } from "@shared/logger";
import { computePinConflict, ensurePinModeOperation } from "./utils/pin-validator";
import { createWriteStream, type WriteStream } from "node:fs";
import { join } from "node:path";
 
type RegistryUpdateCallback = (
  registry: IOPinRecord[],
  baudrate: number | undefined,
  reason?: string,
) => void;
 
interface PerformanceMetrics {
  timestamp: number;
  intendedPinChangesPerSecond: number;
  actualPinChangesPerSecond: number;
  droppedPinChangesPerSecond: number;
  batchesPerSecond: number;
  avgStatesPerBatch: number;
  serialOutputPerSecond: number;
  serialBytesPerSecond: number;
  serialBytesTotal: number;
  serialIntendedBytesPerSecond: number;
  serialDroppedBytesPerSecond: number;
}
 
type TelemetryUpdateCallback = (metrics: PerformanceMetrics) => void;
 
interface RegistryManagerConfig {
  onUpdate?: RegistryUpdateCallback;
  onTelemetry?: TelemetryUpdateCallback;
  enableTelemetry?: boolean;
}
 
/**
 * Helper to clean up pin record by removing line: 0 from usedAt/definedAt
 */
function cleanupPinRecord(pin: IOPinRecord): IOPinRecord {
  // Use a mutable copy so we can delete optional fields safely
  const cleaned: IOPinRecord = { ...pin };
 
  // Remove definedAt if line is 0
  Iif (cleaned.definedAt?.line === 0) {
    delete cleaned.definedAt;
  }
 
  // Filter out usedAt entries with line: 0 that have no operation (true placeholders).
  // Runtime entries always have line: 0 but carry a non-empty operation string
  // (e.g. "pinMode:0") – those must be preserved so the client can detect conflicts.
  if (cleaned.usedAt && cleaned.usedAt.length > 0) {
    cleaned.usedAt = cleaned.usedAt.filter(
      entry => entry.line !== 0 || !!entry.operation,
    );
    // Remove usedAt entirely if empty
    Iif (cleaned.usedAt.length === 0) {
      delete cleaned.usedAt;
    }
  }
 
  return cleaned;
}
 
function mergeUsedAtEntries(
  existing: IOPinRecord["usedAt"],
  incoming: IOPinRecord["usedAt"],
): IOPinRecord["usedAt"] {
  const merged = [...(existing ?? []), ...(incoming ?? [])];
  if (merged.length === 0) return undefined;
 
  const unique = new Map<string, (typeof merged)[number]>();
  for (const entry of merged) {
    const key = `${entry.operation}@${entry.line}`;
    Eif (!unique.has(key)) {
      unique.set(key, entry);
    }
  }
  return Array.from(unique.values());
}
 
/**
 * RegistryManager handles the collection and management of Arduino pin states.
 * It provides debouncing to minimize WebSocket traffic and change detection
 * to avoid sending duplicate registry data.
 */
export class RegistryManager {
  private registry: IOPinRecord[] = [];
  private isCollecting = false;
  private registryHash = "";
  private debounceTimer: NodeJS.Timeout | null = null;
  private waitTimer: NodeJS.Timeout | null = null;
  private heartbeatInterval: NodeJS.Timeout | null = null;
  private waitingForRegistry = false;
  private isDirty = false;
  private baudrate: number | undefined = undefined; // undefined = Serial.begin() not found in code
  private destroyed = false; // Prevent logging after destruction
  private debugStream: WriteStream | null = null; // Non-blocking telemetry stream
  private telemetryPaused = false; // Used to keep telemetry heartbeat paused when requested
  /**
   * Anti-spam: tracks (pin, mode) pairs already sent via updatePinMode so that
   * repeated calls (e.g. from loop()) never trigger a redundant WS message.
   * Keyed as "<pinId>:<mode>" (e.g. "13:1").
   * Reset on reset() / next program start.
   */
  private readonly runtimeSentFingerprints = new Set<string>();
  private readonly logger = new Logger("RegistryManager");
  private readonly onUpdateCallback?: RegistryUpdateCallback;
  private readonly onTelemetryCallback?: TelemetryUpdateCallback;
  private readonly enableTelemetry: boolean;
  private pinStateBatcher: PinStateBatcher | null = null; // Reference to PinStateBatcher for telemetry
  private serialOutputBatcher: SerialOutputBatcher | null = null; // Reference to SerialOutputBatcher for telemetry
  
  // Telemetry tracking
  private readonly telemetry = {
    incomingEvents: 0,
    sentBatches: 0,
    serialOutputEvents: 0, // track serial output events
    serialOutputBytes: 0, // bytes in current interval
    serialOutputBytesTotal: 0, // cumulative bytes since start
    // timestamp at which the simulation was paused (if present)
    pauseTimestamp: null as number | null,
    lastReportTime: Date.now(),
  };
 
  constructor(config: RegistryManagerConfig = {}) {
    this.onUpdateCallback = config.onUpdate;
    this.onTelemetryCallback = config.onTelemetry;
    this.enableTelemetry = config.enableTelemetry ?? false;
 
    // Do NOT start heartbeat here. The telemetry callback (onTelemetry) requires
    // executionState.telemetryCallback to be set first, which happens later in
    // ExecutionManager.runSketch(). The heartbeat will start when the first
    // batcher is attached (setPinStateBatcher/setSerialOutputBatcher).
  }
  
  /**
   * Set reference to PinStateBatcher for telemetry tracking
   */
  setPinStateBatcher(batcher: PinStateBatcher | null): void {
    this.pinStateBatcher = batcher;
 
    // Start telemetry heartbeat when the first batcher is attached.
    // This ensures we have metrics even if no IO_REGISTRY markers were emitted.
    if (batcher && this.enableTelemetry && this.onTelemetryCallback && !this.telemetryPaused) {
      this.startHeartbeat();
    }
  }
  
  /**
   * Set reference to SerialOutputBatcher for telemetry tracking
   */
  setSerialOutputBatcher(batcher: SerialOutputBatcher | null): void {
    this.serialOutputBatcher = batcher;
 
    if (batcher && this.enableTelemetry && this.onTelemetryCallback && !this.telemetryPaused) {
      this.startHeartbeat();
    }
  }
  
  /**
   * Start 1-second heartbeat for telemetry reporting
   */
  private startHeartbeat(): void {
    Iif (this.heartbeatInterval) {
      return;
    }
    Iif (this.telemetryPaused) {
      return;
    }
 
    this.heartbeatInterval = setInterval(() => {
      Eif (!this.destroyed) {
        const metrics = this.getPerformanceMetrics();
        Eif (this.onTelemetryCallback) {
          this.onTelemetryCallback(metrics);
        }
      }
    }, 1000);
  }
 
  /**
   * Stop telemetry heartbeat and clear interval
   */
  private stopTelemetry(): void {
    if (this.heartbeatInterval) {
      clearInterval(this.heartbeatInterval);
      this.heartbeatInterval = null;
    }
  }
 
  /**
   * Pause telemetry heartbeat (called when simulation is paused)
   * Stops sending telemetry data while paused
   */
  pauseTelemetry(): void {
    this.telemetryPaused = true;
    this.stopTelemetry();
  }
 
  /**
   * Inform the manager of the exact timestamp when the simulation was paused.
   * This allows subsequent events (e.g. those buffered in OS pipes) to be
   * labelled with a correct 'pause' time rather than the later resume time.
   *
   * The current implementation simply stores the value for future use; the
   * consuming code may choose how to apply it. See usages in
   * SandboxRunner.pause()/resume().
   */
  markPauseTime(ts: number | null): void {
    this.telemetry.pauseTimestamp = ts;
  }
 
  /**
   * Resume telemetry heartbeat (called when simulation is resumed)
   * Resets counters and restarts the heartbeat
   */
  resumeTelemetry(): void {
    this.telemetryPaused = false;
    Eif (this.onTelemetryCallback && this.enableTelemetry) {
      // Reset timestamp for fresh start after pause
      this.telemetry.lastReportTime = Date.now();
      this.startHeartbeat();
    }
  }
  
  /**
   * Calculate pin metrics from PinStateBatcher telemetry
   */
  private getPinMetrics(timeElapsedSec: number): {
    intendedPinChangesPerSecond: number;
    actualPinChangesPerSecond: number;
    droppedPinChangesPerSecond: number;
    batchesPerSecond: number;
    avgStatesPerBatch: number;
  } {
    let intendedPinChangesPerSecond = 0;
    let actualPinChangesPerSecond = 0;
    let droppedPinChangesPerSecond = 0;
    let batchesPerSecond = 0;
    let avgStatesPerBatch = 0;
 
    if (this.pinStateBatcher) {
      const batcherTelemetry = this.pinStateBatcher.getTelemetryAndReset();
      
      intendedPinChangesPerSecond = timeElapsedSec > 0
        ? Math.round((batcherTelemetry.intended / timeElapsedSec) * 10) / 10
        : 0;
      
      actualPinChangesPerSecond = timeElapsedSec > 0
        ? Math.round((batcherTelemetry.actual / timeElapsedSec) * 10) / 10
        : 0;
      
      droppedPinChangesPerSecond = intendedPinChangesPerSecond - actualPinChangesPerSecond;
      
      batchesPerSecond = timeElapsedSec > 0
        ? Math.round((batcherTelemetry.batches / timeElapsedSec) * 10) / 10
        : 0;
      
      avgStatesPerBatch = batcherTelemetry.batches > 0
        ? Math.round((batcherTelemetry.actual / batcherTelemetry.batches) * 10) / 10
        : 0;
    }
 
    return {
      intendedPinChangesPerSecond,
      actualPinChangesPerSecond,
      droppedPinChangesPerSecond,
      batchesPerSecond,
      avgStatesPerBatch,
    };
  }
 
  /**
   * Calculate serial output metrics from SerialOutputBatcher telemetry
   */
  private getSerialMetrics(timeElapsedSec: number): {
    serialOutputPerSecond: number;
    serialBytesPerSecond: number;
    serialIntendedBytesPerSecond: number;
    serialDroppedBytesPerSecond: number;
    serialBytesTotal: number;
    batcherTelemetry: SerialOutputTelemetry | null;
  } {
    let serialOutputPerSecond = 0;
    let serialBytesPerSecond = 0;
    let serialIntendedBytesPerSecond = 0;
    let serialDroppedBytesPerSecond = 0;
    let serialBytesTotal = this.telemetry.serialOutputBytesTotal;
    let batcherTelemetry: SerialOutputTelemetry | null = null;
 
    if (this.serialOutputBatcher) {
      batcherTelemetry = this.serialOutputBatcher.getTelemetryAndReset();
      
      // Serial events = number of chunks sent (batch outputs)
      serialOutputPerSecond = timeElapsedSec > 0
        ? Math.round((batcherTelemetry.chunks / timeElapsedSec) * 10) / 10
        : 0;
      
      // Serial bytes = actual bytes sent after rate limiting
      serialBytesPerSecond = timeElapsedSec > 0
        ? Math.round((batcherTelemetry.actual / timeElapsedSec) * 10) / 10
        : 0;
      
      // Intended bytes = total bytes enqueued (before drops)
      serialIntendedBytesPerSecond = timeElapsedSec > 0
        ? Math.round((batcherTelemetry.intended / timeElapsedSec) * 10) / 10
        : 0;
      
      // Dropped bytes per second
      serialDroppedBytesPerSecond = timeElapsedSec > 0
        ? Math.round((batcherTelemetry.dropped / timeElapsedSec) * 10) / 10
        : 0;
      
      // Total bytes from batcher (cumulative, never reset)
      serialBytesTotal = batcherTelemetry.totalBytes;
    }
 
    return {
      serialOutputPerSecond,
      serialBytesPerSecond,
      serialIntendedBytesPerSecond,
      serialDroppedBytesPerSecond,
      serialBytesTotal,
      batcherTelemetry,
    };
  }
 
  /**
   * Calculate and return current performance metrics
   */
  private getPerformanceMetrics(): PerformanceMetrics {
    const now = Date.now();
    const timeElapsedMs = now - this.telemetry.lastReportTime;
    const timeElapsedSec = timeElapsedMs / 1000;
 
    // Delegate to specialized metrics functions
    const pinMetrics = this.getPinMetrics(timeElapsedSec);
    const serialMetrics = this.getSerialMetrics(timeElapsedSec);
 
    const {
      intendedPinChangesPerSecond,
      actualPinChangesPerSecond,
      droppedPinChangesPerSecond,
      batchesPerSecond,
      avgStatesPerBatch,
    } = pinMetrics;
 
    const {
      serialOutputPerSecond,
      serialBytesPerSecond,
      serialIntendedBytesPerSecond,
      serialDroppedBytesPerSecond,
      serialBytesTotal,
      batcherTelemetry,
    } = serialMetrics;
 
    const metrics: PerformanceMetrics = {
      timestamp: now,
      intendedPinChangesPerSecond,
      actualPinChangesPerSecond,
      droppedPinChangesPerSecond,
      batchesPerSecond,
      avgStatesPerBatch,
      serialOutputPerSecond,
      serialBytesPerSecond,
      serialBytesTotal, // Now from SerialOutputBatcher
      serialIntendedBytesPerSecond,
      serialDroppedBytesPerSecond,
    };
 
    // Reset counters for next period
    this.telemetry.incomingEvents = 0;
    this.telemetry.sentBatches = 0;
    this.telemetry.serialOutputEvents = 0;
    this.telemetry.serialOutputBytes = 0;
    this.telemetry.lastReportTime = now;
 
    Eif (!this.destroyed) {
      this.logger.debug(
        `Telemetry: intended: ${intendedPinChangesPerSecond} pin/s, actual: ${actualPinChangesPerSecond} pin/s (dropped: ${droppedPinChangesPerSecond}), ${batchesPerSecond} bat/s, ${avgStatesPerBatch} st/bat, ${serialOutputPerSecond} serial/s, SERIAL: intended=${batcherTelemetry?.intended ?? 0} bytes, actual=${batcherTelemetry?.actual ?? 0} bytes, dropped=${batcherTelemetry?.dropped ?? 0} bytes (${serialDroppedBytesPerSecond} B/s)`,
      );
 
      Iif (process.env.NODE_ENV !== "test" && !process.env.VITEST) {
        // DEBUG: Write telemetry to file for inspection
        try {
          const debugPath = join(process.cwd(), "temp", "telemetry-debug.jsonl");
          const debugLine = JSON.stringify({
            timestamp: new Date(now).toISOString(),
            serial: {
              outputPerSec: serialOutputPerSecond,
              bytesPerSec: serialBytesPerSecond,
              intendedPerSec: serialIntendedBytesPerSecond,
              intendedBytes: batcherTelemetry?.intended ?? 0,
              actualBytes: batcherTelemetry?.actual ?? 0,
              droppedBytes: batcherTelemetry?.dropped ?? 0,
              droppedPerSec: serialDroppedBytesPerSecond,
              bytesTotal: serialBytesTotal,
            },
          }) + "\n";
          // Non-blocking write via stream
          if (!this.debugStream) {
            const stream = createWriteStream(debugPath, { flags: "a" });
            stream.on("error", () => { this.debugStream = null; });
            this.debugStream = stream;
          }
          this.debugStream.write(debugLine);
        } catch {
          // Silently ignore file write errors
        }
      }
    }
 
    return metrics;
  }
 
  /**
   * Start collecting registry data (called when [[IO_REGISTRY_START]] marker is received)
   * 
   * NEW: Implements "Initial Sync Flush" to ensure runtime pin definitions (e.g., from updatePinMode)
   * are sent to clients before the registry is cleared. This prevents losing pin definitions that arrive
   * before the IO_REGISTRY_START marker.
   */
  startCollection(): void {
    Iif (this.destroyed) return;
    // Collection start (not logged individually — too noisy).
    
    // ROBUSTNESS: Flush current registry state before clearing
    // This ensures pins added via updatePinMode before IO_REGISTRY_START marker are sent
    Iif (!this.waitingForRegistry && this.registry.length > 0 && this.isDirty) {
      const hasDefinedPins = this.registry.some((p) => p.defined);
      if (hasDefinedPins) {
        this.logger.info(
          `Initial Sync Flush: Sending ${this.registry.length} pins before clearing for new collection`,
        );
        const preCollectionHash = this.computeRegistryHash();
        this.sendNow(preCollectionHash, "pre-collection-flush");
      }
    }
    
    this.isCollecting = true;
    this.registry = [];
    
    // Reset telemetry counters and restart heartbeat
    this.telemetry.incomingEvents = 0;
    this.telemetry.sentBatches = 0;
    this.telemetry.serialOutputEvents = 0;
    this.telemetry.serialOutputBytes = 0;
    this.telemetry.serialOutputBytesTotal = 0;
    this.telemetry.lastReportTime = Date.now();
    
    if (this.onTelemetryCallback && this.enableTelemetry) {
      this.stopTelemetry(); // Clear previous heartbeat
      this.startHeartbeat();
    }
  }
 
  /**
   * Detect and annotate conflicts on a single pin record in-place.
   *
   * Checks:
   *   – Multiple distinct pinMode modes in usedAt  → TC11 / multi-mode conflict
   *   – INPUT/INPUT_PULLUP mode + digitalWrite/analogWrite in usedAt  → TC9
   *   – OUTPUT mode combined with digitalRead/analogRead in usedAt  → TC9b
   */
  private detectConflictsForPin(pin: IOPinRecord): void {
    const conflictInfo = computePinConflict(pin);
    pin.conflict = conflictInfo.conflict;
    pin.conflictMessage = conflictInfo.conflict ? conflictInfo.conflictMessage : undefined;
  }
 
  /**
   * Add a pin record to the registry (called for each [[IO_PIN:...]] marker)
   */
  addPin(pinRecord: IOPinRecord): void {
    Iif (this.destroyed) return;
    if (!this.isCollecting) {
      this.logger.warn("Received pin record while not collecting - ignoring");
      return;
    }
    // Individual pin additions are not logged (20 per start is too noisy).
    const existingIndex = this.registry.findIndex((p) => p.pin === pinRecord.pin);
    if (existingIndex >= 0) {
      const existing = this.registry[existingIndex];
      this.registry[existingIndex] = {
        ...existing,
        ...pinRecord,
        defined: existing.defined || pinRecord.defined,
        usedAt: mergeUsedAtEntries(existing.usedAt, pinRecord.usedAt),
      };
    } else {
      this.registry.push(pinRecord);
    }
    this.isDirty = true;
    this.telemetry.incomingEvents++;
  }
 
  /**
   * Telemetry-only: called when a pin value change is observed outside of the
   * registry collection. These events do not mutate the registry itself.
   */
  updatePinValue(_pin: number, _value: number): void {
    if (this.destroyed) return;
    // count the incoming event for telemetry purposes
    this.telemetry.incomingEvents++;
    // no structural change, so nothing else to do
  }
 
  /**
   * Telemetry-only counterpart for PWM changes.
   */
  updatePinPWM(_pin: number, _value: number): void {
    if (this.destroyed) return;
    this.telemetry.incomingEvents++;
  }
 
  /**
   * Finish collecting and send registry immediately (called when [[IO_REGISTRY_END]] marker is received)
   * Structural changes (new pins discovered) must reach UI immediately, not throttled.
   */
  finishCollection(): void {
    Iif (this.destroyed) return;
    this.logger.debug(
      `Registry collection complete: ${this.registry.length} pins`,
    );
    this.isCollecting = false;
 
    // Annotate conflicts now that all pins from the IO_REGISTRY burst are known
    for (const pin of this.registry) {
      this.detectConflictsForPin(pin);
    }
 
    this.isDirty = true;
    const nextHash = this.computeRegistryHash();
    if (nextHash === this.registryHash) {
      this.isDirty = false;
    } else {
      this.sendNow(nextHash, "collection-complete");
    }
    this.waitingForRegistry = false;
    if (this.waitTimer) {
      clearTimeout(this.waitTimer);
      this.waitTimer = null;
    }
  }
 
  /**
   * Set the baudrate (parsed from Serial.begin() in user code)
   * Only set if baudrate is explicitly defined in the code (not default 9600)
   */
  setBaudrate(baudrate: number): void {
    Iif (this.destroyed) return;
    if (baudrate > 0) {
      // Only set if non-default (not 9600)
      // This ensures we know that Serial.begin() was actually in the code
      if (baudrate === 9600) {
        // Default 9600 might mean Serial.begin() wasn't in the code
        // Leave as undefined so we don't send a false positive
        this.logger.debug(`Serial.begin(9600) - not sending default bandrate`);
      } else {
        this.logger.debug(`Baudrate from Serial.begin(): ${baudrate}`);
        this.baudrate = baudrate;
      }
    }
  }
 
  /**
   * Check if we should skip this pin mode update (anti-spam check)
   */
  private shouldSkipPinMode(pin: number, mode: number): boolean {
    const fingerprint = `${pin}:${mode}`;
    return this.runtimeSentFingerprints.has(fingerprint);
  }
 
  /**
   * Track that we've sent this pin mode combination
   */
  private markPinModeSent(pin: number, mode: number): void {
    const fingerprint = `${pin}:${mode}`;
    this.runtimeSentFingerprints.add(fingerprint);
  }
 
  /**
   * Update a pin's mode at runtime (called when [[PIN_MODE:pin:mode]] is received)
   */
  updatePinMode(pin: number, mode: number): void {
    Iif (this.destroyed) return;
 
    // Anti-spam: skip if this (pin, mode) was already sent
    Iif (this.shouldSkipPinMode(pin, mode)) {
      this.telemetry.incomingEvents++;
      return;
    }
 
    const pinStr = pin >= 14 && pin <= 19 ? `A${pin - 14}` : String(pin);
    const existing = this.registry.find((p) => p.pin === pinStr);
    const isNewRecord = !existing;
    const wasDefinedBefore = existing?.defined ?? false;
 
    this.logger.debug(
      `updatePinMode: pin=${pin} (${pinStr}), mode=${mode}, existing=${!!existing}, wasDefinedBefore=${wasDefinedBefore}`,
    );
 
    // Create or update registry record
    const record: IOPinRecord = existing ?? { pin: pinStr, defined: true, pinMode: mode, usedAt: [] };
 
    record.pinMode = mode;
    record.defined = true;
 
    // Ensure the mode operation is tracked for conflict detection
    ensurePinModeOperation(record, mode);
 
    // Validate and detect conflicts
    const { shouldSend, reason } = this.validateAndDetectConflicts(
      record,
      isNewRecord,
      wasDefinedBefore,
      pinStr,
    );
 
    if (!existing) {
      this.registry.push(record);
    }
 
    this.telemetry.incomingEvents++;
    this.markPinModeSent(pin, mode);
    this.isDirty = true;
 
    // Send based on validation result
    if (shouldSend && !this.isCollecting && !this.waitingForRegistry) {
      const nextHash = this.computeRegistryHash();
      this.sendNow(nextHash, reason);
    }
  }
 
  private validateAndDetectConflicts(
    record: IOPinRecord,
    isNewRecord: boolean,
    wasDefinedBefore: boolean,
    pinStr: string,
  ): { shouldSend: boolean; reason: string } {
    const hadConflict = Boolean(record.conflict);
    const conflictInfo = computePinConflict(record);
    record.conflict = conflictInfo.conflict;
    record.conflictMessage = conflictInfo.conflict ? conflictInfo.conflictMessage : undefined;
 
    // Send on first-time definition
    if (!wasDefinedBefore) {
      this.logger.debug(`Structural change: pin ${pinStr} marked as defined, sending immediately`);
      this.logger.info(`Registry send trigger: first-time pin use ${pinStr} (pinMode:${record.pinMode})`);
      return { shouldSend: true, reason: isNewRecord ? "pin-new-record" : "pin-defined-changed" };
    }
 
    // Send on new conflict detection
    if (conflictInfo.conflict && !hadConflict) {
      this.isDirty = true;
      return { shouldSend: true, reason: "pin-conflict-detected" };
    }
 
    return { shouldSend: false, reason: "" };
  }
 
 
 
  /**
   * Track a serial output event (called when serial data is sent)
   */
  trackSerialOutput(bytes: number = 0): void {
    if (this.destroyed) return;
    this.telemetry.serialOutputEvents++;
    this.telemetry.serialOutputBytes += bytes;
    this.telemetry.serialOutputBytesTotal += bytes;
  }
 
  /**
   * Enable message queuing until first registry is sent
   */
  enableWaitMode(timeoutMs: number = 1500): void {
    Iif (this.destroyed) return;
    this.waitingForRegistry = true;
 
    // Set fallback timer to release queue if registry never arrives
    this.waitTimer = setTimeout(() => {
      Eif (this.waitingForRegistry) {
        this.logger.warn("Registry wait timeout - releasing queue");
        if (this.isDirty) {
          const nextHash = this.computeRegistryHash();
          Eif (nextHash !== this.registryHash) {
            this.sendNow(nextHash, "wait-timeout-flush");
          }
        }
        this.waitingForRegistry = false;
      }
    }, timeoutMs);
  }
 
  /**
   * Check if manager is currently waiting for first registry
   */
  isWaiting(): boolean {
    return this.waitingForRegistry;
  }
 
  /**
   * Reset the manager state (called when simulation stops)
   */
  reset(): void {
    this.registry = [];
    this.isCollecting = false;
    this.registryHash = "";
    this.waitingForRegistry = false;
    this.isDirty = false;
    this.runtimeSentFingerprints.clear(); // reset anti-spam state for new sketch run
 
    // Ensure telemetry is enabled for the next run.
    // `pauseTelemetry()` sets `telemetryPaused` to true, which must be cleared
    // on reset so that subsequent simulations can restart the heartbeat.
    this.telemetryPaused = false;
    this.destroyed = false; // Reset destroyed flag so heartbeat can run in next simulation
    this.stopTelemetry();
 
    Iif (this.debounceTimer) {
      clearTimeout(this.debounceTimer);
      this.debounceTimer = null;
    }
 
    if (this.waitTimer) {
      clearTimeout(this.waitTimer);
      this.waitTimer = null;
    }
 
    // Reset complete (not logged — happens on every stop/start cycle).
  }
 
  /**
   * Destroy the manager and prevent further logging or callbacks
   * This is called during test teardown to prevent "log after tests are done" errors
   */
  destroy(): void {
    this.destroyed = true;
    
    // Clear all timers immediately
    Iif (this.debounceTimer) {
      clearTimeout(this.debounceTimer);
      this.debounceTimer = null;
    }
 
    Iif (this.waitTimer) {
      clearTimeout(this.waitTimer);
      this.waitTimer = null;
    }
    
    this.stopTelemetry();
 
    // Close debug stream if open
    Iif (this.debugStream) {
      this.debugStream.end();
      this.debugStream = null;
    }
 
    // Reset state without logging
    this.registry = [];
    this.isCollecting = false;
    this.registryHash = "";
    this.waitingForRegistry = false;
    this.isDirty = false;
  }
 
  /**
   * Get current registry snapshot
   */
  getRegistry(): readonly IOPinRecord[] {
    return [...this.registry];
  }
 
  /**
   * Immediately send the registry via callback
   * Cancels pending throttle timer to ensure structural changes reach UI immediately
   */
  private sendNow(hash: string, reason?: string): void {
    // Cancel pending throttle timer to ensure immediate send
    Iif (this.debounceTimer) {
      clearTimeout(this.debounceTimer);
      this.debounceTimer = null;
    }
 
    this.registryHash = hash;
    this.isDirty = false;
    this.telemetry.sentBatches++;
 
    Eif (this.onUpdateCallback) {
      // Enhanced logging: show which pins are being sent, especially pin 13
      const pinsList = this.registry.map((p) => `${p.pin}(def=${p.defined})`).join(",");
      const hasPin13 = this.registry.some((p) => p.pin === "13");
      Eif (reason) {
        const baudInfo = this.baudrate === undefined ? " | baud=not-defined" : ` | baud=${this.baudrate}`;
        this.logger.info(
          `📤 Registry SEND [${reason}]: ${this.registry.length} pins [${pinsList}]${baudInfo} ${hasPin13 ? "✅ PIN13_INCLUDED" : "❌ NO_PIN13"}`,
        );
      }
      // Clean up registry to remove useless line: 0 entries
      const cleanedRegistry = this.registry.map(cleanupPinRecord);
      // Only send baudrate if it was actually defined in the code
      this.onUpdateCallback(cleanedRegistry, this.baudrate, reason);
    }
  }
 
  private computeRegistryHash(): string {
    const normalized = this.registry.map((pin) => ({
      pin: pin.pin,
      defined: pin.defined,
      pinMode: pin.pinMode,
      usedAt: pin.usedAt ? [...pin.usedAt] : [],
    }));
    normalized.sort((a, b) => a.pin.localeCompare(b.pin));
    return JSON.stringify(normalized);
  }
 
  /**
   * Legacy hash calculation removed in favor of isDirty flag
   */
}