mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 02:22:55 +00:00
fix(hooks): consolidate after_tool_call context + single-fire behavior (#32201)
* fix(hooks): deduplicate after_tool_call hook in embedded runs (cherry picked from commitc129a1a74b) * fix(hooks): propagate sessionKey in after_tool_call context The after_tool_call hook in handleToolExecutionEnd was passing `sessionKey: undefined` in the ToolContext, even though the value is available on ctx.params. This broke plugins that need session context in after_tool_call handlers (e.g., for per-session audit trails or security logging). - Add `sessionKey` to the `ToolHandlerParams` Pick type - Pass `ctx.params.sessionKey` through to the hook context - Add test assertion to prevent regression Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commitb7117384fc) * fix(hooks): thread agentId through to after_tool_call hook context Follow-up to #30511 — the after_tool_call hook context was passing `agentId: undefined` because SubscribeEmbeddedPiSessionParams did not carry the agent identity. This threads sessionAgentId (resolved in attempt.ts) through the session params into the tool handler context, giving plugins accurate agent-scoped context for both before_tool_call and after_tool_call hooks. Changes: - Add `agentId?: string` to SubscribeEmbeddedPiSessionParams - Add "agentId" to ToolHandlerParams Pick type - Pass `agentId: sessionAgentId` at the subscribeEmbeddedPiSession() call site in attempt.ts - Wire ctx.params.agentId into the after_tool_call hook context - Update tests to assert agentId propagation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commitaad01edd3e) * changelog: credit after_tool_call hook contributors * Update CHANGELOG.md * agents: preserve adjusted params until tool end * agents: emit after_tool_call with adjusted args * tests: cover adjusted after_tool_call params * tests: align adapter after_tool_call expectation --------- Co-authored-by: jbeno <jim@jimbeno.net> Co-authored-by: scoootscooob <zhentongfan@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -114,7 +114,9 @@ describe("after_tool_call hook wiring", () => {
|
||||
const event = firstCall?.[0] as
|
||||
| { toolName?: string; params?: unknown; error?: unknown; durationMs?: unknown }
|
||||
| undefined;
|
||||
const context = firstCall?.[1] as { toolName?: string } | undefined;
|
||||
const context = firstCall?.[1] as
|
||||
| { toolName?: string; agentId?: string; sessionKey?: string }
|
||||
| undefined;
|
||||
expect(event).toBeDefined();
|
||||
expect(context).toBeDefined();
|
||||
if (!event || !context) {
|
||||
@@ -125,6 +127,8 @@ describe("after_tool_call hook wiring", () => {
|
||||
expect(event.error).toBeUndefined();
|
||||
expect(typeof event.durationMs).toBe("number");
|
||||
expect(context.toolName).toBe("read");
|
||||
expect(context.agentId).toBe("main");
|
||||
expect(context.sessionKey).toBe("test-session");
|
||||
});
|
||||
|
||||
it("includes error in after_tool_call event on tool failure", async () => {
|
||||
@@ -163,6 +167,10 @@ describe("after_tool_call hook wiring", () => {
|
||||
throw new Error("missing hook call payload");
|
||||
}
|
||||
expect(event.error).toBeDefined();
|
||||
|
||||
// agentId should be undefined when not provided
|
||||
const context = firstCall?.[1] as { agentId?: string } | undefined;
|
||||
expect(context?.agentId).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not call runAfterToolCall when no hooks registered", async () => {
|
||||
|
||||
Reference in New Issue
Block a user