{"record":{"id":"bb86e6bdaca826ae","repo":"MemPalace/mempalace","slug":"backup-belongs-to-collection-backup-collection-r","errorCode":null,"errorMessage":"backup belongs to collection {backup_collection!r}, not {target_collection!r}","messagePattern":"backup belongs to collection (.+?), not (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mempalace/encoding_repair.py","lineNumber":585,"sourceCode":"\ndef restore_collection(\n    collection,\n    backup_path: Union[str, Path],\n    *,\n    batch_size: int = 500,\n) -> dict:\n    \"\"\"Restore original documents from an encoding-repair backup.\"\"\"\n    if batch_size < 1:\n        raise ValueError(\"batch_size must be at least 1\")\n\n    path = Path(backup_path)\n    header = _read_backup_header(path)\n\n    backup_collection = header.get(\"collection\")\n    target_collection = _collection_name(collection)\n\n    if backup_collection and target_collection and backup_collection != target_collection:\n        raise ValueError(\n            f\"backup belongs to collection {backup_collection!r}, not {target_collection!r}\"\n        )\n\n    # Validate the complete file before performing the first restore write.\n    validated = sum(1 for _record in _iter_backup_records(path))\n\n    restored = 0\n    batch_ids = []\n    batch_documents = []\n\n    for (\n        drawer_id,\n        document,\n    ) in _iter_backup_records(path):\n        batch_ids.append(drawer_id)\n        batch_documents.append(document)\n\n        if len(batch_ids) < batch_size:","sourceCodeStart":567,"sourceCodeEnd":603,"githubUrl":"https://github.com/MemPalace/mempalace/blob/06cb6987f02610784fefbad4b2bd5d026d164ba6/mempalace/encoding_repair.py#L567-L603","documentation":"Raised during restore-from-backup when the backup header's 'collection' name differs from the target ChromaDB collection being restored into. The check (backup_collection and target_collection and backup != target) only fires when both names are known — a headerless/anonymous backup is allowed — but a concrete mismatch is refused because replaying another collection's documents would attach verbatim content to the wrong wing/room structure. The message names both collections with !r quoting.","triggerScenarios":"Calling restore_backup(collection, backup_path=...) with a backup file produced by a repair of a different palace (e.g. a test palace, an old export, or another user profile), or after the collection was renamed/recreated with a different name.","commonSituations":"Multiple palaces on one machine (work vs personal) and backup paths mixed up; restoring a backup archived from a pre-migration collection name; CI tests reusing fixture backups against freshly named collections.","solutions":["Check both names in the message; use the backup against the collection it was created from","If the target collection was renamed, pass the original collection handle (or rename back) so names align","If you truly intend cross-collection restore, copy the backup and edit its header's 'collection' field to the target name — only when you are certain the documents belong there","List backups with their headers: head -1 each backup file to see its 'collection' value"],"exampleFix":"# before\nrestore_backup(new_collection, backup_path='backup-of-old-palace.jsonl')\n# ValueError: backup belongs to collection 'palace_old', not 'palace_new'\n\n# after: retarget deliberately\nimport json\nlines = open('backup.jsonl', encoding='utf-8').read().splitlines()\nh = json.loads(lines[0]); h['collection'] = 'palace_new'\nopen('backup-retargeted.jsonl', 'w', encoding='utf-8').write(\n    json.dumps(h) + '\\n' + '\\n'.join(lines[1:]) + '\\n')\nrestore_backup(new_collection, backup_path='backup-retargeted.jsonl')","handlingStrategy":"validation","validationCode":"import json\n\ndef backup_matches_collection(path, collection_name: str) -> bool:\n    header = json.loads(open(path, encoding='utf-8').readline())\n    bc = header.get('collection')\n    return not bc or bc == collection_name  # anonymous backups are allowed through","typeGuard":null,"tryCatchPattern":"try:\n    restore_backup(collection, backup_path=path)\nexcept ValueError as e:\n    if \"belongs to collection\" in str(e):\n        sys.exit(\"Wrong backup for this collection — locate the matching one (head -1 each backup)\")","preventionTips":["Name backup files after their collection (e.g. palace-main.backup.jsonl)","Record the collection name in your restore runbook alongside the backup path","Check head -1 <backup> before any restore to confirm the 'collection' field"],"tags":["backup","restore","collection-mismatch","repair","safety-check"],"backgroundTag":null,"analyzedSha":"06cb6987f02610784fefbad4b2bd5d026d164ba6","analyzedAt":"2026-08-15T03:03:36.213Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}