@@ -24,16 +24,21 @@ import type { HandlerContext, SessionAgentType } from "../types.ts"
2424
2525const OPENINFERENCE_SPAN_KIND = SemanticConventions . OPENINFERENCE_SPAN_KIND
2626
27+ /** Starts or refreshes the root run span for a single user turn, keyed by the user message ID. */
2728export function handleRunStarted (
29+ runID : string ,
2830 sessionID : string ,
2931 agent : string ,
3032 promptText : string ,
3133 model : string ,
3234 startTime : number ,
3335 ctx : HandlerContext ,
3436) {
37+ ctx . activeRuns . set ( sessionID , runID )
38+ ctx . pendingRuns . delete ( sessionID )
39+ if ( promptText ) setBoundedMap ( ctx . runInputs , runID , promptText )
3540 if ( ! isTraceEnabled ( "session" , ctx ) ) return
36- const existing = ctx . runSpans . get ( sessionID )
41+ const existing = ctx . runSpans . get ( runID )
3742 if ( existing ) {
3843 existing . setAttributes ( {
3944 [ AGENT_NAME ] : agent ,
@@ -72,9 +77,8 @@ export function handleRunStarted(
7277 } ,
7378 ctx . rootContext ( ) ,
7479 )
75- setBoundedMap ( ctx . runSpans , sessionID , runSpan )
76- setBoundedMap ( ctx . runSpanContexts , sessionID , runSpan . spanContext ( ) )
77- setBoundedMap ( ctx . sessionRunRoots , sessionID , sessionID )
80+ ctx . runSpans . set ( runID , runSpan )
81+ setBoundedMap ( ctx . runSpanContexts , runID , runSpan . spanContext ( ) )
7882}
7983
8084/** Increments the session counter, records start time, starts the root session span, and emits a `session.created` log event. */
@@ -89,8 +93,6 @@ export function handleSessionCreated(e: EventSessionCreated, ctx: HandlerContext
8993 setBoundedMap ( ctx . sessionTotals , sessionID , { startMs : createdAt , tokens : 0 , cost : 0 , messages : 0 , agent : "unknown" , agentType } )
9094
9195 if ( isTraceEnabled ( "session" , ctx ) && parentID ) {
92- const runRootID = ctx . sessionRunRoots . get ( parentID ) ?? parentID
93- setBoundedMap ( ctx . sessionRunRoots , sessionID , runRootID )
9496 const sessionSpan = ctx . tracer . startSpan (
9597 `${ ctx . tracePrefix } session` ,
9698 {
@@ -106,7 +108,7 @@ export function handleSessionCreated(e: EventSessionCreated, ctx: HandlerContext
106108 } ,
107109 resolveSessionTraceContext ( parentID , ctx ) ,
108110 )
109- setBoundedMap ( ctx . sessionSpans , sessionID , sessionSpan )
111+ ctx . sessionSpans . set ( sessionID , sessionSpan )
110112 setBoundedMap ( ctx . sessionSpanContexts , sessionID , sessionSpan . spanContext ( ) )
111113 }
112114
@@ -138,7 +140,7 @@ function sweepSession(sessionID: string, ctx: HandlerContext) {
138140 ctx . pendingToolSpans . delete ( key )
139141 }
140142 }
141- ctx . sessionInputs . delete ( sessionID )
143+ ctx . pendingRuns . delete ( sessionID )
142144 const msgPrefix = `${ sessionID } :`
143145 for ( const [ key , span ] of ctx . messageSpans ) {
144146 if ( key . startsWith ( msgPrefix ) ) {
@@ -192,7 +194,9 @@ export function handleSessionIdle(e: EventSessionIdle, ctx: HandlerContext) {
192194 sessionSpan . end ( )
193195 ctx . sessionSpans . delete ( sessionID )
194196 }
195- const runSpan = ctx . runSpans . get ( sessionID )
197+ const runID = ctx . activeRuns . get ( sessionID )
198+ if ( runID ) ctx . activeRuns . delete ( sessionID )
199+ const runSpan = runID ? ctx . runSpans . get ( runID ) : undefined
196200 if ( runSpan ) {
197201 if ( totals ) {
198202 runSpan . setAttributes ( {
@@ -205,7 +209,7 @@ export function handleSessionIdle(e: EventSessionIdle, ctx: HandlerContext) {
205209 }
206210 runSpan . setStatus ( { code : SpanStatusCode . OK } )
207211 runSpan . end ( )
208- ctx . runSpans . delete ( sessionID )
212+ ctx . runSpans . delete ( runID ! )
209213 }
210214
211215 ctx . emitLog ( {
@@ -252,13 +256,15 @@ export function handleSessionError(e: EventSessionError, ctx: HandlerContext) {
252256 sessionSpan . end ( )
253257 ctx . sessionSpans . delete ( rawID )
254258 }
255- const runSpan = ctx . runSpans . get ( rawID )
259+ const runID = ctx . activeRuns . get ( rawID )
260+ if ( runID ) ctx . activeRuns . delete ( rawID )
261+ const runSpan = runID ? ctx . runSpans . get ( runID ) : undefined
256262 if ( runSpan ) {
257263 if ( totals ) runSpan . setAttributes ( { [ AGENT_NAME ] : totals . agent , "agent.type" : totals . agentType } )
258264 runSpan . setStatus ( { code : SpanStatusCode . ERROR , message : error } )
259265 runSpan . setAttribute ( "error" , error )
260266 runSpan . end ( )
261- ctx . runSpans . delete ( rawID )
267+ ctx . runSpans . delete ( runID ! )
262268 }
263269 }
264270
0 commit comments