-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix sub-agent hook propagation: expose sessionId on hook inputs #1290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
802ac97
test: add E2E test for subagent hook propagation
SteveSandersonMS 91347db
Clarify SESSION_BASED_SUBAGENTS flag requirement in subagent hooks test
SteveSandersonMS 26948b3
Improve subagent hooks test to verify parent vs sub-agent ordering
SteveSandersonMS b5ff9f0
Expose agentSessionId in hook inputs to distinguish parent vs sub-agent
SteveSandersonMS 5f634a8
Expose sessionId on BaseHookInput and simplify test
SteveSandersonMS c64c910
Fix BaseHookInput.sessionId doc comment to be hook-type-agnostic
SteveSandersonMS c13447f
Add sessionId to hook input types and subagent hooks E2E tests for Py…
SteveSandersonMS 0691f1f
Add sessionId to Rust hook input types and subagent hooks E2E test
SteveSandersonMS 4ea1241
Add sessionId to all hook input types consistently across Python and …
SteveSandersonMS 7021a8e
Fix Python subagent hooks test to scope env var to client instance
SteveSandersonMS ad80c27
Update snapshot for new runtime notification text and format Python test
SteveSandersonMS 8a3cf48
Apply prettier formatting to Node.js E2E test
SteveSandersonMS 582c506
Fix Rust nightly formatting for subagent_hooks E2E test
SteveSandersonMS 2a2b45c
Add sessionId to Rust hook test fixtures
SteveSandersonMS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| using System.Collections.Concurrent; | ||
| using GitHub.Copilot.SDK.Test.Harness; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace GitHub.Copilot.SDK.Test.E2E; | ||
|
|
||
| public class SubagentHooksE2ETests(E2ETestFixture fixture, ITestOutputHelper output) | ||
| : E2ETestBase(fixture, "subagent_hooks", output) | ||
| { | ||
| [Fact] | ||
| public async Task Should_Invoke_PreToolUse_And_PostToolUse_Hooks_For_Sub_Agent_Tool_Calls() | ||
| { | ||
| var hookLog = new ConcurrentBag<(string Kind, string ToolName, string SessionId)>(); | ||
|
|
||
| // Create a client with the session-based subagents feature flag | ||
| var env = new Dictionary<string, string>(Ctx.GetEnvironment()); | ||
| env["COPILOT_EXP_COPILOT_CLI_SESSION_BASED_SUBAGENTS"] = "true"; | ||
| var client = Ctx.CreateClient(options: new CopilotClientOptions { Environment = env }); | ||
|
|
||
| var session = await client.CreateSessionAsync(new SessionConfig | ||
| { | ||
| OnPermissionRequest = PermissionHandler.ApproveAll, | ||
| Hooks = new SessionHooks | ||
| { | ||
| OnPreToolUse = (input, invocation) => | ||
| { | ||
| hookLog.Add(("pre", input.ToolName, input.SessionId)); | ||
| return Task.FromResult<PreToolUseHookOutput?>(new PreToolUseHookOutput | ||
| { | ||
| PermissionDecision = "allow" | ||
| }); | ||
| }, | ||
| OnPostToolUse = (input, invocation) => | ||
| { | ||
| hookLog.Add(("post", input.ToolName, input.SessionId)); | ||
| return Task.FromResult<PostToolUseHookOutput?>(null); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // Create a file for the sub-agent to read | ||
| await File.WriteAllTextAsync(Path.Combine(Ctx.WorkDir, "subagent-test.txt"), "Hello from subagent test!"); | ||
|
|
||
| await session.SendAndWaitAsync( | ||
| new MessageOptions | ||
| { | ||
| 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." | ||
| }, | ||
| timeout: TimeSpan.FromSeconds(120)); | ||
|
|
||
| var log = hookLog.ToArray(); | ||
|
|
||
| // Parent tool hooks fire for "task" | ||
| var taskPre = log.Where(h => h.Kind == "pre" && h.ToolName == "task").ToArray(); | ||
| Assert.True(taskPre.Length >= 1, "preToolUse should fire for the parent's 'task' tool call"); | ||
|
|
||
| // Sub-agent tool hooks fire for "view" | ||
| var viewPre = log.Where(h => h.Kind == "pre" && h.ToolName == "view").ToArray(); | ||
| var viewPost = log.Where(h => h.Kind == "post" && h.ToolName == "view").ToArray(); | ||
| Assert.True(viewPre.Length > 0, "preToolUse should fire for the sub-agent's 'view' tool call"); | ||
| Assert.True(viewPost.Length > 0, "postToolUse should fire for the sub-agent's 'view' tool call"); | ||
|
|
||
| // input.SessionId distinguishes parent from sub-agent | ||
| Assert.NotEqual(viewPre[0].SessionId, taskPre[0].SessionId); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package e2e | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| copilot "github.com/github/copilot-sdk/go" | ||
| "github.com/github/copilot-sdk/go/internal/e2e/testharness" | ||
| ) | ||
|
|
||
| func TestSubagentHooksE2E(t *testing.T) { | ||
| ctx := testharness.NewTestContext(t) | ||
| client := ctx.NewClient(func(o *copilot.ClientOptions) { | ||
| o.Env = append(o.Env, "COPILOT_EXP_COPILOT_CLI_SESSION_BASED_SUBAGENTS=true") | ||
| }) | ||
| t.Cleanup(func() { client.ForceStop() }) | ||
|
|
||
| t.Run("should invoke preToolUse and postToolUse hooks for sub-agent tool calls", func(t *testing.T) { | ||
| ctx.ConfigureForTest(t) | ||
|
|
||
| type hookEntry struct { | ||
| kind string | ||
| toolName string | ||
| sessionID string | ||
| } | ||
| var hookLog []hookEntry | ||
| var mu sync.Mutex | ||
|
|
||
| session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{ | ||
| OnPermissionRequest: copilot.PermissionHandler.ApproveAll, | ||
| Hooks: &copilot.SessionHooks{ | ||
| OnPreToolUse: func(input copilot.PreToolUseHookInput, invocation copilot.HookInvocation) (*copilot.PreToolUseHookOutput, error) { | ||
| mu.Lock() | ||
| hookLog = append(hookLog, hookEntry{kind: "pre", toolName: input.ToolName, sessionID: input.SessionID}) | ||
| mu.Unlock() | ||
| return &copilot.PreToolUseHookOutput{PermissionDecision: "allow"}, nil | ||
| }, | ||
| OnPostToolUse: func(input copilot.PostToolUseHookInput, invocation copilot.HookInvocation) (*copilot.PostToolUseHookOutput, error) { | ||
| mu.Lock() | ||
| hookLog = append(hookLog, hookEntry{kind: "post", toolName: input.ToolName, sessionID: input.SessionID}) | ||
| mu.Unlock() | ||
| return nil, nil | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create session: %v", err) | ||
| } | ||
|
|
||
| // Create a file for the sub-agent to read | ||
| testFile := filepath.Join(ctx.WorkDir, "subagent-test.txt") | ||
| if err := os.WriteFile(testFile, []byte("Hello from subagent test!"), 0644); err != nil { | ||
| t.Fatalf("Failed to write test file: %v", err) | ||
| } | ||
|
|
||
| _, err = session.SendAndWait(t.Context(), copilot.MessageOptions{ | ||
| 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.", | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Failed to send message: %v", err) | ||
| } | ||
|
|
||
| mu.Lock() | ||
| defer mu.Unlock() | ||
|
|
||
| // Parent tool hooks fire for "task" | ||
| var taskPre *hookEntry | ||
| for i := range hookLog { | ||
| if hookLog[i].kind == "pre" && hookLog[i].toolName == "task" { | ||
| taskPre = &hookLog[i] | ||
| break | ||
| } | ||
| } | ||
| if taskPre == nil { | ||
| t.Fatal("preToolUse should fire for the parent's 'task' tool call") | ||
| } | ||
|
|
||
| // Sub-agent tool hooks fire for "view" | ||
| var viewPre, viewPost []hookEntry | ||
| for _, h := range hookLog { | ||
| if h.toolName == "view" { | ||
| if h.kind == "pre" { | ||
| viewPre = append(viewPre, h) | ||
| } else { | ||
| viewPost = append(viewPost, h) | ||
| } | ||
| } | ||
| } | ||
| if len(viewPre) == 0 { | ||
| t.Fatal("preToolUse should fire for the sub-agent's 'view' tool call") | ||
| } | ||
| if len(viewPost) == 0 { | ||
| t.Fatal("postToolUse should fire for the sub-agent's 'view' tool call") | ||
| } | ||
|
|
||
| // input.SessionID distinguishes parent from sub-agent | ||
| if viewPre[0].sessionID == taskPre.sessionID { | ||
| t.Error("Sub-agent tool hooks should have a different sessionId than parent tool hooks") | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.