{"record":{"id":"e85140edd5923bae","repo":"affaan-m/ECC","slug":"legacy-sync-path-changed-before-removal-preserved","errorCode":null,"errorMessage":"Legacy sync path changed before removal; preserved replacement at ${quarantinePath}","messagePattern":"Legacy sync path changed before removal; preserved replacement at (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/lib/codex-legacy-sync.js","lineNumber":121,"sourceCode":"}\n\nfunction removeOpenedRegularFile(filePath, opened) {\n  const quarantineDir = fs.mkdtempSync(path.join(path.dirname(filePath), '.ecc-remove-'));\n  const quarantinePath = path.join(quarantineDir, path.basename(filePath));\n  fs.renameSync(filePath, quarantinePath);\n  const quarantined = openRegularFileNoFollow(quarantinePath);\n  const openedStat = fs.fstatSync(opened.descriptor, { bigint: true });\n  const quarantinedStat = fs.fstatSync(quarantined.descriptor, { bigint: true });\n  fs.closeSync(quarantined.descriptor);\n  fs.closeSync(opened.descriptor);\n  opened.descriptor = null;\n  if (quarantinedStat.dev !== openedStat.dev || quarantinedStat.ino !== openedStat.ino) {\n    try {\n      fs.linkSync(quarantinePath, filePath);\n      fs.unlinkSync(quarantinePath);\n      fs.rmdirSync(quarantineDir);\n    } catch (_restoreError) {\n      throw new Error(\n        `Legacy sync path changed before removal; preserved replacement at ${quarantinePath}`\n      );\n    }\n    throw new Error(`Legacy sync path changed before removal: ${filePath}`);\n  }\n  fs.unlinkSync(quarantinePath);\n  fs.rmdirSync(quarantineDir);\n}\n\nfunction atomicWriteJson(filePath, value) {\n  fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });\n  const tempPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;\n  fs.writeFileSync(tempPath, `${JSON.stringify(value, null, 2)}\\n`, { mode: 0o600 });\n  fs.renameSync(tempPath, filePath);\n}\n\nfunction readState(statePath) {\n  const snapshot = readRegularFileNoFollow(statePath, 'utf8');","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/affaan-m/ECC/blob/06c5e118c4d3e6c3b7f9445f973a2194c82de193/scripts/lib/codex-legacy-sync.js#L103-L139","documentation":"removeOpenedRegularFile deletes safely by first hardlinking the opened file into a quarantine temp dir, unlinking the original, then comparing dev/ino of the opened descriptor against the quarantined copy. If they differ, the path was replaced between open and removal (error 98's situation) — and in this branch the attempted restore (linking the quarantined original back to the path) also failed. The replacement file currently sitting at the path is left intact, and the original content is preserved at the reported quarantine path so nothing is lost.","triggerScenarios":"A concurrent process renames a new file over the state path while removal is in flight, and simultaneously the state directory's permissions or another failure prevents the quarantine hardlink from being linked back.","commonSituations":"Parallel sync processes fighting over the same state file on a permission-restricted directory; cleanup daemons recreating state files mid-delete.","solutions":["Copy your data out of the preserved original at the reported quarantine path, then decide which version should win.","Delete the quarantine directory (.ecc-remove-*) once recovered.","Eliminate the concurrent writer (single sync job, locking) so the swap cannot recur."],"exampleFix":"# before\n# Error: ...preserved replacement at ~/.codex/.ecc-remove-XXXXXX/state.json\n\n# after — recover the preserved original, drop quarantine\n$ cp ~/.codex/.ecc-remove-*/state.json ~/state-recovered.json\n$ rm -rf ~/.codex/.ecc-remove-*","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { removeLegacyState(statePath); } catch (e) {\n  const m = e.message.match(/preserved replacement at (.+)$/);\n  if (m) {\n    console.error(`Original preserved at ${m[1]} — recovering content, replacement kept at ${statePath}`);\n    fs.copyFileSync(m[1], backupPath);\n    fs.rmSync(path.dirname(m[1]), { recursive: true, force: true });\n  } else throw e;\n}","preventionTips":["Serialize state-file removals so no other process can swap the path mid-delete.","Watch for .ecc-remove-* quarantine dirs after failed syncs — they hold the preserved original."],"tags":["filesystem","race-condition","data-recovery","codex-sync"],"backgroundTag":"file-replaced-during-delete","analyzedSha":"06c5e118c4d3e6c3b7f9445f973a2194c82de193","analyzedAt":"2026-08-18T11:27:13.915Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}