: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
perf(posthog-ai): keep recent task chats loaded after navigation (#120616)
Shy Alter9640f4da8145
diff --git a/products/posthog_ai/frontend/components/RunSurfaceImpl.tsx b/products/posthog_ai/frontend/components/RunSurfaceImpl.tsxindex 4172e168b20a..0a43d7918f18 100644--- a/products/posthog_ai/frontend/components/RunSurfaceImpl.tsx+++ b/products/posthog_ai/frontend/components/RunSurfaceImpl.tsx@@ -6,6 +6,7 @@ import { LemonBanner, LemonButton, LemonDivider } from '@posthog/lemon-ui'import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'import { useThreadSkin } from '../hooks/useThreadSkin'+import { retainMountedLogic } from '../logics/retainedLogics'import { isTerminalRunStatus, runStreamLogic } from '../logics/runStreamLogic'import { taskLogic } from '../logics/taskLogic'import { isPiTaskRuntime, OriginProduct } from '../types/taskTypes'@@ -66,6 +67,7 @@ interface RunSurfaceContextValue {}const FLOATING_INPUTS_GAP = 16+const MAX_RETAINED_RUN_STREAMS = 3const RunSurfaceContext = createContext<RunSurfaceContextValue | null>(null)@@ -80,6 +82,7 @@ function useRunSurfaceContext(): RunSurfaceContextValue {export interface RunSurfaceRootProps extends RunSurfaceProps {/** Custom composition of `RunSurface.*` slots. */children: ReactNode+ retainAfterUnmount?: boolean}/**@@ -95,6 +98,7 @@ function RunSurfaceRoot({conversationId,interaction = 'read-only',children,+ retainAfterUnmount = false,}: RunSurfaceRootProps): JSX.Element {const replayOnly = interaction !== 'live'// A pending surface (no run id) must supply `streamKey` to key on; `runId` is the key otherwise.@@ -102,6 +106,16 @@ function RunSurfaceRoot({const hasOptimisticClientStream = !!streamKey && streamKey !== runIdconst [floatingInputsHeight, setFloatingInputsHeight] = useState(0)const { hasThreadItems } = useValues(runStreamLogic({ streamKey: logicKey, conversationId, replayOnly }))+ useEffect(() => {+ if (retainAfterUnmount && !replayOnly && runId && logicKey === runId) {+ retainMountedLogic(+ 'run-stream',+ logicKey,+ runStreamLogic({ streamKey: logicKey, conversationId, replayOnly }),+ MAX_RETAINED_RUN_STREAMS+ )+ }+ }, [retainAfterUnmount, replayOnly, runId, logicKey, conversationId])// The runtime and scout flag live on the task (not the run), so the surface owns loading it once and// exposing it to the slots. The runner already has the task loaded; an embed fetches it here.diff --git a/products/posthog_ai/frontend/logics/retainedLogics.test.ts b/products/posthog_ai/frontend/logics/retainedLogics.test.tsnew file mode 100644index 000000000000..142885080112--- /dev/null+++ b/products/posthog_ai/frontend/logics/retainedLogics.test.ts@@ -0,0 +1,37 @@+import { actions, kea, key, path, props, reducers } from 'kea'++import { initKeaTests } from '~/test/init'++import { retainMountedLogic } from './retainedLogics'++const counterLogic = kea([+ props({} as { id: string }),+ key((props) => props.id),+ path((id) => ['test', 'retainedLogics', 'counter', id]),+ actions({ increment: true }),+ reducers({ count: [0, { increment: (state: number) => state + 1 }] }),+])++describe('retainMountedLogic', () => {+ beforeEach(() => {+ initKeaTests()+ })++ it('keeps the logic and its state after its holder unmounts, and evicts the oldest past the limit', () => {+ const holderUnmount = counterLogic({ id: 'a' }).mount()+ expect(retainMountedLogic('test', 'a', counterLogic({ id: 'a' }), 2)).toBe(false)+ counterLogic({ id: 'a' }).actions.increment()+ holderUnmount()++ expect(counterLogic.findMounted({ id: 'a' })?.values.count).toBe(1)+ expect(retainMountedLogic('test', 'a', counterLogic({ id: 'a' }), 2)).toBe(true)++ retainMountedLogic('test', 'b', counterLogic({ id: 'b' }), 2)+ retainMountedLogic('test', 'a', counterLogic({ id: 'a' }), 2)+ retainMountedLogic('test', 'c', counterLogic({ id: 'c' }), 2)++ expect(counterLogic.findMounted({ id: 'b' })).toBeNull()+ expect(counterLogic.findMounted({ id: 'a' })?.values.count).toBe(1)+ expect(counterLogic.findMounted({ id: 'c' })).not.toBeNull()+ })+})diff --git a/products/posthog_ai/frontend/logics/retainedLogics.ts b/products/posthog_ai/frontend/logics/retainedLogics.tsnew file mode 100644index 000000000000..bfd4dae087cd--- /dev/null+++ b/products/posthog_ai/frontend/logics/retainedLogics.ts@@ -0,0 +1,37 @@+import { BuiltLogic, getContext } from 'kea'++const retainedByContext = new WeakMap<object, Map<string, Map<string, () => void>>>()++function retainedGroup(group: string): Map<string, () => void> {+ const context = getContext()+ let groups = retainedByContext.get(context)+ if (!groups) {+ groups = new Map()+ retainedByContext.set(context, groups)+ }+ let retained = groups.get(group)+ if (!retained) {+ retained = new Map()+ groups.set(group, retained)+ }+ return retained+}++export function retainMountedLogic(group: string, key: string, logic: BuiltLogic, limit: number): boolean {+ const retained = retainedGroup(group)+ const unmount = retained.get(key)+ if (unmount) {+ retained.delete(key)+ retained.set(key, unmount)+ return true+ }+ retained.set(key, logic.mount())+ for (const [oldestKey, oldestUnmount] of retained) {+ if (retained.size <= limit) {+ break+ }+ retained.delete(oldestKey)+ oldestUnmount()+ }+ return false+}diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunChat.tsx b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunChat.tsxindex ac5e0c3d5d77..57f5df9dd53d 100644--- a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunChat.tsx+++ b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunChat.tsx@@ -160,6 +160,7 @@ function TaskRunChatContent({runId={logicProps.runId}streamKey={logicProps.streamKey}interaction="live"+ retainAfterUnmount><RunEscapeBoundaryscope={escapeScope}