|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + |
| 9 | + copilot "github.com/github/copilot-sdk/go" |
| 10 | + "github.com/github/copilot-sdk/go/internal/e2e/testharness" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSubagentHooksE2E(t *testing.T) { |
| 14 | + ctx := testharness.NewTestContext(t) |
| 15 | + client := ctx.NewClient(func(o *copilot.ClientOptions) { |
| 16 | + o.Env = append(o.Env, "COPILOT_EXP_COPILOT_CLI_SESSION_BASED_SUBAGENTS=true") |
| 17 | + }) |
| 18 | + t.Cleanup(func() { client.ForceStop() }) |
| 19 | + |
| 20 | + t.Run("should invoke preToolUse and postToolUse hooks for sub-agent tool calls", func(t *testing.T) { |
| 21 | + ctx.ConfigureForTest(t) |
| 22 | + |
| 23 | + type hookEntry struct { |
| 24 | + kind string |
| 25 | + toolName string |
| 26 | + sessionID string |
| 27 | + } |
| 28 | + var hookLog []hookEntry |
| 29 | + var mu sync.Mutex |
| 30 | + |
| 31 | + session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 32 | + OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 33 | + Hooks: &copilot.SessionHooks{ |
| 34 | + OnPreToolUse: func(input copilot.PreToolUseHookInput, invocation copilot.HookInvocation) (*copilot.PreToolUseHookOutput, error) { |
| 35 | + mu.Lock() |
| 36 | + hookLog = append(hookLog, hookEntry{kind: "pre", toolName: input.ToolName, sessionID: input.SessionID}) |
| 37 | + mu.Unlock() |
| 38 | + return &copilot.PreToolUseHookOutput{PermissionDecision: "allow"}, nil |
| 39 | + }, |
| 40 | + OnPostToolUse: func(input copilot.PostToolUseHookInput, invocation copilot.HookInvocation) (*copilot.PostToolUseHookOutput, error) { |
| 41 | + mu.Lock() |
| 42 | + hookLog = append(hookLog, hookEntry{kind: "post", toolName: input.ToolName, sessionID: input.SessionID}) |
| 43 | + mu.Unlock() |
| 44 | + return nil, nil |
| 45 | + }, |
| 46 | + }, |
| 47 | + }) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Failed to create session: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Create a file for the sub-agent to read |
| 53 | + testFile := filepath.Join(ctx.WorkDir, "subagent-test.txt") |
| 54 | + if err := os.WriteFile(testFile, []byte("Hello from subagent test!"), 0644); err != nil { |
| 55 | + t.Fatalf("Failed to write test file: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + _, err = session.SendAndWait(t.Context(), copilot.MessageOptions{ |
| 59 | + Prompt: "Use the task tool to spawn an explore agent that reads the file subagent-test.txt in the current directory and reports its contents. You must use the task tool.", |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("Failed to send message: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + mu.Lock() |
| 66 | + defer mu.Unlock() |
| 67 | + |
| 68 | + // Parent tool hooks fire for "task" |
| 69 | + var taskPre *hookEntry |
| 70 | + for i := range hookLog { |
| 71 | + if hookLog[i].kind == "pre" && hookLog[i].toolName == "task" { |
| 72 | + taskPre = &hookLog[i] |
| 73 | + break |
| 74 | + } |
| 75 | + } |
| 76 | + if taskPre == nil { |
| 77 | + t.Fatal("preToolUse should fire for the parent's 'task' tool call") |
| 78 | + } |
| 79 | + |
| 80 | + // Sub-agent tool hooks fire for "view" |
| 81 | + var viewPre, viewPost []hookEntry |
| 82 | + for _, h := range hookLog { |
| 83 | + if h.toolName == "view" { |
| 84 | + if h.kind == "pre" { |
| 85 | + viewPre = append(viewPre, h) |
| 86 | + } else { |
| 87 | + viewPost = append(viewPost, h) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + if len(viewPre) == 0 { |
| 92 | + t.Fatal("preToolUse should fire for the sub-agent's 'view' tool call") |
| 93 | + } |
| 94 | + if len(viewPost) == 0 { |
| 95 | + t.Fatal("postToolUse should fire for the sub-agent's 'view' tool call") |
| 96 | + } |
| 97 | + |
| 98 | + // input.SessionID distinguishes parent from sub-agent |
| 99 | + if viewPre[0].sessionID == taskPre.sessionID { |
| 100 | + t.Error("Sub-agent tool hooks should have a different sessionId than parent tool hooks") |
| 101 | + } |
| 102 | + }) |
| 103 | +} |
0 commit comments