()
| 111 | } |
| 112 | |
| 113 | export async function flushCodexCache(): Promise<void> { |
| 114 | if (!memCache) return |
| 115 | try { |
| 116 | // Evict entries for files that no longer exist on disk |
| 117 | const paths = Object.keys(memCache.files) |
| 118 | for (const p of paths) { |
| 119 | try { |
| 120 | await stat(p) |
| 121 | } catch { |
| 122 | delete memCache.files[p] |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | const dir = getCacheDir() |
| 127 | if (!existsSync(dir)) await mkdir(dir, { recursive: true }) |
| 128 | const finalPath = getCachePath() |
| 129 | const tempPath = `${finalPath}.${randomBytes(8).toString('hex')}.tmp` |
| 130 | const payload = JSON.stringify(memCache) |
| 131 | const handle = await open(tempPath, 'w', 0o600) |
| 132 | try { |
| 133 | await handle.writeFile(payload, { encoding: 'utf-8' }) |
| 134 | await handle.sync() |
| 135 | } finally { |
| 136 | await handle.close() |
| 137 | } |
| 138 | try { |
| 139 | await rename(tempPath, finalPath) |
| 140 | } catch (err) { |
| 141 | try { await unlink(tempPath) } catch {} |
| 142 | throw err |
| 143 | } |
| 144 | } catch {} |
| 145 | } |
no test coverage detected