From c02852d87fdaac9113324c3c4a893c1c3bb9be81 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 2 Oct 2026 11:47:50 +1000 Subject: [PATCH] feat(messaging): add a per-turn interactive carrier for clarify and picker The contract could carry a question but nothing carried the ability to ask one. Add Interactive, resolved from a channel adapter's own capability report through InteractiveOf, with WithInteractive / InteractiveFromContext as the per-turn carrier -- the same shape WithApprovalRequester already uses for consent. The capability still wins over the method set, so a mis-reporting adapter contributes nothing instead of being called. Tests: TestInteractiveOfTrustsCapabilityReport and TestInteractiveCarrierRoundTrip pin the resolution and the round trip. --- internal/domain/messaging/interactive.go | 39 +++++++++++++++++++ internal/domain/messaging/interactive_test.go | 30 ++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/internal/domain/messaging/interactive.go b/internal/domain/messaging/interactive.go index d548b8f76..a937b1fc8 100644 --- a/internal/domain/messaging/interactive.go +++ b/internal/domain/messaging/interactive.go @@ -96,6 +96,45 @@ func PickerOf(sender any) (PickerRequester, bool) { return picker, ok } +// Interactive bundles the human-interaction requesters a channel adapter can +// carry for one turn, resolved once from its capability report. The zero value +// carries nothing, which is what a turn whose channel cannot ask a question +// sees; the gateway turn reads it to decide whether to offer a question tool +// at all rather than one that always fails. +type Interactive struct { + Clarifier ClarifyRequester + Picker PickerRequester +} + +// Carries reports whether the adapter can carry at least one interaction. +func (i Interactive) Carries() bool { return i.Clarifier != nil || i.Picker != nil } + +// InteractiveOf resolves sender into the requesters it can carry, trusting the +// capability report over the method set through ClarifierOf and PickerOf. A +// sender that implements a method but denies the capability contributes +// nothing, so a mis-reporting adapter degrades instead of being called. +func InteractiveOf(sender any) Interactive { + clarifier, _ := ClarifierOf(sender) + picker, _ := PickerOf(sender) + return Interactive{Clarifier: clarifier, Picker: picker} +} + +type interactiveCtxKey struct{} + +// WithInteractive stores a channel's interactive requesters on ctx for one +// turn, the way WithApprovalRequester stores consent. The gateway turn reads +// it to know whether it can ask the human a question. +func WithInteractive(ctx context.Context, i Interactive) context.Context { + return context.WithValue(ctx, interactiveCtxKey{}, i) +} + +// InteractiveFromContext returns the requesters stored on ctx, or the zero +// value when the turn's channel cannot carry an interaction. +func InteractiveFromContext(ctx context.Context) Interactive { + i, _ := ctx.Value(interactiveCtxKey{}).(Interactive) + return i +} + // FormatClarifyText renders a ClarifyRequest as a plain-text prompt for a // channel with no native question UI. func FormatClarifyText(req ClarifyRequest) string { diff --git a/internal/domain/messaging/interactive_test.go b/internal/domain/messaging/interactive_test.go index b906358b5..95a45158c 100644 --- a/internal/domain/messaging/interactive_test.go +++ b/internal/domain/messaging/interactive_test.go @@ -444,3 +444,33 @@ func TestTextFallbackUnconfigured(t *testing.T) { t.Fatalf("RequestChoice error = %v, want ErrTextFallbackNotConfigured", err) } } + +func TestInteractiveOfTrustsCapabilityReport(t *testing.T) { + got := InteractiveOf(&nativeInteractive{}) + if !got.Carries() { + t.Fatal("a channel reporting Clarify and Picker produced no requesters") + } + if got.Clarifier == nil || got.Picker == nil { + t.Fatalf("Interactive = %+v, want both requesters", got) + } + + // The capability is the contract: a sender that claims an interaction it + // does not implement must contribute nothing rather than be called. + if over := InteractiveOf(interactiveOverclaim{}); over.Carries() { + t.Errorf("over-claiming adapter produced requesters: %+v", over) + } + if (Interactive{}).Carries() { + t.Error("the zero Interactive reports that it carries an interaction") + } +} + +func TestInteractiveCarrierRoundTrip(t *testing.T) { + ctx := WithInteractive(context.Background(), InteractiveOf(&nativeInteractive{})) + got := InteractiveFromContext(ctx) + if !got.Carries() { + t.Fatalf("InteractiveFromContext = %+v, want the stored requesters", got) + } + if InteractiveFromContext(context.Background()).Carries() { + t.Error("a context without a carrier reported one") + } +}