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(notebooks): treat client-ahead conflicts as stale instead of empty-step loops (#59702)

Alex Liderf501b9a90b0b
diff --git a/products/notebooks/backend/collab.py b/products/notebooks/backend/collab.py
index cba1d2e140a1..1c4edf08aed4 100644
--- a/products/notebooks/backend/collab.py
+++ b/products/notebooks/backend/collab.py
@@ -139,6 +139,11 @@ def submit_steps(
def _fetch_missed_steps(stream_key: str, *, last_seen_version: int, current_stream_version: int) -> SubmitResult:
+ # Client is somehow ahead of the stream — no missed range we could send.
+ # The only safe response is "reload the notebook".
+ if current_stream_version < last_seen_version:
+ return SubmitResult(status="stale", version=current_stream_version)
+
client = redis_module.get_client()
raw = client.xrange(stream_key, min=f"({last_seen_version}-0", max=f"{current_stream_version}-0")
diff --git a/products/notebooks/backend/test/test_collab.py b/products/notebooks/backend/test/test_collab.py
index 1d05e31b5517..8a507c2b1391 100644
--- a/products/notebooks/backend/test/test_collab.py
+++ b/products/notebooks/backend/test/test_collab.py
@@ -170,6 +170,31 @@ class TestNotebookCollab(BaseTest):
assert result.status == "accepted"
assert result.version == 947
+ def test_non_empty_stream_with_client_ahead_returns_stale(self):
+ # Stream's latest version is below the client's baseline — there is no missed range to
+ # rebase against. Verify we return `stale` so the client opens the conflict modal
+ # instead of looping on conflict-with-empty-steps.
+ submit_steps(
+ self.team.pk,
+ "nb_client_ahead_nonempty",
+ "client1",
+ [{"stepType": "replace", "from": 0, "to": 0}],
+ 0,
+ last_saved_version=0,
+ )
+
+ result = submit_steps(
+ self.team.pk,
+ "nb_client_ahead_nonempty",
+ "client_stale",
+ [{"stepType": "replace", "from": 1, "to": 1}],
+ last_seen_version=5,
+ last_saved_version=5,
+ )
+ assert result.status == "stale"
+ assert result.version == 1
+ assert result.steps_since is None
+
def test_stream_maxlen_constant_is_sane(self):
# Sanity: MAXLEN must comfortably hold an hour of edits. Adjust deliberately if changed.
assert STREAM_MAX_LENGTH >= 1000