@@ -238,6 +238,8 @@ export namespace Telemetry {
238238 // altimate_change start — add os/arch/node_version for environment segmentation
239239 | {
240240 type : "session_start"
241+ /** altimate_change — the turn's workspace, named by the emitter */
242+ workspace_id ?: number
241243 timestamp : number
242244 session_id : string
243245 model_id : string
@@ -668,6 +670,8 @@ export namespace Telemetry {
668670 }
669671 | {
670672 type : "memory_injection"
673+ /** altimate_change — the turn's workspace, named by the emitter */
674+ workspace_id ?: number
671675 timestamp : number
672676 session_id : string
673677 block_count : number
@@ -740,6 +744,8 @@ export namespace Telemetry {
740744 }
741745 | {
742746 type : "skill_used"
747+ /** altimate_change — the turn's workspace, named by the emitter */
748+ workspace_id ?: number
743749 timestamp : number
744750 session_id : string
745751 message_id : string
@@ -851,6 +857,8 @@ export namespace Telemetry {
851857 // altimate_change start — plan refinement telemetry event
852858 | {
853859 type : "plan_revision"
860+ /** altimate_change — the turn's workspace, named by the emitter */
861+ workspace_id ?: number
854862 timestamp : number
855863 session_id : string
856864 revision_number : number
@@ -879,6 +887,8 @@ export namespace Telemetry {
879887 // altimate_change end
880888 | {
881889 type : "core_failure"
890+ /** altimate_change — the turn's workspace, named by the emitter */
891+ workspace_id ?: number
882892 timestamp : number
883893 session_id : string
884894 tool_name : string
@@ -933,6 +943,8 @@ export namespace Telemetry {
933943 // altimate_change start — sql quality telemetry for issue prevention metrics
934944 | {
935945 type : "sql_quality"
946+ /** altimate_change — the turn's workspace, named by the emitter */
947+ workspace_id ?: number
936948 timestamp : number
937949 session_id : string
938950 tool_name : string
@@ -967,6 +979,8 @@ export namespace Telemetry {
967979 // task intent classification for understanding DE problem distribution
968980 | {
969981 type : "task_classified"
982+ /** altimate_change — the turn's workspace, named by the emitter */
983+ workspace_id ?: number
970984 timestamp : number
971985 session_id : string
972986 /** Classified intent category */
@@ -1253,6 +1267,8 @@ export namespace Telemetry {
12531267 | {
12541268 /** derived — see activation_job_selected */
12551269 type : "first_job_completed"
1270+ /** altimate_change — the turn's workspace, named by the emitter */
1271+ workspace_id ?: number
12561272 timestamp : number
12571273 session_id : string
12581274 job : "sample_duck_db" | "breaks_downstream" | "sql_review" | "cost" | "something_else"
@@ -1275,6 +1291,8 @@ export namespace Telemetry {
12751291 }
12761292 | {
12771293 type : "first_prompt_sent"
1294+ /** altimate_change — the turn's workspace, named by the emitter */
1295+ workspace_id ?: number
12781296 timestamp : number
12791297 session_id : string
12801298 }
@@ -1968,21 +1986,18 @@ export namespace Telemetry {
19681986 let sessionId = ""
19691987 let projectId = ""
19701988 // altimate_change — the workspace the current session's project is linked to (the backend
1971- // datamate id), set per prompt turn from the local binding cache; stamped on every envelope so
1972- // the session, first-use, skill, memory, tool and outcome rows join the workspace without each
1973- // emit site knowing about it.
1989+ // datamate id), set per prompt turn from the local binding cache and read back through
1990+ // `getContext()` by callers that ask for it; nothing is stamped from it (see `track`).
19741991 let workspaceId : number | undefined
1975- // altimate_change start — the workspace of every session that set the context, by session: a serve
1976- // process runs sessions side by side, so the singleton above can belong to another session's turn
1977- // when an event is tracked. An entry lives from the session's turn start to its end (endSession);
1978- // the insertion-ordered cap only matters past that many sessions mid-turn at once.
1979- const sessionWorkspaces = new Map < string , number | undefined > ( )
1980- const MAX_SESSION_WORKSPACES = 1024
1981- /** The current turn token per session: `setContext` issues one, `endSession` ends only its own, so a
1982- * cancelled turn unwinding late cannot end the replacement's entries. Tokens come from one
1983- * process-wide counter, so an entry evicted and re-created never reuses a token a stale turn holds. */
1992+ // altimate_change start — the current turn token per session: `setContext` issues one, `endSession`
1993+ // ends only its own, so a cancelled turn unwinding late cannot end the replacement's counters.
1994+ // Tokens come from one process-wide counter, so a token is never reused. The map holds sessions
1995+ // mid-turn; an entry goes at the turn's end.
19841996 const sessionTurns = new Map < string , number > ( )
19851997 let nextTurn = 0
1998+ /** The workspace of each session's current turn, for an emitter that runs inside the turn but
1999+ * outside the loop (a plugin hook) and asks for it explicitly; nothing is stamped from it. */
2000+ const turnWorkspaces = new Map < string , number | undefined > ( )
19862001 // altimate_change end
19872002 let appInsights : AppInsightsConfig | undefined
19882003 let droppedEvents = 0
@@ -2281,22 +2296,19 @@ export namespace Telemetry {
22812296 export function clearWorkspaceUse ( sessionId : string , turn ?: number ) {
22822297 workspaceUse . delete ( useKey ( sessionId , turn ) )
22832298 }
2284- /** The workspace a session's turn bound, or undefined when unbound or unknown. */
2285- export function workspaceOf ( sessionId : string ) : number | undefined {
2286- return sessionWorkspaces . get ( sessionId )
2287- }
2288- /** A session's turn ended: its use counts are read out and its `session_end` tracked by then, and
2289- * its workspace entry goes. With `turn` (from `setContext`), a turn that is no longer the session's
2290- * current one ends nothing: the replacement's entries are the replacement's. */
2299+ /** A session's turn ended: its use counts are read out and its `session_end` tracked by then. With
2300+ * `turn` (from `setContext`), a turn that is no longer the session's current one ends only its own
2301+ * counts: the replacement's are the replacement's. */
22912302 export function endSession ( sessionId : string , turn ?: number ) {
2292- if ( turn !== undefined && sessionTurns . get ( sessionId ) !== turn ) {
2293- // A turn that was replaced ends only its own counts; the replacement's entries stay.
2294- workspaceUse . delete ( useKey ( sessionId , turn ) )
2295- return
2296- }
22972303 workspaceUse . delete ( useKey ( sessionId , turn ) )
2298- sessionWorkspaces . delete ( sessionId )
2304+ if ( turn !== undefined && sessionTurns . get ( sessionId ) !== turn ) return
22992305 sessionTurns . delete ( sessionId )
2306+ turnWorkspaces . delete ( sessionId )
2307+ }
2308+ /** The workspace of the session's current turn, for an emitter inside that turn; undefined when
2309+ * unbound or when no turn is running. An explicit lookup, never a stamp. */
2310+ export function turnWorkspaceOf ( sessionId : string ) : number | undefined {
2311+ return turnWorkspaces . get ( sessionId )
23002312 }
23012313 // altimate_change end
23022314
@@ -2305,15 +2317,9 @@ export namespace Telemetry {
23052317 sessionId = opts . sessionId
23062318 projectId = opts . projectId
23072319 workspaceId = opts . workspaceId
2308- // altimate_change start — remembered per session for the stamping in track()
2320+ // altimate_change start — a turn token for the session's counters and its end
23092321 if ( ! opts . sessionId ) return 0
2310- sessionWorkspaces . delete ( opts . sessionId )
2311- sessionWorkspaces . set ( opts . sessionId , opts . workspaceId )
2312- if ( sessionWorkspaces . size > MAX_SESSION_WORKSPACES ) {
2313- const oldest = sessionWorkspaces . keys ( ) . next ( ) . value !
2314- sessionWorkspaces . delete ( oldest )
2315- sessionTurns . delete ( oldest )
2316- }
2322+ turnWorkspaces . set ( opts . sessionId , opts . workspaceId )
23172323 const turn = ++ nextTurn
23182324 sessionTurns . set ( opts . sessionId , turn )
23192325 return turn
@@ -2471,16 +2477,10 @@ export namespace Telemetry {
24712477 const operationId = typeof eventSession === "string" ? TraceContext . activeTraceId ( eventSession ) : undefined
24722478 if ( operationId ) ( event as any ) . _operation_id = operationId
24732479 // altimate_change end
2474- // altimate_change start — the bound workspace is read now, not at flush, and for the event's own
2475- // session: a serve process runs sessions side by side, so the process-wide context can belong to
2476- // another session's turn. A row with no session takes the current context; a row naming a session
2477- // that never set the context gains no key rather than another session's.
2478- if ( ! ( "workspace_id" in event ) ) {
2479- const own =
2480- typeof eventSession === "string" && eventSession !== "" ? sessionWorkspaces . get ( eventSession ) : workspaceId
2481- if ( own !== undefined ) ( event as any ) . workspace_id = own
2482- }
2483- // altimate_change end
2480+ // altimate_change — no ambient workspace stamping: a row names its workspace itself (the prompt
2481+ // loop's rows, tool rows and memory rows carry the turn's; workspace rows carry their own) or it
2482+ // carries none. A process-wide or per-session fallback kept attributing cancelled turns' rows to
2483+ // their replacements.
24842484 buffer . push ( event )
24852485 if ( buffer . length > MAX_BUFFER_SIZE ) {
24862486 buffer . shift ( )
0 commit comments