fix(sync): sync on write with a request-budget scheduler and a 30 s interval #184

Closed
maximus wants to merge 4 commits from issue-178-sync-scheduler into issue-177-ws-notify

4 commits

Author SHA1 Message Date
le king fu
f8ee31066c Merge branch 'issue-177-ws-notify' into issue-178-sync-scheduler 2026-11-03 20:41:37 -05:00
le king fu
dc479e3618 test(sync): pin the two-place rule of the scheduler
A single free place with a queued backlog starts no run until a second
place frees up; the condition alone was not pinned by the latency test.

Refs #178

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 20:02:20 -05:00
le king fu
589c01955a fix(sync): start a run only with room for one POST and the GET
Review of PR #184: when the budget freed up one place at a time (requests
spread by network latency), every place went to a pull-only run with
pushBatches = 0, so a queued backlog was never sent while the device spent
6 GETs a minute. The scheduler now waits for two free places.

Tests: a fake server with 300 ms per request (9-batch queue empties within
4 min, no run without a POST place), the 600 s Retry-After bound, and source
guards for the `more` signal of pushChanges and the budget of initialMerge.

Refs #178

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 20:01:42 -05:00
le king fu
7265c6cad4 fix(sync): sync on write with a request-budget scheduler and a 30 s interval
Every sync now goes through one scheduler (src/services/syncSchedule.ts,
pure and import-free): one sync at a time, a request during a sync gives a
single relaunch, a sliding budget of 6 real HTTP requests per 60 s (each
POST batch and each GET counted, initialMerge and initialReset included),
pushChanges sends the batches the budget allows (MAX_PUSH_BATCHES becomes a
ceiling) and the rest of the queue follows once the window frees, a 429
suspends the scheduler until Retry-After, and no sync starts during
initialMerge / initialReset.

The repositories call requestSync() (src/services/syncTrigger.ts, 2 s
debounce, single subscriber registered by _layout.tsx) after each outbox
write and after the Inbox repair transaction; outbox.ts and syncClient.ts
never do. _layout.tsx syncs on launch, every 30 s in the foreground, on
return to the foreground and on write through scheduleSync(); the Settings
button too. REVIEW.md and scripts/review-levels.py list the two new files
in the Sync row.

test: reproduces #178 (tests/syncSchedule.test.mjs, smoke guards)

Refs #178
Refs #167

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 20:54:33 -05:00