Skip to content

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

fix: return null failure sentinel for malformed peer manifests (#4560)

[._.]/ Adam Eivy86d3ad7f6751
diff --git a/server/services/brainParity.js b/server/services/brainParity.js
index 6ec5d2ac00..1b115769e1 100644
--- a/server/services/brainParity.js
+++ b/server/services/brainParity.js
@@ -99,9 +99,10 @@ export async function buildBrainManifest() {
* store we don't have yet is not divergence, it's a version gap.
*/
function normalizeRemoteManifest(body) {
+ if (!body || typeof body !== 'object') return null;
+ const types = body.types;
+ if (!types || typeof types !== 'object' || Array.isArray(types)) return null;
const out = {};
- const types = body?.types;
- if (!types || typeof types !== 'object') return out;
for (const type of BRAIN_ENTITY_TYPES) {
const rows = types[type];
if (!Array.isArray(rows)) continue;
@@ -121,12 +122,13 @@ function normalizeRemoteManifest(body) {
* every peer that supports reconcile at all, not just upgraded ones.
*/
function manifestFromSnapshot(snapshot) {
+ if (!snapshot || typeof snapshot !== 'object') return null;
+ const records = snapshot.records;
+ if (!records || typeof records !== 'object' || Array.isArray(records)) return null;
const out = {};
- const records = snapshot?.records;
- if (!records || typeof records !== 'object') return out;
for (const type of BRAIN_ENTITY_TYPES) {
const byId = records[type];
- if (!byId || typeof byId !== 'object') continue;
+ if (!byId || typeof byId !== 'object' || Array.isArray(byId)) continue;
out[type] = Object.entries(byId)
.filter(([, rec]) => rec && typeof rec === 'object')
.map(([id, rec]) => ({ id, updatedAt: rec.updatedAt ?? null, deleted: rec._deleted === true }));
@@ -157,7 +159,11 @@ async function fetchPeerJson(peer, path) {
*/
async function fetchRemoteManifest(peer) {
const direct = await fetchPeerJson(peer, '/api/brain/reconcile/manifest');
- if (!direct.reason) return { reason: null, byType: normalizeRemoteManifest(direct.body) };
+ if (!direct.reason) {
+ const normalized = normalizeRemoteManifest(direct.body);
+ if (!normalized) return { reason: 'fetch-failed', byType: null };
+ return { reason: null, byType: normalized };
+ }
if (direct.reason !== 'peer-too-old') return { reason: direct.reason, byType: null };
const snapshot = await fetchPeerJson(peer, '/api/brain/reconcile/snapshot');
@@ -166,7 +172,9 @@ async function fetchRemoteManifest(peer) {
// all — still "too old", just further back.
return { reason: snapshot.reason === 'peer-too-old' ? 'peer-too-old' : snapshot.reason, byType: null };
}
- return { reason: null, byType: manifestFromSnapshot(snapshot.body) };
+ const normalized = manifestFromSnapshot(snapshot.body);
+ if (!normalized) return { reason: 'fetch-failed', byType: null };
+ return { reason: null, byType: normalized };
}
const emptyCounts = () => ({
diff --git a/server/services/brainParity.test.js b/server/services/brainParity.test.js
index 5acb892fb0..7623fdb68f 100644
--- a/server/services/brainParity.test.js
+++ b/server/services/brainParity.test.js
@@ -249,6 +249,56 @@ describe('checkPeerBrainParity', () => {
expect(await checkPeerBrainParity(PEER)).toMatchObject({ available: false, reason: 'peer-too-old' });
});
+ it('treats a 200 with a malformed manifest body (missing types) as fetch-failed rather than divergence', async () => {
+ stores({ people: { p1: { id: 'p1', updatedAt: 'T1' } } });
+ peerRoutes({
+ '/api/brain/reconcile/manifest': jsonResponse({ ok: true }),
+ });
+
+ const report = await checkPeerBrainParity(PEER);
+
+ expect(report.available).toBe(false);
+ expect(report.reason).toBe('fetch-failed');
+ expect(report.summary.total).toBe(0);
+ });
+
+ it('preserves empty-but-valid manifest { types: {} } as valid parity check reporting local-only', async () => {
+ stores({ people: { p1: { id: 'p1', updatedAt: 'T1' } } });
+ peerRoutes({
+ '/api/brain/reconcile/manifest': jsonResponse({ types: {} }),
+ '/api/brain/reconcile/checksum': jsonResponse({ checksum: 'peer-checksum' }),
+ });
+
+ const report = await checkPeerBrainParity(PEER);
+
+ expect(report.available).toBe(true);
+ expect(report.summary).toMatchObject({ total: 1, 'local-only': 1 });
+ });
+
+ it('treats a 200 with an array-shaped types property as fetch-failed', async () => {
+ stores({ people: { p1: { id: 'p1', updatedAt: 'T1' } } });
+ peerRoutes({
+ '/api/brain/reconcile/manifest': jsonResponse({ types: [] }),
+ });
+
+ const report = await checkPeerBrainParity(PEER);
+
+ expect(report.available).toBe(false);
+ expect(report.reason).toBe('fetch-failed');
+ });
+
+ it('treats a malformed snapshot fallback body (missing or array records) as fetch-failed', async () => {
+ stores({ people: { p1: { id: 'p1', updatedAt: 'T1' } } });
+ peerRoutes({
+ '/api/brain/reconcile/snapshot': jsonResponse({ records: [] }),
+ });
+
+ const report = await checkPeerBrainParity(PEER);
+
+ expect(report.available).toBe(false);
+ expect(report.reason).toBe('fetch-failed');
+ });
+
it('ignores malformed peer manifest rows instead of throwing', async () => {
stores({ people: { p1: { id: 'p1', updatedAt: 'T1' } } });
peerRoutes({