: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
feat(replay-core-v4): inspect sessions across flushes (#117966)
Nicholas Waltz220ef198cedf
diff --git a/rust/ai-research/replay-core-v4/src/inspect/clicks.rs b/rust/ai-research/replay-core-v4/src/inspect/clicks.rsnew file mode 100644index 000000000000..1c87f5f5cab3--- /dev/null+++ b/rust/ai-research/replay-core-v4/src/inspect/clicks.rs@@ -0,0 +1,91 @@+//! Rage and dead clicks, with the session feature recorder's definitions (`session-feature-recorder.ts`).++/// Clicks closer than this in time and space continue a run of clicks.+const RUN_WINDOW_MS: f64 = 1000.0;+const RUN_DISTANCE_PX: f64 = 30.0;+/// The click in a run that starts counting as a rage click.+const RAGE_RUN_LENGTH: u32 = 3;++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum ClickKind {+ Plain,+ Rage,+ /// The previous click was alone and nothing navigated after it. It is counted when this click arrives.+ AfterDead,+}++/// Click state that spans blocks, because a run of clicks can cross a flush.+#[derive(Default)]+pub struct ClickTracker {+ last: Option<(f64, f64, f64)>,+ run: u32,+ navigated_since_last_click: bool,+}++impl ClickTracker {+ pub fn navigated(&mut self) {+ self.navigated_since_last_click = true;+ }++ /// Records a click and says how to count it. A click without coordinates starts a new run.+ pub fn click(&mut self, ts: f64, position: Option<(f64, f64)>) -> ClickKind {+ let previous = self.last;+ let navigated = std::mem::take(&mut self.navigated_since_last_click);+ let Some((x, y)) = position else {+ self.run = 1;+ self.last = None;+ return ClickKind::Plain;+ };+ self.last = Some((ts, x, y));+ let Some((last_ts, last_x, last_y)) = previous else {+ self.run = 1;+ return ClickKind::Plain;+ };+ if ts - last_ts < RUN_WINDOW_MS && (x - last_x).hypot(y - last_y) < RUN_DISTANCE_PX {+ self.run += 1;+ return if self.run >= RAGE_RUN_LENGTH {+ ClickKind::Rage+ } else {+ ClickKind::Plain+ };+ }+ let dead = self.run == 1 && !navigated;+ self.run = 1;+ if dead {+ ClickKind::AfterDead+ } else {+ ClickKind::Plain+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn classifies_clicks_as_the_feature_recorder_does() {+ let at = |ts: f64, x: f64| (ts, Some((x, 100.0)));+ // (time, position, navigated before this click, expected)+ let cases = [+ (at(0.0, 10.0), false, ClickKind::Plain),+ (at(200.0, 12.0), false, ClickKind::Plain),+ (at(400.0, 14.0), false, ClickKind::Rage),+ (at(600.0, 16.0), false, ClickKind::Rage),+ // A distant click ends the run. The run was longer than one click, so nothing was dead.+ (at(5000.0, 500.0), false, ClickKind::Plain),+ // The lone click at 5000 had no navigation after it: dead, counted now.+ (at(9000.0, 10.0), false, ClickKind::AfterDead),+ // A navigation after the lone click at 9000 means the click did something.+ (at(15000.0, 500.0), true, ClickKind::Plain),+ ((16000.0, None), false, ClickKind::Plain),+ ];+ let mut tracker = ClickTracker::default();+ for ((ts, position), navigated, expected) in cases {+ if navigated {+ tracker.navigated();+ }+ assert_eq!(tracker.click(ts, position), expected, "click at {ts}");+ }+ }+}diff --git a/rust/ai-research/replay-core-v4/src/inspect/dom.rs b/rust/ai-research/replay-core-v4/src/inspect/dom.rsindex c95fd1febdc1..472eba7ec8ed 100644--- a/rust/ai-research/replay-core-v4/src/inspect/dom.rs+++ b/rust/ai-research/replay-core-v4/src/inspect/dom.rs@@ -30,6 +30,14 @@ pub struct DomInspector {windows: HashMap<String, WindowDom>,}+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]+pub struct Observed {+ /// How many nodes a mutation added or removed.+ pub changed: u32,+ /// The event is a full snapshot that decoded.+ pub full_snapshot: bool,+}+/// What one walk over serialized nodes found.#[derive(Default)]struct Walk {@@ -135,6 +143,39 @@ fn walk_nodes(bytes: &[u8], span: Span, walk: &mut Walk) {}}+/// What a full snapshot's data holds.+enum Snapshot {+ /// Its compressed data does not decode.+ Undecoded,+ /// It has no node to walk.+ Empty,+ Walked(Walk),+}++/// Walks the DOM in a full snapshot's data, decoded when the event is compressed.+fn walk_snapshot(inner: &[u8], event: &Event, data: Span) -> Snapshot {+ let expansion;+ let (bytes, root) = if event.compressed && scan::is_string(inner, data) {+ // The decode the message already did, within its budget. An event it skipped is not decoded here.+ expansion = cv::expansion(inner, event);+ let Some(Some(json)) = expansion.json(data) else {+ return Snapshot::Undecoded;+ };+ let Ok(root) = scan::locate_value(json, 0) else {+ return Snapshot::Undecoded;+ };+ (json, root)+ } else {+ (inner, data)+ };+ let Some(node) = field(bytes, root, b"node") else {+ return Snapshot::Empty;+ };+ let mut walk = Walk::default();+ walk_nodes(bytes, node, &mut walk);+ Snapshot::Walked(walk)+}+fn attribute_contains(bytes: &[u8], value: Span, word: &str) -> bool {scan::is_string(bytes, value)&& scan::unescape(bytes, value).is_ok_and(|v| v.to_ascii_lowercase().contains(word))@@ -185,55 +226,51 @@ impl WindowDom {impl DomInspector {/// Updates the window's DOM with one event and adds what it found to the block.- pub fn observe(&mut self, inner: &[u8], event: &Event, window: &str, stats: &mut BlockStats) {+ pub fn observe(+ &mut self,+ inner: &[u8],+ event: &Event,+ window: &str,+ stats: &mut BlockStats,+ ) -> Observed {let dom = self.windows.entry(window.to_owned()).or_default();+ let mut observed = Observed::default();match (event.kind, event.source) {(Some(FULL_SNAPSHOT), _) => {- let Some(data) = event.data else { return };- let expansion;- let (bytes, root) = if event.compressed && scan::is_string(inner, data) {- // The decode the message already did, within its budget. An event it skipped is not decoded here.- expansion = cv::expansion(inner, event);- let Some(Some(json)) = expansion.json(data) else {- stats.decode_failures += 1;- return;- };- let Ok(root) = scan::locate_value(json, 0) else {- stats.decode_failures += 1;- return;- };- (json, root)- } else {- (inner, data)- };- let Some(node) = field(bytes, root, b"node") else {- return;- };- let mut walk = Walk::default();- walk_nodes(bytes, node, &mut walk);- // A full snapshot replaces the window's DOM.- *dom = WindowDom {- has_snapshot: true,- ..WindowDom::default()- };- dom.insert(&walk.ids);- stats.dom_nodes = stats.dom_nodes.max(walk.nodes);- stats.dom_text_chars = stats.dom_text_chars.max(walk.text_chars);- stats.remote_stylesheets += walk.remote_stylesheets;- stats.inline_stylesheets += walk.inline_stylesheets;- stats.iframes += walk.iframes;- stats.canvases += walk.canvases;- stats.videos += walk.videos;- stats.shadow_roots += walk.shadow_roots;+ match event+ .data+ .map_or(Snapshot::Empty, |data| walk_snapshot(inner, event, data))+ {+ Snapshot::Walked(walk) => {+ // A full snapshot replaces the window's DOM.+ *dom = WindowDom {+ has_snapshot: true,+ ..WindowDom::default()+ };+ dom.insert(&walk.ids);+ stats.dom_nodes = stats.dom_nodes.max(walk.nodes);+ stats.dom_text_chars = stats.dom_text_chars.max(walk.text_chars);+ stats.remote_stylesheets += walk.remote_stylesheets;+ stats.inline_stylesheets += walk.inline_stylesheets;+ stats.iframes += walk.iframes;+ stats.canvases += walk.canvases;+ stats.videos += walk.videos;+ stats.shadow_roots += walk.shadow_roots;+ observed.full_snapshot = true;+ }+ Snapshot::Undecoded => stats.decode_failures += 1,+ Snapshot::Empty => {}+ }}(Some(INCREMENTAL_SNAPSHOT), Some(MUTATION))if dom.has_snapshot && !dom.cap_reached =>{if let Some(data) = event.data {let expansion = cv::expansion(inner, event);- let (orphans, failed) = observe_mutation(inner, data, &expansion, dom);+ let (orphans, size, failed) = observe_mutation(inner, data, &expansion, dom);stats.orphan_mutations += orphans;stats.decode_failures += u32::from(failed);+ observed.changed = size;}}_ => {}@@ -242,6 +279,7 @@ impl DomInspector {stats.events_before_first_full_snapshot += 1;}stats.node_cap_reached |= dom.cap_reached;+ observed}/// Whether any window of the session has a full snapshot in this process.@@ -277,14 +315,14 @@ fn mutation_lists<'b>((lists, false)}-/// Applies a mutation to the window's node set. Returns how many of its changes name a node that is not there, and-/// whether a list did not decode.+/// Applies a mutation to the window's node set. Returns how many of its changes name a node that is not there, how+/// many nodes it added or removed, and whether a list did not decode.fn observe_mutation(inner: &[u8],data: Span,expansion: &Expansion,dom: &mut WindowDom,-) -> (u32, bool) {+) -> (u32, u32, bool) {let (lists, failed) = mutation_lists(inner, data, expansion);// rrweb does not order adds parent first, so a parent added anywhere in the same mutation counts as present.let mut added = HashSet::new();@@ -313,17 +351,20 @@ fn observe_mutation(.count() as u32;}}+ let mut removed = 0;for (name, bytes) in &lists {if *name == b"removes" {for item in items(bytes) {if let Some(id) = number(bytes, field(bytes, item, b"id")) {dom.nodes.remove(&(id as i64));+ removed += 1;}}}}+ let size = added.len() as u32 + removed;dom.insert(&added.into_iter().collect::<Vec<_>>());- (orphans, failed)+ (orphans, size, failed)}fn items(bytes: &[u8]) -> impl Iterator<Item = Span> + '_ {diff --git a/rust/ai-research/replay-core-v4/src/inspect/mod.rs b/rust/ai-research/replay-core-v4/src/inspect/mod.rsindex ba72864d8dbd..b21f1e1f90c3 100644--- a/rust/ai-research/replay-core-v4/src/inspect/mod.rs+++ b/rust/ai-research/replay-core-v4/src/inspect/mod.rs@@ -1,6 +1,8 @@//! Sampling metadata for the v4 block index, computed in the same byte pass as image extraction.+pub mod clicks;pub mod dom;pub mod events;pub mod rrweb;+pub mod session;pub mod stats;diff --git a/rust/ai-research/replay-core-v4/src/inspect/session.rs b/rust/ai-research/replay-core-v4/src/inspect/session.rsnew file mode 100644index 000000000000..6235cc771112--- /dev/null+++ b/rust/ai-research/replay-core-v4/src/inspect/session.rs@@ -0,0 +1,367 @@+//! Everything the recorder learns about one session, block by block.++use std::sync::Arc;++use super::clicks::{ClickKind, ClickTracker};+use super::dom::DomInspector;+use super::events::{fields, number, Activity, EventCounter};+use super::rrweb::{+ is_click, CUSTOM, FULL_SNAPSHOT, INCREMENTAL_SNAPSHOT, META, MOUSE_INTERACTION, MUTATION,+};+use super::stats::{BlockStats, MAX_WINDOW_IDS};+use crate::scan::message::{Event, Message};++/// Moments kept per block, so a long block cannot grow its index row without bound.+pub const MAX_MOMENTS_PER_BLOCK: usize = 256;+/// A mutation that adds or removes this many nodes changes what the page shows.+pub const LARGE_DOM_CHANGE_NODES: u32 = 100;++/// A point a reader can seek to without replaying the session from its start.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct Moment {+ pub kind: &'static str,+ pub window_id: Arc<str>,+ pub ts_ms: i64,+ /// The event's position in its block.+ pub event_index: u32,+}++/// The state of one session in this process. It outlives blocks, because a session spans many flushes.+#[derive(Default)]+pub struct SessionInspector {+ dom: DomInspector,+ clicks: ClickTracker,+ last_href: Option<String>,+ activity: Activity,+ /// The session's start is known: its first Meta, full snapshot or incremental snapshot arrived.+ decided: bool,+ /// This process first saw the session after its first event, so the DOM state starts unknown. posthog-js can send+ /// custom and plugin events before the first Meta, so only an incremental snapshot before it marks a cold start.+ cold: bool,+ /// Events before the start was known, counted once it is known not to be cold.+ undecided: u32,+}++/// One session's block in the current flush.+#[derive(Default)]+pub struct BlockInspection {+ counter: EventCounter,+ moments: Vec<Moment>,+ events: u32,+}++impl BlockInspection {+ pub fn finish(self) -> (BlockStats, Vec<Moment>) {+ (self.counter.finish(), self.moments)+ }+}++impl SessionInspector {+ pub fn observe_message(&mut self, block: &mut BlockInspection, message: &Message) {+ let window = message.window_id.clone().unwrap_or_default();+ if let Some(id) = message.window_id.as_deref() {+ let ids = &mut block.counter.stats.window_ids;+ if !message.events.is_empty()+ && ids.len() < MAX_WINDOW_IDS+ && !ids.iter().any(|w| w == id)+ {+ ids.push(id.to_owned());+ }+ }+ for event in &message.events {+ self.observe(block, &message.inner, event, &window);+ }+ }++ fn observe(+ &mut self,+ block: &mut BlockInspection,+ inner: &[u8],+ event: &Event,+ window: &Arc<str>,+ ) {+ if !self.decided {+ match event.kind {+ Some(META | FULL_SNAPSHOT) => {+ self.decided = true;+ block.counter.stats.events_before_first_full_snapshot +=+ std::mem::take(&mut self.undecided);+ }+ Some(INCREMENTAL_SNAPSHOT) => {+ self.decided = true;+ self.cold = true;+ self.undecided = 0;+ }+ _ => {}+ }+ }+ let index = block.events;+ block.events += 1;+ let navigations = block.counter.stats.navigations;+ let carried_href =+ block+ .counter+ .observe(inner, event, &mut self.last_href, &mut self.activity);+ let navigated = block.counter.stats.navigations > navigations;++ let waiting = block.counter.stats.events_before_first_full_snapshot;+ let observed = self+ .dom+ .observe(inner, event, window, &mut block.counter.stats);+ if event.kind == Some(FULL_SNAPSHOT) {+ self.cold = false;+ }+ let stats = &mut block.counter.stats;+ if !self.decided {+ self.undecided += stats.events_before_first_full_snapshot - waiting;+ stats.events_before_first_full_snapshot = waiting;+ }+ if self.cold {+ // Events before the snapshot that this process never saw do not tell whether the session plays.+ stats.events_before_first_full_snapshot = waiting;+ stats.cold_start = true;+ }++ // `session-feature-recorder.ts` resets dead click state on every page URL, changed or not.+ if carried_href {+ self.clicks.navigated();+ }+ let ts = event.timestamp_ms.unwrap_or_default();+ let mut moment = match event.kind {+ // A reader seeks to a full snapshot only when it is one the inspector could read.+ Some(FULL_SNAPSHOT) if observed.full_snapshot => Some("full_snapshot"),+ Some(FULL_SNAPSHOT) => Some("full_snapshot_undecoded"),+ Some(META | CUSTOM) if navigated => Some("navigation"),+ Some(INCREMENTAL_SNAPSHOT)+ if event.source == Some(MUTATION) && observed.changed >= LARGE_DOM_CHANGE_NODES =>+ {+ Some("dom_change")+ }+ _ => None,+ };+ if event.kind == Some(INCREMENTAL_SNAPSHOT) && event.source == Some(MOUSE_INTERACTION) {+ if let Some(data) = event.data {+ let [interaction, x, y] = fields(inner, data, [b"type", b"x", b"y"]);+ if number(inner, interaction).is_some_and(|t| is_click(t as u8)) {+ let position = number(inner, x).zip(number(inner, y));+ match self.clicks.click(ts, position) {+ ClickKind::Rage => stats.rage_clicks += 1,+ ClickKind::AfterDead => stats.dead_clicks += 1,+ ClickKind::Plain => {}+ }+ moment = Some("click");+ }+ }+ }+ if let Some(kind) = moment {+ if block.moments.len() < MAX_MOMENTS_PER_BLOCK {+ block.moments.push(Moment {+ kind,+ window_id: Arc::clone(window),+ ts_ms: ts as i64,+ event_index: index,+ });+ } else {+ stats.moments_truncated = true;+ }+ }+ }+}++#[cfg(test)]+mod tests {+ use serde_json::{json, Value};++ use super::*;+ use crate::inspect::stats::fold_session;+ use crate::scan::message::{read_message, Message};++ fn message(items: Value) -> Message {+ message_in(Some("w1"), items)+ }++ fn message_in(window: Option<&str>, items: Value) -> Message {+ let mut properties = json!({"$session_id": "s", "$snapshot_items": items});+ if let Some(window) = window {+ properties["$window_id"] = json!(window);+ }+ let inner = json!({"event": "$snapshot_items", "properties": properties});+ read_message(+ json!({"distinct_id": "d", "data": inner.to_string()})+ .to_string()+ .into_bytes(),+ )+ .unwrap()+ }++ fn inspect(session: &mut SessionInspector, message: &Message) -> (BlockStats, Vec<Moment>) {+ let mut block = BlockInspection::default();+ session.observe_message(&mut block, message);+ block.finish()+ }++ fn big_mutation(ts: i64) -> Value {+ let adds: Vec<Value> = (0..100)+ .map(|i| json!({"parentId": 1, "node": {"type": 3, "id": 1000 + i}}))+ .collect();+ json!({"type": 3, "timestamp": ts, "data": {"source": 0, "adds": adds, "removes": [], "texts": [], "attributes": []}})+ }++ #[test]+ fn records_moments_where_a_reader_can_seek() {+ let click = |ts: i64| json!({"type": 3, "timestamp": ts, "data": {"source": 2, "type": 2, "x": 5, "y": 5}});+ let first = message(json!([+ {"type": 4, "timestamp": 1, "data": {"href": "https://app.example.com/a"}},+ {"type": 2, "timestamp": 2, "data": {"node": {"type": 0, "childNodes": [], "id": 1}}},+ click(3),+ big_mutation(4),+ {"type": 2, "timestamp": 5, "data": "not gzip", "cv": "2024-10"},+ ]));+ let mut session = SessionInspector::default();+ let (stats, moments) = inspect(&mut session, &first);+ let kinds: Vec<_> = moments.iter().map(|m| (m.kind, m.event_index)).collect();+ assert_eq!(+ kinds,+ vec![+ ("navigation", 0),+ ("full_snapshot", 1),+ ("click", 2),+ ("dom_change", 3),+ ("full_snapshot_undecoded", 4)+ ]+ );+ let window = first.window_id.as_ref().unwrap();+ assert!(moments.iter().all(|m| Arc::ptr_eq(&m.window_id, window)));+ assert!(!stats.cold_start && !stats.moments_truncated);++ // The next flush continues the run of clicks the previous block started.+ let second = message(json!([click(100), click(200)]));+ assert_eq!(inspect(&mut session, &second).0.rage_clicks, 1);++ let crowded = message(json!((0..=MAX_MOMENTS_PER_BLOCK as i64)+ .map(|i| click(1_000 + i * 2_000))+ .collect::<Vec<_>>()));+ let (stats, moments) = inspect(&mut session, &crowded);+ assert_eq!(moments.len(), MAX_MOMENTS_PER_BLOCK);+ assert!(stats.moments_truncated);+ }++ #[test]+ fn keeps_the_first_window_ids_a_block_sees() {+ let moved = json!([{"type": 3, "timestamp": 1, "data": {"source": 1, "positions": []}}]);+ let mut block = BlockInspection::default();+ let mut session = SessionInspector::default();+ // A message without a window, and an empty one, add nothing.+ session.observe_message(&mut block, &message_in(None, moved.clone()));+ session.observe_message(&mut block, &message_in(Some("w-empty"), json!([])));+ for window in (0..=MAX_WINDOW_IDS).chain([0]) {+ let window = format!("w{window}");+ session.observe_message(&mut block, &message_in(Some(&window), moved.clone()));+ }+ let windows: Vec<String> = (0..MAX_WINDOW_IDS).map(|w| format!("w{w}")).collect();+ assert_eq!(block.finish().0.window_ids, windows);+ }++ #[test]+ fn counts_spa_navigations_and_dead_clicks_as_the_feature_recorder_does() {+ let meta = |ts: i64, path: &str| json!({"type": 4, "timestamp": ts, "data": {"href": format!("https://app.example.com{path}")}});+ let url_changed = |ts: i64, path: &str| json!({"type": 5, "timestamp": ts, "data": {"tag": "$url_changed", "payload": {"href": format!("https://app.example.com{path}")}}});+ let click = |ts: i64, x: i64| json!({"type": 3, "timestamp": ts, "data": {"source": 2, "type": 2, "x": x, "y": 5}});+ // (events, navigations, dead clicks), traced through `session-feature-recorder.ts`.+ let cases = [+ (+ json!([+ meta(1, "/"),+ click(2, 5),+ url_changed(3, "/next"),+ click(5000, 500),+ meta(6000, "/"),+ click(9000, 5)+ ]),+ 3,+ 0,+ ),+ // The same URL again still means the click did something.+ (+ json!([meta(1, "/"), click(2, 5), meta(3, "/"), click(5000, 500)]),+ 1,+ 0,+ ),+ (json!([meta(1, "/"), click(2, 5), click(5000, 500)]), 1, 1),+ ];+ for (items, navigations, dead_clicks) in cases {+ let (stats, _) = inspect(&mut SessionInspector::default(), &message(items.clone()));+ assert_eq!(+ (stats.navigations, stats.dead_clicks),+ (navigations, dead_clicks),+ "{items}"+ );+ }+ }++ #[test]+ fn a_custom_event_before_the_first_meta_is_not_a_cold_start() {+ let session = message(json!([+ {"type": 5, "timestamp": 1, "data": {"tag": "$session_id_change", "payload": {}}},+ {"type": 4, "timestamp": 2, "data": {"href": "https://app.example.com/a"}},+ {"type": 2, "timestamp": 3, "data": {"node": {"type": 0, "childNodes": [], "id": 1}}},+ ]));+ let (stats, _) = inspect(&mut SessionInspector::default(), &session);+ assert!(!stats.cold_start);+ assert_eq!(stats.events_before_first_full_snapshot, 2);+ }++ #[test]+ fn a_sessions_active_time_does_not_depend_on_how_its_events_split_into_blocks() {+ // Mouse moves are active and plugin events are not.+ let moved =+ |ts: i64| json!({"type": 3, "timestamp": ts, "data": {"source": 1, "positions": []}});+ let logged = |ts: i64| json!({"type": 6, "timestamp": ts, "data": {"plugin": "rrweb/console@1", "payload": {}}});+ // Segments 1000 to 4000, 8500 alone and 20000 to 21000, as one block.+ let whole = [vec![+ moved(1000),+ moved(3000),+ logged(4000),+ moved(8500),+ moved(20000),+ logged(21000),+ ]];+ let split = [+ vec![moved(1000), moved(3000)],+ vec![logged(4000), moved(8500), moved(20000), logged(21000)],+ ];+ // A second window's message comes late and overlaps the block before it.+ let overlapping = [+ vec![moved(1000), logged(4000)],+ vec![moved(3000)],+ vec![moved(8500), moved(20000), logged(21000)],+ ];+ let blocks: [&[Vec<Value>]; 3] = [&whole, &split, &overlapping];+ for messages in blocks {+ let mut session = SessionInspector::default();+ let stats = messages+ .iter()+ .map(|items| inspect(&mut session, &message(json!(items))).0)+ .collect();+ assert_eq!(fold_session(stats).active_ms, 4001, "{messages:?}");+ }+ }++ #[test]+ fn marks_a_session_this_process_joined_midway_as_a_cold_start() {+ let mid_session = message(json!([+ big_mutation(1),+ {"type": 3, "timestamp": 2, "data": {"source": 1, "positions": []}},+ ]));+ let (stats, _) = inspect(&mut SessionInspector::default(), &mid_session);+ assert!(stats.cold_start);+ // Nothing here says the session cannot play: its snapshot came before this process saw it.+ assert_eq!(+ (+ stats.events_before_first_full_snapshot,+ stats.orphan_mutations+ ),+ (0, 0)+ );+ }+}diff --git a/rust/ai-research/replay-core-v4/src/inspect/stats.rs b/rust/ai-research/replay-core-v4/src/inspect/stats.rsindex 1b8219a66bd4..732645c5ab41 100644--- a/rust/ai-research/replay-core-v4/src/inspect/stats.rs+++ b/rust/ai-research/replay-core-v4/src/inspect/stats.rs@@ -3,6 +3,8 @@/// Fingerprints a block or session row keeps. A session row with this many has visited this many pages or more.pub const MAX_URL_FINGERPRINTS: usize = 10;+/// Window IDs a block or session row keeps. A session row with this many has this many windows or more.+pub const MAX_WINDOW_IDS: usize = 32;/// The block index columns from `replay-data.md`, apart from the location columns the writer adds./// The order is only a tie-break that makes [`fold_session`] independent of the order of its input.@@ -59,6 +61,9 @@ pub struct BlockStats {/// The distinct pages the block saw, at most [`MAX_URL_FINGERPRINTS`] in the order it first saw them. Each is a/// [`crate::refs::url_fingerprint`] under the session's fingerprint key, so it compares only within the session.pub url_fingerprints: Vec<String>,+ /// The distinct `$window_id` values of the block's messages, at most [`MAX_WINDOW_IDS`] in the order it first saw+ /// them. A window spans blocks, so a session's window count is the length of its folded list.+ pub window_ids: Vec<String>,pub console_errors: u32,pub console_warnings: u32,pub network_requests: u32,@@ -112,14 +117,12 @@ impl BlockStats {self.max_written_event_bytes = self.max_written_event_bytes.max(later.max_written_event_bytes);- for fingerprint in &later.url_fingerprints {- if self.url_fingerprints.len() >= MAX_URL_FINGERPRINTS {- break;- }- if !self.url_fingerprints.contains(fingerprint) {- self.url_fingerprints.push(fingerprint.clone());- }- }+ union(+ &mut self.url_fingerprints,+ &later.url_fingerprints,+ MAX_URL_FINGERPRINTS,+ );+ union(&mut self.window_ids, &later.window_ids, MAX_WINDOW_IDS);self.touch_input |= later.touch_input;self.set_viewport(later.viewport_ts_ms,@@ -182,6 +185,18 @@ pub fn fold_session(mut blocks: Vec<BlockStats>) -> BlockStats {session}+/// Appends the values of `later` that `values` does not hold, up to `cap` values in all.+fn union(values: &mut Vec<String>, later: &[String], cap: usize) {+ for value in later {+ if values.len() >= cap {+ break;+ }+ if !values.contains(value) {+ values.push(value.clone());+ }+ }+}+fn min_option(a: Option<i64>, b: Option<i64>) -> Option<i64> {match (a, b) {(Some(a), Some(b)) => Some(a.min(b)),@@ -210,6 +225,9 @@ mod tests {mouse_interactions: 2,touch_moves: 3,url_fingerprints: pages.collect(),+ window_ids: (first / 1_000..first / 1_000 + 20)+ .map(|w| format!("w{w}"))+ .collect(),written_jsonl_bytes: 500,max_written_event_bytes: first as u64 / 10,..BlockStats::default()@@ -253,6 +271,9 @@ mod tests {.map(|page| format!("{page:016x}")).collect();assert_eq!(session.url_fingerprints, pages);+ // Windows 1 to 20, 3 to 22 and 64 to 83: the first 32 distinct, in the order the session first saw them.+ let windows: Vec<String> = (1..=22).chain(64..=73).map(|w| format!("w{w}")).collect();+ assert_eq!(session.window_ids, windows);assert_eq!((session.first_ts_ms, session.last_ts_ms),(Some(1_000), Some(65_000))