fix(controlplanerpc): attribute chat workflow drafts so saves succeed
This commit is contained in:
1 parent
4dca007003
commit
e85719f2d2
4 files changed
+125
-3
No files matched your search
@@ -5,6 +5,10 @@ release's per-component sections are labelled beneath its heading.
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### archied
|
||||
|
||||
- Chat saves the workflow draft it writes with `workflow_draft` instead of handing over YAML to paste: the draft lands in the workflow editor for review, attributed to the signed-in account.
|
||||
|
||||
## [1.55.0] - 2026-10-16
|
||||
|
||||
### archied
|
||||
|
||||
@@ -7,7 +7,7 @@ Accounts and organization:
|
||||
Creating a workflow:
|
||||
1. Open /workflows and choose New workflow. The new definition is an unsaved draft with an id and empty steps.
|
||||
2. Use YAML to edit its id, or the canvas to add and select steps. Select Starts when on the canvas to open Workflow settings. Set Runs as to an existing active agent identity. Works on selects Repository, If given, or None.
|
||||
3. Before writing YAML, read a similar workflow (workflow_list, workflow_get) and the step catalog (workflow_step_types). Run workflow_validate on the YAML and fix every problem it reports until it is valid, then give the operator the YAML to paste into the editor's YAML view and save. A repository planning workflow normally prepares the repository, then uses agent.run with read_only: true and a mission asking it to inspect the code and return a plan. Keep forge writes, commits, implementation steps and automatic implementation handoffs out of a planning-only workflow. The shipped feasibility workflow also closes issues or hands off after approval: it is not a planning-only template unchanged.
|
||||
3. Before writing YAML, read a similar workflow (workflow_list, workflow_get) and the step catalog (workflow_step_types). Run workflow_validate on the YAML and fix every problem it reports until it is valid, then save it with workflow_draft so the operator reviews and publishes it in the editor. A repository planning workflow normally prepares the repository, then uses agent.run with read_only: true and a mission asking it to inspect the code and return a plan. Keep forge writes, commits, implementation steps and automatic implementation handoffs out of a planning-only workflow. The shipped feasibility workflow also closes issues or hands off after approval: it is not a planning-only template unchanged.
|
||||
4. Save the definition. Test runs the draft once with forge writes/pushes skipped; commands and tools still run, so a test is not a side-effect-free simulation. Run starts the saved version. History, Performance and Runs show revisions and execution evidence.
|
||||
5. Manual workflows need no automatic trigger. Bind an event creates an event binding from a captured example and maps payload fields to declared inputs. Existing bindings can be paused or removed. Playbooks are edited at /org/playbooks; workflow calls/handoffs are edited in the workflow that invokes them. Built-in issue-label routes are marked Built-in; they cannot be removed in the event-binding editor. Disable the workflow to prevent automatic execution.
|
||||
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user