:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): bound the late-snapshot span by the recording length (#99983)
posthog[bot]1995423fd2c8
diff --git a/frontend/src/scenes/session-recordings/player/PurePlayer.tsx b/frontend/src/scenes/session-recordings/player/PurePlayer.tsxindex d583a250e9cb..6f6ab96c27f7 100644--- a/frontend/src/scenes/session-recordings/player/PurePlayer.tsx+++ b/frontend/src/scenes/session-recordings/player/PurePlayer.tsx@@ -138,7 +138,7 @@ export function PurePlayer({ noMeta = false, noBorder = false }: PurePlayerPropsif (isRecentAndInvalid) {posthog.capture('session loaded recent and invalid', {viewedSessionRecording: sessionRecordingId,- recordingStartTime: sessionPlayerData?.start,+ recordingStartTime: sessionPlayerData?.start?.toISOString(),})}},@@ -151,7 +151,7 @@ export function PurePlayer({ noMeta = false, noBorder = false }: PurePlayerPropsif (isOldAndInvalid) {posthog.capture('session loaded old and invalid', {viewedSessionRecording: sessionRecordingId,- recordingStartTime: sessionPlayerData?.start,+ recordingStartTime: sessionPlayerData?.start?.toISOString(),})}},@@ -159,18 +159,27 @@ export function PurePlayer({ noMeta = false, noBorder = false }: PurePlayerProps[isOldAndInvalid])+ // `durationMs` only applies the metadata cap once the recording is fully loaded, so the span and+ // the duration it is measured against are both final only then. `fullyLoaded` also drops back+ // while the inspector fetches full event data, so remember which recording was reported to keep+ // this one event per view.+ const reportedLateFullSnapshotFor = useRef<string | null>(null)+useEffect(() => {- if (hasLateFullSnapshot) {- posthog.capture('session loaded with late full snapshot', {- viewedSessionRecording: sessionRecordingId,- recordingStartTime: sessionPlayerData?.start,- leadingUnplayableMs,- })+ if (!hasLateFullSnapshot || !fullyLoaded || reportedLateFullSnapshotFor.current === sessionRecordingId) {+ return}+ reportedLateFullSnapshotFor.current = sessionRecordingId+ posthog.capture('session loaded with late full snapshot', {+ viewedSessionRecording: sessionRecordingId,+ recordingStartTime: sessionPlayerData?.start?.toISOString(),+ recordingDurationMs: sessionPlayerData?.durationMs,+ leadingUnplayableMs,+ })},// eslint-disable-next-line react-hooks/exhaustive-deps- [hasLateFullSnapshot]+ [hasLateFullSnapshot, fullyLoaded, sessionRecordingId])// An unrenderable span keeps growing while sources arrive, so the duration is only final once@@ -190,7 +199,8 @@ export function PurePlayer({ noMeta = false, noBorder = false }: PurePlayerPropsreportedUnrenderableWindowFor.current = sessionRecordingIdposthog.capture('session loaded with unrenderable window', {viewedSessionRecording: sessionRecordingId,- recordingStartTime: sessionPlayerData?.start,+ recordingStartTime: sessionPlayerData?.start?.toISOString(),+ recordingDurationMs: sessionPlayerData?.durationMs,unrenderableWindowMs,})},diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.tsindex 2d71c838470c..0191c1f014b2 100644--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts@@ -641,10 +641,31 @@ describe('sessionRecordingPlayerLogic', () => {sessionRecordingDataCoordinatorLogic({ sessionRecordingId: '2' }).actions.setProcessedSnapshots(processed)}- beforeEach(async () => {+ // `durationMs` caps the reported spans, and the default mock recording is only 11 seconds+ // long, so each case states the metadata duration its fixture needs.+ const mountWithRecordingDuration = async (recordingDurationSeconds: number): Promise<void> => {+ logic.unmount()+ overrideSessionRecordingMocks({+ getMocks: {+ '/api/environments/:team_id/session_recordings/:id': {+ ...recordingMetaJson,+ recording_duration: recordingDurationSeconds,+ },+ },+ })+ logic = sessionRecordingPlayerLogic({+ sessionRecordingId: '2',+ playerKey: 'test',+ blobV2PollingDisabled: true,+ })+ logic.mount()await expectLogic(logic).toDispatchActions([snapshotDataLogic({ sessionRecordingId: '2' }).actionTypes.loadSnapshotSources]).toFinishAllListeners()+ }++ beforeEach(async () => {+ await mountWithRecordingDuration(360)})// assertions below run synchronously after the seek dispatch — kea listeners@@ -1027,6 +1048,28 @@ describe('sessionRecordingPlayerLogic', () => {})+ it.each([+ {+ description: 'reports at most the recording length when the start is skewed before the recording',+ recordingDurationSeconds: 60,+ },+ {+ // the clamped span fills the whole timeline here, so gating the warning on it would+ // hide the warning exactly where every second of the recording is unplayable+ description: 'still warns when the recording is no longer than the warning threshold',+ recordingDurationSeconds: 15,+ },+ ])('$description', async ({ recordingDurationSeconds }) => {+ // A skewed start drags `start` back but not the metadata duration the timeline is capped to,+ // so the raw offset to the first full snapshot claims more time than the recording holds.+ await mountWithRecordingDuration(recordingDurationSeconds)+ seedRecording([inc(START), inc(START + 1000)], [fs(LATE_FS_TS)])++ expect(logic.values.leadingUnplayableMs).toBe(logic.values.sessionPlayerData.durationMs)+ expect(logic.values.leadingUnplayableMs).toBeLessThan(LATE_FS_TS - START)+ expect(logic.values.hasLateFullSnapshot).toBe(true)+ })+it.each([{// the reported symptom: window 2 opens and only sends mouse moves, so it animates a@@ -1104,6 +1147,54 @@ describe('sessionRecordingPlayerLogic', () => {expect(logic.values.hasUnrenderableWindow).toBe(true)})+ // The leading span reports everything up to its handover point, so a later window that is+ // blank before that point is time the banner and the telemetry already count.+ it.each([+ {+ description: 'drops a later window span the leading span already covers',+ firstSourceSnapshots: [idle(START)],+ secondSourceSnapshots: [+ w2inc(START + 61000),+ w2inc(START + 62000),+ w2fs(LATE_FS_TS),+ w2inc(LATE_FS_TS + 1000),+ ],+ expectedLeadingUnplayableMs: LATE_FS_TS - START,+ expectedUnrenderableWindowMs: 0,+ expectedHasUnrenderable: false,+ },+ {+ // window 1 recovers on its own late full snapshot, and window 2 stays blank across it+ description: 'keeps only the part of a later window span that follows the handover',+ firstSourceSnapshots: [idle(START)],+ secondSourceSnapshots: [+ ...w2moves(START + 61000, START + 111000),+ fs(START + 116000),+ w1move(START + 117000),+ w1move(START + 122000),+ ...w2moves(START + 127000, START + 177000),+ ],+ expectedLeadingUnplayableMs: 116000,+ expectedUnrenderableWindowMs: 55000,+ expectedHasUnrenderable: true,+ },+ ])(+ '$description',+ ({+ firstSourceSnapshots,+ secondSourceSnapshots,+ expectedLeadingUnplayableMs,+ expectedUnrenderableWindowMs,+ expectedHasUnrenderable,+ }) => {+ seedRecording(firstSourceSnapshots, secondSourceSnapshots)++ expect(logic.values.leadingUnplayableMs).toBe(expectedLeadingUnplayableMs)+ expect(logic.values.unrenderableWindowMs).toBe(expectedUnrenderableWindowMs)+ expect(logic.values.hasUnrenderableWindow).toBe(expectedHasUnrenderable)+ }+ )+it('leaves a recording with no full snapshot at all to the unplayable takeover', () => {// the full-screen error replaces the player here, so a banner behind it would count// recordings this warning never helpeddiff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.tsindex 81734d60a74a..d0fce649f71c 100644--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts@@ -636,6 +636,7 @@ export interface sessionRecordingPlayerLogicValues {isSkippingToMatchingEvent: booleanisWaitingForIngestion: booleanjumpTimeMs: number+ leadingRecoveryTimestamp: number | nullleadingUnplayableMs: numberlogicProps: SessionRecordingPlayerLogicPropsmaskingWindow: boolean@@ -1129,15 +1130,16 @@ export interface sessionRecordingPlayerLogicMeta {sessionPlayerData: SessionPlayerData,storeVersion: number) => (timestamp: number) => SeekRenderability- leadingUnplayableMs: (+ leadingRecoveryTimestamp: (sessionPlayerData: SessionPlayerData,seekRenderability: (timestamp: number) => SeekRenderability- ) => number- hasLateFullSnapshot: (leadingUnplayableMs: number) => boolean+ ) => number | null+ leadingUnplayableMs: (sessionPlayerData: SessionPlayerData, leadingRecoveryTimestamp: number | null) => number+ hasLateFullSnapshot: (sessionPlayerData: SessionPlayerData, leadingRecoveryTimestamp: number | null) => booleanunrenderableWindowSpans: (sessionPlayerData: SessionPlayerData,seekRenderability: (timestamp: number) => SeekRenderability,- leadingUnplayableMs: number+ leadingRecoveryTimestamp: number | null) => UnplayableSpan[]unrenderableWindowMs: (unrenderableWindowSpans: UnplayableSpan[]) => numberhasUnrenderableWindow: (unrenderableWindowMs: number) => boolean@@ -1925,30 +1927,30 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(},],- // The leading span playback can't render — when the initial full snapshot was lost or arrived- // late, this is the offset from start to the FullSnapshot the player clamps the playhead to.- // Derived from seekRenderability so the scrubber marker matches where playback actually starts,- // window-aware and excluding the no-full-snapshot-anywhere case (handled by the unplayable takeover).- // The `clampToFullSnapshot` verdict already requires the data before the recovery point to be- // loaded (and so can't later flip to `renderable`), so it gates itself — surfacing the marker as- // soon as playback would clamp rather than waiting for the whole recording to finish loading.- leadingUnplayableMs: [+ // Where the leading span playback can't render hands over — when the initial full snapshot was+ // lost or arrived late, this is the FullSnapshot the player clamps the playhead to, or null when+ // there is no such span. Derived from seekRenderability so the scrubber marker matches where+ // playback actually starts, window-aware and excluding the no-full-snapshot-anywhere case+ // (handled by the unplayable takeover). The `clampToFullSnapshot` verdict already requires the+ // data before the recovery point to be loaded (and so can't later flip to `renderable`), so it+ // gates itself — surfacing the marker as soon as playback would clamp rather than waiting for the+ // whole recording to finish loading.+ leadingRecoveryTimestamp: [(s) => [s.sessionPlayerData, s.seekRenderability],(sessionPlayerData: SessionPlayerData,seekRenderability: (timestamp: number) => SeekRenderability- ): number => {- const start = sessionPlayerData.start?.valueOf()- if (start == null) {- return 0+ ): number | null => {+ if (sessionPlayerData.start == null) {+ return null}const firstWindowSegment = sessionPlayerData.segments.find((segment) => segment.kind === 'window')if (!firstWindowSegment) {- return 0+ return null}const renderability = seekRenderability(firstWindowSegment.startTimestamp)if (renderability.kind !== 'clampToFullSnapshot') {- return 0+ return null}// A backdated `sessionIdle` Custom event pulls `start` back over the idle span, and the SDK// drops everything else while idle, so a Custom-only span is empty rather than lost.@@ -1960,17 +1962,40 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(break}if (event.type !== EventType.Custom) {- return recoveryTimestamp - start+ return recoveryTimestamp}}}- return 0+ return null},],+ leadingUnplayableMs: [+ (s) => [s.sessionPlayerData, s.leadingRecoveryTimestamp],+ (sessionPlayerData: SessionPlayerData, leadingRecoveryTimestamp: number | null): number => {+ const start = sessionPlayerData.start?.valueOf()+ if (start == null || leadingRecoveryTimestamp == null) {+ return 0+ }+ // `durationMs` is capped by the metadata duration, so a skewed start can put the recovery+ // point past the end of the timeline. A span longer than the recording it belongs to is+ // impossible, so report at most the whole recording.+ return Math.min(leadingRecoveryTimestamp - start, sessionPlayerData.durationMs)+ },+ ],++ // The threshold reads the unclamped offset, not `leadingUnplayableMs`. The clamped span can+ // never exceed the recording length, so a recording no longer than the threshold would always+ // fall under it and silence its own warning, which is the worst case rather than a mild one.hasLateFullSnapshot: [- (s) => [s.leadingUnplayableMs],- (leadingUnplayableMs: number): boolean => leadingUnplayableMs > LATE_FULL_SNAPSHOT_THRESHOLD_MS,+ (s) => [s.sessionPlayerData, s.leadingRecoveryTimestamp],+ (sessionPlayerData: SessionPlayerData, leadingRecoveryTimestamp: number | null): boolean => {+ const start = sessionPlayerData.start?.valueOf()+ if (start == null || leadingRecoveryTimestamp == null) {+ return false+ }+ return leadingRecoveryTimestamp - start > LATE_FULL_SNAPSHOT_THRESHOLD_MS+ },],// Spans of a window that opened without ever sending its initial DOM. rrweb draws its own@@ -1980,11 +2005,11 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(// One check per window is enough: once a window has a FullSnapshot, rrweb keeps its DOM for// every later segment of that window.unrenderableWindowSpans: [- (s) => [s.sessionPlayerData, s.seekRenderability, s.leadingUnplayableMs],+ (s) => [s.sessionPlayerData, s.seekRenderability, s.leadingRecoveryTimestamp],(sessionPlayerData: SessionPlayerData,seekRenderability: (timestamp: number) => SeekRenderability,- leadingUnplayableMs: number+ leadingRecoveryTimestamp: number | null): UnplayableSpan[] => {// A recording where no window ever rendered belongs to the unplayable takeover, which// replaces the player instead of warning over it. Leaving it out also keeps this@@ -2005,9 +2030,9 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(}const firstWindowSegment = sessionPlayerData.segments.find((segment) => segment.kind === 'window')- // Where the leading span hands over. Its recovery point can be another window's+ // The leading span hands over at its recovery point, which can be another window's// FullSnapshot, so the first window can go blank again after it and still needs a span.- const leadingRecoveryTimestamp = (sessionPlayerData.start?.valueOf() ?? 0) + leadingUnplayableMs+ const handoverTimestamp = leadingRecoveryTimestamp ?? sessionPlayerData.start?.valueOf() ?? 0const spans: UnplayableSpan[] = []const checkedWindows = new Set<number>()for (const segment of sessionPlayerData.segments) {@@ -2015,10 +2040,7 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(continue}const windowId = segment.windowId- if (- windowId === firstWindowSegment?.windowId &&- segment.startTimestamp < leadingRecoveryTimestamp- ) {+ if (windowId === firstWindowSegment?.windowId && segment.startTimestamp < handoverTimestamp) {continue}if (checkedWindows.has(windowId)) {@@ -2067,7 +2089,14 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(if (blankSegment.windowId !== windowId) {continue}- const spanStart = Math.max(blankSegment.startTimestamp, segment.startTimestamp)+ // The leading span already claims every millisecond before the handover, so a+ // blank stretch that starts earlier keeps only the part after it. Without this+ // the banner and the telemetry report the same lost time twice.+ const spanStart = Math.max(+ blankSegment.startTimestamp,+ segment.startTimestamp,+ handoverTimestamp+ )const spanEnd = Math.min(blankSegment.endTimestamp, endTimestamp)if (spanEnd <= spanStart) {continue