fix(gatewayrpc): carry tool approvals and ask_user questions between a channel and the gateway turn

This commit is contained in:
sam committed 2026-10-08 00:22:37 +11:00
1 parent eb835d9096
commit 65fdbcd45d
7 files changed
+915 -259

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -25,6 +25,7 @@ const (
ChatService_RecentTurns_FullMethodName = "/gateway.v1.ChatService/RecentTurns"
ChatService_Route_FullMethodName = "/gateway.v1.ChatService/Route"
ChatService_Stream_FullMethodName = "/gateway.v1.ChatService/Stream"
ChatService_Answer_FullMethodName = "/gateway.v1.ChatService/Answer"
ChatService_Cancel_FullMethodName = "/gateway.v1.ChatService/Cancel"
ChatService_StopTasks_FullMethodName = "/gateway.v1.ChatService/StopTasks"
ChatService_SetPersona_FullMethodName = "/gateway.v1.ChatService/SetPersona"
@@ -56,6 +57,7 @@ type ChatServiceClient interface {
RecentTurns(ctx context.Context, in *RecentTurnsRequest, opts ...grpc.CallOption) (*RecentTurnsResponse, error)
Route(ctx context.Context, in *RouteRequest, opts ...grpc.CallOption) (*RouteResponse, error)
Stream(ctx context.Context, in *StreamRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[StreamResponse], error)
Answer(ctx context.Context, in *AnswerRequest, opts ...grpc.CallOption) (*AnswerResponse, error)
Cancel(ctx context.Context, in *CancelRequest, opts ...grpc.CallOption) (*CancelResponse, error)
StopTasks(ctx context.Context, in *StopTasksRequest, opts ...grpc.CallOption) (*StopTasksResponse, error)
SetPersona(ctx context.Context, in *SetPersonaRequest, opts ...grpc.CallOption) (*SetPersonaResponse, error)
@@ -154,6 +156,16 @@ func (c *chatServiceClient) Stream(ctx context.Context, in *StreamRequest, opts
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type ChatService_StreamClient = grpc.ServerStreamingClient[StreamResponse]
func (c *chatServiceClient) Answer(ctx context.Context, in *AnswerRequest, opts ...grpc.CallOption) (*AnswerResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(AnswerResponse)
err := c.cc.Invoke(ctx, ChatService_Answer_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *chatServiceClient) Cancel(ctx context.Context, in *CancelRequest, opts ...grpc.CallOption) (*CancelResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(CancelResponse)
@@ -354,6 +366,7 @@ type ChatServiceServer interface {
RecentTurns(context.Context, *RecentTurnsRequest) (*RecentTurnsResponse, error)
Route(context.Context, *RouteRequest) (*RouteResponse, error)
Stream(*StreamRequest, grpc.ServerStreamingServer[StreamResponse]) error
Answer(context.Context, *AnswerRequest) (*AnswerResponse, error)
Cancel(context.Context, *CancelRequest) (*CancelResponse, error)
StopTasks(context.Context, *StopTasksRequest) (*StopTasksResponse, error)
SetPersona(context.Context, *SetPersonaRequest) (*SetPersonaResponse, error)
@@ -401,6 +414,9 @@ func (UnimplementedChatServiceServer) Route(context.Context, *RouteRequest) (*Ro
func (UnimplementedChatServiceServer) Stream(*StreamRequest, grpc.ServerStreamingServer[StreamResponse]) error {
return status.Error(codes.Unimplemented, "method Stream not implemented")
}
func (UnimplementedChatServiceServer) Answer(context.Context, *AnswerRequest) (*AnswerResponse, error) {
return nil, status.Error(codes.Unimplemented, "method Answer not implemented")
}
func (UnimplementedChatServiceServer) Cancel(context.Context, *CancelRequest) (*CancelResponse, error) {
return nil, status.Error(codes.Unimplemented, "method Cancel not implemented")
}
@@ -580,6 +596,24 @@ func _ChatService_Stream_Handler(srv interface{}, stream grpc.ServerStream) erro
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type ChatService_StreamServer = grpc.ServerStreamingServer[StreamResponse]
func _ChatService_Answer_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(AnswerRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ChatServiceServer).Answer(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ChatService_Answer_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ChatServiceServer).Answer(ctx, req.(*AnswerRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ChatService_Cancel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(CancelRequest)
if err := dec(in); err != nil {
@@ -949,6 +983,10 @@ var ChatService_ServiceDesc = grpc.ServiceDesc{
MethodName: "Route",
Handler: _ChatService_Route_Handler,
},
{
MethodName: "Answer",
Handler: _ChatService_Answer_Handler,
},
{
MethodName: "Cancel",
Handler: _ChatService_Cancel_Handler,
+145
View File
@@ -0,0 +1,145 @@
package gatewayrpc
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"strings"
"sync"
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// Answers to the asks a turn is blocked on, keyed by ask id. A channel's
// requesters live in the caller's process, so a turn asks over its stream and
// the caller answers through the Answer RPC.
type pendingAsks struct {
mu sync.Mutex
waiting map[string]chan string
}
func (p *pendingAsks) open() (string, chan string) {
var b [12]byte
_, _ = rand.Read(b[:])
id := hex.EncodeToString(b[:])
ch := make(chan string, 1)
p.mu.Lock()
if p.waiting == nil {
p.waiting = make(map[string]chan string)
}
p.waiting[id] = ch
p.mu.Unlock()
return id, ch
}
func (p *pendingAsks) close(id string) {
p.mu.Lock()
delete(p.waiting, id)
p.mu.Unlock()
}
func (p *pendingAsks) answer(id, answer string) bool {
p.mu.Lock()
ch, ok := p.waiting[id]
p.mu.Unlock()
if !ok {
return false
}
select {
case ch <- answer:
default:
}
return true
}
// remoteAsker poses a turn's questions on its stream. send is serialized with
// the turn's own frames by the caller.
type remoteAsker struct {
pending *pendingAsks
send func(*pb.StreamResponse) error
}
func (a remoteAsker) ask(ctx context.Context, q *pb.Ask) (string, error) {
id, ch := a.pending.open()
defer a.pending.close(id)
q.Id = id
if err := a.send(&pb.StreamResponse{Kind: "ask", Ask: q}); err != nil {
return "", err
}
select {
case answer := <-ch:
return answer, nil
case <-ctx.Done():
return "", ctx.Err()
}
}
func (a remoteAsker) RequestApproval(ctx context.Context, action, description string) (messaging.ApprovalDecision, error) {
answer, err := a.ask(ctx, &pb.Ask{Kind: "approval", Prompt: action, Detail: description})
if err != nil {
return messaging.ApprovalDenied, err
}
switch answer {
case "approve":
return messaging.ApprovalApproved, nil
case "always":
return messaging.ApprovalPermanentlyApproved, nil
default:
return messaging.ApprovalDenied, nil
}
}
func (a remoteAsker) RequestClarification(ctx context.Context, req messaging.ClarifyRequest) (string, error) {
answer, err := a.ask(ctx, &pb.Ask{Kind: "clarify", Prompt: req.Question, Choices: choicesProto(req.Suggestions)})
if err != nil {
return "", err
}
if answer = strings.TrimSpace(answer); answer == "" {
return "", messaging.ErrEmptyReply
}
return answer, nil
}
func (a remoteAsker) RequestChoice(ctx context.Context, req messaging.PickerRequest) (messaging.InteractiveChoice, error) {
if len(req.Options) == 0 {
return messaging.InteractiveChoice{}, messaging.ErrNoPickerOptions
}
answer, err := a.ask(ctx, &pb.Ask{Kind: "picker", Prompt: req.Prompt, Choices: choicesProto(req.Options)})
if err != nil {
return messaging.InteractiveChoice{}, err
}
for _, option := range req.Options {
if option.ID == answer {
return option, nil
}
}
return messaging.InteractiveChoice{}, errors.New("picker answer matches no option")
}
func choicesProto(choices []messaging.InteractiveChoice) []*pb.Choice {
out := make([]*pb.Choice, 0, len(choices))
for _, c := range choices {
out = append(out, &pb.Choice{Id: c.ID, Label: c.Label})
}
return out
}
// withAsks puts on ctx the requesters the caller said it can answer.
func withAsks(ctx context.Context, r *pb.StreamRequest, asker remoteAsker) context.Context {
if r.CanApprove {
ctx = messaging.WithApprovalRequester(ctx, asker)
}
var interactive messaging.Interactive
if r.CanClarify {
interactive.Clarifier = asker
}
if r.CanPick {
interactive.Picker = asker
}
if interactive.Carries() {
ctx = messaging.WithInteractive(ctx, interactive)
}
return ctx
}
@@ -0,0 +1,93 @@
package gatewayrpc
import (
"context"
"net"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// askingChat is a turn that asks for approval and reports what it got.
type askingChat struct{ messaging.ChatContract }
func (askingChat) Stream(ctx context.Context, _ messaging.Inbound) (<-chan messaging.ChatEvent, error) {
out := make(chan messaging.ChatEvent, 1)
go func() {
defer close(out)
approver := messaging.ApprovalFromContext(ctx)
if approver == nil {
out <- messaging.ChatEvent{Kind: "done", Text: "not configured"}
return
}
decision, err := approver.RequestApproval(ctx, "shell", "rm -rf /tmp/x")
switch {
case err != nil:
out <- messaging.ChatEvent{Kind: "done", Text: "error"}
case decision == messaging.ApprovalDenied:
out <- messaging.ChatEvent{Kind: "done", Text: "denied"}
default:
out <- messaging.ChatEvent{Kind: "done", Text: "approved"}
}
}()
return out, nil
}
type fixedApprover messaging.ApprovalDecision
func (f fixedApprover) RequestApproval(context.Context, string, string) (messaging.ApprovalDecision, error) {
return messaging.ApprovalDecision(f), nil
}
// TestStreamCarriesApproval: an approval-gated turn in the gateway reaches the
// caller's own approver, and a caller with none is never asked.
func TestStreamCarriesApproval(t *testing.T) {
listener := bufconn.Listen(1 << 16)
srv := grpc.NewServer()
RegisterServer(srv, askingChat{}, nil, Catalog{})
go func() { _ = srv.Serve(listener) }()
t.Cleanup(srv.Stop)
conn, err := grpc.NewClient("passthrough:///bufnet",
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { return listener.Dial() }),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = conn.Close() })
client := NewClient(conn)
tests := []struct {
name string
approver messaging.ApprovalRequester
want string
}{
{name: "approved by the caller", approver: fixedApprover(messaging.ApprovalApproved), want: "approved"},
{name: "denied by the caller", approver: fixedApprover(messaging.ApprovalDenied), want: "denied"},
{name: "a caller with no approver is not asked", want: "not configured"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if tt.approver != nil {
ctx = messaging.WithApprovalRequester(ctx, tt.approver)
}
events, err := client.Stream(ctx, messaging.Inbound{})
if err != nil {
t.Fatal(err)
}
var got string
for event := range events {
got = event.Text
}
if got != tt.want {
t.Fatalf("turn saw %q, want %q", got, tt.want)
}
})
}
}
+53 -1
View File
@@ -123,7 +123,14 @@ func (c *Client) ApplyOperatorTaskAction(ctx context.Context, actor taskactions.
}
func (c *Client) Stream(ctx context.Context, in messaging.Inbound) (<-chan messaging.ChatEvent, error) {
stream, err := c.client.Stream(ctx, &pb.StreamRequest{Message: inboundProto(in)})
approver := messaging.ApprovalFromContext(ctx)
interactive := messaging.InteractiveFromContext(ctx)
stream, err := c.client.Stream(ctx, &pb.StreamRequest{
Message: inboundProto(in),
CanApprove: approver != nil,
CanClarify: interactive.Clarifier != nil,
CanPick: interactive.Picker != nil,
})
if err != nil {
return nil, err
}
@@ -135,6 +142,11 @@ func (c *Client) Stream(ctx context.Context, in messaging.Inbound) (<-chan messa
if errors.Is(err, io.EOF) {
return
}
if err == nil && v.GetAsk() != nil {
// The turn blocks on this; keep receiving while the human answers.
go c.answer(ctx, v.GetAsk(), approver, interactive)
continue
}
var event messaging.ChatEvent
if err != nil {
event = messaging.ChatEvent{Kind: "error", Text: err.Error()}
@@ -153,3 +165,43 @@ func (c *Client) Stream(ctx context.Context, in messaging.Inbound) (<-chan messa
}()
return out, nil
}
// answer puts ask to the channel's own requester and returns its reply. A
// failed or abandoned ask answers deny or empty, which the turn reads as no.
func (c *Client) answer(ctx context.Context, ask *pb.Ask, approver messaging.ApprovalRequester, interactive messaging.Interactive) {
var reply string
switch ask.GetKind() {
case "approval":
if approver != nil {
decision, err := approver.RequestApproval(ctx, ask.GetPrompt(), ask.GetDetail())
switch {
case err != nil || decision == messaging.ApprovalDenied:
reply = "deny"
case decision == messaging.ApprovalPermanentlyApproved:
reply = "always"
default:
reply = "approve"
}
}
case "clarify":
if interactive.Clarifier != nil {
reply, _ = interactive.Clarifier.RequestClarification(ctx, messaging.ClarifyRequest{Question: ask.GetPrompt(), Suggestions: choicesValue(ask.GetChoices())})
}
case "picker":
if interactive.Picker != nil {
choice, err := interactive.Picker.RequestChoice(ctx, messaging.PickerRequest{Prompt: ask.GetPrompt(), Options: choicesValue(ask.GetChoices())})
if err == nil {
reply = choice.ID
}
}
}
_, _ = c.client.Answer(ctx, &pb.AnswerRequest{AskId: ask.GetId(), Answer: reply})
}
func choicesValue(choices []*pb.Choice) []messaging.InteractiveChoice {
out := make([]messaging.InteractiveChoice, 0, len(choices))
for _, c := range choices {
out = append(out, messaging.InteractiveChoice{ID: c.GetId(), Label: c.GetLabel()})
}
return out
}
+16 -2
View File
@@ -2,6 +2,7 @@ package gatewayrpc
import (
"context"
"sync"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -19,6 +20,7 @@ type server struct {
chat messaging.ChatContract
sessions messaging.SessionStore
catalog Catalog
asks pendingAsks
}
// RegisterServer serves the Gateway chat contract. catalog supplies the
@@ -259,7 +261,15 @@ func (s *server) SearchMessages(ctx context.Context, r *pb.SearchMessagesRequest
}
func (s *server) Stream(r *pb.StreamRequest, out grpc.ServerStreamingServer[pb.StreamResponse]) error {
events, err := s.chat.Stream(out.Context(), inboundValue(r.Message))
// An ask is sent from the turn's goroutine while this one sends events.
var sendMu sync.Mutex
send := func(v *pb.StreamResponse) error {
sendMu.Lock()
defer sendMu.Unlock()
return out.Send(v)
}
ctx := withAsks(out.Context(), r, remoteAsker{pending: &s.asks, send: send})
events, err := s.chat.Stream(ctx, inboundValue(r.Message))
if err != nil {
return err
}
@@ -271,9 +281,13 @@ func (s *server) Stream(r *pb.StreamRequest, out grpc.ServerStreamingServer[pb.S
if !ok {
return nil
}
if err := out.Send(eventProto(event)); err != nil {
if err := send(eventProto(event)); err != nil {
return err
}
}
}
}
func (s *server) Answer(_ context.Context, r *pb.AnswerRequest) (*pb.AnswerResponse, error) {
return &pb.AnswerResponse{Found: s.asks.answer(r.AskId, r.Answer)}, nil
}
+24 -1
View File
@@ -11,6 +11,7 @@ service ChatService {
rpc RecentTurns(RecentTurnsRequest) returns (RecentTurnsResponse);
rpc Route(RouteRequest) returns (RouteResponse);
rpc Stream(StreamRequest) returns (stream StreamResponse);
rpc Answer(AnswerRequest) returns (AnswerResponse);
rpc Cancel(CancelRequest) returns (CancelResponse);
rpc StopTasks(StopTasksRequest) returns (StopTasksResponse);
rpc SetPersona(SetPersonaRequest) returns (SetPersonaResponse);
@@ -144,13 +145,35 @@ message RouteResponse {
// outcome.
bool rate_limited = 3;
}
message StreamRequest { Message message = 1; }
// The caller states which asks it can answer; the turn offers only those.
message StreamRequest {
Message message = 1;
bool can_approve = 2;
bool can_clarify = 3;
bool can_pick = 4;
}
// Ask is a question the turn blocks on until Answer names its id.
message Ask {
string id = 1;
// approval, clarify or picker.
string kind = 2;
string prompt = 3;
// Tool description, for an approval.
string detail = 4;
repeated Choice choices = 5;
}
message Choice { string id = 1; string label = 2; }
// For an approval, answer is approve, always or deny; for a picker, the
// choice id; for a clarify, the reply text.
message AnswerRequest { string ask_id = 1; string answer = 2; }
message AnswerResponse { bool found = 1; }
message StreamResponse {
string kind = 1;
string text = 2;
string session_id = 3;
ToolCall tool = 4;
Media media = 5;
Ask ask = 6;
}
message CancelRequest { string session_id = 1; }
message CancelResponse { bool cancelled = 1; int64 dropped = 2; }