Sync upstream #393 (session management) into develop - #15
Conversation
* wip * wip * wip * final things * apply session fix
Adopts upstream's sessionBuffer.ingest session lifecycle (retiring the fork's BullMQ createSessionEndJob mechanism) while re-grafting the fork's historical-event session reconstruction on top. Decisions (see FORK-PATCHES.md): - Adopt sessionBuffer.ingest for live traffic. - Reconstruct sessions for historical/late events (offline-first SDKs); upstream deliberately suppresses these. - "Historical" = isTimestampFromThePast (upstream's 15-min cutoff). Conflict resolutions (5 files): - apps/api/src/utils/ids.ts: upstream edge derivation + fork isTimestampFromThePast param that skips the live-session lookup for historical events (so a backfill never joins the live session). - apps/api/src/controllers/track.controller.ts: getTimestamp merges upstream's 15-min isTimestampFromThePast with the fork's 5-day floor; threads isFromPast into the queue payload; keeps batch envelope + dispatchEvent + replay BACKCOMPAT. - apps/api/src/controllers/event.controller.ts: upstream queueData + Kafka dual-path; threads isTimestampFromThePast + eventTimeMs. - apps/worker/src/jobs/events.incoming-event.ts: upstream sessionBuffer.ingest live path; re-grafted historical reconstruction arm (one session_start per bucket, Redis-lock dedup) + __syncedAt. - apps/worker/src/jobs/events.incoming-events.test.ts: rewritten for the new mechanism + reconstruction-arm coverage. Post-merge fix: re-added isTimestampFromThePast to the incoming-event queue payload (packages/queue/src/queues.ts) — the auto-merge silently dropped it, which would have broken worker reconstruction gating. Tests: typecheck (api/worker/queue/validation) clean; worker 27/27; ids 6/6; track.controller 13/13; track-batch.router 43/43. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR overhauls session lifecycle ingestion and cleanup, adds report global filters and new retention outputs, centralizes subscription-state-driven billing and onboarding flows, introduces Amplitude imports, tightens bot handling for authenticated traffic, and adds operational scripts, docs, and end-to-end coverage. ChangesSession lifecycle and event ingestion
Reporting filters and retention queries
Subscription state, billing UI, and onboarding
Amplitude import support
Bot detection, authenticated traffic, and small utilities
Estimated code review effort🎯 5 (Critical) | ⏱️ ~120 minutes Possibly related PRs
Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
|
What
Syncs upstream #393 — new session management into
develop. Upstream moved session lifecycle out of the BullMQ delayed-job mechanism and into asessionBuffer.ingest()abstraction inpackages/db. This PR adopts that engine and re-grafts the fork's offline-first behavior on top.Decisions baked in
sessionBuffer.ingest(retire the fork'screateSessionEndJob/getActiveSessionEndJobmechanism)isTimestampFromThePastflag as the single definitionThese (and re-apply instructions for future syncs) are documented in
FORK-PATCHES.md.Fork-side files to review
apps/api/src/utils/ids.ts— upstream edge derivation (getExistingSession+withinIdleWindow+ deterministic bucket) + forkisTimestampFromThePastparam that skips the live-session lookup for historical events. Load-bearing: without it,withinIdleWindowis true for old timestamps and a backfill would join the device's live session.apps/api/src/controllers/track.controller.ts—getTimestampnow merges upstream's 15-minisTimestampFromThePastwith the fork's 5-day hard floor (throws 400). Threads the flag +eventTimeMsintogetDeviceIdand the queue payload. Keeps the batch envelope,dispatchEvent, and replay BACKCOMPAT.apps/api/src/controllers/event.controller.ts— deprecated/event: upstream's typedqueueData+ Kafka dual-path; threadsisTimestampFromThePast+eventTimeMs.apps/worker/src/jobs/events.incoming-event.ts— adopts upstream'ssessionBuffer.ingestlive path; re-grafts the historical reconstruction arm (onesession_startper(project, sessionId)bucket, Redis-lock dedup, no live-state touched) and preserves the__syncedAtstamp.packages/queue/src/queues.ts— re-addedisTimestampFromThePastto the incoming-event payload.apps/api/src/utils/ids.test.ts,apps/api/src/controllers/track.controller.test.ts,apps/worker/src/jobs/events.incoming-events.test.ts(rewritten for the new mechanism + reconstruction coverage).Testing
session-reaper/session-vacuum/create-session-endcrons)ids.test.ts6/6 ·track.controller.test.ts13/13 ·track-batch.router.test.ts43/43 (batch end-to-end through the resolved controller)insights.router(ClickHouse integration) not run in the sandbox.Before deploy
pnpm migrate— the merge brings 2 new Prisma migrations (codegenalready run for client types).sessionBuffermay need ClickHouse schema/MVs applied locally.🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
New Features
Improvements