Files
archie/internal/infrastructure/gatewayrpc/conformance_test.go
T
sam 68a17aed91 feat(taskactions): one approval path answers the review gate from every surface
Decision 1 of docs/prds/pr-review-operator-response.md: the dashboard's
POST /api/tasks/{id}/action is the authoritative surface, chat's /approve
is a shortcut that posts every offered finding, and both reach the same
Service.Apply so one operator intent cannot be recorded as two decisions.

The action body now carries instructions (required by rereview) and the
finding keys an approve posts. taskactions.Service answers an offer
through the guarded RespondReviewGate write: approve records the
selection on the offer, rereview records the instructions, increments the
round and clears the offer, and a rereview with no instructions, with no
offer, or at the cap is refused as a conflict with nothing written. Chat
reaches the same service: chatTaskControllerAdapter.ApproveChatTask
delegates to b.taskActions() instead of its own requeue, which is what
removed the handler's hardcoded implement handoff -- feasibility's deliver
stage now names implement on the task before it waits, the invariant the
handler needs.

The payload crosses every hop it must: the NATS action request, the
gateway.v1 gRPC contract (regenerated), the LocalChatAdapter, and the
webui handler, which answers 400 for a rereview without instructions and
409 for the cap. A rereview event kind names its actor the way the
approval and rejection trios do.

taskactions.Task gains review_gate and rereview_rounds, and the two
surfaces' tests assert they agree.
2026-10-02 02:27:59 +10:00

237 lines
9.9 KiB
Go

