{"record":{"id":"a95fc77eeb82bddc","repo":"MemPalace/mempalace","slug":"invalid-backup-json-at-line-line-number","errorCode":null,"errorMessage":"invalid backup JSON at line {line_number}","messagePattern":"invalid backup JSON at line (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mempalace/encoding_repair.py","lineNumber":418,"sourceCode":"\n    with path.open(\n        \"r\",\n        encoding=\"utf-8\",\n    ) as handle:\n        # Skip the validated header.\n        handle.readline()\n\n        for line_number, line in enumerate(\n            handle,\n            start=2,\n        ):\n            if not line.strip():\n                continue\n\n            try:\n                record = json.loads(line)\n            except json.JSONDecodeError as exc:\n                raise ValueError(f\"invalid backup JSON at line {line_number}\") from exc\n\n            drawer_id = record.get(\"id\") if isinstance(record, dict) else None\n            document = record.get(\"original_document\") if isinstance(record, dict) else None\n\n            if not isinstance(\n                drawer_id,\n                str,\n            ) or not isinstance(\n                document,\n                str,\n            ):\n                raise ValueError(f\"invalid repair backup record at line {line_number}\")\n\n            yield drawer_id, document\n\n\ndef repair_collection(\n    collection,","sourceCodeStart":400,"sourceCodeEnd":436,"githubUrl":"https://github.com/MemPalace/mempalace/blob/06cb6987f02610784fefbad4b2bd5d026d164ba6/mempalace/encoding_repair.py#L400-L436","documentation":"Raised by _iter_backup_records while streaming backup records (lines 2+, since line 1 is the header) when a non-empty line fails json.loads with JSONDecodeError. The message includes the 1-based line number so the exact corrupt record can be found. Like the header checks, this is a fail-fast gate: restore validates the complete file before the first write, so one bad line aborts everything rather than half-applying a restore.","triggerScenarios":"A backup with a truncated tail (process killed or disk full during backup write), a hand-edited middle line with a syntax error, or CRLF/encoding artifacts inside one record. Any single malformed line among thousands triggers this.","commonSituations":"Interrupted repair runs; backups edited to remove a record by deleting half a line; log-rotation tools truncating files; files transferred through a channel that mangled multi-byte characters.","solutions":["Inspect the named line: sed -n 'Np' backup.jsonl (use the number from the error) — look for truncation or stray characters","If the tail is truncated from an interrupted run, discard the backup and regenerate it with repair_collection","If one record was hand-edited, either restore the full original or carefully fix the JSON on that line","Validate the whole file: python -c \"[json.loads(l) for l in open('backup.jsonl') if l.strip()]\""],"exampleFix":"# before: manual deletion broke a line\n# line 42: {\"id\": \"drawer-41\", \"original_document\": \"half of a docu\n# after: regenerate instead of editing backups\nresult = repair_collection(collection, apply=False)\nrestore_backup(collection, backup_path=result['backup_path'])","handlingStrategy":"validation","validationCode":"def backup_records_all_parse(path) -> bool:\n    try:\n        with open(path, encoding='utf-8') as fh:\n            fh.readline()\n            return all(json.loads(l) is not None for l in fh if l.strip())\n    except (json.JSONDecodeError, OSError):\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    restore_backup(collection, backup_path=path)\nexcept ValueError as e:\n    if \"invalid backup JSON at line\" in str(e):\n        line_no = int(str(e).rsplit('line', 1)[1])\n        sys.exit(f\"Corrupt record at line {line_no}; regenerate the backup\")","preventionTips":["Verify disk space before repairs; full disks truncate backup tails","Do not manually trim or append lines to backups","Archive backups compressed and read-only (chmod a-w) after creation"],"tags":["backup","json","repair","data-integrity"],"backgroundTag":null,"analyzedSha":"06cb6987f02610784fefbad4b2bd5d026d164ba6","analyzedAt":"2026-08-15T03:03:36.213Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}