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: preserve invalid Writers Room settings during migration (#8228)

[._.]/ Adam Eivyd7b971d6818e
diff --git a/scripts/migrations/022-rename-bible-setting-to-place.js b/scripts/migrations/022-rename-bible-setting-to-place.js
index 64cb081888..80e214d2b8 100644
--- a/scripts/migrations/022-rename-bible-setting-to-place.js
+++ b/scripts/migrations/022-rename-bible-setting-to-place.js
@@ -219,23 +219,26 @@ const migrateWritersRoomWorks = async (rootDir) => {
// this branch because `settings.json` is gone; the .bak sits as a
// recovery breadcrumb the user can diff if anything looks missing.
const backupPath = `${legacyPath}.bak-022`;
- await rename(legacyPath, backupPath).catch((err) => {
- console.warn(`⚠️ ${join('data/writers-room/works', entry.name)}: failed to back up legacy settings.json → settings.json.bak-022 — ${err.message}`);
- });
+ if (await fileExists(backupPath)) {
+ throw new Error(`Cannot migrate ${join('data/writers-room/works', entry.name, 'settings.json')}: settings.json.bak-022 already exists`);
+ }
+ await rename(legacyPath, backupPath);
console.log(`🧹 ${join('data/writers-room/works', entry.name)}: both settings.json and places.json existed — kept places.json, backed up legacy settings.json → settings.json.bak-022 (diff if anything looks missing)`);
continue;
}
- const raw = await readFile(legacyPath, 'utf-8').catch(() => null);
- if (raw == null) continue;
+ const raw = await readFile(legacyPath, 'utf-8');
let parsed;
- try { parsed = JSON.parse(raw); } catch { parsed = null; }
- if (parsed && Array.isArray(parsed.settings)) {
- parsed.places = parsed.settings;
- delete parsed.settings;
- await writeJson(newPath, parsed);
- } else {
- await writeJson(newPath, parsed || { places: [], updatedAt: null });
+ try {
+ parsed = JSON.parse(raw);
+ } catch (error) {
+ throw new Error(`Cannot migrate ${join('data/writers-room/works', entry.name, 'settings.json')}: invalid JSON`, { cause: error });
+ }
+ if (!parsed || !Array.isArray(parsed.settings)) {
+ throw new Error(`Cannot migrate ${join('data/writers-room/works', entry.name, 'settings.json')}: missing settings array`);
}
+ parsed.places = parsed.settings;
+ delete parsed.settings;
+ await writeJson(newPath, parsed);
// Try unlink first (clean removal of the now-redundant legacy file).
// If unlink fails (Windows file-lock, EACCES, etc.) the residual
// `settings.json` would otherwise be re-discovered by the next
@@ -247,13 +250,10 @@ const migrateWritersRoomWorks = async (rootDir) => {
const unlinkErr = await unlink(legacyPath).then(() => null, (err) => err);
if (unlinkErr) {
const backupPath = `${legacyPath}.bak-022`;
- await rename(legacyPath, backupPath).catch((renameErr) => {
- console.warn(
- `⚠️ ${join('data/writers-room/works', entry.name)}: failed to clean up legacy settings.json after writing places.json — ` +
- `unlink: ${unlinkErr.message}; rename to .bak-022: ${renameErr.message}. ` +
- `Next migration run will treat this as a phantom "both files exist" case.`,
- );
- });
+ if (await fileExists(backupPath)) {
+ throw new Error(`Cannot back up ${join('data/writers-room/works', entry.name, 'settings.json')} after unlink failed: settings.json.bak-022 already exists`, { cause: unlinkErr });
+ }
+ await rename(legacyPath, backupPath);
}
renamedFiles += 1;
}
diff --git a/scripts/migrations/022-rename-bible-setting-to-place.test.js b/scripts/migrations/022-rename-bible-setting-to-place.test.js
new file mode 100644
index 0000000000..5130b63639
--- /dev/null
+++ b/scripts/migrations/022-rename-bible-setting-to-place.test.js
@@ -0,0 +1,92 @@
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+import { mkdtemp, mkdir, readFile, rm, stat, writeFile } from 'fs/promises';
+import { tmpdir } from 'os';
+import { join } from 'path';
+
+import migration from './022-rename-bible-setting-to-place.js';
+
+let rootDir;
+const workDir = () => join(rootDir, 'data/writers-room/works/example-work');
+const legacyPath = () => join(workDir(), 'settings.json');
+const placesPath = () => join(workDir(), 'places.json');
+const exists = async (path) => stat(path).then(() => true, (error) => {
+ if (error.code === 'ENOENT') return false;
+ throw error;
+});
+
+beforeEach(async () => {
+ rootDir = await mkdtemp(join(tmpdir(), 'migration-022-'));
+ await mkdir(workDir(), { recursive: true });
+});
+
+afterEach(async () => {
+ await rm(rootDir, { recursive: true, force: true });
+});
+
+describe('migration 022 Writers Room settings', () => {
+ it('fails without replacing or deleting malformed legacy bytes', async () => {
+ const malformed = Buffer.from('{"settings":[{"id":"set-1"}', 'utf8');
+ await writeFile(legacyPath(), malformed);
+
+ await expect(migration.up({ rootDir })).rejects.toThrow(/settings\.json/);
+
+ expect(await readFile(legacyPath())).toEqual(malformed);
+ expect(await exists(placesPath())).toBe(false);
+ });
+
+ it('does not treat a valid JSON envelope without settings as an empty bible', async () => {
+ const unexpected = Buffer.from('{"entries":[{"id":"set-1"}]}\n');
+ await writeFile(legacyPath(), unexpected);
+
+ await expect(migration.up({ rootDir })).rejects.toThrow(/missing settings array/);
+
+ expect(await readFile(legacyPath())).toEqual(unexpected);
+ expect(await exists(placesPath())).toBe(false);
+ });
+
+ it('converts valid settings while preserving entries and envelope metadata', async () => {
+ const original = {
+ settings: [{ id: 'set-1', name: 'Example Place', details: { weather: 'rain' } }],
+ updatedAt: '2024-01-01T00:00:00.000Z',
+ customField: { retained: true },
+ };
+ await writeFile(legacyPath(), JSON.stringify(original));
+
+ await migration.up({ rootDir });
+
+ expect(JSON.parse(await readFile(placesPath(), 'utf8'))).toEqual({
+ places: original.settings,
+ updatedAt: original.updatedAt,
+ customField: original.customField,
+ });
+ expect(await exists(legacyPath())).toBe(false);
+ });
+
+ it('keeps existing places authoritative and retains the legacy bytes in a backup', async () => {
+ const legacy = Buffer.from('{"settings":[{"id":"set-legacy"}]}\n');
+ const places = Buffer.from('{"places":[{"id":"place-current"}]}\n');
+ await writeFile(legacyPath(), legacy);
+ await writeFile(placesPath(), places);
+
+ await migration.up({ rootDir });
+
+ expect(await readFile(placesPath())).toEqual(places);
+ expect(await readFile(`${legacyPath()}.bak-022`)).toEqual(legacy);
+ expect(await exists(legacyPath())).toBe(false);
+ });
+
+ it('does not overwrite an earlier backup when both files exist', async () => {
+ const legacy = Buffer.from('{"settings":[{"id":"set-new"}]}\n');
+ const places = Buffer.from('{"places":[{"id":"place-current"}]}\n');
+ const backup = Buffer.from('{"settings":[{"id":"set-earlier"}]}\n');
+ await writeFile(legacyPath(), legacy);
+ await writeFile(placesPath(), places);
+ await writeFile(`${legacyPath()}.bak-022`, backup);
+
+ await expect(migration.up({ rootDir })).rejects.toThrow(/bak-022 already exists/);
+
+ expect(await readFile(legacyPath())).toEqual(legacy);
+ expect(await readFile(placesPath())).toEqual(places);
+ expect(await readFile(`${legacyPath()}.bak-022`)).toEqual(backup);
+ });
+});