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 reads back what the server kept for it, and keeps pyramids when a routine has an exercise twice
Duarte Santosf6b6391c39ce
diff --git a/api/openapi.yaml b/api/openapi.yamlindex e72b4b534..c5f80c5d0 100644--- a/api/openapi.yaml+++ b/api/openapi.yaml@@ -1496,7 +1496,11 @@ paths:description: Echo of the pushed state's `_ts`, if present.rev:type: integer- description: The revision the write produced.+ description: >-+ The revision the write produced. For a writer that does not stamp, when the+ server put back something it left out or set back, the document is stored one+ revision further, so the writer's next check of the revision reads it again.+ `wid` is the stored document's either way.wid:type: stringdescription: The write id (`_wid`) of the stored document.diff --git a/api/server.js b/api/server.jsindex b7abc1663..65faef346 100644--- a/api/server.js+++ b/api/server.js@@ -2170,9 +2170,16 @@ const routes = {// changes (an older app, an API script) changed is stamped here, so it is neither wiped nor// undone by the next device that merges (sync-stamps.js).// A document nested too deep to compare is the bad request the stringify below refuses too.- try { stampPut(cur, body.state, { overRead: body.baseRev != null && body.baseRev === curRev, stamped: body.stamped === true }); }+ const report = {};+ try { stampPut(cur, body.state, { overRead: body.baseRev != null && body.baseRev === curRev, stamped: body.stamped === true, report }); }catch (e) { if (e instanceof RangeError) return json(res, 400, { error: 'invalid state' }); throw e; }body.state._rev = curRev + 1; // server-owned; whatever the client sent is ignored+ // A writer that does not stamp (an older app) takes the revision it is told for the document it+ // sent, and only reads the profile again once the revision moves. When the server put back+ // something it left out or set back (sync-stamps.js), the document stored is not that one: it+ // goes in one revision further, so the writer's next check of the revision (every half minute)+ // finds it moved and reads it, and its next push over the one it was told is a conflict to merge.+ if (report.changed) body.state._rev = curRev + 2;body.state._wids = [...(Array.isArray(cur?._wids) ? cur._wids : []), ...(cur?._wid ? [cur._wid] : [])].filter(x => typeof x === 'string').slice(-WID_KEEP);body.state._wid = crypto.randomBytes(8).toString('hex');@@ -2195,7 +2202,7 @@ const routes = {if (MEDIA_ON) {try { MEDIA.noteState(user.id, body.state); } catch (e) { console.error('media noteState', e); }}- json(res, 200, { ok: true, ts: body.state._ts || null, rev: body.state._rev, wid: body.state._wid });+ json(res, 200, { ok: true, ts: body.state._ts || null, rev: curRev + 1, wid: body.state._wid });},'GET /api/push/public-key': async (req, res) => json(res, 200, { key: vapid.publicKey }),diff --git a/api/sync-stamps.js b/api/sync-stamps.jsindex e9b421fd1..e51feaa65 100644--- a/api/sync-stamps.js+++ b/api/sync-stamps.js@@ -297,9 +297,11 @@ function keepKeys(old, x, place) {for (const [o, n] of pairItems(a, b)) keepKeys(o, n, `${place}.${f}`);}}-// Which item of `a` each item of `b` is: by `id` when every id is there and once per list, by-// position when the lists are as long as each other and no item moved to another id. Anything-// else (a set taken out of the middle, a reorder of repeats) is not guessed at.+// Which item of `a` each item of `b` is: by `id` when every id is there and once per list; with+// ids that repeat (the same exercise twice in a routine), along the longest run of ids both lists+// share in order, so an exercise added, taken out or moved leaves the others paired; without ids+// (the sets of an exercise), by position only when the lists are as long as each other. Anything+// else (a set taken out of the middle) is not guessed at.function pairItems(a, b) {const ids = xs => xs.map(x => (isMap(x) ? x.id : undefined));const ia = ids(a), ib = ids(b);@@ -308,6 +310,17 @@ function pairItems(a, b) {const by = new Map(a.map(x => [x.id, x]));return b.filter(x => by.has(x.id)).map(x => [by.get(x.id), x]);}+ if (ia.every(k => k != null) && ib.every(k => k != null) && a.length * b.length <= 40000) {+ const L = Array.from({ length: a.length + 1 }, () => new Array(b.length + 1).fill(0));+ for (let i = a.length - 1; i >= 0; i--) {+ for (let j = b.length - 1; j >= 0; j--) L[i][j] = same(ia[i], ib[j]) ? L[i + 1][j + 1] + 1 : Math.max(L[i + 1][j], L[i][j + 1]);+ }+ const out = [];+ for (let i = 0, j = 0; i < a.length && j < b.length;) {+ if (same(ia[i], ib[j])) { out.push([a[i], b[j]]); i++; j++; } else if (L[i + 1][j] >= L[i][j + 1]) i++; else j++;+ }+ return out;+ }if (a.length !== b.length || ia.some((k, i) => k !== ib[i])) return [];return b.map((x, i) => [a[i], x]);}@@ -342,12 +355,20 @@ function keepHeld(next, now) {}/**- * PUT /api/data's stamping (see the top of this file). `cur` is the stored document (or null),+ * PUT /api/data's stamping (see the top of this file). With `report`, `report.changed` says whether+ * the document stored differs from what a writer that does not stamp sent, beyond stamps: a field+ * it did not know or a newer value was put back (server.js then makes sure it reads the result).+ * `cur` is the stored document (or null),* `next` the one being written (mutated in place), `opts.overRead` true when the writer sent the* revision it read and it is the current one, `opts.stamped` true for a writer that stamps its own* changes (the app), `opts.now` the server's clock.*/-export function stampPut(cur, next, { overRead = false, stamped = false, now = Date.now() } = {}) {+// What a document says, without its stamps and the server's bookkeeping: whether the server put+// back anything of what a writer sent (stampPut's `report`).+const BOOKKEEPING = new Set(['_ts', '_f', '_u', '_rev', '_wid', '_wids', 'edited', 'deleted', 'undone']);+const content = S => JSON.stringify(S, (k, v) => (BOOKKEEPING.has(k) ? undefined : v));++export function stampPut(cur, next, { overRead = false, stamped = false, now = Date.now(), report = null } = {}) {if (!next || typeof next !== 'object') return next;const t = Math.max(Number(now) || 0, highestStamp(cur) + 1, highestStamp(next) + 1);const reset = (Number(next.resetAt) || 0) > (Number(cur?.resetAt) || 0);@@ -360,8 +381,10 @@ export function stampPut(cur, next, { overRead = false, stamped = false, now = D// them stamped as one it changed (QA 2026-10-07), and such a stamp is dropped here.if (e) { delete e._wid; delete e._wids; }if (e && Object.keys(e).length) next.edited = e; else delete next.edited;+ const before = !stamped && report ? content(next) : null;if (!stamped) keepUnknown(cur, next);if (!stamped && overRead) stampUnstamped(cur, next, sent, t);+ if (before != null && content(next) !== before) report.changed = true;}keepHeld(next, t);return next;diff --git a/api/test/server-bad-input.test.js b/api/test/server-bad-input.test.jsindex b1b73a467..282b67b0a 100644--- a/api/test/server-bad-input.test.js+++ b/api/test/server-bad-input.test.js@@ -149,7 +149,7 @@ test('PUT /api/data: an array is not a document, and null entries never reach th// Null and other non-object entries are dropped: every server-side reader of the document// (reminder tick, admin drill-down) dereferences the entries, and a 400 would strand a client// whose own copy is already malformed.- r = await put({ state: { workouts: [null, { id: 'w2', d: '2026-09-02' }, 7, 'x', [], { id: 'w3', d: '2026-09-03' }], routines: [null, { id: 'r1', name: 'A', ex: [] }] }, baseRev: 1 });+ r = await put({ state: { workouts: [null, { id: 'w2', d: '2026-09-02' }, 7, 'x', [], { id: 'w3', d: '2026-09-03' }], routines: [null, { id: 'r1', name: 'A', ex: [] }], unit: 'kg' }, baseRev: 1 });assert.equal(r.status, 200);assert.equal(r.body.rev, 2);assert.deepEqual(onDisk().workouts.map(w => w.id), ['w2', 'w3']);@@ -160,7 +160,12 @@ test('PUT /api/data: an array is not a document, and null entries never reach thr = await put({ state: { unit: 'kg' }, baseRev: 2 });assert.equal(r.status, 200);assert.deepEqual(onDisk().workouts.map(w => w.id), ['w2', 'w3']);+ // What it was told is not what is stored, so the revision moves on past the one it was told.+ assert.equal(r.body.rev, 3);+ assert.equal(onDisk()._rev, 4);r = await put({ state: { unit: 'kg' }, baseRev: 3, stamped: true });+ assert.equal(r.status, 409);+ r = await put({ state: { unit: 'kg' }, baseRev: 4, stamped: true });assert.equal(r.status, 200);assert.equal('workouts' in onDisk(), false);assert.equal(h.stackFrames(), 0, `stack traces in the log:\n${h.log}`);diff --git a/api/test/server-data-rev.test.js b/api/test/server-data-rev.test.jsindex c702da6bd..21f31887c 100644--- a/api/test/server-data-rev.test.js+++ b/api/test/server-data-rev.test.js@@ -132,6 +132,6 @@ test('PUT /api/data refuses an empty object, which would wipe the profile and keassert.deepEqual(onDisk().workouts.map(w => w.id), ['w1'], 'the profile is still there');// …and a document that carries one real key alongside them is a profile, and goes through.- assert.equal((await put({ state: { _rev: 99, _ts: 1, routines: [] }, baseRev: 1 })).status, 200);+ assert.equal((await put({ state: { _rev: 99, _ts: 1, routines: [] }, baseRev: 1, stamped: true })).status, 200);assert.equal(await rev(), 2);});diff --git a/api/test/server-state-read-cost.test.js b/api/test/server-state-read-cost.test.jsindex 39d94e70f..fdb6c71c8 100644--- a/api/test/server-state-read-cost.test.js+++ b/api/test/server-state-read-cost.test.js@@ -151,7 +151,7 @@ test('the poll really is reading through the stat cache', async t => {// And a PUT evicts, so the poll is back in step with the file immediately.const put = await fetch(`${h.api}/api/data`, {- method: 'PUT', headers, body: JSON.stringify({ state: { _ts: 1, workouts: [], routines: [] } })+ method: 'PUT', headers, body: JSON.stringify({ state: { _ts: 1, workouts: [], routines: [] }, stamped: true })}).then(r => r.json());assert.equal(await rev(), put.rev, 'a write through the app is on the very next poll');});diff --git a/api/test/sync-stamps.test.js b/api/test/sync-stamps.test.jsindex f78c0e377..e90b7f926 100644--- a/api/test/sync-stamps.test.js+++ b/api/test/sync-stamps.test.js@@ -238,3 +238,43 @@ test('an older app\'s push never gets the write ids stamped as settings, and anassert.equal('_wid' in old.edited, false);assert.equal('_wids' in old.edited, false);});++// An older app takes the revision it is told for the copy it sent and reads the profile again only+// once the revision moves (v1.3.9 pullState). When the server put back what it left out, the+// stored document goes one revision further, so that app's next check reads it.+test('PUT /api/data: a document the server corrected for an older app is one revision further than it was told', async t => {+ const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gym-stamps-'));+ fs.writeFileSync(path.join(dataDir, 'secret'), SECRET, { mode: 0o600 });+ fs.writeFileSync(path.join(dataDir, 'db.json'), JSON.stringify({ users: [{ id: 'u1', name: 'One', created: new Date().toISOString() }], creds: [], subs: [], invites: [] }));+ const child = spawn(process.execPath, ['server.js'], { cwd: API, stdio: ['ignore', 'pipe', 'pipe'],+ env: { ...process.env, PORT: '0', DATA_DIR: dataDir, ORIGIN: 'http://localhost:8080', RP_ID: 'localhost' } });+ let log = '';+ child.stdout.on('data', d => log += d); child.stderr.on('data', d => log += d);+ t.after(() => { child.kill('SIGKILL'); fs.rmSync(dataDir, { recursive: true, force: true }); });+ const base = `http://127.0.0.1:${await boundPort(child, () => log)}`;+ const headers = { Cookie: `gymsid=${mintSession('u1')}`, 'Content-Type': 'application/json' };+ const put = async body => { const r = await fetch(`${base}/api/data`, { method: 'PUT', headers, body: JSON.stringify(body) }); return { status: r.status, body: await r.json() }; };+ const rev = async () => (await (await fetch(`${base}/api/data/rev`, { headers })).json()).rev;+ const pyr = { id: 'r1', name: 'P', ex: [{ id: 'sq', sets: 4, pyramid: [12, 10, 8, 6] }], _ts: 5 };+ let r = await put({ state: { _ts: 1, workouts: [], routines: [pyr] }, stamped: true });+ assert.equal(r.body.rev, 1);+ // an older app saves the routine without the pyramid it does not know+ r = await put({ state: { _ts: 2, workouts: [], routines: [{ id: 'r1', name: 'P', ex: [{ id: 'sq', sets: 4 }], _ts: 6 }] }, baseRev: 1 });+ assert.equal(r.status, 200);+ assert.equal(r.body.rev, 2);+ assert.equal(await rev(), 3);+ // the same old app with nothing to put back: one revision, as always+ r = await put({ state: { _ts: 3, workouts: [], routines: [{ id: 'r1', name: 'P2', ex: [{ id: 'sq', sets: 4, pyramid: [12, 10, 8, 6] }], _ts: 7 }] }, baseRev: 3 });+ assert.equal(r.body.rev, 4);+ assert.equal(await rev(), 4);+});++test('an older app that adds an exercise to a routine with one of them twice keeps the pyramids of the others', () => {+ const cur = stored();+ cur.routines[0].ex = [{ id: 'sq', sets: 4, pyramid: [12, 10, 8, 6] }, { id: 'bench', sets: 3 }, { id: 'sq', sets: 3, pyramid: [5, 5, 3] }];+ const next = clone(cur);+ delete next.edited;+ next.routines[0].ex = [{ id: 'sq', sets: 4 }, { id: 'row', sets: 3 }, { id: 'bench', sets: 3 }, { id: 'sq', sets: 3 }, { id: 'curl', sets: 2 }];+ stampPut(cur, next, { overRead: true, stamped: false, now: NOW });+ assert.deepEqual(next.routines[0].ex.map(e => e.pyramid || null), [[12, 10, 8, 6], null, null, [5, 5, 3], null]);+});