feat(controlplane): runtime administration over the State Store's gRPC server
The Web UI and messaging channels now read and write Archie's runtime settings through one versioned, audited control-plane API hosted on the State Store's existing gRPC server: catalog, query, command, watch. Each feature owns its resource, validation and commands; storage only writes versions and audit records. Workflows move with it. Definitions become portable YAML compiled through a closed, typed step registry and stored as a control-plane resource, so the yaegi interpretation paths (internal/gate, wfeval, wfextract, gateeval, gateextract, skillbuild) are deleted rather than carried. The UI process reads definitions as data: the JSON projection lives in internal/domain/workflow/task so a dashboard decodes the stored collection without linking the workflow engine, which cmd/archie-ui's deletion gate forbids. Shipped definitions ride the catalog descriptor rather than a bespoke REST route, keeping the API to its four operations. archie-messaging gains -state-store-target/-state-store-token and reads channel settings from the database, failing closed when the State Store is unreachable: the database is authoritative for those settings.
This commit is contained in:
131 files changed
+8510
-3371
No files matched your search
@@ -21,6 +21,8 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/worktreerpc"
|
||||
)
|
||||
|
||||
const testDefinition = "id: implement\nsteps: []\n"
|
||||
|
||||
func TestTransportContainsNoSharedTaskWorkerTopology(t *testing.T) {
|
||||
source, err := os.ReadFile("transport.go")
|
||||
if err != nil {
|
||||
@@ -111,7 +113,7 @@ func TestSubscribeTasksServesOnlyBootTaskSubject(t *testing.T) {
|
||||
}
|
||||
t.Cleanup(func() { _ = subscription.Close() })
|
||||
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &workflow.Task{ID: 42}, WorktreeGrant: "grant"})
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &workflow.Task{ID: 42}, WorktreeGrant: "grant", WorkflowDefinition: testDefinition})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -151,8 +153,9 @@ func TestSubscribeTasksOwnsWireErrors(t *testing.T) {
|
||||
{name: "null task", payload: []byte(`{"task":null}`), want: "validate taskrun request: task is required"},
|
||||
{name: "nonpositive task ID", payload: []byte(`{"task":{"id":0}}`), want: "validate taskrun request: task ID must be positive, got 0"},
|
||||
{name: "missing worktree grant", payload: []byte(`{"task":{"id":9}}`), want: "validate taskrun request: worktree grant is required"},
|
||||
{name: "payload subject mismatch", payload: []byte(`{"task":{"id":8},"worktree_grant":"grant"}`), want: "validate taskrun request: task ID 8 does not match subject task ID 9"},
|
||||
{name: "handler", payload: []byte(`{"task":{"id":9},"worktree_grant":"grant"}`), want: "run failed", wantHandlerCalls: 1},
|
||||
{name: "missing workflow definition", payload: []byte(`{"task":{"id":9},"worktree_grant":"grant"}`), want: "validate taskrun request: workflow definition is required"},
|
||||
{name: "payload subject mismatch", payload: []byte(`{"task":{"id":8},"worktree_grant":"grant","workflow_definition":"id: implement\nsteps: []\n"}`), want: "validate taskrun request: task ID 8 does not match subject task ID 9"},
|
||||
{name: "handler", payload: []byte(`{"task":{"id":9},"worktree_grant":"grant","workflow_definition":"id: implement\nsteps: []\n"}`), want: "run failed", wantHandlerCalls: 1},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
before := handlerCalls
|
||||
@@ -213,7 +216,7 @@ func TestHandleTaskRejectsSubjectCorrelationErrors(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &workflow.Task{ID: test.requestID}, WorktreeGrant: "grant"})
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &workflow.Task{ID: test.requestID}, WorktreeGrant: "grant", WorkflowDefinition: testDefinition})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -113,6 +113,12 @@ func (s Schedule) firstRun(now time.Time) (time.Time, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s Schedule) FirstRun(now time.Time) (time.Time, error) { return s.firstRun(now) }
|
||||
|
||||
func (s Schedule) NextRun(lastRun time.Time) (time.Time, error) { return s.nextRun(lastRun) }
|
||||
|
||||
func (s Schedule) Resolved() Schedule { return s.resolve() }
|
||||
|
||||
// nextRun returns when the job should fire after a successful run at
|
||||
// lastRun. It is the engine-side counterpart to firstRun: every MarkRun
|
||||
// advances next_run through this function. Interval is fully implemented;
|
||||
|
||||
@@ -8,8 +8,10 @@ import (
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
@@ -22,15 +24,19 @@ import (
|
||||
// as gatewayrpc.Client asserts both messaging.ChatContract and
|
||||
// messaging.SessionStore. See docs/prds/state-store-contract.md §2.
|
||||
type Client struct {
|
||||
client pb.StateStoreServiceClient
|
||||
client pb.StateStoreServiceClient
|
||||
controlPlane controlpb.ControlPlaneServiceClient
|
||||
}
|
||||
|
||||
// NewClient wraps conn's generated client. Close is a no-op: the store
|
||||
// service owns its own DB lifecycle (§11).
|
||||
func NewClient(conn grpc.ClientConnInterface) *Client {
|
||||
return &Client{client: pb.NewStateStoreServiceClient(conn)}
|
||||
return &Client{client: pb.NewStateStoreServiceClient(conn), controlPlane: controlpb.NewControlPlaneServiceClient(conn)}
|
||||
}
|
||||
|
||||
// ControlPlane returns the generic control-plane RPC client on the same authenticated connection.
|
||||
func (c *Client) ControlPlane() controlpb.ControlPlaneServiceClient { return c.controlPlane }
|
||||
|
||||
// Close is a no-op; the remote store service owns its own DB lifecycle.
|
||||
func (c *Client) Close() error { return nil }
|
||||
|
||||
@@ -44,6 +50,7 @@ var (
|
||||
_ storecontract.BindingDispatcher = (*Client)(nil)
|
||||
_ storecontract.BindingTaskCreator = (*Client)(nil)
|
||||
_ storecontract.ConfigSnapshotStore = (*Client)(nil)
|
||||
_ identity.Repository = (*Client)(nil)
|
||||
)
|
||||
|
||||
// Lifecycle
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
func (s *server) ListIdentities(ctx context.Context, _ *pb.ListIdentitiesRequest) (*pb.ListIdentitiesResponse, error) {
|
||||
if s.deps.Identities == nil {
|
||||
return nil, status.Error(codes.Unavailable, "identity store unavailable")
|
||||
}
|
||||
values, err := s.deps.Identities.List(ctx)
|
||||
if err != nil {
|
||||
return nil, identityStatus(err)
|
||||
}
|
||||
out := make([]*pb.Identity, len(values))
|
||||
for i := range values {
|
||||
out[i] = identityProto(values[i])
|
||||
}
|
||||
return &pb.ListIdentitiesResponse{Identities: out}, nil
|
||||
}
|
||||
|
||||
func (s *server) GetIdentity(ctx context.Context, request *pb.GetIdentityRequest) (*pb.GetIdentityResponse, error) {
|
||||
value, err := s.deps.Identities.Get(ctx, identity.IdentityID(request.Id))
|
||||
if errors.Is(err, identity.ErrNotFound) {
|
||||
value, err = s.deps.Identities.ResolveLegacyName(ctx, request.Id)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, identityStatus(err)
|
||||
}
|
||||
return &pb.GetIdentityResponse{Identity: identityProto(value)}, nil
|
||||
}
|
||||
|
||||
func (s *server) CreateIdentity(ctx context.Context, request *pb.CreateIdentityRequest) (*pb.CreateIdentityResponse, error) {
|
||||
value, err := identity.New(identity.IdentityID(request.Id), identity.Kind(request.Kind), request.DisplayName)
|
||||
if err == nil {
|
||||
value, err = s.deps.Identities.Create(ctx, value, identityAudit(request.Audit))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, identityStatus(err)
|
||||
}
|
||||
return &pb.CreateIdentityResponse{Identity: identityProto(value)}, nil
|
||||
}
|
||||
|
||||
func (s *server) RenameIdentity(ctx context.Context, request *pb.RenameIdentityRequest) (*pb.RenameIdentityResponse, error) {
|
||||
value, err := s.applyIdentityCommand(ctx, request.Id, request.ExpectedVersion, request.Audit, identity.Command{Type: identity.CommandRename, DisplayName: request.DisplayName})
|
||||
return &pb.RenameIdentityResponse{Identity: identityProto(value)}, err
|
||||
}
|
||||
|
||||
func (s *server) SuspendIdentity(ctx context.Context, request *pb.SuspendIdentityRequest) (*pb.SuspendIdentityResponse, error) {
|
||||
value, err := s.applyIdentityCommand(ctx, request.Id, request.ExpectedVersion, request.Audit, identity.Command{Type: identity.CommandSuspend})
|
||||
return &pb.SuspendIdentityResponse{Identity: identityProto(value)}, err
|
||||
}
|
||||
|
||||
func (s *server) ReactivateIdentity(ctx context.Context, request *pb.ReactivateIdentityRequest) (*pb.ReactivateIdentityResponse, error) {
|
||||
value, err := s.applyIdentityCommand(ctx, request.Id, request.ExpectedVersion, request.Audit, identity.Command{Type: identity.CommandReactivate})
|
||||
return &pb.ReactivateIdentityResponse{Identity: identityProto(value)}, err
|
||||
}
|
||||
|
||||
func (s *server) RetireIdentity(ctx context.Context, request *pb.RetireIdentityRequest) (*pb.RetireIdentityResponse, error) {
|
||||
value, err := s.applyIdentityCommand(ctx, request.Id, request.ExpectedVersion, request.Audit, identity.Command{Type: identity.CommandRetire})
|
||||
return &pb.RetireIdentityResponse{Identity: identityProto(value)}, err
|
||||
}
|
||||
|
||||
func (s *server) applyIdentityCommand(ctx context.Context, id string, version int64, audit *pb.IdentityAudit, command identity.Command) (identity.Identity, error) {
|
||||
value, err := s.deps.Identities.Apply(ctx, identity.IdentityID(id), version, command, identityAudit(audit))
|
||||
if err != nil {
|
||||
return identity.Identity{}, identityStatus(err)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (c *Client) List(ctx context.Context) ([]identity.Identity, error) {
|
||||
response, err := c.client.ListIdentities(ctx, &pb.ListIdentitiesRequest{})
|
||||
if err != nil {
|
||||
return nil, identityClientError(err)
|
||||
}
|
||||
out := make([]identity.Identity, len(response.Identities))
|
||||
for i := range response.Identities {
|
||||
out[i] = identityValue(response.Identities[i])
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) Get(ctx context.Context, id identity.IdentityID) (identity.Identity, error) {
|
||||
response, err := c.client.GetIdentity(ctx, &pb.GetIdentityRequest{Id: string(id)})
|
||||
if err != nil {
|
||||
return identity.Identity{}, identityClientError(err)
|
||||
}
|
||||
return identityValue(response.Identity), nil
|
||||
}
|
||||
|
||||
func (c *Client) ResolveLegacyName(ctx context.Context, name string) (identity.Identity, error) {
|
||||
return c.Get(ctx, identity.IdentityID(name))
|
||||
}
|
||||
|
||||
func (c *Client) Create(ctx context.Context, value identity.Identity, audit identity.Audit) (identity.Identity, error) {
|
||||
response, err := c.client.CreateIdentity(ctx, &pb.CreateIdentityRequest{Id: string(value.ID), Kind: string(value.Kind), DisplayName: value.DisplayName, Audit: auditProto(audit)})
|
||||
if err != nil {
|
||||
return identity.Identity{}, identityClientError(err)
|
||||
}
|
||||
return identityValue(response.Identity), nil
|
||||
}
|
||||
|
||||
func (c *Client) Apply(ctx context.Context, id identity.IdentityID, version int64, command identity.Command, audit identity.Audit) (identity.Identity, error) {
|
||||
var value *pb.Identity
|
||||
var err error
|
||||
switch command.Type {
|
||||
case identity.CommandRename:
|
||||
response, callErr := c.client.RenameIdentity(ctx, &pb.RenameIdentityRequest{Id: string(id), ExpectedVersion: version, DisplayName: command.DisplayName, Audit: auditProto(audit)})
|
||||
err = callErr
|
||||
if response != nil {
|
||||
value = response.Identity
|
||||
}
|
||||
case identity.CommandSuspend:
|
||||
response, callErr := c.client.SuspendIdentity(ctx, &pb.SuspendIdentityRequest{Id: string(id), ExpectedVersion: version, Audit: auditProto(audit)})
|
||||
err = callErr
|
||||
if response != nil {
|
||||
value = response.Identity
|
||||
}
|
||||
case identity.CommandReactivate:
|
||||
response, callErr := c.client.ReactivateIdentity(ctx, &pb.ReactivateIdentityRequest{Id: string(id), ExpectedVersion: version, Audit: auditProto(audit)})
|
||||
err = callErr
|
||||
if response != nil {
|
||||
value = response.Identity
|
||||
}
|
||||
case identity.CommandRetire:
|
||||
response, callErr := c.client.RetireIdentity(ctx, &pb.RetireIdentityRequest{Id: string(id), ExpectedVersion: version, Audit: auditProto(audit)})
|
||||
err = callErr
|
||||
if response != nil {
|
||||
value = response.Identity
|
||||
}
|
||||
default:
|
||||
return identity.Identity{}, identity.ErrInvalid
|
||||
}
|
||||
if err != nil {
|
||||
return identity.Identity{}, identityClientError(err)
|
||||
}
|
||||
return identityValue(value), nil
|
||||
}
|
||||
|
||||
func identityProto(value identity.Identity) *pb.Identity {
|
||||
return &pb.Identity{Id: string(value.ID), Kind: string(value.Kind), DisplayName: value.DisplayName, Lifecycle: string(value.Lifecycle), Version: value.Version, CreatedAt: timestamppb.New(value.CreatedAt), UpdatedAt: timestamppb.New(value.UpdatedAt)}
|
||||
}
|
||||
|
||||
func identityValue(value *pb.Identity) identity.Identity {
|
||||
if value == nil {
|
||||
return identity.Identity{}
|
||||
}
|
||||
return identity.Identity{ID: identity.IdentityID(value.Id), Kind: identity.Kind(value.Kind), DisplayName: value.DisplayName, Lifecycle: identity.Lifecycle(value.Lifecycle), Version: value.Version, CreatedAt: value.CreatedAt.AsTime(), UpdatedAt: value.UpdatedAt.AsTime()}
|
||||
}
|
||||
|
||||
func identityAudit(value *pb.IdentityAudit) identity.Audit {
|
||||
if value == nil {
|
||||
return identity.Audit{}
|
||||
}
|
||||
return identity.Audit{ActorID: identity.IdentityID(value.ActorId), Source: value.Source, RequestID: value.RequestId}
|
||||
}
|
||||
|
||||
func auditProto(value identity.Audit) *pb.IdentityAudit {
|
||||
return &pb.IdentityAudit{ActorId: string(value.ActorID), Source: value.Source, RequestId: value.RequestID}
|
||||
}
|
||||
|
||||
func identityStatus(err error) error {
|
||||
switch {
|
||||
case errors.Is(err, identity.ErrNotFound):
|
||||
return status.Error(codes.NotFound, err.Error())
|
||||
case errors.Is(err, identity.ErrConflict):
|
||||
return status.Error(codes.Aborted, err.Error())
|
||||
case errors.Is(err, identity.ErrInvalid), errors.Is(err, identity.ErrIllegalTransition), errors.Is(err, identity.ErrSystemImmutable):
|
||||
return status.Error(codes.FailedPrecondition, err.Error())
|
||||
default:
|
||||
return status.Error(codes.Internal, "internal state store error")
|
||||
}
|
||||
}
|
||||
|
||||
func identityClientError(err error) error {
|
||||
switch status.Code(err) {
|
||||
case codes.NotFound:
|
||||
return identity.ErrNotFound
|
||||
case codes.Aborted:
|
||||
return identity.ErrConflict
|
||||
case codes.FailedPrecondition:
|
||||
return errors.Join(identity.ErrInvalid, err)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,10 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
@@ -55,6 +57,8 @@ const taskLogChunkBytes = 256 << 10
|
||||
// codes.Unavailable) exactly as their daemon-side consumers already treat a
|
||||
// nil Mappings/Bindings/BindingDispatcher/BindingTaskCreator as "disabled".
|
||||
type Deps struct {
|
||||
ControlPlane controlpb.ControlPlaneServiceServer
|
||||
Identities identity.Repository
|
||||
Grants *TaskGrants
|
||||
Tasks storecontract.TaskStore
|
||||
Captures storecontract.CaptureStore
|
||||
@@ -87,6 +91,9 @@ func RegisterServer(registrar grpc.ServiceRegistrar, deps Deps) {
|
||||
deps.Log = slog.New(slog.DiscardHandler)
|
||||
}
|
||||
pb.RegisterStateStoreServiceServer(registrar, &server{deps: deps})
|
||||
if deps.ControlPlane != nil {
|
||||
controlpb.RegisterControlPlaneServiceServer(registrar, deps.ControlPlane)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) logErr(rpc string, err error) error {
|
||||
|
||||
Reference in new issue
Block a user