Skip to content

feat(log-stream): spread batches over ticks and replay recent activity - #289

Draft
alukach wants to merge 3 commits into
source-cooperative:mainfrom
alukach:worktree-log-stream-spread-replay
Draft

alukach wants to merge 3 commits into
source-cooperative:mainfrom
alukach:worktree-log-stream-spread-replay

Conversation

@alukach

@alukach alukach commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Stacked on #288 (proxy batching): until that merges, this diff also shows its two commits. Review the last commit, feat(log-stream): spread batches over ticks and replay recent activity.

What

The hub now smooths what viewers see, and new viewers see something immediately.

  • Spreading. Each batch from a proxy isolate is split over the next six 500 ms ticks (three seconds) instead of arriving in one tick. Counts are conserved, extra units rotate across slots with a shared cursor, and every emitted datacenter entry has a count of at least 1. Ticks are numbered by absolute time and alarms are aligned to tick boundaries, so a late alarm catches up on the ticks it missed in a single tick.
  • Idle. The one empty tick that lets the globe fade out is sent only once nothing is queued, not after every sparse tick.
  • Replay on connect. A viewer who connects gets one message straight away: the last three seconds of already-emitted activity, merged. It has the shape of a tick plus replay: true and windowMs, and it never includes a tick the viewer will also receive live.
  • Cost. The alarm runs only while someone is watching or activity is queued, as before, so an idle hub still stops and hibernates. Replay and tick sends are WebSocket messages from the Durable Object, which are not billed.
  • Bounds. A batch keeps at most 100 product keys (keys capped at 128 characters). A datacenter's count still includes the dropped keys.
  • ADR-008 describes spreading and replay and the resulting delay (up to about six seconds end to end).

Why

After #288 the hub receives a lump of activity from each proxy isolate every few seconds. Without this, a datacenter's dot refreshes in bursts, and a new viewer sees an empty globe until the next live tick.

Design notes

  • Replay is type: "tick" with an extra field, not a new message type. LiveGlobe.tsx on source.coop origin/main handles only type === "tick", ignores unknown fields, never reads requestsPerWindow or viewers, and sizes dots by log10(1 + n), so a replay of about six ticks of counts draws as one slightly larger dot. A separate type would be ignored and the replay would do nothing. The frontend needs no change.
  • The pure logic lives in src/aggregate.mjs (spread, merge, due, replay window, prune, pending) with Node tests; location-hub.ts only wires it to the alarm and the WebSocket.

Tested

  • node --test: 18 tests (conservation for every count 0–50 and every cursor, round-robin remainders, key cap, late-alarm catch-up, replay window excludes unemitted and too-old ticks, pruning bounds memory, no zero counts), plus tsc --noEmit.
  • Run locally with wrangler dev and a WebSocket client: 12 requests for one datacenter came out as 2 per tick over exactly 6 ticks, with a single request on one tick, on 500 ms boundaries; no idle tick appeared mid-trickle and one followed once it drained. A second viewer connecting after three ticks received a replay of exactly those three (7 requests) and then the remaining three live, with no overlap and no loss. An old-style bare event still arrived, delayed to a later tick.

Not checked

  • The globe on staging. I did not render it and have no before/after images. Activity now reaches viewers up to about six seconds after the requests, and the frontend fades dots over 10 s, so I expect a steady trickle; the delay and the 10 s fade are what to look at.
  • Behaviour under real load (about 370 posts/second before perf(log-stream): batch live-globe posts per isolate #288) and after a Durable Object eviction, which empties the queue and the replay window for a few seconds.
  • Separate, unchanged by this PR: webSocketClose calls ws.close(code, reason), which throws "Uncaught Error" locally when a client closes without a status code. It is the same line on main.

🤖 Generated with Claude Code

alukach and others added 3 commits November 19, 2026 22:00
The proxy posted one event per public read to a single global Durable Object
(~370 req/s at peak): about $145/month of Durable Object requests and CPU, plus
overload exceptions in the busiest hours.

An isolate runs in one datacenter, so each isolate now gathers its public reads
for 3 s and posts one batch of exact per-product counts. A busy datacenter
collapses into few posts; a quiet one still reports its first request. The hub
adds the counts as posted, so the tick payload is unchanged. Only the colo and
product counts leave the proxy now (no coordinates, city, country or key), and
the hub still accepts the old one-request-per-post form for rollout.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A failed or rejected post dropped its whole batch without a trace. Say so at
info level (not WARN, so a failing globe never counts toward the WARN alerts);
a Durable Object past its request limit now shows up in the logs.

Also list location_batch.rs in ADR-008 and note the up-to-3-second delay.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The hub emitted each proxy batch in a single tick, so a datacenter's dots
arrived as a burst every few seconds, and a new viewer saw nothing until the
next live tick.

Batches are now spread over the next six 500 ms ticks (conserving counts, with
a rotating cursor for the remainders), keyed by absolute tick so a late alarm
catches up. The idle tick is sent only once nothing is queued. A viewer who
connects gets one replay message (type "tick" with replay: true) of the last
three seconds already emitted, so it never overlaps the live ticks. The alarm
runs only while someone watches or activity is queued, so an idle hub still
stops. Batches are capped at 100 product keys without losing the datacenter's
count.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch had an error being deployed

1 failed deployment
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