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): ignore stale notebook responses (#65714)

Marius Andra2baec6797ac3
diff --git a/frontend/src/scenes/notebooks/Notebook/markdownNotebookEditorState.test.ts b/frontend/src/scenes/notebooks/Notebook/markdownNotebookEditorState.test.ts
index 3647b742292d..0192f527a537 100644
--- a/frontend/src/scenes/notebooks/Notebook/markdownNotebookEditorState.test.ts
+++ b/frontend/src/scenes/notebooks/Notebook/markdownNotebookEditorState.test.ts
@@ -204,6 +204,50 @@ Thinking...`)
expect(logic.values.notebookPresenceParticipants).toHaveLength(1)
})
+ it('does not let an older save response roll back newer notebook content', () => {
+ const version2Markdown = `${BASE_MARKDOWN}\n\nnewer save`
+ const version3Markdown = `${BASE_MARKDOWN}\n\nnewest save`
+ const version2Notebook = {
+ ...cachedNotebook,
+ version: 2,
+ content: buildMarkdownNotebookContent(version2Markdown),
+ text_content: version2Markdown,
+ }
+ const version3Notebook = {
+ ...cachedNotebook,
+ version: 3,
+ content: buildMarkdownNotebookContent(version3Markdown),
+ text_content: version3Markdown,
+ }
+
+ logic.actions.saveNotebookSuccess(version3Notebook)
+ logic.actions.saveNotebookSuccess(version2Notebook)
+
+ expect(logic.values.notebook?.version).toBe(3)
+ expect(logic.values.notebook?.content).toEqual(version3Notebook.content)
+ })
+
+ it('keeps the highest notebook version during shuffled remote update bursts', () => {
+ const shuffledRemoteNotebooks = [6, 4, 2, 3, 5, 7].map((version) => {
+ const markdown = `${BASE_MARKDOWN}\n\nremote update ${version}`
+ return {
+ ...cachedNotebook,
+ version,
+ content: buildMarkdownNotebookContent(markdown),
+ text_content: markdown,
+ }
+ })
+
+ for (const notebook of shuffledRemoteNotebooks) {
+ logic.actions.loadNotebookSuccess(notebook)
+ logic.actions.applyRemoteNotebookContent(notebook.content, notebook.version)
+ }
+
+ const latestNotebook = shuffledRemoteNotebooks[shuffledRemoteNotebooks.length - 1]
+ expect(logic.values.notebook?.version).toBe(latestNotebook.version)
+ expect(logic.values.notebook?.content).toEqual(latestNotebook.content)
+ })
+
it('does not surface legacy left-column state for markdown notebooks', () => {
logic.unmount()
logic = notebookLogic({ shortId: SHORT_ID, mode: 'notebook' })
diff --git a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts b/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts
index e66a4ef59ddd..b9376e106fc9 100644
--- a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts
+++ b/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts
@@ -132,6 +132,14 @@ function getNotebookTextContent(content: JSONContent | null | undefined, editorT
return getMarkdownNotebookTextContent(content) ?? editorText
}
+function keepNewestNotebookResponse(current: NotebookType | null, incoming: NotebookType | null): NotebookType | null {
+ if (!current || !incoming || current.short_id !== incoming.short_id) {
+ return incoming
+ }
+
+ return incoming.version < current.version ? current : incoming
+}
+
export type NotebookLogicMode = 'notebook' | 'canvas'
/** A markdown notebook update from the collab SSE stream or a 409 conflict body. */
@@ -905,8 +913,10 @@ export const notebookLogic = kea<notebookLogicType>([
// Extends the loader reducer: canonical remote states (streamed diffs, 409 replays)
// land in `notebook` without a refetch.
notebook: {
+ loadNotebookSuccess: (state, { notebook }) => keepNewestNotebookResponse(state, notebook),
+ saveNotebookSuccess: (state, { notebook }) => keepNewestNotebookResponse(state, notebook),
applyRemoteNotebookContent: (state, { content, version }) =>
- state ? { ...state, content, version } : state,
+ state && version > state.version ? { ...state, content, version } : state,
},
}),
selectors({