Third review pass on the step-vocabulary shape, one fix per finding. P1, roots unpinned. The agreement test built its own manager and injected it, so it pinned the resolution sites and not the roots that decide the provider set; a root regressed to workflow.NewManager() (no shipped stages) left go test green. archied now resolves through one unit, stepVocabulary (step_vocabulary.go), and agentworker's productionWorkerDependencies is exercised directly: internal/app/agentworker/step_vocabulary_test.go and internal/app/archied/step_vocabulary_test.go assert each root's manager resolves the shared provider set's shipped stages and accepts a provider registered through it. RunStateStore and openStateStoreAdapter own a SQLite file and a gRPC dial, so their calls are pinned by source position, the way composition_order_test.go pins the memory-engine ordering. P2-B, dead vocabulary. archie-messaging registered a step vocabulary it never resolved (its only control-plane call is RuntimeChatConfig) and could fail startup on it before the State Store target guard. The workflow-definitions surface is now its own type, controlplane.WorkflowDefinitionsClient, so the vocabulary is a constructor dependency of the one surface that resolves step types and of nothing else. The messaging root no longer imports workflowsteps. P2-C, nil manager. A nil *workflow.Manager dereferenced on every resolution path. stepRegistry and CompilePinnedWorkflow now refuse with an error naming the wiring mistake; TestNilStepVocabularyFailsClosedAtEveryResolutionSite pins the refusal. Claim-vs-code corrections: agreement is stated as within one build (vocabulary.go, stepRegistry, openStateStoreControlPlane, openStateStoreAdapter); the Yaegi claim now says no bridge to StepFactory has been built and cites internal/secret/secretextract; the provider set names its three roots; Manager.Register's ordering is documented as a constraint with its consequence, and the shape rule no longer claims to reserve shipped stage names; the shipped-stage test compares against the builtin factory's own stage.
213 lines
7.6 KiB
Go
213 lines
7.6 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"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/agent"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/cronstore"
|
|
)
|
|
|
|
// Client is the control-plane client for every resource that does not resolve
|
|
// a workflow step type: catalog, history, settings, personas, schedules. It
|
|
// carries no step vocabulary, so a process that never reads or writes a
|
|
// workflow definition -- archie-messaging, which only loads channel settings --
|
|
// needs no provider set and cannot be failed by one.
|
|
type Client struct{ rpc pb.ControlPlaneServiceClient }
|
|
|
|
func NewClient(conn grpc.ClientConnInterface) *Client {
|
|
return &Client{rpc: pb.NewControlPlaneServiceClient(conn)}
|
|
}
|
|
|
|
func NewRPCClient(client pb.ControlPlaneServiceClient) *Client { return &Client{rpc: client} }
|
|
|
|
// WorkflowDefinitionsClient reads and replaces the workflow-definitions
|
|
// resource, the one control-plane surface whose stored values name workflow
|
|
// step types. Resolving those names needs the step vocabulary the process
|
|
// registered at its composition root, so the vocabulary is a constructor
|
|
// dependency of this surface and of no other: a caller that cannot reach
|
|
// workflow definitions cannot ask for one.
|
|
type WorkflowDefinitionsClient struct {
|
|
rpc pb.ControlPlaneServiceClient
|
|
steps workflow.StepRegistry
|
|
}
|
|
|
|
func NewWorkflowDefinitionsClient(client pb.ControlPlaneServiceClient, steps *workflow.Manager) (*WorkflowDefinitionsClient, error) {
|
|
registry, err := stepRegistry(steps)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &WorkflowDefinitionsClient{rpc: client, steps: registry}, nil
|
|
}
|
|
|
|
func (c *Client) Catalog(ctx context.Context) ([]*pb.ResourceDescriptor, error) {
|
|
response, err := c.rpc.Catalog(ctx, &pb.CatalogRequest{})
|
|
if err != nil {
|
|
return nil, clientError(err)
|
|
}
|
|
return response.Resources, nil
|
|
}
|
|
|
|
func (c *WorkflowDefinitionsClient) WorkflowDefinitions(ctx context.Context) (workflow.WorkflowDefinitionCollection, int64, error) {
|
|
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: WorkflowDefinitionsKind})
|
|
if err != nil {
|
|
return workflow.WorkflowDefinitionCollection{}, 0, clientError(err)
|
|
}
|
|
definitions, err := workflow.DecodeDefinitionCollection(response.Resource.ValueJson, c.steps)
|
|
return definitions, response.Resource.Version, err
|
|
}
|
|
|
|
// ReplaceWorkflowDefinitions replaces the collection. Passing
|
|
// workflow.ShippedDefinitions() restores all shipped definitions while keeping
|
|
// the previous override in resource history.
|
|
func (c *WorkflowDefinitionsClient) ReplaceWorkflowDefinitions(ctx context.Context, definitions workflow.WorkflowDefinitionCollection, expectedVersion int64, actor, source, requestID string) (int64, error) {
|
|
value, err := encodeWorkflowDefinitions(definitions, c.steps)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: WorkflowDefinitionsKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
|
|
if err != nil {
|
|
return 0, clientError(err)
|
|
}
|
|
return response.Resource.Version, nil
|
|
}
|
|
|
|
func (c *Client) WorkflowExecutionSettings(ctx context.Context) (workflow.ExecutionSettings, int64, error) {
|
|
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: WorkflowExecutionSettingsKind})
|
|
if err != nil {
|
|
return workflow.ExecutionSettings{}, 0, clientError(err)
|
|
}
|
|
settings, err := decodeSettings(response.Resource.ValueJson)
|
|
return settings, response.Resource.Version, err
|
|
}
|
|
|
|
func (c *Client) ReplaceWorkflowExecutionSettings(ctx context.Context, settings workflow.ExecutionSettings, expectedVersion int64, actor, source, requestID string) (int64, error) {
|
|
if err := settings.Validate(); err != nil {
|
|
return 0, fmt.Errorf("%w: %w", ErrValidation, err)
|
|
}
|
|
value, err := encodeSettings(settings)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: WorkflowExecutionSettingsKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
|
|
if err != nil {
|
|
return 0, clientError(err)
|
|
}
|
|
return response.Resource.Version, nil
|
|
}
|
|
|
|
func (c *Client) WatchWorkflowExecutionSettings(ctx context.Context, afterVersion int64) (<-chan AppliedSettings, error) {
|
|
stream, err := c.rpc.Watch(ctx, &pb.WatchRequest{Kind: WorkflowExecutionSettingsKind, AfterVersion: afterVersion})
|
|
if err != nil {
|
|
return nil, clientError(err)
|
|
}
|
|
out := make(chan AppliedSettings)
|
|
go func() {
|
|
defer close(out)
|
|
for {
|
|
response, err := stream.Recv()
|
|
if err != nil {
|
|
if !errors.Is(err, io.EOF) && ctx.Err() == nil {
|
|
out <- AppliedSettings{Err: clientError(err)}
|
|
}
|
|
return
|
|
}
|
|
settings, err := decodeSettings(response.Resource.ValueJson)
|
|
out <- AppliedSettings{Settings: settings, Version: response.Resource.Version, Err: err}
|
|
}
|
|
}()
|
|
return out, nil
|
|
}
|
|
|
|
type AppliedSettings struct {
|
|
Settings workflow.ExecutionSettings
|
|
Version int64
|
|
Err error
|
|
}
|
|
|
|
func (c *Client) Personas(ctx context.Context) (agent.PersonaCollection, int64, error) {
|
|
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: PersonasKind})
|
|
if err != nil {
|
|
return agent.PersonaCollection{}, 0, clientError(err)
|
|
}
|
|
collection, err := decodePersonas(response.Resource.ValueJson)
|
|
return collection, response.Resource.Version, err
|
|
}
|
|
|
|
type AppliedPersonas struct {
|
|
Collection agent.PersonaCollection
|
|
Version int64
|
|
Err error
|
|
}
|
|
|
|
func (c *Client) WatchPersonas(ctx context.Context, afterVersion int64) (<-chan AppliedPersonas, error) {
|
|
stream, err := c.rpc.Watch(ctx, &pb.WatchRequest{Kind: PersonasKind, AfterVersion: afterVersion})
|
|
if err != nil {
|
|
return nil, clientError(err)
|
|
}
|
|
out := make(chan AppliedPersonas)
|
|
go func() {
|
|
defer close(out)
|
|
for {
|
|
response, err := stream.Recv()
|
|
if err != nil {
|
|
if !errors.Is(err, io.EOF) && ctx.Err() == nil {
|
|
out <- AppliedPersonas{Err: clientError(err)}
|
|
}
|
|
return
|
|
}
|
|
collection, err := decodePersonas(response.Resource.ValueJson)
|
|
out <- AppliedPersonas{Collection: collection, Version: response.Resource.Version, Err: err}
|
|
}
|
|
}()
|
|
return out, nil
|
|
}
|
|
|
|
func (c *Client) Schedules(ctx context.Context) ([]cronstore.JobSpec, int64, error) {
|
|
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: SchedulesKind})
|
|
if err != nil {
|
|
return nil, 0, clientError(err)
|
|
}
|
|
var jobs []cronstore.JobSpec
|
|
if err := json.Unmarshal(response.Resource.ValueJson, &jobs); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return jobs, response.Resource.Version, nil
|
|
}
|
|
|
|
func (c *Client) ReplaceSchedules(ctx context.Context, jobs []cronstore.JobSpec, expectedVersion int64, actor, source, requestID string) (int64, error) {
|
|
value, err := json.Marshal(jobs)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: SchedulesKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
|
|
if err != nil {
|
|
return 0, clientError(err)
|
|
}
|
|
return response.Resource.Version, nil
|
|
}
|
|
|
|
func clientError(err error) error {
|
|
switch status.Code(err) {
|
|
case codes.InvalidArgument:
|
|
return fmt.Errorf("%w: %w", ErrValidation, err)
|
|
case codes.NotFound:
|
|
return fmt.Errorf("%w: %w", ErrNotFound, err)
|
|
case codes.Aborted:
|
|
return fmt.Errorf("%w: %w", ErrVersionConflict, err)
|
|
case codes.Unavailable:
|
|
return fmt.Errorf("%w: %w", ErrUnavailable, err)
|
|
default:
|
|
return err
|
|
}
|
|
}
|