()
| 290 | |
| 291 | // Core flush logic - writes pending entries to disk |
| 292 | async function immediateFlushHistory(): Promise<void> { |
| 293 | if (pendingEntries.length === 0) { |
| 294 | return |
| 295 | } |
| 296 | |
| 297 | let release |
| 298 | try { |
| 299 | const historyPath = join(getClaudeConfigHomeDir(), 'history.jsonl') |
| 300 | |
| 301 | // Ensure the file exists before acquiring lock (append mode creates if missing) |
| 302 | await writeFile(historyPath, '', { |
| 303 | encoding: 'utf8', |
| 304 | mode: 0o600, |
| 305 | flag: 'a', |
| 306 | }) |
| 307 | |
| 308 | release = await lock(historyPath, { |
| 309 | stale: 10000, |
| 310 | retries: { |
| 311 | retries: 3, |
| 312 | minTimeout: 50, |
| 313 | }, |
| 314 | }) |
| 315 | |
| 316 | const jsonLines = pendingEntries.map(entry => jsonStringify(entry) + '\n') |
| 317 | pendingEntries = [] |
| 318 | |
| 319 | await appendFile(historyPath, jsonLines.join(''), { mode: 0o600 }) |
| 320 | } catch (error) { |
| 321 | logForDebugging(`Failed to write prompt history: ${error}`) |
| 322 | } finally { |
| 323 | if (release) { |
| 324 | await release() |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | async function flushPromptHistory(retries: number): Promise<void> { |
| 330 | if (isWriting || pendingEntries.length === 0) { |
no test coverage detected