Skip to content

: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.

feat(aio): search inside a trace (#124362)

Carlos Marchal3c50b359d4f3
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 52dd430ac6a6..e0517a43b640 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -9702,6 +9702,10 @@ snapshots:
hash: v1.k794b7964.772a6f824d906394bf1ba626cf6e5f3d754316599d9c89e95e781cef8d4655bd.9hH4Z90sTumOjlOdrtyXYXTm_WniKPgGHs1kSy8PAig
scenes-app-ai-observability-trace-view-thread--system-only--light:
hash: v1.k794b7964.452eb12baf987aa0e5cfb52c1a10a6e5c51b92ec186827759a01bbfdb4cee491.ZIlIxIh-lQNXA-dEuGofnrenjVRjGgF8uch_9fp-WqU
+ scenes-app-ai-observability-trace-view-thread--with-search-highlight--dark:
+ hash: v1.k794b7964.ad2a89f50876f598bb5905ace7073c7bb3d7962c72dd2d77f8466d14be7bccac.6vg25WfCAw85CsFB3KvBMlli5IECfwXK2zmew0vMx2U
+ scenes-app-ai-observability-trace-view-thread--with-search-highlight--light:
+ hash: v1.k794b7964.cc590ca4811e32018d62064fb1241af4a971385168941af11b39ee03345d5eba.Emq-SWBXyKSRRpevjenYmiGWFKtR71v_pvPXNai5CSY
scenes-app-ai-observability-trace-view-thread-bubble--assistant--dark:
hash: v1.k794b7964.81506360f7ed9cb1493c6185af28fad6ba393c4fdc4182f0ed58082a61feb127.3WezhJg9DLucKhiP-EYc3a95RZrZjhkRswT9SDzz7mc
scenes-app-ai-observability-trace-view-thread-bubble--assistant--light:
@@ -9742,6 +9746,10 @@ snapshots:
hash: v1.k794b7964.27a5697d3abcb7ec0beef7c786d7a062d4cad10661f4f2b0a12fd13f18e68ca2.QFyabg8rXByyhb9o4PrGGt4mcRuHOuqEJ9zKTwGNSnE
scenes-app-ai-observability-trace-view-trace-header--narrow--light:
hash: v1.k794b7964.9fca5efe3c2de8d82b73848a8d53c57cc8beb108f927b4243c83f5e16e6814db.dlDT_i8yO4AuXDskXau5HcG0BzAI_n0LYfiX8aENnIw
+ scenes-app-ai-observability-trace-view-trace-header--with-search--dark:
+ hash: v1.k794b7964.aa0454ec525f326773af3ab8881a60f981feedb06714be27b9b44a594e0fdf42.84mnlZI9H7SWkfSDjNvp-sAYDTdT7lY2vi8RNNJFEXg
+ scenes-app-ai-observability-trace-view-trace-header--with-search--light:
+ hash: v1.k794b7964.6aa55cf290ef829c19219427567d7beac78cf49270e189658db3a429567bbe41.qs3xFQxD-qunCuupDH25kZaQ9FbU8XRGtdeGjGULYyg
scenes-app-ai-observability-trace-view-trace-mode-tabs--default--dark:
hash: v1.k794b7964.438ee912f36d4f19ccec0b12f645b823068e4a5103daef99effe519602b07c07.757Ls8z7uxMrtEqvXV0u69Mo0onygkWWCfZvys0zLw0
scenes-app-ai-observability-trace-view-trace-mode-tabs--default--light:
@@ -9750,6 +9758,34 @@ snapshots:
hash: v1.k794b7964.4d6f84c870fe9b3fb3648dfe92a798a76849f7c97b0ee2e1558b30866e8ed274.fOeyVQsBh9OfZRZnRzhoUQ9pnnoJrW0OBNvyBldR5Lo
scenes-app-ai-observability-trace-view-trace-mode-tabs--with-feedback-count--light:
hash: v1.k794b7964.7cc0c8d5f26b9f7ded485d8b5366a5496b5cb0104742ad305af8ac1194cbe070.-LKvyqtdXXL8XDIkB2alL1uqfXpXTrKaKCkPvAZNBCA
+ scenes-app-ai-observability-trace-view-trace-search-box--failed--dark:
+ hash: v1.k794b7964.cd6e8fc2f27d579b2b8729e678b7bde8c4415e118d4e56fc7a7b9e5a99b6ed84.djQdm1nJjks_9clVef2eZKyUuMjvoWrifyWmsUgIo1s
+ scenes-app-ai-observability-trace-view-trace-search-box--failed--light:
+ hash: v1.k794b7964.e516e3fc464569538fb06988e150e96d6dd054a1770e68a36f8714fce693b7a3.UO-5cT_mTokEBJdR3kAJzeLINdQPGOM7sEx9VinntAY
+ scenes-app-ai-observability-trace-view-trace-search-box--idle--dark:
+ hash: v1.k794b7964.0d9493dc53f4242ab51344d9b75c15c10b33eb443810ec57aa365936f023b2b1.mZ8yVntiGcsC2LDduPAQnRJnMPiZvFJumPdy036HrKU
+ scenes-app-ai-observability-trace-view-trace-search-box--idle--light:
+ hash: v1.k794b7964.41e2d8e4367e5946a587e874cd00f6d498bfebcf52f956731280b538d563dde5.YaauWv41HL_7Ye_YBafXxBFaHceLrMz8PSP9oIfafWE
+ scenes-app-ai-observability-trace-view-trace-search-box--match-selected--dark:
+ hash: v1.k794b7964.db48d18d864d9df7f228171f7b5f8b7b5ae16fdcb60e5572e9f6428499b4f60f.FWi2VBWRfQC3PqFS2v8-PnbKZr6ITJXamFdsuYA_XVs
+ scenes-app-ai-observability-trace-view-trace-search-box--match-selected--light:
+ hash: v1.k794b7964.438988c2f1f4b60ebc2e82a48d0881d955b25bda7f01ecffa3545b690b6345df.O-u77CFU48w3RladxElY213_q8RXstMIsbUoZ3zDrQA
+ scenes-app-ai-observability-trace-view-trace-search-box--matches--dark:
+ hash: v1.k794b7964.0c99101cdba6377bfb40f615d3c3d7762c89ccc66c62370feffe8a7b0e11f146.JIg9Q0oBRCnKcW1o_IqRvFpRKCqpwM3LLSKBROSDYEM
+ scenes-app-ai-observability-trace-view-trace-search-box--matches--light:
+ hash: v1.k794b7964.5cfee5f120ea2ef442306375e305aa7a1397da075275a9d7fee8ab8daa44ae0c.YMfiXnPFUuzA56VLdNIZBFn1DpzQ8EumYd7bGTi94sI
+ scenes-app-ai-observability-trace-view-trace-search-box--no-matches--dark:
+ hash: v1.k794b7964.c3c9cd88a3377ea9cf7014c30373f381acaa309f6ba6da7d33ca31ffd2e90dab.PdTmnx5LXz8acn3x644FcAtfYXuPtttAdFz5HR0vWno
+ scenes-app-ai-observability-trace-view-trace-search-box--no-matches--light:
+ hash: v1.k794b7964.0961fd7fb23596020f2898d4bfaf2f7d6a062cde2a765a9815e7514bc689a319.3pGCCHEEnu10Cj-dyqZWrp3KMCy8TTY8Qv_U0EnGZRQ
+ scenes-app-ai-observability-trace-view-trace-search-box--searching--dark:
+ hash: v1.k794b7964.e3e704978b8dd0e36af718101d49d89a8e3cf666939c5e06cd315fa1a283e28d.KE_Bek-Pswer-2m_DJmDKnZpmCkJ4SxD9uXy2Jg8EVg
+ scenes-app-ai-observability-trace-view-trace-search-box--searching--light:
+ hash: v1.k794b7964.b32bfa79a26f0c865ef65886c99588f316cd017a741da2bf8c37b20cdd68d206.1HgqBVgK2VwYgoziTui9CadFlBmwRL5TTSi-sMR6FGc
+ scenes-app-ai-observability-trace-view-trace-search-box--too-slow--dark:
+ hash: v1.k794b7964.32f4045aadf6fe0e2177d7112cbf324f66cc252ba56d3e458fe6a74de040e2df.SchWD7UAgUn56m8ASaThsBhKXjKVtIa8QAoOEYVT8f8
+ scenes-app-ai-observability-trace-view-trace-search-box--too-slow--light:
+ hash: v1.k794b7964.6f195c15d6fae05d1bcc9435087c623cc7fefa32f35599c6a3b2f543e5d7151a.iRcwQNkJf9Zv2I7xfOGImy2mjsVqocGd2qHKHfBMg2M
scenes-app-ai-observability-trace-view-trace-split-view--narrow--dark:
hash: v1.k794b7964.7e4eef01f6f5dca0671db52b44959fdc401a61dec4d72ca3730a7276033c73ee.NS9WNST2qDaBnvQacs6Lnro9dedItLwYX7M40RNgJxo
scenes-app-ai-observability-trace-view-trace-split-view--narrow--light:
@@ -9818,6 +9854,10 @@ snapshots:
hash: v1.k794b7964.7730a2b3770b10e409eef4c6a86610831683b14aa08145997147ca2cb6bf0795.0qxynAXIKe7WjkMoQNTZwDXLKbHrJVIjIh3ZwzaOPew
scenes-app-ai-observability-trace-view-trace-timeline--unknown-latency--light:
hash: v1.k794b7964.5647196090ec187214388c33b9a880a56038e4817bddb4cb216126f20e84b578.J4-wMZdmCzadsiUDl5ir54BgY-gcV4R09j2A6YoDwT8
+ scenes-app-ai-observability-trace-view-trace-timeline--with-search-matches--dark:
+ hash: v1.k794b7964.a5771934bf343414cd04ce83185928d53c2025d3013433fc113027598984827d.cX9c0IOq9ImKdqn_6_j2Kd2DGqTDy73P0x_9RbY4gHY
+ scenes-app-ai-observability-trace-view-trace-timeline--with-search-matches--light:
+ hash: v1.k794b7964.9391cfe620a305bcfb56ed636d3c52023130745d83137dfe3c5811effb626e41.o7boWEa3LxpuULQco9xmNUKNrd8ewu_tJywIUQcKxYY
scenes-app-ai-observability-trace-view-trace-tree--deep-agent-tree--dark:
hash: v1.k794b7964.53e1d8f04927c1fd17a2d63017f8ff66c9c5a43c2ac13385dd24d44e3baa21dc.8oaBpscoUfIlPXZul7-mdIF_Ho3VmTK_sbeomLUaRSs
scenes-app-ai-observability-trace-view-trace-tree--deep-agent-tree--light:
@@ -9838,6 +9878,10 @@ snapshots:
hash: v1.k794b7964.c6cd54aabda6eef28a4514347459d2a599b0e381940dbba7d2a59e1ccdcc532d.7Ae1tlm2Y3ZYceOrhcnzv7Oga1YutazxEoehY1zMZco
scenes-app-ai-observability-trace-view-trace-tree--with-feedback-markers--light:
hash: v1.k794b7964.391466dbd7a7552e5e08a538864c400041ad512e957dbe68d845d99b74753dd5.HI6rvM5LIg8SjUwVEnIQNRRXlcfIP6B8Kw9qQ6ExqWE
+ scenes-app-ai-observability-trace-view-trace-tree--with-search-matches--dark:
+ hash: v1.k794b7964.56b5d00853ac62e9910aa88d251b0810a7250540399b3abc40615a7ecc998778.0fkT-o3z6EX1a80VQI19_IIEFtkasuth8hBRALXRmK0
+ scenes-app-ai-observability-trace-view-trace-tree--with-search-matches--light:
+ hash: v1.k794b7964.8371437af66a899465e8b6af209192d8faf8908b085b3ac845c286fdd571a1c0.-QUndAyEEy3bilMOkmUg1mKqV2a9wXYtwqUv6lq-zMc
scenes-app-ai-observability-trace-view-trace-view--error-trace--dark:
hash: v1.k794b7964.a497ad504c6a9d0d425173914797467cb239bd4a2bb2cc25afb26c81eea3cdfd.1XmbemnPvJjwGHM3Jev1TAMsYMNdyx6ykWGU2d-qq2k
scenes-app-ai-observability-trace-view-trace-view--error-trace--light:
@@ -9866,6 +9910,10 @@ snapshots:
hash: v1.k794b7964.1ec15d0977dfc6ba6ce0244d4a128cb29e0626e8bb0a8ef68abc1133c6866923.uGAbbvbbsX8u4xKeQqFQv7rImzUP0OPv4kMslKy30aY
scenes-app-ai-observability-trace-view-trace-view--narrow-with-evals--light:
hash: v1.k794b7964.6129305fc216488d475d85c4a37d01d76114c241395233fc6e34d7bedb73c7cd.hkKR3Hq1HrBBKZk0aYwMNNZYk9fKt7e5D_36eoqSXcs
+ scenes-app-ai-observability-trace-view-trace-view--narrow-with-search--dark:
+ hash: v1.k794b7964.3b171bcede81eae28d091c8433f7dfbd6ae1a4a8ec50a404d5a9a47f683bd8c2.TcJM6FGXA5D4h78VVph1QsmAOP5nNyQXtM2PJt5bm8c
+ scenes-app-ai-observability-trace-view-trace-view--narrow-with-search--light:
+ hash: v1.k794b7964.cb6943150304640bb70cf1a3360b44711e24d9d519718d1925ad8dd5ded2ed69.VNcexyRs4WBghuIIMjyjqR4rsV1NmxYQh8tyAHZc8x4
scenes-app-ai-observability-trace-view-trace-view--sample-anthropic-thinking-tool-use--dark:
hash: v1.k794b7964.ee72ae95c2c0db21361b7924fb3f7db28af93dae0f3773ad4530f847f95fc20c.1m0893IlRbuhUW5V-caBPubusOcO8QdDfOSfYt_iA5k
scenes-app-ai-observability-trace-view-trace-view--sample-anthropic-thinking-tool-use--light:
diff --git a/frontend/src/initKea.ts b/frontend/src/initKea.ts
index 7b74f709ca70..c021759ec4c1 100644
--- a/frontend/src/initKea.ts
+++ b/frontend/src/initKea.ts
@@ -138,6 +138,7 @@ const ERROR_FILTER_ALLOW_LIST_BY_LOGIC_PATH: Record<string, string> = {
deleteProviderKey: 'products.ai_observability.settings.llmProviderKeysLogic',
loadEvaluationResults: 'products.ai_observability.frontend.redesign.trace.traceViewLogic', // The trace view's Evals tab shows the error
loadTraceFeedback: 'products.ai_observability.frontend.redesign.trace.traceFeedbackLogic', // The trace view's Feedback mode shows the error
+ loadSearchResults: 'products.ai_observability.frontend.redesign.trace.traceSearchLogic', // The trace view's search box shows the error
// The trace view's Summary mode shows the error
loadSummary: 'products.ai_observability.frontend.redesign.trace.traceSummaryLogic',
requestSummary: 'products.ai_observability.frontend.redesign.trace.traceSummaryLogic',
diff --git a/products/ai_observability/backend/facade/api.py b/products/ai_observability/backend/facade/api.py
index 6f51e3d5f944..f2abf859d088 100644
--- a/products/ai_observability/backend/facade/api.py
+++ b/products/ai_observability/backend/facade/api.py
@@ -37,7 +37,14 @@ from ..logic.traces.summary_generation import generate_summary
from ..logic.traces.summary_store import fail_abandoned_summary, request_summary, viewer_summary
from ..logic.traces.timeline import TimelineRow
from ..logic.traces.trace import Trace
-from ..logic.traces.trace_queries import load_trace, load_trace_feedback_rows, load_trace_node, load_trace_root
+from ..logic.traces.trace_queries import (
+ load_trace,
+ load_trace_feedback_rows,
+ load_trace_node,
+ load_trace_root,
+ load_trace_search,
+)
+from ..logic.traces.trace_search import TraceSearchResult
from ..logic.traces.tree import TreeNode
from ..logic.traces.usage import Usage
from ..temporal.trace_summary_client import start_trace_summary
@@ -194,6 +201,20 @@ def _to_node_detail(node_id: str, event: NodeEvent) -> contracts.TraceNodeDetail
)
+def search_trace(
+ team: Team, user: User | None, trace_id: str, query: str, timestamp_hint: datetime | None
+) -> contracts.TraceSearchResults:
+ """Raises `TraceNotFoundError`, and `ClickHouseQueryTimeOut` when the search outlasts its time limit."""
+ loaded = load_trace_search(team, user, trace_id, query, timestamp_hint)
+ if not loaded.rows:
+ raise TraceNotFoundError(trace_id)
+ result = TraceSearchResult.of(Trace(trace_id, loaded.rows, None), loaded.counts, query)
+ return contracts.TraceSearchResults(
+ total=result.total,
+ matches=[contracts.TraceSearchMatch(node_id=match.node_id, count=match.count) for match in result.matches],
+ )
+
+
def resolve_evaluation_pass_markers[P](properties: list[P], team_id: int) -> list[P]:
"""Replace each evaluation pass marker with the team's current condition."""
markers = {contracts.EVALUATION_GRADED_MARKER, contracts.EVALUATION_PASSED_MARKER}
diff --git a/products/ai_observability/backend/facade/contracts.py b/products/ai_observability/backend/facade/contracts.py
index 85fbee9b547f..d4be062c8a6c 100644
--- a/products/ai_observability/backend/facade/contracts.py
+++ b/products/ai_observability/backend/facade/contracts.py
@@ -201,6 +201,20 @@ class TraceFeedback(Contract):
summary: TraceFeedbackSummary
+class TraceSearchMatch(Contract):
+ node_id: str = Field(description="A step whose stored data contains the query. The root's id is the trace id.")
+ count: int = Field(
+ description="How many times the query occurs in the step's stored inputs, outputs, state, tools, name, model, provider and error, ignoring case. Keys of the stored JSON count too."
+ )
+
+
+class TraceSearchResults(Contract):
+ total: int = Field(description="Occurrences of the query across every matching step.")
+ matches: list[TraceSearchMatch] = Field(
+ description="Matching steps in tree order, the root first. Steps without a match are left out."
+ )
+
+
TraceSummaryStatus = Literal["pending", "ready", "failed"]
TraceSummaryFailureReason = Literal[
"trace_not_found", "too_large", "timeout", "provider_error", "not_approved", "access_changed", "error"
diff --git a/products/ai_observability/backend/logic/traces/trace_queries.py b/products/ai_observability/backend/logic/traces/trace_queries.py
index ee45ff372a68..2d7ef41fdf9b 100644
--- a/products/ai_observability/backend/logic/traces/trace_queries.py
+++ b/products/ai_observability/backend/logic/traces/trace_queries.py
@@ -1,13 +1,16 @@
import json
import math
from collections.abc import Callable, Sequence
+from dataclasses import replace
from datetime import datetime
-from typing import Any
+from functools import reduce
+from typing import Any, Self
from uuid import UUID
import structlog
from posthog.hogql import ast
+from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.context import HogQLContext
from posthog.hogql.database.database import Database
from posthog.hogql.query import execute_hogql_query
@@ -237,6 +240,7 @@ def _read(
window: FallbackWindow | None,
*,
event_names: Sequence[str] = TRACE_EVENT_NAMES,
+ settings: HogQLGlobalSettings | None = None,
) -> list[Sequence[Any]]:
tag_queries(name=name)
response = query_ai_events(
@@ -252,6 +256,7 @@ def _read(
)
},
posthog_tables_only=True,
+ settings=settings,
)
return list(response.results or [])
@@ -360,16 +365,26 @@ def _read_trace(
*,
widen_unhinted: bool,
event_names: Sequence[str] = TRACE_EVENT_NAMES,
+ settings: HogQLGlobalSettings | None = None,
) -> list[Sequence[Any]]:
if timestamp_hint is not None:
return _read(
- name, build_query(), team, user, trace_id, fallback_window(timestamp_hint), event_names=event_names
+ name,
+ build_query(),
+ team,
+ user,
+ trace_id,
+ fallback_window(timestamp_hint),
+ event_names=event_names,
+ settings=settings,
)
- results = _read(name, build_query(), team, user, trace_id, None, event_names=event_names)
+ results = _read(name, build_query(), team, user, trace_id, None, event_names=event_names, settings=settings)
if results or not widen_unhinted:
return results
logger.warning("ai_trace_unhinted_events_fallback", team_id=team.pk, trace_id_length=len(trace_id), read=name)
- return _read(name, build_query(), team, user, trace_id, unhinted_window(), event_names=event_names)
+ return _read(
+ name, build_query(), team, user, trace_id, unhinted_window(), event_names=event_names, settings=settings
+ )
def load_trace(team: Team, user: User | None, trace_id: str, timestamp_hint: datetime | None) -> LoadedTrace:
@@ -536,3 +551,99 @@ def load_trace_feedback_rows(
NODE_EVENT_NAMES,
)
return (*feedback, *steps)
+
+
+_SEARCH_TIMEOUT_SECONDS = 10
+_JSON_SEARCHED = (*(ast.Field(chain=[column]) for column in _HEAVY_COLUMNS), _raw_property("$ai_error"))
+_TEXT_SEARCHED = tuple(ast.Field(chain=[column]) for column in ("span_name", "trace_name", "model", "provider"))
+_ESCAPED_BACKSLASH = "\\\\"
+_ESCAPED_BACKSLASH_MARK = "\x01"
+
+
+def _json_escaped(value: str, *, ascii_only: bool = False) -> str:
+ return json.dumps(value, ensure_ascii=ascii_only)[1:-1]
+
+
+@frozen
+class SearchNeedles:
+ text: str
+ # The heavy columns hold raw JSON, so quotes and backslashes in the query match only in their escaped form. A
+ # string that holds JSON, such as tool arguments, keeps its own escapes and is escaped a second time around them.
+ json: tuple[str, ...]
+
+ @classmethod
+ def of(cls, query: str) -> Self:
+ escaped = _json_escaped(query)
+ needles = [escaped]
+ for nested in (_json_escaped(_json_escaped(query, ascii_only=True)), _json_escaped(escaped)):
+ # Each occurrence of a nested needle that holds a kept needle is already counted once.
+ if not any(kept.lower() in nested.lower() for kept in needles):
+ needles.append(nested)
+ return cls(text=query, json=tuple(needles))
+
+
+@frozen
+class LoadedTraceSearch:
+ rows: tuple[TraceEventRow, ...]
+ counts: dict[str, int]
+
+
+def _search_settings() -> HogQLGlobalSettings:
+ # Search reads every stored byte of the trace, so it fails fast instead of holding a request for the default minute.
+ return HogQLGlobalSettings(max_execution_time=_SEARCH_TIMEOUT_SECONDS, timeout_overflow_mode="throw")
+
+
+def _occurrences(haystack: ast.Expr, needle: str) -> ast.Expr:
+ return ast.Call(
+ name="ifNull",
+ args=[
+ ast.Call(name="countSubstringsCaseInsensitiveUTF8", args=[haystack, ast.Constant(value=needle)]),
+ ast.Constant(value=0),
+ ],
+ )
+
+
+def _json_occurrences(column: ast.Expr, needle: str) -> ast.Expr:
+ if not needle.startswith("\\"):
+ return _occurrences(column, needle)
+ # A needle that starts with a backslash can match the second half of an escaped backslash, as `\"` matches the
+ # end of `"C:\\"`. Raw JSON never holds a raw control byte, so each escaped backslash becomes one such byte first.
+ marked = ast.Call(
+ name="replaceAll",
+ args=[column, ast.Constant(value=_ESCAPED_BACKSLASH), ast.Constant(value=_ESCAPED_BACKSLASH_MARK)],
+ )
+ return _occurrences(marked, needle.replace(_ESCAPED_BACKSLASH, _ESCAPED_BACKSLASH_MARK))
+
+
+def _trace_search_query(needles: SearchNeedles) -> ast.SelectQuery:
+ counts = [
+ *(_json_occurrences(column, needle) for column in _JSON_SEARCHED for needle in needles.json),
+ *(_occurrences(column, needles.text) for column in _TEXT_SEARCHED),
+ ]
+ match_count = reduce(lambda left, right: ast.Call(name="plus", args=[left, right]), counts)
+ query = _trace_rows_query()
+ return replace(query, select=[*query.select, ast.Alias(alias="match_count", expr=match_count)])
+
+
+def load_trace_search(
+ team: Team, user: User | None, trace_id: str, query: str, timestamp_hint: datetime | None
+) -> LoadedTraceSearch:
+ needles = SearchNeedles.of(query)
+ results = _read_trace(
+ "ai_trace_search",
+ lambda: _trace_search_query(needles),
+ team,
+ user,
+ trace_id,
+ timestamp_hint,
+ widen_unhinted=False,
+ settings=_search_settings(),
+ )
+ rows: list[TraceEventRow] = []
+ counts: dict[str, int] = {}
+ for *values, match_count in results:
+ row = _trace_event_row(values)
+ rows.append(row)
+ if match_count:
+ counts[row.uuid] = int(match_count)
+ return LoadedTraceSearch(rows=tuple(rows), counts=counts)
diff --git a/products/ai_observability/backend/logic/traces/trace_search.py b/products/ai_observability/backend/logic/traces/trace_search.py
new file mode 100644
index 000000000000..463a3ebd68b7
--- /dev/null
+++ b/products/ai_observability/backend/logic/traces/trace_search.py
@@ -0,0 +1,34 @@
+from collections.abc import Mapping
+from typing import Self
+
+from posthog.dataclasses import frozen
+
+from .trace import Trace
+
+
+@frozen
+class NodeMatch:
+ node_id: str
+ count: int
+
+
+@frozen
+class TraceSearchResult:
+ total: int
+ matches: tuple[NodeMatch, ...]
+
+ @classmethod
+ def of(cls, trace: Trace, counts: Mapping[str, int], query: str) -> Self:
+ """`counts` holds occurrences by event uuid. The root carries the counts of the trace's `$ai_trace` events, or
+ the occurrences in its title when it has none, because the root then shows a step's name."""
+ root = NodeMatch(node_id=trace.id, count=_root_count(trace, counts, query))
+ rows = (NodeMatch(node_id=row.event.id, count=counts.get(row.event.id, 0)) for row in trace.timeline.rows)
+ matches = tuple(match for match in (root, *rows) if match.count > 0)
+ return cls(total=sum(match.count for match in matches), matches=matches)
+
+
+def _root_count(trace: Trace, counts: Mapping[str, int], query: str) -> int:
+ trace_events = [event for event in trace.events if event.is_trace_event]
+ if trace_events:
+ return sum(counts.get(event.id, 0) for event in trace_events)
+ return trace.title.lower().count(query.lower())
diff --git a/products/ai_observability/backend/presentation/views.py b/products/ai_observability/backend/presentation/views.py
index e201a2219360..d78f8452a3b5 100644
--- a/products/ai_observability/backend/presentation/views.py
+++ b/products/ai_observability/backend/presentation/views.py
@@ -37,6 +37,10 @@ class TraceQuerySerializer(serializers.Serializer):
return value
+class TraceSearchQuerySerializer(TraceQuerySerializer):
+ q = serializers.CharField(max_length=200, help_text="The text to find in the trace's steps, ignoring case.")
+
+
TRACE_ID_PARAMETER = OpenApiParameter(
"id",
OpenApiTypes.STR,
@@ -62,7 +66,7 @@ class TraceSummaryRequestSerializer(serializers.Serializer):
class TraceViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
scope_object = "llm_analytics"
- scope_object_read_actions = ["retrieve", "node", "evaluations", "feedback", "summary"]
+ scope_object_read_actions = ["retrieve", "node", "evaluations", "feedback", "search", "summary"]
scope_object_write_actions = ["request_summary"]
# Traces are project-wide data, so a grant on a single llm_analytics object must not open them.
requires_resource_level_access = True
@@ -164,6 +168,34 @@ class TraceViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
feedback = self._read(request, lambda user, hint: api.get_trace_feedback(self.team, user, trace_id, hint))
return Response(feedback.model_dump(mode="json", by_alias=True))
+ @validated_request(
+ query_serializer=TraceSearchQuerySerializer,
+ parameters=[TRACE_ID_PARAMETER],
+ responses={
+ 200: OpenApiResponse(response=contracts.TraceSearchResults),
+ 400: OpenApiResponse(
+ description="The query is missing or too long, or the timestamp hint or the encoded trace id is malformed."
+ ),
+ 404: OpenApiResponse(description="No trace with this id in the project."),
+ 429: THROTTLED_RESPONSE,
+ 504: OpenApiResponse(description="The search took too long on this trace."),
+ },
+ operation_id="ai_observability_traces_search_retrieve",
+ )
+ @action(detail=True, methods=["GET"], url_path="search")
+ def search(self, request: ValidatedRequest, pk: str, **kwargs: Any) -> Response:
+ """Find text in a trace's steps: each matching step in tree order with its number of occurrences."""
+ try:
+ trace_id = decode_trace_id_segment(pk)
+ except MalformedTraceIdSegmentError:
+ return Response({"detail": "Malformed encoded trace id."}, status=status.HTTP_400_BAD_REQUEST)
+ query = request.validated_query_data["q"]
+ try:
+ results = self._read(request, lambda user, hint: api.search_trace(self.team, user, trace_id, query, hint))
+ except api.TraceNotFoundError:
+ return Response({"detail": "Trace not found."}, status=status.HTTP_404_NOT_FOUND)
+ return Response(results.model_dump(mode="json", by_alias=True))
+
def get_throttles(self) -> list[BaseThrottle]:
# Shares the legacy summary endpoint's scopes, so a user has one summarization budget. Reads poll, so only
# requests spend it.
diff --git a/products/ai_observability/backend/tests/logic/traces/test_trace_queries.py b/products/ai_observability/backend/tests/logic/traces/test_trace_queries.py
index bf35f47a69c4..f627a28d9f88 100644
--- a/products/ai_observability/backend/tests/logic/traces/test_trace_queries.py
+++ b/products/ai_observability/backend/tests/logic/traces/test_trace_queries.py
@@ -12,12 +12,14 @@ from posthog.models.ai_events.test_util import bulk_create_ai_events
from posthog.models.event.util import bulk_create_events
from products.ai_observability.backend.logic.traces.trace_queries import (
+ SearchNeedles,
TraceRootLookup,
is_truthy_json,
load_trace,
load_trace_feedback_rows,
load_trace_node,
load_trace_root,
+ load_trace_search,
)
T0 = datetime(2026, 9, 1, 10, 0, tzinfo=UTC)
@@ -57,6 +59,20 @@ def test_is_truthy_json(_name: str, raw: str, expected: bool) -> None:
assert is_truthy_json(raw) is expected
+ [
+ ("plain text", "invoice", "invoice", ("invoice",)),
+ ("a double quote", 'say "hi"', 'say "hi"', ('say \\"hi\\"', 'say \\\\\\"hi\\\\\\"')),
+ ("a backslash", "C:\\tmp", "C:\\tmp", ("C:\\\\tmp", "C:\\\\\\\\tmp")),
+ ("non-ASCII text", "café", "café", ("café", "caf\\\\u00e9")),
+ ]
+)
+def test_search_needles_match_text_and_stored_json(
+ _name: str, query: str, text: str, json_needles: tuple[str, ...]
+) -> None:
+ assert SearchNeedles.of(query) == SearchNeedles(text=text, json=json_needles)
+
+
class TraceEventsMixin:
team: Team
@@ -371,3 +387,64 @@ class TestEventsFallback(TraceEventsMixin, ClickhouseTestMixin, BaseTest):
assert load_trace_node(self.team, None, "trace-1", NODE, None) is None
assert load_trace_root(self.team, None, "trace-1", None) == TraceRootLookup(trace_event=None, trace_found=False)
+
+ def test_search_falls_back_to_events_with_a_hint_only(self) -> None:
+ bulk_create_events([self._event(NODE, properties={"$ai_span_name": "Fetch invoice"})])
+
+ assert load_trace_search(self.team, None, "trace-1", "invoice", T0).counts == {NODE: 1}
+ assert load_trace_search(self.team, None, "trace-1", "invoice", None).rows == ()
+
+
+SPAN = "00000000-0000-0000-0000-000000000011"
+GENERATION = "00000000-0000-0000-0000-000000000012"
+QUIET_GENERATION = "00000000-0000-0000-0000-000000000013"
+
+
+class TestLoadTraceSearch(TraceEventsMixin, ClickhouseTestMixin, BaseTest):
+ def test_counts_occurrences_per_event_across_content_and_names(self) -> None:
+ bulk_create_ai_events(
+ [
+ self._event(
+ GENERATION,
+ event="$ai_generation",
+ properties={"$ai_input": [{"role": "user", "content": "Invoice total? The invoice is late."}]},
+ ),
+ self._event(
+ SPAN, properties={"$ai_span_name": "Fetch invoice", "$ai_output_state": {"note": "nothing here"}}
+ ),
+ self._event(QUIET_GENERATION, event="$ai_generation", properties={"$ai_model": "gpt-4o-mini"}),
+ ]
+ )
+
+ loaded = load_trace_search(self.team, None, "trace-1", "INVOICE", None)
+
+ assert loaded.counts == {GENERATION: 2, SPAN: 1}
+ assert len(loaded.rows) == 3
+
+ @parameterized.expand(
+ [
+ ("a quoted phrase", '"late"', {"$ai_input": [{"content": 'It said "late" twice'}]}, 1),
+ ("a backslash", "C:\\tmp", {"$ai_output": "saved to C:\\tmp"}, 1),
+ ("a percent sign", "50%", {"$ai_output": "50% done, 500 left"}, 1),
+ ("an underscore", "a_b", {"$ai_output": "a_b and axb"}, 1),
+ ("an error object", "quota", {"$ai_error": {"message": "Quota exceeded"}}, 1),
+ ("a quote after a trailing backslash", '"', {"$ai_output": "saved to C:\\"}, 0),
+ ("backslashes before a quote", '\\"', {"$ai_output": 'C:\\\\"x" and \\\\'}, 1),
+ (
+ "non-ASCII text in a JSON string",
+ "2 × 3",
+ {"$ai_input": [{"arguments": json.dumps({"op": "2 × 3"})}]},
+ 1,
+ ),
+ ("quotes in a JSON string", '"', {"$ai_output": json.dumps('say "hi"')}, 4),
+ ("a model", "GPT-4O", {"$ai_model": "gpt-4o-mini"}, 1),
+ ("a provider", "anthropic", {"$ai_provider": "anthropic"}, 1),
+ ("a trace name", "checkout", {"$ai_trace_name": "Checkout flow"}, 1),
+ ]
+ )
+ def test_matches_text_as_stored_in_json(
+ self, _name: str, query: str, properties: dict[str, object], expected: int
+ ) -> None:
+ self._ingest(NODE, properties, event="$ai_generation")
+
+ assert load_trace_search(self.team, None, "trace-1", query, None).counts.get(NODE, 0) == expected
diff --git a/products/ai_observability/backend/tests/logic/traces/test_trace_search.py b/products/ai_observability/backend/tests/logic/traces/test_trace_search.py
new file mode 100644
index 000000000000..92c40c4f7a21
--- /dev/null
+++ b/products/ai_observability/backend/tests/logic/traces/test_trace_search.py
@@ -0,0 +1,42 @@
+from datetime import timedelta
+
+from products.ai_observability.backend.logic.traces.trace import Trace
+from products.ai_observability.backend.logic.traces.trace_search import NodeMatch, TraceSearchResult
+
+from .rows import T0, make_row
+
+TRACE = Trace(
+ "trace-1",
+ [
+ make_row(uuid="root-event", event="$ai_trace", parent_id=None, timestamp=T0),
+ make_row(uuid="span-1", span_id="s1", timestamp=T0 + timedelta(seconds=1)),
+ make_row(uuid="gen-1", event="$ai_generation", parent_id="s1", timestamp=T0 + timedelta(seconds=2)),
+ make_row(uuid="gen-2", event="$ai_generation", timestamp=T0 + timedelta(seconds=3)),
+ ],
+ None,
+)
+
+
+def test_matches_follow_the_tree_with_the_root_first() -> None:
+ result = TraceSearchResult.of(TRACE, {"gen-2": 1, "gen-1": 4, "root-event": 2, "span-1": 0}, "trace")
+
+ assert result == TraceSearchResult(
+ total=7,
+ matches=(
+ NodeMatch(node_id="trace-1", count=2),
+ NodeMatch(node_id="gen-1", count=4),
+ NodeMatch(node_id="gen-2", count=1),
+ ),
+ )
+
+
+def test_no_counts_is_an_empty_result() -> None:
+ assert TraceSearchResult.of(TRACE, {}, "trace") == TraceSearchResult(total=0, matches=())
+
+
+def test_without_a_trace_event_the_root_counts_its_title() -> None:
+ trace = Trace("trace-1", [make_row(uuid="span-1", span_name="Fetch invoice", timestamp=T0)], None)
+
+ assert TraceSearchResult.of(trace, {"span-1": 1}, "INVOICE") == TraceSearchResult(
+ total=2, matches=(NodeMatch(node_id="trace-1", count=1), NodeMatch(node_id="span-1", count=1))
+ )
diff --git a/products/ai_observability/backend/tests/test_trace_views.py b/products/ai_observability/backend/tests/test_trace_views.py
index 57fcbd2059da..a19a03b8c271 100644
--- a/products/ai_observability/backend/tests/test_trace_views.py
+++ b/products/ai_observability/backend/tests/test_trace_views.py
@@ -10,6 +10,7 @@ from parameterized import parameterized
from posthog.api.query import CONCURRENCY_LIMIT_RETRY_AFTER_SECONDS
from posthog.clickhouse.client.limit import ConcurrencyLimitExceeded, ConcurrencySlot, RateLimit
from posthog.constants import AvailableFeature
+from posthog.exceptions import ClickHouseQueryTimeOut
from posthog.models import Team
from posthog.models.ai_events.test_util import bulk_create_ai_events
from posthog.models.event.util import bulk_create_events
@@ -19,6 +20,7 @@ from posthog.models.user import User
from products.access_control.backend.facade.contracts import PropertyAccessLevel
from products.access_control.backend.models.access_control import AccessControl
from products.access_control.backend.models.property_access_control import PropertyAccessControl
+from products.ai_observability.backend.logic.traces import trace_queries
from products.ai_observability.backend.logic.traces.summary import ReadySummary, SummaryItem
from products.ai_observability.backend.logic.traces.summary_store import summary_key, write_summary
from products.ai_observability.backend.models.review_queues import ReviewQueue
@@ -197,6 +199,7 @@ class TestTraceViewSet(ClickhouseTestMixin, APIBaseTest):
("trace", ""),
("node", f"nodes/{_encode('trace-1')}/"),
("evaluations", "evaluations/"),
+ ("search", "search/?q=invoice"),
]
]
)
@@ -418,6 +421,52 @@ class TestTraceFeedbackView(ClickhouseTestMixin, APIBaseTest):
assert (len(body["scores"]), body["responses"]) == (1, [])
+class TestTraceSearchView(ClickhouseTestMixin, APIBaseTest):
+ def _url(self, trace_id: str) -> str:
+ return f"/api/projects/{self.team.id}/ai_observability/traces/{_encode(trace_id)}/search/"
+
+ def test_returns_matches_in_camel_case(self) -> None:
+ _create_node_trace(self.team)
+
+ response = self.client.get(self._url("trace-1"), {"q": "Invoice"})
+
+ assert response.status_code == 200, response.content
+ assert response.json() == {"total": 2, "matches": [{"nodeId": GENERATION, "count": 2}]}
+
+ @parameterized.expand([("no query", {}), ("a blank query", {"q": " "}), ("a long query", {"q": "a" * 201})])
+ def test_rejects_a_bad_query(self, _name: str, query: dict[str, str]) -> None:
+ _create_node_trace(self.team)
+
+ assert self.client.get(self._url("trace-1"), query).status_code == 400
+
+ def test_unknown_trace_is_not_found(self) -> None:
+ assert self.client.get(self._url("trace-2"), {"q": "invoice"}).status_code == 404
+
+ def test_restricted_content_never_matches(self) -> None:
+ self.organization.available_product_features = [
+ {"key": AvailableFeature.PROPERTY_ACCESS_CONTROL, "name": AvailableFeature.PROPERTY_ACCESS_CONTROL}
+ ]
+ self.organization.save()
+ definition = PropertyDefinition.objects.create(
+ team=self.team, name="$ai_input", property_type="String", type=PropertyDefinition.Type.EVENT
+ )
+ PropertyAccessControl.objects.create(
+ team=self.team,
+ property_definition=definition,
+ organization_member=self.organization_membership,
+ access_level=PropertyAccessLevel.NONE.value,
+ )
+ _create_node_trace(self.team)
+
+ assert self.client.get(self._url("trace-1"), {"q": "go up"}).json() == {"total": 0, "matches": []}
+
+ def test_the_search_time_limit_reaches_clickhouse_and_a_timeout_is_a_504(self) -> None:
+ with patch.object(trace_queries, "query_ai_events", side_effect=ClickHouseQueryTimeOut()) as query:
+ assert self.client.get(self._url("trace-1"), {"q": "invoice"}).status_code == 504
+
+ assert query.call_args.kwargs["settings"].max_execution_time == 10
+
+
class TestTraceViewSetResourceLevelAccess(ClickhouseTestMixin, APIBaseTest):
def test_a_grant_on_one_object_does_not_open_traces(self) -> None:
self.organization.available_product_features = [
diff --git a/products/ai_observability/frontend/aiObservabilityTraceLogic.test.ts b/products/ai_observability/frontend/aiObservabilityTraceLogic.test.ts
index 6f069637692b..b85da87ade92 100644
--- a/products/ai_observability/frontend/aiObservabilityTraceLogic.test.ts
+++ b/products/ai_observability/frontend/aiObservabilityTraceLogic.test.ts
@@ -3,6 +3,7 @@ import { MOCK_TEAM_ID } from 'lib/api.mock'
import { combineUrl, router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
+import api from 'lib/api'
import { addProjectIdIfMissing } from 'lib/utils/kea-router'
import { sceneLogic } from 'scenes/sceneLogic'
import { urls } from 'scenes/urls'
@@ -170,6 +171,22 @@ describe('aiObservabilityTraceLogic', () => {
})
})
+ it('records one trace view intent per trace, not per URL change within it', async () => {
+ const intents = jest.spyOn(api.productIntents, 'update').mockReturnValue(new Promise(() => {}))
+ const open = (traceId: string, params: Record<string, string> = {}): void => {
+ router.actions.push(addProjectIdIfMissing(urls.aiObservabilityTrace(traceId, params), MOCK_TEAM_ID))
+ }
+
+ open('trace-1')
+ open('trace-1', { event: 'step-1', search: 'invoice' })
+ open('trace-1', { event: 'step-2', search: 'invoice' })
+ open('trace-2')
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(intents).toHaveBeenCalledTimes(2)
+ intents.mockRestore()
+ })
+
it('does not write the search query back to the URL when it originates from the URL', async () => {
// Regression: setSearchQuery's listener wrote the query back to the URL, which
// re-fired urlToAction, which called setSearchQuery again. For a query that the URL
diff --git a/products/ai_observability/frontend/aiObservabilityTraceLogic.ts b/products/ai_observability/frontend/aiObservabilityTraceLogic.ts
index 6108dd27bbec..575cec5e89ec 100644
--- a/products/ai_observability/frontend/aiObservabilityTraceLogic.ts
+++ b/products/ai_observability/frontend/aiObservabilityTraceLogic.ts
@@ -868,10 +868,13 @@ export const aiObservabilityTraceLogic = kea<aiObservabilityTraceLogicType>([
const traceId = tryDecodeURIComponent(id ?? '')
const isNewTrace = traceId !== values.traceId
actions.setTraceId(traceId)
- void addProductIntent({
- product_type: ProductKey.AI_OBSERVABILITY,
- intent_context: ProductIntentContext.LLM_ANALYTICS_TRACE_VIEWED,
- })
+ // The trace views rewrite the URL as people select steps and search, and each intent is a team PATCH.
+ if (isNewTrace) {
+ void addProductIntent({
+ product_type: ProductKey.AI_OBSERVABILITY,
+ intent_context: ProductIntentContext.LLM_ANALYTICS_TRACE_VIEWED,
+ })
+ }
actions.setEventId(event || null)
const parsedMsg = msg ? parseInt(msg, 10) : NaN
actions.setHighlightMessageIndex(!isNaN(parsedMsg) ? parsedMsg : null)
diff --git a/products/ai_observability/frontend/generated/api.schemas.ts b/products/ai_observability/frontend/generated/api.schemas.ts
index 3d3ad96b2d8c..b37e5cec74b2 100644
--- a/products/ai_observability/frontend/generated/api.schemas.ts
+++ b/products/ai_observability/frontend/generated/api.schemas.ts
@@ -1559,6 +1559,20 @@ export interface TraceNodeDetailApi {
event: TraceNodeEventApi | null
}
+export interface TraceSearchMatchApi {
+ /** A step whose stored data contains the query. The root's id is the trace id. */
+ nodeId: string
+ /** How many times the query occurs in the step's stored inputs, outputs, state, tools, name, model, provider and error, ignoring case. Keys of the stored JSON count too. */
+ count: number
+}
+
+export interface TraceSearchResultsApi {
+ /** Occurrences of the query across every matching step. */
+ total: number
+ /** Matching steps in tree order, the root first. Steps without a match are left out. */
+ matches: TraceSearchMatchApi[]
+}
+
export type TraceSummaryStatusEnumApi = (typeof TraceSummaryStatusEnumApi)[keyof typeof TraceSummaryStatusEnumApi]
export const TraceSummaryStatusEnumApi = {
@@ -5617,6 +5631,19 @@ export type AiObservabilityTracesNodesRetrieveParams = {
timestamp_hint?: string
}
+export type AiObservabilityTracesSearchRetrieveParams = {
+ /**
+ * The text to find in the trace's steps, ignoring case.
+ * @minLength 1
+ * @maxLength 200
+ */
+ q: string
+ /**
+ * When the trace happened, as carried by links into it. Lets a trace older than the AI events retention load from the shared events table.
+ */
+ timestamp_hint?: string
+}
+
export type AiObservabilityTracesSummaryCreateParams = {
/**
* When the trace happened, as carried by links into it. Lets a trace older than the AI events retention load from the shared events table.
diff --git a/products/ai_observability/frontend/generated/api.ts b/products/ai_observability/frontend/generated/api.ts
index 7d350ef52549..88c22d4a6f5b 100644
--- a/products/ai_observability/frontend/generated/api.ts
+++ b/products/ai_observability/frontend/generated/api.ts
@@ -19,6 +19,7 @@ import type {
AiObservabilityTracesFeedbackRetrieveParams,
AiObservabilityTracesNodesRetrieveParams,
AiObservabilityTracesRetrieveParams,
+ AiObservabilityTracesSearchRetrieveParams,
AiObservabilityTracesSummaryCreateParams,
BatchCheckRequestApi,
BatchCheckResponseApi,
@@ -156,6 +157,7 @@ import type {
TraceNodeDetailApi,
TraceReviewApi,
TraceReviewCreateApi,
+ TraceSearchResultsApi,
TraceSummaryApi,
TraceSummaryRequestApi,
TranslateRequestApi,
@@ -778,6 +780,41 @@ export const aiObservabilityTracesNodesRetrieve = async (
})
}
+export const getAiObservabilityTracesSearchRetrieveUrl = (
+ projectId: string,
+ id: string,
+ params: AiObservabilityTracesSearchRetrieveParams
+) => {
+ const normalizedParams = new URLSearchParams()
+
+ Object.entries(params || {}).forEach(([key, value]) => {
+ if (value !== undefined) {
+ normalizedParams.append(key, value === null ? 'null' : String(value))
+ }
+ })
+
+ const stringifiedParams = normalizedParams.toString()
+
+ return stringifiedParams.length > 0
+ ? `/api/projects/${projectId}/ai_observability/traces/${id}/search/?${stringifiedParams}`
+ : `/api/projects/${projectId}/ai_observability/traces/${id}/search/`
+}
+
+/**
+ * Find text in a trace's steps: each matching step in tree order with its number of occurrences.
+ */
+export const aiObservabilityTracesSearchRetrieve = async (
+ projectId: string,
+ id: string,
+ params: AiObservabilityTracesSearchRetrieveParams,
+ options?: RequestInit
+): Promise<TraceSearchResultsApi> => {
+ return apiMutator<TraceSearchResultsApi>(getAiObservabilityTracesSearchRetrieveUrl(projectId, id, params), {
+ ...options,
+ method: 'GET',
+ })
+}
+
export const getAiObservabilityTracesSummaryRetrieveUrl = (projectId: string, id: string) => {
return `/api/projects/${projectId}/ai_observability/traces/${id}/summary/`
}
diff --git a/products/ai_observability/frontend/redesign/trace/TraceScene.tsx b/products/ai_observability/frontend/redesign/trace/TraceScene.tsx
index bb68da99c55b..8b3d04a75453 100644
--- a/products/ai_observability/frontend/redesign/trace/TraceScene.tsx
+++ b/products/ai_observability/frontend/redesign/trace/TraceScene.tsx
@@ -4,7 +4,7 @@ import { aiObservabilityTraceLogic } from '../../aiObservabilityTraceLogic'
import { SaveToDatasetButton } from '../../datasets/SaveToDatasetButton'
import { traceViewAdapterLogic, traceViewAdapterLogicValues } from './adapter/traceViewAdapterLogic'
import { TraceView, TraceViewProps, TraceViewReadyProps } from './components/TraceView'
-import { FeedbackEntry, NodeDetailTab, SummaryItemKind, TraceMode } from './types'
+import { FeedbackEntry, MatchDirection, NodeDetailTab, SummaryItemKind, TraceMode } from './types'
type ReadyValues = Pick<
traceViewAdapterLogicValues,
@@ -22,6 +22,10 @@ type ReadyValues = Pick<
| 'evalsChip'
| 'evalOutcomesByNode'
| 'gitBranch'
+ | 'searchBox'
+ | 'matchingNodeIds'
+ | 'highlightQuery'
+ | 'matchStepCount'
| 'summaryState'
| 'summaryRequesting'
| 'summaryRequestError'
@@ -41,6 +45,8 @@ interface ReadyActions {
viewInThread: () => void
prefetchNode: (id: string) => void
showFailingEvals: () => void
+ setQuery: (query: string) => void
+ stepMatch: (direction: MatchDirection) => void
summarize: () => void
regenerateSummary: () => void
loadSummary: () => void
@@ -88,6 +94,10 @@ function toReadyProps(values: ReadyValues, actions: ReadyActions): TraceViewRead
thread: values.thread,
timeline: values.timeline,
evalOutcomes: values.evalOutcomesByNode,
+ search: { ...values.searchBox, onQueryChange: actions.setQuery, onStep: actions.stepMatch },
+ matchingNodeIds: values.matchingNodeIds,
+ highlightQuery: values.highlightQuery,
+ matchStepCount: values.matchStepCount,
summaryMode: {
state: values.summaryState,
requesting: values.summaryRequesting,
@@ -127,6 +137,10 @@ export function TraceScene(): JSX.Element {
evalsChip,
evalOutcomesByNode,
gitBranch,
+ searchBox,
+ matchingNodeIds,
+ highlightQuery,
+ matchStepCount,
summaryState,
summaryRequesting,
summaryRequestError,
@@ -157,6 +171,10 @@ export function TraceScene(): JSX.Element {
evalsChip,
evalOutcomesByNode,
gitBranch,
+ searchBox,
+ matchingNodeIds,
+ highlightQuery,
+ matchStepCount,
summaryState,
summaryRequesting,
summaryRequestError,
diff --git a/products/ai_observability/frontend/redesign/trace/adapter/toHeader.test.ts b/products/ai_observability/frontend/redesign/trace/adapter/toHeader.test.ts
index 685a5be32a40..82b0740bb570 100644
--- a/products/ai_observability/frontend/redesign/trace/adapter/toHeader.test.ts
+++ b/products/ai_observability/frontend/redesign/trace/adapter/toHeader.test.ts
@@ -10,11 +10,11 @@ const noNeighbours: TraceNeighbours = {
}
describe('toHeader', () => {
- it('links neighbours with their timestamps and the list context, and leaves missing neighbours null', () => {
+ it('links neighbours with their timestamps and the list context without the search, and leaves missing neighbours null', () => {
const header = toHeader(
title,
{ ...noNeighbours, olderTraceId: 'trace-0', olderTimestamp: '2026-09-01T10:00:00Z' },
- { date_from: '-7d', event: 'gen-1', line: '4', back_to: 'generations' }
+ { date_from: '-7d', event: 'gen-1', line: '4', back_to: 'generations', search: 'invoice' }
)
expect(header.olderHref).toBe(
'/ai-observability/traces/trace-0?date_from=-7d&timestamp=2026-09-01T10%3A00%3A00Z'
@@ -24,13 +24,13 @@ describe('toHeader', () => {
it.each([
[
- 'the traces list by default, keeping its filters',
- { date_from: '-7d', event: 'gen-1' },
+ 'the traces list by default, keeping its filters but not the search',
+ { date_from: '-7d', event: 'gen-1', search: 'invoice' },
'/ai-observability/traces?date_from=-7d',
],
[
'the generations list when opened from it',
- { back_to: 'generations', date_from: '-7d', tab: 'evals' },
+ { back_to: 'generations', date_from: '-7d', tab: 'evals', search: 'invoice' },
'/ai-observability/generations?date_from=-7d',
],
[
@@ -40,7 +40,7 @@ describe('toHeader', () => {
],
[
'the review queue when opened from one',
- { back_to: 'reviews', queue_id: 'queue-1' },
+ { back_to: 'reviews', queue_id: 'queue-1', search: 'invoice' },
'/ai-observability/reviews?queue_id=queue-1',
],
])('goes back to %s', (_name, searchParams, expected) => {
diff --git a/products/ai_observability/frontend/redesign/trace/adapter/toHeader.ts b/products/ai_observability/frontend/redesign/trace/adapter/toHeader.ts
index 188de613e398..30b8c4d40147 100644
--- a/products/ai_observability/frontend/redesign/trace/adapter/toHeader.ts
+++ b/products/ai_observability/frontend/redesign/trace/adapter/toHeader.ts
@@ -15,10 +15,15 @@ export interface TraceNeighbours {
type SearchParams = Record<string, unknown>
+// The lists read `search` as their own search term, and another trace starts with an empty search.
+function listParams(searchParams: SearchParams): SearchParams {
+ return sanitizeTraceUrlSearchParams(searchParams, { removeSearch: true })
+}
+
function neighbourHref(traceId: string | null, timestamp: string | null, searchParams: SearchParams): string | null {
return traceId
? combineUrl(urls.aiObservabilityTrace(traceId), {
- ...sanitizeTraceUrlSearchParams(searchParams),
+ ...listParams(searchParams),
timestamp: timestamp ?? undefined,
}).url
: null
@@ -28,7 +33,7 @@ function neighbourHref(traceId: string | null, timestamp: string | null, searchP
function reviewsBackHref(searchParams: SearchParams): string {
const inQueue = typeof searchParams.queue_id === 'string' && searchParams.queue_id !== ''
return combineUrl(urls.aiObservabilityReviews(), {
- ...sanitizeTraceUrlSearchParams(searchParams),
+ ...listParams(searchParams),
human_reviews_tab: inQueue ? undefined : 'reviews',
}).url
}
@@ -37,14 +42,13 @@ export function toBackLink(searchParams: SearchParams): LabeledLink {
if (searchParams.back_to === 'reviews') {
return { label: 'Back to reviews', href: reviewsBackHref(searchParams) }
}
- const listParams = sanitizeTraceUrlSearchParams(searchParams)
if (searchParams.back_to === 'generations') {
return {
label: 'Back to generations',
- href: combineUrl(urls.aiObservabilityGenerations(), listParams).url,
+ href: combineUrl(urls.aiObservabilityGenerations(), listParams(searchParams)).url,
}
}
- return { label: 'Back to traces', href: combineUrl(urls.aiObservabilityTraces(), listParams).url }
+ return { label: 'Back to traces', href: combineUrl(urls.aiObservabilityTraces(), listParams(searchParams)).url }
}
export function toHeader(
diff --git a/products/ai_observability/frontend/redesign/trace/adapter/toSearch.test.ts b/products/ai_observability/frontend/redesign/trace/adapter/toSearch.test.ts
new file mode 100644
index 000000000000..1a4057f8e540
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/adapter/toSearch.test.ts
@@ -0,0 +1,44 @@
+import type { TraceSearchMatchApi, TraceSearchResultsApi } from '../../../generated/api.schemas'
+import { steppedMatchId, toMatchingNodeIds, toSearchBox } from './toSearch'
+
+const MATCHES: TraceSearchMatchApi[] = [
+ { nodeId: 'trace-1', count: 2 },
+ { nodeId: 'gen-1', count: 4 },
+ { nodeId: 'gen-2', count: 1 },
+]
+const RESULTS: TraceSearchResultsApi = { total: 7, matches: MATCHES }
+
+describe('toSearch', () => {
+ test.each([
+ ['next from a match', 'gen-1', 'next', 'gen-2'],
+ ['next wraps', 'gen-2', 'next', 'trace-1'],
+ ['previous wraps', 'trace-1', 'previous', 'gen-2'],
+ ['next from a non-match', 'span-1', 'next', 'trace-1'],
+ ['previous from a non-match', 'span-1', 'previous', 'gen-2'],
+ ] as const)('steppedMatchId %s', (_, selected, direction, expected) => {
+ expect(steppedMatchId(MATCHES, selected, direction)).toBe(expected)
+ })
+
+ test('steppedMatchId with no matches is null', () => {
+ expect(steppedMatchId([], 'gen-1', 'next')).toBeNull()
+ })
+
+ test.each([
+ ['the selected match', 'gen-1', 2],
+ ['a step without a match', 'span-1', null],
+ ])('toSearchBox places %s', (_, selected, position) => {
+ expect(toSearchBox('invoice', 'ready', RESULTS, null, selected)).toEqual({
+ query: 'invoice',
+ status: 'ready',
+ total: 7,
+ matchingSteps: 3,
+ position,
+ failure: null,
+ })
+ })
+
+ test('toMatchingNodeIds fades nothing without results', () => {
+ expect(toMatchingNodeIds(null)).toBeNull()
+ expect(toMatchingNodeIds(RESULTS)).toEqual(new Set(['trace-1', 'gen-1', 'gen-2']))
+ })
+})
diff --git a/products/ai_observability/frontend/redesign/trace/adapter/toSearch.ts b/products/ai_observability/frontend/redesign/trace/adapter/toSearch.ts
new file mode 100644
index 000000000000..9df9e119eb16
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/adapter/toSearch.ts
@@ -0,0 +1,43 @@
+import type { TraceSearchMatchApi, TraceSearchResultsApi } from '../../../generated/api.schemas'
+import type { TraceSearchBoxData } from '../components/TraceSearchBox'
+import type { MatchDirection, TraceSearchFailure, TraceSearchStatus } from '../types'
+
+export function toMatchingNodeIds(results: TraceSearchResultsApi | null): ReadonlySet<string> | null {
+ return results ? new Set(results.matches.map((match) => match.nodeId)) : null
+}
+
+/** `selectedNodeId` is null when the current mode does not show the selected step, so stepping starts at an end. */
+export function steppedMatchId(
+ matches: TraceSearchMatchApi[],
+ selectedNodeId: string | null,
+ direction: MatchDirection
+): string | null {
+ if (matches.length === 0) {
+ return null
+ }
+ const index = matches.findIndex((match) => match.nodeId === selectedNodeId)
+ if (index === -1) {
+ return direction === 'next' ? matches[0].nodeId : matches[matches.length - 1].nodeId
+ }
+ const step = direction === 'next' ? 1 : -1
+ return matches[(index + step + matches.length) % matches.length].nodeId
+}
+
+export function toSearchBox(
+ query: string,
+ status: TraceSearchStatus,
+ results: TraceSearchResultsApi | null,
+ failure: TraceSearchFailure | null,
+ selectedNodeId: string | null
+): TraceSearchBoxData {
+ const matches = results?.matches ?? []
+ const index = matches.findIndex((match) => match.nodeId === selectedNodeId)
+ return {
+ query,
+ status,
+ total: results?.total ?? 0,
+ matchingSteps: matches.length,
+ position: index === -1 ? null : index + 1,
+ failure,
+ }
+}
diff --git a/products/ai_observability/frontend/redesign/trace/adapter/traceSceneWiring.test.tsx b/products/ai_observability/frontend/redesign/trace/adapter/traceSceneWiring.test.tsx
index ea2d4e3b0dc1..0b43644980e3 100644
--- a/products/ai_observability/frontend/redesign/trace/adapter/traceSceneWiring.test.tsx
+++ b/products/ai_observability/frontend/redesign/trace/adapter/traceSceneWiring.test.tsx
@@ -38,6 +38,7 @@ const NODE_URL = '/api/projects/:team_id/ai_observability/traces/:id/nodes/:node
const EVALUATIONS_URL = '/api/projects/:team_id/ai_observability/traces/:id/evaluations/'
const FEEDBACK_URL = '/api/projects/:team_id/ai_observability/traces/:id/feedback/'
const DATASETS_URL = '/api/projects/:team_id/datasets/'
+const SEARCH_URL = '/api/projects/:team_id/ai_observability/traces/:id/search/'
const SUMMARY_URL = '/api/projects/:team_id/ai_observability/traces/:id/summary/'
const answeredGeneration = makeNodeDetail({
id: 'gen-2',
@@ -47,12 +48,18 @@ const answeredGeneration = makeNodeDetail({
},
ioKeys: { input: '$ai_input', output: '$ai_output_choices' },
})
+const askedGeneration = makeNodeDetail({
+ id: 'gen-1',
+ properties: { $ai_input: [{ role: 'user', content: 'Is my invoice right?' }] },
+ ioKeys: { input: '$ai_input' },
+})
+const nodeDetails: Record<string, typeof answeredGeneration> = { 'gen-1': askedGeneration, 'gen-2': answeredGeneration }
let nodeRequests: string[] = []
const nodeMock = {
[NODE_URL]: ({ params }: { params: Record<string, unknown> }) => {
const nodeId = decodeIdSegment(String(params.node_id))
nodeRequests.push(nodeId)
- return [200, nodeId === 'gen-2' ? answeredGeneration : makeNodeDetail({ id: nodeId })]
+ return [200, nodeDetails[nodeId] ?? makeNodeDetail({ id: nodeId })]
},
}
const evaluationsMock = { [EVALUATIONS_URL]: () => [200, makeEvaluationResults()] }
@@ -94,6 +101,7 @@ describe('TraceScene', () => {
get: {
[TRACE_URL]: () => [200, makeTraceResource({ defaultNodeId: 'gen-2' })],
[DATASETS_URL]: () => [200, { count: 0, results: [] }],
+ [SEARCH_URL]: () => [200, { total: 1, matches: [{ nodeId: 'gen-1', count: 1 }] }],
...nodeMock,
...evaluationsMock,
...feedbackMock,
@@ -122,6 +130,43 @@ describe('TraceScene', () => {
await waitFor(() => expect(nodeRequests).toContain('span-1'))
})
+ it('searches from the header, fades the steps without a match and steps to the match in view', async () => {
+ const { container, findAllByText, findByText } = renderNewView()
+ const treeRow = async (name: string): Promise<Element> => {
+ const row = (await findAllByText(name))
+ .map((element) => element.closest('[data-attr="trace-view-tree-node"]'))
+ .find((element) => element !== null)
+ if (!row) {
+ throw new Error(`expected ${name} to render as a tree row`)
+ }
+ return row
+ }
+ await treeRow('gen-1')
+ const input = container.querySelector('input[data-attr="trace-view-search-input"]')
+ if (!input) {
+ throw new Error('expected the header to render the search input')
+ }
+ expect(input.getAttribute('maxlength')).toBe('200')
+
+ fireEvent.change(input, { target: { value: 'invoice' } })
+
+ expect(await findByText('1 match')).toBeTruthy()
+ expect((await treeRow('gen-2')).className).toContain('opacity-50')
+ expect((await treeRow('gen-1')).className).not.toContain('opacity-50')
+
+ const scrollIntoView = jest.spyOn(Element.prototype, 'scrollIntoView')
+
+ fireEvent.keyDown(input, { key: 'Enter' })
+
+ const matchedRow = await treeRow('gen-1')
+ await waitFor(() => expect(matchedRow.getAttribute('aria-current')).toBe('true'))
+ const firstHighlight = await findByText('Is my invoice right?')
+ await waitFor(() =>
+ expect(scrollIntoView.mock.contexts).toEqual(expect.arrayContaining([matchedRow, firstHighlight]))
+ )
+ scrollIntoView.mockRestore()
+ })
+
it('the Summary tab shows a stored summary whose step link opens that step in spans', async () => {
useMocks({
get: {
diff --git a/products/ai_observability/frontend/redesign/trace/adapter/traceViewAdapterLogic.test.ts b/products/ai_observability/frontend/redesign/trace/adapter/traceViewAdapterLogic.test.ts
index 537681b42f61..290ba022d1bb 100644
--- a/products/ai_observability/frontend/redesign/trace/adapter/traceViewAdapterLogic.test.ts
+++ b/products/ai_observability/frontend/redesign/trace/adapter/traceViewAdapterLogic.test.ts
@@ -1,5 +1,5 @@
import { waitFor } from '@testing-library/react'
-import { router } from 'kea-router'
+import { decodeParams, router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import posthog from 'posthog-js'
@@ -22,6 +22,7 @@ const TRACE_URL = '/api/projects/:team_id/ai_observability/traces/:id/'
const NODE_URL = '/api/projects/:team_id/ai_observability/traces/:id/nodes/:node_id/'
const EVALUATIONS_URL = '/api/projects/:team_id/ai_observability/traces/:id/evaluations/'
const FEEDBACK_URL = '/api/projects/:team_id/ai_observability/traces/:id/feedback/'
+const SEARCH_URL = '/api/projects/:team_id/ai_observability/traces/:id/search/'
const SUMMARY_URL = '/api/projects/:team_id/ai_observability/traces/:id/summary/'
const answeredGeneration = makeNodeDetail({
@@ -40,6 +41,7 @@ describe('traceViewAdapterLogic', () => {
let nodeRequests: string[]
let failingNodeId: string | null
let feedbackStatus: number
+ let searchStatus: number
let summaryRequests: number
let evaluations: TraceEvaluationResultsApi
@@ -55,6 +57,7 @@ describe('traceViewAdapterLogic', () => {
nodeRequests = []
failingNodeId = null
feedbackStatus = 200
+ searchStatus = 200
summaryRequests = 0
evaluations = makeEvaluationResults()
useMocks({
@@ -73,6 +76,19 @@ describe('traceViewAdapterLogic', () => {
[EVALUATIONS_URL]: () => [200, evaluations],
[FEEDBACK_URL]: () =>
feedbackStatus === 200 ? [200, makeFeedback()] : [feedbackStatus, { detail: 'Server error.' }],
+ [SEARCH_URL]: () =>
+ searchStatus === 200
+ ? [
+ 200,
+ {
+ total: 3,
+ matches: [
+ { nodeId: 'gen-1', count: 2 },
+ { nodeId: 'gen-2', count: 1 },
+ ],
+ },
+ ]
+ : [searchStatus, { detail: 'Search timed out.' }],
[NODE_URL]: ({ params }) => {
const nodeId = decodeIdSegment(String(params.node_id))
nodeRequests.push(nodeId)
@@ -97,6 +113,7 @@ describe('traceViewAdapterLogic', () => {
afterEach(() => {
resumeKeaLoadersErrors()
logic.unmount()
+ resumeKeaLoadersErrors()
jest.restoreAllMocks()
})
@@ -310,6 +327,120 @@ describe('traceViewAdapterLogic', () => {
})
})
+ it('reads the search from the link and keeps the list it came from', async () => {
+ mountAt({ search: 'invoice', back_to: 'generations' })
+
+ await expectLogic(logic)
+ .toFinishAllListeners()
+ .toMatchValues({
+ searchBox: expect.objectContaining({ query: 'invoice', status: 'ready', total: 3 }),
+ backLink: expect.objectContaining({ label: 'Back to generations' }),
+ })
+ })
+
+ it('writes the settled search to the URL once, keeping the selection and the other params', async () => {
+ mountAt({ back_to: 'generations', timestamp: '2026-09-01T10:15:00Z' })
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.selectNode('gen-1')
+ const replace = jest.spyOn(router.actions, 'replace')
+
+ await expectLogic(logic, () => {
+ logic.actions.setQuery('two')
+ logic.actions.setQuery('two words + 50%')
+ })
+ .toDispatchActions(['setQuery', 'setQuery'])
+ .toFinishAllListeners()
+ .toNotHaveDispatchedActions(['setQuery'])
+
+ expect(replace).toHaveBeenCalledTimes(1)
+ expect(router.values.searchParams).toEqual({
+ back_to: 'generations',
+ timestamp: '2026-09-01T10:15:00Z',
+ search: 'two words + 50%',
+ event: 'gen-1',
+ })
+ expect(logic.values.selectedNodeId).toBe('gen-1')
+ })
+
+ it.each(['1.50', '[draft]'])('keeps the typed search %s when the browser restores its URL', async (query) => {
+ mountAt()
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.setQuery(query)
+ await expectLogic(logic).toFinishAllListeners()
+
+ const { pathname, search } = router.values.location
+
+ // Back and forward decode the URL again, as a page load does.
+ await expectLogic(logic, () => {
+ router.actions.locationChanged({
+ method: 'POP',
+ pathname,
+ search,
+ searchParams: decodeParams(search, '?'),
+ hash: '',
+ hashParams: {},
+ url: `${pathname}${search}`,
+ })
+ }).toFinishAllListeners()
+
+ expect(logic.values.query).toBe(query)
+ })
+
+ it('steps to the next match from another mode and shows it in spans', async () => {
+ mountAt({ search: 'invoice' })
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.setMode('thread')
+
+ await expectLogic(logic, () => logic.actions.stepMatch('next')).toMatchValues({
+ mode: 'spans',
+ selectedNodeId: 'gen-1',
+ searchBox: expect.objectContaining({ position: 1 }),
+ })
+ })
+
+ it('keeps the selection in the URL while a search is active', async () => {
+ mountAt()
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.selectNode('gen-1')
+ logic.actions.setQuery('invoice')
+ await expectLogic(logic).toFinishAllListeners()
+
+ logic.actions.stepMatch('next')
+ expect(router.values.searchParams).toMatchObject({ search: 'invoice', event: 'gen-2' })
+
+ logic.actions.selectNode('span-1')
+ expect(router.values.searchParams).toMatchObject({ search: 'invoice', event: 'span-1' })
+ })
+
+ it('steps to the first match from a mode that does not show the selected step', async () => {
+ mountAt({ search: 'invoice' })
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.selectNode('gen-1')
+ logic.actions.setMode('thread')
+
+ expect(logic.values.searchBox.position).toBeNull()
+
+ logic.actions.stepMatch('next')
+ expect(logic.values).toMatchObject({ mode: 'spans', selectedNodeId: 'gen-1' })
+ })
+
+ it('drops the previous matches when a newer search fails', async () => {
+ silenceKeaLoadersErrors()
+ mountAt({ search: 'invoice' })
+ await expectLogic(logic).toFinishAllListeners()
+ searchStatus = 504
+
+ logic.actions.setQuery('refund')
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.stepMatch('next')
+
+ expect(logic.values).toMatchObject({
+ matchingNodeIds: null,
+ selectedNodeId: 'trace-1',
+ searchBox: expect.objectContaining({ status: 'error', failure: 'too_slow', matchingSteps: 0 }),
+ })
+ })
+
it('the Summary mode reads the summary on open, and a step link leaves it for that step in spans', async () => {
const capture = jest.spyOn(posthog, 'capture')
mountAt()
diff --git a/products/ai_observability/frontend/redesign/trace/adapter/traceViewAdapterLogic.ts b/products/ai_observability/frontend/redesign/trace/adapter/traceViewAdapterLogic.ts
index 7be1a3965540..a32ec75081bd 100644
--- a/products/ai_observability/frontend/redesign/trace/adapter/traceViewAdapterLogic.ts
+++ b/products/ai_observability/frontend/redesign/trace/adapter/traceViewAdapterLogic.ts
@@ -1,12 +1,14 @@
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
-import { router } from 'kea-router'
+import { router, urlToAction } from 'kea-router'
import { subscriptions } from 'kea-subscriptions'
import posthog from 'posthog-js'
import { FEATURE_FLAGS } from 'lib/constants'
import { FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'
+import { tryDecodeURIComponent } from 'lib/utils/url'
import { aiConsentLogic } from 'scenes/settings/organization/aiConsentLogic'
import { teamLogic } from 'scenes/teamLogic'
+import { urls } from 'scenes/urls'
import { aiObservabilityTraceLogic } from '../../../aiObservabilityTraceLogic'
import type { SaveToDatasetButtonProps } from '../../../datasets/SaveToDatasetButton'
@@ -15,12 +17,15 @@ import type {
TraceEvaluationResultsApi,
TraceFeedbackApi,
TraceNodeApi,
+ TraceSearchResultsApi,
TraceTimelineRowApi,
} from '../../../generated/api.schemas'
import { GitBranchChipProps } from '../components/GitBranchChip'
import { TraceHeaderProps } from '../components/TraceHeader'
+import type { TraceSearchBoxData } from '../components/TraceSearchBox'
import { TraceSummaryBarProps } from '../components/TraceSummaryBar'
import { traceFeedbackLogic } from '../traceFeedbackLogic'
+import { SEARCH_DEBOUNCE_MS, traceSearchLogic } from '../traceSearchLogic'
import { traceSummaryLogic } from '../traceSummaryLogic'
import { TraceViewStatus, traceViewLogic } from '../traceViewLogic'
import {
@@ -31,6 +36,7 @@ import {
FeedbackEntry,
FeedbackModeState,
LabeledLink,
+ MatchDirection,
NodeDetailTab,
NodeLoad,
StepFeedback,
@@ -38,15 +44,25 @@ import {
SummaryModeState,
SummaryRequestError,
TraceMode,
+ TraceSearchFailure,
+ TraceSearchStatus,
} from '../types'
import { toDatasetItemSource } from './toDatasetItemSource'
import { firstFailingNodeId, toEvalOutcomesByNode, toEvalsChip } from './toEvalSummary'
import { stepFeedbackOf, toFeedbackChip, toFeedbackMode, toStepFeedback } from './toFeedback'
import { toBackLink, toHeader } from './toHeader'
import { NodeDetailData, toNodeDetail } from './toNodeDetail'
+import { steppedMatchId, toMatchingNodeIds, toSearchBox } from './toSearch'
import { toThread } from './toThread'
import { findTreeNode } from './toTraceTree'
+// kea-router reads a numeric param as a number, which turns a typed `1.50` into `1.5`. It also pads a value shaped like
+// a JSON array or object with one space, so it reads that value back as a string.
+function searchParamFromUrl(): string {
+ const value = new URLSearchParams(router.values.location.search).get('search') ?? ''
+ return /^(\[.*\]|\{.*\}) +$/.test(value) ? value.slice(0, -1) : value
+}
+
export interface TraceViewAdapterLogicProps {
traceId: string
timestampHint: string | null
@@ -66,6 +82,10 @@ export interface traceViewAdapterLogicValues {
currentTeamId: number | null // teamLogic
traceFeedback: TraceFeedbackApi | null // traceFeedbackLogic
traceFeedbackFailed: boolean // traceFeedbackLogic
+ query: string // traceSearchLogic
+ searchFailure: TraceSearchFailure | null // traceSearchLogic
+ searchResults: TraceSearchResultsApi | null // traceSearchLogic
+ searchStatus: TraceSearchStatus // traceSearchLogic
summaryRequestError: SummaryRequestError | null // traceSummaryLogic
summaryRequesting: boolean // traceSummaryLogic
summaryState: SummaryModeState // traceSummaryLogic
@@ -103,11 +123,17 @@ export interface traceViewAdapterLogicValues {
feedbackMode: FeedbackModeState
gitBranch: GitBranchChipProps | null
header: TraceHeaderProps
+ highlightQuery: string | null
+ matchStepCount: number
+ matchingNodeIds: ReadonlySet<string> | null
mode: TraceMode
+ readySearchResults: TraceSearchResultsApi | null
+ searchBox: TraceSearchBoxData
selectedNodeId: string
selectedNodeLoad: NodeLoad
selectedStepFeedback: StepFeedback | null
selectedTreeNode: TraceNodeApi | null
+ shownSelectedNodeId: string | null
stepFeedback: Record<string, StepFeedback>
thread: ConversationState
threadNodeLoad: NodeLoad | null
@@ -128,6 +154,9 @@ export interface traceViewAdapterLogicActions {
retryTraceFeedback: () => {
value: true
} // traceFeedbackLogic
+ setQuery: (query: string) => {
+ query: string
+ } // traceSearchLogic
closeSummary: () => {
value: true
} // traceSummaryLogic
@@ -183,6 +212,9 @@ export interface traceViewAdapterLogicActions {
entry: FeedbackEntry
nodeId: string | null
}
+ stepMatch: (direction: MatchDirection) => {
+ direction: MatchDirection
+ }
summarize: () => {
value: true
}
@@ -267,6 +299,20 @@ export interface traceViewAdapterLogicMeta {
feedbackFocusNodeId: string | null,
traceId: string
) => FeedbackModeState
+ readySearchResults: (
+ searchStatus: TraceSearchStatus,
+ searchResults: TraceSearchResultsApi | null
+ ) => TraceSearchResultsApi | null
+ shownSelectedNodeId: (mode: TraceMode, selectedNodeId: string) => string | null
+ searchBox: (
+ query: string,
+ searchStatus: TraceSearchStatus,
+ readySearchResults: TraceSearchResultsApi | null,
+ searchFailure: TraceSearchFailure | null,
+ shownSelectedNodeId: string | null
+ ) => TraceSearchBoxData
+ matchingNodeIds: (readySearchResults: TraceSearchResultsApi | null) => ReadonlySet<string> | null
+ highlightQuery: (query: string, searchStatus: TraceSearchStatus) => string | null
}
}
@@ -317,6 +363,8 @@ export const traceViewAdapterLogic = kea<traceViewAdapterLogicType>([
['featureFlags'],
traceFeedbackLogic({ traceId: props.traceId, timestampHint: props.timestampHint }),
['traceFeedback', 'traceFeedbackFailed'],
+ traceSearchLogic({ traceId: props.traceId, timestampHint: props.timestampHint }),
+ ['query', 'searchResults', 'searchStatus', 'searchFailure'],
traceSummaryLogic({ traceId: props.traceId }),
['state as summaryState', 'requesting as summaryRequesting', 'requestError as summaryRequestError'],
aiConsentLogic,
@@ -329,6 +377,8 @@ export const traceViewAdapterLogic = kea<traceViewAdapterLogicType>([
['loadNode', 'loadTraceSuccess'],
traceFeedbackLogic({ traceId: props.traceId, timestampHint: props.timestampHint }),
['retryTraceFeedback'],
+ traceSearchLogic({ traceId: props.traceId, timestampHint: props.timestampHint }),
+ ['setQuery'],
traceSummaryLogic({ traceId: props.traceId }),
['openSummary', 'closeSummary', 'requestSummary', 'loadSummary'],
],
@@ -342,6 +392,7 @@ export const traceViewAdapterLogic = kea<traceViewAdapterLogicType>([
prefetchNode: (id: string) => ({ id }),
showFailingEvals: true,
showFeedback: (nodeId: string | null, entry: FeedbackEntry) => ({ nodeId, entry }),
+ stepMatch: (direction: MatchDirection) => ({ direction }),
openSummaryFromLink: true,
summarize: true,
regenerateSummary: true,
@@ -368,6 +419,7 @@ export const traceViewAdapterLogic = kea<traceViewAdapterLogicType>([
viewInThread: () => null,
},
],
+ matchStepCount: [0, { stepMatch: (count) => count + 1 }],
}),
selectors({
selectedNodeId: [
@@ -557,14 +609,55 @@ export const traceViewAdapterLogic = kea<traceViewAdapterLogicType>([
return feedback ? toFeedbackMode({ feedback, tree, traceId, focusNodeId }) : { status: 'loading' }
},
],
+ // Results of an older query stay in the loader while a newer one loads or after it fails.
+ readySearchResults: [
+ (s) => [s.searchStatus, s.searchResults],
+ (status: TraceSearchStatus, results: TraceSearchResultsApi | null): TraceSearchResultsApi | null =>
+ status === 'ready' ? results : null,
+ ],
+ // Only Spans shows the selected step, so in other modes it is no place to count or step from.
+ shownSelectedNodeId: [
+ (s) => [s.mode, s.selectedNodeId],
+ (mode: TraceMode, selectedNodeId: string): string | null => (mode === 'spans' ? selectedNodeId : null),
+ ],
+ searchBox: [
+ (s) => [s.query, s.searchStatus, s.readySearchResults, s.searchFailure, s.shownSelectedNodeId],
+ (
+ query: string,
+ status: TraceSearchStatus,
+ results: TraceSearchResultsApi | null,
+ failure: TraceSearchFailure | null,
+ shownSelectedNodeId: string | null
+ ): TraceSearchBoxData => toSearchBox(query, status, results, failure, shownSelectedNodeId),
+ ],
+ matchingNodeIds: [
+ (s) => [s.readySearchResults],
+ (results: TraceSearchResultsApi | null): ReadonlySet<string> | null => toMatchingNodeIds(results),
+ ],
+ highlightQuery: [
+ (s) => [s.query, s.searchStatus],
+ (query: string, status: TraceSearchStatus): string | null => (status === 'ready' ? query.trim() : null),
+ ],
}),
listeners(({ actions, values, selectors, props }) => {
// The click's read starts before the selection moves, so the selectedNodeId subscription's navigation read
// for the same id is deduped and the load event reports a click. Selecting the node that is already
// selected changes no value and the subscription does not fire, so this read also retries a failed node.
+ // The legacy logic resets its selection from the URL on every location change, so a URL that carries the
+ // search carries the selection too. Each change also records a product intent there.
+ const writeUrl = (query: string): void => {
+ router.actions.replace(
+ urls.aiObservabilityTrace(props.traceId),
+ { ...values.searchParams, search: query || undefined, event: values.eventId ?? undefined },
+ router.values.hashParams
+ )
+ }
const select = ({ id }: { id: string }): void => {
actions.loadNode(id, 'selection')
actions.setEventId(id)
+ if (values.query.trim() && values.searchParams.event !== id) {
+ writeUrl(values.query)
+ }
}
const captureFeedbackViewed = (entry: FeedbackEntry | 'tab'): void => {
posthog.capture('llma trace view feedback viewed', {
@@ -610,6 +703,20 @@ export const traceViewAdapterLogic = kea<traceViewAdapterLogicType>([
posthog.capture('llma trace view summary step clicked', { kind })
actions.selectAndShowSpans(nodeId)
},
+ stepMatch: ({ direction }) => {
+ const matches = values.readySearchResults?.matches ?? []
+ const id = steppedMatchId(matches, values.shownSelectedNodeId, direction)
+ if (id) {
+ actions.selectAndShowSpans(id)
+ }
+ },
+ // The URL follows the debounced query rather than each keystroke.
+ setQuery: async ({ query }, breakpoint) => {
+ await breakpoint(SEARCH_DEBOUNCE_MS)
+ if (searchParamFromUrl() !== query) {
+ writeUrl(query)
+ }
+ },
showFailingEvals: () => {
actions.selectAndShowSpans(firstFailingNodeId(values.tree, values.evalOutcomesByNode) ?? props.traceId)
actions.setDetailTab('evals')
@@ -627,6 +734,14 @@ export const traceViewAdapterLogic = kea<traceViewAdapterLogicType>([
},
}
}),
+ urlToAction(({ actions, values, props }) => ({
+ [`${urls.aiObservabilityTraces()}/:id`]: ({ id }) => {
+ const query = searchParamFromUrl()
+ if (tryDecodeURIComponent(id ?? '') === props.traceId && query !== values.query) {
+ actions.setQuery(query)
+ }
+ },
+ })),
subscriptions(({ actions }) => ({
selectedNodeId: (selectedNodeId: string) => actions.loadNode(selectedNodeId, 'navigation'),
})),
diff --git a/products/ai_observability/frontend/redesign/trace/components/MessageBlock.tsx b/products/ai_observability/frontend/redesign/trace/components/MessageBlock.tsx
index 0f71cf21f133..db0c0e339b0a 100644
--- a/products/ai_observability/frontend/redesign/trace/components/MessageBlock.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/MessageBlock.tsx
@@ -1,4 +1,4 @@
-import { useState } from 'react'
+import { useEffect, useState } from 'react'
import { IconChevronRight } from '@posthog/icons'
@@ -30,6 +30,12 @@ export interface MessageBlockProps {
export function MessageBlock({ message, defaultOpen }: MessageBlockProps): JSX.Element {
const [isOpen, setIsOpen] = useState(defaultOpen)
+ // Search results can arrive after the message mounts, and a message that matches them opens.
+ useEffect(() => {
+ if (defaultOpen) {
+ setIsOpen(true)
+ }
+ }, [defaultOpen])
const preview = isOpen ? null : previewText(message)
return (
diff --git a/products/ai_observability/frontend/redesign/trace/components/MessageList.test.tsx b/products/ai_observability/frontend/redesign/trace/components/MessageList.test.tsx
new file mode 100644
index 000000000000..83f4e752be27
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/MessageList.test.tsx
@@ -0,0 +1,33 @@
+import '@testing-library/jest-dom'
+
+import { render } from '@testing-library/react'
+
+import { ThreadMessage } from '../types'
+import { MessageList } from './MessageList'
+
+function textMessage(id: string, text: string): ThreadMessage {
+ return { id, role: 'user', parts: [{ kind: 'text', text }], isInternal: false, sourceNodeId: 'gen-1' }
+}
+
+const messages = [
+ textMessage('m-1', 'Where is my invoice?'),
+ textMessage('m-2', 'Let me check.'),
+ textMessage('m-3', 'Any news?'),
+]
+
+function expandedStates(container: HTMLElement): (string | null)[] {
+ return Array.from(container.querySelectorAll('button[aria-expanded]')).map((button) =>
+ button.getAttribute('aria-expanded')
+ )
+}
+
+describe('MessageList', () => {
+ it('opens the earlier messages that contain the search, including after the results arrive', () => {
+ const { container, rerender } = render(<MessageList messages={messages} emptyText="" defaultOpen="last" />)
+ expect(expandedStates(container)).toEqual(['false', 'false', 'true'])
+
+ rerender(<MessageList messages={messages} emptyText="" defaultOpen="last" highlightQuery="INVOICE" />)
+
+ expect(expandedStates(container)).toEqual(['true', 'false', 'true'])
+ })
+})
diff --git a/products/ai_observability/frontend/redesign/trace/components/MessageList.tsx b/products/ai_observability/frontend/redesign/trace/components/MessageList.tsx
index 673376f1bb69..944631167ff5 100644
--- a/products/ai_observability/frontend/redesign/trace/components/MessageList.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/MessageList.tsx
@@ -1,5 +1,6 @@
import { ThreadMessage } from '../types'
import { MessageBlock } from './MessageBlock'
+import { messageContainsText } from './messageContainsText'
export type MessageListDefaultOpen = 'all' | 'last'
@@ -7,9 +8,16 @@ export interface MessageListProps {
messages: ThreadMessage[]
emptyText: string
defaultOpen: MessageListDefaultOpen
+ /** Messages that contain this text open too, so the search highlights in them show. */
+ highlightQuery?: string | null
}
-export function MessageList({ messages, emptyText, defaultOpen }: MessageListProps): JSX.Element {
+export function MessageList({
+ messages,
+ emptyText,
+ defaultOpen,
+ highlightQuery = null,
+}: MessageListProps): JSX.Element {
if (messages.length === 0) {
return <p className="m-0 text-sm text-secondary">{emptyText}</p>
}
@@ -19,7 +27,11 @@ export function MessageList({ messages, emptyText, defaultOpen }: MessageListPro
<MessageBlock
key={message.id}
message={message}
- defaultOpen={defaultOpen === 'all' || index === messages.length - 1}
+ defaultOpen={
+ defaultOpen === 'all' ||
+ index === messages.length - 1 ||
+ (!!highlightQuery && messageContainsText(message, highlightQuery))
+ }
/>
))}
</div>
diff --git a/products/ai_observability/frontend/redesign/trace/components/MessageSection.tsx b/products/ai_observability/frontend/redesign/trace/components/MessageSection.tsx
index ec3f93610589..9bccf6eba1d3 100644
--- a/products/ai_observability/frontend/redesign/trace/components/MessageSection.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/MessageSection.tsx
@@ -6,13 +6,25 @@ export interface MessageSectionProps {
messages: ThreadMessage[]
emptyText: string
defaultOpen: MessageListDefaultOpen
+ highlightQuery?: string | null
}
-export function MessageSection({ title, messages, emptyText, defaultOpen }: MessageSectionProps): JSX.Element {
+export function MessageSection({
+ title,
+ messages,
+ emptyText,
+ defaultOpen,
+ highlightQuery = null,
+}: MessageSectionProps): JSX.Element {
return (
<section className="flex flex-col gap-1.5">
<h4 className="m-0 text-xs font-semibold text-secondary">{title}</h4>
- <MessageList messages={messages} emptyText={emptyText} defaultOpen={defaultOpen} />
+ <MessageList
+ messages={messages}
+ emptyText={emptyText}
+ defaultOpen={defaultOpen}
+ highlightQuery={highlightQuery}
+ />
</section>
)
}
diff --git a/products/ai_observability/frontend/redesign/trace/components/NodeDetail.tsx b/products/ai_observability/frontend/redesign/trace/components/NodeDetail.tsx
index b199d439c429..dea935b1118b 100644
--- a/products/ai_observability/frontend/redesign/trace/components/NodeDetail.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/NodeDetail.tsx
@@ -11,6 +11,7 @@ import { NodeMessagesTab } from './NodeMessagesTab'
import { NodePropertyList } from './NodePropertyList'
import { NodeRawTab } from './NodeRawTab'
import { StepFeedbackChipsProps } from './StepFeedbackChips'
+import { useSearchHighlight } from './useSearchHighlight'
export interface NodeDetailProps {
node: TraceNodeApi
@@ -25,11 +26,14 @@ export interface NodeDetailProps {
onSelectNode: (nodeId: string) => void
actions?: ReactNode
stepFeedback?: StepFeedbackChipsProps | null
+ highlightQuery?: string | null
+ matchStepCount?: number
}
export function NodeDetail(props: NodeDetailProps): JSX.Element {
+ const highlightRef = useSearchHighlight(props.highlightQuery ?? null, props.matchStepCount)
return (
- <div className="flex min-w-0 flex-col gap-2">
+ <div className="flex min-w-0 flex-col gap-2" ref={highlightRef}>
<NodeDetailHeader node={props.node} actions={props.actions} stepFeedback={props.stepFeedback} />
<LemonTabs<NodeDetailTab>
size="small"
@@ -46,6 +50,7 @@ export function NodeDetail(props: NodeDetailProps): JSX.Element {
content={props.content}
error={props.error}
onViewInThread={props.onViewInThread}
+ highlightQuery={props.highlightQuery}
/>
),
},
diff --git a/products/ai_observability/frontend/redesign/trace/components/NodeMessagesTab.tsx b/products/ai_observability/frontend/redesign/trace/components/NodeMessagesTab.tsx
index 7e8e13a930bd..3d40cf49ac09 100644
--- a/products/ai_observability/frontend/redesign/trace/components/NodeMessagesTab.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/NodeMessagesTab.tsx
@@ -10,9 +10,15 @@ export interface NodeMessagesTabProps {
content: NodeContent
error: string | null
onViewInThread: (() => void) | null
+ highlightQuery?: string | null
}
-export function NodeMessagesTab({ content, error, onViewInThread }: NodeMessagesTabProps): JSX.Element {
+export function NodeMessagesTab({
+ content,
+ error,
+ onViewInThread,
+ highlightQuery = null,
+}: NodeMessagesTabProps): JSX.Element {
return (
<div className="flex flex-col gap-3">
{error ? <ErrorCallout message={error} /> : null}
@@ -27,12 +33,14 @@ export function NodeMessagesTab({ content, error, onViewInThread }: NodeMessages
messages={content.input}
emptyText="No input captured."
defaultOpen="last"
+ highlightQuery={highlightQuery}
/>
<MessageSection
title="Output"
messages={content.output}
emptyText="No output captured."
defaultOpen="all"
+ highlightQuery={highlightQuery}
/>
</>
) : (
diff --git a/products/ai_observability/frontend/redesign/trace/components/Thread.stories.tsx b/products/ai_observability/frontend/redesign/trace/components/Thread.stories.tsx
index e367382fd49c..b8ca3f9ffebe 100644
--- a/products/ai_observability/frontend/redesign/trace/components/Thread.stories.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/Thread.stories.tsx
@@ -20,6 +20,7 @@ function ready(turns: ConversationTurn[]): Story['args'] {
}
export const SingleTurn: Story = { args: ready([FIXTURE_TURN]) }
+export const WithSearchHighlight: Story = { args: { ...ready([FIXTURE_TURN]), highlightQuery: 'seats' } }
export const Loading: Story = {
args: { conversation: { status: 'loading' } },
decorators: [withWidth(720)],
diff --git a/products/ai_observability/frontend/redesign/trace/components/Thread.tsx b/products/ai_observability/frontend/redesign/trace/components/Thread.tsx
index a4ffd8fa3dc7..fa407aa90d89 100644
--- a/products/ai_observability/frontend/redesign/trace/components/Thread.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/Thread.tsx
@@ -4,13 +4,23 @@ import { ConversationState } from '../types'
import { conversationalMessages } from './conversationalMessages'
import { ErrorCallout } from './ErrorCallout'
import { ThreadTurn } from './ThreadTurn'
+import { useSearchHighlight } from './useSearchHighlight'
export interface ThreadProps {
conversation: ConversationState
onSelectMessage: (sourceNodeId: string) => void
+ highlightQuery?: string | null
+ /** The steps that contain the search query, or null when nothing is searched. */
+ matchingNodeIds?: ReadonlySet<string> | null
}
-export function Thread({ conversation, onSelectMessage }: ThreadProps): JSX.Element {
+export function Thread({
+ conversation,
+ onSelectMessage,
+ highlightQuery = null,
+ matchingNodeIds = null,
+}: ThreadProps): JSX.Element {
+ const highlightRef = useSearchHighlight(highlightQuery)
if (conversation.status === 'loading') {
return (
<div className="flex flex-col gap-3" role="status" aria-busy="true" aria-label="Loading conversation">
@@ -32,13 +42,14 @@ export function Thread({ conversation, onSelectMessage }: ThreadProps): JSX.Elem
)
}
return (
- <div className="flex flex-col gap-3">
+ <div className="flex flex-col gap-3" ref={highlightRef}>
{turns.map((turn) => (
<ThreadTurn
key={turn.id}
turn={turn}
isActive={turns.length > 1 && turn.id === activeTurnId}
onSelectMessage={onSelectMessage}
+ matchingNodeIds={matchingNodeIds}
/>
))}
</div>
diff --git a/products/ai_observability/frontend/redesign/trace/components/ThreadBubble.tsx b/products/ai_observability/frontend/redesign/trace/components/ThreadBubble.tsx
index e70b33b4eadd..b30a7db418d3 100644
--- a/products/ai_observability/frontend/redesign/trace/components/ThreadBubble.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/ThreadBubble.tsx
@@ -9,15 +9,17 @@ import { MessagePartView } from './MessagePartView'
export interface ThreadBubbleProps {
message: ThreadMessage
onSelect?: () => void
+ isFaded?: boolean
}
-export function ThreadBubble({ message, onSelect }: ThreadBubbleProps): JSX.Element {
+export function ThreadBubble({ message, onSelect, isFaded = false }: ThreadBubbleProps): JSX.Element {
const isUser = message.role === 'user'
const baseClassName = cn(
'flex flex-col gap-2 text-left text-sm',
'max-w-4/5 rounded-lg px-3 py-2',
isUser ? 'self-end bg-fill-highlight-100' : 'self-start border border-primary bg-surface-primary',
- message.isInternal && 'text-secondary'
+ message.isInternal && 'text-secondary',
+ isFaded && 'opacity-50'
)
const content = message.parts.map((part, index) => <MessagePartView key={index} part={part} />)
diff --git a/products/ai_observability/frontend/redesign/trace/components/ThreadTurn.test.tsx b/products/ai_observability/frontend/redesign/trace/components/ThreadTurn.test.tsx
new file mode 100644
index 000000000000..a958a46e05a0
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/ThreadTurn.test.tsx
@@ -0,0 +1,34 @@
+import { cleanup, render } from '@testing-library/react'
+
+import { ConversationTurn } from '../types'
+import { ThreadTurn } from './ThreadTurn'
+
+const turn: ConversationTurn = {
+ id: 'trace-1',
+ timestamp: '2026-09-01T10:15:00Z',
+ error: null,
+ messages: [
+ {
+ id: 'gen-1-out-0',
+ role: 'assistant',
+ parts: [{ kind: 'text', text: 'Two seats were added.' }],
+ isInternal: false,
+ sourceNodeId: 'gen-1',
+ },
+ ],
+}
+
+describe('ThreadTurn', () => {
+ afterEach(cleanup)
+
+ test.each([
+ ['matches, even where the text does not show the query', new Set(['gen-1']), false],
+ ['does not match', new Set(['gen-2']), true],
+ ])('fades a message by whether its step %s', (_name, matchingNodeIds, faded) => {
+ const { getByText } = render(
+ <ThreadTurn turn={turn} isActive={false} onSelectMessage={() => {}} matchingNodeIds={matchingNodeIds} />
+ )
+
+ expect(getByText('Two seats were added.').closest('.opacity-50') !== null).toBe(faded)
+ })
+})
diff --git a/products/ai_observability/frontend/redesign/trace/components/ThreadTurn.tsx b/products/ai_observability/frontend/redesign/trace/components/ThreadTurn.tsx
index 53fd8d43cdee..340478ae644c 100644
--- a/products/ai_observability/frontend/redesign/trace/components/ThreadTurn.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/ThreadTurn.tsx
@@ -14,9 +14,11 @@ export interface ThreadTurnProps {
turn: ConversationTurn
isActive: boolean
onSelectMessage: (sourceNodeId: string) => void
+ /** The steps that contain the search query, or null when nothing is searched. */
+ matchingNodeIds?: ReadonlySet<string> | null
}
-export function ThreadTurn({ turn, isActive, onSelectMessage }: ThreadTurnProps): JSX.Element {
+export function ThreadTurn({ turn, isActive, onSelectMessage, matchingNodeIds = null }: ThreadTurnProps): JSX.Element {
const [showInternal, setShowInternal] = useState(false)
const conversational = conversationalMessages(turn.messages)
const hiddenCount = conversational.filter((message) => message.isInternal).length
@@ -42,6 +44,9 @@ export function ThreadTurn({ turn, isActive, onSelectMessage }: ThreadTurnProps)
key={message.id}
message={message}
onSelect={sourceNodeId ? () => onSelectMessage(sourceNodeId) : undefined}
+ isFaded={
+ matchingNodeIds !== null && sourceNodeId !== null && !matchingNodeIds.has(sourceNodeId)
+ }
/>
)
})}
diff --git a/products/ai_observability/frontend/redesign/trace/components/TimelineRow.tsx b/products/ai_observability/frontend/redesign/trace/components/TimelineRow.tsx
index fbe16360a2cd..8f9b089fa4a9 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TimelineRow.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TimelineRow.tsx
@@ -18,11 +18,20 @@ export interface TimelineRowProps {
totalMs: number
ticks: TimelineTick[]
isSelected: boolean
+ isFaded?: boolean
onSelect: () => void
onPrefetch: () => void
}
-export function TimelineRow({ row, totalMs, ticks, isSelected, onSelect, onPrefetch }: TimelineRowProps): JSX.Element {
+export function TimelineRow({
+ row,
+ totalMs,
+ ticks,
+ isSelected,
+ isFaded = false,
+ onSelect,
+ onPrefetch,
+}: TimelineRowProps): JSX.Element {
const { leftPercent, widthPercent } = timelineBarGeometry(row.startMs, row.durationMs ?? 0, totalMs)
const barColors = row.hasError ? ERROR_BAR_COLORS : NODE_KIND_COLORS[row.kind]
return (
@@ -35,7 +44,8 @@ export function TimelineRow({ row, totalMs, ticks, isSelected, onSelect, onPrefe
className={cn(
'grid w-full items-center border-b border-primary text-left hover:bg-fill-button-tertiary-hover',
TIMELINE_COLUMNS,
- isSelected && 'bg-fill-button-tertiary-active'
+ isSelected && 'bg-fill-button-tertiary-active',
+ isFaded && 'opacity-50'
)}
>
<span
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceHeader.stories.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceHeader.stories.tsx
index 40bd9172295b..f14900753333 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceHeader.stories.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceHeader.stories.tsx
@@ -20,6 +20,20 @@ type Story = StoryObj<typeof TraceHeader>
export const Default: Story = {}
export const Errored: Story = { args: { hasError: true } }
+export const WithSearch: Story = {
+ args: {
+ search: {
+ query: 'seats',
+ status: 'ready',
+ total: 3,
+ matchingSteps: 2,
+ position: 1,
+ failure: null,
+ onQueryChange: () => {},
+ onStep: () => {},
+ },
+ },
+}
export const Narrow: Story = {
args: { name: 'a-very-long-trace-name-that-should-truncate-cleanly' },
decorators: [withWidth(360)],
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceHeader.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceHeader.tsx
index f5b7511ebae9..21c37fc0c6aa 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceHeader.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceHeader.tsx
@@ -3,6 +3,7 @@ import { LemonButton } from '@posthog/lemon-ui'
import { LabeledLink } from '../types'
import { TraceNeighbourLinks } from './TraceNeighbourLinks'
+import { TraceSearchBox, TraceSearchBoxProps } from './TraceSearchBox'
import { TraceStatusBadge } from './TraceStatusBadge'
export interface TraceHeaderProps {
@@ -11,9 +12,17 @@ export interface TraceHeaderProps {
olderHref: string | null
newerHref: string | null
backLink: LabeledLink
+ search?: TraceSearchBoxProps | null
}
-export function TraceHeader({ name, hasError, olderHref, newerHref, backLink }: TraceHeaderProps): JSX.Element {
+export function TraceHeader({
+ name,
+ hasError,
+ olderHref,
+ newerHref,
+ backLink,
+ search = null,
+}: TraceHeaderProps): JSX.Element {
return (
<header className="flex flex-wrap items-center gap-2">
<LemonButton
@@ -26,7 +35,8 @@ export function TraceHeader({ name, hasError, olderHref, newerHref, backLink }:
/>
<h1 className="m-0 min-w-0 truncate text-lg font-semibold">{name}</h1>
<TraceStatusBadge hasError={hasError} />
- <div className="ml-auto">
+ <div className="ml-auto flex min-w-0 flex-wrap items-center gap-2">
+ {search ? <TraceSearchBox {...search} /> : null}
<TraceNeighbourLinks olderHref={olderHref} newerHref={newerHref} />
</div>
</header>
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceSearchBox.stories.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceSearchBox.stories.tsx
new file mode 100644
index 000000000000..393bdfe455e7
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceSearchBox.stories.tsx
@@ -0,0 +1,32 @@
+import type { Meta, StoryObj } from '@storybook/react'
+
+import { TraceSearchBox } from './TraceSearchBox'
+
+const meta: Meta<typeof TraceSearchBox> = {
+ title: 'Scenes-App/AI observability/Trace view/Trace search box',
+ component: TraceSearchBox,
+ args: {
+ query: 'invoice',
+ status: 'ready',
+ total: 7,
+ matchingSteps: 3,
+ position: null,
+ failure: null,
+ onQueryChange: () => {},
+ onStep: () => {},
+ },
+}
+export default meta
+
+type Story = StoryObj<typeof TraceSearchBox>
+
+export const Idle: Story = { args: { query: '', status: 'idle', total: 0, matchingSteps: 0 } }
+export const Searching: Story = {
+ args: { status: 'loading', total: 0, matchingSteps: 0 },
+ parameters: { testOptions: { waitForLoadersToDisappear: false } },
+}
+export const Matches: Story = {}
+export const MatchSelected: Story = { args: { position: 2 } }
+export const NoMatches: Story = { args: { query: 'refund', total: 0, matchingSteps: 0 } }
+export const TooSlow: Story = { args: { status: 'error', failure: 'too_slow', total: 0, matchingSteps: 0 } }
+export const Failed: Story = { args: { status: 'error', failure: 'failed', total: 0, matchingSteps: 0 } }
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceSearchBox.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceSearchBox.tsx
new file mode 100644
index 000000000000..424d4d40fb88
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceSearchBox.tsx
@@ -0,0 +1,104 @@
+import { IconChevronDown, IconWarning } from '@posthog/icons'
+import { LemonButton, LemonInput, Tooltip } from '@posthog/lemon-ui'
+
+import { pluralize } from 'lib/utils/strings'
+
+import { MatchDirection, TraceSearchFailure, TraceSearchStatus } from '../types'
+
+export interface TraceSearchBoxData {
+ query: string
+ status: TraceSearchStatus
+ total: number
+ matchingSteps: number
+ /** The 1-based place of the selected step among the matching steps, or null when it does not match. */
+ position: number | null
+ failure: TraceSearchFailure | null
+}
+
+export interface TraceSearchBoxProps extends TraceSearchBoxData {
+ onQueryChange: (query: string) => void
+ onStep: (direction: MatchDirection) => void
+}
+
+// The search API rejects longer queries.
+const MAX_QUERY_LENGTH = 200
+
+const FAILURES: Record<TraceSearchFailure, { label: string; tooltip: string }> = {
+ too_slow: {
+ label: 'Too large to search',
+ tooltip: "Searching this trace took too long. Open a step and use the browser's find instead.",
+ },
+ failed: { label: 'Search failed', tooltip: 'Something went wrong. Try again in a moment.' },
+}
+
+function SearchStatus({ status, total, matchingSteps, position, failure }: TraceSearchBoxData): JSX.Element | null {
+ if (status === 'idle') {
+ return null
+ }
+ if (status === 'error') {
+ const { label, tooltip } = FAILURES[failure ?? 'failed']
+ return (
+ <Tooltip title={tooltip}>
+ <span className="flex shrink-0 items-center gap-1 text-xs text-danger">
+ <IconWarning className="shrink-0" />
+ <span>{label}</span>
+ </span>
+ </Tooltip>
+ )
+ }
+ const text =
+ status === 'loading'
+ ? 'Searching…'
+ : total === 0
+ ? 'No matches'
+ : `${position === null ? '' : `${position}/${matchingSteps} · `}${pluralize(total, 'match', 'matches')}`
+ return (
+ <span className="shrink-0 text-xs whitespace-nowrap text-secondary" aria-live="polite" translate="no">
+ {text}
+ </span>
+ )
+}
+
+export function TraceSearchBox(props: TraceSearchBoxProps): JSX.Element {
+ const { query, matchingSteps, onQueryChange, onStep } = props
+ const disabledReason = matchingSteps === 0 ? 'No matching steps' : undefined
+ return (
+ <LemonInput
+ type="search"
+ size="small"
+ className="w-80 max-w-full"
+ placeholder="Search this trace"
+ value={query}
+ maxLength={MAX_QUERY_LENGTH}
+ onChange={onQueryChange}
+ onPressEnter={(event) => onStep(event.shiftKey ? 'previous' : 'next')}
+ data-attr="trace-view-search-input"
+ suffixAfterClear
+ suffix={
+ <span className="flex shrink-0 items-center gap-0.5">
+ <SearchStatus {...props} />
+ <LemonButton
+ size="xsmall"
+ noPadding
+ icon={<IconChevronDown className="rotate-180" />}
+ onClick={() => onStep('previous')}
+ disabledReason={disabledReason}
+ tooltip="Previous match (Shift+Enter)"
+ aria-label="Previous match"
+ data-attr="trace-view-search-previous"
+ />
+ <LemonButton
+ size="xsmall"
+ noPadding
+ icon={<IconChevronDown />}
+ onClick={() => onStep('next')}
+ disabledReason={disabledReason}
+ tooltip="Next match (Enter)"
+ aria-label="Next match"
+ data-attr="trace-view-search-next"
+ />
+ </span>
+ }
+ />
+ )
+}
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceSplitView.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceSplitView.tsx
index a2369d7c023b..74dbfe2db1e3 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceSplitView.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceSplitView.tsx
@@ -8,9 +8,12 @@ export interface TraceSplitViewProps {
selectedNodeId: string | null
onSelectNode: (id: string) => void
onPrefetchNode: (id: string) => void
- detail: Omit<NodeDetailProps, 'onSelectNode'> | null
+ detail: Omit<NodeDetailProps, 'onSelectNode' | 'highlightQuery' | 'matchStepCount'> | null
evalOutcomes: Record<string, EvalRollupOutcome>
feedbackCountByNodeId?: Record<string, number>
+ matchingNodeIds?: ReadonlySet<string> | null
+ highlightQuery?: string | null
+ matchStepCount?: number
}
export function TraceSplitView({
@@ -21,6 +24,9 @@ export function TraceSplitView({
detail,
evalOutcomes,
feedbackCountByNodeId,
+ matchingNodeIds = null,
+ highlightQuery = null,
+ matchStepCount = 0,
}: TraceSplitViewProps): JSX.Element {
return (
<div className="@container">
@@ -33,11 +39,18 @@ export function TraceSplitView({
onPrefetchNode={onPrefetchNode}
evalOutcomes={evalOutcomes}
feedbackCountByNodeId={feedbackCountByNodeId}
+ matchingNodeIds={matchingNodeIds}
+ matchStepCount={matchStepCount}
/>
</aside>
<div className="min-w-0 flex-1 @3xl:sticky @3xl:top-[var(--scene-title-section-height,50px)] @3xl:max-h-[calc(100vh-var(--scene-title-section-height,50px))] @3xl:overflow-y-auto">
{detail ? (
- <NodeDetail {...detail} onSelectNode={onSelectNode} />
+ <NodeDetail
+ {...detail}
+ onSelectNode={onSelectNode}
+ highlightQuery={highlightQuery}
+ matchStepCount={matchStepCount}
+ />
) : (
<p className="m-0 text-secondary">Select a step to see its details.</p>
)}
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceTimeline.stories.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceTimeline.stories.tsx
index 15fd6c4be9c3..961b9ab62ceb 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceTimeline.stories.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceTimeline.stories.tsx
@@ -22,6 +22,7 @@ export default meta
type Story = StoryObj<typeof TraceTimeline>
export const Default: Story = {}
+export const WithSearchMatches: Story = { args: { matchingNodeIds: new Set(['span-search', 'gen-answer']) } }
export const AgentRun: Story = {
args: { ...openaiAgentsWithEvals.timeline, selectedNodeId: openaiAgentsWithEvals.initialNodeId },
}
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceTimeline.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceTimeline.tsx
index 24ac221628ba..9426740bdfd6 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceTimeline.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceTimeline.tsx
@@ -11,6 +11,7 @@ export interface TraceTimelineProps {
selectedNodeId: string | null
onSelectNode: (id: string) => void
onPrefetchNode: (id: string) => void
+ matchingNodeIds?: ReadonlySet<string> | null
}
export function TraceTimeline({
@@ -19,6 +20,7 @@ export function TraceTimeline({
selectedNodeId,
onSelectNode,
onPrefetchNode,
+ matchingNodeIds = null,
}: TraceTimelineProps): JSX.Element {
if (rows.length === 0) {
return <p className="m-0 text-secondary">This trace has no timed steps.</p>
@@ -37,6 +39,7 @@ export function TraceTimeline({
totalMs={totalMs}
ticks={ticks}
isSelected={row.id === selectedNodeId}
+ isFaded={matchingNodeIds !== null && !matchingNodeIds.has(row.id)}
onSelect={() => onSelectNode(row.id)}
onPrefetch={() => onPrefetchNode(row.id)}
/>
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceTree.stories.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceTree.stories.tsx
index 018b09f3b98a..a80fa761f3bd 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceTree.stories.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceTree.stories.tsx
@@ -12,11 +12,13 @@ function Stateful({
initialSelectedId = 'gen-answer',
evalOutcomes = {},
feedbackCountByNodeId,
+ matchingNodeIds = null,
}: {
nodes: TraceNodeApi[]
initialSelectedId?: string
evalOutcomes?: Record<string, EvalRollupOutcome>
feedbackCountByNodeId?: Record<string, number>
+ matchingNodeIds?: ReadonlySet<string> | null
}): JSX.Element {
const [selected, setSelected] = useState<string | null>(initialSelectedId)
return (
@@ -27,6 +29,7 @@ function Stateful({
onPrefetchNode={() => {}}
evalOutcomes={evalOutcomes}
feedbackCountByNodeId={feedbackCountByNodeId}
+ matchingNodeIds={matchingNodeIds}
/>
)
}
@@ -50,6 +53,9 @@ export const WithEvalMarkers: Story = {
evalOutcomes: { 'trace-1': 'pass', 'embed-question': 'unrated', 'gen-answer': 'fail' },
},
}
+export const WithSearchMatches: Story = {
+ args: { nodes: FIXTURE_TREE, matchingNodeIds: new Set(['span-search', 'gen-answer']) },
+}
export const WithError: Story = {
args: {
nodes: [
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceTree.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceTree.tsx
index 733da42deacb..9a724e044296 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceTree.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceTree.tsx
@@ -9,6 +9,8 @@ export interface TraceTreeProps {
onPrefetchNode: (id: string) => void
evalOutcomes: Record<string, EvalRollupOutcome>
feedbackCountByNodeId?: Record<string, number>
+ matchingNodeIds?: ReadonlySet<string> | null
+ matchStepCount?: number
}
export function TraceTree({
@@ -18,6 +20,8 @@ export function TraceTree({
onPrefetchNode,
evalOutcomes,
feedbackCountByNodeId,
+ matchingNodeIds = null,
+ matchStepCount = 0,
}: TraceTreeProps): JSX.Element {
return (
<ul aria-label="Trace steps" className="m-0 list-none rounded border border-primary bg-surface-primary p-1">
@@ -31,6 +35,8 @@ export function TraceTree({
onPrefetchNode={onPrefetchNode}
evalOutcomes={evalOutcomes}
feedbackCountByNodeId={feedbackCountByNodeId}
+ matchingNodeIds={matchingNodeIds}
+ matchStepCount={matchStepCount}
/>
))}
</ul>
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceTreeRow.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceTreeRow.tsx
index ee4c538ffc8d..76470f31cf3c 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceTreeRow.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceTreeRow.tsx
@@ -1,3 +1,5 @@
+import { useEffect, useRef } from 'react'
+
import { IconWarning } from '@posthog/icons'
import { cn } from 'lib/utils/css-classes'
@@ -18,6 +20,10 @@ export interface TraceTreeRowProps {
evalOutcomes: Record<string, EvalRollupOutcome>
/** Feedback entries per step id, trace-level ones on the root. */
feedbackCountByNodeId?: Record<string, number>
+ /** The steps that contain the search query, or null when nothing is searched. */
+ matchingNodeIds?: ReadonlySet<string> | null
+ /** Changes on each step to a match, which scrolls the selected row into view. */
+ matchStepCount?: number
}
export function TraceTreeRow({
@@ -28,8 +34,16 @@ export function TraceTreeRow({
onPrefetchNode,
evalOutcomes,
feedbackCountByNodeId,
+ matchingNodeIds = null,
+ matchStepCount = 0,
}: TraceTreeRowProps): JSX.Element {
const isSelected = node.id === selectedNodeId
+ const rowRef = useRef<HTMLButtonElement>(null)
+ useEffect(() => {
+ if (isSelected && matchStepCount > 0) {
+ rowRef.current?.scrollIntoView({ block: 'nearest' })
+ }
+ }, [isSelected, matchStepCount])
// Node ids come from free-form trace ids, so a name like "constructor" would read an inherited property.
const evalOutcome = Object.hasOwn(evalOutcomes, node.id) ? evalOutcomes[node.id] : null
const feedbackCount =
@@ -37,6 +51,7 @@ export function TraceTreeRow({
return (
<li>
<button
+ ref={rowRef}
type="button"
onClick={() => onSelectNode(node.id)}
onMouseEnter={() => onPrefetchNode(node.id)}
@@ -45,7 +60,8 @@ export function TraceTreeRow({
data-attr="trace-view-tree-node"
className={cn(
'flex w-full min-w-0 items-start gap-2 rounded py-1 pr-1.5 text-left hover:bg-fill-button-tertiary-hover',
- isSelected && 'bg-fill-button-tertiary-active'
+ isSelected && 'bg-fill-button-tertiary-active',
+ matchingNodeIds && !matchingNodeIds.has(node.id) && 'opacity-50'
)}
// Depth is data, so the indent cannot be a static Tailwind class.
style={{ paddingLeft: `${0.375 + depth * 0.75}rem` }}
@@ -79,6 +95,8 @@ export function TraceTreeRow({
onPrefetchNode={onPrefetchNode}
evalOutcomes={evalOutcomes}
feedbackCountByNodeId={feedbackCountByNodeId}
+ matchingNodeIds={matchingNodeIds}
+ matchStepCount={matchStepCount}
/>
))}
</ul>
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceView.stories.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceView.stories.tsx
index a805550e3b06..c9c54500b26d 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceView.stories.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceView.stories.tsx
@@ -2,6 +2,8 @@ import type { Meta, StoryObj } from '@storybook/react'
import { useState } from 'react'
import type { TraceNodeApi } from '../../../generated/api.schemas'
+import type { TraceSearchResultsApi } from '../../../generated/api.schemas'
+import { toMatchingNodeIds, toSearchBox } from '../adapter/toSearch'
import { anthropicMessagesThinkingToolUse } from '../sampleFixtures/anthropicMessagesThinkingToolUse'
import { claudeCodePluginSession } from '../sampleFixtures/claudeCodePluginSession'
import { langchainCerebrasImageError } from '../sampleFixtures/langchainCerebrasImageError'
@@ -51,15 +53,33 @@ const FIXTURE_ERROR_TRACE: SampleTraceFixture = {
},
}
+interface PlaygroundSearch {
+ query: string
+ matchCounts: Record<string, number>
+}
+
interface PlaygroundProps {
fixture: SampleTraceFixture
initialMode: TraceMode
initialNodeId?: string
evals?: EvalsChipData
evalOutcomes?: Record<string, EvalRollupOutcome>
+ search?: PlaygroundSearch
}
-function Playground({ fixture, initialMode, initialNodeId, evals, evalOutcomes = {} }: PlaygroundProps): JSX.Element {
+function searchResults(search: PlaygroundSearch): TraceSearchResultsApi {
+ const matches = Object.entries(search.matchCounts).map(([nodeId, count]) => ({ nodeId, count }))
+ return { total: matches.reduce((sum, match) => sum + match.count, 0), matches }
+}
+
+function Playground({
+ fixture,
+ initialMode,
+ initialNodeId,
+ evals,
+ evalOutcomes = {},
+ search,
+}: PlaygroundProps): JSX.Element {
const [mode, setMode] = useState<TraceMode>(initialMode)
const [selectedNodeId, setSelectedNodeId] = useState(initialNodeId ?? fixture.initialNodeId)
const [tab, setTab] = useState<NodeDetailTab>('messages')
@@ -94,6 +114,17 @@ function Playground({ fixture, initialMode, initialNodeId, evals, evalOutcomes =
thread={fixture.thread}
timeline={fixture.timeline}
evalOutcomes={evalOutcomes}
+ search={
+ search
+ ? {
+ ...toSearchBox(search.query, 'ready', searchResults(search), null, selectedNodeId),
+ onQueryChange: () => {},
+ onStep: () => {},
+ }
+ : null
+ }
+ matchingNodeIds={search ? toMatchingNodeIds(searchResults(search)) : null}
+ highlightQuery={search?.query ?? null}
summaryMode={{
state: { status: 'ready', summary: FIXTURE_SUMMARY },
requesting: false,
@@ -141,6 +172,14 @@ export const NarrowWithEvals: Story = {
},
decorators: [withWidth(520)],
}
+export const NarrowWithSearch: Story = {
+ args: {
+ fixture: FIXTURE_TRACE,
+ initialMode: 'spans',
+ search: { query: 'seats', matchCounts: { 'gen-answer': 2 } },
+ },
+ decorators: [withWidth(520)],
+}
export const SampleAnthropicThinkingToolUse: Story = {
args: { fixture: anthropicMessagesThinkingToolUse, initialMode: 'spans' },
}
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceView.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceView.tsx
index 2a9daca9cd52..3511b69616b9 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceView.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceView.tsx
@@ -6,6 +6,7 @@ import { Thread } from './Thread'
import { TraceFeedbackView } from './TraceFeedbackView'
import { TraceHeader, TraceHeaderProps } from './TraceHeader'
import { TraceModeTabs } from './TraceModeTabs'
+import { TraceSearchBoxProps } from './TraceSearchBox'
import { TraceSplitView } from './TraceSplitView'
import { TraceSummaryBar, TraceSummaryBarProps } from './TraceSummaryBar'
import { TraceTimeline } from './TraceTimeline'
@@ -13,7 +14,7 @@ import { TraceViewError } from './TraceViewError'
import { TraceViewLoading } from './TraceViewLoading'
export interface TraceViewReadyProps {
- header: TraceHeaderProps
+ header: Omit<TraceHeaderProps, 'search'>
summary: TraceSummaryBarProps
mode: TraceMode
onModeChange: (mode: TraceMode) => void
@@ -22,10 +23,16 @@ export interface TraceViewReadyProps {
onSelectNode: (id: string) => void
onPrefetchNode: (id: string) => void
onSelectFromView: (id: string) => void
- detail: Omit<NodeDetailProps, 'onSelectNode'> | null
+ detail: Omit<NodeDetailProps, 'onSelectNode' | 'highlightQuery' | 'matchStepCount'> | null
thread: ConversationState
timeline: { rows: TraceTimelineRowApi[]; totalMs: number }
+ search?: TraceSearchBoxProps | null
evalOutcomes: Record<string, EvalRollupOutcome>
+ /** The steps that contain the search query, or null when nothing is searched. */
+ matchingNodeIds?: ReadonlySet<string> | null
+ highlightQuery?: string | null
+ /** Changes on each step to a match, which scrolls the selected step and its first highlight into view. */
+ matchStepCount?: number
summaryMode: SummaryModeViewProps
feedbackMode?: FeedbackModeState
onRetryFeedback?: () => void
@@ -51,7 +58,7 @@ export function TraceView(props: TraceViewProps): JSX.Element {
}
return (
<div className="flex flex-col gap-3">
- <TraceHeader {...props.header} />
+ <TraceHeader {...props.header} search={props.search} />
<TraceSummaryBar {...props.summary} />
<TraceModeTabs mode={props.mode} onModeChange={props.onModeChange} feedbackCount={props.feedbackCount} />
{props.mode === 'spans' ? (
@@ -63,9 +70,17 @@ export function TraceView(props: TraceViewProps): JSX.Element {
detail={props.detail}
evalOutcomes={props.evalOutcomes}
feedbackCountByNodeId={props.feedbackCountByNodeId}
+ matchingNodeIds={props.matchingNodeIds}
+ highlightQuery={props.highlightQuery}
+ matchStepCount={props.matchStepCount}
/>
) : props.mode === 'thread' ? (
- <Thread conversation={props.thread} onSelectMessage={props.onSelectFromView} />
+ <Thread
+ conversation={props.thread}
+ onSelectMessage={props.onSelectFromView}
+ highlightQuery={props.highlightQuery}
+ matchingNodeIds={props.matchingNodeIds}
+ />
) : props.mode === 'summary' ? (
<SummaryModeView {...props.summaryMode} />
) : props.mode === 'feedback' ? (
@@ -81,6 +96,7 @@ export function TraceView(props: TraceViewProps): JSX.Element {
selectedNodeId={props.selectedNodeId}
onSelectNode={props.onSelectFromView}
onPrefetchNode={props.onPrefetchNode}
+ matchingNodeIds={props.matchingNodeIds}
/>
)}
</div>
diff --git a/products/ai_observability/frontend/redesign/trace/components/messageContainsText.test.ts b/products/ai_observability/frontend/redesign/trace/components/messageContainsText.test.ts
new file mode 100644
index 000000000000..dc52a211ea4d
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/messageContainsText.test.ts
@@ -0,0 +1,19 @@
+import { MessagePart, ThreadMessage } from '../types'
+import { messageContainsText } from './messageContainsText'
+
+function message(part: MessagePart): ThreadMessage {
+ return { id: 'm1', role: 'assistant', parts: [part], isInternal: false, sourceNodeId: 'gen-1' }
+}
+
+const CASES: [string, MessagePart, boolean][] = [
+ ['a text part, ignoring case', { kind: 'text', text: 'Your Invoice went up.' }, true],
+ ['a tool call argument', { kind: 'toolCall', name: 'lookup', args: { id: 'invoice-42' } }, true],
+ ['a thinking part', { kind: 'thinking', text: 'The invoice changed in August.' }, true],
+ ['a message without the text', { kind: 'text', text: 'Two seats were added.' }, false],
+]
+
+describe('messageContainsText', () => {
+ test.each(CASES)('finds the query in %s', (_name, part, expected) => {
+ expect(messageContainsText(message(part), 'invoice')).toBe(expected)
+ })
+})
diff --git a/products/ai_observability/frontend/redesign/trace/components/messageContainsText.ts b/products/ai_observability/frontend/redesign/trace/components/messageContainsText.ts
new file mode 100644
index 000000000000..670e545a6bfa
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/messageContainsText.ts
@@ -0,0 +1,18 @@
+import { MessagePart, ThreadMessage } from '../types'
+
+function partText(part: MessagePart): string {
+ switch (part.kind) {
+ case 'text':
+ case 'thinking':
+ return part.text
+ case 'toolCall':
+ return [part.name, JSON.stringify(part.args ?? null), JSON.stringify(part.result ?? null)].join('\n')
+ case 'attachment':
+ return part.name ?? ''
+ }
+}
+
+export function messageContainsText(message: ThreadMessage, query: string): boolean {
+ const needle = query.toLowerCase()
+ return message.parts.some((part) => partText(part).toLowerCase().includes(needle))
+}
diff --git a/products/ai_observability/frontend/redesign/trace/components/traceSearchHighlight.scss b/products/ai_observability/frontend/redesign/trace/components/traceSearchHighlight.scss
new file mode 100644
index 000000000000..334c2d59f917
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/traceSearchHighlight.scss
@@ -0,0 +1,4 @@
+::highlight(trace-view-search) {
+ color: var(--color-black);
+ background-color: var(--mark);
+}
diff --git a/products/ai_observability/frontend/redesign/trace/components/useSearchHighlight.test.ts b/products/ai_observability/frontend/redesign/trace/components/useSearchHighlight.test.ts
new file mode 100644
index 000000000000..be26446cf09c
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/useSearchHighlight.test.ts
@@ -0,0 +1,14 @@
+import { MAX_HIGHLIGHTS, highlightRanges } from './useSearchHighlight'
+
+describe('highlightRanges', () => {
+ test.each([
+ ['inside one text node', '<p>Where is my invoice?</p>', 'INVOICE', ['invoice']],
+ ['across syntax tokens', '<code><span>total</span> = <span>42</span></code>', 'total = 42', ['total = 42']],
+ ['up to the cap', `<p>${'a'.repeat(MAX_HIGHLIGHTS + 10)}</p>`, 'a', Array(MAX_HIGHLIGHTS).fill('a')],
+ ])('marks matches %s', (_name, html, query, expected) => {
+ const container = document.createElement('div')
+ container.innerHTML = html
+
+ expect(highlightRanges(container, query).map((range) => range.toString())).toEqual(expected)
+ })
+})
diff --git a/products/ai_observability/frontend/redesign/trace/components/useSearchHighlight.ts b/products/ai_observability/frontend/redesign/trace/components/useSearchHighlight.ts
new file mode 100644
index 000000000000..911d7d03ba7a
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/components/useSearchHighlight.ts
@@ -0,0 +1,96 @@
+import './traceSearchHighlight.scss'
+
+import { RefCallback, useEffect, useRef, useState } from 'react'
+
+// pinned: the stylesheet styles `::highlight(trace-view-search)`
+const HIGHLIGHT_NAME = 'trace-view-search'
+
+// A query for a common character in a long message would otherwise create a range per character on every render.
+export const MAX_HIGHLIGHTS = 500
+
+interface TextRun {
+ node: Node
+ start: number
+}
+
+function boundary(runs: TextRun[], offset: number, side: 'start' | 'end'): [Node, number] {
+ let low = 0
+ let high = runs.length - 1
+ while (low < high) {
+ const middle = Math.ceil((low + high) / 2)
+ const runStart = runs[middle].start
+ if (side === 'start' ? runStart <= offset : runStart < offset) {
+ low = middle
+ } else {
+ high = middle - 1
+ }
+ }
+ return [runs[low].node, offset - runs[low].start]
+}
+
+/** Matches run across text nodes, because syntax highlighting splits a code line into one node per token. */
+export function highlightRanges(container: HTMLElement, query: string): Range[] {
+ const needle = query.toLowerCase()
+ const runs: TextRun[] = []
+ let haystack = ''
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT)
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
+ const text = node.textContent ?? ''
+ const lowered = text.toLowerCase()
+ runs.push({ node, start: haystack.length })
+ // Lowercasing can change the length of some characters, which would misplace every offset after them.
+ haystack += lowered.length === text.length ? lowered : text
+ }
+ const ranges: Range[] = []
+ for (
+ let index = haystack.indexOf(needle);
+ index !== -1 && ranges.length < MAX_HIGHLIGHTS;
+ index = haystack.indexOf(needle, index + needle.length)
+ ) {
+ const range = new Range()
+ range.setStart(...boundary(runs, index, 'start'))
+ range.setEnd(...boundary(runs, index + needle.length, 'end'))
+ ranges.push(range)
+ }
+ return ranges
+}
+
+function findOrCreateHighlight(highlights: HighlightRegistry): Highlight {
+ const highlight = highlights.get(HIGHLIGHT_NAME) ?? new Highlight()
+ highlights.set(HIGHLIGHT_NAME, highlight)
+ return highlight
+}
+
+/**
+ * Marks each case-insensitive occurrence of the query in the text of the element the returned ref is attached to,
+ * without changing the DOM. When `revealKey` changes, the first occurrence scrolls into view once it renders.
+ */
+export function useSearchHighlight(query: string | null, revealKey: number = 0): RefCallback<HTMLElement> {
+ const [element, setElement] = useState<HTMLElement | null>(null)
+ const revealedKey = useRef(0)
+ useEffect(() => {
+ if (!element || !query) {
+ return
+ }
+ const highlight = typeof CSS !== 'undefined' && CSS.highlights ? findOrCreateHighlight(CSS.highlights) : null
+ let ranges: Range[] = []
+ const mark = (): void => {
+ ranges.forEach((range) => highlight?.delete(range))
+ ranges = highlightRanges(element, query)
+ ranges.forEach((range) => highlight?.add(range))
+ if (ranges.length > 0 && revealedKey.current !== revealKey) {
+ revealedKey.current = revealKey
+ ranges[0].startContainer.parentElement?.scrollIntoView({ block: 'nearest' })
+ }
+ }
+ mark()
+ // Steps load and tabs switch inside the container after the query is set.
+ const observer = new MutationObserver(mark)
+ observer.observe(element, { childList: true, subtree: true, characterData: true })
+ return () => {
+ observer.disconnect()
+ ranges.forEach((range) => highlight?.delete(range))
+ }
+ }, [element, query, revealKey])
+ return setElement
+}
diff --git a/products/ai_observability/frontend/redesign/trace/traceSearchLogic.test.ts b/products/ai_observability/frontend/redesign/trace/traceSearchLogic.test.ts
new file mode 100644
index 000000000000..d653cf6917d0
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/traceSearchLogic.test.ts
@@ -0,0 +1,180 @@
+import { waitFor } from '@testing-library/react'
+import { expectLogic } from 'kea-test-utils'
+import posthog from 'posthog-js'
+
+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
+
+import { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import type { TraceSearchResultsApi } from '../../generated/api.schemas'
+import { makeTraceResource } from './adapter/testFixtures'
+import { SEARCH_DEBOUNCE_MS, traceSearchLogic } from './traceSearchLogic'
+import { traceViewLogic } from './traceViewLogic'
+
+const TRACE_URL = '/api/projects/:team_id/ai_observability/traces/:id/'
+const EVALUATIONS_URL = '/api/projects/:team_id/ai_observability/traces/:id/evaluations/'
+const SEARCH_URL = '/api/projects/:team_id/ai_observability/traces/:id/search/'
+const CREATED_AT = '2026-09-01T10:15:00Z'
+
+function resultsFor(query: string): TraceSearchResultsApi {
+ return { total: query.length, matches: [{ nodeId: 'gen-1', count: query.length }] }
+}
+
+function deferred(): { promise: Promise<void>; resolve: () => void } {
+ let resolve = (): void => {}
+ const promise = new Promise<void>((done) => {
+ resolve = done
+ })
+ return { promise, resolve }
+}
+
+describe('traceSearchLogic', () => {
+ let logic: ReturnType<typeof traceSearchLogic.build>
+ let searches: { q: string | null; hint: string | null }[]
+ let searchStatus: number
+ let searchStatuses: Record<string, number>
+ let searchGates: Record<string, Promise<void>>
+ let traceGate: Promise<void>
+
+ beforeEach(() => {
+ searches = []
+ searchStatus = 200
+ searchStatuses = {}
+ searchGates = {}
+ traceGate = Promise.resolve()
+ useMocks({
+ get: {
+ [TRACE_URL]: async () => {
+ await traceGate
+ return [200, makeTraceResource({ createdAt: CREATED_AT })]
+ },
+ [EVALUATIONS_URL]: () => [200, { results: [] }],
+ [SEARCH_URL]: async ({ request }) => {
+ const params = new URL(request.url).searchParams
+ const q = params.get('q')
+ searches.push({ q, hint: params.get('timestamp_hint') })
+ await (searchGates[q ?? ''] ?? Promise.resolve())
+ const status = searchStatuses[q ?? ''] ?? searchStatus
+ return status === 200 ? [200, resultsFor(q ?? '')] : [status, { detail: 'nope' }]
+ },
+ },
+ })
+ initKeaTests()
+ logic = traceSearchLogic({ traceId: 'trace-1', timestampHint: null })
+ })
+
+ afterEach(() => {
+ logic.unmount()
+ resumeKeaLoadersErrors()
+ jest.useRealTimers()
+ jest.restoreAllMocks()
+ })
+
+ async function mountWithTrace(): Promise<void> {
+ logic.mount()
+ await expectLogic(traceViewLogic({ traceId: 'trace-1', timestampHint: null })).toFinishAllListeners()
+ }
+
+ function typeAndWait(...queries: string[]): void {
+ jest.useFakeTimers()
+ for (const query of queries) {
+ logic.actions.setQuery(query)
+ }
+ jest.advanceTimersByTime(SEARCH_DEBOUNCE_MS)
+ jest.useRealTimers()
+ }
+
+ it('sends one request with the trace timestamp once the typing settles', async () => {
+ await mountWithTrace()
+
+ typeAndWait('in', 'inv', 'invoice')
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(searches).toEqual([{ q: 'invoice', hint: CREATED_AT }])
+ expect(logic.values).toMatchObject({ searchResults: resultsFor('invoice'), searchStatus: 'ready' })
+ })
+
+ it.each([
+ ['answers', 200],
+ ['fails', 500],
+ ])('sends only the latest query once the request in flight %s', async (_name, firstStatus) => {
+ silenceKeaLoadersErrors()
+ await mountWithTrace()
+ const firstAnswer = deferred()
+ searchGates.first = firstAnswer.promise
+ searchStatuses.first = firstStatus
+
+ typeAndWait('first')
+ await waitFor(() => expect(searches).toHaveLength(1))
+ typeAndWait('second')
+ typeAndWait('third')
+ expect(searches).toHaveLength(1)
+ firstAnswer.resolve()
+ await waitFor(() => expect(logic.values.searchResults).toEqual(resultsFor('third')))
+
+ expect(searches.map(({ q }) => q)).toEqual(['first', 'third'])
+ expect(logic.values).toMatchObject({ searchFailure: null, searchStatus: 'ready' })
+ })
+
+ it('clears the results without a request when the query is blank', async () => {
+ await mountWithTrace()
+ typeAndWait('invoice')
+ await expectLogic(logic).toFinishAllListeners()
+
+ logic.actions.setQuery(' ')
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(searches).toHaveLength(1)
+ expect(logic.values).toMatchObject({ searchResults: null, searchStatus: 'idle' })
+ })
+
+ it.each([
+ ['a timeout', 504, 'too_slow'],
+ ['any other failure', 500, 'failed'],
+ ])('names %s without a toast', async (_name, status, failure) => {
+ silenceKeaLoadersErrors()
+ const toast = jest.spyOn(lemonToast, 'error')
+ searchStatus = status
+ await mountWithTrace()
+
+ typeAndWait('invoice')
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values).toMatchObject({ searchFailure: failure, searchStatus: 'error' })
+ expect(toast).not.toHaveBeenCalled()
+ })
+
+ it('waits for the trace before searching a query from a link', async () => {
+ const traceAnswer = deferred()
+ traceGate = traceAnswer.promise
+ logic.mount()
+
+ await expectLogic(logic, () => logic.actions.setQuery('invoice')).toDispatchActions([
+ 'loadSearchResultsSuccess',
+ ])
+ expect(searches).toEqual([])
+ expect(logic.values.searchStatus).toBe('loading')
+
+ traceAnswer.resolve()
+ await expectLogic(logic).toDispatchActions(['loadTraceSuccess', 'loadSearchResultsSuccess'])
+
+ expect(searches).toEqual([{ q: 'invoice', hint: CREATED_AT }])
+ })
+
+ it('reports each search it shows', async () => {
+ const capture = jest.spyOn(posthog, 'capture')
+ await mountWithTrace()
+
+ typeAndWait('invoice')
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(capture).toHaveBeenCalledWith('llma trace view searched', {
+ query_length: 7,
+ total: 7,
+ matching_steps: 1,
+ load_ms: expect.any(Number),
+ })
+ })
+})
diff --git a/products/ai_observability/frontend/redesign/trace/traceSearchLogic.ts b/products/ai_observability/frontend/redesign/trace/traceSearchLogic.ts
new file mode 100644
index 000000000000..4f815173861e
--- /dev/null
+++ b/products/ai_observability/frontend/redesign/trace/traceSearchLogic.ts
@@ -0,0 +1,179 @@
+import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
+import { loaders } from 'kea-loaders'
+import posthog from 'posthog-js'
+
+import { ApiConfig, ApiError } from 'lib/api'
+
+import { aiObservabilityTracesSearchRetrieve } from '../../generated/api'
+import type { TraceSearchResultsApi } from '../../generated/api.schemas'
+import { encodeTraceIdSegment } from './traceIdSegment'
+import { elapsedMs, traceViewLogic } from './traceViewLogic'
+import type { TraceSearchFailure, TraceSearchStatus } from './types'
+
+export interface TraceSearchLogicProps {
+ traceId: string
+ timestampHint: string | null
+}
+
+export const SEARCH_DEBOUNCE_MS = 300
+
+function toSearchFailure(error: unknown): TraceSearchFailure {
+ return error instanceof ApiError && error.status === 504 ? 'too_slow' : 'failed'
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface traceSearchLogicValues {
+ createdAt: string | null // traceViewLogic
+ query: string
+ searchFailure: TraceSearchFailure | null
+ searchResults: TraceSearchResultsApi | null
+ searchResultsLoading: boolean
+ searchStatus: TraceSearchStatus
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface traceSearchLogicActions {
+ loadTraceSuccess: (
+ trace: import('../../generated/api.schemas').TraceApi,
+ payload?: any
+ ) => {
+ payload?: any
+ trace: import('../../generated/api.schemas').TraceApi
+ } // traceViewLogic
+ loadSearchResults: (_: void) => void
+ loadSearchResultsFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadSearchResultsSuccess: (
+ searchResults: TraceSearchResultsApi | null,
+ payload?: void
+ ) => {
+ searchResults: TraceSearchResultsApi | null
+ payload?: void
+ }
+ setQuery: (query: string) => {
+ query: string
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface traceSearchLogicMeta {
+ key: string
+ __keaTypeGenInternalSelectorTypes: {
+ searchStatus: (
+ query: string,
+ searchFailure: TraceSearchFailure | null,
+ searchResults: TraceSearchResultsApi | null,
+ searchResultsLoading: boolean
+ ) => TraceSearchStatus
+ }
+}
+
+export type traceSearchLogicType = MakeLogicType<
+ traceSearchLogicValues,
+ traceSearchLogicActions,
+ TraceSearchLogicProps,
+ traceSearchLogicMeta
+>
+
+export const traceSearchLogic = kea<traceSearchLogicType>([
+ path((key) => ['products', 'ai_observability', 'frontend', 'redesign', 'trace', 'traceSearchLogic', key]),
+ props({} as TraceSearchLogicProps),
+ key((props) => props.traceId),
+ connect((props: TraceSearchLogicProps) => ({
+ values: [traceViewLogic({ traceId: props.traceId, timestampHint: props.timestampHint }), ['createdAt']],
+ actions: [traceViewLogic({ traceId: props.traceId, timestampHint: props.timestampHint }), ['loadTraceSuccess']],
+ })),
+ actions({
+ setQuery: (query: string) => ({ query }),
+ }),
+ reducers({
+ query: ['', { setQuery: (_, { query }) => query }],
+ }),
+ loaders(({ props, values, cache }) => ({
+ searchResults: [
+ null as TraceSearchResultsApi | null,
+ {
+ loadSearchResults: async (_: void, breakpoint): Promise<TraceSearchResultsApi | null> => {
+ const query = values.query.trim()
+ const createdAt = values.createdAt
+ if (!query || !createdAt) {
+ return null
+ }
+ await breakpoint(SEARCH_DEBOUNCE_MS)
+ // Each search scans the whole trace in ClickHouse, and a request the user moved past keeps running
+ // on the server, so the box sends the latest query only after the previous request answers.
+ await cache.searchInFlight
+ breakpoint()
+ cache.searchRequestedAt = performance.now()
+ const request = aiObservabilityTracesSearchRetrieve(
+ String(ApiConfig.getCurrentProjectId()),
+ encodeTraceIdSegment(props.traceId),
+ { q: query, timestamp_hint: createdAt }
+ )
+ cache.searchInFlight = request.then(
+ () => undefined,
+ () => undefined
+ )
+ const results = await request.catch((error: unknown) => {
+ // A superseded request must not show its failure on the newer query.
+ breakpoint()
+ throw error
+ })
+ breakpoint()
+ return results
+ },
+ },
+ ],
+ })),
+ reducers({
+ searchFailure: [
+ null as TraceSearchFailure | null,
+ {
+ loadSearchResults: () => null,
+ loadSearchResultsFailure: (_, { errorObject }) => toSearchFailure(errorObject),
+ },
+ ],
+ }),
+ selectors({
+ searchStatus: [
+ (s) => [s.query, s.searchFailure, s.searchResults, s.searchResultsLoading],
+ (
+ query: string,
+ failure: TraceSearchFailure | null,
+ results: TraceSearchResultsApi | null,
+ loading: boolean
+ ): TraceSearchStatus => {
+ if (!query.trim()) {
+ return 'idle'
+ }
+ if (failure) {
+ return 'error'
+ }
+ return loading || !results ? 'loading' : 'ready'
+ },
+ ],
+ }),
+ listeners(({ actions, values, cache }) => ({
+ setQuery: () => actions.loadSearchResults(),
+ loadSearchResultsSuccess: ({ searchResults }) => {
+ if (searchResults) {
+ posthog.capture('llma trace view searched', {
+ query_length: values.query.trim().length,
+ total: searchResults.total,
+ matching_steps: searchResults.matches.length,
+ load_ms: elapsedMs(cache.searchRequestedAt),
+ })
+ }
+ },
+ loadTraceSuccess: () => {
+ if (values.query.trim()) {
+ actions.loadSearchResults()
+ }
+ },
+ })),
+])
diff --git a/products/ai_observability/frontend/redesign/trace/traceViewLogic.ts b/products/ai_observability/frontend/redesign/trace/traceViewLogic.ts
index a0aaceae8e3f..d8dd5053f6d2 100644
--- a/products/ai_observability/frontend/redesign/trace/traceViewLogic.ts
+++ b/products/ai_observability/frontend/redesign/trace/traceViewLogic.ts
@@ -183,7 +183,7 @@ function toLoadFailure(error: unknown): LoadFailure {
const layoutNavOverrideOwner = (props: TraceViewLogicProps): string => `ai-trace-${props.traceId}`
-function elapsedMs(since: number): number {
+export function elapsedMs(since: number): number {
return Math.round(performance.now() - since)
}
diff --git a/products/ai_observability/frontend/redesign/trace/types.ts b/products/ai_observability/frontend/redesign/trace/types.ts
index d8fdd1129952..f297d2b2e635 100644
--- a/products/ai_observability/frontend/redesign/trace/types.ts
+++ b/products/ai_observability/frontend/redesign/trace/types.ts
@@ -126,6 +126,12 @@ export interface LabeledLink {
href: string
}
+export type TraceSearchStatus = 'idle' | 'loading' | 'ready' | 'error'
+
+export type TraceSearchFailure = 'too_slow' | 'failed'
+
+export type MatchDirection = 'next' | 'previous'
+
export type SummaryItemKind = 'bullet' | 'note'
/** Why a summary request failed. `throttled` means the user reached their summary limit. */
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 57bcd4a245c8..0faa5146dec8 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -121635,6 +121635,20 @@ export namespace Schemas {
queue_id?: string | null;
}
+ export interface TraceSearchMatch {
+ /** A step whose stored data contains the query. The root's id is the trace id. */
+ nodeId: string;
+ /** How many times the query occurs in the step's stored inputs, outputs, state, tools, name, model, provider and error, ignoring case. Keys of the stored JSON count too. */
+ count: number;
+ }
+
+ export interface TraceSearchResults {
+ /** Occurrences of the query across every matching step. */
+ total: number;
+ /** Matching steps in tree order, the root first. Steps without a match are left out. */
+ matches: TraceSearchMatch[];
+ }
+
export type TraceSummaryStatusEnum = typeof TraceSummaryStatusEnum[keyof typeof TraceSummaryStatusEnum];
@@ -130103,6 +130117,19 @@ export namespace Schemas {
timestamp_hint?: string;
};
+ export type AiObservabilityTracesSearchRetrieveParams = {
+ /**
+ * The text to find in the trace's steps, ignoring case.
+ * @minLength 1
+ * @maxLength 200
+ */
+ q: string;
+ /**
+ * When the trace happened, as carried by links into it. Lets a trace older than the AI events retention load from the shared events table.
+ */
+ timestamp_hint?: string;
+ };
+
export type AiObservabilityTracesSummaryCreateParams = {
/**
* When the trace happened, as carried by links into it. Lets a trace older than the AI events retention load from the shared events table.