Skip to content

Commit 824ba82

Browse files
committed
fix(webapp): preserve colons in session-stream span session keys (#5160)
1 parent 07946c7 commit 824ba82

3 files changed

Lines changed: 212 additions & 27 deletions

File tree

‎apps/webapp/app/presenters/v3/SpanPresenter.server.ts‎

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
startedSessionExternalId,
6363
traceRootRunId,
6464
} from "./startedSession.server";
65+
import { extractSessionStreamEntity } from "./sessionStreamEntity";
6566

6667
export type PromptSpanData = {
6768
slug: string;
@@ -950,41 +951,30 @@ export class SpanPresenter extends BasePresenter {
950951
};
951952
}
952953
case "session-stream": {
953-
if (!span.entity.id) {
954-
logger.error(`SpanPresenter: No session stream id`, {
955-
spanId,
956-
sessionStreamId: span.entity.id,
957-
});
958-
return { ...data, entity: null };
959-
}
960-
961-
const parts = span.entity.id.split(":");
962-
const io = parts.at(-1);
963-
const channel = parts.at(-2) ?? "";
964-
const sessionId = parts.at(-3);
954+
const sessionStream = extractSessionStreamEntity({
955+
entityId: span.entity.id,
956+
entityMetadata: span.entity.metadata,
957+
properties: span.properties,
958+
});
965959

966-
if (!sessionId || (io !== "out" && io !== "in")) {
967-
logger.error(`SpanPresenter: Invalid session stream id`, {
968-
spanId,
969-
sessionStreamId: span.entity.id,
970-
});
960+
if (!sessionStream) {
961+
logger.error(
962+
span.entity.id
963+
? `SpanPresenter: Invalid session stream id`
964+
: `SpanPresenter: No session stream id`,
965+
{
966+
spanId,
967+
sessionStreamId: span.entity.id,
968+
}
969+
);
971970
return { ...data, entity: null };
972971
}
973972

974-
const metadata = span.entity.metadata
975-
? (safeJsonParse(span.entity.metadata) as Record<string, unknown> | undefined)
976-
: undefined;
977-
978973
return {
979974
...data,
980975
entity: {
981976
type: "session-stream" as const,
982-
object: {
983-
sessionId,
984-
channel: channel.length > 0 ? channel : undefined,
985-
io,
986-
metadata,
987-
},
977+
object: sessionStream,
988978
},
989979
};
990980
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { safeJsonParse } from "~/utils/json";
2+
3+
type SessionStreamEntityObject = {
4+
sessionId: string;
5+
channel: string | undefined;
6+
io: "out" | "in";
7+
metadata: Record<string, unknown> | undefined;
8+
};
9+
10+
/**
11+
* Extracts `session-stream` span entity fields.
12+
*
13+
* Prefers unsplit `session`, `channel`, and `io` span properties because
14+
* session keys may contain `:` (e.g. `clerk:user_123:v2`) and `"in"` stream
15+
* entity IDs carry a leading `runId:` prefix.
16+
*/
17+
export function extractSessionStreamEntity({
18+
entityId,
19+
entityMetadata,
20+
properties,
21+
}: {
22+
entityId?: string | null;
23+
entityMetadata?: string | null;
24+
properties?: unknown;
25+
}): SessionStreamEntityObject | null {
26+
const props =
27+
properties && typeof properties === "object"
28+
? (properties as Record<string, unknown>)
29+
: undefined;
30+
31+
const propSessionId =
32+
typeof props?.session === "string" && props.session.length > 0 ? props.session : undefined;
33+
const propIo = props?.io === "out" || props?.io === "in" ? props.io : undefined;
34+
const propChannel =
35+
typeof props?.channel === "string" && props.channel.length > 0 ? props.channel : undefined;
36+
37+
let sessionId = propSessionId;
38+
let io: string | undefined = propIo;
39+
let channel = propChannel;
40+
41+
if (!sessionId || !io) {
42+
if (!entityId) {
43+
return null;
44+
}
45+
46+
const parts = entityId.split(":");
47+
const parsedIo = parts.at(-1);
48+
const parsedChannel = parts.at(-2) ?? "";
49+
const parsedSessionId =
50+
parsedIo === "in" && parts.length >= 4 && parts[0]?.startsWith("run_")
51+
? parts.slice(1, -2).join(":")
52+
: parts.slice(0, -2).join(":");
53+
54+
sessionId = sessionId ?? (parsedSessionId.length > 0 ? parsedSessionId : undefined);
55+
io = io ?? parsedIo;
56+
channel = channel ?? (parsedChannel.length > 0 ? parsedChannel : undefined);
57+
}
58+
59+
if (!sessionId || (io !== "out" && io !== "in")) {
60+
return null;
61+
}
62+
63+
const metadata = entityMetadata
64+
? (safeJsonParse(entityMetadata) as Record<string, unknown> | undefined)
65+
: undefined;
66+
67+
return {
68+
sessionId,
69+
channel,
70+
io,
71+
metadata,
72+
};
73+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { describe, expect, it } from "vitest";
2+
import { extractSessionStreamEntity } from "~/presenters/v3/sessionStreamEntity";
3+
4+
describe("extractSessionStreamEntity", () => {
5+
it("preserves colons in session keys for out spans using properties", () => {
6+
expect(
7+
extractSessionStreamEntity({
8+
entityId: "clerk:user_123:v2::out",
9+
properties: {
10+
session: "clerk:user_123:v2",
11+
io: "out",
12+
},
13+
})
14+
).toEqual({
15+
sessionId: "clerk:user_123:v2",
16+
channel: undefined,
17+
io: "out",
18+
metadata: undefined,
19+
});
20+
});
21+
22+
it("extracts channel and metadata when session key contains colons", () => {
23+
expect(
24+
extractSessionStreamEntity({
25+
entityId: "org:42:thread:9:tokens:out",
26+
entityMetadata: JSON.stringify({ model: "gpt-4o" }),
27+
properties: {
28+
session: "org:42:thread:9",
29+
channel: "tokens",
30+
io: "out",
31+
},
32+
})
33+
).toEqual({
34+
sessionId: "org:42:thread:9",
35+
channel: "tokens",
36+
io: "out",
37+
metadata: { model: "gpt-4o" },
38+
});
39+
});
40+
41+
it("does not include the runId prefix in sessionId for in spans", () => {
42+
expect(
43+
extractSessionStreamEntity({
44+
entityId: "run_abc123:clerk:user_123:v2:approvals:in",
45+
properties: {
46+
session: "clerk:user_123:v2",
47+
channel: "approvals",
48+
io: "in",
49+
},
50+
})
51+
).toEqual({
52+
sessionId: "clerk:user_123:v2",
53+
channel: "approvals",
54+
io: "in",
55+
metadata: undefined,
56+
});
57+
});
58+
59+
it("preserves session keys starting with run_ when properties are present", () => {
60+
expect(
61+
extractSessionStreamEntity({
62+
entityId: "run_outer:run_custom:session:1::in",
63+
properties: {
64+
session: "run_custom:session:1",
65+
io: "in",
66+
},
67+
})
68+
).toEqual({
69+
sessionId: "run_custom:session:1",
70+
channel: undefined,
71+
io: "in",
72+
metadata: undefined,
73+
});
74+
});
75+
76+
it("resolves session stream fields from properties when entityId is absent", () => {
77+
expect(
78+
extractSessionStreamEntity({
79+
entityId: undefined,
80+
properties: {
81+
session: "user:abc:def",
82+
io: "in",
83+
},
84+
})
85+
).toEqual({
86+
sessionId: "user:abc:def",
87+
channel: undefined,
88+
io: "in",
89+
metadata: undefined,
90+
});
91+
});
92+
93+
it("falls back to entityId parsing when properties are missing", () => {
94+
expect(
95+
extractSessionStreamEntity({
96+
entityId: "clerk:user_123:v2::out",
97+
})
98+
).toEqual({
99+
sessionId: "clerk:user_123:v2",
100+
channel: undefined,
101+
io: "out",
102+
metadata: undefined,
103+
});
104+
105+
expect(
106+
extractSessionStreamEntity({
107+
entityId: "run_abc123:clerk:user_123:v2:side:in",
108+
})
109+
).toEqual({
110+
sessionId: "clerk:user_123:v2",
111+
channel: "side",
112+
io: "in",
113+
metadata: undefined,
114+
});
115+
});
116+
117+
it("returns null for missing ids or invalid io directions", () => {
118+
expect(extractSessionStreamEntity({ entityId: undefined })).toBeNull();
119+
expect(extractSessionStreamEntity({ entityId: "sess_1::invalid" })).toBeNull();
120+
expect(extractSessionStreamEntity({ entityId: ":channel:out" })).toBeNull();
121+
});
122+
});

0 commit comments

Comments
 (0)