Skip to content

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

fix(v4): allow public traces to be seen by authed users (#14903)

Nimar563f941c50c1
diff --git a/web/src/components/trace/api/usePrefetchObservation.ts b/web/src/components/trace/api/usePrefetchObservation.ts
index 412b9af4d5..9ae7827d44 100644
--- a/web/src/components/trace/api/usePrefetchObservation.ts
+++ b/web/src/components/trace/api/usePrefetchObservation.ts
@@ -26,6 +26,7 @@ export function usePrefetchObservation({
utils.events.batchIO.prefetch(
{
projectId,
+ traceId, // Must match useLogViewObservationIO for cache hit
observations: [{ id: observationId, traceId }],
minStartTime: startTime,
maxStartTime: startTime,
diff --git a/web/src/components/trace/components/TraceLogView/useLogViewObservationIO.ts b/web/src/components/trace/components/TraceLogView/useLogViewObservationIO.ts
index a78f812225..1ffe627429 100644
--- a/web/src/components/trace/components/TraceLogView/useLogViewObservationIO.ts
+++ b/web/src/components/trace/components/TraceLogView/useLogViewObservationIO.ts
@@ -54,6 +54,7 @@ export function useLogViewObservationIO({
const eventsQuery = api.events.batchIO.useQuery(
{
projectId,
+ traceId,
observations: [{ id: observationId, traceId }],
minStartTime: startTime,
maxStartTime: startTime,
diff --git a/web/src/features/events/hooks/useEventsTraceData.ts b/web/src/features/events/hooks/useEventsTraceData.ts
index 55a98e6faf..a51426e382 100644
--- a/web/src/features/events/hooks/useEventsTraceData.ts
+++ b/web/src/features/events/hooks/useEventsTraceData.ts
@@ -105,6 +105,7 @@ export function useEventsTraceData(
const rootIOQuery = api.events.batchIO.useQuery(
{
projectId,
+ traceId,
observations: primaryObservation
? [{ id: primaryObservation.id, traceId }]
: [],
diff --git a/web/src/features/events/server/eventsRouter.ts b/web/src/features/events/server/eventsRouter.ts
index 62b251c751..6a3545fc3b 100644
--- a/web/src/features/events/server/eventsRouter.ts
+++ b/web/src/features/events/server/eventsRouter.ts
@@ -79,6 +79,8 @@ export const BatchIOInput = zodSchema.object({
minStartTime: zodSchema.date(),
maxStartTime: zodSchema.date(),
truncated: zodSchema.boolean().optional(), // Defaults to true for performance
+ // Opts into trace-level auth (public traces) in protectedGetTraceProcedure
+ traceId: zodSchema.string().optional(),
});
export type BatchIOInput = z.infer<typeof BatchIOInput>;
@@ -178,18 +180,24 @@ export const eventsRouter = createTRPCRouter({
},
);
}),
- batchIO: protectedProjectProcedure
+ batchIO: protectedGetTraceProcedure
.input(BatchIOInput)
- .query(async ({ input, ctx }) => {
+ .query(async ({ input }) => {
return instrumentAsync(
{ name: "get-event-batch-io-trpc" },
async (span) => {
span.setAttribute("project_id", input.projectId);
span.setAttribute("observation_count", input.observations.length);
+ // the middleware only authorized access to input.traceId (which may
+ // merely be public) — never fetch other traces' observations through it
+ const observations = input.traceId
+ ? input.observations.filter((o) => o.traceId === input.traceId)
+ : input.observations;
+
const batchIO = await getEventBatchIO({
- projectId: ctx.session.projectId,
- observations: input.observations,
+ projectId: input.projectId,
+ observations,
minStartTime: input.minStartTime,
maxStartTime: input.maxStartTime,
truncated: input.truncated,
diff --git a/web/src/hooks/useParsedObservation.ts b/web/src/hooks/useParsedObservation.ts
index 27440e9eff..d090db1b52 100644
--- a/web/src/hooks/useParsedObservation.ts
+++ b/web/src/hooks/useParsedObservation.ts
@@ -225,6 +225,7 @@ export function useParsedObservation({
const eventsQuery = api.events.batchIO.useQuery(
{
projectId,
+ traceId,
observations: [{ id: observationId, traceId }],
minStartTime: startTime ?? new Date(0),
maxStartTime: startTime ?? new Date(),
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index 05c7c4e920..5f30394152 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -356,6 +356,12 @@ export const traceRouter = createTRPCRouter({
}),
)
.query(async ({ ctx }) => {
+ if (!ctx.trace) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Trace not found",
+ });
+ }
return {
...ctx.trace,
input: ctx.trace.input as string,
diff --git a/web/src/server/api/trpc.ts b/web/src/server/api/trpc.ts
index 940d694892..10b8552a7d 100644
--- a/web/src/server/api/trpc.ts
+++ b/web/src/server/api/trpc.ts
@@ -469,10 +469,11 @@ export const protectedOrganizationProcedure = withOtelTracingProcedure
* Protect trace-level getter routes.
* - Users need to be member of the project to access the trace.
* - Alternatively, the trace needs to be public.
+ * - Without a traceId, falls back to the project-membership check (trace: null).
*/
const inputTraceSchema = z.object({
- traceId: z.string(),
+ traceId: z.string().optional(),
projectId: z.string(),
timestamp: z.date().nullish(),
fromTimestamp: z.date().nullish(),
@@ -499,19 +500,21 @@ const enforceTraceAccess = t.middleware(async (opts) => {
const fromTimestamp = result.data.fromTimestamp;
const verbosity = result.data.verbosity;
- // eslint-disable-next-line @typescript-eslint/no-deprecated
- const clickhouseTrace = await getTraceById({
- traceId,
- projectId,
- timestamp: timestamp ?? undefined,
- fromTimestamp: fromTimestamp ?? undefined,
- renderingProps: {
- truncated: verbosity === "truncated",
- shouldJsonParse: false, // we do not want to parse the input/output for tRPC
- },
- });
+ const clickhouseTrace = traceId
+ ? // eslint-disable-next-line @typescript-eslint/no-deprecated
+ await getTraceById({
+ traceId,
+ projectId,
+ timestamp: timestamp ?? undefined,
+ fromTimestamp: fromTimestamp ?? undefined,
+ renderingProps: {
+ truncated: verbosity === "truncated",
+ shouldJsonParse: false, // we do not want to parse the input/output for tRPC
+ },
+ })
+ : null;
- if (!clickhouseTrace) {
+ if (traceId && !clickhouseTrace) {
logger.error(`Trace with id ${traceId} not found for project ${projectId}`);
throw new TRPCError({
code: "NOT_FOUND",
@@ -519,17 +522,19 @@ const enforceTraceAccess = t.middleware(async (opts) => {
});
}
- const trace = {
- ...clickhouseTrace,
- input: parseIO(clickhouseTrace.input, verbosity),
- output: parseIO(clickhouseTrace.output, verbosity),
- };
+ const trace = clickhouseTrace
+ ? {
+ ...clickhouseTrace,
+ input: parseIO(clickhouseTrace.input, verbosity),
+ output: parseIO(clickhouseTrace.output, verbosity),
+ }
+ : null;
const sessionProject = ctx.session?.user?.organizations
.flatMap((org) => org.projects)
.find(({ id }) => id === projectId);
- const traceSession = !!trace.sessionId
+ const traceSession = !!trace?.sessionId
? await ctx.prisma.traceSession.findFirst({
where: {
id: trace.sessionId,
@@ -544,7 +549,7 @@ const enforceTraceAccess = t.middleware(async (opts) => {
const isSessionPublic = traceSession?.public === true;
if (
- !trace.public &&
+ !trace?.public &&
!sessionProject &&
!isSessionPublic &&
ctx.session?.user?.admin !== true