{"record":{"id":"feb9b24224672b72","repo":"siyuan-note/siyuan","slug":"invalid-encrypted-index-compatibility-metadata","errorCode":null,"errorMessage":"invalid encrypted index compatibility metadata","messagePattern":"invalid encrypted index compatibility metadata","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/encrypted_index.go","lineNumber":56,"sourceCode":"\t\tif err = db.QueryRow(\"SELECT count(*) FROM sqlite_master WHERE type = 'table'\").Scan(&tables); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif tables != 0 {\n\t\t\treturn errors.New(\"encrypted index has no compatibility metadata\")\n\t\t}\n\t\tif _, err = db.Exec(\"CREATE TABLE encrypted_index_meta (kind TEXT NOT NULL, schema_version INTEGER NOT NULL, cipher_settings TEXT NOT NULL)\"); err != nil {\n\t\t\treturn err\n\t\t}\n\t\t_, err = db.Exec(\"INSERT INTO encrypted_index_meta VALUES (?, ?, ?)\", kind, schema, string(encoded))\n\t\treturn err\n\t}\n\tvar storedKind, storedSettings string\n\tvar storedSchema, rows int\n\tif err = db.QueryRow(\"SELECT count(*) FROM encrypted_index_meta\").Scan(&rows); err != nil {\n\t\treturn err\n\t}\n\tif rows != 1 {\n\t\treturn errors.New(\"invalid encrypted index compatibility metadata\")\n\t}\n\tif err = db.QueryRow(\"SELECT kind, schema_version, cipher_settings FROM encrypted_index_meta\").Scan(&storedKind, &storedSchema, &storedSettings); err != nil {\n\t\treturn err\n\t}\n\tif storedKind != kind || storedSchema != schema || storedSettings != string(encoded) {\n\t\treturn errors.New(\"incompatible encrypted index\")\n\t}\n\treturn nil\n}\n","sourceCodeStart":38,"sourceCodeEnd":66,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/util/encrypted_index.go#L38-L66","documentation":"The encrypted_index_meta table exists but does not contain exactly one row. The compatibility contract requires a single metadata row holding kind, schema_version, and the cipher-settings fingerprint. Zero rows (table created but insert never ran, e.g. an interrupted initialization) or multiple rows mean the metadata is corrupt, so the library cannot authenticate which format the index was built with.","triggerScenarios":"OpenEncryptedDB/OpenEncryptedBlockTreeDB on a database where encrypted_index_meta has 0 or >= 2 rows — e.g. a partially created index where the CREATE TABLE succeeded but the INSERT failed or ran twice, or manual edits to the database.","commonSituations":"Kernel crash or power loss between table creation and metadata insertion; concurrent kernel instances writing the same workspace index; a user or tooling manipulating the SQLite file directly.","solutions":["Treat the index as unrecoverable metadata: delete the index file and let the kernel rebuild it from source documents (source ciphertext is not touched)","If multiple rows exist, inspect with 'SELECT * FROM encrypted_index_meta' to see if a concurrent writer duplicated the row, then remove the offending writer","Ensure only one SiYuan kernel instance runs against a workspace at a time before rebuilding"],"exampleFix":"-- inspect the corrupt metadata\nSELECT count(*) FROM encrypted_index_meta; -- expect exactly 1\n-- if not, rebuild the index from source documents","handlingStrategy":"validation","validationCode":"var rows int\nif err := db.QueryRow(\"SELECT count(*) FROM encrypted_index_meta\").Scan(&rows); err != nil || rows != 1 {\n    return fmt.Errorf(\"metadata invalid (rows=%d), index must be rebuilt\", rows)\n}","typeGuard":null,"tryCatchPattern":"if err := CheckEncryptedIndexCompatibility(db, kind, schema); err != nil {\n    if strings.Contains(err.Error(), \"invalid encrypted index compatibility metadata\") {\n        return rebuildDerivedIndex()\n    }\n    return err\n}","preventionTips":["Ensure single-instance access to a workspace (no two kernels on one data directory)","Avoid killing the kernel during first index creation","Never modify index databases with external SQLite tooling"],"tags":["sqlite","encryption","corruption","schema"],"backgroundTag":"schema-validation-failed","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}