Skip to content

πŸͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(search-bar): keep caret in filter on operator pick; blank value stays unarmed (LFE-10501) (#14714)

Nikita Kabardin1105ad658ac5
diff --git a/web/src/features/search-bar/components/SearchComposer.tsx b/web/src/features/search-bar/components/SearchComposer.tsx
index d5cd50e9ef..2041dd2a0f 100644
--- a/web/src/features/search-bar/components/SearchComposer.tsx
+++ b/web/src/features/search-bar/components/SearchComposer.tsx
@@ -25,13 +25,13 @@ import {
deriveComposerSegments,
type ComposerSegment,
} from "@/src/features/search-bar/lib/composer-segments";
-import { serializeValue, termAt } from "@/src/features/search-bar/lib/langQ";
import {
scoreTypeContextFromObserved,
type ObservedOptions,
} from "@/src/features/search-bar/lib/observed-options";
import { getRecentSearches } from "@/src/features/search-bar/lib/recent-searches";
import {
+ applyPick,
flattenOptions,
planInputCompletions,
type CompletionOption,
@@ -756,76 +756,18 @@ export function SearchComposer({
return;
}
- const current = draftRef.current;
- let insert: string;
- let keepOpen: boolean;
- let replaceFrom = currentPlan.from;
- let replaceTo = currentPlan.to;
- if (option.kind === "field") {
- // Replacing the key of an existing filter: the span ends AT the colon,
- // so the insert must not bring its own.
- const colonFollows = current.slice(currentPlan.to).startsWith(":");
- insert = option.fieldId.endsWith(".")
- ? option.fieldId
- : colonFollows
- ? option.fieldId
- : `${option.fieldId}:`;
- keepOpen = true;
- // A dot-prefix field (`metadata.`/`scores.`/`traceScores.`) is itself a
- // partial key. When an existing `:value` follows the replaced key, the
- // bare prefix would splice in front of it (`meta:foo` -> broken
- // `metadata.:foo`). Consume the whole term so the user re-picks the key
- // from observed options instead.
- if (option.fieldId.endsWith(".") && colonFollows) {
- replaceTo = termAt(current, currentPlan.from)?.to ?? currentPlan.to;
- }
- } else if (option.kind === "value") {
- insert = serializeValue(option.value);
- keepOpen = currentPlan.keepOpenOnPick ?? false;
- } else {
- insert = option.insert;
- // A scope rewrite carries its own span (the whole coalesced free-text
- // run), so it replaces that, not just the token under the caret.
- if (option.kind === "pattern" && option.replaceSpan) {
- replaceFrom = option.replaceSpan.from;
- replaceTo = option.replaceSpan.to;
- }
- // A trailing `:`, ` `, or `(` drops the caret into an interactive
- // context (value stage, next field, or an open array group like
- // `tags:(`) β€” keep the popover open so the next pick is immediate.
- keepOpen =
- option.insert.endsWith(":") ||
- option.insert.endsWith(" ") ||
- option.insert.endsWith("(");
- }
-
- // A pick that COMPLETES a filter token β€” a value (`name:checkout`) or a
- // ready-to-run suggestion (`level:DEFAULT`) β€” and sits at the END of the
- // draft advances to a fresh token: append a trailing space, drop the caret
- // AFTER it (OUTSIDE the just-completed pill), and reopen field suggestions
- // for the next filter, instead of leaving the caret inside the pill where
- // typing would edit what was just picked. Picks that invite more input (a
- // bare `field:` key, a `metadata.` prefix, an open `tags:(` group), grouped
- // value entry, and mid-query edits (non-whitespace follows) stay put.
- const grouped = currentPlan.keepOpenOnPick ?? false;
- const invitesMoreInput =
- option.kind === "field" || // a `field:` key always needs a value next
- insert.endsWith(":") ||
- insert.endsWith(" ") ||
- insert.endsWith("(");
- const completesFilterAtEnd =
- !grouped &&
- !invitesMoreInput &&
- current.slice(replaceTo).trim().length === 0;
- if (completesFilterAtEnd) {
- insert += " ";
- // Consume any existing trailing whitespace so the space never doubles.
- replaceTo = current.length;
- keepOpen = true;
- }
-
- const next = replaceRange(current, replaceFrom, replaceTo, insert);
- const caretAt = replaceFrom + insert.length;
+ // The pure text/caret computation β€” insert, caret placement, and whether
+ // to keep the popover open β€” lives in `applyPick` (unit-tested). A pick
+ // that COMPLETES a filter at the END of the draft appends a trailing space
+ // and drops the caret OUTSIDE the pill (next-filter affordance); a pick
+ // that INVITES MORE INPUT (a `field:` key, a `metadata.` prefix, an open
+ // `tags:(` group, a comparison/logical operator awaiting its value) keeps
+ // the caret in place.
+ const {
+ next,
+ caret: caretAt,
+ keepOpen,
+ } = applyPick(option, draftRef.current, currentPlan);
setDraftWithSelection(next, caretAt);
setAutocompleteOpen(keepOpen);
setHighlightedOptionId(null);
diff --git a/web/src/features/search-bar/lib/completions.clienttest.ts b/web/src/features/search-bar/lib/completions.clienttest.ts
index 6ad3cf46b5..11631bbfba 100644
--- a/web/src/features/search-bar/lib/completions.clienttest.ts
+++ b/web/src/features/search-bar/lib/completions.clienttest.ts
@@ -1,4 +1,5 @@
import {
+ applyPick,
flattenOptions,
planInputCompletions,
SECTION_COMPARE_OPS,
@@ -6,6 +7,7 @@ import {
SECTION_MATCH_OPS,
SECTION_RECENT,
SECTION_VALUES,
+ type CompletionOption,
type InputCompletionContext,
} from "@/src/features/search-bar/lib/completions";
import type { ObservedOptions } from "@/src/features/search-bar/lib/observed-options";
@@ -103,6 +105,51 @@ describe("planInputCompletions", () => {
expect(labels).toContain("=chat"); // exact
});
+ it("keeps a whitespace-only value an unarmed plain list (space inside empty quotes)", () => {
+ // LFE-10501 BUG B. `-traceName:""` with the caret between the quotes lists
+ // observed values as a PLAIN, unarmed list β€” Enter commits the query, not a
+ // value. Typing a space between the quotes (`-traceName:" "`) must NOT flip
+ // that into an armed, committable value: a lone space matched every value
+ // that CONTAINS a space and auto-highlighted the first, so Enter yielded an
+ // unwanted `-traceName:"Codex Turn"`.
+ const observed = {
+ ...OBSERVED,
+ traceName: [{ value: "Codex Turn" }, { value: "Other Trace" }],
+ };
+ // Empty quotes, caret between them β€” the baseline "just a list" state.
+ const empty = plan('-traceName:""', 12, { observed });
+ expect(empty?.stage).toBe("value");
+ expect(empty?.autoHighlight ?? false).toBe(false);
+ expect(
+ flattenOptions(empty).some((o) => o.kind === "value" && o.active),
+ ).toBe(false);
+
+ // A space between the quotes, caret after it β€” must stay the same plain list.
+ const spaced = plan('-traceName:" "', 13, { observed });
+ expect(spaced?.stage).toBe("value");
+ expect(spaced?.autoHighlight ?? false).toBe(false);
+ const values = flattenOptions(spaced).filter((o) => o.kind === "value");
+ // Full observed list still offered for browsing, but nothing armed/active.
+ expect(values.map((o) => o.kind === "value" && o.value)).toEqual([
+ "Codex Turn",
+ "Other Trace",
+ ]);
+ expect(values.some((o) => o.kind === "value" && o.active)).toBe(false);
+ // A lone space must not wrap into match-op refinements (`*" "*`) either.
+ expect(spaced?.sections.map((s) => s.title) ?? []).not.toContain(
+ SECTION_MATCH_OPS,
+ );
+ });
+
+ it("still arms Enter for a real typed value prefix (space fix keeps normal typing)", () => {
+ // Guard on the BUG B fix: only empty/whitespace-only values go unarmed. A
+ // real prefix (`ER`) still ranks + arms Enter-to-complete.
+ const p = plan("level:ER", 8);
+ expect(p?.autoHighlight).toBe(true);
+ const first = flattenOptions(p).find((o) => o.kind === "value");
+ expect(first).toMatchObject({ kind: "value", value: "ERROR" });
+ });
+
it("marks a complete typed value active without arming Enter", () => {
const p = plan("level:ERROR", 11);
expect(p?.autoHighlight).toBe(false);
@@ -505,3 +552,75 @@ describe("planInputCompletions", () => {
expect(operators.map((o) => o.label)).not.toContain("OR");
});
});
+
+describe("applyPick", () => {
+ // Narrowing helper: the popover only picks non-recent options here.
+ const nonRecent = (o: CompletionOption | undefined) =>
+ o as Exclude<CompletionOption, { kind: "recent" }>;
+
+ it("keeps the caret INSIDE the block after picking a numeric operator", () => {
+ // LFE-10501 BUG A. Picking `>` for a numeric field must not append a
+ // trailing space and jump the caret outside the filter β€” an operator
+ // invites the value next, so the caret stays right after the symbol and the
+ // next keystroke types the number into `latency:>`.
+ const p = plan("latency:", 8);
+ const gt = flattenOptions(p).find(
+ (o) => o.kind === "operator" && o.label === ">",
+ );
+ expect(gt).toBeDefined();
+ const result = applyPick(nonRecent(gt), "latency:", p!);
+ expect(result.next).toBe("latency:>"); // no trailing space appended
+ expect(result.caret).toBe(9); // caret sits right after `>`, inside the block
+ });
+
+ it("keeps the caret inside the block for datetime operators too", () => {
+ const p = plan("startTime:", 10);
+ const gte = flattenOptions(p).find(
+ (o) => o.kind === "operator" && o.label === ">=",
+ );
+ expect(gte).toBeDefined();
+ const result = applyPick(nonRecent(gte), "startTime:", p!);
+ expect(result.next).toBe("startTime:>=");
+ expect(result.caret).toBe(12);
+ });
+
+ it("keeps the caret inside a score-path block for its comparison operators", () => {
+ const q = "scores.accuracy:";
+ const p = plan(q, q.length);
+ const lt = flattenOptions(p).find(
+ (o) => o.kind === "operator" && o.label === "<",
+ );
+ expect(lt).toBeDefined();
+ const result = applyPick(nonRecent(lt), q, p!);
+ expect(result.next).toBe(`${q}<`);
+ expect(result.caret).toBe(q.length + 1);
+ });
+
+ it("still appends a trailing space + jumps out when a VALUE completes the filter at end", () => {
+ // Guard: the operator fix must not disturb the completes-at-end affordance
+ // for value picks β€” `level:` + ERROR still lands `level:ERROR ` with the
+ // caret AFTER the trailing space so the next filter starts outside the pill.
+ const p = plan("level:", 6);
+ const err = flattenOptions(p).find(
+ (o) => o.kind === "value" && o.value === "ERROR",
+ );
+ expect(err).toBeDefined();
+ const result = applyPick(nonRecent(err), "level:", p!);
+ expect(result.next).toBe("level:ERROR ");
+ expect(result.caret).toBe(12);
+ expect(result.keepOpen).toBe(true);
+ });
+
+ it("keeps the caret put for an AND/NOT connective (trailing space already inserted)", () => {
+ // AND/NOT already carry their own trailing space, so they were never the
+ // BUG A case β€” assert the operator clause leaves them unchanged.
+ const p = plan("level:ERROR N", 13);
+ const not = flattenOptions(p).find(
+ (o) => o.kind === "operator" && o.label === "NOT",
+ );
+ expect(not).toBeDefined();
+ const result = applyPick(nonRecent(not), "level:ERROR N", p!);
+ expect(result.next).toBe("level:ERROR NOT ");
+ expect(result.caret).toBe("level:ERROR NOT ".length);
+ });
+});
diff --git a/web/src/features/search-bar/lib/completions.ts b/web/src/features/search-bar/lib/completions.ts
index 4f1556186c..767a7add8a 100644
--- a/web/src/features/search-bar/lib/completions.ts
+++ b/web/src/features/search-bar/lib/completions.ts
@@ -1082,9 +1082,17 @@ export function planInputCompletions(
// Drop quotes so the typed text matches observed values (both the grouped
// and non-grouped value segments go through the same helper). For a glob
// value the typed text is the bare core inside the `*`s.
- const typed = stripValueQuotes(
+ const typedRaw = stripValueQuotes(
glob !== null ? glob.core : activeValueText.slice(valuePrefix.length),
);
+ // An empty OR whitespace-only value is "no value typed yet": normalize it to
+ // "" so the value stage stays a plain, unarmed list β€” no active value, no
+ // autoHighlight, no match-op refinements. Without this, a lone space typed
+ // between empty quotes (`traceName:" "`) ranked every observed value that
+ // CONTAINS a space and armed the first for Enter, committing an unwanted
+ // value (LFE-10501 BUG B). A value with real content plus surrounding spaces
+ // (`"foo "`) still counts as typed.
+ const typed = typedRaw.trim().length === 0 ? "" : typedRaw;
const groupedPlanAttrs =
grouped === null ? {} : { keepOpenOnPick: !grouped.completeGroup };
@@ -1126,6 +1134,94 @@ export function planInputCompletions(
};
}
+/**
+ * Apply a picked completion option to the draft β€” the pure text/caret half of
+ * the composer's `pickOption` (the composer keeps the DOM/selection side
+ * effects and the whole-query `recent` replacement, which is why `recent` is
+ * excluded here). Returns the rewritten draft, the caret offset to place inside
+ * it, and whether the popover should stay open.
+ *
+ * The classification is the crux: an option that INVITES MORE INPUT leaves the
+ * caret where it lands β€” a `field:` key awaiting a value, a `metadata.` prefix,
+ * an open `tags:(` group, and a comparison/logical OPERATOR awaiting its value.
+ * An option that COMPLETES a filter at the END of the draft appends a trailing
+ * space and drops the caret AFTER it (outside the finished pill), reopening
+ * field suggestions for the next filter.
+ */
+export function applyPick(
+ option: Exclude<CompletionOption, { kind: "recent" }>,
+ current: string,
+ plan: CompletionPlan,
+): { next: string; caret: number; keepOpen: boolean } {
+ let insert: string;
+ let keepOpen: boolean;
+ let replaceFrom = plan.from;
+ let replaceTo = plan.to;
+ if (option.kind === "field") {
+ // Replacing the key of an existing filter: the span ends AT the colon, so
+ // the insert must not bring its own.
+ const colonFollows = current.slice(plan.to).startsWith(":");
+ insert = option.fieldId.endsWith(".")
+ ? option.fieldId
+ : colonFollows
+ ? option.fieldId
+ : `${option.fieldId}:`;
+ keepOpen = true;
+ // A dot-prefix field (`metadata.`/`scores.`/`traceScores.`) is itself a
+ // partial key. When an existing `:value` follows, consume the whole term so
+ // the user re-picks the key from observed options instead.
+ if (option.fieldId.endsWith(".") && colonFollows) {
+ replaceTo = termAt(current, plan.from)?.to ?? plan.to;
+ }
+ } else if (option.kind === "value") {
+ insert = serializeValue(option.value);
+ keepOpen = plan.keepOpenOnPick ?? false;
+ } else {
+ insert = option.insert;
+ // A scope rewrite carries its own span (the whole coalesced free-text run),
+ // so it replaces that, not just the token under the caret.
+ if (option.kind === "pattern" && option.replaceSpan) {
+ replaceFrom = option.replaceSpan.from;
+ replaceTo = option.replaceSpan.to;
+ }
+ // A trailing `:`, ` `, or `(` drops the caret into an interactive context
+ // (value stage, next field, or an open `tags:(` group) β€” keep the popover
+ // open so the next pick is immediate.
+ keepOpen =
+ option.insert.endsWith(":") ||
+ option.insert.endsWith(" ") ||
+ option.insert.endsWith("(");
+ }
+
+ const grouped = plan.keepOpenOnPick ?? false;
+ const invitesMoreInput =
+ option.kind === "field" || // a `field:` key always needs a value next
+ // A comparison (`>`, `>=`, …) or logical (AND/NOT) OPERATOR awaits its
+ // value next, so keep the caret in the block instead of appending a space
+ // and jumping outside it. Without this, a `latency:` β†’ `>` pick landed
+ // `latency:> ` with the caret after the space, so the number typed OUTSIDE
+ // the filter (LFE-10501 BUG A). AND/NOT already carry a trailing space, so
+ // this only changes the value-stage comparison operators.
+ option.kind === "operator" ||
+ insert.endsWith(":") ||
+ insert.endsWith(" ") ||
+ insert.endsWith("(");
+ const completesFilterAtEnd =
+ !grouped &&
+ !invitesMoreInput &&
+ current.slice(replaceTo).trim().length === 0;
+ if (completesFilterAtEnd) {
+ insert += " ";
+ // Consume any existing trailing whitespace so the space never doubles.
+ replaceTo = current.length;
+ keepOpen = true;
+ }
+
+ const next =
+ current.slice(0, replaceFrom) + insert + current.slice(replaceTo);
+ return { next, caret: replaceFrom + insert.length, keepOpen };
+}
+
/** Flat option list in render order (keyboard navigation walks this). */
export function flattenOptions(
plan: CompletionPlan | null,