Skip to content

feat(hooks): add agent and tool lifecycle boundaries#13

Open
BingqingLyu wants to merge 1 commit into
mainfrom
fork-pr-18889-vincentkoc-code-hooks-agent-tool-lifecycle-r3
Open

feat(hooks): add agent and tool lifecycle boundaries#13
BingqingLyu wants to merge 1 commit into
mainfrom
fork-pr-18889-vincentkoc-code-hooks-agent-tool-lifecycle-r3

Conversation

@BingqingLyu

@BingqingLyu BingqingLyu commented Apr 27, 2026

Copy link
Copy Markdown
Owner

Why

Agent execution lacked complete lifecycle boundaries around thinking, response generation, and tool execution, reducing traceability for observability and policy systems. This PR adds those boundaries and ensures tool lifecycle hooks carry toolCallId context when available. lobster-biscuit

Split Context

This PR was accidentally closed after a messed up rebase openclaw#12583 which was originally split from closed umbrella PR openclaw#9761: openclaw#9761.

Detailed Changes

  • Added internal agent lifecycle hook emissions:
    • agent:thinking:start / agent:thinking:end
    • agent:response:start / agent:response:end
    • agent:tool:start / agent:tool:end
  • Added runAfterToolCallHook wiring for tool execution lifecycle completion
  • Propagated toolCallId through before/after tool hook contexts where available
  • Extended client tool definition adapter to pass toolCallId and run after-tool hook flow
  • Added focused tests for agent lifecycle emissions and tool lifecycle behavior

Related Links, Issues and Resolution

Greptile Summary

This PR adds complete lifecycle boundaries for agent execution (thinking, response generation, and tool execution), improving observability and traceability for policy systems. The implementation propagates toolCallId through tool lifecycle hooks and ensures consistent hook firing across different execution paths.

Key improvements:

  • Split lifecycle hook logic into dedicated lifecycle-hooks.ts module
  • Ensured agent:thinking:start/end fire for all agent runs
  • Added conditional agent:response:start/end hooks that only fire when response output is generated
  • Propagated toolCallId to before_tool_call and after_tool_call hooks
  • Added runAfterToolCallHook wiring for both success and error paths in tool execution
  • Comprehensive test coverage validates hook emissions in different scenarios

Confidence Score: 5/5

  • Safe to merge - well-structured refactoring with comprehensive test coverage
  • The changes are well-architected with proper error handling throughout. The new lifecycle-hooks.ts module cleanly separates concerns, all hook emission paths include error handling that logs warnings without failing execution, and the test coverage validates the core lifecycle hook behavior. The toolCallId propagation is handled consistently with proper fallbacks.
  • No files require special attention

Last reviewed commit: d48824c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants