@@ -2252,7 +2252,10 @@ export namespace Telemetry {
22522252 // whether any workspace context was in play. Keyed by session so a serve process with several
22532253 // sessions keeps them apart; cleared when the session ends.
22542254 export type WorkspaceUse = { skills : number ; memory : number ; integrations : number }
2255+ /** Keyed by session and turn, so a cancelled turn unwinding late and its replacement never share
2256+ * counts; a site that carries no turn counts toward the session's current one. */
22552257 const workspaceUse = new Map < string , WorkspaceUse > ( )
2258+ const useKey = ( sessionId : string , turn ?: number ) => `${ sessionId } #${ turn ?? sessionTurns . get ( sessionId ) ?? 0 } `
22562259 /** `add` accumulates; `max` records the largest value seen in the turn (memory is injected on every
22572260 * step of a turn, so a sum would multiply the blocks by the step count). */
22582261 export function noteWorkspaceUse (
@@ -2266,15 +2269,17 @@ export namespace Telemetry {
22662269 ) {
22672270 if ( ! sessionId || n <= 0 ) return
22682271 if ( turn !== undefined && sessionTurns . get ( sessionId ) !== turn ) return
2269- const use = workspaceUse . get ( sessionId ) ?? { skills : 0 , memory : 0 , integrations : 0 }
2272+ const key = useKey ( sessionId , turn )
2273+ const use = workspaceUse . get ( key ) ?? { skills : 0 , memory : 0 , integrations : 0 }
22702274 use [ kind ] = mode === "max" ? Math . max ( use [ kind ] , n ) : use [ kind ] + n
2271- workspaceUse . set ( sessionId , use )
2275+ workspaceUse . set ( key , use )
22722276 }
2273- export function workspaceUseOf ( sessionId : string ) : WorkspaceUse {
2274- return { skills : 0 , memory : 0 , integrations : 0 , ...workspaceUse . get ( sessionId ) }
2277+ /** The counts of a turn (the session's current one when no turn is given). */
2278+ export function workspaceUseOf ( sessionId : string , turn ?: number ) : WorkspaceUse {
2279+ return { skills : 0 , memory : 0 , integrations : 0 , ...workspaceUse . get ( useKey ( sessionId , turn ) ) }
22752280 }
2276- export function clearWorkspaceUse ( sessionId : string ) {
2277- workspaceUse . delete ( sessionId )
2281+ export function clearWorkspaceUse ( sessionId : string , turn ?: number ) {
2282+ workspaceUse . delete ( useKey ( sessionId , turn ) )
22782283 }
22792284 /** The workspace a session's turn bound, or undefined when unbound or unknown. */
22802285 export function workspaceOf ( sessionId : string ) : number | undefined {
@@ -2284,8 +2289,12 @@ export namespace Telemetry {
22842289 * its workspace entry goes. With `turn` (from `setContext`), a turn that is no longer the session's
22852290 * current one ends nothing: the replacement's entries are the replacement's. */
22862291 export function endSession ( sessionId : string , turn ?: number ) {
2287- if ( turn !== undefined && sessionTurns . get ( sessionId ) !== turn ) return
2288- workspaceUse . delete ( sessionId )
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+ }
2297+ workspaceUse . delete ( useKey ( sessionId , turn ) )
22892298 sessionWorkspaces . delete ( sessionId )
22902299 sessionTurns . delete ( sessionId )
22912300 }
0 commit comments