: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
fix(posthog-ai): lazy-load Monaco diff editor (#97992)
Paul D'Ambraeed13e0e2eea
diff --git a/products/posthog_ai/frontend/components/Approval.test.tsx b/products/posthog_ai/frontend/components/Approval.test.tsxindex e427598f3844..ea8fb12bd990 100644--- a/products/posthog_ai/frontend/components/Approval.test.tsx+++ b/products/posthog_ai/frontend/components/Approval.test.tsx@@ -1,6 +1,6 @@import '@testing-library/jest-dom'-import { cleanup, fireEvent, render, screen } from '@testing-library/react'+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'import { useActions, useValues } from 'kea'import type { PermissionRequestRecord, ToolInvocation } from '../types/streamTypes'@@ -252,7 +252,7 @@ describe('Sandbox approval input area', () => {expect(snippet().textContent).toContain('"c19"')})- it('renders diff evidence instead of the raw payload when the tool call carries a diff', () => {+ it('renders diff evidence instead of the raw payload when the tool call carries a diff', async () => {render(<PermissionInputstreamKey="conv-1"@@ -267,8 +267,11 @@ describe('Sandbox approval input area', () => {/>)- const diff = document.querySelector('[data-attr="monaco-diff"]')- expect(diff).not.toBeNull()+ const diff = await waitFor(() => {+ const node = document.querySelector('[data-attr="monaco-diff"]')+ expect(node).not.toBeNull()+ return node+ })expect(diff?.getAttribute('data-original')).toBe('let a := 1')expect(diff?.getAttribute('data-modified')).toBe('let a := 2')expect(screen.getByText('+1')).toBeInTheDocument()diff --git a/products/posthog_ai/frontend/components/PermissionInput.tsx b/products/posthog_ai/frontend/components/PermissionInput.tsxindex 942959c1ce60..c631d0bc0ab2 100644--- a/products/posthog_ai/frontend/components/PermissionInput.tsx+++ b/products/posthog_ai/frontend/components/PermissionInput.tsx@@ -16,8 +16,9 @@ import { isPlanPermissionRequest, mapPermissionOptions, type ApprovalCardOptionimport type { PermissionRequestRecord } from '../types/streamTypes'import { resolveToolCall } from '../utils/toolResolver'import { isPlanApprovalModeOptionId, InlineEditableText, PlanApprovalSelector } from './PlanApprovalActions'-import { DiffEditor, DiffStats } from './tool/EditDiffRenderer'+import { DiffStats } from './tool/DiffStats'import { FilePath } from './tool/FilePath'+import { LazyDiffEditor } from './tool/LazyDiffEditor'import { findAllDiffContent, getDiffStats } from './tool/toolDiffContent'import { lookupToolRenderer } from './tool/toolRegistry'@@ -79,7 +80,7 @@ function PermissionEvidence({ request, label, payload }: PermissionEvidenceProps)}<DiffStats added={stats.added} removed={stats.removed} /></div>- <DiffEditor diff={diff} path={diff.path} sideBySide />+ <LazyDiffEditor diff={diff} path={diff.path} sideBySide /></div>)})}diff --git a/products/posthog_ai/frontend/components/tool/DiffEvidenceCard.tsx b/products/posthog_ai/frontend/components/tool/DiffEvidenceCard.tsxindex ebd4d09ac049..cfccf865d3ec 100644--- a/products/posthog_ai/frontend/components/tool/DiffEvidenceCard.tsx+++ b/products/posthog_ai/frontend/components/tool/DiffEvidenceCard.tsx@@ -5,7 +5,8 @@ import { Button } from '@posthog/quill-primitives'import { cn } from 'lib/utils/css-classes'-import { DiffEditor, DiffStats } from './EditDiffRenderer'+import { DiffStats } from './DiffStats'+import { LazyDiffEditor } from './LazyDiffEditor'import { getDiffStats } from './toolDiffContent'/** Collapsed body cap — roughly a dozen diff lines, per the permission-card evidence design. Must stay in sync with the `max-h-60` class below. */@@ -46,7 +47,12 @@ export function DiffEvidenceCard({ label, oldText, newText, path }: DiffEvidence<DiffStats added={added} removed={removed} /></div><div className={cn('min-w-0', collapsed && 'relative max-h-60 overflow-hidden')}>- <DiffEditor diff={{ type: 'diff', oldText, newText }} path={path} sideBySide hideUnchanged={false} />+ <LazyDiffEditor+ diff={{ type: 'diff', oldText, newText }}+ path={path}+ sideBySide+ hideUnchanged={false}+ />{collapsed && (<div className="absolute inset-x-0 bottom-0 h-10 bg-gradient-to-t from-surface-primary to-transparent pointer-events-none" />)}diff --git a/products/posthog_ai/frontend/components/tool/DiffStats.tsx b/products/posthog_ai/frontend/components/tool/DiffStats.tsxnew file mode 100644index 000000000000..372d5a0943df--- /dev/null+++ b/products/posthog_ai/frontend/components/tool/DiffStats.tsx@@ -0,0 +1,8 @@+/** +added / -removed mono stat chip for a diff. */+export function DiffStats({ added, removed }: { added: number; removed: number }): JSX.Element {+ return (+ <span className="font-mono text-xs shrink-0">+ <span className="text-success">+{added}</span> <span className="text-danger">-{removed}</span>+ </span>+ )+}diff --git a/products/posthog_ai/frontend/components/tool/EditDiffRenderer.tsx b/products/posthog_ai/frontend/components/tool/EditDiffRenderer.tsxindex 512dd69d4e8a..dee6d8643d0b 100644--- a/products/posthog_ai/frontend/components/tool/EditDiffRenderer.tsx+++ b/products/posthog_ai/frontend/components/tool/EditDiffRenderer.tsx@@ -8,6 +8,7 @@ import MonacoDiffEditor from 'lib/components/MonacoDiffEditor'import { themeLogic } from '~/layout/navigation-3000/themeLogic'+import { DiffStats } from './DiffStats'import { EditorSkeleton } from './EditorSkeleton'import { FilePath } from './FilePath'import { GenericMcpToolRenderer } from './GenericMcpToolRenderer'@@ -73,17 +74,14 @@ function pickDiffEditorOptions(sideBySide: boolean, hideUnchanged: boolean): edireturn hideUnchanged ? DIFF_EDITOR_OPTIONS : SHOW_ALL_DIFF_EDITOR_OPTIONS}-export function DiffEditor({- diff,- path,- sideBySide,- hideUnchanged = true,-}: {+export interface DiffEditorProps {diff: ToolCallDiffContentpath?: stringsideBySide?: booleanhideUnchanged?: boolean-}): JSX.Element {+}++export function DiffEditor({ diff, path, sideBySide, hideUnchanged = true }: DiffEditorProps): JSX.Element {// Lazy-mount: only instantiate the Monaco diff editor once the card scrolls near the viewport.const { ref, inView } = useInView({ rootMargin: '500px', triggerOnce: true })// Match the surrounding app theme — without this Monaco falls back to its default `vs` (white) theme.@@ -108,15 +106,6 @@ export function DiffEditor({)}-/** +added / -removed mono stat chip for a diff. */-export function DiffStats({ added, removed }: { added: number; removed: number }): JSX.Element {- return (- <span className="font-mono text-xs shrink-0">- <span className="text-success">+{added}</span> <span className="text-danger">-{removed}</span>- </span>- )-}-/*** Renderer for Edit / Write / MultiEdit / NotebookEdit. The header reads "Edited a file" / "Created a* file" (or "Edited N files"); expanding the card reveals the filename, line stats, and a per-file view:diff --git a/products/posthog_ai/frontend/components/tool/LazyDiffEditor.tsx b/products/posthog_ai/frontend/components/tool/LazyDiffEditor.tsxnew file mode 100644index 000000000000..e71241ed750c--- /dev/null+++ b/products/posthog_ai/frontend/components/tool/LazyDiffEditor.tsx@@ -0,0 +1,35 @@+import { Suspense } from 'react'++import { lazyWithRetry } from 'lib/utils/retryImport'++import type { DiffEditorProps } from './EditDiffRenderer'+import { EditorSkeleton } from './EditorSkeleton'++const DiffEditor = lazyWithRetry(() => import('./EditDiffRenderer').then((m) => ({ default: m.DiffEditor })))++/**+ * The diff editor's height bounds, mirrored from `MonacoDiffEditor` so the loading fallback reserves the height+ * the editor settles on. Importing them from that module would pull Monaco into this chunk and defeat the lazy load.+ */+const EDITOR_LINE_HEIGHT_PX = 18+const EDITOR_MIN_LINES = 5+const EDITOR_MAX_LINES = 30+const EDITOR_PADDING_PX = 18++/**+ * Loads the Monaco-backed diff editor on first render, behind a skeleton sized to the diff. The approval and+ * evidence cards render through this boundary so their chunk never bundles Monaco.+ */+export function LazyDiffEditor(props: DiffEditorProps): JSX.Element {+ const { oldText, newText } = props.diff+ const lineCount = Math.max(oldText?.split('\n').length ?? 0, newText?.split('\n').length ?? 0)+ const lines = Math.max(EDITOR_MIN_LINES, Math.min(EDITOR_MAX_LINES, lineCount))++ return (+ <Suspense+ fallback={<EditorSkeleton height={lines * EDITOR_LINE_HEIGHT_PX + EDITOR_PADDING_PX} lines={lines} />}+ >+ <DiffEditor {...props} />+ </Suspense>+ )+}