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

address review: LWW timestamp check on remote deletes, fix compaction to include unsynced peers

Adam Eivy12884d71a6c2
diff --git a/server/services/brainStorage.js b/server/services/brainStorage.js
index 56704441a9..71b562f6db 100644
--- a/server/services/brainStorage.js
+++ b/server/services/brainStorage.js
@@ -255,9 +255,10 @@ export async function remove(type, id) {
}
const originInstanceId = data.records[id]?.originInstanceId ?? 'unknown';
+ const deleteRecord = { updatedAt: now() };
delete data.records[id];
await saveJsonStore(type, data);
- await brainSyncLog.appendChange('delete', type, id, null, originInstanceId);
+ await brainSyncLog.appendChange('delete', type, id, deleteRecord, originInstanceId);
console.log(`🧠 Deleted ${type} record: ${id}`);
return true;
@@ -589,6 +590,10 @@ export async function applyRemoteRecord(type, id, record, op) {
if (op === 'delete') {
if (!data.records[id]) return { applied: false, reason: 'not_found' };
+ // LWW: only delete if local record isn't newer than the remote delete
+ if (record?.updatedAt && data.records[id].updatedAt > record.updatedAt) {
+ return { applied: false, reason: 'local_newer' };
+ }
delete data.records[id];
} else {
const existing = data.records[id];
diff --git a/server/services/syncOrchestrator.js b/server/services/syncOrchestrator.js
index b451f26788..4238f48da0 100644
--- a/server/services/syncOrchestrator.js
+++ b/server/services/syncOrchestrator.js
@@ -160,7 +160,7 @@ export async function syncAllPeers() {
// Compact sync log below the minimum peer cursor to bound log growth
const cursors = await loadCursors();
- const seqs = Object.values(cursors).map(c => c.brainSeq ?? 0).filter(s => s > 0);
+ const seqs = Object.values(cursors).map(c => c.brainSeq ?? 0);
if (seqs.length > 0) {
const minSeq = Math.min(...seqs);
await brainSyncLog.compactLog(minSeq);