Skip to content

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

fix(v4): use trace TS to get traces, not obs ts (#15135)

Nimar097d44c6a46b
diff --git a/web/src/__tests__/server/traces-trpc-events-only.servertest.ts b/web/src/__tests__/server/traces-trpc-events-only.servertest.ts
index 7292f5a103..9c5e720d6d 100644
--- a/web/src/__tests__/server/traces-trpc-events-only.servertest.ts
+++ b/web/src/__tests__/server/traces-trpc-events-only.servertest.ts
@@ -111,6 +111,49 @@ maybe("traces trpc (events_only write mode)", () => {
expect(env.LANGFUSE_MIGRATION_V4_WRITE_MODE).toBe("events_only");
});
+ it("loads a cross-midnight trace from a clicked observation timestamp", async () => {
+ const traceId = randomUUID();
+ const rootId = randomUUID();
+ const clickedId = randomUUID();
+ const rootTimestamp = new Date("2026-07-14T21:42:12.184Z");
+ const clickedTimestamp = new Date("2026-07-15T00:27:13.935Z");
+
+ await createEventsCh([
+ createEvent({
+ id: rootId,
+ span_id: rootId,
+ trace_id: traceId,
+ project_id: projectId,
+ parent_span_id: "",
+ start_time: rootTimestamp.getTime() * 1000,
+ }),
+ createEvent({
+ id: clickedId,
+ span_id: clickedId,
+ trace_id: traceId,
+ project_id: projectId,
+ parent_span_id: rootId,
+ start_time: clickedTimestamp.getTime() * 1000,
+ is_app_root: true,
+ }),
+ ]);
+ await waitForExpect(async () => {
+ const trace = await getTraceByIdFromEventsTable({ projectId, traceId });
+ expect(trace?.id).toBe(traceId);
+ });
+
+ const result = await caller.events.byTraceId({
+ projectId,
+ traceId,
+ timestamp: clickedTimestamp,
+ });
+
+ expect(result.observations.map(({ id }) => id)).toEqual(
+ expect.arrayContaining([rootId, clickedId]),
+ );
+ expect(result.observations).toHaveLength(2);
+ });
+
// On a fresh events_only deployment tracing data is written ONLY to the
// events tables; the legacy `traces` table stays intentionally empty. The
// onboarding gate must detect data in the events table, otherwise the
diff --git a/web/src/__tests__/server/traces-trpc.servertest.ts b/web/src/__tests__/server/traces-trpc.servertest.ts
index 31cf72a0e2..8694404091 100644
--- a/web/src/__tests__/server/traces-trpc.servertest.ts
+++ b/web/src/__tests__/server/traces-trpc.servertest.ts
@@ -567,23 +567,34 @@ describe("traces trpc", () => {
// the events table instead of 404ing on the legacy `traces` miss — the
// fast-preview list showed such traces while the detail view threw
// "Trace not found".
- // Gate matches the code: events-table reads work in dual (fallback) and
- // events_only (getTraceById routing); legacy deployments (azure /
- // redis-cluster CI) have no events tables and skip.
- const eventsTraceReadable =
- env.LANGFUSE_MIGRATION_V4_WRITE_MODE !== "legacy";
- (eventsTraceReadable ? it : it.skip)(
+ // This specifically covers the dual-write fallback. Events-only routing is
+ // covered in traces-trpc-events-only.servertest.ts.
+ const isDualWrite = env.LANGFUSE_MIGRATION_V4_WRITE_MODE === "dual";
+ (isDualWrite ? it : it.skip)(
"access trace that only exists in the events table",
async () => {
const traceId = randomUUID();
+ const rootId = randomUUID();
+ const clickedId = randomUUID();
+ const rootTimestamp = new Date("2026-07-14T21:42:12.184Z");
+ const clickedTimestamp = new Date("2026-07-15T00:27:13.935Z");
await createEventsCh([
createEvent({
- id: traceId,
- span_id: traceId,
+ id: rootId,
+ span_id: rootId,
trace_id: traceId,
project_id: projectId,
parent_span_id: null,
+ start_time: rootTimestamp.getTime() * 1000,
+ }),
+ createEvent({
+ id: clickedId,
+ span_id: clickedId,
+ trace_id: traceId,
+ project_id: projectId,
+ parent_span_id: rootId,
+ start_time: clickedTimestamp.getTime() * 1000,
}),
]);
@@ -594,12 +605,15 @@ describe("traces trpc", () => {
// ClickHouse insert visibility can lag.
await waitForExpect(async () => {
- const traceRes = await caller.traces.byId({
+ const result = await caller.events.byTraceId({
projectId,
traceId,
+ timestamp: clickedTimestamp,
});
- expect(traceRes?.id).toEqual(traceId);
- expect(traceRes?.projectId).toEqual(projectId);
+ expect(result.observations.map(({ id }) => id)).toEqual(
+ expect.arrayContaining([rootId, clickedId]),
+ );
+ expect(result.observations).toHaveLength(2);
});
},
);
diff --git a/web/src/features/events/server/eventsRouter.ts b/web/src/features/events/server/eventsRouter.ts
index ba74c1b39d..1ce5d02038 100644
--- a/web/src/features/events/server/eventsRouter.ts
+++ b/web/src/features/events/server/eventsRouter.ts
@@ -248,7 +248,7 @@ export const eventsRouter = createTRPCRouter({
timestamp: zodSchema.date().optional(),
}),
)
- .query(async ({ input }) => {
+ .query(async ({ input, ctx }) => {
return instrumentAsync(
{ name: "get-events-scores-for-trace-trpc" },
async (span) => {
@@ -258,7 +258,9 @@ export const eventsRouter = createTRPCRouter({
return getScoresAndCorrectionsForTraces({
projectId: input.projectId,
traceIds: [input.traceId],
- timestamp: input.timestamp,
+ // we need traceTS here because we filter for that in DB
+ // fallback to input in case trace unavailable - shouldn't happen
+ timestamp: ctx.trace?.timestamp ?? input.timestamp,
});
},
);
@@ -276,7 +278,7 @@ export const eventsRouter = createTRPCRouter({
timestamp: zodSchema.date().optional(),
}),
)
- .query(async ({ input }) => {
+ .query(async ({ input, ctx }) => {
return instrumentAsync(
{ name: "get-events-by-trace-id-trpc" },
async (span) => {
@@ -287,7 +289,9 @@ export const eventsRouter = createTRPCRouter({
await getObservationsForTraceFromEventsTable({
projectId: input.projectId,
traceId: input.traceId,
- timestamp: input.timestamp,
+ // we need traceTS here because we filter for that in DB
+ // fallback to input in case trace unavailable - shouldn't happen
+ timestamp: ctx.trace?.timestamp ?? input.timestamp,
});
return {
diff --git a/web/src/server/api/trpc.ts b/web/src/server/api/trpc.ts
index 1ac71726f8..51ce8dc1c5 100644
--- a/web/src/server/api/trpc.ts
+++ b/web/src/server/api/trpc.ts
@@ -508,14 +508,16 @@ const enforceTraceAccess = t.middleware(async (opts) => {
const timestamp = result.data.timestamp;
const fromTimestamp = result.data.fromTimestamp;
const verbosity = result.data.verbosity;
+ const isEventsOnly = env.LANGFUSE_MIGRATION_V4_WRITE_MODE === "events_only";
let clickhouseTrace = traceId
? // eslint-disable-next-line @typescript-eslint/no-deprecated
await getTraceById({
traceId,
projectId,
- timestamp: timestamp ?? undefined,
- fromTimestamp: fromTimestamp ?? undefined,
+ timestamp: isEventsOnly ? undefined : (timestamp ?? undefined),
+ fromTimestamp:
+ fromTimestamp ?? (isEventsOnly ? timestamp : undefined) ?? undefined,
renderingProps: {
truncated: verbosity === "truncated",
shouldJsonParse: false, // we do not want to parse the input/output for tRPC
@@ -526,9 +528,9 @@ const enforceTraceAccess = t.middleware(async (opts) => {
// In dual write mode the lookup above reads the legacy traces table, but
// internally produced traces (e.g. code-eval execution traces) were written
// to the events tables only — fall back so trace-level auth does not 404 on
- // a trace the events-backed views can render (LFE-10884). Gated on "dual"
- // because in "legacy" mode the events tables may not exist and in
- // "events_only" mode getTraceById already read them.
+ // a trace the events-backed views can render (LFE-10884). The timestamp can
+ // identify a clicked observation, so use it as a bounded lookup anchor rather
+ // than as the synthesized trace timestamp (LFE-10947).
if (
traceId &&
!clickhouseTrace &&
@@ -537,8 +539,7 @@ const enforceTraceAccess = t.middleware(async (opts) => {
clickhouseTrace = await getTraceByIdFromEventsTable({
traceId,
projectId,
- timestamp: timestamp ?? undefined,
- fromTimestamp: fromTimestamp ?? undefined,
+ fromTimestamp: fromTimestamp ?? timestamp ?? undefined,
renderingProps: {
truncated: verbosity === "truncated",
shouldJsonParse: false,