mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 18:10:22 +00:00
* 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>
112 lines
3.8 KiB
TypeScript
112 lines
3.8 KiB
TypeScript
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
|
import { Type } from "@sinclair/typebox";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { toToolDefinitions } from "./pi-tool-definition-adapter.js";
|
|
|
|
const hookMocks = vi.hoisted(() => ({
|
|
runner: {
|
|
hasHooks: vi.fn((_: string) => true),
|
|
runAfterToolCall: vi.fn(async () => {}),
|
|
},
|
|
isToolWrappedWithBeforeToolCallHook: vi.fn(() => false),
|
|
consumeAdjustedParamsForToolCall: vi.fn((_: string) => undefined as unknown),
|
|
runBeforeToolCallHook: vi.fn(async ({ params }: { params: unknown }) => ({
|
|
blocked: false,
|
|
params,
|
|
})),
|
|
}));
|
|
|
|
vi.mock("../plugins/hook-runner-global.js", () => ({
|
|
getGlobalHookRunner: () => hookMocks.runner,
|
|
}));
|
|
|
|
vi.mock("./pi-tools.before-tool-call.js", () => ({
|
|
consumeAdjustedParamsForToolCall: hookMocks.consumeAdjustedParamsForToolCall,
|
|
isToolWrappedWithBeforeToolCallHook: hookMocks.isToolWrappedWithBeforeToolCallHook,
|
|
runBeforeToolCallHook: hookMocks.runBeforeToolCallHook,
|
|
}));
|
|
|
|
function createReadTool() {
|
|
return {
|
|
name: "read",
|
|
label: "Read",
|
|
description: "reads",
|
|
parameters: Type.Object({}),
|
|
execute: vi.fn(async () => ({ content: [], details: { ok: true } })),
|
|
} satisfies AgentTool;
|
|
}
|
|
|
|
type ToolExecute = ReturnType<typeof toToolDefinitions>[number]["execute"];
|
|
const extensionContext = {} as Parameters<ToolExecute>[4];
|
|
|
|
describe("pi tool definition adapter after_tool_call", () => {
|
|
beforeEach(() => {
|
|
hookMocks.runner.hasHooks.mockClear();
|
|
hookMocks.runner.runAfterToolCall.mockClear();
|
|
hookMocks.runner.runAfterToolCall.mockResolvedValue(undefined);
|
|
hookMocks.isToolWrappedWithBeforeToolCallHook.mockClear();
|
|
hookMocks.isToolWrappedWithBeforeToolCallHook.mockReturnValue(false);
|
|
hookMocks.consumeAdjustedParamsForToolCall.mockClear();
|
|
hookMocks.consumeAdjustedParamsForToolCall.mockReturnValue(undefined);
|
|
hookMocks.runBeforeToolCallHook.mockClear();
|
|
hookMocks.runBeforeToolCallHook.mockImplementation(async ({ params }) => ({
|
|
blocked: false,
|
|
params,
|
|
}));
|
|
});
|
|
|
|
// Regression guard: after_tool_call is handled exclusively by
|
|
// handleToolExecutionEnd in the subscription handler to prevent
|
|
// duplicate invocations in embedded runs.
|
|
it("does not fire after_tool_call from the adapter (handled by subscription handler)", async () => {
|
|
const defs = toToolDefinitions([createReadTool()]);
|
|
const def = defs[0];
|
|
if (!def) {
|
|
throw new Error("missing tool definition");
|
|
}
|
|
await def.execute("call-ok", { path: "/tmp/file" }, undefined, undefined, extensionContext);
|
|
|
|
expect(hookMocks.runner.runAfterToolCall).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not fire after_tool_call from the adapter on error", async () => {
|
|
const tool = {
|
|
name: "bash",
|
|
label: "Bash",
|
|
description: "throws",
|
|
parameters: Type.Object({}),
|
|
execute: vi.fn(async () => {
|
|
throw new Error("boom");
|
|
}),
|
|
} satisfies AgentTool;
|
|
|
|
const defs = toToolDefinitions([tool]);
|
|
const def = defs[0];
|
|
if (!def) {
|
|
throw new Error("missing tool definition");
|
|
}
|
|
await def.execute("call-err", { cmd: "ls" }, undefined, undefined, extensionContext);
|
|
|
|
expect(hookMocks.runner.runAfterToolCall).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not consume adjusted params in adapter for wrapped tools", async () => {
|
|
hookMocks.isToolWrappedWithBeforeToolCallHook.mockReturnValue(true);
|
|
const defs = toToolDefinitions([createReadTool()]);
|
|
const def = defs[0];
|
|
if (!def) {
|
|
throw new Error("missing tool definition");
|
|
}
|
|
await def.execute(
|
|
"call-wrapped",
|
|
{ path: "/tmp/file" },
|
|
undefined,
|
|
undefined,
|
|
extensionContext,
|
|
);
|
|
|
|
expect(hookMocks.runBeforeToolCallHook).not.toHaveBeenCalled();
|
|
expect(hookMocks.consumeAdjustedParamsForToolCall).not.toHaveBeenCalled();
|
|
});
|
|
});
|