Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Sync: a removal and an Undo with the same stamp merge the same in either order

Duarte Santosff050f5f3ba4
diff --git a/api/sync-stamps.js b/api/sync-stamps.js
index c7ed567a7..9bb4f1ac1 100644
--- a/api/sync-stamps.js
+++ b/api/sync-stamps.js
@@ -57,13 +57,19 @@ const PER_KEY = new Set(['week', 'dayPlan', 'exNotes', 'barWeights']);
const ENTRY_META = new Set(['id', '_ts', '_f']);
const ENTRY_LISTS = ['routines', 'customEx', 'equipProfiles', 'gymCards', 'workouts'];
+// Whether removal record `v` replaces `cur`: the later stamp, and on a tie the add-back.
+const laterDel = (v, cur) => {
+ const x = Number(v) || 0, y = Number(cur) || 0;
+ return Math.abs(x) > Math.abs(y) || (Math.abs(x) === Math.abs(y) && x < y);
+};
/** Both records of removals: per entry, the later stamp. */
export function mergeDeletions(a, b) {
const out = {};
for (const f of Object.keys(DEL_LISTS)) {
const x = isMap(a?.[f]) ? a[f] : {}, y = isMap(b?.[f]) ? b[f] : {};
const m = { ...x };
- for (const [k, v] of Object.entries(y)) if (!(k in m) || Math.abs(Number(v) || 0) > Math.abs(Number(m[k]) || 0)) m[k] = v;
+ // On equal stamps the add-back (negative) wins, whichever copy is first (as the app's).
+ for (const [k, v] of Object.entries(y)) if (!(k in m) || laterDel(v, m[k])) m[k] = v;
if (Object.keys(m).length) out[f] = capStamps(m);
}
return Object.keys(out).length ? out : null;
diff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.js
index ef7de93fb..59e8d048b 100644
--- a/frontend/src/lib/sync-merge.js
+++ b/frontend/src/lib/sync-merge.js
@@ -451,13 +451,21 @@ export function stampDeletions(prev, next, now = Date.now()) {
return next
}
+// Whether removal record `v` replaces `cur`: the later stamp, and on a tie the add-back.
+const laterDel = (v, cur) => {
+ const x = Number(v) || 0, y = Number(cur) || 0
+ return Math.abs(x) > Math.abs(y) || (Math.abs(x) === Math.abs(y) && x < y)
+}
/** Both copies' records of removals: per entry, the later stamp. */
export function mergeDeletions(a, b) {
const out = {}
for (const f of Object.keys(DEL_LISTS)) {
const x = isMap(a?.[f]) ? a[f] : {}, y = isMap(b?.[f]) ? b[f] : {}
const m = { ...x }
- for (const [k, v] of Object.entries(y)) if (!(k in m) || Math.abs(Number(v) || 0) > Math.abs(Number(m[k]) || 0)) m[k] = v
+ // On equal stamps the add-back (negative) wins, whichever copy is first: two devices whose
+ // clocks were lifted from the same stamp can record an Undo and a removal at the same time,
+ // and a first-wins tie kept a different record on each side (QA 2026-10-06).
+ for (const [k, v] of Object.entries(y)) if (!(k in m) || laterDel(v, m[k])) m[k] = v
if (Object.keys(m).length) out[f] = capStamps(m)
}
return Object.keys(out).length ? out : null
diff --git a/frontend/src/lib/sync-stamps-parity.test.js b/frontend/src/lib/sync-stamps-parity.test.js
index f90ae9b52..9b2b1f1d7 100644
--- a/frontend/src/lib/sync-stamps-parity.test.js
+++ b/frontend/src/lib/sync-stamps-parity.test.js
@@ -27,6 +27,30 @@ describe('api/sync-stamps.js matches sync-merge.js', () => {
})
})
+describe('a removal and an add-back with the same stamp', () => {
+ // QA round 2 (2026-10-06): two phones lifted their clocks from one highest stamp, so one's Undo
+ // (-H-2) and the other's removal (+H-2) tied, and the first argument won: the device (local
+ // first) and the server (stored first) kept opposite records.
+ const H = 1_800_000_000_000
+ it('merges the same in either order, on both sides: the add-back wins', () => {
+ const a = { routines: { R: -(H + 2) } }, b = { routines: { R: H + 2 } }
+ for (const f of [mergeDeletions, S.mergeDeletions]) {
+ expect(f(a, b)).toEqual({ routines: { R: -(H + 2) } })
+ expect(f(b, a)).toEqual({ routines: { R: -(H + 2) } })
+ }
+ })
+ it('the routine put back with Undo is kept whichever phone merges first', () => {
+ const R = { id: 'R', name: 'Push', ex: [], _ts: H - 10 }
+ const base = { _ts: H, unit: 'kg', workouts: [], routines: [R], week: { 1: ['R'] } }
+ const A = { ...clone(base), routines: [{ ...R, _ts: H + 2 }], deleted: { routines: { R: -(H + 2) } } }
+ const B = { ...clone(base), routines: [], week: {}, deleted: { routines: { R: H + 2 } } }
+ const ab = mergeStates(A, B), ba = mergeStates(B, A)
+ expect(ab.deleted).toEqual(ba.deleted)
+ expect(ab.routines.map(r => r.id)).toEqual(['R'])
+ expect(ba.routines.map(r => r.id)).toEqual(['R'])
+ })
+})
+
describe('what the server stamps holds in the merge', () => {
const T = 1_800_000_000_000
const change = (prev, wall, mut) => { const n = clone(prev); mut(n); n._ts = stampChange(prev, n, wall); return n }