|
| 1 | +const vectorClockMath = require('../utils/vectorClockMath'); |
| 2 | +const SyncConflict = require('../models/SyncConflict'); |
| 3 | + |
| 4 | +/** |
| 5 | + * Consensus Service |
| 6 | + * Issue #705: Orchestrates field-level merging and conflict detection. |
| 7 | + */ |
| 8 | +class ConsensusService { |
| 9 | + /** |
| 10 | + * Resolve incoming state against current database state. |
| 11 | + */ |
| 12 | + async reconcile(currentEntity, incomingData, deviceId, userId) { |
| 13 | + const currentClock = currentEntity.vectorClock?.toObject() || {}; |
| 14 | + const incomingClock = incomingData.vectorClock || {}; |
| 15 | + |
| 16 | + const comparison = vectorClockMath.compare(incomingClock, currentClock); |
| 17 | + |
| 18 | + if (comparison === 1) { |
| 19 | + // Incoming is strictly newer - Fast Forward |
| 20 | + return { action: 'update', data: incomingData, clock: incomingClock }; |
| 21 | + } |
| 22 | + |
| 23 | + if (comparison === 0 || comparison === -1) { |
| 24 | + // Incoming is identical or strictly older - Reject/No-op |
| 25 | + return { action: 'ignore', reason: 'stale_clock' }; |
| 26 | + } |
| 27 | + |
| 28 | + // comparison === null -> Conflict detected |
| 29 | + return await this._handleConflict(currentEntity, incomingData, deviceId, userId); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Perform field-level semantic merge or log conflict for manual resolution. |
| 34 | + */ |
| 35 | + async _handleConflict(currentEntity, incomingData, deviceId, userId) { |
| 36 | + const mergedData = { ...currentEntity.toObject() }; |
| 37 | + const conflicts = []; |
| 38 | + |
| 39 | + // Simple field-level merge logic |
| 40 | + for (const [key, incomingValue] of Object.entries(incomingData)) { |
| 41 | + if (key === 'vectorClock' || key === '_id') continue; |
| 42 | + |
| 43 | + const currentValue = mergedData[key]; |
| 44 | + if (JSON.stringify(currentValue) !== JSON.stringify(incomingValue)) { |
| 45 | + // Semantic check: can we auto-merge? |
| 46 | + if (typeof currentValue === 'number' && typeof incomingValue === 'number') { |
| 47 | + // Example: for some metrics we might add, but for transactions we usually want latest |
| 48 | + // Here we log as conflict for safety |
| 49 | + conflicts.push(key); |
| 50 | + } else { |
| 51 | + conflicts.push(key); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (conflicts.length === 0) { |
| 57 | + // Clocks were concurrent but values were actually the same |
| 58 | + const mergedClock = vectorClockMath.merge(currentEntity.vectorClock.toObject(), incomingData.vectorClock); |
| 59 | + return { action: 'update', data: mergedData, clock: mergedClock }; |
| 60 | + } |
| 61 | + |
| 62 | + // Persistent conflict log |
| 63 | + const conflictRecord = new SyncConflict({ |
| 64 | + entityId: currentEntity._id, |
| 65 | + entityType: currentEntity.constructor.modelName, |
| 66 | + userId, |
| 67 | + baseState: currentEntity.toObject(), |
| 68 | + conflictingStates: [{ |
| 69 | + deviceId, |
| 70 | + state: incomingData, |
| 71 | + vectorClock: incomingData.vectorClock |
| 72 | + }] |
| 73 | + }); |
| 74 | + await conflictRecord.save(); |
| 75 | + |
| 76 | + return { |
| 77 | + action: 'conflict', |
| 78 | + conflictId: conflictRecord._id, |
| 79 | + conflictingFields: conflicts |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +module.exports = new ConsensusService(); |
0 commit comments