@@ -28,15 +28,15 @@ import type { WebSocket } from 'ws'
2828const sandboxManager = new SandboxManager ( )
2929
3030// Maintains generator state for all agents. Generator state can't be serialized, so we store it in memory.
31- const agentIdToGenerator : Record < string , StepGenerator | undefined > = { }
32- export const agentIdToStepAll : Set < string > = new Set ( )
31+ const runIdToGenerator : Record < string , StepGenerator | undefined > = { }
32+ export const runIdToStepAll : Set < string > = new Set ( )
3333
3434// Function to clear the generator cache for testing purposes
3535export function clearAgentGeneratorCache ( ) {
36- for ( const key in agentIdToGenerator ) {
37- delete agentIdToGenerator [ key ]
36+ for ( const key in runIdToGenerator ) {
37+ delete runIdToGenerator [ key ]
3838 }
39- agentIdToStepAll . clear ( )
39+ runIdToStepAll . clear ( )
4040 // Clean up QuickJS sandboxes
4141 sandboxManager . dispose ( )
4242}
@@ -78,16 +78,20 @@ export async function runProgrammaticStep(
7878 throw new Error ( 'No step handler found for agent template ' + template . id )
7979 }
8080
81+ if ( ! agentState . runId ) {
82+ throw new Error ( 'Agent state has no run ID' )
83+ }
84+
8185 // Run with either a generator or a sandbox.
82- let generator = agentIdToGenerator [ agentState . agentId ]
83- let sandbox = sandboxManager . getSandbox ( agentState . agentId )
86+ let generator = runIdToGenerator [ agentState . runId ]
87+ let sandbox = sandboxManager . getSandbox ( agentState . runId )
8488
8589 // Check if we need to initialize a generator (either native or QuickJS-based)
8690 if ( ! generator && ! sandbox ) {
8791 if ( typeof template . handleSteps === 'string' ) {
8892 // Initialize QuickJS sandbox for string-based generator
8993 sandbox = await sandboxManager . getOrCreateSandbox (
90- agentState . agentId ,
94+ agentState . runId ,
9195 template . handleSteps ,
9296 {
9397 agentState,
@@ -102,15 +106,15 @@ export async function runProgrammaticStep(
102106 prompt,
103107 params,
104108 } )
105- agentIdToGenerator [ agentState . agentId ] = generator
109+ runIdToGenerator [ agentState . runId ] = generator
106110 }
107111 }
108112
109113 // Check if we're in STEP_ALL mode
110- if ( agentIdToStepAll . has ( agentState . agentId ) ) {
114+ if ( runIdToStepAll . has ( agentState . runId ) ) {
111115 if ( stepsComplete ) {
112116 // Clear the STEP_ALL mode. Stepping can continue if handleSteps doesn't return.
113- agentIdToStepAll . delete ( agentState . agentId )
117+ runIdToStepAll . delete ( agentState . runId )
114118 } else {
115119 return { agentState, endTurn : false , stepNumber }
116120 }
@@ -143,7 +147,9 @@ export async function runProgrammaticStep(
143147 ...data ,
144148 } )
145149 } ,
146- agentState : cloneDeep ( agentState ) ,
150+ agentState : cloneDeep (
151+ agentState as AgentState & Required < Pick < AgentState , 'runId' > > ,
152+ ) ,
147153 agentContext : cloneDeep ( agentState . agentContext ) ,
148154 messages : cloneDeep ( agentState . messageHistory ) ,
149155 }
@@ -182,7 +188,7 @@ export async function runProgrammaticStep(
182188 break
183189 }
184190 if ( result . value === 'STEP_ALL' ) {
185- agentIdToStepAll . add ( state . agentState . agentId )
191+ runIdToStepAll . add ( state . agentState . agentId )
186192 break
187193 }
188194
@@ -322,20 +328,21 @@ export async function runProgrammaticStep(
322328 if ( endTurn ) {
323329 if ( sandbox ) {
324330 // Clean up QuickJS sandbox if execution is complete
325- sandboxManager . removeSandbox ( agentState . agentId )
331+ sandboxManager . removeSandbox ( agentState . runId )
326332 }
327- delete agentIdToGenerator [ agentState . agentId ]
328- agentIdToStepAll . delete ( agentState . agentId )
333+ delete runIdToGenerator [ agentState . runId ]
334+ runIdToStepAll . delete ( agentState . runId )
329335 }
330336 }
331337}
332338
333339export const getPublicAgentState = (
334- agentState : AgentState ,
340+ agentState : AgentState & Required < Pick < AgentState , 'runId' > > ,
335341) : PublicAgentState => {
336- const { agentId, parentId, messageHistory, output } = agentState
342+ const { agentId, runId , parentId, messageHistory, output } = agentState
337343 return {
338344 agentId,
345+ runId,
339346 parentId,
340347 messageHistory : messageHistory as any as PublicAgentState [ 'messageHistory' ] ,
341348 output,
0 commit comments