Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): tolerate CodeMirror hover after a failed measure (#17189)

cursor[bot]9ec0ef133616
diff --git a/web/src/components/editor/CodeMirrorEditor.tsx b/web/src/components/editor/CodeMirrorEditor.tsx
index 3d659d0328..35c2e48cc2 100644
--- a/web/src/components/editor/CodeMirrorEditor.tsx
+++ b/web/src/components/editor/CodeMirrorEditor.tsx
@@ -37,6 +37,7 @@ import { lightTheme } from "@/src/components/editor/light-theme";
import { darkTheme } from "@/src/components/editor/dark-theme";
import { autoScrollOnSelectionDrag } from "@/src/components/editor/autoScrollOnSelectionDrag";
import { createJsonMagicPasteExtension } from "@/src/components/editor/jsonMagicPaste";
+import { tolerateUnstableViewportPosAtCoords } from "@/src/components/editor/tolerateUnstableViewportPosAtCoords";
import { codeMirrorSearchPanel } from "@/src/constants/codeMirrorSearchPanel";
// Custom language mode for prompts that highlights mustache variables and prompt dependency tags
@@ -468,6 +469,7 @@ export function CodeMirrorEditor({
// a full reconfigure on every parent re-render (e.g. every keystroke).
const extensions = useMemo(
() => [
+ tolerateUnstableViewportPosAtCoords,
// Block document changes (including paste) when not editable; the
// `editable` DOM facet alone does not always prevent paste (see CM6
// EditorState.readOnly vs EditorView.editable).
diff --git a/web/src/components/editor/tolerateUnstableViewportPosAtCoords.clienttest.ts b/web/src/components/editor/tolerateUnstableViewportPosAtCoords.clienttest.ts
new file mode 100644
index 0000000000..d6ec0bc9c7
--- /dev/null
+++ b/web/src/components/editor/tolerateUnstableViewportPosAtCoords.clienttest.ts
@@ -0,0 +1,127 @@
+// @vitest-environment jsdom
+
+import { type EditorView } from "@uiw/react-codemirror";
+import {
+ isCodeMirrorUnstableViewportError,
+ wrapEditorViewCoordLookups,
+} from "@/src/components/editor/tolerateUnstableViewportPosAtCoords";
+
+const COORDS = { x: 10, y: 20 };
+
+function createView(overrides?: {
+ posAtCoords?: (
+ coords: { x: number; y: number },
+ precise?: boolean,
+ ) => number | null;
+ posAndSideAtCoords?: (
+ coords: { x: number; y: number },
+ precise?: boolean,
+ ) => { pos: number; assoc: number } | null;
+}) {
+ return {
+ posAtCoords: (overrides?.posAtCoords ??
+ (() => 4)) as EditorView["posAtCoords"],
+ posAndSideAtCoords: (overrides?.posAndSideAtCoords ??
+ (() => ({ pos: 4, assoc: 1 }))) as EditorView["posAndSideAtCoords"],
+ };
+}
+
+const nullTileError = () => {
+ throw new TypeError("Cannot read properties of null (reading 'length')");
+};
+
+describe("isCodeMirrorUnstableViewportError", () => {
+ it("matches Chrome, Firefox, and Safari null-tile TypeErrors", () => {
+ expect(
+ isCodeMirrorUnstableViewportError(
+ new TypeError("Cannot read properties of null (reading 'length')"),
+ ),
+ ).toBe(true);
+ expect(
+ isCodeMirrorUnstableViewportError(
+ new TypeError("Cannot read property 'length' of null"),
+ ),
+ ).toBe(true);
+ expect(
+ isCodeMirrorUnstableViewportError(
+ new TypeError('can\'t access property "length" of null'),
+ ),
+ ).toBe(true);
+ expect(
+ isCodeMirrorUnstableViewportError(
+ new TypeError("null is not an object (evaluating 'line.array.length')"),
+ ),
+ ).toBe(true);
+ });
+
+ it("does not match other TypeErrors or non-TypeErrors", () => {
+ expect(
+ isCodeMirrorUnstableViewportError(
+ new TypeError("Cannot read properties of null (reading 'top')"),
+ ),
+ ).toBe(false);
+ expect(
+ isCodeMirrorUnstableViewportError(
+ new TypeError("null is not an object (evaluating 'line.array.top')"),
+ ),
+ ).toBe(false);
+ expect(isCodeMirrorUnstableViewportError(new Error("length"))).toBe(false);
+ expect(isCodeMirrorUnstableViewportError("length")).toBe(false);
+ });
+});
+
+describe("wrapEditorViewCoordLookups", () => {
+ it("returns null when precise lookups throw the null-tile TypeError", () => {
+ const view = createView({
+ posAtCoords: nullTileError,
+ posAndSideAtCoords: nullTileError,
+ });
+
+ wrapEditorViewCoordLookups(view);
+
+ expect(view.posAtCoords(COORDS)).toBeNull();
+ expect(view.posAndSideAtCoords(COORDS)).toBeNull();
+ });
+
+ it("returns a non-null fallback when imprecise lookups throw", () => {
+ const view = createView({
+ posAtCoords: nullTileError,
+ posAndSideAtCoords: nullTileError,
+ });
+
+ wrapEditorViewCoordLookups(view);
+
+ expect(view.posAtCoords(COORDS, false)).toBe(0);
+ expect(view.posAndSideAtCoords(COORDS, false)).toEqual({
+ pos: 0,
+ assoc: -1,
+ });
+ });
+
+ it("rethrows unrelated lookup errors", () => {
+ const view = createView({
+ posAtCoords: () => {
+ throw new TypeError("Cannot read properties of null (reading 'top')");
+ },
+ });
+
+ wrapEditorViewCoordLookups(view);
+
+ expect(() => view.posAtCoords(COORDS)).toThrow(
+ "Cannot read properties of null (reading 'top')",
+ );
+ });
+
+ it("passes through successful lookups and restores the originals", () => {
+ const view = createView();
+
+ const restore = wrapEditorViewCoordLookups(view);
+
+ expect(view.posAtCoords(COORDS)).toBe(4);
+ expect(view.posAndSideAtCoords(COORDS)).toEqual({ pos: 4, assoc: 1 });
+
+ restore();
+
+ expect(view.posAtCoords(COORDS)).toBe(4);
+ });
+});
diff --git a/web/src/components/editor/tolerateUnstableViewportPosAtCoords.ts b/web/src/components/editor/tolerateUnstableViewportPosAtCoords.ts
new file mode 100644
index 0000000000..a5a0cd171a
--- /dev/null
+++ b/web/src/components/editor/tolerateUnstableViewportPosAtCoords.ts
@@ -0,0 +1,101 @@
+import { type EditorView, ViewPlugin } from "@uiw/react-codemirror";
+
+/**
+ * After a failed CodeMirror measure (viewport did not stabilize), `lineAt` can
+ * return null and `scanTile` reads `.length` on that null. Default
+ * `posAtCoords` / `posAndSideAtCoords` already return null when a position
+ * cannot be resolved. Callers that pass `precise: false` (mouse selection,
+ * drops) require a non-null result, so fall back to the document start.
+ */
+export function isCodeMirrorUnstableViewportError(error: unknown): boolean {
+ if (!(error instanceof TypeError)) {
+ return false;
+ }
+
+ // V8 (Chrome/Edge), SpiderMonkey (Firefox), and JavaScriptCore (Safari)
+ // each phrase the same null.length dereference differently.
+ return (
+ /Cannot read properties of null \(reading ['"]length['"]\)/.test(
+ error.message,
+ ) ||
+ /Cannot read property ['"]length['"] of null/.test(error.message) ||
+ /can't access property ["']length["'] of null/.test(error.message) ||
+ /null is not an object \(evaluating ['"].*\.length['"]\)/.test(
+ error.message,
+ )
+ );
+}
+
+type CoordLookupView = {
+ posAtCoords: EditorView["posAtCoords"];
+ posAndSideAtCoords: EditorView["posAndSideAtCoords"];
+};
+
+type PosAtCoordsFn = (
+ coords: { x: number; y: number },
+ precise?: boolean,
+) => number | null;
+
+type PosAndSideAtCoordsFn = (
+ coords: { x: number; y: number },
+ precise?: boolean,
+) => { pos: number; assoc: number } | null;
+
+const IMPRECISE_SIDE_FALLBACK = { pos: 0, assoc: -1 } as const;
+
+function fallbackForUnstableViewport(precise: boolean | undefined) {
+ // precise === false is the mouse-selection / drop path; those callers read
+ // .pos / .assoc (or the number) without a null check.
+ if (precise === false) {
+ return { pos: 0 as number | null, side: IMPRECISE_SIDE_FALLBACK };
+ }
+ return { pos: null as number | null, side: null };
+}
+
+export function wrapEditorViewCoordLookups(view: CoordLookupView): () => void {
+ const originalPosAtCoords = view.posAtCoords as PosAtCoordsFn;
+ const originalPosAndSideAtCoords =
+ view.posAndSideAtCoords as PosAndSideAtCoordsFn;
+
+ view.posAtCoords = ((coords, precise?: boolean) => {
+ try {
+ return originalPosAtCoords.call(view, coords, precise);
+ } catch (error) {
+ if (isCodeMirrorUnstableViewportError(error)) {
+ return fallbackForUnstableViewport(precise).pos;
+ }
+ throw error;
+ }
+ }) as EditorView["posAtCoords"];
+
+ view.posAndSideAtCoords = ((coords, precise?: boolean) => {
+ try {
+ return originalPosAndSideAtCoords.call(view, coords, precise);
+ } catch (error) {
+ if (isCodeMirrorUnstableViewportError(error)) {
+ return fallbackForUnstableViewport(precise).side;
+ }
+ throw error;
+ }
+ }) as EditorView["posAndSideAtCoords"];
+
+ return () => {
+ view.posAtCoords = originalPosAtCoords as EditorView["posAtCoords"];
+ view.posAndSideAtCoords =
+ originalPosAndSideAtCoords as EditorView["posAndSideAtCoords"];
+ };
+}
+
+export const tolerateUnstableViewportPosAtCoords = ViewPlugin.fromClass(
+ class {
+ private readonly restore: () => void;
+
+ constructor(view: EditorView) {
+ this.restore = wrapEditorViewCoordLookups(view);
+ }
+
+ destroy() {
+ this.restore();
+ }
+ },
+);
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/JsonPathInput.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/JsonPathInput.tsx
index 7de7bc757a..53e0d5e4a3 100644
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/JsonPathInput.tsx
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/JsonPathInput.tsx
@@ -5,6 +5,7 @@ import { StreamLanguage, type StringStream } from "@codemirror/language";
import { useTheme } from "next-themes";
import { lightTheme } from "@/src/components/editor/light-theme";
import { darkTheme } from "@/src/components/editor/dark-theme";
+import { tolerateUnstableViewportPosAtCoords } from "@/src/components/editor/tolerateUnstableViewportPosAtCoords";
import { cn } from "@/src/utils/tailwind";
import { evaluateJsonPath } from "@langfuse/shared";
@@ -133,6 +134,7 @@ export function JsonPathInput({
highlightActiveLine: false,
}}
extensions={[
+ tolerateUnstableViewportPosAtCoords,
EditorView.theme({
"&.cm-focused": {
outline: "none",
diff --git a/web/src/features/evals/components/code-eval-template-form-body.tsx b/web/src/features/evals/components/code-eval-template-form-body.tsx
index bbf96c6980..ef98a59088 100644
--- a/web/src/features/evals/components/code-eval-template-form-body.tsx
+++ b/web/src/features/evals/components/code-eval-template-form-body.tsx
@@ -34,6 +34,7 @@ import {
import { darkTheme } from "@/src/components/editor/dark-theme";
import { lightTheme } from "@/src/components/editor/light-theme";
import { autoScrollOnSelectionDrag } from "@/src/components/editor/autoScrollOnSelectionDrag";
+import { tolerateUnstableViewportPosAtCoords } from "@/src/components/editor/tolerateUnstableViewportPosAtCoords";
import { codeMirrorSearchPanel } from "@/src/constants/codeMirrorSearchPanel";
import {
getCodeEvalHoverDocs,
@@ -365,6 +366,7 @@ export function CodeEvalTemplateFormBody({
);
const extensions = useMemo(
() => [
+ tolerateUnstableViewportPosAtCoords,
// The `editable` prop only blocks direct typing; readOnly also blocks
// paste and drag-and-drop edits.
...(!editable ? [EditorState.readOnly.of(true)] : []),