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: abort split recovery when the existing-record directory cannot be read (#9558)

Adam Eivye69ce7fc83a6
diff --git a/scripts/migrations/035-036-split-pipeline-collections.test.js b/scripts/migrations/035-036-split-pipeline-collections.test.js
index d36cb42456..0c0493e2c5 100644
--- a/scripts/migrations/035-036-split-pipeline-collections.test.js
+++ b/scripts/migrations/035-036-split-pipeline-collections.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { mkdtempSync, rmSync, writeFileSync, readFileSync, mkdirSync, existsSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
@@ -6,6 +6,19 @@ import { join } from 'path';
import issuesMigration from './035-split-pipeline-issues-to-per-id.js';
import seriesMigration from './036-split-pipeline-series-to-per-id.js';
+// Lets a test fail directory listings (EIO) while every other fs call is real.
+const fault = vi.hoisted(() => ({ readdirFails: false }));
+vi.mock('fs/promises', async (importOriginal) => {
+ const actual = await importOriginal();
+ return {
+ ...actual,
+ readdir: (...args) => {
+ if (fault.readdirFails) return Promise.reject(Object.assign(new Error('EIO: i/o error, scandir'), { code: 'EIO' }));
+ return actual.readdir(...args);
+ },
+ };
+});
+
const writeJson = (path, value) => writeFileSync(path, JSON.stringify(value, null, 2) + '\n');
const readJson = (path) => JSON.parse(readFileSync(path, 'utf-8'));
@@ -101,6 +114,37 @@ describe.each(cases)('migration split pipeline $name', (cfg) => {
expect(existsSync(join(typeDir, cfg.otherId, 'index.json'))).toBe(true);
});
+ it('aborts without mutating anything when the existing-record directory cannot be listed, then recovers on retry', async () => {
+ writeJson(legacyPath, {
+ [cfg.key]: [
+ { id: cfg.validId, name: 'Legacy', title: 'Legacy' },
+ { id: cfg.otherId, name: 'Other', title: 'Other' },
+ ],
+ });
+ mkdirSync(join(typeDir, cfg.validId), { recursive: true });
+ const newerPath = join(typeDir, cfg.validId, 'index.json');
+ writeJson(newerPath, { id: cfg.validId, name: 'Newer', title: 'Newer' });
+ const newerBytes = readFileSync(newerPath);
+ const legacyBytes = readFileSync(legacyPath);
+
+ fault.readdirFails = true;
+ try {
+ await expect(cfg.migration.up({ rootDir })).rejects.toThrow(/EIO/);
+ } finally {
+ fault.readdirFails = false;
+ }
+ expect(readFileSync(newerPath).equals(newerBytes)).toBe(true);
+ expect(readFileSync(legacyPath).equals(legacyBytes)).toBe(true);
+ expect(existsSync(typeIndexPath)).toBe(false);
+ expect(existsSync(join(typeDir, cfg.otherId, 'index.json'))).toBe(false);
+ expect(existsSync(backupPath)).toBe(false);
+
+ const retry = await cfg.migration.up({ rootDir });
+ expect(retry).toEqual({ ok: true, reason: 'split', written: 1, skipped: 1, invalid: 0 });
+ expect(readFileSync(newerPath).equals(newerBytes)).toBe(true);
+ expect(existsSync(join(typeDir, cfg.otherId, 'index.json'))).toBe(true);
+ });
+
it('skips invalid ids and rejects unreadable legacy files', async () => {
writeJson(legacyPath, { [cfg.key]: [{ id: cfg.validId }, { id: cfg.invalidId }, null] });
const split = await cfg.migration.up({ rootDir });
diff --git a/scripts/migrations/_lib.js b/scripts/migrations/_lib.js
index 2b0dcd6695..35e1bd93f6 100644
--- a/scripts/migrations/_lib.js
+++ b/scripts/migrations/_lib.js
@@ -152,7 +152,9 @@ const RESERVED_MAP_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
async function splitExistingRecordIds(typeDir) {
const ids = new Set();
if (!await splitFileExists(typeDir)) return ids;
- const entries = await readdir(typeDir, { withFileTypes: true }).catch(() => []);
+ // No catch-to-empty: a failed listing is not proof that no records exist, and
+ // an empty set would let the legacy values overwrite fresher split records.
+ const entries = await readdir(typeDir, { withFileTypes: true });
for (const entry of entries) {
if (entry.name === 'index.json' || entry.name.startsWith('.') || !entry.isDirectory()) continue;
if (await splitFileExists(join(typeDir, entry.name, 'index.json'))) ids.add(entry.name);