Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions apps/webapp/app/presenters/v3/SpanPresenter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
startedSessionExternalId,
traceRootRunId,
} from "./startedSession.server";
import { extractSessionStreamEntity } from "./sessionStreamEntity";

export type PromptSpanData = {
slug: string;
Expand Down Expand Up @@ -950,41 +951,30 @@ export class SpanPresenter extends BasePresenter {
};
}
case "session-stream": {
if (!span.entity.id) {
logger.error(`SpanPresenter: No session stream id`, {
spanId,
sessionStreamId: span.entity.id,
});
return { ...data, entity: null };
}

const parts = span.entity.id.split(":");
const io = parts.at(-1);
const channel = parts.at(-2) ?? "";
const sessionId = parts.at(-3);
const sessionStream = extractSessionStreamEntity({
entityId: span.entity.id,
entityMetadata: span.entity.metadata,
properties: span.properties,
});
Comment on lines +954 to +958

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Missing dashboard fix release note

The dashboard fix is user-facing and server-only, but the PR omits the required server release note.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.


if (!sessionId || (io !== "out" && io !== "in")) {
logger.error(`SpanPresenter: Invalid session stream id`, {
spanId,
sessionStreamId: span.entity.id,
});
if (!sessionStream) {
logger.error(
span.entity.id
? `SpanPresenter: Invalid session stream id`
: `SpanPresenter: No session stream id`,
{
spanId,
sessionStreamId: span.entity.id,
}
);
return { ...data, entity: null };
}

const metadata = span.entity.metadata
? (safeJsonParse(span.entity.metadata) as Record<string, unknown> | undefined)
: undefined;

return {
...data,
entity: {
type: "session-stream" as const,
object: {
sessionId,
channel: channel.length > 0 ? channel : undefined,
io,
metadata,
},
object: sessionStream,
},
};
}
Expand Down
73 changes: 73 additions & 0 deletions apps/webapp/app/presenters/v3/sessionStreamEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { safeJsonParse } from "~/utils/json";

type SessionStreamEntityObject = {
sessionId: string;
channel: string | undefined;
io: "out" | "in";
metadata: Record<string, unknown> | undefined;
};

/**
* Extracts `session-stream` span entity fields.
*
* Prefers unsplit `session`, `channel`, and `io` span properties because
* session keys may contain `:` (e.g. `clerk:user_123:v2`) and `"in"` stream
* entity IDs carry a leading `runId:` prefix.
*/
export function extractSessionStreamEntity({
entityId,
entityMetadata,
properties,
}: {
entityId?: string | null;
entityMetadata?: string | null;
properties?: unknown;
}): SessionStreamEntityObject | null {
const props =
properties && typeof properties === "object"
? (properties as Record<string, unknown>)
: undefined;

const propSessionId =
typeof props?.session === "string" && props.session.length > 0 ? props.session : undefined;
const propIo = props?.io === "out" || props?.io === "in" ? props.io : undefined;
const propChannel =
typeof props?.channel === "string" && props.channel.length > 0 ? props.channel : undefined;

let sessionId = propSessionId;
let io: string | undefined = propIo;
let channel = propChannel;

if (!sessionId || !io) {
Comment on lines +31 to +41

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Redacted spans break stream playback

When a span processor masks a session key, extractSessionStreamEntity uses the masked key for playback. UserSpanProcessorWrapper preserves the original entity ID, which previously supplied the key. The dashboard requests a session that does not exist.

Learn more

A session-stream span carries its address both in an internal entity ID and in public telemetry attributes, as shown in the output stream emitter. Projects can install a span processor that masks emails before those attributes reach the dashboard. UserSpanProcessorWrapper restores the internal ID but leaves the public attributes redacted. SpanEntity uses the extracted address to open the stream, so refreshing does not help.

Example: A task writes to the default output stream for alex@example.com. The documented email scrubber changes the public session attribute to [email] but leaves the entity ID as alex@example.com::out. The previous viewer opened the existing stream; the new viewer requests [email] and gets a 404 unless a session exists under that placeholder.

Recommended fix: Make the protected entity ID authoritative for current-format stream addresses, parsing the direction and channel from the right and removing the input run prefix. Use public attributes only for formats where they are needed to disambiguate the protected ID, or where the ID is absent. Add a regression case with a scrubbed public session attribute and an intact entity ID.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

if (!entityId) {
return null;
}

const parts = entityId.split(":");
const parsedIo = parts.at(-1);
const parsedChannel = parts.at(-2) ?? "";
const parsedSessionId =
parsedIo === "in" && parts.length >= 4 && parts[0]?.startsWith("run_")
? parts.slice(1, -2).join(":")
: parts.slice(0, -2).join(":");

sessionId = sessionId ?? (parsedSessionId.length > 0 ? parsedSessionId : undefined);
io = io ?? parsedIo;
channel = channel ?? (parsedChannel.length > 0 ? parsedChannel : undefined);
}

if (!sessionId || (io !== "out" && io !== "in")) {
return null;
}

const metadata = entityMetadata
? (safeJsonParse(entityMetadata) as Record<string, unknown> | undefined)
: undefined;

return {
sessionId,
channel,
io,
metadata,
};
}
122 changes: 122 additions & 0 deletions apps/webapp/test/sessionStreamEntity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { describe, expect, it } from "vitest";
import { extractSessionStreamEntity } from "~/presenters/v3/sessionStreamEntity";

describe("extractSessionStreamEntity", () => {
it("preserves colons in session keys for out spans using properties", () => {
expect(
extractSessionStreamEntity({
entityId: "clerk:user_123:v2::out",
properties: {
session: "clerk:user_123:v2",
io: "out",
},
})
).toEqual({
sessionId: "clerk:user_123:v2",
channel: undefined,
io: "out",
metadata: undefined,
});
});

it("extracts channel and metadata when session key contains colons", () => {
expect(
extractSessionStreamEntity({
entityId: "org:42:thread:9:tokens:out",
entityMetadata: JSON.stringify({ model: "gpt-4o" }),
properties: {
session: "org:42:thread:9",
channel: "tokens",
io: "out",
},
})
).toEqual({
sessionId: "org:42:thread:9",
channel: "tokens",
io: "out",
metadata: { model: "gpt-4o" },
});
});

it("does not include the runId prefix in sessionId for in spans", () => {
expect(
extractSessionStreamEntity({
entityId: "run_abc123:clerk:user_123:v2:approvals:in",
properties: {
session: "clerk:user_123:v2",
channel: "approvals",
io: "in",
},
})
).toEqual({
sessionId: "clerk:user_123:v2",
channel: "approvals",
io: "in",
metadata: undefined,
});
});

it("preserves session keys starting with run_ when properties are present", () => {
expect(
extractSessionStreamEntity({
entityId: "run_outer:run_custom:session:1::in",
properties: {
session: "run_custom:session:1",
io: "in",
},
})
).toEqual({
sessionId: "run_custom:session:1",
channel: undefined,
io: "in",
metadata: undefined,
});
});

it("resolves session stream fields from properties when entityId is absent", () => {
expect(
extractSessionStreamEntity({
entityId: undefined,
properties: {
session: "user:abc:def",
io: "in",
},
})
).toEqual({
sessionId: "user:abc:def",
channel: undefined,
io: "in",
metadata: undefined,
});
});
Comment on lines +76 to +91

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Properties-only test bypasses storage gate

getSpan requires an entity ID before it emits a ClickHouse span entity. The helper test does not cover what the dashboard receives when that ID is missing.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.


it("falls back to entityId parsing when properties are missing", () => {
expect(
extractSessionStreamEntity({
entityId: "clerk:user_123:v2::out",
})
).toEqual({
sessionId: "clerk:user_123:v2",
channel: undefined,
io: "out",
metadata: undefined,
});

expect(
extractSessionStreamEntity({
entityId: "run_abc123:clerk:user_123:v2:side:in",
})
).toEqual({
sessionId: "clerk:user_123:v2",
channel: "side",
io: "in",
metadata: undefined,
});
});

it("returns null for missing ids or invalid io directions", () => {
expect(extractSessionStreamEntity({ entityId: undefined })).toBeNull();
expect(extractSessionStreamEntity({ entityId: "sess_1::invalid" })).toBeNull();
expect(extractSessionStreamEntity({ entityId: ":channel:out" })).toBeNull();
});
});