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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([issue-722]): address codex review — plan/apply split preserves fail-stop semantics
[._.]/ Adam Eivy17ab1821cb60
diff --git a/scripts/migrations/_lib.js b/scripts/migrations/_lib.jsindex a6790e0b51..d539056174 100644--- a/scripts/migrations/_lib.js+++ b/scripts/migrations/_lib.js@@ -279,14 +279,22 @@ export async function applyPromptReplaceMigration({}) {const stagesDir = join(rootDir, 'data', 'prompts', 'stages');const sampleDir = join(rootDir, 'data.reference', 'prompts', 'stages');-- // Per-file scan runs in parallel — each file's read/compare/write is- // independent (no cross-file shared state), so the only cost of the old- // serial loop was wall-clock time on a multi-file migration. Each task- // returns the name of the single counter it bumped (or null for a no-op);- // we sum them post-flight, so counter accumulation never races (it happens- // after every task settles, not from inside the async work).- const outcomes = await Promise.all(Object.keys(accepted).map(async (filename) => {+ const filenames = Object.keys(accepted);++ // Two phases so parallelism never changes the migration's failure semantics:+ //+ // 1. PLAN (parallel) — every file's reads + hash compares run concurrently+ // (that's the dominant cost, and they share no state). Each yields a+ // side-effect-free descriptor: which write/unlink to perform, which+ // counter to bump, and any deferred log line. A read rejection here+ // rejects the whole migration BEFORE any file has been mutated — so a+ // fail-stop on a missing/unreadable file leaves data/ untouched (a+ // stricter guarantee than the old serial loop, which could leave an+ // already-processed prefix mutated).+ // 2. APPLY (ordered) — perform the planned writes/unlinks in Object.keys+ // order so log output and the on-disk result are deterministic, exactly+ // as the serial loop produced them.+ const plans = await Promise.all(filenames.map(async (filename) => {const dataPath = join(stagesDir, filename);const samplePath = join(sampleDir, filename);@@ -299,13 +307,10 @@ export async function applyPromptReplaceMigration({if (createIfMissing) {const sampleContent = await readFile(samplePath, 'utf-8').catch(() => null);if (sampleContent != null) {- await writeFile(dataPath, sampleContent);- console.log(`📄 created ${label}: ${filename}`);- return 'created';+ return { write: { path: dataPath, content: sampleContent }, counter: 'created', log: `📄 created ${label}: ${filename}` };}}- console.log(`📄 ${label} ${filename}: not present in data/, will be created by setup-data.js`);- return null;+ return { log: `📄 ${label} ${filename}: not present in data/, will be created by setup-data.js` };}const existingMd5 = md5(existing);@@ -325,39 +330,41 @@ export async function applyPromptReplaceMigration({});if (!sampleExists) {if (matchesAcceptedOld || matchesCurrent) {- await unlink(dataPath);- console.log(`🗑️ ${label} ${filename} was renamed/retired upstream — removed unmodified copy from data/`);- return 'retired';+ return { unlink: dataPath, counter: 'retired', log: `🗑️ ${label} ${filename} was renamed/retired upstream — removed unmodified copy from data/` };}- console.warn(- `⚠️ ${label} ${filename} was renamed/retired upstream but your local copy has been customized.\n` +- ` Check data.reference/prompts/stages/ for the replacement file and merge any custom edits manually.`,- );- return 'skipped';+ return {+ counter: 'skipped',+ warn: `⚠️ ${label} ${filename} was renamed/retired upstream but your local copy has been customized.\n` ++ ` Check data.reference/prompts/stages/ for the replacement file and merge any custom edits manually.`,+ };}}if (matchesCurrent) {- return 'alreadyCurrent';+ return { counter: 'alreadyCurrent' };}if (!matchesAcceptedOld) {- console.warn(- `⚠️ ${label} ${filename} has been customized — skipping auto-update.\n` +- customizedHint(filename),- );- return 'skipped';+ return {+ counter: 'skipped',+ warn: `⚠️ ${label} ${filename} has been customized — skipping auto-update.\n` + customizedHint(filename),+ };}+ // Sample read stays in the plan phase: a missing sample for an accepted-old+ // file is the migration-author error the old loop surfaced by throwing, so+ // it must still abort before any write lands.const sampleContent = await readFile(samplePath, 'utf-8');- await writeFile(dataPath, sampleContent);- console.log(`✅ updated ${label}: ${filename}`);- return 'updated';+ return { write: { path: dataPath, content: sampleContent }, counter: 'updated', log: `✅ updated ${label}: ${filename}` };}));const counts = { updated: 0, alreadyCurrent: 0, skipped: 0, created: 0, retired: 0 };- for (const outcome of outcomes) {- if (outcome) counts[outcome]++;+ for (const plan of plans) {+ if (plan.unlink) await unlink(plan.unlink);+ if (plan.write) await writeFile(plan.write.path, plan.write.content);+ if (plan.warn) console.warn(plan.warn);+ if (plan.log) console.log(plan.log);+ if (plan.counter) counts[plan.counter]++;}return counts;diff --git a/scripts/migrations/_lib.test.js b/scripts/migrations/_lib.test.jsindex 3e7aaddcc8..448b63b88d 100644--- a/scripts/migrations/_lib.test.js+++ b/scripts/migrations/_lib.test.js@@ -146,6 +146,27 @@ describe('applyPromptReplaceMigration opt-ins', () => {expect(readFileSync(join(stagesDir, name), 'utf-8')).toBe(BODY_NEW);}});++ it('fail-stops before any write when one file has a missing sample', async () => {+ // Regression for the parallel-scan failure semantics: the plan phase+ // reads every file concurrently, but a single rejection (here, an+ // accepted-old file whose sample is gone → ENOENT) must abort the whole+ // migration BEFORE the apply phase mutates ANY file — so the other+ // updatable file is left untouched, not nondeterministically written.+ writeFileSync(join(stagesDir, 'pipeline-a.md'), BODY_OLD);+ writeFileSync(join(sampleDir, 'pipeline-a.md'), BODY_NEW); // would update+ writeFileSync(join(stagesDir, 'pipeline-b.md'), BODY_OLD); // sample missing → throws+ writeFileSync(join(stagesDir, 'pipeline-c.md'), BODY_OLD);+ writeFileSync(join(sampleDir, 'pipeline-c.md'), BODY_NEW); // would update++ await expect(+ applyPromptReplaceMigration({ rootDir, ...multiOpts }),+ ).rejects.toThrow(/ENOENT/);++ // No write landed — every data file still holds its pre-migration body.+ expect(readFileSync(join(stagesDir, 'pipeline-a.md'), 'utf-8')).toBe(BODY_OLD);+ expect(readFileSync(join(stagesDir, 'pipeline-c.md'), 'utf-8')).toBe(BODY_OLD);+ });});});