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
make migration 305 reversible by backing up the plan before refreshing it
[._.]/ Adam Eivy4715ecd2adb2
diff --git a/scripts/migrations/305-refresh-quota-burn-audit-contract.js b/scripts/migrations/305-refresh-quota-burn-audit-contract.jsindex 8a8d507297..c83ea4ed06 100644--- a/scripts/migrations/305-refresh-quota-burn-audit-contract.js+++ b/scripts/migrations/305-refresh-quota-burn-audit-contract.js@@ -25,6 +25,20 @@ import { upgradeStoredAuditPrompt } from '../../server/lib/quotaBurnPresets.js';const QUOTA_BURN_PATH = join('data', 'cos', 'quota-burn.json');+/**+ * Where the pre-refresh plan is copied before anything is rewritten.+ *+ * The two gates in `upgradeStoredAuditPrompt` recognize a user's own procedure+ * by structure, and structure can only ever be a heuristic: a user who kept+ * every shipped anchor and heading but added an unbolded sentence of their own+ * inside the contract would still have it replaced. `data/` is gitignored+ * runtime state, so there would be nothing to recover it from. One copy taken+ * before the first rewrite makes the whole question moot — the refresh stops+ * being irreversible, which is worth more than tightening a heuristic that+ * cannot be made exact without reintroducing migration 294's failure.+ */+const BACKUP_PATH = join('data', 'cos', 'quota-burn.pre-305.json');+/*** Absent → `null` (nothing to migrate). Unparseable → THROW.*@@ -34,14 +48,16 @@ const QUOTA_BURN_PATH = join('data', 'cos', 'quota-burn.json');* whole point of this migration would never run again. Failing loudly on a* corrupt file is the only outcome that stays recoverable.*/-async function readJson(path) {+async function readPlan(path) {const raw = await readFile(path, 'utf-8').catch((err) => {if (err.code === 'ENOENT') return null;throw err;});if (raw == null) return null;try {- return JSON.parse(raw);+ // The raw text rides along so the backup is a byte-for-byte copy of what+ // was on disk, not a re-serialization that could differ in formatting.+ return { raw, config: JSON.parse(raw) };} catch (err) {throw new Error(`${QUOTA_BURN_PATH} is not valid JSON (${err.message}) — repair it and re-run migrations`);}@@ -50,7 +66,9 @@ async function readJson(path) {export default {async up({ rootDir }) {const fullPath = join(rootDir, QUOTA_BURN_PATH);- const config = await readJson(fullPath);+ const plan = await readPlan(fullPath);+ const config = plan?.config;+ const original = plan?.raw;if (!config?.families) return { updated: 0 };let updated = 0;@@ -76,7 +94,15 @@ export default {// repaired-JSON path above would then never re-run because the runner has// already recorded 305 as applied. The trailing newline matches what the// store itself writes, so this never shows as a spurious diff.- if (updated) await atomicWrite(fullPath, `${JSON.stringify(config, null, 2)}\n`);+ //+ // The backup is written FIRST and only when there is something to change,+ // so a no-op install gains no stray file and a crash between the two writes+ // still leaves the original plan readable at BACKUP_PATH.+ if (updated) {+ await atomicWrite(join(rootDir, BACKUP_PATH), original);+ console.log(`💾 ${QUOTA_BURN_PATH}: previous plan saved to ${BACKUP_PATH}`);+ await atomicWrite(fullPath, `${JSON.stringify(config, null, 2)}\n`);+ }if (skipped) console.log(`✋ ${QUOTA_BURN_PATH}: left ${skipped} custom or already-current prompt(s) untouched`);return { updated, skipped };},diff --git a/scripts/migrations/305-refresh-quota-burn-audit-contract.test.js b/scripts/migrations/305-refresh-quota-burn-audit-contract.test.jsindex 0267141001..db47111ae0 100644--- a/scripts/migrations/305-refresh-quota-burn-audit-contract.test.js+++ b/scripts/migrations/305-refresh-quota-burn-audit-contract.test.js@@ -1,5 +1,5 @@import { describe, it, expect, beforeEach, afterEach } from 'vitest';-import { mkdtempSync, rmSync, writeFileSync, readFileSync, mkdirSync } from 'fs';+import { mkdtempSync, rmSync, writeFileSync, readFileSync, mkdirSync, existsSync } from 'fs';import { tmpdir } from 'os';import { join } from 'path';@@ -40,11 +40,13 @@ const storedPrompt = (path) => readJson(path).families.claude.jobs[0].params.prodescribe('migration 305 — refresh quota-burn audit contract', () => {let rootDir;let configPath;+ let backupPath;beforeEach(() => {rootDir = mkdtempSync(join(tmpdir(), 'migration-305-'));mkdirSync(join(rootDir, 'data/cos'), { recursive: true });configPath = join(rootDir, 'data/cos/quota-burn.json');+ backupPath = join(rootDir, 'data/cos/quota-burn.pre-305.json');});afterEach(() => rmSync(rootDir, { recursive: true, force: true }));@@ -56,6 +58,24 @@ describe('migration 305 — refresh quota-burn audit contract', () => {expect(storedPrompt(configPath)).toBe(currentPrompt);});+ it('saves a byte-for-byte backup before rewriting anything', async () => {+ // The edit-detection gates are structural heuristics, and `data/` is+ // gitignored, so without this copy a wrongly-refreshed prompt is gone for+ // good. Asserted on the raw bytes, not the parsed object, because a+ // re-serialized backup would not round-trip a hand-formatted file.+ writeJson(configPath, config(stalePrompt));+ const before = readFileSync(configPath, 'utf8');+ await migration.up({ rootDir });+ expect(readFileSync(backupPath, 'utf8')).toBe(before);+ expect(readFileSync(configPath, 'utf8')).not.toBe(before);+ });++ it('writes no backup when it upgrades nothing', async () => {+ writeJson(configPath, config(currentPrompt));+ await migration.up({ rootDir });+ expect(existsSync(backupPath)).toBe(false);+ });+it('leaves a prompt whose How-to-run section the user rewrote', async () => {// Mission intact, procedure replaced — the anchors are gone, so this is the// user's own contract and refreshing it would delete their instructions.