[Feature] SessionHooks should support multiple handlers per hook slot
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 38/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- csharp
- Domain
- backend-api-design
Research direction
Start by locating SessionHooks and the existing hook invocation behavior, especially OnPostToolUse and OnPreToolUse. Compare the current single-delegate and multicast-delegate behavior with the proposed UsePreToolUse and UsePostToolUse pipeline. Done means handlers can compose in explicit order while inspecting, modifying, short-circuiting, or forwarding hook execution without silent clobbering.
Written by the indexing model from the issue text.
Description
At least in C#, SessionHooks properties (OnPostToolUse, OnPreToolUse, etc.) are single-assignment delegates. This makes it easy to accidentally clobber a previously-registered handler with =, and there is no SDK-level way to compose multiple handlers.
Why this matters for library authors
We are building a library on top of the SDK. Our framework needs to register its own hooks (for example, observing update_todo calls in a post-tool-use hook to persist state). But we also need to leave hook slots open for our downstream consumers to register their own hooks.
Today there is no safe way to do this. We can build our own closure-chaining helper (capture the existing delegate and wrap it), but since SessionHooks properties are public settable, any downstream consumer doing config.Hooks.OnPostToolUse = myHandler silently clobbers our framework hook. We cannot prevent that as a library because we do not control the downstream code.
Multicast delegates (+=) do not appear to work either, because await hooks.OnPostToolUse(...) only awaits the last delegate in the invocation list.
What would help
A middleware-style pipeline where each handler receives a next delegate and decides whether to call it, short-circuit, or modify the input/output. This gives callers full control over composition without the SDK needing different resolution rules for different hook types:
hooks.UsePreToolUse(async (input, invocation, next) => {
// inspect, modify input, or short-circuit
return await next(input, invocation);
});
hooks.UsePostToolUse(async (input, invocation, next) => {
var result = await next(input, invocation);
// observe, modify result, or replace it
return result;
});
This is the same pattern as ASP.NET Core middleware and Akka message pipelines. It is simple to reason about, order is explicit, and each handler can decide independently whether to pass through or stop the chain. No special-case semantics per hook type needed.
Workaround
We worked around this by moving our observer to session.On() events instead of hooks, which works for our case but is not a general solution for hooks that need to mutate tool behavior.
- Dominant language
- Java
- Stars
- 10.5k
- Forks
- 1.5k
- Avg merge
- 1d 9h
- Merged PRs (30d)
- 130
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from github/copilot-sdk
-
agentic-workflows
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
github/copilot-sdk#2760 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
github/copilot-sdk#2759 ·
-
documentation
Difficulty 1/5 Under an hour Newbie friendliness 85/100
github/copilot-sdk#2758 ·
-
agentic-workflows
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
github/copilot-sdk#2709 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
github/copilot-sdk#2673 ·
All issues in github/copilot-sdk
Similar issues
-
area/plugin
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
kestra-io/plugin-kestra#190 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
integra-team-red/meet-map#249 ·
-
[Studio][Bug] Cancelled create-user dialog keeps the password and admin switch for the next attempt Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
apache/rocketmq-dashboard#5064 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
wso2/dpdp-accelerator#287 ·