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: stop re-downloading a schema-refused sync snapshot every cycle (#12645)
[._.]/4c3652918d89
diff --git a/server/services/syncOrchestrator.js b/server/services/syncOrchestrator.jsindex 825a3fd2e1..25e119bf43 100644--- a/server/services/syncOrchestrator.js+++ b/server/services/syncOrchestrator.js@@ -568,10 +568,14 @@ async function syncDataCategoryFromPeer(peer, peerId, category, cachedChecksums,const lastChecksum = cachedChecksums?.[category] ?? null;// A saved schema gap is compatibility state, independent of payload changes.- // Keep retrying that category even when its payload checksum is unchanged so- // a peer upgrade can clear the old warning without requiring another edit.- const hasSchemaGap = Boolean(peer?.schemaGaps?.[category]);- if (lastChecksum && lastChecksum === checksumRes.checksum && !hasSchemaGap) {+ // Retry only when something that could clear it moved (payload checksum, the+ // peer's version, or our own schema versions); otherwise the refused body+ // would be downloaded and discarded again every cycle.+ const gap = peer?.schemaGaps?.[category];+ if (gap && !schemaGapMayHaveCleared(gap, peer, checksumRes.checksum)) {+ return { totalApplied: 0, checksum: null, blockedBySchema: gap };+ }+ if (!gap && lastChecksum && lastChecksum === checksumRes.checksum) {return { totalApplied: 0, checksum: checksumRes.checksum };}@@ -605,7 +609,7 @@ async function syncDataCategoryFromPeer(peer, peerId, category, cachedChecksums,peerId,});if (result.blockedBySchema) {- await recordPeerSchemaGap(peerId, category, result.blockedBySchema)+ await recordPeerSchemaGap(peerId, category, { ...result.blockedBySchema, checksum: checksumRes.checksum }).catch((err) => logFailureWithStack('⚠️ syncOrchestrator: persist schema gap failed', err));// Don't advance the cached checksum — when the user upgrades, the// category will look "changed" again and we'll re-try the apply.@@ -687,9 +691,25 @@ async function recordPeerSchemaGap(peerId, category, gap) {ahead: Array.isArray(gap.ahead) ? gap.ahead : [],behind: Array.isArray(gap.behind) ? gap.behind : [],senderPortosVersion: typeof gap.senderPortosVersion === 'string' ? gap.senderPortosVersion : null,+ // Inputs that could clear the block; see schemaGapMayHaveCleared.+ checksum: typeof gap.checksum === 'string' ? gap.checksum : null,+ localSchemaVersions: { ...PORTOS_SCHEMA_VERSIONS },});}+/**+ * True when a recorded snapshot-category gap is worth re-fetching: the refused+ * payload's checksum, the peer's probed version, or our own schema versions+ * differ from what the gap was recorded against. A gap recorded without that+ * evidence (older install, catalog gap) always retries.+ */+function schemaGapMayHaveCleared(gap, peer, checksum) {+ if (typeof gap.checksum !== 'string' || !isPlainObjectShallow(gap.localSchemaVersions)) return true;+ if (gap.checksum !== checksum) return true;+ if ((peer?.version ?? null) !== (gap.senderPortosVersion ?? null)) return true;+ return JSON.stringify(gap.localSchemaVersions) !== JSON.stringify(PORTOS_SCHEMA_VERSIONS);+}+async function clearPeerSchemaGap(peerId, category) {await setPeerSchemaGap(peerId, category, null);}diff --git a/server/services/syncOrchestrator.test.js b/server/services/syncOrchestrator.test.jsindex 81550d6af7..e8405c60d7 100644--- a/server/services/syncOrchestrator.test.js+++ b/server/services/syncOrchestrator.test.js@@ -1,6 +1,7 @@import http from 'node:http';import { once } from 'node:events';import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';+import { PORTOS_SCHEMA_VERSIONS } from '../lib/schemaVersions.js';// Mock all dependenciesvi.mock('./instances.js', async () => ({@@ -1306,6 +1307,67 @@ describe('syncOrchestrator', () => {});});+ // A category refused for a schema gap must not re-download the refused body+ // every cycle; only a changed checksum, peer version or local schema retries.+ describe('schema-gapped snapshot category', () => {+ let dataSync;+ const snapshotCalls = () => mockFetch.mock.calls.filter((c) => c[0].includes('/goals/snapshot'));+ const gapPeer = (gapOverrides = {}, peerOverrides = {}) => ({+ ...mockPeer,+ version: '2.5.0',+ syncCategories: { goals: true },+ schemaGaps: {+ goals: {+ detectedAt: '2026-01-01T00:00:00.000Z', ahead: ['goals'], behind: [],+ senderPortosVersion: '2.5.0', checksum: 'remote',+ localSchemaVersions: { ...PORTOS_SCHEMA_VERSIONS }, ...gapOverrides,+ },+ },+ ...peerOverrides,+ });++ beforeEach(async () => {+ dataSync = await import('./dataSync.js');+ dataSync.getSupportedCategories.mockReturnValue(['goals']);+ mockFetch.mockImplementation(async (url) => {+ if (url.includes('/goals/checksum')) return { ok: true, json: async () => ({ checksum: 'remote' }) };+ if (url.includes('/goals/snapshot')) return { ok: true, json: async () => ({ data: { goals: [] }, checksum: 'remote' }) };+ return { ok: true, json: async () => ({}) };+ });+ });++ it('makes no snapshot request while checksum, peer version and local schema are unchanged', async () => {+ await syncWithPeer(gapPeer());+ expect(snapshotCalls()).toHaveLength(0);+ expect(dataSync.applyRemote).not.toHaveBeenCalled();+ });++ it.each([+ ['the checksum', {}, { checksum: 'older' }],+ ['the peer version', { version: '2.6.0' }, {}],+ ['our local schema versions', {}, { localSchemaVersions: { ...PORTOS_SCHEMA_VERSIONS, goals: -1 } }],+ ['a gap recorded without retry evidence', {}, { checksum: undefined, localSchemaVersions: undefined }],+ ])('fetches exactly once when %s changed, and a compatible payload clears the gap', async (_label, peerOverrides, gapOverrides) => {+ const { setPeerSchemaGap } = await import('./instances.js');+ dataSync.applyRemote.mockResolvedValue({ applied: true, count: 1 });+ await syncWithPeer(gapPeer(gapOverrides, peerOverrides));+ expect(snapshotCalls()).toHaveLength(1);+ expect(setPeerSchemaGap).toHaveBeenCalledWith(mockPeer.instanceId, 'goals', null);+ });++ it('records the refused checksum and schema versions when a snapshot is blocked', async () => {+ const { setPeerSchemaGap } = await import('./instances.js');+ dataSync.applyRemote.mockResolvedValue({+ applied: false, count: 0,+ blockedBySchema: { ahead: ['goals'], behind: [], senderPortosVersion: '2.7.0' },+ });+ await syncWithPeer({ ...mockPeer, syncCategories: { goals: true } });+ expect(setPeerSchemaGap).toHaveBeenCalledWith(mockPeer.instanceId, 'goals', expect.objectContaining({+ checksum: 'remote', senderPortosVersion: '2.7.0', localSchemaVersions: PORTOS_SCHEMA_VERSIONS,+ }));+ });+ });+// #5759: `usage` is always dirty AND a map of per-instance slots, so the// checksum short-circuit alone dragged every instance's digest across the// wire whenever any one of them advanced.