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(security): stop brain and memory pulls from looping on a stuck peer cursor

Adam Eivyfc521bb65a2a
diff --git a/server/services/syncOrchestrator.js b/server/services/syncOrchestrator.js
index 47ab85ee9b..5cb952fc7d 100644
--- a/server/services/syncOrchestrator.js
+++ b/server/services/syncOrchestrator.js
@@ -252,8 +252,16 @@ async function syncBrainFromPeer(peer, cursor) {
const result = await brainSync.applyRemoteChanges(data.changes);
totalApplied += result.inserted + result.updated + result.deleted;
+ // A page of changes always advances a well-behaved peer's cursor. One that
+ // doesn't (a bug, or a hostile peer answering hasMore forever) would re-pull
+ // the same window while holding this peer's sync; the catalog loop guards
+ // the same way. Resets are handled before the drain (detectCursorReset).
+ if (!Number.isSafeInteger(data.maxSeq) || data.maxSeq <= brainSeq) {
+ console.warn(`⚠️ Brain sync from ${peer.name}: cursor did not advance past ${brainSeq} — stopping this pull`);
+ break;
+ }
brainSeq = data.maxSeq;
- hasMore = data.hasMore;
+ hasMore = data.hasMore === true;
}
// --- Anti-entropy reconcile (#1077) ---
@@ -324,8 +332,14 @@ async function syncMemoryFromPeer(peer, cursor) {
const result = await memorySync.applyRemoteChanges(data.memories, data.schemaVersion ?? 0);
totalApplied += result.inserted + result.updated;
+ // Same non-advancing-cursor guard as the brain drain above.
+ if (typeof data.maxSequence !== 'string' || !/^\d+$/.test(data.maxSequence)
+ || safeBigInt(data.maxSequence) <= safeBigInt(memorySeq)) {
+ console.warn(`⚠️ Memory sync from ${peer.name}: cursor did not advance past ${memorySeq} — stopping this pull`);
+ break;
+ }
memorySeq = data.maxSequence;
- hasMore = data.hasMore;
+ hasMore = data.hasMore === true;
}
return { memorySeq, totalApplied };
diff --git a/server/services/syncOrchestrator.test.js b/server/services/syncOrchestrator.test.js
index 8a7e20f1ee..0405309882 100644
--- a/server/services/syncOrchestrator.test.js
+++ b/server/services/syncOrchestrator.test.js
@@ -334,6 +334,24 @@ describe('syncOrchestrator', () => {
expect(mockFetch).toHaveBeenCalledTimes(3);
});
+ it('stops draining a peer that reports more without advancing its cursor', async () => {
+ // Every page repeats the same window: unguarded, both loops never end.
+ mockFetch.mockImplementation(async (url) => {
+ if (url.includes('/api/brain/sync')) return { ok: true, json: async () => ({ changes: [{ seq: 1 }], maxSeq: 0, hasMore: true }) };
+ if (url.includes('/api/memory/sync')) return { ok: true, json: async () => ({ memories: [{ id: 'm1' }], maxSequence: '0', hasMore: true }) };
+ return { ok: true, json: async () => ({}) };
+ });
+ applyBrainChanges.mockResolvedValue({ inserted: 0, updated: 1, deleted: 0, skipped: 0 });
+ applyMemoryChanges.mockResolvedValue({ inserted: 0, updated: 1 });
+
+ const result = await syncWithPeer(mockPeer);
+
+ expect(applyBrainChanges).toHaveBeenCalledTimes(1);
+ expect(applyMemoryChanges).toHaveBeenCalledTimes(1);
+ expect(result.brain.brainSeq).toBe(0);
+ expect(result.memory.memorySeq).toBe('0');
+ });
+
it('handles pagination loop with hasMore=true', async () => {
// First brain batch: hasMore=true
mockFetch