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.

Import: Replace wins over another device's older unsent change, so the workouts it replaced stay gone

Duarte Santosa90ccbdb07bc
diff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.js
index 2134a2c90..fd6ad4ffd 100644
--- a/frontend/src/lib/sync-merge.js
+++ b/frontend/src/lib/sync-merge.js
@@ -903,6 +903,51 @@ export function stampRestore(next, others = [], now = Date.now()) {
return next
}
+/**
+ * A backup that replaces the profile ("Replace anyway", or an import with nothing to ask about) is
+ * stamped like a reset of what it replaced, on top of stampRestore: every entry this device or the
+ * server held that the backup lacks is recorded as removed at `now`, and every field of an entry
+ * the backup holds in another version is stamped at `now`, and so is a stamped settings entry
+ * (balance, loading, plates) it sets differently. A device that still had an unsent change from
+ * before the replace used to bring the replaced workouts back with its merge, and its older edit
+ * of an entry won over the backup's version. A change made after the replace (a later stamp) still
+ * wins, and an entry only that device knew of is not named, so it stays. `others` are the copies
+ * the replace knew: this device's and the server's. Mutates and returns `next`.
+ */
+export function stampReplace(next, others = [], now = Date.now()) {
+ if (!next || typeof next !== 'object') return next
+ const known = {}
+ for (const f of Object.keys(DEL_LISTS)) known[f] = others.flatMap(o => list(o?.[f])).filter(x => x != null)
+ stampDeletions(known, next, now)
+ for (const f of ['workouts', 'routines', 'customEx', 'equipProfiles', 'gymCards']) {
+ const versions = new Map()
+ for (const o of others) for (const x of list(o?.[f])) if (x && typeof x === 'object' && x.id != null) versions.set(x.id, [...(versions.get(x.id) || []), x])
+ for (const x of list(next[f])) {
+ if (!x || typeof x !== 'object' || x.id == null || !versions.has(x.id)) continue
+ const fields = isMap(x._f) ? { ...x._f } : {}
+ let moved = false
+ for (const old of versions.get(x.id)) {
+ for (const k of new Set([...Object.keys(old), ...Object.keys(x)])) {
+ if (ENTRY_META.has(k)) continue
+ if ((k in old) !== (k in x) || !sameJSON(old[k], x[k])) { fields[k] = now; moved = true }
+ }
+ }
+ if (!moved) continue
+ x._f = fields
+ delete x._u
+ x._ts = Math.max(Number(x._ts) || 0, now)
+ }
+ }
+ for (const f of ['balanceOverrides', 'loadKind', 'plates']) {
+ if (!isMap(next[f])) continue
+ for (const [k, v] of Object.entries(next[f])) {
+ if (!isMap(v)) continue
+ if (others.some(o => isMap(o?.[f]) && k in o[f] && !sameJSON({ ...o[f][k], _ts: 0 }, { ...v, _ts: 0 }))) v._ts = Math.max(stampOf(v), now)
+ }
+ }
+ return next
+}
+
// ---- One causal time per change ---------------------------------------------------------------
//
// Every stamp above is a wall-clock time, compared between devices. A phone whose clock runs
diff --git a/frontend/src/store/useStore.import-restore.test.jsx b/frontend/src/store/useStore.import-restore.test.jsx
index 68e39b98f..53df4952f 100644
--- a/frontend/src/store/useStore.import-restore.test.jsx
+++ b/frontend/src/store/useStore.import-restore.test.jsx
@@ -67,9 +67,29 @@ describe('a restored backup brings back what it holds, and it stays', () => {
useStore.getState().importBackup(backup)
await useStore.getState().pushState()
expect(ids(doc.workouts)).toEqual(['w1', 'w2', 'w3'])
- // w4, which the replace dropped without a record, is still on B and comes back: kept, not lost
- expect(ids(mergeStates(other, doc).workouts)).toEqual(['w1', 'w2', 'w3', 'w4', 'w5'])
- expect(ids(mergeStates(doc, other).workouts)).toEqual(['w1', 'w2', 'w3', 'w4', 'w5'])
+ // RC review 2026-10-07: w4, which the replace dropped, came back from B's unsent change. The
+ // replace now records what it replaced, like a reset: w4 stays gone, while w5, which only B
+ // knew of, stays.
+ expect(ids(mergeStates(other, doc).workouts)).toEqual(['w1', 'w2', 'w3', 'w5'])
+ expect(ids(mergeStates(doc, other).workouts)).toEqual(['w1', 'w2', 'w3', 'w5'])
+ })
+
+ it('"Replace" wins over another device\'s older unsent edit of an entry it replaces, not over a later one', async () => {
+ // the server's w3 was corrected after the backup was made; B, offline, changed its note too
+ doc.workouts = doc.workouts.map(w => (w.id === 'w3' ? { ...w, note: 'server', _ts: T + 10, _f: { note: T + 10 } } : w))
+ other = change(other, S => { const w = S.workouts.find(x => x.id === 'w3'); w.note = 'B before'; w._ts = Date.now() })
+ await useStore.getState().importConflict(backup)
+ useStore.getState().importBackup(backup)
+ await useStore.getState().pushState()
+ let m = mergeStates(other, doc)
+ expect(m.workouts.find(w => w.id === 'w3').note).toBeUndefined() // the backup's w3, as it was
+ // a change B makes after the replace, and a workout it logs then, still win
+ const later = change(other, S => { S.workouts.find(x => x.id === 'w3').note = 'B after'; S.workouts.find(x => x.id === 'w3')._ts = Date.now() + 60000
+ S.workouts.push(workout('w7', Date.now() + 60000)) }, Date.now() + 60000)
+ for (m of [mergeStates(later, doc), mergeStates(doc, later)]) {
+ expect(m.workouts.find(w => w.id === 'w3').note).toBe('B after')
+ expect(ids(m.workouts)).toEqual(['w1', 'w2', 'w3', 'w5', 'w7'])
+ }
})
it('the backup\'s settings win over an older unsent setting change on another device', async () => {
diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js
index 6baa82b4b..06d60ac6c 100644
--- a/frontend/src/store/useStore.js
+++ b/frontend/src/store/useStore.js
@@ -9,7 +9,7 @@ import { guestAllowed } from '../lib/guest.js'
import { MOBILE, initReminderSync, nativeLoad, nativeSave, onAppActive, readJsonFile, syncReminder, writeAutoBackup, writeJsonFile } from '../lib/mobile.js'
import { refillIfComplete } from '../lib/rotation.js'
import { liftLegacy } from '../lib/sync-legacy.js'
-import { mergeStates, localExtras, stampChange, highestStamp, stampRestore, inUnitOf, keepReset, resetIdsOf, mergeResetIds, entryKey } from '../lib/sync-merge.js'
+import { mergeStates, localExtras, stampChange, highestStamp, stampRestore, stampReplace, inUnitOf, keepReset, resetIdsOf, mergeResetIds, entryKey } from '../lib/sync-merge.js'
import { convertStateUnit } from '../lib/units.js'
import { pendingRefCount, settleMedia, loadPending } from '../lib/media-owed.js'
import { referencedHashes } from '../lib/media-refs.js'
@@ -1406,6 +1406,9 @@ export const useStore = create((set, get) => {
const running = carryActive(cur, next) || next.active || null
if (!mergeWith?.state || !uid) {
stampRestore(next, [cur, server], now)
+ // …and like a reset of what it replaces: an unsent change of another device from before it
+ // no longer brings the replaced workouts back (lib/sync-merge.js stampReplace).
+ stampReplace(next, [cur, server].filter(Boolean), now)
next._ts = now
next.active = running
const at = read?.rev != null ? { rev: read.rev, wid: read.state?._wid } : metaOf().base