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: verify migration 224's plan marker instead of trusting it, and stop swallowing discard errors (#3465)
[._.]/ Adam Eivy3b59b0be3198
diff --git a/scripts/migrations/224-iso-week-numbering-year-ids.js b/scripts/migrations/224-iso-week-numbering-year-ids.jsindex ac9e478846..4825998d6e 100644--- a/scripts/migrations/224-iso-week-numbering-year-ids.js+++ b/scripts/migrations/224-iso-week-numbering-year-ids.js@@ -212,23 +212,40 @@ const PLAN_FILE = '.migration-224-plan';/*** Finish (or discard) a previous run that died mid-write.*- * Plan file present ⇒ every rewrite was durable, so promote whatever staging- * files are left; that is precisely the set whose targets may have been+ * Plan file present ⇒ every rewrite reached staging, so promote whatever+ * staging files are left; that is precisely the set whose targets may have been* clobbered. Plan file absent ⇒ staging was incomplete and promoting would* overwrite live digests with a partial plan, so drop the leftovers.+ *+ * The plan lists its targets, so the promote path can VERIFY that set rather+ * than trusting the marker: a target with neither a staging file nor a promoted+ * file lost data the retry cannot reconstruct (no fsync barrier stands between+ * the staging writes and the marker), so throw and leave the migration+ * unrecorded instead of promoting over a live digest on top of that loss.*/async function recoverInterruptedRun(dir, entries) {+ const present = new Set(entries);const staged = entries.filter(f => f.endsWith(STAGING_SUFFIX));const plan = await readJsonOrNull(join(dir, PLAN_FILE));if (staged.length === 0 && !plan) return false;if (plan) {+ const planned = Array.isArray(plan.files) ? plan.files : [];+ const lost = planned.filter(f => !present.has(`${f}${STAGING_SUFFIX}`) && !present.has(f));+ if (lost.length > 0) {+ throw new Error(`migration 224: interrupted run left ${lost.length} planned digest rewrite(s) with neither a staged nor a promoted file (${lost.join(', ')}) — refusing to promote a partial plan over live digests`);+ }for (const file of staged) {await rename(join(dir, file), join(dir, file.slice(0, -STAGING_SUFFIX.length)));}console.warn(`⚠️ migration 224: completed ${staged.length} staged rewrite(s) left by an interrupted run`);} else {- for (const file of staged) await unlink(join(dir, file)).catch(() => {});+ // Same rule as the main cleanup: only an already-gone file is benign. Any+ // other failure must throw, or the runner banks 224 as applied with staged+ // data still on disk and recovery never runs again.+ for (const file of staged) {+ await unlink(join(dir, file)).catch((err) => { if (err.code !== 'ENOENT') throw err; });+ }console.warn(`⚠️ migration 224: discarded ${staged.length} incomplete staged rewrite(s) from an interrupted run`);}await unlink(join(dir, PLAN_FILE)).catch((err) => { if (err.code !== 'ENOENT') throw err; });diff --git a/scripts/migrations/224-iso-week-numbering-year-ids.test.js b/scripts/migrations/224-iso-week-numbering-year-ids.test.jsindex e8f52eab3f..4aceac851d 100644--- a/scripts/migrations/224-iso-week-numbering-year-ids.test.js+++ b/scripts/migrations/224-iso-week-numbering-year-ids.test.js@@ -322,6 +322,19 @@ describe('migration 224 up()', () => {expect((await readdir(digestsDir())).sort()).toEqual(['2026-W01.json']);});+ it('refuses to promote when a planned rewrite has neither a staged nor a promoted file', async () => {+ // The marker survived a crash but one staging write did not. Promoting the+ // survivor would overwrite a live source whose replacement no longer exists.+ await mkdir(digestsDir(), { recursive: true });+ await writeFile(join(digestsDir(), '2019-W01.json'), JSON.stringify(digestFixture('2019-W01', '2019-12-30')));+ await writeFile(join(digestsDir(), '2019-W01.json.staging-224'), JSON.stringify(digestFixture('2019-W01', '2018-12-31')));+ await writeFile(join(digestsDir(), '.migration-224-plan'), JSON.stringify({ migration: 224, files: ['2019-W01.json', '2020-W01.json'] }));++ await expect(migration.up({ rootDir })).rejects.toThrow(/2020-W01\.json/);+ // Nothing was promoted, so the live digest is intact for the next attempt.+ expect(await readJson(join(digestsDir(), '2019-W01.json'))).toMatchObject({ weekId: '2019-W01' });+ });+it('leaves no staging or plan files behind on a successful run', async () => {await mkdir(digestsDir(), { recursive: true });await writeFile(join(digestsDir(), '2025-W01.json'), JSON.stringify(digestFixture('2025-W01', '2025-12-29')));