: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
refactor(ai-research-v4): own the v4 ref grammar in replay-core-v4 (#128360)
Nicholas Waltz1ddb3b874476
diff --git a/products/ai_training/docs/replay-data.md b/products/ai_training/docs/replay-data.mdindex 4fc6704a49fe..08ad44f576e9 100644--- a/products/ai_training/docs/replay-data.md+++ b/products/ai_training/docs/replay-data.md@@ -475,7 +475,7 @@ The recorder streams each block into the encoder. Read a block as a stream: a Br- A remote image URL becomes `imageurl:v4:{team_id}:{YYYY-MM}:{hash}` in the block and in its fetch job, where the month is the UTC month of the write. The block and the job always carry the same reference, so a reader opens the fetched image directly.- A URL that the URL policy refuses becomes `imageurl:v4:declined:{reason}` in the block, for example `declined:credential`, `declined:non_public_host` or `declined:tracking_beacon`. The original URL cannot be recovered. The recorder counts rewrites and refusals in `ai_research_v4_recorder_image_url_rewrites_total`.- Each `src` URL of an `@font-face` rule becomes a font reference in the same way, as [Web fonts](#web-fonts) describes. `ai_research_v4_recorder_image_url_rewrites_total` counts both, by `kind`: `image`, `font` or `bundled_font`.-- The `v4_image_refs` module of the `posthog-replay-anonymizer` crate defines each reference and marker shape, for images and for fonts. The recorder writes them with it. A reader that must tell them apart from page text uses its `parse` function, which accepts only the exact strings the recorder writes.+- The `v4_image_refs` module of the `replay-core-v4` crate defines each reference and marker shape, for images and for fonts. The recorder writes them with it. A reader that must tell them apart from page text uses its `parse` function, which accepts only the exact strings the recorder writes.- The rewrite covers every remote URL that the recorder reads from an image slot: image sources, `srcset`, SVG image links, video posters, `<picture>` sources and CSS `url(...)` in style attributes, style sheets and style rules, in custom properties too, because a page passes a custom property to an image property through `var()`. It also covers `src`, `srcset`, `href` and `poster` in an attribute change on a node the recorder does not know, for example after a restart. `srcset` keeps its descriptors and CSS keeps its `url(...)` syntax. A URL is also replaced where a selector holds it with CSSOM's escapes, by the reference with the same escapes, so a class such as `bg-[url(https://cdn.example.com/a.png)]` and its rule `.bg-\[url\(https\:\/\/cdn\.example\.com\/a\.png\)\]` still match within a block. A reader that replaces references by text must replace the escaped form as well. It is not limited by the 512 URLs per message that get fetch jobs.- A srcset in an image slot keeps one candidate: the one that Chromium shows at a device pixel ratio of 1, which is the ratio the screenshotter renders at. Of `x` descriptors, that is the smallest density at or above 1x, or the largest density when all are below 1x. A `w` descriptor gives a density only against the layout width, which the recorder does not know, so of `w` descriptors the recorder keeps the v3 choice: the largest at or below 1024w, or the smallest when all are wider. The first candidate wins a tie. Only that candidate gets a fetch job, because the renderer has no network access and shows only fetched images. A srcset that mixes `w` and `x` descriptors, or that v3 would read as malformed, keeps no candidate.- The kept candidate stays only when it can show offline: a URL that the URL policy accepts, or an inline image that the image scan keeps. Otherwise the srcset is empty, and no other candidate takes its place, because no other candidate has a fetch job. A candidate that leaves becomes no `declined` marker.diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex bbf2a05aae71..7abb87bef810 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -9832,7 +9832,7 @@ dependencies = [[[package]]name = "posthog-replay-anonymizer"-version = "0.14.0"+version = "0.15.0"dependencies = ["ahash 0.8.12","anyhow",@@ -11105,7 +11105,6 @@ dependencies = ["memchr","moka","percent-encoding",- "posthog-replay-anonymizer","public-suffix","rand 0.8.5","serde",@@ -11153,7 +11152,6 @@ dependencies = ["lifecycle","metrics","moka",- "posthog-replay-anonymizer","rand 0.8.5","rdkafka","replay-core-v4",diff --git a/rust/ai-research/replay-core-v4/Cargo.toml b/rust/ai-research/replay-core-v4/Cargo.tomlindex 9c1772771c2d..417445a69d11 100644--- a/rust/ai-research/replay-core-v4/Cargo.toml+++ b/rust/ai-research/replay-core-v4/Cargo.toml@@ -32,7 +32,6 @@ md5 = "0.8"memchr = "2"moka = { workspace = true, optional = true }percent-encoding = { workspace = true }-posthog-replay-anonymizer = { path = "../../replay-anonymizer", default-features = false }public-suffix = { workspace = true }rand = { workspace = true, optional = true }serde = { workspace = true }diff --git a/rust/ai-research/replay-core-v4/src/fetched_image.rs b/rust/ai-research/replay-core-v4/src/fetched_image.rsindex b9d1d2aaafe4..5d285e6fd3c6 100644--- a/rust/ai-research/replay-core-v4/src/fetched_image.rs+++ b/rust/ai-research/replay-core-v4/src/fetched_image.rs@@ -2,7 +2,7 @@//! `replay-core-v4-node`, and a reader rebuilds its context from the file's URL reference. A public font file belongs//! to no team, so the fetcher stores it unsealed.-use posthog_replay_anonymizer::v4_image_refs::{parse, Parsed, Ref, RefKind};+use crate::v4_image_refs::{parse, Parsed, Ref, RefKind};use crate::envelope::{seal_frame, Codec, Context, EnvelopeError, Version};use crate::refs::{public_font_url_ref, reference_hash, UrlAsset};diff --git a/rust/ai-research/replay-core-v4/src/lib.rs b/rust/ai-research/replay-core-v4/src/lib.rsindex a4dee6991b87..2a6efdf81c64 100644--- a/rust/ai-research/replay-core-v4/src/lib.rs+++ b/rust/ai-research/replay-core-v4/src/lib.rs@@ -16,3 +16,4 @@ pub mod scan;pub mod seal;pub mod session;pub mod url_policy;+pub mod v4_image_refs;diff --git a/rust/ai-research/replay-core-v4/src/refs.rs b/rust/ai-research/replay-core-v4/src/refs.rsindex 8f53469944b1..76d0fb353d96 100644--- a/rust/ai-research/replay-core-v4/src/refs.rs+++ b/rust/ai-research/replay-core-v4/src/refs.rs@@ -1,16 +1,15 @@-//! The keyed hash of the image and URL references in `posthog_replay_anonymizer::v4_image_refs`, the asset of a URL reference, and the URL fingerprints of the block index.+//! The keyed hash of the image and URL references in `crate::v4_image_refs`, the asset of a URL reference, and the URL fingerprints of the block index.-use base64::engine::general_purpose::URL_SAFE_NO_PAD;-use base64::Engine;-use hmac::{Hmac, Mac};-use posthog_replay_anonymizer::v4_image_refs::{+use crate::v4_image_refs::{font_url_declined_marker, font_url_ref, image_url_ref, public_font_ref, url_declined_marker,HASH_CHARS,};-use posthog_replay_anonymizer::Decline;+use base64::engine::general_purpose::URL_SAFE_NO_PAD;+use base64::Engine;+use hmac::{Hmac, Mac};use sha2::{Digest, Sha256};-use crate::url_policy::CanonicalUrl;+use crate::url_policy::{CanonicalUrl, Decline};/// 64 bits of the HMAC: enough to tell the pages of one session apart.const FINGERPRINT_BYTES: usize = 8;diff --git a/rust/ai-research/replay-core-v4/src/scan/cv.rs b/rust/ai-research/replay-core-v4/src/scan/cv.rsindex 89445932a52f..09c91e2273af 100644--- a/rust/ai-research/replay-core-v4/src/scan/cv.rs+++ b/rust/ai-research/replay-core-v4/src/scan/cv.rs@@ -10,7 +10,7 @@ use crate::compression;use serde::de::IgnoredAny;use super::canvas::{self, CanvasImage};-use posthog_replay_anonymizer::v4_image_refs::ImageDecline;+use crate::v4_image_refs::ImageDecline;use super::images::{extract_images, ImageSink};use super::message::{canonicalize_keys, Event, Message};diff --git a/rust/ai-research/replay-core-v4/src/scan/images.rs b/rust/ai-research/replay-core-v4/src/scan/images.rsindex 8416b50b8a2b..f5d9c9987d53 100644--- a/rust/ai-research/replay-core-v4/src/scan/images.rs+++ b/rust/ai-research/replay-core-v4/src/scan/images.rs@@ -14,7 +14,7 @@ use base64::engine::general_purpose::{GeneralPurpose, GeneralPurposeConfig};use base64::engine::DecodePaddingMode;use base64::Engine;-use posthog_replay_anonymizer::v4_image_refs::{image_ref, ImageDecline};+use crate::v4_image_refs::{image_ref, ImageDecline};use crate::bytescan as scan;use crate::refs::reference_hash;diff --git a/rust/ai-research/replay-core-v4/src/url_policy.rs b/rust/ai-research/replay-core-v4/src/url_policy.rsindex 69fad8961f2d..8475d397f528 100644--- a/rust/ai-research/replay-core-v4/src/url_policy.rs+++ b/rust/ai-research/replay-core-v4/src/url_policy.rs@@ -312,6 +312,18 @@ pub enum Decline {}impl Decline {+ pub const ALL: [Decline; 9] = [+ Decline::TooLong,+ Decline::NotAbsolute,+ Decline::BadScheme,+ Decline::BadPort,+ Decline::NoHost,+ Decline::NonPublicHost,+ Decline::Credential,+ Decline::InvalidQuery,+ Decline::TrackingBeacon,+ ];+/// True for a URL the policy refuses because nobody wants it fetched, not because it is/// malformed or unsafe. A consumer that reads jobs from a queue drops only that job for such a/// decline, and rejects the whole record for every other one.@@ -319,24 +331,19 @@ impl Decline {matches!(self, Decline::TrackingBeacon)}- /// The metric label. Stable, because a dashboard and an alert both key on it.+ /// The metric label, and the reason in a v4 URL decline marker. Stable, because a dashboard, an+ /// alert and a reader of v4 blocks all key on it.pub fn label(self) -> &'static str {- posthog_replay_anonymizer::Decline::from(self).label()- }-}--impl From<Decline> for posthog_replay_anonymizer::Decline {- fn from(decline: Decline) -> Self {- match decline {- Decline::TooLong => Self::TooLong,- Decline::NotAbsolute => Self::NotAbsolute,- Decline::BadScheme => Self::BadScheme,- Decline::BadPort => Self::BadPort,- Decline::NoHost => Self::NoHost,- Decline::NonPublicHost => Self::NonPublicHost,- Decline::Credential => Self::Credential,- Decline::InvalidQuery => Self::InvalidQuery,- Decline::TrackingBeacon => Self::TrackingBeacon,+ match self {+ Decline::TooLong => "too_long",+ Decline::NotAbsolute => "not_absolute",+ Decline::BadScheme => "bad_scheme",+ Decline::BadPort => "bad_port",+ Decline::NoHost => "no_host",+ Decline::NonPublicHost => "non_public_host",+ Decline::Credential => "credential",+ Decline::InvalidQuery => "invalid_query",+ Decline::TrackingBeacon => "tracking_beacon",}}}diff --git a/rust/replay-anonymizer/src/v4_image_refs.rs b/rust/ai-research/replay-core-v4/src/v4_image_refs.rssimilarity index 100%rename from rust/replay-anonymizer/src/v4_image_refs.rsrename to rust/ai-research/replay-core-v4/src/v4_image_refs.rsdiff --git a/rust/ai-research/replay-core-v4/tests/v4_vector.rs b/rust/ai-research/replay-core-v4/tests/v4_vector.rsindex e438fb138e13..24b39cae921b 100644--- a/rust/ai-research/replay-core-v4/tests/v4_vector.rs+++ b/rust/ai-research/replay-core-v4/tests/v4_vector.rs@@ -1,6 +1,5 @@use base64::engine::general_purpose::STANDARD;use base64::Engine;-use posthog_replay_anonymizer::v4_image_refs::{image_ref, image_url_ref};use replay_core_v4::envelope::{open_frame, seal_frame_with_nonce, seal_json_envelope_with_nonce, Codec, Context, Version,};@@ -10,6 +9,7 @@ use replay_core_v4::seal::{image_content_key, image_url_key, open_session_key, seal_session_key_with_nonce,url_fingerprint_key, SealedKey, WrappingContext,};+use replay_core_v4::v4_image_refs::{image_ref, image_url_ref};use serde_json::Value;// Written with Node's crypto from the spec in replay-data.md, so this checks the Rust code against an independentdiff --git a/rust/ai-research/replay-recorder-v4/Cargo.toml b/rust/ai-research/replay-recorder-v4/Cargo.tomlindex 5586a2d1ed91..d10574b8ab99 100644--- a/rust/ai-research/replay-recorder-v4/Cargo.toml+++ b/rust/ai-research/replay-recorder-v4/Cargo.toml@@ -17,7 +17,6 @@ common-kafka = { path = "../../common/kafka" }common-metrics = { path = "../../common/metrics" }common-redis = { path = "../../common/redis" }lifecycle = { path = "../../common/lifecycle" }-posthog-replay-anonymizer = { path = "../../replay-anonymizer", default-features = false }replay-core-v4 = { path = "../replay-core-v4" }aho-corasick = { workspace = true }diff --git a/rust/ai-research/replay-recorder-v4/src/rewrite.rs b/rust/ai-research/replay-recorder-v4/src/rewrite.rsindex 5110ecaeb8a7..97a9d164a25c 100644--- a/rust/ai-research/replay-recorder-v4/src/rewrite.rs+++ b/rust/ai-research/replay-recorder-v4/src/rewrite.rs@@ -15,9 +15,6 @@ use std::collections::{BTreeMap, HashMap};use aho_corasick::{AhoCorasick, AhoCorasickKind, MatchKind};use metrics::counter;-use posthog_replay_anonymizer::v4_image_refs::{- bundled_font_ref, ImageDecline, FONT_URL_UNCHECKED_MARKER, IMAGE_DECLINED_PREFIX,-};use replay_core_v4::bytescan::{unescape, write_json_string, Span};use replay_core_v4::inspect::css::{escape_in_identifier, rewrite_font_src_functions, rewrite_functions, CssRewrite,@@ -28,6 +25,9 @@ use replay_core_v4::refs::{declined_url_ref, UrlAsset};use replay_core_v4::scan::tokens::{Token, Tokens};use replay_core_v4::seal::image_url_key;use replay_core_v4::url_policy::is_remote;+use replay_core_v4::v4_image_refs::{+ bundled_font_ref, ImageDecline, FONT_URL_UNCHECKED_MARKER, IMAGE_DECLINED_PREFIX,+};use crate::scrub::{Scrub, Scrubber};use crate::urls::url_reference;@@ -51,10 +51,7 @@ fn named_as(named.err().map(|decline| decline.label()),),(_, Ok((_, reference))) => (reference, None),- (_, Err(decline)) => (- declined_url_ref(asset, decline.into()),- Some(decline.label()),- ),+ (_, Err(decline)) => (declined_url_ref(asset, decline), Some(decline.label())),}}diff --git a/rust/ai-research/replay-recorder-v4/src/scrub.rs b/rust/ai-research/replay-recorder-v4/src/scrub.rsindex e7dc4a08a593..e96a78b0d018 100644--- a/rust/ai-research/replay-recorder-v4/src/scrub.rs+++ b/rust/ai-research/replay-recorder-v4/src/scrub.rs@@ -10,7 +10,7 @@use std::borrow::Cow;use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind};-use posthog_replay_anonymizer::v4_image_refs::URL_UNCHECKED_MARKER;+use replay_core_v4::v4_image_refs::URL_UNCHECKED_MARKER;/// Which URL tokens the scrub replaces.#[derive(Debug, Clone, Default, PartialEq, Eq)]diff --git a/rust/ai-research/replay-recorder-v4/src/write.rs b/rust/ai-research/replay-recorder-v4/src/write.rsindex b0824218165f..a99ff2bcc299 100644--- a/rust/ai-research/replay-recorder-v4/src/write.rs+++ b/rust/ai-research/replay-recorder-v4/src/write.rs@@ -10,7 +10,6 @@ use std::sync::Arc;use chrono::{DateTime, Utc};use futures::stream::{self, StreamExt, TryStreamExt};use metrics::counter;-use posthog_replay_anonymizer::v4_image_refs::ImageDecline;use rand::Rng;use replay_core_v4::bytescan::write_json_string;use replay_core_v4::envelope::{seal_frame, seal_json_envelope, Codec, Context, Version};@@ -22,6 +21,7 @@ use replay_core_v4::refs::url_fingerprint;use replay_core_v4::scan::cv::extract_event_images;use replay_core_v4::scan::images::{Image, ImageLimits, ImageSink};use replay_core_v4::seal::{image_content_key, url_fingerprint_key};+use replay_core_v4::v4_image_refs::ImageDecline;use tokio::sync::Semaphore;@@ -1000,9 +1000,7 @@ mod tests {json!({"type": 2, "tagName": "a", "attributes": {"href": link}, "childNodes": [], "id": 3}),json!({"type": 2, "tagName": "style", "attributes": {"_cssText": split_face}, "childNodes": [], "id": 4}),];- use posthog_replay_anonymizer::v4_image_refs::{- FONT_URL_UNCHECKED_MARKER, URL_UNCHECKED_MARKER,- };+ use replay_core_v4::v4_image_refs::{FONT_URL_UNCHECKED_MARKER, URL_UNCHECKED_MARKER};for (nodes, named, marker, other_marker) in [(images,@@ -1564,7 +1562,7 @@ mod tests {keys.sessions.insert(id, key);}keys.team_months.insert(7, [6; 32]);- let old_ref = posthog_replay_anonymizer::v4_image_refs::image_ref(+ let old_ref = replay_core_v4::v4_image_refs::image_ref(7,"2026-10",&replay_core_v4::refs::reference_hash(&image_content_key(&[6; 32]), &png(b"old")),diff --git a/rust/replay-anonymizer/Cargo.toml b/rust/replay-anonymizer/Cargo.tomlindex 187f1833d0c7..e5e2f36c374d 100644--- a/rust/replay-anonymizer/Cargo.toml+++ b/rust/replay-anonymizer/Cargo.toml@@ -1,6 +1,6 @@[package]name = "posthog-replay-anonymizer"-version = "0.14.0"+version = "0.15.0"edition = "2021"description = "PII scrubber for rrweb session-replay events: text/URL redaction, image collection for an out-of-band scrub, and canvas/cv neutralization"license = "MIT"diff --git a/rust/replay-anonymizer/README.md b/rust/replay-anonymizer/README.mdindex c38f9fe0083a..5793ad1201f8 100644--- a/rust/replay-anonymizer/README.md+++ b/rust/replay-anonymizer/README.md@@ -6,7 +6,6 @@ Consumers:- `replay-anonymizer-node` (this workspace) wraps it as a Neon addon for the Node ingestion workers.- The MLHog training pipelines consume it from crates.io.-- The v4 recorder crates (`replay-core-v4`, `replay-recorder-v4`) write their image and font references and decline markers with the `v4_image_refs` module, by path and with `default-features = false`. A reader of v4 blocks parses those strings with `v4_image_refs::parse`, so the writer and the reader share one definition.Behavior is pinned by the JSON fixtures under `tests/fixtures/`, shared with the Node addon's Jest suite. Scrubbing operates on untrusted input; the public entry points contain panics and convert them to errors, so callers fail closed (drop the message) without their own `catch_unwind`. Under `panic = "abort"` that backstop cannot run — builds that must fail closed need `panic = "unwind"`.diff --git a/rust/replay-anonymizer/src/lib.rs b/rust/replay-anonymizer/src/lib.rsindex c50708bbb690..180d30bf15ae 100644--- a/rust/replay-anonymizer/src/lib.rs+++ b/rust/replay-anonymizer/src/lib.rs@@ -19,7 +19,7 @@//! The stable crates.io surface is what this page documents: [`AllowLists`], [`Ctx`], the event//! entry points ([`anonymize_message`], [`anonymize_event`], [`anonymize_line`] and friends), the//! byte-buffer snapshot pipeline ([`anonymize_kafka_payload`] and friends), the rrweb routing-//! constants in [`event`], the v4 image references and markers in [`v4_image_refs`], and — behind+//! constants in [`event`], and — behind//! the default `typed-parse` feature — the typed rrweb parse in [`typed`]//! ([`parse_scrubbed_event`]). Everything `#[doc(hidden)]` stays `pub` only for this workspace//! (the Node addon, the parity tests) — it is internal and may change or disappear in any release.@@ -67,7 +67,6 @@ pub mod url;pub mod url_collect;#[doc(hidden)]pub mod url_policy;-pub mod v4_image_refs;#[doc(hidden)]pub mod value;diff --git a/rust/replay-anonymizer/src/url_policy.rs b/rust/replay-anonymizer/src/url_policy.rsindex 9b04c3dd0d84..955b2749195c 100644--- a/rust/replay-anonymizer/src/url_policy.rs+++ b/rust/replay-anonymizer/src/url_policy.rs@@ -312,18 +312,6 @@ pub enum Decline {}impl Decline {- pub const ALL: [Decline; 9] = [- Decline::TooLong,- Decline::NotAbsolute,- Decline::BadScheme,- Decline::BadPort,- Decline::NoHost,- Decline::NonPublicHost,- Decline::Credential,- Decline::InvalidQuery,- Decline::TrackingBeacon,- ];-/// True for a URL the policy refuses because nobody wants it fetched, not because it is/// malformed or unsafe. A consumer that reads jobs from a queue drops only that job for such a/// decline, and rejects the whole record for every other one.@@ -331,8 +319,7 @@ impl Decline {matches!(self, Decline::TrackingBeacon)}- /// The metric label, and the reason in a v4 URL decline marker. Stable, because a dashboard, an- /// alert and a reader of v4 blocks all key on it.+ /// The metric label. Stable, because a dashboard and an alert both key on it.pub fn label(self) -> &'static str {match self {Decline::TooLong => "too_long",