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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Sync: an older app back from a dead spot no longer sets back a setting, plan day or routine field changed after its copy
Duarte Santos2aa38c2b683f
diff --git a/api/openapi.yaml b/api/openapi.yamlindex 9ed6fc07a..e72b4b534 100644--- a/api/openapi.yaml+++ b/api/openapi.yaml@@ -1438,7 +1438,10 @@ paths:profile or favourite), every setting and plan day it changed (`queue`,`week.<day>`, …), and every entry it edited (`_ts`, per field `_f`). That keeps aplanner's new week, or a routine it removed, from being undone by a phone that- reconnects later with an older change. An entry the document holds while a+ reconnects later with an older change. A setting, plan day or entry field whose+ stored stamp (`edited`, `_f`) is later than the one the writer sends back keeps the+ stored value: the writer never saw that change, so send back the `edited` and `_f`+ you read. A writer that sends no `edited` at all is not held to it. An entry the document holds while aremoval on record says it was deleted after its last edit is kept and marked asadded back.diff --git a/api/sync-stamps.js b/api/sync-stamps.jsindex 88ac2875e..6f3017b46 100644--- a/api/sync-stamps.js+++ b/api/sync-stamps.js@@ -22,7 +22,13 @@// a reset, which starts the profile over, keeps its own);// 2. for a writer that does not stamp (no `stamped: true` in the body) and wrote over the// revision it read: stamps what it changed against the stored copy, at the server's time —-// entries removed, settings and plan days changed, entries edited;+// entries removed, settings and plan days changed, entries edited. A setting, plan day or+// entry field whose stored stamp is later than the one the writer sent back (it never saw+// that change) keeps the stored value: an older app back from a dead spot no longer sets+// them back. What remains: such a writer's own change of that same field in the meantime+// loses to the stamped one (two changes, neither saw the other), one that sends no record of+// edits at all (it never read a copy from an updated app) still wins as before, and the older+// app shows its own value until it next reads the profile;// 3. for every writer: an entry the document holds while a removal on record says it was// deleted after its last edit is kept, and marked as added back. An updated app never sends// such a document, so this is an older app's merge (or its Coach undo, or a backup it@@ -141,9 +147,17 @@ function stampUnstamped(cur, next, sent, now) {}if (Object.keys(del).length) next.deleted = del;// Settings and plan days changed, whose stamp the writer did not move past the stored one.+ //+ // Unless the stored value was changed, by its own stamp, after the copy this writer worked on:+ // the stamps it sends back are the ones it last read (v1.3.9 keeps the record it does not know),+ // so a stamp of the stored copy later than the writer's own means it never saw that change.+ // Then what it sends is the value from before (a phone back from a dead spot, whose merge kept+ // its whole older copy) or a change made at the same time without seeing the other: the stamped+ // change stays. A writer that sends no record at all cannot be placed and goes through as before.const ed = isMap(next.edited) ? next.edited : {};const mine = isMap(sent) ? sent : {}, theirs = isMap(cur.edited) ? cur.edited : {};const moved = k => (Number(mine[k]) || 0) > (Number(theirs[k]) || 0);+ const stale = k => isMap(sent) && (Number(theirs[k]) || 0) > (Number(mine[k]) || 0);for (const k of new Set([...Object.keys(cur), ...Object.keys(next)])) {if (OWN_MERGE.has(k)) continue;const p = cur[k], n = next[k];@@ -151,20 +165,42 @@ function stampUnstamped(cur, next, sent, now) {const pm = isMap(p) ? p : {}, nm = isMap(n) ? n : {};for (const s of new Set([...Object.keys(pm), ...Object.keys(nm)])) {const sk = `${k}.${s}`;- if (((s in pm) !== (s in nm) || !same(pm[s], nm[s])) && !moved(sk)) ed[sk] = now;+ if ((s in pm) === (s in nm) && same(pm[s], nm[s])) continue;+ if (stale(sk)) {+ const into = isMap(next[k]) ? next[k] : (next[k] = {});+ if (s in pm) into[s] = clone(pm[s]); else delete into[s];+ } else if (!moved(sk)) ed[sk] = now;}- } else if (n !== undefined && !same(p, n) && !moved(k)) ed[k] = now; // absent: a field the writer does not know+ } else if (n !== undefined && !same(p, n)) { // absent: a field the writer does not know+ if (stale(k)) { if (k in cur) next[k] = clone(p); else delete next[k]; }+ else if (!moved(k)) ed[k] = now;+ }}if (Object.keys(ed).length) next.edited = ed;- // Entries edited without a stamp of their own: a routine, a custom exercise, a profile, a card,- // a workout whose content changed while its `_ts` did not move.+ // Entries edited without stamps of their own: a routine, a custom exercise, a profile, a card,+ // a workout whose content changed. The same rule per field: one the stored entry changed (`_f`)+ // after the version this writer worked on (its `_f`, as last read) keeps the stored value. An+ // older app keeps the version of an entry edited last as a whole, so its edit of the name used to+ // bring back the sets as they were before another device changed them.for (const f of ENTRY_LISTS) {const before = new Map(list(cur[f]).filter(x => x && x.id != null).map(x => [x.id, x]));for (const x of list(next[f])) {if (!x || typeof x !== 'object' || x.id == null) continue;const old = before.get(x.id);if (!old) { if (f !== 'workouts' && x._ts == null) x._ts = now; continue; }- if ((Number(x._ts) || 0) > (Number(old._ts) || 0) || sameEntry(old, x)) continue;+ if (sameEntry(old, x)) continue;+ if (isMap(sent) || isMap(x._f)) {+ const base = isMap(x._f) ? x._f : {}, of = isMap(old._f) ? old._f : {};+ for (const k of new Set([...Object.keys(old), ...Object.keys(x)])) {+ if (ENTRY_META.has(k) || !((Number(of[k]) || 0) > (Number(base[k]) || 0))) continue;+ if ((k in old) === (k in x) && same(old[k], x[k])) continue;+ if (k in old) x[k] = clone(old[k]); else delete x[k];+ }+ const fx = { ...base };+ for (const [k, v] of Object.entries(of)) if (!((Number(fx[k]) || 0) >= (Number(v) || 0))) fx[k] = v;+ if (Object.keys(fx).length) x._f = fx; else delete x._f;+ if (sameEntry(old, x)) { x._ts = Math.max(Number(x._ts) || 0, Number(old._ts) || 0); continue; }+ }stampEntry(old, x, now);}}diff --git a/api/test/sync-stamps.test.js b/api/test/sync-stamps.test.jsindex eeabb378e..9b59159c4 100644--- a/api/test/sync-stamps.test.js+++ b/api/test/sync-stamps.test.js@@ -180,3 +180,46 @@ test('a reset from an older app is not refilled from the profile it wiped', () =stampPut(cur, next, { overRead: true, stamped: false, now: NOW });assert.equal('accentCustom' in next, false);});++// RC review 2026-10-07: a v1.3.9 phone back from a dead spot got the 409, its merge kept its+// whole older copy (newer `_ts`), and its push set the rest timer, the plan day and the routine's+// sets back to what they were before an updated phone changed them.+test('an older app back from offline does not set back settings, plan days or entry fields changed after its copy', () => {+ const cur = stored();+ cur.restSec = 60; cur.week = { 1: 'r2' }; cur.edited = { restSec: NOW - 1000, 'week.1': NOW - 1000, theme: NOW - 9000 };+ cur.theme = 'dark';+ cur.routines[0] = { id: 'r1', name: 'Push', ex: [{ id: 'bench', sets: 5 }], _ts: NOW - 1000, _f: { ex: NOW - 1000 } };+ const old = clone(cur);+ old.edited = { restSec: NOW - 8000, theme: NOW - 9000 }; // the record it read before going offline+ old.restSec = 90; old.week = { 1: 'r1' }; // its older copy+ old.theme = 'light'; // its own change, of a setting nobody changed since+ old.routines[0] = { id: 'r1', name: 'Push A', ex: [{ id: 'bench', sets: 3 }], _ts: NOW - 500 }; // renamed offline, older sets+ old._ts = NOW - 500;+ stampPut(cur, old, { overRead: true, stamped: false, now: NOW });+ assert.equal(old.restSec, 60);+ assert.deepEqual(old.week, { 1: 'r2' });+ assert.equal(old.theme, 'light');+ assert.ok(old.edited.theme >= NOW);+ assert.equal(old.routines[0].name, 'Push A');+ assert.deepEqual(old.routines[0].ex, [{ id: 'bench', sets: 5 }]);+ assert.ok(old.routines[0]._f.name >= NOW);+ assert.equal(old.routines[0]._f.ex, NOW - 1000);+});++test('an older app that read the latest change still changes it, and one that sends no record goes through', () => {+ const cur = stored();+ cur.edited = { restSec: NOW - 1000 };+ const seen = clone(cur);+ seen.restSec = 45;+ stampPut(cur, seen, { overRead: true, stamped: false, now: NOW });+ assert.equal(seen.restSec, 45);+ assert.ok(seen.edited.restSec >= NOW);+ const blind = clone(cur); delete blind.edited;+ blind.restSec = 30;+ stampPut(cur, blind, { overRead: true, stamped: false, now: NOW });+ assert.equal(blind.restSec, 30);+ // a replace (no baseRev: an older app's backup import) is not held against the record either+ const replace = clone(cur); replace.edited = {}; replace.restSec = 120;+ stampPut(cur, replace, { overRead: false, stamped: false, now: NOW });+ assert.equal(replace.restSec, 120);+});diff --git a/frontend/src/lib/sync-stamps-parity.test.js b/frontend/src/lib/sync-stamps-parity.test.jsindex 099bdb0c5..b6ae92e36 100644--- a/frontend/src/lib/sync-stamps-parity.test.js+++ b/frontend/src/lib/sync-stamps-parity.test.js@@ -110,4 +110,18 @@ describe('what the server stamps holds in the merge', () => {expect(m.restSec).toBe(75)}})++ // RC review 2026-10-07: an older app back from offline set the rest timer back on every device.+ it('an older app\'s older copy after a dead spot does not undo a setting the updated phone changed since', () => {+ const cur = change(doc0(), T + 1000, S2 => { S2.restSec = 60 }) // the updated phone, pushed+ const old = clone(doc0()); old.restSec = 90; old._ts = T + 9000 // v1.3.9 merged its older copy+ old.workouts.push({ id: 'w9', d: '2026-10-07', start: T + 8000, end: T + 9000 })+ S.stampPut(cur, old, { overRead: true, now: T + 9000 })+ expect(old.restSec).toBe(60)+ const later = change(cur, T + 2000, S2 => { S2.queue = { label: 'US W3' } }) // the phone, offline meanwhile+ for (const m of [mergeStates(later, old), mergeStates(old, later)]) {+ expect(m.restSec).toBe(60)+ expect(m.workouts.map(w => w.id).sort()).toEqual(['w1', 'w9'])+ }+ })})diff --git a/website/api.html b/website/api.htmlindex 3bd6510b5..d107a9199 100644--- a/website/api.html+++ b/website/api.html@@ -1485,7 +1485,7 @@ main.api-main .lead { color: var(--fg-2); max-width: 58ch; margin: 14px 0 0; fon<div class="ep-body"><p>Replaces the stored state wholesale (atomic write; there is no merge on the server). The <code>active</code> field — an in-progress workout — is stripped before saving: a running session belongs to the device running it. Body limit 5 MiB.</p><p>With <code>baseRev</code> the write is conditional: it goes through only when <code>baseRev</code> equals the document's current revision, and answers 409 with the current document otherwise — the client merges the two and pushes again with the revision it was given. Without <code>baseRev</code> the write is unconditional (clients from before revisions, and deliberate replaces such as a backup import). The server stamps the new revision into the document as <code>_rev</code>; a <code>_rev</code> sent by the client is ignored.</p>-<p>The records of removals (<code>deleted</code>) and of edits (<code>edited</code>) never shrink: the stored ones are joined into the write (a reset keeps its own). A writer that does not stamp its own changes (no <code>stamped: true</code>; an API script, an older app) and sends the current <code>baseRev</code> gets them stamped by the server, at the server's time: every entry it removed (a workout, routine, custom exercise, weigh-in, card, profile or favourite), every setting and plan day it changed (<code>queue</code>, <code>week.<day></code>, …), and every entry it edited (<code>_ts</code>, per field <code>_f</code>). That keeps a planner's new week, or a routine it removed, from being undone by a phone that reconnects later with an older change. An entry the document holds while a removal on record says it was deleted after its last edit is kept and marked as added back.</p>+<p>The records of removals (<code>deleted</code>) and of edits (<code>edited</code>) never shrink: the stored ones are joined into the write (a reset keeps its own). A writer that does not stamp its own changes (no <code>stamped: true</code>; an API script, an older app) and sends the current <code>baseRev</code> gets them stamped by the server, at the server's time: every entry it removed (a workout, routine, custom exercise, weigh-in, card, profile or favourite), every setting and plan day it changed (<code>queue</code>, <code>week.<day></code>, …), and every entry it edited (<code>_ts</code>, per field <code>_f</code>). That keeps a planner's new week, or a routine it removed, from being undone by a phone that reconnects later with an older change. A setting, plan day or entry field whose stored stamp (<code>edited</code>, <code>_f</code>) is later than the one the writer sends back keeps the stored value: the writer never saw that change, so send back the <code>edited</code> and <code>_f</code> you read. A writer that sends no <code>edited</code> at all is not held to it. An entry the document holds while a removal on record says it was deleted after its last edit is kept and marked as added back.</p><p>Such a writer also cannot delete what it does not know: a field of the stored document, of a stored routine, workout, custom exercise, card or profile, of a routine's exercise, or of a logged exercise or set that its copy leaves out comes back from the stored copy (an older app rebuilds an exercise from the fields it knows, which dropped pyramid sets made on an updated device). Only the fields v1.3.9 itself removes (a superset link, a note, a cleared set value, …) can be removed this way. Send <code>stamped: true</code> to remove anything else.</p><p>Every accepted write also starts the grace period of each stored file (tag <code>media</code>) the new state no longer references, and stops it for each one it references again. That bookkeeping never changes the answer.</p><p class="ep-auth"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="4" y="11" width="16" height="10" rx="2.5"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg><span>Requires a session — the cookie set at sign-in, or <code>Authorization: Bearer <token></code> from mobile-app pairing.</span></p>