@@ -23,6 +23,15 @@ export class ExecutionAgent extends Agent<WorkerEnv, ExecutionState> {
2323 ) : Promise < { success : boolean ; result ?: unknown ; error ?: string } > {
2424 // Check if already executing to prevent concurrent runs
2525 if ( this . state . isExecuting ) {
26+ logger
27+ . withTags ( {
28+ agentId : input . agentId ,
29+ conversationId : input . conversationId ,
30+ } )
31+ . warn ( "ExecutionAgent: Attempted concurrent execution" , {
32+ taskDescription : input . taskDescription . substring ( 0 , 100 ) ,
33+ } ) ;
34+
2635 return {
2736 success : false ,
2837 error : "Agent is already executing a task" ,
@@ -37,21 +46,69 @@ export class ExecutionAgent extends Agent<WorkerEnv, ExecutionState> {
3746 conversationId : input . conversationId ,
3847 } )
3948 . info ( "ExecutionAgent: Starting task execution" , {
40- taskDescription : input . taskDescription ,
49+ taskDescription : input . taskDescription . substring ( 0 , 200 ) ,
50+ taskLength : input . taskDescription . length ,
4151 } ) ;
4252
4353 try {
4454 const db = getDb ( this . env . HYPERDRIVE . connectionString ) ;
55+
56+ logger
57+ . withTags ( {
58+ agentId : input . agentId ,
59+ conversationId : input . conversationId ,
60+ } )
61+ . info ( "ExecutionAgent: Updating agent status to active" ) ;
62+
4563 await updateAgentStatus ( db , input . agentId , "active" ) ;
4664
4765 // Get agent details from database
66+ logger
67+ . withTags ( {
68+ agentId : input . agentId ,
69+ conversationId : input . conversationId ,
70+ } )
71+ . info ( "ExecutionAgent: Fetching agent record from database" ) ;
72+
4873 const agentRecord = await db . query . agents . findFirst ( {
4974 where : ( agents , { eq } ) => eq ( agents . id , input . agentId ) ,
5075 } ) ;
5176
77+ if ( ! agentRecord ) {
78+ logger
79+ . withTags ( {
80+ agentId : input . agentId ,
81+ conversationId : input . conversationId ,
82+ } )
83+ . error ( "ExecutionAgent: Agent record not found" ) ;
84+
85+ throw new Error ( "Agent record not found" ) ;
86+ }
87+
5288 const agentName = agentRecord ?. purpose || "execution_agent" ;
5389
90+ logger
91+ . withTags ( {
92+ agentId : input . agentId ,
93+ conversationId : input . conversationId ,
94+ } )
95+ . info ( "ExecutionAgent: Agent record retrieved" , {
96+ agentName,
97+ purpose : agentRecord . purpose ,
98+ status : agentRecord . status ,
99+ } ) ;
100+
54101 // Create agentic loop with tools
102+ logger
103+ . withTags ( {
104+ agentId : input . agentId ,
105+ conversationId : input . conversationId ,
106+ } )
107+ . info ( "ExecutionAgent: Creating ToolLoopAgent" , {
108+ availableTools : [ "research" , "wait" ] ,
109+ maxSteps : 20 ,
110+ } ) ;
111+
55112 const agent = new ToolLoopAgent ( {
56113 model : gemini25 ( this . env . OPENROUTER_API_KEY ) ,
57114 instructions : `You are the assistant of Poke by the Interaction Company of California. You are the "execution engine" of Poke, helping complete tasks for Poke, while Poke talks to the user. Your job is to execute and accomplish a goal, and you do not have direct access to the user.
@@ -89,6 +146,13 @@ ${input.taskDescription}`,
89146 stopWhen : stepCountIs ( 20 ) ,
90147 } ) ;
91148
149+ logger
150+ . withTags ( {
151+ agentId : input . agentId ,
152+ conversationId : input . conversationId ,
153+ } )
154+ . info ( "ExecutionAgent: Starting agent generation" ) ;
155+
92156 const result = await agent . generate ( {
93157 messages : [
94158 {
@@ -98,13 +162,33 @@ ${input.taskDescription}`,
98162 ] ,
99163 } ) ;
100164
165+ logger
166+ . withTags ( {
167+ agentId : input . agentId ,
168+ conversationId : input . conversationId ,
169+ } )
170+ . info ( "ExecutionAgent: Agent generation completed" , {
171+ stepsExecuted : result . steps ?. length || 0 ,
172+ outputLength : result . text ?. length || 0 ,
173+ usage : result . usage ,
174+ } ) ;
175+
101176 // Extract the final result
102177 const finalResult = {
103178 output : result . text ,
104179 usage : result . usage ,
105180 steps : result . steps ?. length || 0 ,
106181 } ;
107182
183+ logger
184+ . withTags ( {
185+ agentId : input . agentId ,
186+ conversationId : input . conversationId ,
187+ } )
188+ . info ( "ExecutionAgent: Updating agent status to completed" , {
189+ outputPreview : result . text ?. substring ( 0 , 100 ) ,
190+ } ) ;
191+
108192 await updateAgentStatus ( db , input . agentId , "completed" , {
109193 result : finalResult ,
110194 } ) ;
@@ -116,16 +200,15 @@ ${input.taskDescription}`,
116200 } )
117201 . info ( "ExecutionAgent: Task completed successfully" , {
118202 steps : finalResult . steps ,
203+ totalTokens : result . usage ?. totalTokens ,
119204 } ) ;
120205
121206 this . setState ( { isExecuting : false } ) ;
122207 return { success : true , result : finalResult } ;
123208 } catch ( error ) {
124209 const errorMessage =
125210 error instanceof Error ? error . message : String ( error ) ;
126-
127- const db = getDb ( this . env . HYPERDRIVE . connectionString ) ;
128- await updateAgentStatus ( db , input . agentId , "failed" , { errorMessage } ) ;
211+ const errorStack = error instanceof Error ? error . stack : undefined ;
129212
130213 logger
131214 . withTags ( {
@@ -134,7 +217,37 @@ ${input.taskDescription}`,
134217 } )
135218 . error ( "ExecutionAgent: Task execution failed" , {
136219 error : errorMessage ,
220+ errorType : error instanceof Error ? error . constructor . name : typeof error ,
221+ stack : errorStack ,
222+ } ) ;
223+
224+ try {
225+ const db = getDb ( this . env . HYPERDRIVE . connectionString ) ;
226+
227+ logger
228+ . withTags ( {
229+ agentId : input . agentId ,
230+ conversationId : input . conversationId ,
231+ } )
232+ . info ( "ExecutionAgent: Updating agent status to failed" ) ;
233+
234+ await updateAgentStatus ( db , input . agentId , "failed" , {
235+ errorMessage,
137236 } ) ;
237+ } catch ( statusUpdateError ) {
238+ logger
239+ . withTags ( {
240+ agentId : input . agentId ,
241+ conversationId : input . conversationId ,
242+ } )
243+ . error ( "ExecutionAgent: Failed to update agent status" , {
244+ originalError : errorMessage ,
245+ statusUpdateError :
246+ statusUpdateError instanceof Error
247+ ? statusUpdateError . message
248+ : String ( statusUpdateError ) ,
249+ } ) ;
250+ }
138251
139252 this . setState ( { isExecuting : false } ) ;
140253 return { success : false , error : errorMessage } ;
0 commit comments