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(workflows): stop editor infinite update loop on graph rebuilds (#69698)

Harley Alexander7b55ace4163f
diff --git a/frontend/src/lib/components/CyclotronJob/integrations/IntegrationChoice.test.tsx b/frontend/src/lib/components/CyclotronJob/integrations/IntegrationChoice.test.tsx
index 396974d97e56..e70cc39b1845 100644
--- a/frontend/src/lib/components/CyclotronJob/integrations/IntegrationChoice.test.tsx
+++ b/frontend/src/lib/components/CyclotronJob/integrations/IntegrationChoice.test.tsx
@@ -49,6 +49,32 @@ describe('IntegrationChoice', () => {
expect(screen.queryByText(/no longer available/)).not.toBeInTheDocument()
})
+ it('auto-selects the first integration only once, even when the written value lags re-renders', async () => {
+ // The consumer's write can take a full state round-trip before it flows back into the
+ // `value` prop (e.g. the workflow editor rebuilds its graph first). Re-firing the
+ // auto-select on every render in that window dispatches an update per render, which can
+ // amplify into an infinite update loop (React #185).
+ const onChange = jest.fn()
+ const view = render(
+ <Provider>
+ <IntegrationChoice integration="github" onChange={onChange} />
+ </Provider>
+ )
+
+ await waitFor(() => {
+ expect(onChange).toHaveBeenCalledWith(1)
+ })
+
+ // Value prop still unset, as during the consumer's write round-trip. rerender flushes
+ // effects synchronously, so a repeat dispatch would have happened by the assertion.
+ view.rerender(
+ <Provider>
+ <IntegrationChoice integration="github" onChange={onChange} />
+ </Provider>
+ )
+ expect(onChange).toHaveBeenCalledTimes(1)
+ })
+
it('still warns when the stored id matches no integration', async () => {
// Regression guard: a genuinely dangling reference must keep surfacing the banner.
render(
diff --git a/frontend/src/lib/components/CyclotronJob/integrations/IntegrationChoice.tsx b/frontend/src/lib/components/CyclotronJob/integrations/IntegrationChoice.tsx
index 79702dd95de9..f080358c8780 100644
--- a/frontend/src/lib/components/CyclotronJob/integrations/IntegrationChoice.tsx
+++ b/frontend/src/lib/components/CyclotronJob/integrations/IntegrationChoice.tsx
@@ -1,5 +1,5 @@
import { useActions, useValues } from 'kea'
-import { useEffect } from 'react'
+import { useEffect, useRef } from 'react'
import { IconExternal, IconTrash, IconX } from '@posthog/icons'
import { LemonBanner, LemonButton, LemonMenu, LemonSkeleton } from '@posthog/lemon-ui'
@@ -47,8 +47,13 @@ export function IntegrationChoice({
// saves. The UI surfaces a warning below instead so the user picks explicitly.
const valueIsMissing = !integrationsLoading && !!value && !!integrations && !integrationKind
+ // Fire at most once: the consumer's write may take a full state round-trip before it flows
+ // back into `value`, and re-dispatching on every render in that window can amplify into an
+ // infinite update loop (React #185).
+ const autoSelected = useRef(false)
useEffect(() => {
- if (!integrationsLoading && !value && integrationsOfKind?.length) {
+ if (!integrationsLoading && !value && integrationsOfKind?.length && !autoSelected.current) {
+ autoSelected.current = true
onChange?.(integrationsOfKind[0].id)
}
}, [integrationsLoading, onChange, integrationsOfKind?.length, value, integrationsOfKind])
diff --git a/frontend/src/lib/utils/objects.test.ts b/frontend/src/lib/utils/objects.test.ts
index 730255a464c0..9daca1bca1c4 100644
--- a/frontend/src/lib/utils/objects.test.ts
+++ b/frontend/src/lib/utils/objects.test.ts
@@ -89,6 +89,16 @@ describe('objects utils', () => {
expect(reconcileById([], next, (item) => item.id)).toBe(next)
})
+ it('returns the previous array itself when every item is reused', () => {
+ // Consumers that diff collections by reference (ReactFlow's controlled nodes/edges
+ // props) must see an unchanged reconcile as unchanged: a fresh array wrapper per
+ // rebuild re-syncs ReactFlow's store on every render, which can amplify upstream
+ // churn into an infinite update loop (React #185).
+ const prev = [makeItem({ id: 'a' }), makeItem({ id: 'b' })]
+ const next = [makeItem({ id: 'a' }), makeItem({ id: 'b' })]
+ expect(reconcileById(prev, next, (item) => item.id)).toBe(prev)
+ })
+
it('never reuses items the isReusable predicate rejects, even when deep-equal', () => {
// An in-flight item's row may render wall-clock time: reusing its reference would let a
// memoized row skip the poll re-render and freeze.
diff --git a/frontend/src/lib/utils/objects.ts b/frontend/src/lib/utils/objects.ts
index 3956de7a1fcd..8a2ab70795f6 100644
--- a/frontend/src/lib/utils/objects.ts
+++ b/frontend/src/lib/utils/objects.ts
@@ -30,10 +30,15 @@ export function reconcileById<T>(
return next
}
const previousById = new Map(previous.map((item) => [getId(item), item]))
- return next.map((item) => {
+ const result = next.map((item) => {
const existing = previousById.get(getId(item))
return existing && isReusable(item) && objectsEqual(existing, item) ? existing : item
})
+ // Keep the collection itself reference-stable too: consumers that diff by reference
+ // (e.g. ReactFlow's controlled nodes/edges props) must see an unchanged reconcile as
+ // unchanged, or every rebuild re-syncs them and can amplify churn into an update loop.
+ const unchanged = result.length === previous.length && result.every((item, index) => item === previous[index])
+ return unchanged ? previous : result
}
// https://stackoverflow.com/questions/25421233/javascript-removing-undefined-fields-from-an-object
diff --git a/products/workflows/frontend/Workflows/hogflows/hogFlowEditorLogic.test.ts b/products/workflows/frontend/Workflows/hogflows/hogFlowEditorLogic.test.ts
index 111cbfe122d8..b76816758cdc 100644
--- a/products/workflows/frontend/Workflows/hogflows/hogFlowEditorLogic.test.ts
+++ b/products/workflows/frontend/Workflows/hogflows/hogFlowEditorLogic.test.ts
@@ -540,8 +540,10 @@ describe('hogFlowEditorLogic', () => {
// Freshly constructed but deep-equal, as an auto-save round-trip would deliver.
await applyFlow(makeFlow())
- logic.values.nodes.forEach((node, index) => expect(node).toBe(initialNodes[index]))
- logic.values.edges.forEach((edge, index) => expect(edge).toBe(initialEdges[index]))
+ // Array identity, not just item identity: ReactFlow diffs its nodes/edges props by
+ // reference, so a fresh array wrapper per rebuild re-syncs its store every render.
+ expect(logic.values.nodes).toBe(initialNodes)
+ expect(logic.values.edges).toBe(initialEdges)
})
it('replaces only the changed node reference when one action changes', async () => {