Skip to content

fix(remote): stop reading a trimmed feed as a broken one - #863

Merged
huichen merged 2 commits into
mainfrom
fix/lean-feed-integrity
Sep 26, 2026
Merged

huichen merged 2 commits into
mainfrom
fix/lean-feed-integrity

Conversation

@huichen

@huichen huichen commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

What broke

The lean trim (#844) drops whole events, which leaves holes in the idx sequence. Three client-side integrity checks still assumed one event per source index, so all three rejected a trimmed feed:

check what it did
runCursor.nextEvent called the jump a gap and reconciled — but reconciling fetches the same trimmed range, so the lane retried forever
fetchEventsSince snapshot tail required watermark == boundary + events.length
runReconcile tail required watermark == since + events.length
H3 "unknown prefix" reconciled a run whose first surviving frame sits above idx 0

The user-visible result on a real phone: during streaming, the transcript sat under "最新内容尚未同步完成,正在重试…" and never cleared — the reconcile could not succeed by construction.

The desktop side already knew this class of hazard: publish_event refuses to drop an oversize event and truncates its data instead, with the comment "so the client's dedup cursor doesn't get a permanent hole". The lean trim dropped events anyway and nothing told the client.

The fix

On the wire — a trimmed replay page states how many source events its range held before the trim (rawEvents). That keeps the watermark check exact rather than relaxed: the raw count proves the tail reached the pinned watermark, and strict ordering plus range bounds still catch a garbled or reordered reply. A page from a peer that trims nothing carries no rawEvents and is validated exactly as before.

On the live lane — there is no page to annotate, so the client's cursor is told whether this connection agreed to a feed that omits indices (its own lean_events_v1 ack, read through the client ref because the ack lands after the engine is built) and then advances over a hole. A gap verdict is unanswerable when the peer is not sending those slices, so it must not be issued.

Verification

  • Both directions tested, and each falsified by reverting the rule it guards:
    • a trimmed tail passes with its raw count and is still rejected without one (falsify → exactly those two tests fail);
    • a jump applies on a feed that omits indices and is still a gap on a feed that does not (falsify → exactly that test fails).
  • mobile npx jest — 128 suites / 2067 tests; tsc --noEmit and eslint clean.
  • desktop remote_host:: — 83 passed; cargo fmt --check and clippy --all-targets -D warnings clean.
  • python3 scripts/docs/check-docs.py — 205 docs OK.

Still open (reported, not silently fixed)

The live-lane half trusts the peer's ack instead of a per-event statement. A desktop that trims could also stamp each survivor with how many indices it skipped, which would be verifiable rather than trusted — worthwhile, but a larger wire change than this fix should carry.

The lean trim drops whole events, which leaves holes in the `idx` sequence.
Three client-side integrity checks still assumed one event per source index, so
every one of them rejected a trimmed feed:

- `nextEvent` called the jump a gap and reconciled — but reconciling fetches
  the same trimmed range, so the lane retried forever. That is the sync notice
  a phone showed above the transcript and could never clear.
- `fetchEventsSince`'s snapshot-tail check required
  `watermark == boundary + events.length`, and `runReconcile` required
  `watermark == since + events.length`.
- The "unknown prefix" (H3) reconcile fired for a run whose first *surviving*
  frame sits above idx 0 — again for slices the peer is not sending.

A trimmed replay page now states how many source events its range held before
the trim (`rawEvents`), so the watermark check stays exact instead of being
relaxed to "whatever arrived is enough": the raw count proves the tail reached
the watermark, and strict ordering within the range still catches a garbled or
reordered reply. A page from a peer that trims nothing carries no `rawEvents`
and is validated exactly as before, so this is also the boundary for old
clients.

The live lane needs the peer's answer rather than the page: the client's cursor
is told whether this connection agreed to a feed that omits indices (its own
`lean_events_v1` ack) and then advances over a hole. A gap verdict is
unanswerable by construction when the peer is not sending those slices.

Tests cover both directions — a trimmed tail passes with its raw count and is
still rejected without one; a jump applies on a feed that omits indices and is
still a gap on a feed that does not — and each was falsified by reverting the
rule it guards.
@huichen
huichen enabled auto-merge (squash) September 26, 2026 01:23
`npm test` runs jest with `--runInBand`, so a lane left holding its retry timer
keeps the process alive after the run: jest prints "did not exit" and then waits
forever. CI's Mobile job hung for over an hour on this.

A gap verdict schedules that timer, and the "still a gap on an untrimmed feed"
test provokes one on purpose. Both new tests now release the engine in a
`finally`, the way the neighbouring timer-touching tests already do.

Verified with the CI command against a clean tree: `npx jest --runInBand` exits
in ~34s; before this it was still running after 200s. Reverting this file alone
makes the suite exit again, and so does reverting the source change — the leak
was the missing release, not the fix.
@huichen
huichen merged commit 9a7e95a into main Sep 26, 2026
12 checks passed
@huichen
huichen deleted the fix/lean-feed-integrity branch September 26, 2026 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant