Heuristic:777genius Agent teams ai Atomic File Writes
| Knowledge Sources | |
|---|---|
| Domains | File_System, Reliability |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Write to a temporary file then rename over the target to prevent partial writes and data corruption during crashes or concurrent access.
Description
All persistent file writes in the app use atomic write helpers (`atomicWriteAsync`, `atomicWriteSync`, `atomicWriteInboxAsync`). The pattern is: write data to a temporary file (`.tmp.<uuid>`), optionally fsync for durability, then rename the temp file over the target. This ensures readers never see a partially-written file. On Windows, transient `EPERM`/`EACCES`/`EBUSY` errors during rename are retried automatically. The write helpers also support a `beforeCommit` hook for compare-and-swap guards and directory sync for strict durability.
Usage
Use this heuristic for all file persistence in the app: team config, inbox messages, kanban state, task data, review decisions, notification journals, and member storage paths. Never use `fs.writeFile` directly for persistent state — always go through `atomicWriteAsync` or `atomicWriteSync`.
The Insight (Rule of Thumb)
- Action: Use `atomicWriteAsync(targetPath, data, options)` or `atomicWriteSync(targetPath, data)`.
- Value: Write to `.tmp.<uuid>` → fsync (optional) → rename with retry → directory sync (optional).
- Trade-off: Slightly more I/O (extra temp file + rename + optional fsync). Negligible for typical file sizes.
- Compatibility: Cross-platform. Windows rename retries handle antivirus/file-lock transient errors. `flag: 'wx'` prevents temp file collisions.
Reasoning
Direct `fs.writeFile` can leave a partially-written file if the process crashes mid-write. Subsequent reads would see truncated or corrupted data. The write-then-rename pattern is atomic at the filesystem level — the rename operation either completes (target gets new content) or fails (target keeps old content). The temp file is cleaned up on error. Directory sync ensures the rename itself is durable on disk.
For review file mutations, a more elaborate three-phase transaction (prepare → detach → publish) with a manifest provides crash recovery: if the process dies mid-transaction, the manifest allows resuming or rolling back.
Code Evidence
Atomic write from `atomicWrite.ts:264-292`:
export async function atomicWriteAsync(
targetPath: string,
data: string | Buffer,
options: AtomicWriteOptions = {}
): Promise<void> {
const dir = path.dirname(targetPath);
const tmpPath = path.join(dir, `.tmp.${randomUUID()}`);
try {
await fs.promises.mkdir(dir, { recursive: true, ... });
await fs.promises.writeFile(tmpPath, data, {
...(typeof data === 'string' ? { encoding: 'utf8' as const } : {}),
flag: 'wx',
...
});
await syncFile(tmpPath, options.durability === 'strict');
await renameWithRetry(tmpPath, targetPath, options.beforeCommit);
if (options.syncDirectory) {
await syncDirectory(dir, options.durability === 'strict');
}
} catch (error) {
await fs.promises.unlink(tmpPath).catch(() => undefined);
throw error;
}
}
Widespread usage from `TeamInboxWriter.ts:6`:
import { atomicWriteInboxAsync } from './atomicWrite';
Widespread usage from `TeamConfigReader.ts:1`:
import { atomicWriteAsync } from '@main/utils/atomicWrite';
Related Pages
- Implementation:777genius_Agent_teams_ai_TeamConfigReader_getConfig
- Implementation:777genius_Agent_teams_ai_TeamInboxWriter_sendMessage
- Implementation:777genius_Agent_teams_ai_TeamKanbanManager
- Implementation:777genius_Agent_teams_ai_ReviewDecisionStore
- Implementation:777genius_Agent_teams_ai_ReviewApplierService_applyReviewDecisions