Skip to content

fix: walk every sync chunk when polling for changes - #95

Merged
jack-arturo merged 1 commit into
mainfrom
fix/polling-multi-chunk-walk
Nov 23, 2026
Merged

jack-arturo merged 1 commit into
mainfrom
fix/polling-multi-chunk-walk

Conversation

@jack-arturo

Copy link
Copy Markdown
Member

Closes #61.

The bug

checkForPollingChanges read one 100-entry sync chunk and then returned
nextUpdateCount: currentUpdateCount — the account-wide head from a
getSyncState taken before the read. src/index.ts commits that as the polling
cursor, so every change past the first 100 in an interval was skipped
permanently. For a webhook consumer that is a notification that never fires.

Filed in July as a follow-up to #60, deferred once so the 2.0.0 release wasn't
held up further, and still present on main.

The fix

Walk the chunks: resume from each chunk's chunkHighUSN, and stop when it
reaches that chunk's own updateCount.

Using the chunk's head rather than the earlier getSyncState value matters
twice. It is self-consistent with chunkHighUSN, and because the service
advances chunkHighUSN across the whole scanned range — including objects this
filter excludes, resources among them — a quiet account still costs exactly
one call
. Looping on the getSyncState value instead would add an empty
second call to most ordinary polls: double the quota on the common path, not
just on backlogs. There's a test pinning that.

nextUpdateCount is now the USN the walk actually reached, never the account
head. That turns the iteration cap from a data-loss boundary into a pacing
bound — a burst larger than MAX_POLL_CHUNK_ITERATIONS (50) chunks drains over
the following intervals rather than being dropped. So this needs no synthetic
backlog_truncated change type, and webhook consumers see no contract change.

The cap is deliberately well above note-cache's MAX_CHUNK_ITERATIONS = 10.
That one's cap-exceeded branch calls clear(), which is safe precisely because
it is a cache — a dropped entry only forces a re-fetch. Here it would be wrong.

Two failure modes handled explicitly

A failure partway through keeps its progress. The likeliest failure is a
rate_limited on chunk 2 from the shared queue added in #86. changes and
cursor live outside the try, and the catch returns both along with the
error. Resetting to lastUpdateCount would re-walk the same chunks every poll,
so a backlog bigger than one quota window could never drain — and would trip
index.ts's five-strike limit and stop polling altogether.

A chunk that doesn't advance the cursor returns an error instead of looping
or skipping. The return happens before that chunk is appended and leaves the
cursor where it was, so its changes are deferred to the next poll — not lost,
not double-sent. Hitting the cap, by contrast, is not an error: that would
make a large import stop polling after five intervals.

Tests

Ten added, and the negative control matters more than the pass:

Test Fails on main?
walks every chunk when more than one chunk is pending yes
keeps the chunks already read when the walk fails partway yes
advances only as far as it read when the backlog outruns the cap yes
stops with an error when a chunk does not advance yes
stops after one chunk when that chunk already reaches the head regression guard
makes exactly one call when only excluded objects changed regression guard
treats a chunk with no chunkHighUSN as the end of the walk regression guard
keeps a cursor that raced past the pre-walk sync state regression guard
falls back to the sync-state head when a chunk omits updateCount regression guard
errors when a chunk reports a high USN below the cursor regression guard

Reverting src/polling.ts alone and re-running gave 4 failed / 6 passed on the
first six, so those four genuinely pin the bug rather than merely describing it.
The last four closed gaps the local review named — in particular the
excluded-only chunk, which is the case that would have exposed a doubled call on
the common path. The first existing test's mock gained
chunkHighUSN/updateCount, since real non-empty chunks carry both.

Full suite: 449 tests, 38 suites, green. Lint and build clean.

Risk

Low. Polling runs only when EVERNOTE_POLLING_ENABLED is exactly "true",
which is off by default, and the path only reads sync state and posts webhooks —
it never writes to Evernote. The one real behavioural change for anyone who does
poll is call volume: a backlog now costs up to 50 chunk calls against the
per-token hourly quota instead of 1, bounded by the cap and paced through #86's
shared queue.

chunkHighUSN needed no new plumbing — evernote-api.ts already returns the
raw SDK chunk, and note-cache.ts already declares the field. This only widens
polling.ts's local interface.


Implementation harness: Claude Code (Opus 5). Local pre-PR review: Codex CLI
gpt-6.1-sol / medium.

