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(backup): recover a pre-split snapshot's CoS config on a repeat restore too
Adam Eivy6e5053e76774
diff --git a/server/services/backup.js b/server/services/backup.jsindex 08b4bd4cce..167e289000 100644--- a/server/services/backup.js+++ b/server/services/backup.js@@ -1757,13 +1757,31 @@ async function restoreHeldSnapshot({ snapshotDir, currentSource }, snapshotId, {const COS_RESTORE_SCOPES = ['cos', 'cos/config.json', 'cos/state.json', 'cos/agents'];+/**+ * The CoS config a restore of `scope` installs from a snapshot taken before the+ * #6182 split: such a snapshot has no cos/config.json and keeps the settings in+ * the `config` slice of cos/state.json. Null when the snapshot carries its own+ * config.json or the scope leaves cos/state.json alone.+ */+async function snapshotLegacyCosConfig(srcDir, scope) {+ if (scope && scope !== 'cos' && scope !== 'cos/state.json') return null;+ const carriesConfigFile = await stat(join(srcDir, 'cos', 'config.json')).then(+ info => info.isFile(),+ error => { if (error.code === 'ENOENT') return false; throw error; },+ );+ if (carriesConfigFile) return null;+ const { ok, value } = await readJSONFileStrict(join(srcDir, 'cos', 'state.json'), null, { logError: false });+ const config = ok ? value?.config : null;+ return config && typeof config === 'object' && !Array.isArray(config) ? config : null;+}+/*** Owners a live file restore holds, OUTERMOST FIRST — this array order IS the* acquisition order, and each owner is released in reverse after everything* inside it (transfer plus partial-failure reconciliation) has settled.* `appliesTo` receives the normalized scope (`''`/undefined = full restore);- * `hold(inner, { srcDir })` acquires the owner, runs `inner`, and releases on- * every exit; `srcDir` is the verified snapshot's data directory. Domain+ * `hold(inner, { srcDir, scope })` acquires the owner, runs `inner`, and+ * releases on every exit; `srcDir` is the verified snapshot's data directory. Domain* modules are imported only once every outer owner is held.** - The snapshot cut drains in-flight asset publications BEFORE any domain@@ -1807,14 +1825,10 @@ const LIVE_FILE_RESTORE_OWNERS = Object.freeze([{name: 'CoS config and runtime',appliesTo: scope => !scope || COS_RESTORE_SCOPES.includes(scope) || scope.startsWith('cos/agents/'),- hold: async (inner, { srcDir }) => {- // A pre-split snapshot (#6182) has no config.json; its config is in state.json.- const snapshotCarriesConfig = await stat(join(srcDir, 'cos', 'config.json')).then(- info => info.isFile(),- error => { if (error.code === 'ENOENT') return false; throw error; },- );+ hold: async (inner, { srcDir, scope }) => {+ const snapshotLegacyConfig = await snapshotLegacyCosConfig(srcDir, scope);const { withLiveCosRestore } = await import('./cosState.js');- return withLiveCosRestore(inner, { snapshotCarriesConfig });+ return withLiveCosRestore(inner, { snapshotLegacyConfig });},},{@@ -1838,7 +1852,7 @@ const LIVE_FILE_RESTORE_OWNERS = Object.freeze([/** Run `operation` inside every live-restore owner that applies to `scope`, in table order. */function withLiveFileRestoreOwners(scope, srcDir, operation) {const nested = LIVE_FILE_RESTORE_OWNERS.reduceRight(- (inner, owner) => (owner.appliesTo(scope) ? () => owner.hold(inner, { srcDir }) : inner),+ (inner, owner) => (owner.appliesTo(scope) ? () => owner.hold(inner, { srcDir, scope }) : inner),operation,);return nested();diff --git a/server/services/backup.test.js b/server/services/backup.test.jsindex 3643c3a0de..3bff30ef73 100644--- a/server/services/backup.test.js+++ b/server/services/backup.test.js@@ -2295,6 +2295,22 @@ describe('restoreSnapshot manifest verification', () => {expect(await realFs.readFile(livePath, 'utf8')).toBe('healthy live data');});+ // A pre-#6182 snapshot keeps the CoS config in state.json's `config` slice.+ it.each([+ { name: 'pre-split snapshot, full restore', files: ['cos/state.json'], subdirFilter: undefined, expected: { maxConcurrentAgents: 7 } },+ { name: 'pre-split snapshot, state.json scope', files: ['cos/state.json'], subdirFilter: 'cos/state.json', expected: { maxConcurrentAgents: 7 } },+ { name: 'pre-split snapshot, agents scope', files: ['cos/state.json'], subdirFilter: 'cos/agents', expected: null },+ { name: 'snapshot with config.json', files: ['cos/state.json', 'cos/config.json'], subdirFilter: undefined, expected: null },+ ])('hands CoS restore the legacy config slice the snapshot installs: $name', async ({ files, subdirFilter, expected }) => {+ const bodies = { 'cos/state.json': '{"agents":{},"config":{"maxConcurrentAgents":7}}', 'cos/config.json': '{"maxConcurrentAgents":5}' };+ const manifest = {};+ for (const file of files) manifest[file] = await writeSnapshotFile(file, bodies[file]);+ await writeManifest(manifest);+ withLiveCosRestore.mockClear();+ await finishRestore({ dryRun: false, subdirFilter });+ expect(withLiveCosRestore).toHaveBeenCalledWith(expect.any(Function), { snapshotLegacyConfig: expected });+ });+it('names the unmanifested file so the operator can act on it', async () => {const relativePath = 'restore-integrity/example.json';await writeManifest({ [relativePath]: await writeSnapshotFile(relativePath, 'trusted backup') });@@ -3428,8 +3444,6 @@ describe('restoreSnapshot snapshotId, filter flags, and settings re-sync', () =>it.each([undefined, 'cos', 'cos/', 'cos/config.json', 'cos/state.json', 'cos/agents', 'cos/agents/', 'cos/agents/index.json'])('holds the boundary for affected live scope %s', async subdirFilter => {await runRestore('/dest', 'snap-1', { dryRun: false, subdirFilter });expect(withLiveCosRestore).toHaveBeenCalledTimes(1);- // This snapshot has no cos/config.json, like one taken before the #6182 split.- expect(withLiveCosRestore).toHaveBeenCalledWith(expect.any(Function), { snapshotCarriesConfig: false });});it.each([{ dryRun: true }, { dryRun: false, subdirFilter: 'images' }, { dryRun: true, subdirFilter: 'cos/agents' }])('leaves unaffected scope alone: %j', async options => {await runRestore('/dest', 'snap-1', options);diff --git a/server/services/cosState.js b/server/services/cosState.jsindex e423b706e4..d92d240592 100644--- a/server/services/cosState.js+++ b/server/services/cosState.js@@ -557,15 +557,15 @@ const legacyConfigSlice = (state) => (isPlainObject(state?.config) ? JSON.string* state.json and carries no config.json. Restored over an upgraded install, the* live config.json is newer than the snapshot yet wins every read (and migration* 339 no-ops on its presence), so the restored settings were silently discarded- * and then stripped by the next saveState. When the transfer lands a legacy- * slice the live state.json did not already hold, move config.json aside — kept- * for manual recovery, never deleted — so readConfig() completes the split from- * the restored slice.+ * and then stripped by the next saveState. Once the transfer has installed the+ * snapshot's slice (a failed partial transfer may not have), move config.json+ * aside — kept for manual recovery, never deleted — so readConfig() completes+ * the split from the restored slice.*/-async function supersedeConfigForRestoredLegacySlice(sliceBefore) {+async function supersedeConfigForRestoredLegacySlice(snapshotLegacyConfig) {if (!existsSync(STATE_FILE) || !existsSync(CONFIG_FILE)) return;const restored = legacyConfigSlice(parseStateFile(await readFile(STATE_FILE, 'utf-8'), 'CoS state (restored legacy config)'));- if (restored === null || restored === sliceBefore) return;+ if (restored === null || restored !== JSON.stringify(snapshotLegacyConfig)) return;const parked = join(dirname(CONFIG_FILE), `config.pre-restore-${new Date().toISOString().replace(/[:.]/g, '-')}.json`);await rename(CONFIG_FILE, parked);console.log(`📦 Restored snapshot predates config.json; moved the newer CoS config aside to ${basename(parked)} and recovering settings from the restored state.json`);@@ -576,10 +576,10 @@ async function supersedeConfigForRestoredLegacySlice(sliceBefore) {* reload settle. This order matches config changes that consult runtime state;* never acquire the config queue from inside the state queue.* Callers must read their mutation's pre-image INSIDE its respective queue.- * `snapshotCarriesConfig: false` marks a snapshot without config.json (see- * supersedeConfigForRestoredLegacySlice).+ * `snapshotLegacyConfig` is the config slice a pre-split snapshot installs, if+ * any (see supersedeConfigForRestoredLegacySlice).*/-export function withLiveCosRestore(transfer, { snapshotCarriesConfig = true } = {}) {+export function withLiveCosRestore(transfer, { snapshotLegacyConfig = null } = {}) {return withConfigLock(() => withStateLock(async () => {const persisted = await readStateForSafetyCheck();const states = [stateCache, persisted.state].filter(Boolean);@@ -594,14 +594,12 @@ export function withLiveCosRestore(transfer, { snapshotCarriesConfig = true } =});}- const sliceBefore = legacyConfigSlice(persisted.state);const [result] = await Promise.allSettled([Promise.resolve().then(transfer)]);restoreGeneration += 1;configCache = null;stateCache = null;const [reload] = await Promise.allSettled([(async () => {- // Also after a partial transfer: a restored slice is the snapshot's config.- if (!snapshotCarriesConfig) await supersedeConfigForRestoredLegacySlice(sliceBefore);+ if (snapshotLegacyConfig) await supersedeConfigForRestoredLegacySlice(snapshotLegacyConfig);const { reloadAgentIndexAfterRestore } = await import('./cosAgentIndex.js');await reloadAgentIndexAfterRestore();const state = await loadState();diff --git a/server/services/cosState.test.js b/server/services/cosState.test.jsindex be91872d59..87d93d16b2 100644--- a/server/services/cosState.test.js+++ b/server/services/cosState.test.js@@ -418,17 +418,21 @@ describe('live CoS restore boundary', () => {// A pre-#6182 snapshot keeps its config in state.json and ships no// config.json, so the live config.json is newer than the restore point.it.each([- { snapshotCarriesConfig: false, restoredConfig: null, expected: 7, parked: true },- { snapshotCarriesConfig: true, restoredConfig: { maxConcurrentAgents: 5 }, expected: 5, parked: false },- ])('restores the snapshot config over a newer config.json: %j', async ({ snapshotCarriesConfig, restoredConfig, expected, parked }) => {+ { name: 'first restore', liveSlice: null, landed: true, legacy: true, expected: 7, parked: true },+ { name: 'repeat restore', liveSlice: { maxConcurrentAgents: 7 }, landed: true, legacy: true, expected: 7, parked: true },+ { name: 'transfer failed first', liveSlice: null, landed: false, legacy: true, expected: 3, parked: false },+ { name: 'snapshot has config.json', liveSlice: null, landed: true, legacy: false, expected: 5, parked: false },+ ])('restores the snapshot config over a newer config.json: $name', async ({ liveSlice, landed, legacy, expected, parked }) => {writeConfig({ maxConcurrentAgents: 3 });- writeState({ agents: {} });+ writeState(liveSlice ? { agents: {}, config: liveSlice } : { agents: {} });const store = await freshModule();- await store.loadState();- await store.withLiveCosRestore(async () => {- if (restoredConfig) writeConfig(restoredConfig);+ const restore = store.withLiveCosRestore(async () => {+ if (!landed) throw new Error('rsync failed');+ if (!legacy) writeConfig({ maxConcurrentAgents: 5 });writeState({ agents: {}, config: { maxConcurrentAgents: 7 } });- }, { snapshotCarriesConfig });+ }, { snapshotLegacyConfig: legacy ? { maxConcurrentAgents: 7 } : null });+ if (landed) await restore;+ else await expect(restore).rejects.toThrow('rsync failed');expect((await store.getConfig()).maxConcurrentAgents).toBe(expected);expect(readJson(CONFIG_PATH).maxConcurrentAgents).toBe(expected);const aside = readdirSync(COS_DIR).filter(f => f.startsWith('config.pre-restore-'));