Skip to content

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

feat(filters): surface has:/-has: while typing a nullable field name (#16725)

Nikita Kabardinee735036cbb4
diff --git a/web/src/features/filters/config/sessionsSearchRegistry.ts b/web/src/features/filters/config/sessionsSearchRegistry.ts
index 2fbb974033..2f79f9fec3 100644
--- a/web/src/features/filters/config/sessionsSearchRegistry.ts
+++ b/web/src/features/filters/config/sessionsSearchRegistry.ts
@@ -104,6 +104,7 @@ function sessionsRegistry(config: FilterConfig, metadata: boolean) {
// by a wide margin, so a bare word means that rather than an error.
allowFreeText: false,
defaultTextField: "id",
+ hasExample: "userIds",
recentSearches: true,
searchExamples: [
"userIds:alice",
diff --git a/web/src/features/search-bar/lib/completions.ts b/web/src/features/search-bar/lib/completions.ts
index 047eb9c68f..8d3665ebb5 100644
--- a/web/src/features/search-bar/lib/completions.ts
+++ b/web/src/features/search-bar/lib/completions.ts
@@ -123,6 +123,7 @@ export const SECTION_MATCHING_FILTERS = "Matching filters";
export const SECTION_VALUES = "Observed values";
const SECTION_OPERATORS = "Operators";
const SECTION_PATTERNS = "Patterns";
+const SECTION_PRESENCE = "Presence";
export const SECTION_RECENT = "Recent searches";
export const SECTION_MATCH_OPS = "Match operators";
export const SECTION_COMPARE_OPS = "Comparisons";
@@ -229,9 +230,10 @@ function fieldOptions(
);
}
if (includeVirtual) {
- // The example names one of THIS view's nullable fields; `has:endTime` on a
- // view without endTime advertises a filter that cannot resolve.
- const example = registry.nullableFields()[0]?.id;
+ // Written per view where it matters (see FieldRegistry.hasExample); the
+ // first nullable field is only the fallback. `has:endTime` on a view
+ // without endTime advertises a filter that cannot resolve.
+ const example = registry.hasExample ?? registry.nullableFields()[0]?.id;
opts.push({
id: "field:has",
kind: "field",
@@ -415,6 +417,64 @@ function recentOptions(
);
}
+/**
+ * `has:<field>` / `-has:<field>` for the nullable field the user is typing.
+ * Presence filters are otherwise only reachable by knowing the `has:`
+ * pseudo-field exists — typing the column name never revealed them.
+ */
+function presenceOptions(
+ keyPart: string,
+ registry: FieldRegistry,
+ negated: boolean,
+ rankedFields: CompletionOption[],
+): CompletionOption[] {
+ if (keyPart.length < 2) return [];
+ const exact = registry.resolveField(keyPart);
+ const candidate =
+ exact?.type === "field"
+ ? exact.field
+ : // Not an exact match yet: offer it for the field the list is already
+ // leading with, so the options appear WHILE typing the column name.
+ (() => {
+ const top = rankedFields[0];
+ const id = top?.kind === "field" ? top.fieldId : null;
+ const ref = id === null ? null : registry.resolveField(id);
+ return ref?.type === "field" ? ref.field : null;
+ })();
+ if (candidate === null || candidate === undefined) return [];
+ if (candidate.nullable !== true) return [];
+ // The typed term already carries a `-`; insert only `has:` so the surviving
+ // dash yields `-has:` (a second dash would splice `--has:`). Label/detail
+ // must match that outcome — not the positive copy.
+ if (negated) {
+ return [
+ {
+ id: `presence:-has:${candidate.id}`,
+ kind: "pattern",
+ label: `-has:${candidate.id}`,
+ detail: `${candidate.label} is missing`,
+ insert: `has:${candidate.id} `,
+ },
+ ];
+ }
+ return [
+ {
+ id: `presence:has:${candidate.id}`,
+ kind: "pattern",
+ label: `has:${candidate.id}`,
+ detail: `${candidate.label} has a value`,
+ insert: `has:${candidate.id} `,
+ },
+ {
+ id: `presence:-has:${candidate.id}`,
+ kind: "pattern",
+ label: `-has:${candidate.id}`,
+ detail: `${candidate.label} is missing`,
+ insert: `-has:${candidate.id} `,
+ },
+ ];
+}
+
function queryPresetSections(
presetSections: QueryPresetSection[],
currentQueryText: string,
@@ -1312,6 +1372,8 @@ export function planInputCompletions(
? ranked
: hoistFieldOption(ranked, exactId, allFields);
})();
+ const presence =
+ colon === -1 ? presenceOptions(keyPart, registry, negated, fields) : [];
const operators =
colon === -1 ? rankFilter(OPERATOR_OPTIONS, tokenBody) : [];
const patterns =
@@ -1387,7 +1449,8 @@ export function planInputCompletions(
operators.length +
patterns.length +
matchingFilters.length +
- searchScopes.length ===
+ searchScopes.length +
+ presence.length ===
0
)
return null;
@@ -1414,6 +1477,7 @@ export function planInputCompletions(
// and the full-text fallback.
...section(SECTION_FIELDS, fields),
...section(SECTION_MATCHING_FILTERS, matchingFilters),
+ ...section(SECTION_PRESENCE, presence),
...section(SECTION_OPERATORS, operators),
...section(SECTION_PATTERNS, patterns),
...section(SECTION_SEARCH_IN, searchScopes),
diff --git a/web/src/features/search-bar/lib/fields.ts b/web/src/features/search-bar/lib/fields.ts
index 940889fb4d..c89b0e8263 100644
--- a/web/src/features/search-bar/lib/fields.ts
+++ b/web/src/features/search-bar/lib/fields.ts
@@ -90,6 +90,12 @@ export type FieldRegistry = {
/** Placeholder examples. Written per view, never derived from field ids: the
* events examples advertise `latency:`/`level:` on a view that has neither. */
searchExamples: readonly string[];
+ /**
+ * Field id used in the `has:` hint. Written per view like `searchExamples`:
+ * deriving it (e.g. "first nullable field") silently rewrites the hint
+ * whenever the field order changes. Null falls back to the first nullable.
+ */
+ hasExample: string | null;
/**
* Offer the project's recent searches on the empty bar. Off by default: the
* store is per PROJECT, not per view, so a view opts in and only the recents
@@ -139,6 +145,7 @@ export function fieldRegistryFromColumns(
allowFreeText?: boolean;
defaultTextField?: string;
searchExamples?: readonly string[];
+ hasExample?: string;
recentSearches?: boolean;
aiFilterPrompt?: boolean;
aiContextFields?: readonly AIContextField[];
@@ -199,6 +206,7 @@ export function fieldRegistryFromColumns(
allowFreeText: overlay.allowFreeText ?? true,
defaultTextField: overlay.defaultTextField ?? null,
searchExamples: overlay.searchExamples ?? [],
+ hasExample: overlay.hasExample ?? null,
recentSearches: overlay.recentSearches ?? false,
aiFilterPrompt: overlay.aiFilterPrompt ?? false,
aiContextFields: overlay.aiContextFields ?? [],
@@ -225,6 +233,7 @@ export function extendFieldRegistryWithColumns(
allowFreeText: registry.allowFreeText,
defaultTextField: registry.defaultTextField,
searchExamples: registry.searchExamples,
+ hasExample: registry.hasExample,
recentSearches: registry.recentSearches,
aiFilterPrompt: registry.aiFilterPrompt,
aiContextFields: registry.aiContextFields,
@@ -266,6 +275,7 @@ export function withFieldOptions(
allowFreeText: registry.allowFreeText,
defaultTextField: registry.defaultTextField,
searchExamples: registry.searchExamples,
+ hasExample: registry.hasExample,
recentSearches: registry.recentSearches,
aiFilterPrompt: registry.aiFilterPrompt,
aiContextFields: registry.aiContextFields,
@@ -377,6 +387,7 @@ function createFieldRegistry({
allowFreeText,
defaultTextField,
searchExamples,
+ hasExample,
recentSearches,
aiFilterPrompt,
aiContextFields,
@@ -390,6 +401,7 @@ function createFieldRegistry({
allowFreeText: boolean;
defaultTextField: string | null;
searchExamples: readonly string[];
+ hasExample: string | null;
recentSearches: boolean;
aiFilterPrompt: boolean;
aiContextFields: readonly AIContextField[];
@@ -424,6 +436,7 @@ function createFieldRegistry({
traceScores,
defaultTextField,
searchExamples,
+ hasExample,
recentSearches,
aiFilterPrompt,
aiContextFields,
@@ -458,6 +471,7 @@ export const EVENTS_FIELD_REGISTRY = createFieldRegistry({
"latency:>2",
"scores.accuracy:>0.8",
],
+ hasExample: "endTime",
recentSearches: true,
aiFilterPrompt: true,
aiContextFields: [
diff --git a/web/src/features/search-bar/lib/searchBarInvariants.clienttest.ts b/web/src/features/search-bar/lib/searchBarInvariants.clienttest.ts
index 1857bdb11e..d4993020f5 100644
--- a/web/src/features/search-bar/lib/searchBarInvariants.clienttest.ts
+++ b/web/src/features/search-bar/lib/searchBarInvariants.clienttest.ts
@@ -17,7 +17,11 @@ import { EXPERIMENTS_FIELD_REGISTRY } from "@/src/features/experiments/constants
import { validateQuery } from "./validate";
import { planCommit } from "./commit";
import { filterStateToQueryText } from "./filter-state-to-query";
-import { planInputCompletions } from "./completions";
+import {
+ applyPick,
+ planInputCompletions,
+ type CompletionOption,
+} from "./completions";
import {
generateQueryCases,
runSearchBarInvariants,
@@ -700,6 +704,56 @@ describe("search bar invariants — sessions registry", () => {
// Opt-in: a view that has not asked for recents gets none, valid or not.
expect(RULE_FIELD_REGISTRY.recentSearches).toBe(false);
});
+
+ it("surfaces has:/-has: while typing a nullable field name", () => {
+ const planFor = (term: string) =>
+ planInputCompletions(
+ {
+ input: term,
+ caret: term.length,
+ observed: {},
+ recents: [],
+ currentQueryText: term,
+ },
+ SESSIONS_FIELD_REGISTRY,
+ );
+ const offered = (term: string) =>
+ (planFor(term)?.sections ?? [])
+ .flatMap((section) => section.options)
+ .filter((option) => option.id.startsWith("presence:"))
+ .map((option) => option.label);
+ // Typing the column name reveals both presence forms — previously reachable
+ // only by knowing the `has:` pseudo-field existed.
+ expect(offered("userId")).toEqual(["has:userIds", "-has:userIds"]);
+ // Mid-word too, so they appear while typing rather than only on a full match.
+ expect(offered("userI")).toEqual(["has:userIds", "-has:userIds"]);
+ // A negated term already carries the `-`; the sole option is the missing
+ // form (label matches what applyPick commits once the surviving dash joins
+ // the inserted `has:`). Offering `-has:` as insert would splice `--has:`.
+ expect(offered("-userIds")).toEqual(["-has:userIds"]);
+ const negatedPlan = planFor("-userIds");
+ const missing = (negatedPlan?.sections ?? [])
+ .flatMap((section) => section.options)
+ .find((option) => option.id === "presence:-has:userIds");
+ expect(missing).toBeDefined();
+ expect(
+ applyPick(
+ missing as Exclude<CompletionOption, { kind: "recent" | "preset" }>,
+ "-userIds",
+ negatedPlan!,
+ ).next,
+ ).toBe("-has:userIds ");
+ // Non-nullable and too-short terms stay quiet.
+ expect(offered("countTraces")).toEqual([]);
+ expect(offered("u")).toEqual([]);
+ });
+
+ it("pins the has: hint per view instead of deriving it from field order", () => {
+ // Derived from "first nullable field", the GA events hint silently became
+ // has:name; it is written down so reordering FIELDS cannot rewrite copy.
+ expect(EVENTS_FIELD_REGISTRY.hasExample).toBe("endTime");
+ expect(SESSIONS_FIELD_REGISTRY.hasExample).toBe("userIds");
+ });
});
const experimentsView: RegistryUnderTest = {