Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: write CoS runner completion files atomically and survive corrupt metadata

[._.]/ Adam Eivyab564f543d99
diff --git a/server/cos-runner/index.js b/server/cos-runner/index.js
index 6d470dab75..3430b52af4 100644
--- a/server/cos-runner/index.js
+++ b/server/cos-runner/index.js
@@ -18,7 +18,7 @@ import { writeFile, readFile } from 'fs/promises';
import { existsSync } from 'fs';
import http from 'http';
import { Server as SocketServer } from 'socket.io';
-import { ensureDir, PATHS } from '../lib/fileUtils.js';
+import { ensureDir, atomicWrite, PATHS } from '../lib/fileUtils.js';
import { prepareCliSpawn, killProcessTree, guardChildStdin, deliverChildStdin } from '../lib/bufferedSpawn.js';
import { buildCliChildEnv } from '../lib/cliChildEnv.js';
import { prepareCliPrompt } from '../lib/cliProviderArgs.js';
@@ -128,16 +128,24 @@ io.use((socket, next) => next(lifecycle.isStopping() ? new Error('Runner is shut
async function persistCompletion(agentId, output, metadata) {
const agentDir = join(AGENTS_DIR, agentId);
await ensureDir(agentDir);
- await writeFile(join(agentDir, 'output.txt'), output);
+ // atomicWrite (temp + rename): a crash mid-write must not leave a torn file
+ // that the next completion attempt then chokes on.
+ await atomicWrite(join(agentDir, 'output.txt'), output);
if (!metadata) return;
const metadataPath = join(agentDir, 'metadata.json');
- const existing = JSON.parse(await readFile(metadataPath, 'utf-8').catch(err => {
- if (err.code === 'ENOENT') return '{}';
+ const existing = await readFile(metadataPath, 'utf-8').then(JSON.parse).catch(err => {
+ if (err.code === 'ENOENT') return {};
+ // Unreadable/corrupt prior metadata must not block recording the terminal
+ // result — the new fields below are the authoritative completion evidence.
+ if (err instanceof SyntaxError) {
+ console.warn(`⚠️ Agent ${agentId} metadata.json is corrupt (${err.message}) — rewriting from completion data`);
+ return {};
+ }
throw err;
- }));
- await writeFile(metadataPath, JSON.stringify({
+ });
+ await atomicWrite(metadataPath, {
...existing, agentId, ...metadata, outputSize: Buffer.byteLength(output),
- }, null, 2));
+ });
}
/**
diff --git a/server/cos-runner/index.test.js b/server/cos-runner/index.test.js
index a4c6b1fc8e..ba5cb5cee3 100644
--- a/server/cos-runner/index.test.js
+++ b/server/cos-runner/index.test.js
@@ -214,3 +214,21 @@ describe('cos-runner output — ANSI decoloring parity with spawnDirectly', () =
expect(RUNNER_SRC).toMatch(/if \(!trimmedDecolored \|\| isKnownCliStderrNoise\(trimmedDecolored\)\) return;/);
});
});
+
+describe('cos-runner persistCompletion — crash-safe completion evidence', () => {
+ const body = RUNNER_SRC.slice(
+ RUNNER_SRC.indexOf('async function persistCompletion'),
+ RUNNER_SRC.indexOf('Emit event to connected')
+ );
+
+ it('writes output.txt and metadata.json atomically, not via in-place writeFile', () => {
+ expect(body).toMatch(/atomicWrite\(join\(agentDir, 'output\.txt'\)/);
+ expect(body).toMatch(/atomicWrite\(metadataPath,/);
+ expect(body).not.toMatch(/\bwriteFile\(/);
+ });
+
+ it('tolerates a corrupt prior metadata.json instead of throwing on JSON.parse', () => {
+ expect(body).toMatch(/\.then\(JSON\.parse\)\.catch\(/);
+ expect(body).toMatch(/err instanceof SyntaxError/);
+ });
+});