Skip to content

Commit 6c98c85

Browse files
authored
fix: tolerate unknown hook types in .NET and Go SDKs (#1013)
1 parent 0388810 commit 6c98c85

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

dotnet/src/Session.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ internal void RegisterHooks(SessionHooks hooks)
967967
JsonSerializer.Deserialize(input.GetRawText(), SessionJsonContext.Default.ErrorOccurredHookInput)!,
968968
invocation)
969969
: null,
970-
_ => throw new ArgumentException($"Unknown hook type: {hookType}")
970+
_ => null
971971
};
972972
}
973973

go/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ func (s *Session) handleHooksInvoke(hookType string, rawInput json.RawMessage) (
451451
}
452452
return hooks.OnErrorOccurred(input, invocation)
453453
default:
454-
return nil, fmt.Errorf("unknown hook type: %s", hookType)
454+
return nil, nil
455455
}
456456
}
457457

go/session_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package copilot
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"strings"
67
"sync"
@@ -530,6 +531,45 @@ func TestSession_ElicitationHandler(t *testing.T) {
530531
})
531532
}
532533

534+
func TestSession_HookForwardCompatibility(t *testing.T) {
535+
t.Run("unknown hook type returns nil without error when known hooks are registered", func(t *testing.T) {
536+
session, cleanup := newTestSession()
537+
defer cleanup()
538+
539+
// Register known hook handlers to simulate a real session configuration.
540+
// The handler itself does nothing; it only exists to confirm that even
541+
// when other hooks are active, an unknown hook type is still ignored.
542+
session.registerHooks(&SessionHooks{
543+
OnPostToolUse: func(input PostToolUseHookInput, invocation HookInvocation) (*PostToolUseHookOutput, error) {
544+
return nil, nil
545+
},
546+
})
547+
548+
// "postToolUseFailure" is an example of a hook type introduced by a newer
549+
// CLI version that the SDK does not yet know about.
550+
output, err := session.handleHooksInvoke("postToolUseFailure", json.RawMessage(`{}`))
551+
if err != nil {
552+
t.Errorf("Expected no error for unknown hook type, got: %v", err)
553+
}
554+
if output != nil {
555+
t.Errorf("Expected nil output for unknown hook type, got: %v", output)
556+
}
557+
})
558+
559+
t.Run("unknown hook type with no hooks registered returns nil without error", func(t *testing.T) {
560+
session, cleanup := newTestSession()
561+
defer cleanup()
562+
563+
output, err := session.handleHooksInvoke("futureHookType", json.RawMessage(`{"someField":"value"}`))
564+
if err != nil {
565+
t.Errorf("Expected no error for unknown hook type with no hooks, got: %v", err)
566+
}
567+
if output != nil {
568+
t.Errorf("Expected nil output for unknown hook type with no hooks, got: %v", output)
569+
}
570+
})
571+
}
572+
533573
func TestSession_ElicitationRequestSchema(t *testing.T) {
534574
t.Run("elicitation.requested passes full schema to handler", func(t *testing.T) {
535575
// Verify the schema extraction logic from handleBroadcastEvent

0 commit comments

Comments
 (0)