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
address review (claude): sort synced documents for a stable snapshot checksum
[._.]/ Adam Eivyfbbf64e6429d
diff --git a/server/services/digital-twin-sync.js b/server/services/digital-twin-sync.jsindex 958abbc291..e8e969f2a1 100644--- a/server/services/digital-twin-sync.js+++ b/server/services/digital-twin-sync.js@@ -293,6 +293,11 @@ async function readMarkdownDocuments() {readFile(join(DIR, name), 'utf-8').then((content) => [name, content], () => [name, null])));+ // Sort by filename before building the map: readdir() order is+ // filesystem-dependent, and the documents object's key insertion order feeds+ // the snapshot checksum via JSON.stringify — without a stable order two peers+ // with identical documents compute different checksums and never converge.+ reads.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));const out = {};for (const [name, content] of reads) if (typeof content === 'string') out[name] = content;return out;diff --git a/server/services/digital-twin-sync.snapshot.test.js b/server/services/digital-twin-sync.snapshot.test.jsnew file mode 100644index 0000000000..94a023ac04--- /dev/null+++ b/server/services/digital-twin-sync.snapshot.test.js@@ -0,0 +1,60 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';++// digital-twin-sync reads the .md documents via fs/promises readdir+readFile and+// the JSON files via fileUtils.readJSONFile. Mock those so we can vary the+// readdir() order between two snapshots of identical data and assert the+// checksum is stable (the documents map must be sorted, not readdir-ordered).++const readdirMock = vi.fn();+const readFileMock = vi.fn();+vi.mock('fs/promises', () => ({+ readdir: (...a) => readdirMock(...a),+ readFile: (...a) => readFileMock(...a),+}));++const readJSONFileMock = vi.fn();+vi.mock('../lib/fileUtils.js', async () => {+ const actual = await vi.importActual('../lib/fileUtils.js');+ return new Proxy(actual, {+ get(target, prop) {+ if (prop === 'readJSONFile') return readJSONFileMock;+ return target[prop];+ },+ });+});++const { getDigitalTwinSnapshot } = await import('./digital-twin-sync.js');++describe('getDigitalTwinSnapshot — checksum determinism', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ readJSONFileMock.mockResolvedValue(null); // all JSON files absent+ // content keyed by basename so both snapshots see identical bytes per file+ readFileMock.mockImplementation((p) => Promise.resolve(`# content of ${String(p).split('/').pop()}`));+ });++ it('is stable regardless of readdir() ordering for identical documents', async () => {+ readdirMock.mockResolvedValueOnce(['b.md', 'a.md', 'c.md']);+ const first = await getDigitalTwinSnapshot();++ readdirMock.mockResolvedValueOnce(['c.md', 'b.md', 'a.md']);+ const second = await getDigitalTwinSnapshot();++ expect(second.checksum).toBe(first.checksum);+ // documents map is sorted regardless of the order readdir returned+ expect(Object.keys(first.data.documents)).toEqual(['a.md', 'b.md', 'c.md']);+ });++ it('ignores non-.md and unsafe filenames in the snapshot', async () => {+ readdirMock.mockResolvedValueOnce(['a.md', 'notes.txt', '.hidden.md', 'b.md']);+ const { data } = await getDigitalTwinSnapshot();+ expect(Object.keys(data.documents)).toEqual(['a.md', 'b.md']);+ });++ it('produces a snapshot whose documents survive a JSON round-trip key-stable', async () => {+ readdirMock.mockResolvedValue(['a.md', 'b.md']);+ const { data } = await getDigitalTwinSnapshot();+ // re-serializing must not reorder keys (what the checksum relies on)+ expect(JSON.stringify(data.documents)).toBe(JSON.stringify({ ...data.documents }));+ });+});