Skip to content

: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.

fix(replay-vision): read the block line shape recording-api actually serves (#102663)

Tue Haulund65dc0291d28e
diff --git a/products/replay_vision/backend/temporal/network_capture.py b/products/replay_vision/backend/temporal/network_capture.py
index 54c6b7b5aad3..d89e9339a61b 100644
--- a/products/replay_vision/backend/temporal/network_capture.py
+++ b/products/replay_vision/backend/temporal/network_capture.py
@@ -141,6 +141,26 @@ def parse_network_payload(lines: Iterable[str]) -> SessionNetworkPayload:
return collector.finish()
+def _events_from_line(parsed: Any) -> list[Any]:
+ """The rrweb events on one snapshot line.
+
+ A recording block stores one event per line as `[window_id, event]`. The snapshots API serves the same
+ events wrapped as `{"window_id": ..., "data": [event, ...]}`, so both are read rather than tying the
+ decoder to whichever source a caller happened to fetch from.
+ """
+ if isinstance(parsed, list):
+ # A pair is identified by its window id, not by its length: a list of two events also has two
+ # members, and reading that as a pair would drop the first event and any failure it carries.
+ if len(parsed) == 2 and isinstance(parsed[0], str) and isinstance(parsed[1], dict):
+ return [parsed[1]]
+ return []
+ if isinstance(parsed, dict):
+ events = parsed.get("data")
+ if isinstance(events, list):
+ return events
+ return []
+
+
def _iter_captured_requests(lines: Iterable[str]) -> Iterator[tuple[dict[str, Any], int]]:
"""Yield `(raw request, event timestamp)` for every network plugin event across the snapshot lines."""
for line in lines:
@@ -151,12 +171,7 @@ def _iter_captured_requests(lines: Iterable[str]) -> Iterator[tuple[dict[str, An
parsed = json.loads(line)
except (ValueError, TypeError):
continue
- if not isinstance(parsed, dict):
- continue
- events = parsed.get("data")
- if not isinstance(events, list):
- continue
- for event in events:
+ for event in _events_from_line(parsed):
yield from _iter_event_requests(event)
diff --git a/products/replay_vision/backend/tests/test_network_capture.py b/products/replay_vision/backend/tests/test_network_capture.py
index 6e0d4d7480f4..047ff8d108a0 100644
--- a/products/replay_vision/backend/tests/test_network_capture.py
+++ b/products/replay_vision/backend/tests/test_network_capture.py
@@ -10,7 +10,11 @@ from products.replay_vision.backend.temporal.network_capture import (
)
-def _line(*events: dict[str, Any], window_id: str = "w1") -> str:
+def _line(event: dict[str, Any], window_id: str = "w1") -> str:
+ return json.dumps([window_id, event])
+
+
+def _api_line(*events: dict[str, Any], window_id: str = "w1") -> str:
return json.dumps({"window_id": window_id, "data": list(events)})
@@ -77,12 +81,14 @@ class TestParseNetworkPayload:
_rrweb_event(
1000,
{"name": "https://app.test/a", "responseStatus": 200, "status": 503, "duration": 5},
- ),
+ )
+ ),
+ _line(
_rrweb_event(
2000,
{"name": "https://app.test/b", "status": 503, "responseStatus": 200, "duration": 5},
- ),
- )
+ )
+ ),
]
)
assert [request.status for request in payload.requests] == [503, 503]
@@ -169,6 +175,25 @@ class TestParseNetworkPayload:
assert captured_but_clean.captured
assert captured_but_clean.requests == []
+ def test_an_unrecognized_array_is_ignored_not_misread(self) -> None:
+ # A two-event list matches a [window_id, event] pair by length. Reading it as one would keep the
+ # second event and silently drop the first, inventing a request the line never described.
+ both = json.dumps(
+ [
+ _rrweb_event(1000, {"name": "https://app.test/first", "status": 500}),
+ _rrweb_event(2000, {"name": "https://app.test/second", "status": 503}),
+ ]
+ )
+ payload = parse_network_payload([both])
+ assert payload.requests == []
+ assert payload.captured is False
+
+ def test_reads_the_api_wrapper_shape_too(self) -> None:
+ payload = parse_network_payload(
+ [_api_line(_rrweb_event(1000, {"name": "https://app.test/api/x", "status": 503}))]
+ )
+ assert [request.url for request in payload.requests] == ["https://app.test/api/x"]
+
def test_survives_corrupt_lines_and_unusable_requests(self) -> None:
payload = parse_network_payload(
[