Files
archie/internal/daemon/model_egress_test.go
T
sam f92c5b4536
ci/woodpecker/pr/quality Pipeline was successful
Quality / check (pull_request) Canceled after 7m39s
chore(runtime): delete Kit and external agent harness support (#1877)
Archie runs its own agent loop only. Removes Kit resolution and launch,
the harness runner and adapters, the setup terminal and sandbox view,
harness secrets and OAuth capture (table dropped in migration 0075), the
task-tools contract that only Kit containers reached, and the
sandbox-kit-spec dependency. Agent profiles carry image and tools only.
The egress proxy keeps the model-session path; the sandbox relay code
stays for #1867.
2026-11-12 21:29:05 +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, Identity: string(sys), 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, Identity: string(sys), Attempt: 1, Workflow: "implement", WorkflowDefinitionYAML: "id: w\nsteps:\n - type: agent.run\n"}
d.runViaAgent(t.Context(), task, config.Repo{}, config.AgentProfile{}, "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)
}
}