{"record":{"id":"2f396bebd058ac51","repo":"ruvnet/RuView","slug":"invalid-magic-expected-0x-self-magic-08x-got-0x","errorCode":null,"errorMessage":"Invalid magic: expected 0x{self.MAGIC:08X}, got 0x{magic:08X}","messagePattern":"Invalid magic: expected 0x(.+?), got 0x(.+?)","errorType":"exception","errorClass":"CSIParseError","httpStatus":null,"severity":"error","filePath":"archive/v1/src/hardware/csi_extractor.py","lineNumber":197,"sourceCode":"        Args:\n            raw_data: Raw binary frame bytes.\n\n        Returns:\n            Parsed CSI data with amplitude/phase arrays shaped (n_antennas, n_subcarriers).\n\n        Raises:\n            CSIParseError: If frame is too short, has invalid magic, or malformed I/Q data.\n        \"\"\"\n        if len(raw_data) < self.HEADER_SIZE:\n            raise CSIParseError(\n                f\"Frame too short: need {self.HEADER_SIZE} bytes, got {len(raw_data)}\"\n            )\n\n        magic, node_id, n_antennas, n_subcarriers, freq_mhz, sequence, rssi_u8, noise_u8, \\\n            ppdu_byte, flags_byte = struct.unpack_from(self.HEADER_FMT, raw_data, 0)\n\n        if magic != self.MAGIC:\n            raise CSIParseError(\n                f\"Invalid magic: expected 0x{self.MAGIC:08X}, got 0x{magic:08X}\"\n            )\n\n        # Convert unsigned bytes to signed i8\n        rssi = rssi_u8 if rssi_u8 < 128 else rssi_u8 - 256\n        noise_floor = noise_u8 if noise_u8 < 128 else noise_u8 - 256\n\n        iq_count = n_antennas * n_subcarriers\n        iq_bytes = iq_count * 2\n        expected_len = self.HEADER_SIZE + iq_bytes\n\n        if len(raw_data) < expected_len:\n            raise CSIParseError(\n                f\"Frame too short for I/Q data: need {expected_len} bytes, got {len(raw_data)}\"\n            )\n\n        # Parse I/Q pairs as signed bytes\n        iq_raw = struct.unpack_from(f'<{iq_count * 2}b', raw_data, self.HEADER_SIZE)","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/ruvnet/RuView/blob/4685618388a5e49fad5b3005806f3bdd6a7c25c3/archive/v1/src/hardware/csi_extractor.py#L179-L215","documentation":"The first 4 bytes of an ADR-018 binary frame must equal magic 0xC5110001 (little-endian); a mismatch raises CSIParseError printing both the expected and received hex dwords. The buffer was long enough to unpack a header, but its first dword is not the protocol signature — classic mid-frame desync or wrong data routed to this parser.","triggerScenarios":"Parse starts mid-frame after earlier frames lost/gained bytes; ASCII CSI_DATA text fed to the binary parser; a 32-byte ADR-110 sync packet (magic 0xC511A110) dispatched to the CSI frame parser; byte-order bug in a custom collector.","commonSituations":"Serial overflow on the ESP32 dropping bytes so framing drifts; both packet types sharing one UDP socket with length-based dispatch; firmware/pipeline version skew changing the header layout.","solutions":["Resync: slide a 4-byte window over the buffer until the little-endian dword equals 0xC5110001, then parse from that offset","Dispatch packets by magic, not length: 0xC5110001 goes to ESP32BinaryParser, 0xC511A110 to SyncPacketParser","Reduce host-side latency or enable flow control so serial overruns stop corrupting framing"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import struct\n\ndef starts_with_csi_magic(buf: bytes) -> bool:\n    return len(buf) >= 4 and struct.unpack_from('<I', buf, 0)[0] == ESP32BinaryParser.MAGIC","typeGuard":null,"tryCatchPattern":"try:\n    data = parser.parse(frame)\nexcept CSIParseError as e:\n    logger.warning('magic mismatch — resyncing stream: %s', e)\n    buf = resync_to_magic(buf, ESP32BinaryParser.MAGIC)","preventionTips":["Always dispatch on the 4-byte magic dword; check it before calling parse","Implement a resync helper that scans forward to the next magic occurrence after any parse failure","Watch for serial-overflow symptoms (framing drift after load) and reduce host latency rather than retrying blindly"],"tags":["esp32","binary","framing","desync","adr-018"],"backgroundTag":null,"analyzedSha":"4685618388a5e49fad5b3005806f3bdd6a7c25c3","analyzedAt":"2026-08-16T06:09:40.886Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}