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: history migration 037 now fails cleanly instead of crashing on disk write errors (#9784)

[._.]/ Adam Eivy9c34c61113eb
diff --git a/scripts/migrations/037-history-json-to-jsonl.js b/scripts/migrations/037-history-json-to-jsonl.js
index a839f0ac24..4dd94a185e 100644
--- a/scripts/migrations/037-history-json-to-jsonl.js
+++ b/scripts/migrations/037-history-json-to-jsonl.js
@@ -10,10 +10,18 @@
* The live service appends to `history.jsonl`, so routine action logging no
* longer rewrites the full history file. The legacy file is renamed to
* `history.json.bak-037` after conversion for manual recovery.
+ *
+ * Conversion writes the merged JSONL (existing lines first, then new legacy
+ * entries) to an exclusive temporary sibling, then renames it over
+ * `history.jsonl` before the legacy file is renamed aside. A destination I/O
+ * failure (open/write/finish) aborts: the temp file is removed, both original
+ * files stay byte-identical, and `up()` rejects so the runner leaves the
+ * migration pending for a retry after repair (#9784).
*/
import { createReadStream, createWriteStream } from 'fs';
import { mkdir, rename, rm, stat } from 'fs/promises';
+import { finished } from 'stream/promises';
import { createInterface } from 'readline';
import { join, relative } from 'path';
import { writeJSONLines } from '../../server/lib/fileUtils.js';
@@ -113,17 +121,24 @@ async function streamLegacyEntries(legacyPath, onEntry) {
else if (ch === '}') depth -= 1;
if (depth === 0) {
+ let entry;
+ let parsed = false;
try {
- await onEntry(JSON.parse(buf));
+ entry = JSON.parse(buf);
+ parsed = true;
} catch {
invalid += 1;
}
inObject = false;
buf = '';
+ // Outside the parse try: an onEntry (destination) failure is an
+ // operational error that aborts the stream, never a malformed entry.
+ if (parsed) await onEntry(entry);
}
}
stream.resume();
} catch (err) {
+ stream.destroy();
reject(err);
}
});
@@ -131,11 +146,46 @@ async function streamLegacyEntries(legacyPath, onEntry) {
});
}
-function writeJsonlLine(out, entry) {
- const line = `${JSON.stringify(entry)}\n`;
- return new Promise((resolve, reject) => {
- out.write(line, (err) => err ? reject(err) : resolve());
+/**
+ * Exclusive-create write stream whose `error` event is owned from the start,
+ * so an open/write failure surfaces as a rejected promise instead of an
+ * unhandled EventEmitter error that kills the process.
+ */
+function openExclusiveSink(path) {
+ const out = createWriteStream(path, { flags: 'wx' });
+ let streamError = null;
+ let created = false;
+ out.on('error', (err) => { streamError ??= err; });
+ out.once('open', () => { created = true; });
+
+ const write = (chunk) => new Promise((resolve, reject) => {
+ if (streamError) return reject(streamError);
+ out.write(chunk, (err) => (err ? reject(err) : resolve()));
});
+ const close = async () => {
+ if (streamError) throw streamError;
+ out.end();
+ await finished(out);
+ };
+ // Wait for the fd to close so a later rm() cannot race a still-pending open.
+ const abort = () => new Promise((resolve) => {
+ if (out.closed) return resolve();
+ out.once('close', resolve);
+ out.destroy();
+ });
+ return { write, close, abort, wasCreated: () => created };
+}
+
+async function copyExistingJsonl(jsonlPath, sink) {
+ let last = null;
+ for await (const chunk of createReadStream(jsonlPath)) {
+ if (!chunk.length) continue;
+ await sink.write(chunk);
+ last = chunk[chunk.length - 1];
+ }
+ // A final line without its newline would otherwise fuse with the first
+ // converted entry.
+ if (last !== null && last !== 0x0a) await sink.write('\n');
}
export default {
@@ -161,31 +211,52 @@ export default {
}
const existingIds = await readExistingIds(jsonlPath);
- if (!jsonlExists) await writeJSONLines(jsonlPath, []);
-
- const out = createWriteStream(jsonlPath, { flags: 'a' });
+ const tmpPath = `${jsonlPath}.tmp-037-${process.pid}-${Date.now()}`;
+ const sink = openExclusiveSink(tmpPath);
let skippedDuplicate = 0;
let converted = 0;
- const { skippedInvalid } = await streamLegacyEntries(legacyPath, async (entry) => {
- if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
- return;
- }
- if (typeof entry.id === 'string' && existingIds.has(entry.id)) {
- skippedDuplicate += 1;
- return;
+ let skippedInvalid;
+ let destinationError = null;
+ const discardTemp = async () => {
+ await sink.abort();
+ // Only remove a temp file this run created — never a pre-existing one
+ // that made the exclusive open fail.
+ if (sink.wasCreated()) await rm(tmpPath, { force: true });
+ };
+ try {
+ if (jsonlExists) await copyExistingJsonl(jsonlPath, sink);
+ const parsed = await streamLegacyEntries(legacyPath, async (entry) => {
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
+ return;
+ }
+ if (typeof entry.id === 'string' && existingIds.has(entry.id)) {
+ skippedDuplicate += 1;
+ return;
+ }
+ try {
+ await sink.write(`${JSON.stringify(entry)}\n`);
+ } catch (err) {
+ destinationError = err;
+ throw err;
+ }
+ converted += 1;
+ if (typeof entry.id === 'string') existingIds.add(entry.id);
+ }).catch((err) => {
+ if (destinationError) throw destinationError;
+ console.warn(`⚠️ migration 037: ${legacyPath} unreadable or not { entries: [...] } — skipping. Resolve manually before next boot.`);
+ return null;
+ });
+ if (parsed === null) {
+ await discardTemp();
+ return { ok: false, reason: 'unreadable' };
}
- await writeJsonlLine(out, entry);
- converted += 1;
- if (typeof entry.id === 'string') existingIds.add(entry.id);
- }).catch((err) => {
- out.end();
- console.warn(`⚠️ migration 037: ${legacyPath} unreadable or not { entries: [...] } — skipping. Resolve manually before next boot.`);
- return { __error: err };
- });
- await new Promise((resolve, reject) => out.end((err) => err ? reject(err) : resolve()));
- if (skippedInvalid == null) {
- if (!jsonlExists) await rm(jsonlPath, { force: true });
- return { ok: false, reason: 'unreadable' };
+ skippedInvalid = parsed.skippedInvalid;
+ await sink.close();
+ await rename(tmpPath, jsonlPath);
+ } catch (err) {
+ await discardTemp();
+ console.error(`❌ migration 037: writing data/${JSONL_FILENAME} failed (${err.code ?? err.message}) — legacy history left in place; migration stays pending`);
+ throw err;
}
const finalBackupPath = await fileExists(backupPath)
diff --git a/scripts/migrations/037-history-json-to-jsonl.test.js b/scripts/migrations/037-history-json-to-jsonl.test.js
index 22aa4dea22..ff2065d097 100644
--- a/scripts/migrations/037-history-json-to-jsonl.test.js
+++ b/scripts/migrations/037-history-json-to-jsonl.test.js
@@ -1,10 +1,24 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync, mkdirSync } from 'fs';
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
+import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync, mkdirSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import migration from './037-history-json-to-jsonl.js';
+// Destination fault injection: when set, wraps the migration's write stream.
+const faults = vi.hoisted(() => ({ wrap: null }));
+vi.mock('fs', async (importOriginal) => {
+ const actual = await importOriginal();
+ return {
+ ...actual,
+ createWriteStream: (path, opts) => (faults.wrap
+ ? faults.wrap(actual.createWriteStream, path, opts)
+ : actual.createWriteStream(path, opts)),
+ };
+});
+
+const enospc = () => Object.assign(new Error('ENOSPC: no space left on device'), { code: 'ENOSPC' });
+
const writeJson = (path, value) => writeFileSync(path, JSON.stringify(value, null, 2) + '\n');
const readJson = (path) => JSON.parse(readFileSync(path, 'utf-8'));
const readJsonl = (path) =>
@@ -27,6 +41,7 @@ describe('migration 037 — history.json to history.jsonl', () => {
});
afterEach(() => {
+ faults.wrap = null;
rmSync(rootDir, { recursive: true, force: true });
});
@@ -103,4 +118,54 @@ describe('migration 037 — history.json to history.jsonl', () => {
expect(existsSync(legacyPath)).toBe(true);
expect(existsSync(jsonlPath)).toBe(false);
});
+
+ describe('destination I/O failure (#9784)', () => {
+ const injections = {
+ open: (create, path, opts) => create(join(dataDir, 'missing-dir', 'history.jsonl.tmp'), opts),
+ write: (create, path, opts) => {
+ const stream = create(path, opts);
+ stream._write = (chunk, encoding, cb) => cb(enospc());
+ stream._writev = (chunks, cb) => cb(enospc());
+ return stream;
+ },
+ finish: (create, path, opts) => {
+ const stream = create(path, opts);
+ stream._final = (cb) => cb(enospc());
+ return stream;
+ },
+ };
+
+ it.each(Object.keys(injections))(
+ '%s failure rejects, leaves both inputs byte-identical, and a retry after repair converts without duplicates',
+ async (kind) => {
+ writeFileSync(jsonlPath, '{"id":"a","action":"start"}\n');
+ writeJson(legacyPath, { entries: [{ id: 'a', action: 'start' }, { id: 'b', action: 'stop' }] });
+ const legacyBefore = readFileSync(legacyPath);
+ const jsonlBefore = readFileSync(jsonlPath);
+ const filesBefore = readdirSync(dataDir).sort();
+
+ faults.wrap = injections[kind];
+ await expect(migration.up({ rootDir })).rejects.toThrow(kind === 'open' ? /ENOENT/ : /ENOSPC/);
+
+ expect(readFileSync(legacyPath)).toEqual(legacyBefore);
+ expect(readFileSync(jsonlPath)).toEqual(jsonlBefore);
+ expect(readdirSync(dataDir).sort()).toEqual(filesBefore);
+
+ faults.wrap = null;
+ const result = await migration.up({ rootDir });
+ expect(result).toMatchObject({ ok: true, reason: 'converted', converted: 1, skippedDuplicate: 1 });
+ expect(readJsonl(jsonlPath).map((entry) => entry.id)).toEqual(['a', 'b']);
+ expect(existsSync(legacyPath)).toBe(false);
+ },
+ );
+ });
+
+ it('keeps an existing JSONL whose last line lacks a newline separate from converted entries', async () => {
+ writeFileSync(jsonlPath, '{"id":"a","action":"start"}');
+ writeJson(legacyPath, { entries: [{ id: 'b', action: 'stop' }] });
+
+ await migration.up({ rootDir });
+
+ expect(readJsonl(jsonlPath).map((entry) => entry.id)).toEqual(['a', 'b']);
+ });
});