Repository navigation
fix(webapp): preserve colons in session-stream span session keys #5161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, Learn moreA 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 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. 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, | ||
| }; | ||
| } | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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(); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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.
Was this helpful? React with 👍 or 👎 to provide feedback.