{"record":{"id":"27dfbc57dd30f8b6","repo":"affaan-m/ECC","slug":"memory-memoryid-is-duplicated-in-matches-leng","errorCode":null,"errorMessage":"Memory ${memoryId} is duplicated in ${matches.length} files.","messagePattern":"Memory (.+?) is duplicated in (.+?) files\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/lib/memory-vault.js","lineNumber":673,"sourceCode":"\nfunction readMemoryById(id, options = {}) {\n  const memoryId = validateMemoryId(id);\n  const targetHarness = options.targetHarness\n    ? validateSlug(options.targetHarness, 'target harness')\n    : null;\n  const loaded = readMemoryFiles(options);\n  const matches = loaded.entries\n    .filter(entry => entry.memory.id === memoryId)\n    .filter(entry => (\n      !targetHarness\n      || entry.memory.targetHarnesses.includes('all')\n      || entry.memory.targetHarnesses.includes(targetHarness)\n    ));\n  if (matches.length === 0) {\n    throw new Error(`Memory ${memoryId} was not found.`);\n  }\n  if (matches.length > 1) {\n    throw new Error(`Memory ${memoryId} is duplicated in ${matches.length} files.`);\n  }\n  const allBacklinks = loaded.entries\n    .filter(entry => entry.memory.links.includes(memoryId))\n    .filter(entry => entry.memory.status === 'active')\n    .map(entry => entry.memory)\n    .filter(memory => (\n      !targetHarness\n      || memory.targetHarnesses.includes('all')\n      || memory.targetHarnesses.includes(targetHarness)\n    ))\n    .sort((left, right) => left.id.localeCompare(right.id));\n  const backlinks = allBacklinks\n    .slice(0, MAX_RESULTS)\n    .map(summarizeMemory);\n  return {\n    ...matches[0],\n    backlinks,\n    backlinksTruncated: allBacklinks.length > backlinks.length,","sourceCodeStart":655,"sourceCodeEnd":691,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/scripts/lib/memory-vault.js#L655-L691","documentation":"readMemoryById requires that an id maps to exactly one file. If two or more files in the scanned scopes share the same memory id (after applying the targetHarness filter), the vault refuses to pick one arbitrarily and throws a duplication error. This protects against ambiguous reads when the vault integrity invariant (one id -> one file) has been broken.","triggerScenarios":"Two .md files under the scanned vault roots parse to memories with identical id fields. Caused by a buggy save that wrote the same id twice, a manual copy/paste of a memory file, a botched vault merge, or a sync conflict that created a duplicate. doctorMemoryVault reports the same condition in its duplicateIds diagnostic.","commonSituations":"Sync conflicts (Syncthing/Dropbox) creating mem_x.md and mem_x (1).md both with the same id; manually copying a memory file to a second scope without changing the id; a race in a custom idFactory; merging two users' vaults by copying files.","solutions":["Run doctorMemoryVault to list every duplicate id and the offending file paths.","Decide which copy is canonical, delete the others (rm <path>), and re-run readMemoryById.","If both copies have unique content, re-save the non-canonical one with a new id (load it, saveMemory with a fresh id and a link to the survivor), then delete the original duplicate.","Fix the upstream cause: stop syncing the vault with a file-level sync tool, or fix the idFactory that produced the collision."],"exampleFix":"// diagnose\nconst report = doctorMemoryVault({});\nconsole.log(report.duplicateIds); // [{ id: 'mem_x', paths: ['project:notes/mem_x.md', 'user:notes/mem_x.md'] }]\n// fix: remove the non-canonical copy\nfs.unlinkSync('<user-root>/notes/mem_x.md');\nconst mem = readMemoryById('mem_x'); // now resolves uniquely","handlingStrategy":"validation","validationCode":"const { doctorMemoryVault } = require('./scripts/lib/memory-vault');\nfunction assertNoDuplicateIds(options = {}) {\n  const report = doctorMemoryVault(options);\n  if (report.duplicateIdCount > 0) {\n    throw new Error(`Duplicate memory ids detected: ${report.duplicateIds.map(d => d.id).join(', ')}`);\n  }\n}\n// before readMemoryById:\nassertNoDuplicateIds(options);","typeGuard":null,"tryCatchPattern":"try { return readMemoryById(id, options); }\ncatch (error) {\n  if (/is duplicated in/.test(error.message)) {\n    const report = doctorMemoryVault(options);\n    console.error('Resolve duplicates:', report.duplicateIds);\n    return null;\n  }\n  throw error;\n}","preventionTips":["Run doctorMemoryVault periodically to catch duplicate ids early.","Do not file-level sync the vault with tools that create conflict copies.","When merging vaults, copy memories through saveMemory (which regenerates ids) rather than copying files.","Never copy a memory .md file by hand without changing its id."],"tags":["memory-vault","validation","integrity","data-integrity"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}