Fix: Live Ops events stuck at 0 on cold SSE Lambda - #39
Merged
Conversation
Symptom: navigating to /earn/activity sometimes left the Live Ops DockBar stuck at "0 events" — the SSE snapshot arrived with replayed_count: 0 and no activity frames ever followed, so the counter never advanced. Root cause: on Vercel the SSE endpoint, tRPC handler, and webhook each run as separate Lambda functions with per-process globalThis state. The activity store and event log were seeded only as a top-level side effect of importing src/server/routers/deals.ts. The SSE route never imports that module, so when routing luck put the SSE connection on a cold Lambda that had never handled a tRPC call, getEventsSince(0) returned [] and latest_seq was 0. Polling eventually filled the Events tab via the tRPC Lambda, but the DockBar counter (wired to SSE deltas only) stayed at zero. Fix: extract the activity store + seed + projection into src/server/activity/state.ts and call ensureSeeded() inside the SSE GET handler. ensureSeeded is idempotent (guarded by state.seeded), so double seeding is a no-op on warm instances. deals.ts now imports from the new module and re-exports the helpers external callers already depend on, so no caller churn. Verified: 327/327 unit tests (including the new app/api/events/activity/route.test.ts which reproduces the cold-Lambda case by wiping globalThis before calling GET), pnpm typecheck clean, 9/9 Playwright specs green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/api/events/activitySSE endpoint can cold-start on a Lambda that has never handled a tRPC call. The activity store + event log were seeded only as a top-level side effect of importingsrc/server/routers/deals.ts, so on those cold LambdasgetEventsSince(0)returned[], the initial SSE snapshot shippedreplayed_count: 0, and the Live Ops DockBar counter stayed stuck at "0 events". The tRPC polling fallback filled the Events tab eventually, but the DockBar (SSE-only) never advanced.ensureSeeded, and projection helpers (reprojectOffer,getOfferSnapshot) intosrc/server/activity/state.ts. The SSE GET handler now callsensureSeeded()explicitly; the call is idempotent (guarded bystate.seeded), so warm instances pay nothing.deals.tsre-exports the helpers external callers (webhooks/process,ai/investigate,ai/tools, tests) already depend on — no caller churn.Test plan
pnpm typecheckcleanpnpm test— 327/327 pass, including a newapp/api/events/activity/route.test.tsthat simulates a cold SSE Lambda by wipingglobalThisbefore callingGETand assertsreplayed_count > 0+ anevent: activityframe follows. This test fails onmainand passes here.pnpm exec playwright test— 9/9 pass (includes the existing Live Ops console spec and the SSE-resume spec).🤖 Generated with Claude Code
Note
Medium Risk
Touches server-side in-memory seeding and the SSE
GET /api/events/activitypath; incorrect seeding/idempotency could cause missing or duplicated replay events across Lambda instances.Overview
Fixes a cold-start issue where the
/api/events/activitySSE Lambda could start without any seeded activity events, causing the initialsnapshotto reportreplayed_count: 0and leaving clients stuck on an empty stream.Seeding/projection logic is extracted into a new
src/server/activity/state.tsmodule (store management,ensureSeeded, reset, and projection helpers),deals.tsis updated to use/re-export those helpers, and the SSE handler now callsensureSeeded()explicitly. Adds a vitest that simulates a cold SSE Lambda by clearingglobalThisstate and asserting that seed events are replayed.Reviewed by Cursor Bugbot for commit b8474db. Bugbot is set up for automated code reviews on this repo. Configure here.