{"record":{"id":"9589cec70a93990b","repo":"ruvnet/RuView","slug":"sync-magic-mismatch-got-0x-magic-08x","errorCode":null,"errorMessage":"Sync magic mismatch: got 0x{magic:08x}","messagePattern":"Sync magic mismatch: got 0x(.+?)","errorType":"exception","errorClass":"CSIParseError","httpStatus":null,"severity":"error","filePath":"archive/v1/src/hardware/csi_extractor.py","lineNumber":363,"sourceCode":"    \"\"\"\n\n    MAGIC = 0xC511A110\n    SIZE = 32\n    # <IBBBB QQ IB3x>\n    # I=magic, B=node_id, B=proto_ver, B=flags, B=reserved,\n    # Q=local_us, Q=epoch_us, I=sequence, B+3x=reserved\n    HEADER_FMT = '<IBBBBQQI4x'\n\n    @classmethod\n    def parse(cls, raw_data: bytes) -> SyncPacket:\n        if len(raw_data) < cls.SIZE:\n            raise CSIParseError(\n                f\"Sync packet too short: {len(raw_data)} bytes, need {cls.SIZE}\"\n            )\n        magic, node_id, proto_ver, flags_byte, _, local_us, epoch_us, seq = \\\n            struct.unpack_from(cls.HEADER_FMT, raw_data, 0)\n        if magic != cls.MAGIC:\n            raise CSIParseError(f\"Sync magic mismatch: got 0x{magic:08x}\")\n        return SyncPacket(\n            node_id=node_id,\n            proto_ver=proto_ver,\n            is_leader=bool(flags_byte & 0x01),\n            is_valid=bool(flags_byte & 0x02),\n            smoothed_used=bool(flags_byte & 0x04),\n            local_us=local_us,\n            epoch_us=epoch_us,\n            sequence=seq,\n            flags_raw=flags_byte,\n        )\n\n\nclass RouterCSIParser:\n    \"\"\"Parser for router CSI data format.\"\"\"\n    \n    def parse(self, raw_data: bytes) -> CSIData:\n        \"\"\"Parse router CSI data format.","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/ruvnet/RuView/blob/4685618388a5e49fad5b3005806f3bdd6a7c25c3/archive/v1/src/hardware/csi_extractor.py#L345-L381","documentation":"After the length check, SyncPacketParser validates the first dword against magic 0xC511A110 and raises CSIParseError('Sync magic mismatch: got 0x...') on mismatch. The 32 bytes came from a source that is not an ADR-110 sync packet — most often an ADR-018 CSI frame (magic 0xC5110001) routed to the wrong parser, or a desynced stream window.","triggerScenarios":"Dispatching a CSI frame buffer to SyncPacketParser.parse; a 32-byte window cut mid-stream that starts inside a packet; the wrong socket's data fed to the sync parser.","commonSituations":"Both frame types share one UDP socket in node deployments; naive length-based dispatch ('32 bytes must be a sync packet') colliding with short or garbled frames; version skew where an older firmware emits a different sync layout.","solutions":["Dispatch on magic, not length: peek the first 4 bytes — 0xC5110001 goes to the CSI parser, 0xC511A110 to the sync parser, anything else is dropped/resynced","If streams are interleaved, resync by scanning for the magic dword","Log the received magic: 0xC5110001 confirms a routing bug; random garbage confirms desync"],"exampleFix":"# before\nif len(pkt) == 32:\n    sync = SyncPacketParser.parse(pkt)  # may be a 32-byte-aligned CSI fragment\n\n# after\nimport struct\nmagic = struct.unpack_from('<I', pkt, 0)[0] if len(pkt) >= 4 else 0\nif magic == SyncPacketParser.MAGIC:\n    sync = SyncPacketParser.parse(pkt)\nelif magic == ESP32BinaryParser.MAGIC:\n    frame = ESP32BinaryParser().parse(pkt)","handlingStrategy":"validation","validationCode":"import struct\n\ndef peek_magic(buf: bytes) -> int:\n    return struct.unpack_from('<I', buf, 0)[0] if len(buf) >= 4 else 0\n\nmagic = peek_magic(pkt)\nif magic == SyncPacketParser.MAGIC:\n    sync = SyncPacketParser.parse(pkt)\nelif magic == ESP32BinaryParser.MAGIC:\n    frame = ESP32BinaryParser().parse(pkt)\nelse:\n    drop_or_resync(pkt)","typeGuard":"def is_sync_packet(buf: bytes) -> bool:\n    import struct\n    return len(buf) >= 32 and struct.unpack_from('<I', buf, 0)[0] == SyncPacketParser.MAGIC","tryCatchPattern":"try:\n    sync = SyncPacketParser.parse(pkt)\nexcept CSIParseError as e:\n    if 'Sync magic mismatch' in str(e):\n        logger.warning('wrong packet routed to sync parser; re-dispatch by magic')\n    raise","preventionTips":["Route packets by magic dword, never by length — CSI frames and sync packets share one socket","Peek the first 4 bytes before choosing a parser (0xC5110001 = CSI, 0xC511A110 = sync)","Log mismatched magic values: 0xC5110001 means a dispatch bug; random values mean stream desync"],"tags":["sync","binary","dispatch","adr-110","framing"],"backgroundTag":null,"analyzedSha":"4685618388a5e49fad5b3005806f3bdd6a7c25c3","analyzedAt":"2026-08-16T06:09:40.886Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}