fix(controlplanerpc): attribute chat workflow drafts so saves succeed

This commit is contained in:
sam committed 2026-10-26 18:24:46 +11:00
1 parent 877d45f275
commit 394f396b6b
4 files changed
+125 -3

No files matched your search

@@ -2,6 +2,8 @@ package controlplanerpc
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
@@ -10,6 +12,7 @@ import (
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
@@ -105,8 +108,19 @@ func (w Workflows) Draft(ctx context.Context, id string) (string, bool, error) {
}
// PutDraft writes a workflow's draft, the same document the editor saves to,
// so the operator reviews it there before publishing.
// so the operator reviews it there before publishing. The revision is
// attributed to the caller the gateway authenticated, never to an actor the
// model supplied, and each save carries its own request ID so it lands as a
// revision rather than replaying an earlier one.
func (w Workflows) PutDraft(ctx context.Context, id, src string) error {
principal, ok := access.PrincipalFromContext(ctx)
if !ok || principal.IdentityID == "" {
return errors.New("save workflow draft: no caller to attribute the draft to")
}
requestID, err := newChatRequestID()
if err != nil {
return fmt.Errorf("save workflow draft: %w", err)
}
drafts, version, err := w.drafts(ctx)
if err != nil {
return err
@@ -122,13 +136,28 @@ func (w Workflows) PutDraft(ctx context.Context, id, src string) error {
if err != nil {
return err
}
_, err = w.rpc.Command(ctx, &pb.CommandRequest{Kind: workflowDraftsKind, Command: "replace", ValueJson: value, ExpectedVersion: version, Source: "chat"})
_, err = w.rpc.Command(ctx, &pb.CommandRequest{
Kind: workflowDraftsKind, Command: "replace", ValueJson: value, ExpectedVersion: version,
Actor: string(principal.IdentityID), Source: "chat", RequestId: requestID,
})
if err != nil {
return fmt.Errorf("save workflow draft: %w", err)
}
return nil
}
// newChatRequestID mints the idempotency key of one chat-authored write. It
// fails rather than falling back to a weaker value: a repeated request ID is
// replayed as the earlier revision, which would report a save that never
// happened.
func newChatRequestID() (string, error) {
var value [16]byte
if _, err := rand.Read(value[:]); err != nil {
return "", err
}
return "chat-" + hex.EncodeToString(value[:]), nil
}
// Resource returns one control-plane document's value in the caller's org.
func (w Workflows) Resource(ctx context.Context, kind string) (json.RawMessage, error) {
response, err := w.rpc.Query(ctx, &pb.QueryRequest{Kind: kind})
@@ -0,0 +1,89 @@
package controlplanerpc
import (
"context"
"strings"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
)
// fakeControlPlane records the commands it is sent and refuses a replace the
// way the control plane does when a field it audits is missing.
type fakeControlPlane struct {
pb.ControlPlaneServiceClient
commands []*pb.CommandRequest
}
func (*fakeControlPlane) Query(context.Context, *pb.QueryRequest, ...grpc.CallOption) (*pb.QueryResponse, error) {
return &pb.QueryResponse{Resource: &pb.Resource{ValueJson: []byte(`{"drafts":[]}`), Version: 3}}, nil
}
func (f *fakeControlPlane) Command(_ context.Context, request *pb.CommandRequest, _ ...grpc.CallOption) (*pb.CommandResponse, error) {
f.commands = append(f.commands, request)
if request.GetActor() == "" || request.GetSource() == "" || request.GetRequestId() == "" {
return nil, status.Error(codes.InvalidArgument, "actor, source, and request ID are required")
}
return &pb.CommandResponse{}, nil
}
func TestPutDraftAttributesTheCaller(t *testing.T) {
for _, tt := range []struct {
name string
principal *access.Principal
saves int
wantErr bool
}{
{"authenticated caller is the actor", &access.Principal{IdentityID: "alice"}, 1, false},
{"no principal", nil, 1, true},
{"each save is its own request", &access.Principal{IdentityID: "alice"}, 2, false},
} {
t.Run(tt.name, func(t *testing.T) {
client := &fakeControlPlane{}
ctx := t.Context()
if tt.principal != nil {
ctx = access.WithPrincipal(ctx, *tt.principal)
}
workflows := NewWorkflows(client)
for range tt.saves {
err := workflows.PutDraft(ctx, "report", "id: report\n")
if (err != nil) != tt.wantErr {
t.Fatalf("PutDraft error = %v, want error %t", err, tt.wantErr)
}
}
if tt.wantErr {
if len(client.commands) != 0 {
t.Fatalf("issued %d commands, want none", len(client.commands))
}
return
}
if len(client.commands) != tt.saves {
t.Fatalf("issued %d commands, want %d", len(client.commands), tt.saves)
}
requestIDs := make(map[string]bool, len(client.commands))
for _, command := range client.commands {
if got, want := command.GetActor(), string(tt.principal.IdentityID); got != want {
t.Errorf("actor = %q, want %q", got, want)
}
if command.GetSource() == "" || command.GetRequestId() == "" {
t.Errorf("command %+v lacks a source or request ID", command)
}
if requestIDs[command.GetRequestId()] {
t.Errorf("request ID %q repeats an earlier save", command.GetRequestId())
}
requestIDs[command.GetRequestId()] = true
if command.GetExpectedVersion() != 3 {
t.Errorf("expected version = %d, want 3", command.GetExpectedVersion())
}
if !strings.Contains(string(command.GetValueJson()), `"id":"report"`) {
t.Errorf("value = %s, want the draft for report", command.GetValueJson())
}
}
})
}
}