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: identify incomplete sources and ignore universe temp files (#6950)

[._.]/ Adam Eivycc1733b352fc
diff --git a/server/scripts/legacyImport.js b/server/scripts/legacyImport.js
index 699147821b..75ce919f12 100644
--- a/server/scripts/legacyImport.js
+++ b/server/scripts/legacyImport.js
@@ -1,6 +1,8 @@
// Shared filesystem boundaries for the legacy creative importers. Only ENOENT
// means absence; a present source that cannot be verified keeps import pending.
import { readFile, stat, rename } from 'fs/promises';
+import { relative } from 'path';
+import { PATHS } from '../lib/fileUtils.js';
const absent = error => {
if (error.code === 'ENOENT') return null;
@@ -29,6 +31,7 @@ export async function parkLegacyFile(path, aside) {
}
export function incompleteImport(domain, counts, incomplete) {
- console.warn(`⚠️ ${domain}→DB import incomplete: ${incomplete} source(s) could not be verified or parked; retained for retry`);
- return { ok: true, reason: 'incomplete', ...counts, incomplete };
+ const sources = [...new Set(incomplete.map(path => relative(PATHS.data, path)))];
+ console.warn(`⚠️ ${domain}→DB import incomplete: ${sources.length} source(s) could not be verified or parked; retained for retry: ${sources.slice(0, 5).join(', ')}${sources.length > 5 ? ' (more sources omitted)' : ''}`);
+ return { ok: true, reason: 'incomplete', ...counts, incomplete: sources.length, sources };
}
diff --git a/server/scripts/legacyImportRetry.test.js b/server/scripts/legacyImportRetry.test.js
index d3842d8b3d..92b3a64a5c 100644
--- a/server/scripts/legacyImportRetry.test.js
+++ b/server/scripts/legacyImportRetry.test.js
@@ -84,7 +84,7 @@ it('retains canonical metadata when a recovery copy prevents parking', async ()
await put('pipeline-series/ser-a/index.json', { id: 'ser-a' });
await put('pipeline-series/ser-a/index.json.imported', { id: 'ser-a', name: 'old recovery' });
await put('pipeline-series/ser-a/manuscript-review.json', { review: 'file primary' });
- expect(await migrateSeriesToDB()).toMatchObject({ reason: 'incomplete' });
+ expect(await migrateSeriesToDB()).toMatchObject({ reason: 'incomplete', sources: [join('pipeline-series', 'ser-a', 'index.json')] });
expect(await exists('pipeline-series.migrated.json')).toBe(false);
expect(await exists('pipeline-series/ser-a/index.json')).toBe(true);
expect(JSON.parse(await readFile(join(dataDir, 'pipeline-series/ser-a/index.json.imported'), 'utf8')).name).toBe('old recovery');
@@ -145,3 +145,11 @@ it('accepts DB-native prose/review directories with no legacy metadata or marker
expect(await exists('writers-room/works/wr-work-aaa/drafts/wr-draft-aaa.md')).toBe(true);
expect(await exists('pipeline-series/ser-a/manuscript-review.json')).toBe(true);
});
+
+it('ignores orphaned atomic-write files and invalid record directory names in the universe walk', async () => {
+ await put('universes/uni-a/index.json', { id: 'uni-a' });
+ await put('universes/index.json.123.example.tmp', '{');
+ await put('universes/not.a.record/readme.txt', 'unrelated');
+ expect(await migrateUniversesToDB()).toMatchObject({ reason: 'imported', imported: 1 });
+ expect(await exists('universes.imported/index.json.123.example.tmp')).toBe(true);
+});
diff --git a/server/scripts/migrateIssuesToDB.js b/server/scripts/migrateIssuesToDB.js
index 8db7ce5a8b..9c505a8b0f 100644
--- a/server/scripts/migrateIssuesToDB.js
+++ b/server/scripts/migrateIssuesToDB.js
@@ -71,7 +71,7 @@ export async function migrateIssuesToDB() {
const entries = await readdir(legacyDir);
let imported = 0;
let skipped = 0;
- let incomplete = 0;
+ const incomplete = [];
for (const name of entries) {
if (!RECORD_RE.test(name)) continue;
const recordPath = join(legacyDir, name, 'index.json');
@@ -80,18 +80,18 @@ export async function migrateIssuesToDB() {
// Current DB-backed records may own file-primary siblings without legacy
// metadata. Confirm the row instead of treating those directories as loss.
const existing = await query('SELECT id FROM pipeline_issues WHERE id = $1', [name]);
- if (!existing.rows.length) incomplete += 1;
+ if (!existing.rows.length) incomplete.push(recordPath);
skipped += 1;
continue;
}
- if (source.status !== 'valid' || source.value?.id !== name) { incomplete += 1; continue; }
+ if (source.status !== 'valid' || source.value?.id !== name) { incomplete.push(recordPath); continue; }
const inserted = await importRecord(source.value);
- if (inserted === null) { incomplete += 1; continue; }
+ if (inserted === null) { incomplete.push(recordPath); continue; }
if (inserted) imported += 1;
else skipped += 1;
}
- if (incomplete) return incompleteImport('Issues', { imported, skipped }, incomplete);
+ if (incomplete.length) return incompleteImport('Issues', { imported, skipped }, incomplete);
try {
await rename(legacyDir, join(PATHS.data, IMPORTED_DIRNAME));
diff --git a/server/scripts/migrateSeriesToDB.js b/server/scripts/migrateSeriesToDB.js
index eb86e6e2fb..0ce9d060b6 100644
--- a/server/scripts/migrateSeriesToDB.js
+++ b/server/scripts/migrateSeriesToDB.js
@@ -84,7 +84,7 @@ export async function migrateSeriesToDB() {
const entries = await readdir(legacyDir);
let imported = 0;
let skipped = 0;
- let incomplete = 0;
+ const incomplete = [];
for (const name of entries) {
if (!RECORD_RE.test(name)) continue; // skip index.json, hidden, non-record dirs
const recordPath = join(legacyDir, name, 'index.json');
@@ -93,19 +93,19 @@ export async function migrateSeriesToDB() {
// Current DB-backed records may own file-primary siblings without legacy
// metadata. Confirm the row instead of treating those directories as loss.
const existing = await query('SELECT id FROM pipeline_series WHERE id = $1', [name]);
- if (!existing.rows.length) incomplete += 1;
+ if (!existing.rows.length) incomplete.push(recordPath);
skipped += 1;
continue;
}
- if (source.status !== 'valid' || source.value?.id !== name) { incomplete += 1; continue; }
+ if (source.status !== 'valid' || source.value?.id !== name) { incomplete.push(recordPath); continue; }
const inserted = await importRecord(source.value);
- if (inserted === null) { incomplete += 1; continue; }
+ if (inserted === null) { incomplete.push(recordPath); continue; }
if (inserted) imported += 1;
else skipped += 1;
- if (!await parkLegacyFile(recordPath, `${recordPath}.imported`)) incomplete += 1;
+ if (!await parkLegacyFile(recordPath, `${recordPath}.imported`)) incomplete.push(recordPath);
}
- if (incomplete) return incompleteImport('Series', { imported, skipped }, incomplete);
+ if (incomplete.length) return incompleteImport('Series', { imported, skipped }, incomplete);
await writeMarker(MARKER_FILENAME, { migratedAt: new Date().toISOString(), imported, skipped, reason: 'imported' });
console.log(`🎬 pipeline-series→DB import: imported ${imported} series (${skipped} skipped); index.json renamed aside in place (review siblings kept)`);
diff --git a/server/scripts/migrateStoryBuilderToDB.js b/server/scripts/migrateStoryBuilderToDB.js
index 994ec727b8..1689374794 100644
--- a/server/scripts/migrateStoryBuilderToDB.js
+++ b/server/scripts/migrateStoryBuilderToDB.js
@@ -75,7 +75,7 @@ export async function migrateStoryBuilderToDB() {
const entries = await readdir(legacyDir);
let imported = 0;
let skipped = 0;
- let incomplete = 0;
+ const incomplete = [];
for (const name of entries) {
if (!RECORD_RE.test(name)) continue; // skip index.json, hidden, non-record dirs
const recordPath = join(legacyDir, name, 'index.json');
@@ -84,18 +84,18 @@ export async function migrateStoryBuilderToDB() {
// Current DB-backed records may own file-primary siblings without legacy
// metadata. Confirm the row instead of treating those directories as loss.
const existing = await query('SELECT id FROM story_builder_sessions WHERE id = $1', [name]);
- if (!existing.rows.length) incomplete += 1;
+ if (!existing.rows.length) incomplete.push(recordPath);
skipped += 1;
continue;
}
- if (source.status !== 'valid' || source.value?.id !== name) { incomplete += 1; continue; }
+ if (source.status !== 'valid' || source.value?.id !== name) { incomplete.push(recordPath); continue; }
const inserted = await importRecord(source.value);
- if (inserted === null) { incomplete += 1; continue; }
+ if (inserted === null) { incomplete.push(recordPath); continue; }
if (inserted) imported += 1;
else skipped += 1;
}
- if (incomplete) return incompleteImport('StoryBuilder', { imported, skipped }, incomplete);
+ if (incomplete.length) return incompleteImport('StoryBuilder', { imported, skipped }, incomplete);
try {
await rename(legacyDir, join(PATHS.data, IMPORTED_DIRNAME));
diff --git a/server/scripts/migrateUniversesToDB.js b/server/scripts/migrateUniversesToDB.js
index f5a8c193cd..d19617cd27 100644
--- a/server/scripts/migrateUniversesToDB.js
+++ b/server/scripts/migrateUniversesToDB.js
@@ -99,25 +99,26 @@ export async function migrateUniversesToDB() {
return { ok: true, reason: 'fresh-install', imported: 0, runs: 0 };
}
- const entries = await readdir(legacyDir);
+ const entries = await readdir(legacyDir, { withFileTypes: true });
let imported = 0;
let skipped = 0;
- let incomplete = 0;
- for (const name of entries) {
- if (name === 'index.json' || name.startsWith('.')) continue;
+ const incomplete = [];
+ for (const entry of entries) {
+ const name = entry.name;
+ if (!entry.isDirectory() || !/^[A-Za-z0-9_-]{1,128}$/.test(name)) continue;
const recordPath = join(legacyDir, name, 'index.json');
const source = await readLegacyJSON(recordPath);
if (source.status === 'missing') {
// Current DB-backed records may own file-primary siblings without legacy
// metadata. Confirm the row instead of treating those directories as loss.
const existing = await query('SELECT id FROM universes WHERE id = $1', [name]);
- if (!existing.rows.length) incomplete += 1;
+ if (!existing.rows.length) incomplete.push(recordPath);
skipped += 1;
continue;
}
- if (source.status !== 'valid' || source.value?.id !== name) { incomplete += 1; continue; }
+ if (source.status !== 'valid' || source.value?.id !== name) { incomplete.push(recordPath); continue; }
const inserted = await importRecord(source.value);
- if (inserted === null) { incomplete += 1; continue; }
+ if (inserted === null) { incomplete.push(recordPath); continue; }
if (inserted) imported += 1;
else skipped += 1;
}
@@ -125,13 +126,13 @@ export async function migrateUniversesToDB() {
// Runs from the type-level index.json `config.runs[]`.
let runs = 0;
const typeIndex = await readLegacyJSON(join(legacyDir, 'index.json'));
- if (typeIndex.status === 'invalid' || Array.isArray(typeIndex.value)) incomplete += 1;
+ if (typeIndex.status === 'invalid' || Array.isArray(typeIndex.value)) incomplete.push(join(legacyDir, 'index.json'));
if (typeIndex.status === 'valid') {
const legacyRuns = typeIndex.value?.config?.runs;
- if (legacyRuns !== undefined && !Array.isArray(legacyRuns)) incomplete += 1;
+ if (legacyRuns !== undefined && !Array.isArray(legacyRuns)) incomplete.push(join(legacyDir, 'index.json'));
for (const run of Array.isArray(legacyRuns) ? legacyRuns : []) {
const inserted = await importRun(run);
- if (inserted === null) incomplete += 1;
+ if (inserted === null) incomplete.push(join(legacyDir, 'index.json'));
else if (inserted) runs += 1;
}
}
@@ -140,7 +141,7 @@ export async function migrateUniversesToDB() {
// if the rename succeeded (so a rollback / MEMORY_BACKEND=file boot can't read
// a stale dir while the marker claims migration is done). If the rename fails,
// leave the dir + no marker → next boot retries the idempotent import.
- if (incomplete) return incompleteImport('Universes', { imported, skipped, runs }, incomplete);
+ if (incomplete.length) return incompleteImport('Universes', { imported, skipped, runs }, incomplete);
try {
await rename(legacyDir, join(PATHS.data, IMPORTED_DIRNAME));
diff --git a/server/scripts/migrateUniversesToDB.test.js b/server/scripts/migrateUniversesToDB.test.js
index e3d8ca2ec5..d316767464 100644
--- a/server/scripts/migrateUniversesToDB.test.js
+++ b/server/scripts/migrateUniversesToDB.test.js
@@ -46,7 +46,7 @@ vi.mock('fs/promises', () => ({
}),
readdir: vi.fn(async () => {
if (dirEntries === null) return [];
- return dirEntries;
+ return dirEntries.map(name => ({ name, isDirectory: () => name !== 'index.json' && !name.startsWith('.') }));
}),
stat: vi.fn(async (path) => {
if (toPosix(path) === '/fake/data/universes') {
diff --git a/server/scripts/migrateWritersRoomToDB.js b/server/scripts/migrateWritersRoomToDB.js
index e8ecc468af..e492ded1aa 100644
--- a/server/scripts/migrateWritersRoomToDB.js
+++ b/server/scripts/migrateWritersRoomToDB.js
@@ -143,19 +143,19 @@ export async function migrateWritersRoomToDB() {
if (!rootStat) return { ok: true, reason: 'fresh-install', folders: 0, works: 0, exercises: 0 };
const counts = { folders: 0, works: 0, exercises: 0 };
- let incomplete = 0;
+ const incomplete = [];
for (const [name, importRow] of [['folders', importFolder], ['exercises', importExercise]]) {
const path = join(root, `${name}.json`);
const source = await readLegacyJSON(path);
if (source.status === 'missing') continue;
- if (source.status !== 'valid' || !Array.isArray(source.value)) { incomplete += 1; continue; }
+ if (source.status !== 'valid' || !Array.isArray(source.value)) { incomplete.push(path); continue; }
let valid = true;
for (const row of source.value) {
const inserted = await importRow(row);
- if (inserted === null) { incomplete += 1; valid = false; }
+ if (inserted === null) { incomplete.push(path); valid = false; }
else if (inserted) counts[name] += 1;
}
- if (valid && !await parkLegacyFile(path, path.replace(/\.json$/, '.imported.json'))) incomplete += 1;
+ if (valid && !await parkLegacyFile(path, path.replace(/\.json$/, '.imported.json'))) incomplete.push(path);
}
const worksDir = join(root, 'works');
@@ -168,17 +168,17 @@ export async function migrateWritersRoomToDB() {
if (source.status === 'missing') {
// A DB-native work creates prose directories but no legacy manifest.
const existing = await query('SELECT id FROM writers_room_works WHERE id = $1', [entry.name]);
- if (!existing.rows.length) incomplete += 1;
+ if (!existing.rows.length) incomplete.push(path);
continue;
}
- if (source.status !== 'valid' || source.value?.id !== entry.name) { incomplete += 1; continue; }
+ if (source.status !== 'valid' || source.value?.id !== entry.name) { incomplete.push(path); continue; }
const inserted = await importWork(source.value);
- if (inserted === null) { incomplete += 1; continue; }
+ if (inserted === null) { incomplete.push(path); continue; }
if (inserted) counts.works += 1;
- if (!await parkLegacyFile(path, aside)) incomplete += 1;
+ if (!await parkLegacyFile(path, aside)) incomplete.push(path);
}
- if (incomplete) return incompleteImport('Writers Room', counts, incomplete);
+ if (incomplete.length) return incompleteImport('Writers Room', counts, incomplete);
await writeMarker(MARKER_FILENAME, { migratedAt: new Date().toISOString(), ...counts, reason: 'imported' });
console.log(`✍️ writers-room→DB import: ${counts.folders} folder(s), ${counts.works} work(s), ${counts.exercises} exercise(s); .md bodies left in place`);
return { ok: true, reason: 'imported', ...counts };