diff --git a/config.example.toml b/config.example.toml index 605924ef2..afd30c72c 100644 --- a/config.example.toml +++ b/config.example.toml @@ -20,7 +20,6 @@ # workflow_routing_file = "~/.local/share/archie/workflow-routing.yaml" # kind->workflow rebinding (bug/feature/bootstrap) # workflow_labels_file = "~/.local/share/archie/workflow-labels.yaml" # arbitrary forge label -> workflow (EDA playbook slice 2) # playbook_dirs = ["~/.local/share/archie/workflows"] # list of dirs of *.yaml binding files, merged with the two above (slice 3); a key bound twice across sources is a load failure -# eda_playbook_dir = "~/.local/share/archie/eda-playbooks" # rich EDA playbook documents (one trigger; either a CEL-gated workflow action or a list of module actions); a matching workflow playbook decides the task's workflow ahead of the three binding sources above; distinct from playbook_dirs poll_interval = "60s" label = "archie" diff --git a/examples/eda-playbooks/bug-tdd.yaml b/examples/eda-playbooks/bug-tdd.yaml deleted file mode 100644 index 138387803..000000000 --- a/examples/eda-playbooks/bug-tdd.yaml +++ /dev/null @@ -1,48 +0,0 @@ -# Example EDA playbook document (t2db.15 slice). -# -# Install: copy this file into the configured eda_playbook_dir (default -# ~/.config/archie/eda-playbooks) and restart archied. It is loaded at -# startup with the reject-at-load rule: any malformed playbook, a mixed -# workflow/module action shape, or a `when`/args compile failure aborts -# startup with a reported error -- a partially-valid set never runs. -# -# This file is the workflow playbook shape: exactly one action, position: -# workflow. Action playbooks (one or more module actions) load and type-check -# but do not execute yet. -# -# A trigger matches on either axis, and both are checked when both are given: -# -# kind: the closed workintake routing vocabulary -- bug, feature, -# bootstrap, default. It is closed because a kind also addresses the -# task's NATS subject; anything else is rejected at load. Set it -# when you want the rule to follow archie's own classification. -# labels: any labels you like, including ones no kind recognises -# ("ops::security-review"). All of them must be present on the -# issue. This is the axis an instance defines for itself: it binds -# your own taxonomy to a workflow with no code change. See -# label-trigger.yaml. The `when` condition is a CEL -# expression over `event`. -# -# A matching playbook decides the task's workflow ahead of the kind/label -# binding files, and a workflow it names that is not defined parks the task -# rather than falling back. For a forge-discovered task, `event` carries: -# -# kind routing kind the labels produced (bug/feature/bootstrap/default) -# labels the issue's labels, as a list -# owner repo owner -# repo repo name -# number issue number -# title issue title -# body issue body -# source "forge" or "chat" -# -# Reading a field that is not in that set is an evaluation error, which the -# coordinator treats as the condition being false (the playbook is skipped). - -trigger: - kind: bug - -actions: - - position: workflow - workflow: tdd - when: '"regression" in event.labels' diff --git a/examples/eda-playbooks/label-trigger.yaml b/examples/eda-playbooks/label-trigger.yaml deleted file mode 100644 index fd04b9bcb..000000000 --- a/examples/eda-playbooks/label-trigger.yaml +++ /dev/null @@ -1,19 +0,0 @@ -# Example EDA playbook triggering on an instance-defined label. -# -# The kind axis (bug/feature/bootstrap/default) is archie's own closed -# classification. This file is the other axis: a label set of your own -# choosing, matched as a subset of the issue's labels. Every label listed here -# must be present on the issue for the playbook to fire. -# -# A matching playbook decides the task's workflow ahead of the -# workflow_routing_file, workflow_labels_file and playbook_dirs bindings. The -# workflow it names must be a defined workflow; naming one that is not parks -# the task with a reported error rather than quietly running a different one. - -trigger: - labels: - - ops::security-review - -actions: - - position: workflow - workflow: feasibility diff --git a/examples/profiles/Dockerfile.agent-net b/examples/profiles/Dockerfile.agent-net index ddc3ade22..fb3a5df57 100644 --- a/examples/profiles/Dockerfile.agent-net +++ b/examples/profiles/Dockerfile.agent-net @@ -1,6 +1,6 @@ # An agent profile image: the archie-agent image plus network lookup tools, # for a workflow that investigates addresses rather than code. Select it with -# [containers.profiles.network-investigator] image = "archie-agent-net:local". +# the network-investigator profile package (archipelago: profiles/network-investigator). # # Build: docker compose build agent-net ARG AGENT_IMAGE=ghcr.io/samcharles93/archie-agent:latest diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 188509e58..511790f00 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -33,8 +33,6 @@ import ( "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/agent" "github.com/samcharles93/archie-core/internal/domain/applystatus" - "github.com/samcharles93/archie-core/internal/domain/eda/module" - "github.com/samcharles93/archie-core/internal/domain/eda/playbook" "github.com/samcharles93/archie-core/internal/domain/health" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/presence" @@ -88,10 +86,6 @@ type boot struct { forgeClient forge.Forge token string - modules *module.ModuleRegistry - - playbooks *playbook.Store - // stateStore is the State Store contract adapter every daemon and gateway // store consumer depends on. It is ALWAYS the remote *staterpc.Client // dialed to the standalone archie-state-store gRPC service at @@ -479,10 +473,6 @@ func (b *boot) loadWorkflows() error { if err := b.loadWorkflowRouting(cfg, log); err != nil { return err } - b.modules = module.New() - if err := b.loadEDAPlaybooks(cfg, log); err != nil { - return err - } log.Info("workflow step registry built", "shipped_workflows", len(workflow.ShippedDefinitions().Definitions)) return nil @@ -524,24 +514,6 @@ func (b *boot) loadWorkflowRouting(cfg config.Config, log *slog.Logger) error { return nil } -// loadEDAPlaybooks loads the EDA playbook documents: trigger + workflow-kind -// actions and module-kind action playbooks with CEL when conditions and args -// values. Loaded at startup with the same reject-at-load rule -- any malformed -// playbook, mixed/unsupported action shape, unknown module kind, when compile -// failure, or args key failure aborts startup, matching the routing-file load -// pattern (not degrade-and-skip). A nonexistent dir is an empty store. Action -// playbooks run through the daemon once buildDaemon wires the dispatch ledger. -func (b *boot) loadEDAPlaybooks(cfg config.Config, log *slog.Logger) error { - var err error - b.playbooks, err = playbook.Load(cfg.EDAPlaybookDir, b.modules) - if err != nil { - log.Error("eda playbook load failed", "dir", cfg.EDAPlaybookDir, "err", err) - return err - } - log.Info("eda playbooks loaded", "dir", cfg.EDAPlaybookDir, "playbooks", len(b.playbooks.Playbooks)) - return nil -} - func (b *boot) buildWorktreeManager() { cfg := b.cfg b.trees = &worktree.Manager{ @@ -644,7 +616,7 @@ func (b *boot) buildDaemon() { LabelWorkflows: b.labelWorkflows, WorkflowDefinitions: b.workflowDefinitions, WorkflowEnablement: b.controlPlane, - Playbooks: b.playbooks, + Playbooks: controlplane.NewLivePlaybooks(b.controlPlaneRPC, b.log), } if identities, ok := b.stateStore.(identity.Repository); ok { @@ -680,19 +652,15 @@ func (b *boot) buildDaemon() { b.d.Denials = denials } } - b.d.PlaybookLedger = playbookLedger(b.stateStore, b.playbooks, b.log) + b.d.PlaybookLedger = playbookLedger(b.stateStore, b.log) b.setupForgeWebhook() } -func playbookLedger(source any, playbooks *playbook.Store, log *slog.Logger) storecontract.PlaybookDispatcher { +func playbookLedger(source any, log *slog.Logger) storecontract.PlaybookDispatcher { if pd, ok := source.(storecontract.PlaybookDispatcher); ok { return pd } - for _, pb := range playbooks.Playbooks { - if pb.IsActionPlaybook() { - log.Warn("eda action playbook will not run: no dispatch ledger is wired", "playbook", pb.ID) - } - } + log.Warn("eda action playbooks will not run: no dispatch ledger is wired") return nil } diff --git a/internal/app/archieplaybooks/command.go b/internal/app/archieplaybooks/command.go index 5699dedda..65a18879f 100644 --- a/internal/app/archieplaybooks/command.go +++ b/internal/app/archieplaybooks/command.go @@ -44,7 +44,7 @@ func runLint(args []string, stderr io.Writer) int { flags.SetOutput(stderr) var dirs multiFlag flags.Var(&dirs, "dir", "routing binding directory to lint (repeatable)") - edaDir := flags.String("eda-dir", "", "EDA playbook directory to lint (the daemon's eda_playbook_dir)") + edaDir := flags.String("eda-dir", "", "EDA playbook directory to lint") if err := flags.Parse(args); err != nil { return 2 // flag.ContinueOnError already printed the message } diff --git a/internal/app/archieplaybooks/lint.go b/internal/app/archieplaybooks/lint.go index 048fb3a31..9f47ecf59 100644 --- a/internal/app/archieplaybooks/lint.go +++ b/internal/app/archieplaybooks/lint.go @@ -37,8 +37,8 @@ func Lint(dirs []string, stderr io.Writer) Result { return Result{ExitCode: 0, Findings: nil} } -// LintEDA validates an EDA playbook directory (the daemon's eda_playbook_dir) -// with playbook.Load, the loader the daemon runs at startup. +// LintEDA validates a directory of EDA playbook documents with the compiler +// the eda-playbooks resource validates against. func LintEDA(dir string, stderr io.Writer) Result { if _, err := playbook.Load(dir, module.New()); err != nil { fmt.Fprintln(stderr, err) diff --git a/internal/app/controlplane/definitions.go b/internal/app/controlplane/definitions.go index 20aee748e..261edb0e8 100644 --- a/internal/app/controlplane/definitions.go +++ b/internal/app/controlplane/definitions.go @@ -8,7 +8,7 @@ import ( // Schemas are not declared here: each Definition names its document type, and // Definition.Descriptor derives the JSON Schema from it (internal/app/controlplane/schema.go). func builtinDefinitions(steps workflow.StepRegistry) []Definition { - definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), workflowEnablementDefinition(), personaDefinition(), scheduleDefinition(), agentProfileDefinition(), credentialBindingsDefinition(), reviewSettingsDefinition()} + definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), workflowEnablementDefinition(), personaDefinition(), scheduleDefinition(), agentProfileDefinition(), playbooksDefinition(), credentialBindingsDefinition(), reviewSettingsDefinition()} definitions = append(definitions, modelDefinitions()...) definitions = append(definitions, operationalDefinitions()...) return definitions diff --git a/internal/app/controlplane/package_projection.go b/internal/app/controlplane/package_projection.go new file mode 100644 index 000000000..417442fe7 --- /dev/null +++ b/internal/app/controlplane/package_projection.go @@ -0,0 +1,47 @@ +package controlplane + +import ( + "context" + "errors" + "fmt" + "strconv" + "time" + + "github.com/samcharles93/archie-core/internal/domain/storecontract" +) + +// projectDocument edits one org resource on behalf of an installed package. +// Each attempt re-reads the stored document and re-applies edit, so an +// operator edit made during the projection is folded in, and only a +// persistently moving document refuses it. edit receives nil for a resource +// the org never wrote. +func projectDocument(ctx context.Context, store ResourceStore, orgID, kind, source, requestPrefix string, edit func(current []byte) ([]byte, error)) error { + requestID := requestPrefix + ":" + strconv.FormatInt(time.Now().UTC().UnixNano(), 36) + for attempt := 0; ; attempt++ { + var current []byte + var version int64 + resource, err := store.Resource(ctx, orgID, kind) + switch { + case errors.Is(err, storecontract.ErrResourceNotFound): + case err != nil: + return err + default: + current, version = resource.Value, resource.Version + } + value, err := edit(current) + if err != nil { + return err + } + _, err = store.PutResource(ctx, storecontract.ResourceWrite{ + OrgID: orgID, Kind: kind, Value: value, ExpectedVersion: version, + Actor: projectionActor, Source: source, RequestID: requestID, + }) + if errors.Is(err, storecontract.ErrResourceVersionConflict) && attempt < projectWriteAttempts-1 { + continue + } + if err != nil { + return fmt.Errorf("write %s: %w", kind, err) + } + return nil + } +} diff --git a/internal/app/controlplane/playbooks.go b/internal/app/controlplane/playbooks.go new file mode 100644 index 000000000..59106454f --- /dev/null +++ b/internal/app/controlplane/playbooks.go @@ -0,0 +1,160 @@ +package controlplane + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "path" + "slices" + "strings" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/eda/module" + "github.com/samcharles93/archie-core/internal/domain/eda/playbook" + "github.com/samcharles93/archie-core/internal/domain/storepkg" +) + +// PlaybooksKind is the control-plane resource holding the org's EDA playbook +// documents. +const PlaybooksKind = "eda-playbooks" + +// PlaybookCollection is the eda-playbooks document: playbook sources under +// their stable ids. +type PlaybookCollection struct { + Playbooks []PlaybookEntry `json:"playbooks"` +} + +type PlaybookEntry struct { + ID string `json:"id"` + YAML string `json:"yaml"` +} + +func playbooksDefinition() Definition { + return Definition{ + Kind: PlaybooksKind, + Title: "EDA playbooks", + Document: PlaybookCollection{}, + ApplyMode: "live", + Seed: func(config.Config) any { return PlaybookCollection{Playbooks: []PlaybookEntry{}} }, + Validate: func(input []byte) error { + return validateAs(input, func(c PlaybookCollection) error { + _, err := CompilePlaybooks(c) + return err + }) + }, + } +} + +// CompilePlaybooks compiles a collection against the module vocabulary. Any +// invalid playbook fails the set. +func CompilePlaybooks(c PlaybookCollection) (*playbook.Store, error) { + docs := make([]playbook.Document, 0, len(c.Playbooks)) + seen := map[string]bool{} + for _, entry := range c.Playbooks { + if entry.ID == "" || seen[entry.ID] { + return nil, fmt.Errorf("%w: playbook id %q is empty or repeated", ErrValidation, entry.ID) + } + seen[entry.ID] = true + docs = append(docs, playbook.Document{ID: entry.ID, YAML: []byte(entry.YAML)}) + } + store, err := playbook.Compile(docs, module.New()) + if err != nil { + return nil, fmt.Errorf("%w: %w", ErrValidation, err) + } + return store, nil +} + +// PlaybookProjector merges an installed package's playbooks into the org's +// eda-playbooks resource. Ids owned by an operator or another package are +// refused. +type PlaybookProjector struct { + store ResourceStore + ledger storepkg.ProjectionLedger +} + +var _ storepkg.FamilyProjector = (*PlaybookProjector)(nil) + +func NewPlaybookPackageProjector(resources ResourceStore, ledger storepkg.ProjectionLedger) (*PlaybookProjector, error) { + if resources == nil || ledger == nil { + return nil, errors.New("control plane: playbook projection needs a resource store and a contributions ledger") + } + return &PlaybookProjector{store: resources, ledger: ledger}, nil +} + +func (p *PlaybookProjector) Apply(ctx context.Context, orgID, name, digest string, contents map[string][]byte) error { + incoming := make([]PlaybookEntry, 0, len(contents)) + recorded := make([]storepkg.ProjectionEntry, 0, len(contents)) + for contentName, content := range contents { + id := path.Base(contentName) + incoming = append(incoming, PlaybookEntry{ID: id, YAML: string(content)}) + recorded = append(recorded, storepkg.ProjectionEntry{Family: storepkg.FamilyPlaybooks, EntryID: id}) + } + slices.SortFunc(incoming, func(a, b PlaybookEntry) int { return strings.Compare(a.ID, b.ID) }) + err := projectDocument(ctx, p.store, orgID, PlaybooksKind, "package:"+name+"@"+digest, "package-project:"+digest, func(current []byte) ([]byte, error) { + collection, err := decodePlaybooks(current) + if err != nil { + return nil, err + } + for _, entry := range collection.Playbooks { + if slices.ContainsFunc(incoming, func(in PlaybookEntry) bool { return in.ID == entry.ID }) { + return nil, fmt.Errorf("package %q: %w: playbook %q", name, storepkg.ErrContributionCollision, entry.ID) + } + } + collection.Playbooks = append(collection.Playbooks, incoming...) + return encodePlaybooks(collection) + }) + if err != nil { + return err + } + if err := p.ledger.Record(ctx, orgID, name, recorded); err != nil { + return fmt.Errorf("record contributions of %q: %w", name, err) + } + return nil +} + +func (p *PlaybookProjector) Withdraw(ctx context.Context, orgID, name, digest string) error { + entries, err := p.ledger.Entries(ctx, orgID, name) + if err != nil { + return fmt.Errorf("entries of package %q: %w", name, err) + } + var withdrawn []string + for _, entry := range entries { + if entry.Family == storepkg.FamilyPlaybooks { + withdrawn = append(withdrawn, entry.EntryID) + } + } + if len(withdrawn) == 0 { + return nil + } + err = projectDocument(ctx, p.store, orgID, PlaybooksKind, "package-withdraw:"+name+"@"+digest, "package-withdraw:"+digest, func(current []byte) ([]byte, error) { + collection, err := decodePlaybooks(current) + if err != nil { + return nil, err + } + collection.Playbooks = slices.DeleteFunc(collection.Playbooks, func(e PlaybookEntry) bool { return slices.Contains(withdrawn, e.ID) }) + return encodePlaybooks(collection) + }) + if err != nil { + return err + } + return p.ledger.Forget(ctx, orgID, name, storepkg.FamilyPlaybooks) +} + +func decodePlaybooks(value []byte) (PlaybookCollection, error) { + collection := PlaybookCollection{Playbooks: []PlaybookEntry{}} + if value == nil { + return collection, nil + } + if err := json.Unmarshal(value, &collection); err != nil { + return PlaybookCollection{}, fmt.Errorf("decode %s: %w", PlaybooksKind, err) + } + return collection, nil +} + +func encodePlaybooks(collection PlaybookCollection) ([]byte, error) { + if _, err := CompilePlaybooks(collection); err != nil { + return nil, err + } + return json.Marshal(collection) +} diff --git a/internal/app/controlplane/playbooks_client.go b/internal/app/controlplane/playbooks_client.go new file mode 100644 index 000000000..4cbabfc08 --- /dev/null +++ b/internal/app/controlplane/playbooks_client.go @@ -0,0 +1,70 @@ +package controlplane + +import ( + "context" + "encoding/json" + "log/slog" + "sync" + "time" + + pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" + "github.com/samcharles93/archie-core/internal/domain/eda/playbook" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" +) + +const playbookQueryTimeout = 5 * time.Second + +// LivePlaybooks serves the daemon the org's eda-playbooks resource. Each call +// reads the resource's version and recompiles only when it moved, so an +// install or an operator edit applies to the next task. A control plane that +// cannot be reached, or a document that no longer compiles, leaves the last +// good set in force. +type LivePlaybooks struct { + rpc pb.ControlPlaneServiceClient + log *slog.Logger + + mu sync.Mutex + version int64 + store *playbook.Store +} + +func NewLivePlaybooks(client pb.ControlPlaneServiceClient, log *slog.Logger) *LivePlaybooks { + return &LivePlaybooks{rpc: client, log: log} +} + +func (l *LivePlaybooks) current(ctx context.Context) *playbook.Store { + l.mu.Lock() + defer l.mu.Unlock() + ctx, cancel := context.WithTimeout(ctx, playbookQueryTimeout) + defer cancel() + response, err := l.rpc.Query(ctx, controlplanerpc.QueryRequest(ctx, PlaybooksKind)) + if err != nil { + l.log.Warn("eda playbooks unavailable; keeping the last good set", "err", controlplanerpc.ClientError(err)) + return l.store + } + if l.store != nil && response.Resource.Version == l.version { + return l.store + } + var collection PlaybookCollection + if err := json.Unmarshal(response.Resource.ValueJson, &collection); err != nil { + l.log.Error("eda playbooks undecodable; keeping the last good set", "err", err) + return l.store + } + store, err := CompilePlaybooks(collection) + if err != nil { + l.log.Error("eda playbooks do not compile; keeping the last good set", "err", err) + return l.store + } + l.version, l.store = response.Resource.Version, store + l.log.Info("eda playbooks loaded", "version", l.version, "playbooks", len(store.Playbooks)) + return store +} + +func (l *LivePlaybooks) Dispatch(input playbook.DispatchInput) (playbook.Decision, bool) { + return l.current(context.Background()).Dispatch(input) +} + +func (l *LivePlaybooks) Run(ctx context.Context, ledger storecontract.PlaybookDispatcher, log *slog.Logger, input playbook.DispatchInput) error { + return l.current(ctx).Run(ctx, ledger, log, input) +} diff --git a/internal/app/controlplane/profiles_projector.go b/internal/app/controlplane/profiles_projector.go new file mode 100644 index 000000000..fcc3a451a --- /dev/null +++ b/internal/app/controlplane/profiles_projector.go @@ -0,0 +1,133 @@ +package controlplane + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "slices" + + "gopkg.in/yaml.v3" + + "github.com/samcharles93/archie-core/internal/domain/storepkg" +) + +// profileFile is one contributed profile: a name and the profile it defines. +// The image is a reference to a published image, never a build. +type profileFile struct { + Name string `yaml:"name"` + Image string `yaml:"image"` + Kit string `yaml:"kit"` + Adapter string `yaml:"adapter"` + Tools []string `yaml:"tools"` +} + +// ProfileProjector merges an installed package's agent profiles into the +// org's agent-profiles resource. Names owned by an operator or another +// package are refused. +type ProfileProjector struct { + store ResourceStore + ledger storepkg.ProjectionLedger +} + +var _ storepkg.FamilyProjector = (*ProfileProjector)(nil) + +func NewProfilePackageProjector(resources ResourceStore, ledger storepkg.ProjectionLedger) (*ProfileProjector, error) { + if resources == nil || ledger == nil { + return nil, errors.New("control plane: profile projection needs a resource store and a contributions ledger") + } + return &ProfileProjector{store: resources, ledger: ledger}, nil +} + +func (p *ProfileProjector) Apply(ctx context.Context, orgID, name, digest string, contents map[string][]byte) error { + incoming := make(map[string]agentProfile, len(contents)) + recorded := make([]storepkg.ProjectionEntry, 0, len(contents)) + for contentName, content := range contents { + var file profileFile + if err := yaml.Unmarshal(content, &file); err != nil { + return fmt.Errorf("contributed profile %q: %w", contentName, err) + } + if file.Name == "" { + return fmt.Errorf("contributed profile %q has no name", contentName) + } + incoming[file.Name] = agentProfile{Image: file.Image, Kit: file.Kit, Adapter: file.Adapter, Tools: file.Tools} + recorded = append(recorded, storepkg.ProjectionEntry{Family: storepkg.FamilyProfiles, EntryID: file.Name}) + } + err := projectDocument(ctx, p.store, orgID, AgentProfileKind, "package:"+name+"@"+digest, "package-project:"+digest, func(current []byte) ([]byte, error) { + profiles, err := decodeProfiles(current) + if err != nil { + return nil, err + } + for profile, value := range incoming { + if _, taken := profiles[profile]; taken { + return nil, fmt.Errorf("package %q: %w: profile %q", name, storepkg.ErrContributionCollision, profile) + } + profiles[profile] = value + } + return encodeProfiles(profiles) + }) + if err != nil { + return err + } + if err := p.ledger.Record(ctx, orgID, name, recorded); err != nil { + return fmt.Errorf("record contributions of %q: %w", name, err) + } + return nil +} + +func (p *ProfileProjector) Withdraw(ctx context.Context, orgID, name, digest string) error { + entries, err := p.ledger.Entries(ctx, orgID, name) + if err != nil { + return fmt.Errorf("entries of package %q: %w", name, err) + } + var withdrawn []string + for _, entry := range entries { + if entry.Family == storepkg.FamilyProfiles { + withdrawn = append(withdrawn, entry.EntryID) + } + } + if len(withdrawn) == 0 { + return nil + } + err = projectDocument(ctx, p.store, orgID, AgentProfileKind, "package-withdraw:"+name+"@"+digest, "package-withdraw:"+digest, func(current []byte) ([]byte, error) { + profiles, err := decodeProfiles(current) + if err != nil { + return nil, err + } + for profile := range profiles { + if slices.Contains(withdrawn, profile) { + delete(profiles, profile) + } + } + return encodeProfiles(profiles) + }) + if err != nil { + return err + } + return p.ledger.Forget(ctx, orgID, name, storepkg.FamilyProfiles) +} + +func decodeProfiles(value []byte) (map[string]agentProfile, error) { + profiles := map[string]agentProfile{} + if value == nil { + return profiles, nil + } + if err := json.Unmarshal(value, &profiles); err != nil { + return nil, fmt.Errorf("decode %s: %w", AgentProfileKind, err) + } + if profiles == nil { + profiles = map[string]agentProfile{} + } + return profiles, nil +} + +func encodeProfiles(profiles map[string]agentProfile) ([]byte, error) { + value, err := json.Marshal(profiles) + if err != nil { + return nil, err + } + if err := validateAgentProfiles(value); err != nil { + return nil, fmt.Errorf("%w: %w", ErrValidation, err) + } + return value, nil +} diff --git a/internal/app/statestore/server.go b/internal/app/statestore/server.go index 37e276c40..4debfaa4d 100644 --- a/internal/app/statestore/server.go +++ b/internal/app/statestore/server.go @@ -499,8 +499,8 @@ func stateStoreServerOpts(listen, token string, grants *staterpc.TaskGrants) (op } // newPackageProjections builds the family projectors an installed package's -// contributions apply through: workflows become org workflow definitions, the -// one resource kind the runtime reads live today. The step vocabulary +// contributions apply through: workflows, playbooks and agent profiles become +// the org resources the runtime reads live. The step vocabulary // (stepVocabulary) is the same one this process's control plane validates // dashboard edits with, so a definition the projector admits is compilable // where it dispatches (openStateStoreControlPlane for the pairing). @@ -514,5 +514,18 @@ func newPackageProjections(pool *pgxpool.Pool) (map[string]storepkg.FamilyProjec if err != nil { return nil, fmt.Errorf("build workflow package projector: %w", err) } - return map[string]storepkg.FamilyProjector{storepkg.FamilyWorkflows: workflows}, nil + resources, ledger := postgres.NewResources(pool), postgres.NewPackageContributions(pool) + playbooks, err := controlplane.NewPlaybookPackageProjector(resources, ledger) + if err != nil { + return nil, fmt.Errorf("build playbook package projector: %w", err) + } + profiles, err := controlplane.NewProfilePackageProjector(resources, ledger) + if err != nil { + return nil, fmt.Errorf("build profile package projector: %w", err) + } + return map[string]storepkg.FamilyProjector{ + storepkg.FamilyWorkflows: workflows, + storepkg.FamilyPlaybooks: playbooks, + storepkg.FamilyProfiles: profiles, + }, nil } diff --git a/internal/config/config.go b/internal/config/config.go index a388f1b21..37fdb69be 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -370,9 +370,6 @@ type Config struct { // PlaybookDirs are optional directories of kind/label-to-workflow binding // files. A key bound in more than one file is a load error. PlaybookDirs []string `toml:"playbook_dirs" yaml:"playbook_dirs"` - // EDAPlaybookDir is an optional directory of EDA playbook documents: a - // trigger plus ordered actions with CEL conditions. - EDAPlaybookDir string `toml:"eda_playbook_dir" yaml:"eda_playbook_dir"` // MaxRetries caps how many times a parked task is retried before // being permanently parked (status "dead"). Defaults to 3. MaxRetries int `toml:"max_retries" yaml:"max_retries"` diff --git a/internal/domain/eda/playbook/playbook.go b/internal/domain/eda/playbook/playbook.go index b6642523f..ccc5478f1 100644 --- a/internal/domain/eda/playbook/playbook.go +++ b/internal/domain/eda/playbook/playbook.go @@ -46,7 +46,7 @@ type Store struct { // Playbook is one trigger+actions document. type Playbook struct { - // ID is the playbook's path relative to the directory root. + // ID is the playbook's stable name. ID string // Version is a content hash of the loaded file, recomputed on every // load. It pins dispatched-run provenance to the exact definition active @@ -198,6 +198,12 @@ func validateModuleActionIDs(path string, actions []rawAction) error { return nil } +// Document is one playbook's source text under its stable id. +type Document struct { + ID string + YAML []byte +} + // Load reads and compiles every playbook in dir. Any invalid playbook fails // the load. A missing directory is an empty store. func Load(dir string, modules Modules) (*Store, error) { @@ -209,21 +215,30 @@ func Load(dir string, modules Modules) (*Store, error) { return nil, fmt.Errorf("read playbook dir %s: %w", dir, err) } - var names []string + var docs []Document for _, e := range entries { if e.IsDir() { continue } - if ext := strings.ToLower(filepath.Ext(e.Name())); ext == ".yaml" || ext == ".yml" { - names = append(names, e.Name()) + if ext := strings.ToLower(filepath.Ext(e.Name())); ext != ".yaml" && ext != ".yml" { + continue } + data, err := os.ReadFile(filepath.Join(dir, e.Name())) + if err != nil { + return nil, fmt.Errorf("read playbook %s: %w", e.Name(), err) + } + docs = append(docs, Document{ID: e.Name(), YAML: data}) } - sort.Strings(names) + return Compile(docs, modules) +} +// Compile validates and compiles playbook documents in id order. Any invalid +// playbook fails the whole set: a partially valid set never runs. +func Compile(docs []Document, modules Modules) (*Store, error) { + sort.Slice(docs, func(i, j int) bool { return docs[i].ID < docs[j].ID }) store := &Store{modules: modules} - for _, name := range names { - path := filepath.Join(dir, name) - pb, err := loadOne(dir, path, modules) + for _, doc := range docs { + pb, err := compileOne(doc, modules) if err != nil { return nil, err } @@ -232,27 +247,17 @@ func Load(dir string, modules Modules) (*Store, error) { return store, nil } -// loadOne loads and validates a single playbook file, deriving its stable ID -// (path relative to the configured root) and a content-hash Version. -func loadOne(dir, path string, schemas KindSchemas) (*Playbook, error) { - data, err := os.ReadFile(path) - if err != nil { - return nil, fmt.Errorf("read playbook %s: %w", path, err) - } - - id := path - if rel, relErr := filepath.Rel(dir, path); relErr == nil { - id = rel - } - +// compileOne validates a single playbook, deriving its content-hash Version. +func compileOne(doc Document, schemas KindSchemas) (*Playbook, error) { + path := doc.ID var raw rawPlaybook - if err := yaml.Unmarshal(data, &raw); err != nil { + if err := yaml.Unmarshal(doc.YAML, &raw); err != nil { return nil, fmt.Errorf("parse playbook %s: %w", path, err) } pb := &Playbook{ - ID: filepath.ToSlash(id), - Version: fmt.Sprintf("%x", sha256.Sum256(data)), + ID: path, + Version: fmt.Sprintf("%x", sha256.Sum256(doc.YAML)), Trigger: Trigger{ Kind: workintake.Kind(strings.TrimSpace(raw.Trigger.Kind)), Labels: raw.Trigger.Labels, diff --git a/internal/domain/storepkg/contributions.go b/internal/domain/storepkg/contributions.go index fbd4ca7eb..b8433717a 100644 --- a/internal/domain/storepkg/contributions.go +++ b/internal/domain/storepkg/contributions.go @@ -13,11 +13,13 @@ const ( FamilySkills = "skills" FamilyMCPServer = "mcpServers" FamilyDefaults = "defaults" + FamilyPlaybooks = "playbooks" + FamilyProfiles = "profiles" ) // families is the family order projection follows, for a deterministic // install and withdrawal. -var families = []string{FamilyWorkflows, FamilyPrompts, FamilySkills, FamilyMCPServer, FamilyDefaults} +var families = []string{FamilyWorkflows, FamilyPrompts, FamilySkills, FamilyMCPServer, FamilyDefaults, FamilyPlaybooks, FamilyProfiles} // familyNames returns the names one descriptor family contributes. func (c Contributions) familyNames(family string) []string { @@ -32,6 +34,10 @@ func (c Contributions) familyNames(family string) []string { return c.MCPServers case FamilyDefaults: return c.Defaults + case FamilyPlaybooks: + return c.Playbooks + case FamilyProfiles: + return c.Profiles default: return nil } diff --git a/internal/domain/storepkg/descriptor.go b/internal/domain/storepkg/descriptor.go index dd461d427..42ba1be24 100644 --- a/internal/domain/storepkg/descriptor.go +++ b/internal/domain/storepkg/descriptor.go @@ -36,6 +36,8 @@ type Contributions struct { Skills []string `yaml:"skills"` MCPServers []string `yaml:"mcpServers"` Defaults []string `yaml:"defaults"` + Playbooks []string `yaml:"playbooks"` + Profiles []string `yaml:"profiles"` // Extensions are executable plugins the host launches as processes. They // are the only package files allowed to carry an executable mode. Extensions []Extension `yaml:"extensions"` @@ -171,7 +173,8 @@ func (d Descriptor) validateExtensions() (map[string]bool, error) { func (d Descriptor) hasContribution() bool { return len(d.Contributes.Workflows)+len(d.Contributes.Prompts)+len(d.Contributes.Skills)+ - len(d.Contributes.MCPServers)+len(d.Contributes.Defaults)+len(d.Contributes.Extensions) > 0 + len(d.Contributes.MCPServers)+len(d.Contributes.Defaults)+len(d.Contributes.Playbooks)+ + len(d.Contributes.Profiles)+len(d.Contributes.Extensions) > 0 } func validForgePermission(permission string) bool {