From fc0e15664955720d2741e525f4ed8d89ea605d8b Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 11 Oct 2026 03:42:02 +1100 Subject: [PATCH] fix(daemon): bound a package workflow's tools by its accepted authority --- internal/agentexec/modelloop/loop_runner.go | 6 +-- internal/app/archied/bootstrap.go | 1 + internal/daemon/daemon.go | 5 +- internal/daemon/package_tools.go | 45 +++++++++++++++++ internal/daemon/package_tools_test.go | 53 +++++++++++++++++++++ internal/taskrun/taskrun.go | 4 +- 6 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 internal/daemon/package_tools.go create mode 100644 internal/daemon/package_tools_test.go diff --git a/internal/agentexec/modelloop/loop_runner.go b/internal/agentexec/modelloop/loop_runner.go index 7485bef1b..f7fb671b2 100644 --- a/internal/agentexec/modelloop/loop_runner.go +++ b/internal/agentexec/modelloop/loop_runner.go @@ -31,7 +31,7 @@ type LoopRunner struct { // Limits bounds the size of tool results fed back into the stage. The // zero value applies no limits. Limits ToolLimits - // AllowTools is the profile's allowlist over archie-added tools. Empty allows + // AllowTools is the allowlist over archie-added tools. Nil allows // all. Capture tools and agentloop's built-in file tools are never // filtered. AllowTools []string @@ -187,9 +187,9 @@ func makeCaptureHandler(spec agentrun.CaptureTool, captures map[string][]json.Ra } } -// allowedTools keeps the tools allow names; an empty allow keeps them all. +// allowedTools keeps the tools allow names; a nil allow keeps them all. func allowedTools(set core.ToolSet, allow []string) core.ToolSet { - if len(allow) == 0 { + if allow == nil { return set } kept := core.ToolSet{} diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 9b2f3094f..1ccdaf199 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -572,6 +572,7 @@ func (b *boot) buildDaemon() { Bus: b.bus, Forge: b.forgeClient, Trees: b.trees, + PackageAuthorities: installedAuthorities{b.stateStoreGrants.Client}, Storage: b.storeBackend, Log: log, Tasks: b.natsClient, diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index f0bad8391..19ccb510b 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -191,6 +191,9 @@ type Daemon struct { IdentityRepository identity.Repository RootIdentityID identity.IdentityID + // PackageAuthorities bounds the tools a package workflow's run is offered. + PackageAuthorities PackageAuthorities + // Playbooks are the loaded EDA playbooks, consulted before kind/label // bindings when pinning a workflow. Nil means none. Playbooks interface { @@ -1954,7 +1957,7 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf MCPServers: cfg.Tools.MCPServers, RunCredential: credential, WorkflowDefinition: task.WorkflowDefinitionYAML, - Tools: profile.Tools, + Tools: d.toolAllowlist(ctx, task, profile.Tools), Harness: harness, } data, err := json.Marshal(req) diff --git a/internal/daemon/package_tools.go b/internal/daemon/package_tools.go new file mode 100644 index 000000000..3cabe3a79 --- /dev/null +++ b/internal/daemon/package_tools.go @@ -0,0 +1,45 @@ +package daemon + +import ( + "context" + "slices" + + "github.com/samcharles93/archie-core/internal/domain/storepkg" + "github.com/samcharles93/archie-core/internal/domain/workflow" +) + +// PackageAuthorities answers what the operator accepted for an installed +// package. A package that is not installed, or not accepted, has none. +type PackageAuthorities interface { + AcceptedAuthority(ctx context.Context, name string) (storepkg.Authority, error) +} + +// toolAllowlist is the tool allowlist a task runs with. nil offers every tool; +// a non-nil list offers exactly its members. A run of a package's workflow is +// bounded by the tools accepted for that package, so a missing, unaccepted or +// unreadable package offers none. +func (d *Daemon) toolAllowlist(ctx context.Context, task *workflow.Task, profileTools []string) []string { + name := workflow.PackageOf(task.WorkflowDefinitionYAML) + if name == "" { + if len(profileTools) == 0 { + return nil + } + return profileTools + } + var accepted []string + if d.PackageAuthorities != nil { + authority, err := d.PackageAuthorities.AcceptedAuthority(ctx, name) + if err != nil { + d.Log.Warn("package authority unavailable; offering no tools", "task", task.ID, "package", name, "err", err) + } else { + accepted = authority.Tools + } + } + allowed := []string{} + for _, tool := range accepted { + if len(profileTools) == 0 || slices.Contains(profileTools, tool) { + allowed = append(allowed, tool) + } + } + return allowed +} diff --git a/internal/daemon/package_tools_test.go b/internal/daemon/package_tools_test.go new file mode 100644 index 000000000..05af693e1 --- /dev/null +++ b/internal/daemon/package_tools_test.go @@ -0,0 +1,53 @@ +package daemon + +import ( + "context" + "errors" + "log/slog" + "reflect" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/storepkg" + "github.com/samcharles93/archie-core/internal/domain/workflow" +) + +type acceptedTools map[string]storepkg.Authority + +func (a acceptedTools) AcceptedAuthority(_ context.Context, name string) (storepkg.Authority, error) { + if authority, ok := a[name]; ok { + return authority, nil + } + return storepkg.Authority{}, errors.New("not installed") +} + +// TestToolAllowlistFollowsPackageAuthority pins that a package workflow is +// offered only the tools accepted for its package, and none when the package +// is missing or unaccepted, while an operator workflow keeps its profile's list. +func TestToolAllowlistFollowsPackageAuthority(t *testing.T) { + d := &Daemon{Log: slog.Default(), PackageAuthorities: acceptedTools{ + "triage": {Tools: []string{"search", "fetch"}}, + "unsigned": {}, + }} + tests := []struct { + name string + yaml string + profile []string + want []string + }{ + {"operator workflow without a profile list", "id: a\n", nil, nil}, + {"operator workflow keeps its profile list", "id: a\n", []string{"x"}, []string{"x"}}, + {"package workflow gets its accepted tools", "package: triage\nid: a\n", nil, []string{"search", "fetch"}}, + {"profile narrows the accepted tools", "package: triage\nid: a\n", []string{"fetch", "shell"}, []string{"fetch"}}, + {"profile cannot widen the accepted tools", "package: triage\nid: a\n", []string{"shell"}, []string{}}, + {"package with nothing accepted", "package: unsigned\nid: a\n", nil, []string{}}, + {"package that is gone", "package: gone\nid: a\n", nil, []string{}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := d.toolAllowlist(context.Background(), &workflow.Task{WorkflowDefinitionYAML: tt.yaml}, tt.profile) + if !reflect.DeepEqual(got, tt.want) { + t.Fatalf("got %#v, want %#v", got, tt.want) + } + }) + } +} diff --git a/internal/taskrun/taskrun.go b/internal/taskrun/taskrun.go index 9d714c300..9e8317ec3 100644 --- a/internal/taskrun/taskrun.go +++ b/internal/taskrun/taskrun.go @@ -28,8 +28,8 @@ type Request struct { RunCredential string `json:"run_credential,omitempty"` // WorkflowDefinition is the exact YAML pinned on Task before dispatch. WorkflowDefinition string `json:"workflow_definition"` - // Tools is the agent profile's tool allowlist; empty allows every tool. - Tools []string `json:"tools,omitempty"` + // Tools is the tool allowlist: nil allows every tool, an empty list none. + Tools []string `json:"tools"` // Harness, set for a Kit profile, runs every agent stage on the Kit's // CLI instead of the built-in loop. Harness *agentrun.HarnessSpec `json:"harness,omitempty"`