♻️ refactor(bot): migrate Bot service to Agent Runtime Hooks framework#13546
♻️ refactor(bot): migrate Bot service to Agent Runtime Hooks framework#13546
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@rdmclin2 @nekomeowww - This refactors the Bot service to use the unified Agent Runtime Hooks framework, with database and backend API changes. Please coordinate review. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## canary #13546 +/- ##
===========================================
- Coverage 86.00% 66.71% -19.30%
===========================================
Files 609 1996 +1387
Lines 49045 165500 +116455
Branches 7596 16832 +9236
===========================================
+ Hits 42179 110406 +68227
- Misses 6742 54970 +48228
Partials 124 124
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
955d90d to
6d9968e
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 955d90d744
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const reason = this.determineCompletionReason(agentState); | ||
|
|
||
| // Trigger completion callback so eval run can finalize properly | ||
| if (callbacks?.onComplete) { | ||
| try { | ||
| await callbacks.onComplete({ | ||
| finalState: agentState, | ||
| operationId, | ||
| reason, | ||
| }); | ||
| this.unregisterStepCallbacks(operationId); | ||
| } catch (callbackError) { | ||
| log('[%s] onComplete callback error: %O', operationId, callbackError); | ||
| } | ||
| } | ||
|
|
||
| return { |
There was a problem hiding this comment.
Dispatch completion hooks before early terminal return
In executeStep, the early-terminal branch (interrupted/done/error) returns immediately after computing reason and never calls dispatchCompletionHooks. This path is reachable when an operation is interrupted between queued steps (for example via /stop), so onComplete hooks/webhooks are skipped entirely. That breaks consumers that finalize on onComplete (e.g., bot local-mode promise resolution in executeWithHooksLocalMode) and can leave executions hanging until timeout or miss completion-side cleanup.
Useful? React with 👍 / 👎.
Migrate the last consumer (Bot/AgentBridgeService) from legacy completionWebhook/stepWebhook/stepCallbacks dual-track pattern to the unified hooks API. This completes LOBE-6208 Step 4. - Enrich AgentHookEvent with step presentation + tracking data - Enrich afterStep hook dispatch with full step context - Merge executeWithWebhooks + executeWithInMemoryCallbacks into unified hooks - Remove legacy triggerCompletionWebhook, triggerStepWebhook, stepCallbacks - Remove completionWebhook/stepWebhook/webhookDelivery from params LOBE-6675 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…fix totalToolCalls lag - Add dispatchCompletionHooks in early-terminal branch of executeStep so onComplete hooks fire when operation is already interrupted/done/error between queued steps (e.g., via /stop) - Include current step's toolsCalling in afterStep totalToolCalls so consumers get an accurate cumulative count instead of lagging by one step Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Rewrite executeStep tests to use hookDispatcher spies instead of removed registerStepCallbacks/getStepCallbacks API - Rewrite completionWebhook tests to use hooks param and _hooks metadata instead of removed completionWebhook param - Delete stepLifecycleCallbacks.test.ts (tests removed API, coverage now provided by HookDispatcher.test.ts + executeStep.test.ts) - Update AgentRuntimeService.test.ts abort test to remove stepCallbacks Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Fix TS18048 errors: guard metadata access with null check in _stepTracking block - Migrate remaining registerStepCallbacks usage in AgentRuntimeService.test.ts to hookDispatcher.dispatch spies: onComplete error tests and onAfterStep tool result extraction tests Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Old tests expected execAgent to NOT be called (because APP_URL check would throw in queue mode). With hooks migration, the APP_URL check is gone (hooks use relative URLs resolved by HookDispatcher), so execAgent is now called. Update tests to verify hooks are passed correctly instead. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add tests verifying that webhook payloads from HookDispatcher (containing hookId/hookType fields) are correctly handled by BotCallbackService. This validates the critical contract between the hooks framework and the bot callback endpoint for step progress, completion, and error paths. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add integration tests that verify the full executeStep → hookDispatcher chain produces events with all fields bot consumers depend on: - afterStep event includes content, stepType, totalTokens, executionTimeMs - afterStep event includes cross-step tracking (lastLLMContent, totalToolCalls) - afterStep event includes toolsResult for tool_result phases - onComplete fires on early-terminal states (interrupted) with lastAssistantContent - All RenderStepParams-required fields are present and correctly typed These tests catch payload format regressions without needing production infrastructure (Redis, QStash, real bot platforms). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
f0a7a14 to
243f24b
Compare
Summary
AgentBridgeService) from legacycompletionWebhook/stepWebhook/stepCallbacksdual-track pattern to the unified hooks APIAgentHookEventwith step presentation data (content, toolsCalling, executionTimeMs, etc.) and cross-step tracking data (lastLLMContent, totalToolCalls, etc.)AgentRuntimeService(~260 lines deleted)Changes
hooks/types.tsAgentHookEventAgentRuntimeService.tsafterStepdispatch, extend_stepTrackingfor hooks, removetriggerCompletionWebhook/triggerStepWebhook/stepCallbacks/deliverWebhooktypes.tscompletionWebhook/stepWebhook/stepCallbacks/webhookDeliveryfromOperationCreationParamsaiAgent/index.tsInternalExecAgentParamsandexecAgentAgentBridgeService.tsexecuteWithWebhooks+executeWithInMemoryCallbacksinto unified hooks patternBotCallbackService.tshookId/hookTypefields toBotCallbackBodyTest plan
bun run type-check— no new type errorsHookDispatcher.test.ts— 19/19 tests passingCloses LOBE-6675
🤖 Generated with Claude Code