package gatewayrpc
import (
"context"
"errors"
"fmt"
"net"
"reflect"
"strings"
"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"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/taskactions"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/taskstate"
)
func remoteChat(t *testing.T, local gateway.ChatContract) gateway.ChatContract {
t.Helper()
listener := bufconn.Listen(1 << 20)
server := grpc.NewServer()
RegisterServer(server, local)
go func() { _ = server.Serve(listener) }()
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
conn, err := grpc.NewClient("passthrough:///gateway", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { return listener.DialContext(ctx) }))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = conn.Close() })
return NewClient(conn)
}
func TestChatContractConformance(t *testing.T) {
for _, mode := range []string{"local", "grpc"} {
t.Run(mode, func(t *testing.T) {
ctx := t.Context()
sessions := gateway.NewSessionStoreMemory()
t.Cleanup(func() { _ = sessions.Close() })
router := gateway.NewRouter(nil, nil, "web")
router.Version = "Archie\nGateway: 1.2.3\nRuntime: 4.5.6"
router.InitSessions(sessions)
local := &gateway.LocalChatAdapter{Router: router, Sessions: sessions}
var chat gateway.ChatContract = local
if mode == "grpc" {
chat = remoteChat(t, local)
}
if _, found, err := chat.GetSession(ctx, "missing"); err != nil || found {
t.Fatalf("missing session: %v %v", found, err)
}
msg := gateway.Inbound{
Message: messaging.Message{ConversationID: messaging.ConversationID{ChannelID: "browser"}, Sender: "web", Role: messaging.RoleUser, Text: "/help"},
Page: "/tasks",
}
reply, err := chat.Route(ctx, msg)
if err != nil || reply.Text == "" || reply.SessionID == "" {
t.Fatalf("route: %+v %v", reply, err)
}
session, found, err := chat.GetSession(ctx, reply.SessionID)
if err != nil || !found || session.Source.Platform != "web" {
t.Fatalf("session: %+v %v %v", session, found, err)
}
snap, err := chat.Snapshot(ctx)
if err != nil || len(snap.Sessions) != 1 || snap.CancellationAvailable || snap.PersonasAvailable {
t.Fatalf("snapshot: %+v %v", snap, err)
}
// The Messaging Service renders /version from this field rather
// than stamping its own build, so it has to survive the wire.
if snap.Version != router.Version {
t.Fatalf("snapshot version = %q, want %q", snap.Version, router.Version)
}
if _, err := chat.RecentMessages(ctx, reply.SessionID, 200); err != nil {
t.Fatal(err)
}
if turns, err := chat.RecentTurns(ctx, reply.SessionID, 200); err != nil || len(turns) != 0 {
t.Fatalf("turns: %+v %v", turns, err)
}
if _, err := chat.Cancel(ctx, reply.SessionID); err == nil {
t.Fatal("missing cancellation capability accepted")
}
if _, err := chat.SetPersona(ctx, reply.SessionID, "missing"); err == nil {
t.Fatal("missing persona capability accepted")
}
events, err := chat.Stream(ctx, msg)
if err != nil {
t.Fatal(err)
}
var got []gateway.ChatEvent
for event := range events {
got = append(got, event)
}
if len(got) < 2 || got[0].Kind != "started" || got[len(got)-1].Kind != "done" || got[len(got)-1].Text != reply.Text {
t.Fatalf("stream: %+v", got)
}
})
}
}
func TestWireValuesPreserveHistoryAndMedia(t *testing.T) {
now := time.Date(2026, 9, 5, 1, 2, 3, 456, time.UTC)
tool := gateway.ToolCallEvent{ID: "tool-1", Name: "read", Parameters: "{}", Output: "result", Err: "failed"}
turn := gateway.TurnRecord{TurnID: "turn", SessionID: "session", SourceID: "source", Status: gateway.TurnStatusPartial, Attempt: 3, OwnerID: "owner", InputMessageID: "input", AssistantMessageID: "assistant", PartialText: "partial", ResponseText: "reply", ToolCalls: []gateway.ToolCallEvent{tool}, Error: "failure", CreatedAt: now, UpdatedAt: now}
if got := turnValue(turnProto(turn)); !reflect.DeepEqual(got, turn) {
t.Fatalf("turn round trip: %+v", got)
}
size := int64(0)
width := 640
height := 480
duration := 3
event := gateway.ChatEvent{Kind: "media", Text: "caption", SessionID: "session", Tool: tool, Media: gateway.MediaEvent{ToolName: "send_file", Attachment: gateway.MediaAttachment{Type: "video", FileID: "file", URL: "https://example.test/video", Path: "/tmp/video", MIMEType: "video/mp4", FileName: "video.mp4", FileSize: &size, Width: &width, Height: &height, Duration: &duration}}}
if got := eventValue(eventProto(event)); !reflect.DeepEqual(got, event) {
t.Fatalf("event round trip: %+v", got)
}
// Transport-only inbound context rides alongside the record and must
// survive the hop: Page reaches the system prompt, BudgetKey is what the
// Gateway charges the message against when the channel has no per-person
// SenderID to charge (a webhook route), and Platform is how the Gateway
// learns which channel it is serving -- one Router serves all of them
// (archie-core-c1qx).
in := gateway.Inbound{
Message: messaging.Message{ConversationID: messaging.ConversationID{ChannelID: "/hook"}, Role: messaging.RoleUser, Text: "hi"},
Page: "/tasks",
BudgetKey: "/hook",
Platform: "webhook",
}
if got := inboundValue(inboundProto(in)); !reflect.DeepEqual(got, in) {
t.Fatalf("inbound round trip = %+v, want %+v", got, in)
}
if got := inboundValue(inboundProto(gateway.Inbound{})); !got.Message.At.IsZero() {
t.Fatalf("zero time became %v", got.Message.At)
}
}
// recordingTaskActor captures the identity scope an action reached the daemon
// with. A pointer, because nil is a distinct authorization, not a blank name.
type recordingTaskActor struct {
identity *string
taskID int64
action taskstate.Action
}
func (a *recordingTaskActor) ApplyChatTaskAction(
_ context.Context, identity *string, actor taskactions.Actor, taskID int64, action taskstate.Action, _ taskactions.ReviewResponse,
) (gateway.TaskActionResult, error) {
a.identity, a.taskID, a.action = identity, taskID, action
return gateway.TaskActionResult{TaskID: taskID, Action: string(action), Message: "applied"}, nil
}
// TestTaskActionScopeSurvivesTheWire pins the distinction the daemon's action
// service draws (internal/domain/taskactions.Service.Apply): a nil identity is
// an authenticated dashboard operator acting across identities, a non-nil one
// is a chat user acting on their own task. ApplyTaskAction cannot express the
// operator case -- "" is a real identity in a single-identity deployment (see
// chatTaskProfiles) -- so it has its own contract method, and the difference
// has to survive both the local adapter and the gRPC hop.
func TestTaskActionScopeSurvivesTheWire(t *testing.T) {
for _, mode := range []string{"local", "grpc"} {
t.Run(mode, func(t *testing.T) {
ctx := t.Context()
actor := &recordingTaskActor{}
local := &gateway.LocalChatAdapter{TaskActor: actor}
var chat gateway.ChatContract = local
if mode == "grpc" {
chat = remoteChat(t, local)
}
result, err := chat.ApplyOperatorTaskAction(ctx, taskactions.Actor{}, 42, taskstate.ActionApprove, taskactions.ReviewResponse{})
if err != nil {
t.Fatalf("operator action: %v", err)
}
if actor.identity != nil {
t.Fatalf("operator action arrived scoped to %q, want an unscoped nil identity", *actor.identity)
}
if actor.taskID != 42 || actor.action != taskstate.ActionApprove {
t.Fatalf("operator action = (%d, %q), want (42, %q)", actor.taskID, actor.action, taskstate.ActionApprove)
}
if result.TaskID != 42 || result.Action != string(taskstate.ActionApprove) || result.Message != "applied" {
t.Fatalf("operator result = %+v, want the daemon's result", result)
}
if _, err := chat.ApplyTaskAction(ctx, "scout", 7, taskstate.ActionRetry, taskactions.ReviewResponse{}); err != nil {
t.Fatalf("chat action: %v", err)
}
if actor.identity == nil || *actor.identity != "scout" {
t.Fatalf("chat action lost its identity scope: %v", actor.identity)
}
})
}
}
// failingTaskActor reports one error, whatever the action.
type failingTaskActor struct{ err error }
func (a failingTaskActor) ApplyChatTaskAction(
context.Context, *string, taskactions.Actor, int64, taskstate.Action, taskactions.ReviewResponse,
) (gateway.TaskActionResult, error) {
return gateway.TaskActionResult{}, a.err
}
// TestTaskActionErrorsKeepTheirSentinelOverGRPC: the dashboard answers 404,
// 409 or 503 by matching these sentinels, and gRPC carries a code and a
// string, not a Go error. Losing the class here turns "that task already
// moved on" into "the daemon is broken".
func TestTaskActionErrorsKeepTheirSentinelOverGRPC(t *testing.T) {
for _, tc := range []struct {
name string
err error
}{
{name: "not found", err: fmt.Errorf("task 7: %w", taskactions.ErrNotFound)},
{name: "conflict", err: fmt.Errorf("%w: action \"approve\" is not available while task is parked", taskactions.ErrConflict)},
{name: "stale transition", err: fmt.Errorf("stop task 7: %w", storecontract.ErrStaleTransition)},
{name: "unavailable", err: taskactions.ErrUnavailable},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := t.Context()
local := &gateway.LocalChatAdapter{TaskActor: failingTaskActor{err: tc.err}}
chat := remoteChat(t, local)
_, err := chat.ApplyOperatorTaskAction(ctx, taskactions.Actor{}, 7, taskstate.ActionApprove, taskactions.ReviewResponse{})
if err == nil {
t.Fatalf("ApplyOperatorTaskAction error = nil, want %v", tc.err)
}
if !errors.Is(err, errors.Unwrap(tc.err)) && !errors.Is(err, tc.err) {
t.Fatalf("error %v is not the daemon's sentinel %v", err, tc.err)
}
if !strings.Contains(err.Error(), tc.err.Error()) {
t.Fatalf("error message = %q, want it to carry the daemon's %q", err.Error(), tc.err.Error())
}
})
}
}