🤖 Generated with Claude Code

Closes #61.

checkForPollingChanges read a single 100-entry sync chunk and then set
nextUpdateCount to the account-wide head regardless of how far that chunk
actually reached. src/index.ts commits that as the new cursor, so every change
past the first 100 in an interval was skipped permanently — a webhook that
never fires.

Walk the chunks instead, resuming from each chunk's chunkHighUSN and ending
when it reaches that chunk's own updateCount. Using the chunk's head rather
than the earlier getSyncState value matters twice: it is consistent with
chunkHighUSN, and because the service advances chunkHighUSN across the whole
scanned range — including objects this filter excludes, such as resources — a
quiet account still costs exactly one call instead of an extra empty one on
every ordinary poll.

nextUpdateCount is now the USN the walk actually reached, never the account
head. That makes the cap a pacing bound rather than a data-loss boundary: a
burst larger than MAX_POLL_CHUNK_ITERATIONS (50) chunks drains over the
following intervals instead of being dropped, so no synthetic "backlog
truncated" change type is needed and webhook consumers see no contract change.
The cap is deliberately well above note-cache's 10, whose cap-exceeded branch
calls clear() — safe for a cache, where a dropped entry only forces a
re-fetch, and wrong here.

Two failure modes are handled explicitly:

- A failure partway through the walk keeps the chunks already read and the
  cursor already reached. Resetting to lastUpdateCount would re-walk them next
  poll, so a backlog larger than one rate-limit window could never drain and
  would trip index.ts's five-strike poll error limit.
- A chunk that does not advance the cursor returns an error rather than looping
  or skipping. The return happens before that chunk is appended and leaves the
  cursor where it was, so its changes are deferred to the next poll rather than
  lost or double-sent.

Ten tests added. Four fail against the previous implementation: the multi-chunk
walk, the partial-failure resume, the cap-exceeded partial cursor, and the
non-advancing chunk. The rest pin behaviour that must not regress — exactly one
call on a quiet account and when only excluded objects changed, a chunk with no
chunkHighUSN treated as the end of the walk, a cursor that raced past the
pre-walk sync state not being clamped back, a chunk omitting updateCount, and a
high USN below the cursor.

Blast radius is small regardless: polling only runs when
EVERNOTE_POLLING_ENABLED is exactly "true", which is off by default, and the
path only reads sync state and posts webhooks — it never writes to Evernote.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T04:46:55.003005Z 91f7437 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@jack-arturo jack-arturo added babysit:waiting-codex 👀 Codex reviewing current head babysit:ready 👍 Codex all clear + gates pass; human may merge and removed babysit:waiting-codex 👀 Codex reviewing current head labels Nov 23, 2026
@jack-arturo

jack-arturo commented Nov 23, 2026 •

Copy link
Copy Markdown
Member Author

Babysit: ready. Verified green at 91f743763b52afebf2915af0e4597bee275f8dcf.

Gate Result
Codex 👍 clean, settled by reaction on this head (completed, no findings, not stale)
Review threads 0 unresolved, 0 outdated-pending
CI 15 / 15 SUCCESS
Mergeability MERGEABLE / CLEAN
Head sync matches the pushed commit, re-checked at readiness
Size 371 changed lines — under the 400 band
Review requests posted by babysit 0 — smart auto-review engaged on open
Remediation pushes 0

Local pre-PR review: Codex CLI gpt-6.1-sol / medium — no P0/P1. It verified the
no-skip, no-duplicate and termination properties against the SyncChunk reference
and named four coverage gaps, which are closed in this PR (notably the
excluded-only chunk, the case that would expose a doubled call on the common
path).

Negative control: reverting src/polling.ts alone and re-running gave 4 failed /
6 passed, so four of the tests pin the bug rather than merely describing it.

No merge performed — that is yours. Merging this should also let release PR #94
regenerate with this fix: entry.

Edited to remove a literal review-trigger token from the table above: it was read as a review request and bounced on environment setup. The 👍 clean review on 91f7437 predates that and is unaffected.

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@jack-arturo
jack-arturo merged commit f0da7b6 into main Nov 23, 2026
15 checks passed
@jack-arturo
jack-arturo deleted the fix/polling-multi-chunk-walk branch November 23, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

babysit:ready 👍 Codex all clear + gates pass; human may merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Polling drops >100 changes per interval (single-chunk force-advance)

1 participant