| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- import { describe, expect, it } from "vitest";
- import {
- HookEventName,
- SessionEventType,
- createRootTraceContext,
- createSessionId,
- createToolCallId,
- createTurnId,
- type SessionEvent,
- } from "@zcode/contracts";
- import { createInMemoryHookRunner } from "../src/hooks/index.js";
- import { PermissionService, defaultPermissionConfig } from "../src/permission/service.js";
- import { createToolExecutor } from "../src/tool/executor.js";
- import { createToolRegistry } from "../src/tool/registry.js";
- import type { ToolEntry, ToolHandler } from "../src/tool/types.js";
- describe("ToolExecutor hook additional context", () => {
- it("adds PreToolUse hook context to successful model content", async () => {
- const fixture = createExecutorFixture("tool-hook-pre-context");
- fixture.registry.register(createTestToolEntry("ContextTool", async () => "tool-output"));
- const hookRunner = createInMemoryHookRunner({
- hooks: [
- {
- event: HookEventName.PreToolUse,
- matcher: "ContextTool",
- callback: async (input) => ({
- hookSpecificOutput: {
- additionalContext: "pre-tool context",
- hookEventName: input.hookEventName,
- },
- }),
- },
- ],
- });
- const executor = fixture.createExecutor(hookRunner);
- const result = await executor.execute(
- {
- id: createToolCallId("pre-context"),
- input: {},
- name: "ContextTool",
- },
- { traceContext: fixture.traceContext },
- );
- expect(result.success).toBe(true);
- expect(result.modelContent).toContain("tool-output");
- expect(result.modelContent).toContain("pre-tool context");
- });
- it("adds PreToolUse and PostToolUseFailure hook context to failed model content", async () => {
- const fixture = createExecutorFixture("tool-hook-failure-context");
- fixture.registry.register(
- createTestToolEntry("FailingContextTool", async () => {
- throw new Error("tool exploded");
- }),
- );
- const hookRunner = createInMemoryHookRunner({
- hooks: [
- {
- event: HookEventName.PreToolUse,
- matcher: "FailingContextTool",
- callback: async (input) => ({
- hookSpecificOutput: {
- additionalContext: "pre failure context",
- hookEventName: input.hookEventName,
- },
- }),
- },
- {
- event: HookEventName.PostToolUseFailure,
- matcher: "FailingContextTool",
- callback: async (input) => ({
- hookSpecificOutput: {
- additionalContext: "post failure context",
- hookEventName: input.hookEventName,
- },
- }),
- },
- ],
- });
- const executor = fixture.createExecutor(hookRunner);
- const result = await executor.execute(
- {
- id: createToolCallId("pre-failure-context"),
- input: {},
- name: "FailingContextTool",
- },
- { traceContext: fixture.traceContext },
- );
- expect(result.success).toBe(false);
- expect(result.modelContent).toContain("tool exploded");
- expect(result.modelContent).toContain("pre failure context");
- expect(result.modelContent).toContain("post failure context");
- });
- it("keeps PreToolUse context when hook-updated input fails schema validation", async () => {
- const fixture = createExecutorFixture("tool-hook-invalid-update-context");
- let handlerRan = false;
- const entry = createTestToolEntry("InvalidHookUpdateTool", async () => {
- handlerRan = true;
- return "should-not-run";
- });
- entry.inputSchema = {
- additionalProperties: false,
- properties: { value: { type: "string" } },
- required: ["value"],
- type: "object",
- };
- fixture.registry.register(entry);
- const hookRunner = createInMemoryHookRunner({
- hooks: [
- {
- event: HookEventName.PreToolUse,
- matcher: "InvalidHookUpdateTool",
- callback: async (input) => ({
- hookSpecificOutput: {
- additionalContext: "invalid update context",
- hookEventName: input.hookEventName,
- updatedInput: {},
- },
- }),
- },
- ],
- });
- const result = await fixture.createExecutor(hookRunner).execute(
- {
- id: createToolCallId("invalid-hook-update-context"),
- input: { value: "valid-before-hook" },
- name: "InvalidHookUpdateTool",
- },
- { traceContext: fixture.traceContext },
- );
- expect(result.success).toBe(false);
- expect(result.error?.message).toContain("Tool input failed inputSchema validation");
- expect(result.modelContent).toContain("invalid update context");
- expect(result.modelContent).not.toContain("InputValidationError:");
- expect(handlerRan).toBe(false);
- });
- it("routes a handler failure through the existing failure lifecycle", async () => {
- const fixture = createExecutorFixture("tool-handler-failure");
- let handlerCalls = 0;
- let postSuccessRan = false;
- let postFailureRan = false;
- fixture.registry.register(
- createTestToolEntry("HandlerFailureTool", async () => {
- handlerCalls += 1;
- return {
- result: false,
- errorCode: 7,
- message: "Tool-specific failure",
- };
- }),
- );
- const hookRunner = createInMemoryHookRunner({
- hooks: [
- {
- event: HookEventName.PreToolUse,
- matcher: "HandlerFailureTool",
- callback: async (input) => ({
- hookSpecificOutput: {
- additionalContext: "pre failure context",
- hookEventName: input.hookEventName,
- },
- }),
- },
- {
- event: HookEventName.PostToolUse,
- matcher: "HandlerFailureTool",
- callback: async () => {
- postSuccessRan = true;
- return {};
- },
- },
- {
- event: HookEventName.PostToolUseFailure,
- matcher: "HandlerFailureTool",
- callback: async (input) => {
- postFailureRan = true;
- return {
- hookSpecificOutput: {
- additionalContext: "post failure context",
- hookEventName: input.hookEventName,
- },
- };
- },
- },
- ],
- });
- const result = await fixture.createExecutor(hookRunner).execute(
- {
- id: createToolCallId("handler-failure"),
- input: {},
- name: "HandlerFailureTool",
- },
- { traceContext: fixture.traceContext },
- );
- expect(result.success).toBe(false);
- expect(result.error).toMatchObject({
- code: "7",
- message: "Tool-specific failure",
- });
- expect(result.modelContent).toContain("<tool_use_error>Tool-specific failure</tool_use_error>");
- expect(result.modelContent).toContain("pre failure context");
- expect(result.modelContent).toContain("post failure context");
- expect(handlerCalls).toBe(1);
- expect(postSuccessRan).toBe(false);
- expect(postFailureRan).toBe(true);
- expect(fixture.events.map((event) => event.type)).toEqual([
- SessionEventType.ToolCallStarted,
- SessionEventType.ToolCallError,
- ]);
- });
- it("records non-blocking PostToolUse continue false without failing the tool", async () => {
- const fixture = createExecutorFixture("tool-hook-post-continue-false");
- const events: SessionEvent[] = [];
- let handlerRan = false;
- fixture.registry.register(
- createTestToolEntry("PostContinueTool", async () => {
- handlerRan = true;
- return "tool-output";
- }),
- );
- const hookRunner = createInMemoryHookRunner({
- emitEvent: async (event) => {
- events.push(event);
- },
- hooks: [
- {
- event: HookEventName.PostToolUse,
- matcher: "PostContinueTool",
- callback: async (input) => ({
- continue: false,
- reason: "post hook requested stop",
- hookSpecificOutput: {
- hookEventName: input.hookEventName,
- },
- }),
- },
- ],
- });
- const executor = fixture.createExecutor(hookRunner);
- const result = await executor.execute(
- {
- id: createToolCallId("post-continue-false"),
- input: {},
- name: "PostContinueTool",
- },
- { traceContext: fixture.traceContext },
- );
- expect(result.success).toBe(true);
- expect(handlerRan).toBe(true);
- expect(events.map((event) => event.type)).toEqual([
- // D20:block 决策只产生一个终态事件(runner.ts:143 为 blocked ? Blocked : Completed)。
- // 此前同时断言 Completed + Blocked,会让「被拦截」在 UI 上呈现为已完成绿勾。
- SessionEventType.HookRunStarted,
- SessionEventType.HookRunBlocked,
- ]);
- });
- it("matches Task hook matcher aliases without rewriting the Agent hook input", async () => {
- const fixture = createExecutorFixture("tool-hook-task-agent-alias");
- fixture.registry.register(createTestToolEntry("Agent", async () => "agent-output"));
- let observedToolName: string | undefined;
- const hookRunner = createInMemoryHookRunner({
- hooks: [
- {
- event: HookEventName.PreToolUse,
- matcher: "Task",
- callback: async (input) => {
- observedToolName = "toolName" in input ? input.toolName : undefined;
- return {
- hookSpecificOutput: {
- additionalContext: "task matcher matched agent",
- hookEventName: input.hookEventName,
- },
- };
- },
- },
- ],
- });
- const executor = fixture.createExecutor(hookRunner);
- const result = await executor.execute(
- {
- id: createToolCallId("agent-task-matcher"),
- input: {},
- name: "Agent",
- },
- { traceContext: fixture.traceContext },
- );
- expect(result.success).toBe(true);
- expect(result.modelContent).toContain("task matcher matched agent");
- expect(observedToolName).toBe("Agent");
- });
- it("matches Agent hook matcher aliases without rewriting the Task hook input", async () => {
- const fixture = createExecutorFixture("tool-hook-agent-task-alias");
- fixture.registry.register(createTestToolEntry("Task", async () => "task-output"));
- let observedToolName: string | undefined;
- const hookRunner = createInMemoryHookRunner({
- hooks: [
- {
- event: HookEventName.PreToolUse,
- matcher: "Agent",
- callback: async (input) => {
- observedToolName = "toolName" in input ? input.toolName : undefined;
- return {
- hookSpecificOutput: {
- additionalContext: "agent matcher matched task",
- hookEventName: input.hookEventName,
- },
- };
- },
- },
- ],
- });
- const executor = fixture.createExecutor(hookRunner);
- const result = await executor.execute(
- {
- id: createToolCallId("task-agent-matcher"),
- input: {},
- name: "Task",
- },
- { traceContext: fixture.traceContext },
- );
- expect(result.success).toBe(true);
- expect(result.modelContent).toContain("agent matcher matched task");
- expect(observedToolName).toBe("Task");
- });
- });
- function createExecutorFixture(sessionName: string) {
- const sessionId = createSessionId(sessionName);
- const turnId = createTurnId("tool-hook-context-turn");
- const traceContext = createRootTraceContext({ sessionId, turnId });
- const registry = createToolRegistry();
- const events: SessionEvent[] = [];
- return {
- events,
- registry,
- traceContext,
- createExecutor(hookRunner: ReturnType<typeof createInMemoryHookRunner>) {
- return createToolExecutor({
- emitEvent: async (event) => {
- events.push(event);
- },
- hookRunner,
- permissionService: new PermissionService(defaultPermissionConfig),
- registry,
- sessionId,
- traceContext,
- turnId,
- });
- },
- };
- }
- function createTestToolEntry(name: string, handler: ToolHandler): ToolEntry {
- return {
- capability: `${name} test capability`,
- handler,
- inputSchema: {
- additionalProperties: true,
- type: "object",
- },
- metadata: {
- concurrentSafe: true,
- destructive: false,
- name,
- needsApproval: false,
- readOnly: true,
- riskLevel: "low",
- sideEffectScope: "none",
- timeoutMs: 30000,
- },
- outputSchema: {
- type: "string",
- },
- permission: {
- denyPriority: "beforeAsk",
- needsApproval: false,
- patternSources: ["none"],
- permission: "test",
- reason: "test tool",
- riskLevel: "low",
- sideEffectScope: "none",
- },
- resultBudget: {
- maxInlineBytes: 100_000,
- maxModelBytes: 100_000,
- strategy: "truncate",
- },
- };
- }
|