Skip to content

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

Open
vincentkoc wants to merge 1 commit intoopenclaw:mainfrom
vincentkoc:vincentkoc-code/hooks-agent-tool-lifecycle-r3
Open

feat(hooks): add agent and tool lifecycle boundaries#18889
vincentkoc wants to merge 1 commit intoopenclaw:mainfrom
vincentkoc:vincentkoc-code/hooks-agent-tool-lifecycle-r3

Conversation

@vincentkoc
Copy link
Member

@vincentkoc vincentkoc commented Feb 17, 2026

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 #12583 which was originally split from closed umbrella PR #9761: #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

@vincentkoc vincentkoc force-pushed the vincentkoc-code/hooks-agent-tool-lifecycle-r3 branch from d48824c to d942194 Compare March 8, 2026 04:58
@aisle-research-bot
Copy link

aisle-research-bot bot commented Mar 8, 2026

🤖 We're reviewing this PR with Aisle

We're running a security check on the changes in this PR now. This usually takes a few minutes. ⌛
We'll post the results here as soon as they're ready.

Progress:

  • Analysis
  • Triage
  • Finalization

Latest run failed. Keeping previous successful results. Trace ID: 019ccbcfa3e5886d0749b81d7876a579.

Last updated on: 2026-03-08T04:58:46Z

@openclaw-barnacle openclaw-barnacle bot added size: M maintainer Maintainer-authored PR and removed docs Improvements or additions to documentation size: L labels Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR size: M

Projects

None yet

1 participant