Files
archie/internal/daemon/model_egress_test.go
T
sam 4d4f30b719 fix(egress): bind a task's model providers from their api_key_ref
A container task parked when a provider carried an api_key_env but no
credential binding named after it, even though the task's models never
reached that provider. Egress pre-bound every provider the instance
configures and ignored api_key_ref, so the credentials an operator stores in
provider-settings were never used.

A task's config now carries only the providers its model aliases resolve to,
each with the variable its credential is read from: the operator's
api_key_env, or a name derived from the provider when its credential is a
secret reference. modelEgress resolves that reference once for the session
and the proxy substitutes the session's key for the sentinel, so the
container still holds no key. A provider that only declares an env var keeps
resolving through its org credential binding.
2026-10-20 06:56:38 +11:00

135 lines
4.5 KiB
Go

package daemon
import (
"context"
"encoding/json"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/eventbus"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
"github.com/samcharles93/archie-core/internal/taskrun"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// recordingModelEgress keeps the providers a task's model session was opened
// with.
type recordingModelEgress struct {
providers map[string]config.Provider
}
func (r *recordingModelEgress) Open(_, _ string, providers map[string]config.Provider) (ModelSession, error) {
r.providers = providers
return ModelSession{}, nil
}
func (r *recordingModelEgress) Close(context.Context, string) {}
// A task's model session carries only the providers its models name: a
// configured provider no alias reaches must never be required, and the
// provider the task does use arrives with the variable its credential is read
// from.
func TestOpenModelEgressCarriesOnlyReferencedProviders(t *testing.T) {
t.Parallel()
egressProxy := &recordingModelEgress{}
d := &Daemon{
Cfg: config.NewHolder(config.Config{
Models: map[string]string{"default": "used/model-one"},
Providers: map[string]config.Provider{
"used": {Class: "deepseek", APIKey: config.SecretRef{Engine: "env", Key: "USED_KEY"}},
"unused": {Class: "gemini", APIKeyEnv: "UNUSED_KEY"},
},
}),
ModelEgress: egressProxy,
}
task := &workflow.Task{ID: 7, WorkflowDefinitionYAML: "id: w\nsteps:\n - type: agent.run\n"}
if _, _, err := d.openModelEgress(t.Context(), task, "run-credential"); err != nil {
t.Fatalf("open model egress: %v", err)
}
if _, ok := egressProxy.providers["unused"]; ok {
t.Fatalf("a provider no model names reached the session: %v", egressProxy.providers)
}
used, ok := egressProxy.providers["used"]
if !ok {
t.Fatalf("the provider the task's model names is missing: %v", egressProxy.providers)
}
if want := config.ProviderKeyEnvName("used"); used.APIKeyEnv != want {
t.Fatalf("provider variable = %q, want %q", used.APIKeyEnv, want)
}
}
// capturingBus records the taskrun request the daemon sent.
type capturingBus struct {
request []byte
reply []byte
}
func (b *capturingBus) PublishUnique(context.Context, string, string, []byte) error { return nil }
func (b *capturingBus) Fetch(context.Context) (eventbus.Message, error) {
return nil, nil
}
func (b *capturingBus) Request(_ context.Context, _ string, payload []byte) ([]byte, error) {
b.request = payload
return b.reply, nil
}
// The provider a task's model runs on reaches the container with a variable the
// agent can read, so the runtime authenticates through the egress proxy instead
// of running the provider unauthenticated.
func TestRunViaAgentCarriesProviderCredentialVariable(t *testing.T) {
reply, err := json.Marshal(taskrun.Response{Status: taskstate.Completed})
if err != nil {
t.Fatalf("marshal response: %v", err)
}
bus := &capturingBus{reply: reply}
d := &Daemon{
Cfg: config.NewHolder(config.Config{
Models: map[string]string{"default": "used/model-one"},
Providers: map[string]config.Provider{
"used": {Class: "deepseek", APIKey: config.SecretRef{Engine: "env", Key: "USED_KEY"}},
},
}),
Tasks: bus,
Log: slog.New(slog.DiscardHandler),
}
task := &workflow.Task{ID: 7, Attempt: 1, Workflow: "implement", WorkflowDefinitionYAML: "id: w\nsteps:\n - type: agent.run\n"}
d.runViaAgent(t.Context(), task, config.Repo{}, config.AgentProfile{}, nil, "run-credential")
if bus.request == nil {
t.Fatal("the daemon sent no taskrun request")
}
var req taskrun.Request
if err := json.Unmarshal(bus.request, &req); err != nil {
t.Fatalf("decode taskrun request: %v", err)
}
provider, ok := req.Providers["used"]
if !ok {
t.Fatalf("the task's provider is missing from the request: %v", req.Providers)
}
if provider.APIKeyEnv == "" {
t.Fatalf("the provider reached the container without a credential variable: %+v", provider)
}
sentinel := egress.SentinelFor("used")
t.Setenv(provider.APIKeyEnv, sentinel)
rt := modelloop.NewRuntime(req.Providers, nil)
if rt == nil {
t.Fatal("no runtime built from the request's providers")
}
auth, err := rt.ResolveAuth(t.Context(), "used")
if err != nil {
t.Fatalf("resolve auth: %v", err)
}
if auth.Token != sentinel {
t.Fatalf("the runtime resolves %q, so the egress sentinel is never sent", auth.Token)
}
}