From bddabbf93d7bca8b8af138f26230b561cd738d04 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 1 Dec 2026 17:59:29 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Surface=20changes=20rejected=20b?= =?UTF-8?q?y=20the=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The server now answers an invalid change per item instead of failing the whole batch, and the client dropped such a change silently: the round went through and nothing told the user that the edit never reached the server. A rejected change now sets the sync status to ": ", cut to 200 characters, so the sync button shows its warning state. The change is not retried, as before. The message survives the collections that still succeed in the same round, since each of them used to clear the error. The next round without a rejection clears it. --- web/src/lib/db.svelte.ts | 13 ++++++++++-- web/src/lib/db.test.ts | 45 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/web/src/lib/db.svelte.ts b/web/src/lib/db.svelte.ts index f642bb2..5f0916c 100644 --- a/web/src/lib/db.svelte.ts +++ b/web/src/lib/db.svelte.ts @@ -32,7 +32,8 @@ type AnyCollection = Collection; interface PushResult { id: string; collection: string; - status: 'ok' | 'stale' | 'forbidden' | 'readonly'; + status: 'ok' | 'stale' | 'forbidden' | 'readonly' | 'invalid'; + error?: unknown; record?: Record | null; } @@ -151,6 +152,8 @@ const pendingCursor = new Map(); const staleBuffer = new Map(); /** Change records the last push of a collection sent; the pull after it keeps newer local changes. */ const pushedChanges = new Map>(); +/** Set when the server rejected a change of the current round; its message stays until a round succeeds. */ +let rejected = false; /** Collections whose local state the sync is about to touch or is touching, by name and nesting depth. */ const applying = new Map(); @@ -197,7 +200,7 @@ function onSynced(name: string) { pendingCursor.delete(name); } status.lastSync = Date.now(); - status.error = null; + if (!rejected) status.error = null; } class WotraSync extends SyncManager, AnyDoc, string> { @@ -324,6 +327,11 @@ async function doOpenDb(uid: string): Promise { } }); for (const r of res?.results ?? []) { + if (r.status === 'invalid') { + if (!rejected) status.error = `${name}: ${String(r.error ?? 'rejected')}`.slice(0, 200); + rejected = true; + continue; + } if (r.status === 'ok' || !r.record || !isCollection(r.collection)) continue; const list = staleBuffer.get(r.collection) ?? []; list.push(toRecord(r.collection, r.record)); @@ -377,6 +385,7 @@ export function syncNow(): Promise { if (syncing) return syncing; const m = sm; if (!m || !status.online) return Promise.resolve(); + rejected = false; const run: Promise = runBeforeSync() .then(() => m.syncAll()) .catch((err: Error) => { diff --git a/web/src/lib/db.test.ts b/web/src/lib/db.test.ts index 82f9c39..c68bd70 100644 --- a/web/src/lib/db.test.ts +++ b/web/src/lib/db.test.ts @@ -8,11 +8,15 @@ const mocks = vi.hoisted(() => { const server = new Map>>(); const pushes: { collection: string; id: string; data: Record; client_updated_at: number; deleted: boolean }[] = []; const batches: number[] = []; + const reject = { collection: '', error: '' }; + const slow = { name: '' }; let tick = 0; return { server, pushes, batches, + reject, + slow, stamp: () => new Date(Date.UTC(2026, 0, 1) + tick++).toISOString().replace('T', ' ') }; }); @@ -22,7 +26,10 @@ vi.mock('$lib/pb', () => ({ autoCancellation: () => undefined, filter: (tpl: string, params: Record) => tpl.replace(/\{:(\w+)\}/g, (_m, key: string) => JSON.stringify(String(params[key]))), collection: (name: string) => ({ - getFullList: async () => [...(mocks.server.get(name)?.values() ?? [])], + getFullList: async () => { + if (mocks.slow.name === name) await new Promise((resolve) => setTimeout(resolve, 5)); + return [...(mocks.server.get(name)?.values() ?? [])]; + }, subscribe: async () => async () => undefined }), send: async (path: string, options: { method?: string; body?: unknown }) => { @@ -30,6 +37,8 @@ vi.mock('$lib/pb', () => ({ const body = options.body as { changes: (typeof mocks.pushes)[number][] }; mocks.batches.push(body.changes.length); const results = body.changes.map((c) => { + mocks.pushes.push(c); + if (mocks.reject.collection === c.collection) return { id: c.id, collection: c.collection, status: 'invalid', error: mocks.reject.error }; const record = { id: c.id, ...c.data, client_updated_at: c.client_updated_at, rev: 1, deleted: c.deleted, updated: mocks.stamp() }; let store = mocks.server.get(c.collection); if (!store) { @@ -37,7 +46,6 @@ vi.mock('$lib/pb', () => ({ mocks.server.set(c.collection, store); } store.set(c.id, record); - mocks.pushes.push(c); return { id: c.id, collection: c.collection, status: 'ok', record }; }); return { results }; @@ -218,4 +226,37 @@ describe('sync boundary', () => { expect(Math.max(...mocks.batches)).toBeLessThanOrEqual(200); expect(mocks.batches.reduce((sum, size) => sum + size, 0)).toBe(250); }, 20000); + + it('a rejected change is dropped and reported, and a later success of the round keeps the message', async () => { + status.online = true; + mocks.reject.collection = 'settings_history'; + mocks.reject.error = 'hours_per_week must be at most 168'; + mocks.slow.name = 'balance_adjustments'; + try { + const id = await save('settings_history', null, settingsRow); + await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 }); + await syncNow(); + expect(status.error).toBe('settings_history: hours_per_week must be at most 168'); + mocks.reject.collection = ''; + await syncNow(); + expect(status.error).toBeNull(); + expect(mocks.pushes.filter((c) => c.id === id)).toHaveLength(1); + } finally { + mocks.reject.collection = ''; + mocks.slow.name = ''; + } + }); + + it('a long rejection is cut to 200 characters', async () => { + status.online = true; + mocks.reject.collection = 'balance_adjustments'; + mocks.reject.error = 'x'.repeat(300); + try { + await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 }); + await syncNow(); + expect(status.error).toBe(`balance_adjustments: ${'x'.repeat(200 - 'balance_adjustments: '.length)}`); + } finally { + mocks.reject.collection = ''; + } + }); });