refactor: testing from the beginning. a journey in 150k lines of tests to none
This commit is contained in:
1 parent
f0fe550a35
commit
780bc44d82
651 files changed
-146443
No files matched your search
@@ -1,110 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCommandBoundary(t *testing.T) {
|
||||
paths, err := filepath.Glob("*.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, path := range paths {
|
||||
if !strings.HasSuffix(path, "_test.go") && path != "main.go" {
|
||||
t.Errorf("cmd/archie-agent contains substantive production file %q; runtime ownership belongs in internal/app/agentworker", path)
|
||||
}
|
||||
}
|
||||
|
||||
file, err := parser.ParseFile(token.NewFileSet(), "main.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
const (
|
||||
projectInternal = "github.com/samcharles93/archie-core/internal/"
|
||||
appImport = projectInternal + "app/agentworker"
|
||||
)
|
||||
appIdent := "agentworker"
|
||||
for _, imported := range file.Imports {
|
||||
path, err := strconv.Unquote(imported.Path.Value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.HasPrefix(path, projectInternal) && path != appImport {
|
||||
t.Errorf("cmd/archie-agent imports substantive internal package %q; only %q is allowed", path, appImport)
|
||||
}
|
||||
if path == appImport && imported.Name != nil {
|
||||
if imported.Name.Name == "." || imported.Name.Name == "_" {
|
||||
t.Errorf("cmd/archie-agent imports %q with unsupported alias %q", appImport, imported.Name.Name)
|
||||
} else {
|
||||
appIdent = imported.Name.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
allowedFunctions := map[string]bool{
|
||||
"main": true,
|
||||
"run": true,
|
||||
"runCommand": true,
|
||||
"natsConnectionSettings": true,
|
||||
"runRelay": true,
|
||||
"runMCP": true,
|
||||
}
|
||||
for _, declaration := range file.Decls {
|
||||
function, ok := declaration.(*ast.FuncDecl)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if function.Recv != nil || !allowedFunctions[function.Name.Name] {
|
||||
t.Errorf("cmd/archie-agent main.go declares unexpected function %q; orchestration belongs in internal/app/agentworker", function.Name.Name)
|
||||
}
|
||||
delete(allowedFunctions, function.Name.Name)
|
||||
}
|
||||
for name := range allowedFunctions {
|
||||
t.Errorf("cmd/archie-agent main.go is missing intended process function %q", name)
|
||||
}
|
||||
|
||||
var runFunction *ast.FuncDecl
|
||||
for _, declaration := range file.Decls {
|
||||
function, ok := declaration.(*ast.FuncDecl)
|
||||
if ok && function.Name.Name == "run" {
|
||||
runFunction = function
|
||||
break
|
||||
}
|
||||
}
|
||||
if runFunction == nil {
|
||||
t.Fatal("cmd/archie-agent has no run function")
|
||||
}
|
||||
|
||||
workerCalls := 0
|
||||
ast.Inspect(runFunction.Body, func(node ast.Node) bool {
|
||||
call, ok := node.(*ast.CallExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
callee, ok := call.Fun.(*ast.Ident)
|
||||
if !ok || callee.Name != "runCommand" {
|
||||
return true
|
||||
}
|
||||
for _, argument := range call.Args {
|
||||
selector, ok := argument.(*ast.SelectorExpr)
|
||||
if !ok || selector.Sel.Name != "Run" {
|
||||
continue
|
||||
}
|
||||
pkg, ok := selector.X.(*ast.Ident)
|
||||
if ok && pkg.Name == appIdent {
|
||||
workerCalls++
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
if workerCalls != 1 {
|
||||
t.Fatalf("cmd/archie-agent run passes agentworker.Run to runCommand %d times, want exactly 1", workerCalls)
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"maps"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunRelay(t *testing.T) {
|
||||
noServe := func(t *testing.T) relayServer {
|
||||
return func(context.Context, map[string]string) error {
|
||||
t.Error("the relay served despite a usage error")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
for name, args := range map[string][]string{
|
||||
"no forward": {},
|
||||
"a forward with no =": {"-forward", "127.0.0.1:0"},
|
||||
"a forward with no port": {"-forward", "=172.17.0.1:3129"},
|
||||
"one listen address used": {"-forward", ":3128=a:1", "-forward", ":3128=b:2"},
|
||||
} {
|
||||
t.Run(name+" is a usage error", func(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
if code := runRelay(args, &stderr, noServe(t)); code == 0 {
|
||||
t.Fatalf("exit 0, want a usage error; stderr %q", stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("every forward is served", func(t *testing.T) {
|
||||
var got map[string]string
|
||||
serve := func(_ context.Context, forwards map[string]string) error {
|
||||
got = forwards
|
||||
return nil
|
||||
}
|
||||
args := []string{"-forward", ":3128=172.17.0.1:3129", "-forward", ":4222=172.17.0.1:4222"}
|
||||
if code := runRelay(args, &bytes.Buffer{}, serve); code != 0 {
|
||||
t.Fatalf("exit %d, want 0", code)
|
||||
}
|
||||
want := map[string]string{":3128": "172.17.0.1:3129", ":4222": "172.17.0.1:4222"}
|
||||
if !maps.Equal(got, want) {
|
||||
t.Fatalf("served %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRelaySubcommandRoutes(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
code := runCommand([]string{"relay"}, func(string) string { return "" }, &stderr, nil)
|
||||
if code != 1 || !strings.Contains(stderr.String(), "-forward") {
|
||||
t.Fatalf("relay subcommand exit %d stderr %q; want the relay's own usage error", code, stderr.String())
|
||||
}
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/agentworker"
|
||||
)
|
||||
|
||||
func TestRunCommandPreservesCLIExitSemantics(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
getenv func(string) string
|
||||
worker workerRunner
|
||||
wantCode int
|
||||
}{
|
||||
{name: "help", args: []string{"-h"}},
|
||||
{name: "flag parse error", args: []string{"-unknown"}, wantCode: 2},
|
||||
{name: "removed consumer flag", args: []string{"-consumer", "legacy"}, wantCode: 2},
|
||||
{name: "missing NATS URL", wantCode: 1},
|
||||
{name: "missing state store URL", args: []string{"-nats-url", "nats://test"}, wantCode: 1},
|
||||
{
|
||||
name: "worker failure",
|
||||
args: []string{"-nats-url", "nats://test", "-state-store-url", "127.0.0.1:9090"},
|
||||
worker: func(context.Context, agentworker.Settings, *slog.Logger) error {
|
||||
return errors.New("worker failed")
|
||||
},
|
||||
wantCode: 1,
|
||||
},
|
||||
{
|
||||
name: "success",
|
||||
args: []string{"-nats-url", "nats://test", "-state-store-url", "127.0.0.1:9090"},
|
||||
worker: func(context.Context, agentworker.Settings, *slog.Logger) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
getenv := test.getenv
|
||||
if getenv == nil {
|
||||
getenv = func(string) string { return "" }
|
||||
}
|
||||
worker := test.worker
|
||||
if worker == nil {
|
||||
worker = func(context.Context, agentworker.Settings, *slog.Logger) error {
|
||||
t.Fatal("worker called")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if got := runCommand(test.args, getenv, &bytes.Buffer{}, worker); got != test.wantCode {
|
||||
t.Fatalf("runCommand() = %d, want %d", got, test.wantCode)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandRequiresNATSURL(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
called := false
|
||||
code := runCommand(nil, func(string) string { return "" }, &stderr, func(context.Context, agentworker.Settings, *slog.Logger) error {
|
||||
called = true
|
||||
return nil
|
||||
})
|
||||
if code != 1 || called {
|
||||
t.Fatalf("(code, worker called) = (%d, %v), want (1, false)", code, called)
|
||||
}
|
||||
for _, want := range []string{"error: -nats-url or NATS_URL is required", "Usage of archie-agent:"} {
|
||||
if !strings.Contains(stderr.String(), want) {
|
||||
t.Fatalf("stderr %q does not contain %q", stderr.String(), want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandMapsWorkerOutcomeToExitStatus(t *testing.T) {
|
||||
workerErr := errors.New("startup failed")
|
||||
tests := []struct {
|
||||
name string
|
||||
workerErr error
|
||||
wantCode int
|
||||
}{
|
||||
{name: "success"},
|
||||
{name: "worker failure", workerErr: workerErr, wantCode: 1},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
code := runCommand([]string{"-nats-url", "nats://flag"}, func(string) string { return "token" }, &stderr, func(_ context.Context, settings agentworker.Settings, _ *slog.Logger) error {
|
||||
if settings.NATSURL != "nats://flag" || settings.NATSToken != "token" {
|
||||
t.Fatalf("settings = %#v", settings)
|
||||
}
|
||||
return test.workerErr
|
||||
})
|
||||
if code != test.wantCode {
|
||||
t.Fatalf("code = %d, want %d", code, test.wantCode)
|
||||
}
|
||||
if test.workerErr != nil && stderr.Len() != 0 {
|
||||
t.Fatalf("stderr = %q, want command not to duplicate application logging", stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNATSConnectionSettings(t *testing.T) {
|
||||
getenv := func(name string) string {
|
||||
switch name {
|
||||
case "NATS_URL":
|
||||
return "nats://nats.example:4222"
|
||||
case "NATS_TOKEN":
|
||||
return "test-nats-token"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("container environment", func(t *testing.T) {
|
||||
url, token := natsConnectionSettings("", getenv)
|
||||
if url != "nats://nats.example:4222" || token != "test-nats-token" {
|
||||
t.Fatalf("natsConnectionSettings() = (%q, %q)", url, token)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("flag URL takes precedence", func(t *testing.T) {
|
||||
url, token := natsConnectionSettings("nats://flag.example:4222", getenv)
|
||||
if url != "nats://flag.example:4222" || token != "test-nats-token" {
|
||||
t.Fatalf("natsConnectionSettings() = (%q, %q)", url, token)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
// This file enforces the objective half of the Messaging Service deletion gate
|
||||
// (docs/prds/messaging-service-boundary.md, "Deletion Gate and Verification"): the
|
||||
// linked binary carries no state-store, daemon, workflow, forge, gateway or
|
||||
// dashboard runtime. Go imports are atomic, so a single reference to a type living
|
||||
// beside a runtime relinks the runtime, and the severance holds only while
|
||||
// something re-measures it. That is exactly how this package came to link the
|
||||
// SQLite driver, the workflow engine and agentexec through one import of
|
||||
// internal/app/controlplane (archie-core-1ng1).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const modulePath = "github.com/samcharles93/archie-core/"
|
||||
|
||||
// bannedCategory is one class of runtime the Messaging Service must not link.
|
||||
// The names are the PRD's wording; the prefixes are how that class appears in
|
||||
// this module.
|
||||
type bannedCategory struct {
|
||||
name string
|
||||
prefixes []string
|
||||
}
|
||||
|
||||
// deletionGate is the PRD's list, one entry per category it names. Two deliberate
|
||||
// absences:
|
||||
//
|
||||
// - internal/channels is NOT here. The Messaging Service hosts those adapters;
|
||||
// it is the process they live in, not a consumer of them.
|
||||
// - internal/gatewayrpc needs no exception. It is the Gateway's contract, not
|
||||
// its runtime, and it does not sit under the internal/gateway prefix -- the
|
||||
// check below would catch it if it ever did.
|
||||
var deletionGate = []bannedCategory{
|
||||
{"state store", []string{
|
||||
"modernc.org/sqlite",
|
||||
"github.com/jackc/pgx",
|
||||
modulePath + "internal/infrastructure/postgres",
|
||||
}},
|
||||
{"daemon runtime", []string{
|
||||
modulePath + "internal/daemon",
|
||||
modulePath + "internal/app/archied",
|
||||
modulePath + "internal/container",
|
||||
modulePath + "internal/worktree",
|
||||
}},
|
||||
{"workflow engine", []string{
|
||||
modulePath + "internal/domain/workflow",
|
||||
modulePath + "internal/agentexec",
|
||||
modulePath + "internal/taskrun",
|
||||
}},
|
||||
{"forge client", []string{
|
||||
modulePath + "internal/forge",
|
||||
modulePath + "internal/forgerpc",
|
||||
}},
|
||||
{"gateway runtime", []string{
|
||||
modulePath + "internal/gateway",
|
||||
}},
|
||||
// The PRD names "Direct LLM providers, model runtimes" among what the
|
||||
// Messaging Service must not own or link. These are the in-module
|
||||
// infrastructure packages that construct one; building either here means
|
||||
// a model-provider credential is in the messaging process.
|
||||
{"model providers", []string{
|
||||
modulePath + "internal/infrastructure/transcription",
|
||||
modulePath + "internal/infrastructure/embedding",
|
||||
}},
|
||||
{"dashboard", []string{
|
||||
modulePath + "internal/webui",
|
||||
}},
|
||||
}
|
||||
|
||||
// gateExceptions are packages under a banned prefix that carry only contract,
|
||||
// vocabulary or view types, so linking them is the intended end state rather
|
||||
// than a tolerated leak. internal/domain/workflow/task is the Task status and
|
||||
// view vocabulary the Messaging Service renders; its own dependency set holds
|
||||
// none of the banned runtimes, which is what makes the exception defensible
|
||||
// rather than a hole. cmd/archie-ui/architecture_test.go carries the same entry
|
||||
// for the same reason.
|
||||
//
|
||||
// Each one must stay linked: an exception nothing needs any more is a hole in
|
||||
// the fence, so this list is checked in both directions.
|
||||
var gateExceptions = []string{
|
||||
modulePath + "internal/domain/workflow/task",
|
||||
}
|
||||
|
||||
// under reports whether importPath is prefix or a package below it.
|
||||
func under(importPath, prefix string) bool {
|
||||
return importPath == prefix || strings.HasPrefix(importPath, prefix+"/")
|
||||
}
|
||||
|
||||
func TestMessagingProcessLinksNoRuntimeItOnlyDials(t *testing.T) {
|
||||
listed, err := exec.CommandContext(t.Context(), "go", "list", "-deps", ".").CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("go list -deps .: %v\n%s", err, listed)
|
||||
}
|
||||
|
||||
linked := make(map[string]bool, len(gateExceptions))
|
||||
for importPath := range strings.FieldsSeq(string(listed)) {
|
||||
if excepted(importPath, linked) {
|
||||
continue
|
||||
}
|
||||
if category, banned := bannedLink(importPath); banned {
|
||||
t.Errorf("archie-messaging links %s (%s); the Messaging Service reaches that capability over a contract, never in-process (docs/prds/messaging-service-boundary.md, deletion gate)", importPath, category)
|
||||
}
|
||||
}
|
||||
for _, exception := range gateExceptions {
|
||||
if !linked[exception] {
|
||||
t.Errorf("%s is excepted from the deletion gate but is no longer linked; drop the exception instead of leaving its prefix open", exception)
|
||||
}
|
||||
}
|
||||
// A gate that cannot fail, and a `go list` that returns nothing usable, both
|
||||
// read as a pass. This pins the one thing the loop above assumes.
|
||||
if !strings.Contains(string(listed), modulePath+"cmd/archie-messaging") {
|
||||
t.Fatal("go list -deps returned a list that does not even name this package: the gate asserted nothing")
|
||||
}
|
||||
}
|
||||
|
||||
// bannedLink reports the deletion-gate category importPath falls under, or
|
||||
// ("", false). Classifying a single package is the rule the unit test below
|
||||
// pins, so the gate can be exercised without a live `go list`.
|
||||
func bannedLink(importPath string) (category string, banned bool) {
|
||||
for _, c := range deletionGate {
|
||||
for _, prefix := range c.prefixes {
|
||||
if under(importPath, prefix) {
|
||||
return c.name, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// TestDeletionGateBansModelProviderCapabilities pins the PRD category that the
|
||||
// voice-transcription violation slipped past: the link gate would not flag
|
||||
// anything merely because the process built an LLM client, so the capability
|
||||
// packages are named explicitly. gatewayrpc is checked as the negative, since
|
||||
// it is the permitted contract and must not be swept up by the internal/
|
||||
// infrastructure/ prefix.
|
||||
func TestDeletionGateBansModelProviderCapabilities(t *testing.T) {
|
||||
for _, pkg := range []string{
|
||||
modulePath + "internal/infrastructure/transcription",
|
||||
modulePath + "internal/infrastructure/embedding",
|
||||
} {
|
||||
if _, banned := bannedLink(pkg); !banned {
|
||||
t.Errorf("bannedLink(%q) = not banned; the Messaging Service must not construct a model-provider capability in-process", pkg)
|
||||
}
|
||||
}
|
||||
if category, banned := bannedLink(modulePath + "internal/infrastructure/gatewayrpc"); banned {
|
||||
t.Errorf("bannedLink(gatewayrpc) = %q; the Gateway contract is permitted", category)
|
||||
}
|
||||
}
|
||||
|
||||
// excepted reports whether importPath sits under an excepted package, recording
|
||||
// the ones actually linked so a stale exception fails.
|
||||
func excepted(importPath string, linked map[string]bool) bool {
|
||||
for _, exception := range gateExceptions {
|
||||
if under(importPath, exception) {
|
||||
linked[exception] = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -1,433 +0,0 @@
|
||||
// The link gate in architecture_test.go measures which packages the Messaging
|
||||
// binary links. It cannot see a process that links nothing new but still reads
|
||||
// a decoded configuration value it is not allowed to own -- which is exactly
|
||||
// how voice transcription was wired into the Messaging Service while a green
|
||||
// link gate watched: internal/app/archiemessaging/config.go built an LLM
|
||||
// transcriber from [models].transcription and [providers.*], both of which
|
||||
// docs/prds/messaging-service-boundary.md reserves to the model-owning process.
|
||||
//
|
||||
// This file closes that shape of hole: it scans the Messaging Service's own
|
||||
// production sources for a read of a top-level config section the PRD forbids,
|
||||
// so re-adding the projection field fails here rather than shipping green.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// messagingAppDir is the Messaging Service's application package, relative to
|
||||
// this package's directory (go test runs with the package dir as cwd).
|
||||
const messagingAppDir = "../../internal/app/archiemessaging"
|
||||
|
||||
// bannedConfigFields are the top-level config.Config fields the Messaging
|
||||
// Service must never read, by Go field name. The PRD's "Configuration
|
||||
// Ownership" section permits [chat.*], [services.*], [secrets] and the
|
||||
// path/identity keys the process needs to locate itself; every entry here is a
|
||||
// capability another process owns. internal/config's field names are the
|
||||
// concrete keys, because that is what a Go read names.
|
||||
var bannedConfigFields = map[string]bool{
|
||||
"Forge": true, // [forge]
|
||||
"Repos": true, // [repos]
|
||||
"Models": true, // [models]
|
||||
"Providers": true, // [providers]
|
||||
"Containers": true, // [containers]
|
||||
"NATS": true, // [nats]
|
||||
// [runners] has no top-level field in internal/config today; if one is
|
||||
// added its own name must be listed here with it.
|
||||
}
|
||||
|
||||
// configReadOffenders returns the banned "<holder>.<Field>" config reads in
|
||||
// src, in source order. A holder is anything the file gives the
|
||||
// internal/config.Config type: an explicitly typed parameter, var, receiver or
|
||||
// struct field, a short variable declaration or inferred var whose right-hand
|
||||
// side resolves to one ("cfg := doc.Config"), or a range variable over a slice
|
||||
// of them. The type is tracked by name because a name is what a Go read
|
||||
// selects on. A config value returned by a call this file does not declare is
|
||||
// outside a syntactic rule and stays untracked.
|
||||
func configReadOffenders(src []byte) []string {
|
||||
fset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fset, "config.go", src, parser.SkipObjectResolution)
|
||||
if err != nil {
|
||||
// A file that does not parse is reported as an offender rather than
|
||||
// ignored: silently skipping it would make this gate blind exactly
|
||||
// when the tree is broken.
|
||||
return []string{"parse error: " + err.Error()}
|
||||
}
|
||||
kinds := configKinds(file)
|
||||
|
||||
var offenders []string
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
sel, ok := n.(*ast.SelectorExpr)
|
||||
if !ok || !bannedConfigFields[sel.Sel.Name] {
|
||||
return true
|
||||
}
|
||||
switch base := sel.X.(type) {
|
||||
case *ast.Ident:
|
||||
if kinds[base.Name] == configValue {
|
||||
offenders = append(offenders, base.Name+"."+sel.Sel.Name)
|
||||
}
|
||||
case *ast.SelectorExpr:
|
||||
// doc.Config, or the config-typed struct field h.cfg. Config is
|
||||
// named by configuration.Document in another package, so it
|
||||
// cannot come out of the kinds map; treat a selected Config as
|
||||
// a config holder unconditionally.
|
||||
if base.Sel.Name == "Config" || kinds[base.Sel.Name] == configValue {
|
||||
offenders = append(offenders, base.Sel.Name+"."+sel.Sel.Name)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return offenders
|
||||
}
|
||||
|
||||
// configKind is how an identifier or expression reaches a config.Config value.
|
||||
type configKind int
|
||||
|
||||
const (
|
||||
notConfig configKind = iota // not a config value
|
||||
configValue // config.Config
|
||||
configSlice // []config.Config, [N]config.Config or map[K]config.Config
|
||||
)
|
||||
|
||||
// configKinds resolves the identifiers in file that hold a config.Config, in
|
||||
// two passes because an inferred holder's source must be classified before the
|
||||
// holder's own name can be.
|
||||
func configKinds(file *ast.File) map[string]configKind {
|
||||
kinds := map[string]configKind{}
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
switch node := n.(type) {
|
||||
case *ast.FuncDecl:
|
||||
classifyFields(node.Recv, kinds)
|
||||
classifyFields(node.Type.Params, kinds)
|
||||
case *ast.ValueSpec:
|
||||
classifyNames(node.Type, node.Names, kinds)
|
||||
case *ast.StructType:
|
||||
for _, field := range node.Fields.List {
|
||||
classifyNames(field.Type, field.Names, kinds)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
// Inferred holders: a single source-order pass suffices because Go
|
||||
// requires a name to be declared before it is referenced.
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
switch node := n.(type) {
|
||||
case *ast.AssignStmt:
|
||||
if node.Tok != token.DEFINE {
|
||||
return true
|
||||
}
|
||||
classifyValues(node.Lhs, node.Rhs, kinds)
|
||||
case *ast.ValueSpec:
|
||||
// An explicit type was handled above; here only "var cfg =
|
||||
// doc.Config", which infers the same way a := does.
|
||||
if node.Type != nil {
|
||||
return true
|
||||
}
|
||||
names := make([]ast.Expr, len(node.Names))
|
||||
for i, name := range node.Names {
|
||||
names[i] = name
|
||||
}
|
||||
classifyValues(names, node.Values, kinds)
|
||||
case *ast.RangeStmt:
|
||||
if exprKind(node.X, kinds) != configSlice {
|
||||
return true
|
||||
}
|
||||
if id, ok := node.Value.(*ast.Ident); ok {
|
||||
kinds[id.Name] = configValue
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return kinds
|
||||
}
|
||||
|
||||
// classifyValues records each lhs identifier that resolves to a config kind of
|
||||
// the rhs expression in the same position. A shorter lhs or rhs is fine;
|
||||
// position is what pairs a name with its value.
|
||||
func classifyValues(lhs, rhs []ast.Expr, kinds map[string]configKind) {
|
||||
for i, target := range lhs {
|
||||
if i >= len(rhs) {
|
||||
return
|
||||
}
|
||||
id, ok := target.(*ast.Ident)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if kind := exprKind(rhs[i], kinds); kind != notConfig {
|
||||
kinds[id.Name] = kind
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// classifyFields records every named field in fields whose type holds a
|
||||
// config.Config.
|
||||
func classifyFields(fields *ast.FieldList, kinds map[string]configKind) {
|
||||
if fields == nil {
|
||||
return
|
||||
}
|
||||
for _, field := range fields.List {
|
||||
classifyNames(field.Type, field.Names, kinds)
|
||||
}
|
||||
}
|
||||
|
||||
// classifyNames records names as holding the config kind of typ.
|
||||
func classifyNames(typ ast.Expr, names []*ast.Ident, kinds map[string]configKind) {
|
||||
kind := typeKind(typ)
|
||||
if kind == notConfig {
|
||||
return
|
||||
}
|
||||
for _, name := range names {
|
||||
kinds[name.Name] = kind
|
||||
}
|
||||
}
|
||||
|
||||
// exprKind resolves the config kind of an expression from the identifiers
|
||||
// already classified in kinds.
|
||||
func exprKind(expr ast.Expr, kinds map[string]configKind) configKind {
|
||||
switch e := expr.(type) {
|
||||
case *ast.Ident:
|
||||
return kinds[e.Name]
|
||||
case *ast.SelectorExpr:
|
||||
// doc.Config of a configuration.Document, or a struct field the file
|
||||
// declared as config.Config.
|
||||
if e.Sel.Name == "Config" {
|
||||
return configValue
|
||||
}
|
||||
return kinds[e.Sel.Name]
|
||||
case *ast.CompositeLit:
|
||||
return typeKind(e.Type)
|
||||
case *ast.StarExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.UnaryExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.ParenExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.IndexExpr:
|
||||
if exprKind(e.X, kinds) == configSlice {
|
||||
return configValue
|
||||
}
|
||||
}
|
||||
return notConfig
|
||||
}
|
||||
|
||||
// typeKind reports the config kind a type expression names: config.Config, or
|
||||
// a slice, array or map whose element is one.
|
||||
func typeKind(expr ast.Expr) configKind {
|
||||
switch t := expr.(type) {
|
||||
case *ast.ArrayType:
|
||||
if isConfigType(t.Elt) {
|
||||
return configSlice
|
||||
}
|
||||
case *ast.MapType:
|
||||
// A map's range value is its element; classify the map itself as a
|
||||
// slice-like source so a range over it yields config values.
|
||||
if isConfigType(t.Value) {
|
||||
return configSlice
|
||||
}
|
||||
case *ast.StarExpr:
|
||||
return typeKind(t.X)
|
||||
case *ast.ParenExpr:
|
||||
return typeKind(t.X)
|
||||
}
|
||||
if isConfigType(expr) {
|
||||
return configValue
|
||||
}
|
||||
return notConfig
|
||||
}
|
||||
|
||||
// isConfigType reports whether expr names internal/config's Config type,
|
||||
// directly or behind a pointer.
|
||||
func isConfigType(expr ast.Expr) bool {
|
||||
switch t := expr.(type) {
|
||||
case *ast.SelectorExpr:
|
||||
id, ok := t.X.(*ast.Ident)
|
||||
return ok && id.Name == "config" && t.Sel.Name == "Config"
|
||||
case *ast.StarExpr:
|
||||
return isConfigType(t.X)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TestMessagingReadsNoBannedConfigSection is the live gate: every production
|
||||
// source file in the Messaging Service's application package is scanned, and a
|
||||
// read of a PRD-forbidden section fails the build.
|
||||
func TestMessagingReadsNoBannedConfigSection(t *testing.T) {
|
||||
entries, err := os.ReadDir(messagingAppDir)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", messagingAppDir, err)
|
||||
}
|
||||
scanned := 0
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
|
||||
continue
|
||||
}
|
||||
src, err := os.ReadFile(filepath.Join(messagingAppDir, name))
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", name, err)
|
||||
}
|
||||
scanned++
|
||||
for _, offender := range configReadOffenders(src) {
|
||||
t.Errorf("%s reads %s; the Messaging Service must not read or decode that section (docs/prds/messaging-service-boundary.md, Configuration Ownership) -- the capability belongs to the model-owning process, reached over a contract", name, offender)
|
||||
}
|
||||
}
|
||||
// A gate that read no files would pass silently. Pin that it scanned the
|
||||
// package it names.
|
||||
if scanned == 0 {
|
||||
t.Fatalf("no production .go files found in %s: the gate asserted nothing", messagingAppDir)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigReadRuleCatchesTheOldWiring proves the rule fails on a synthetic
|
||||
// copy of the shape that shipped: a projection built by reading
|
||||
// [models].transcription and [providers.*]. Without this, the live gate's rule
|
||||
// could rot into a no-op and still report a pass.
|
||||
func TestConfigReadRuleCatchesTheOldWiring(t *testing.T) {
|
||||
const reverted = `package archiemessaging
|
||||
|
||||
import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/transcription"
|
||||
)
|
||||
|
||||
func project(cfg config.Config) projection {
|
||||
return projection{
|
||||
models: cfg.Models,
|
||||
providers: cfg.Providers,
|
||||
}
|
||||
}
|
||||
|
||||
func setupTranscriber(cfg config.Config) {
|
||||
_, _ = transcription.New(cfg.Models, cfg.Providers, transcription.Options{})
|
||||
}
|
||||
`
|
||||
got := configReadOffenders([]byte(reverted))
|
||||
want := []string{"cfg.Models", "cfg.Providers", "cfg.Models", "cfg.Providers"}
|
||||
if strings.Join(got, ",") != strings.Join(want, ",") {
|
||||
t.Fatalf("configReadOffenders() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigReadRuleCatchesInferredHolders covers the holder shapes the first
|
||||
// cut of this rule missed. It only saw a config.Config written out as a
|
||||
// parameter or var type, so a value reached through an inferred short
|
||||
// variable declaration ("cfg := doc.Config") or held in a struct field
|
||||
// ("h.cfg.Models") left the forbidden [models]/[providers] read intact while
|
||||
// the rule saw no holder to blame. Each case here fails red against that rule:
|
||||
// the live gate would have reported a pass with the violation still shipping.
|
||||
func TestConfigReadRuleCatchesInferredHolders(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
src string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "short var decl from a Document's Config field",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
|
||||
func f(doc configuration.Document) {
|
||||
cfg := doc.Config
|
||||
_ = cfg.Models
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
{
|
||||
name: "inferred var without an explicit type",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
|
||||
func f(doc configuration.Document) {
|
||||
var cfg = doc.Config
|
||||
_ = cfg.Models
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
{
|
||||
name: "short var decl inherits config type from another holder",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
func f(cfg config.Config) {
|
||||
other := cfg
|
||||
_ = other.Providers
|
||||
}
|
||||
`,
|
||||
want: []string{"other.Providers"},
|
||||
},
|
||||
{
|
||||
name: "config held in a struct field",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
type holder struct {
|
||||
cfg config.Config
|
||||
}
|
||||
|
||||
func f(h holder) {
|
||||
_ = h.cfg.Providers
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Providers"},
|
||||
},
|
||||
{
|
||||
name: "range variable over a slice of config",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
func f(cfgs []config.Config) {
|
||||
for _, cfg := range cfgs {
|
||||
_ = cfg.Models
|
||||
}
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := configReadOffenders([]byte(tc.src))
|
||||
if strings.Join(got, ",") != strings.Join(tc.want, ",") {
|
||||
t.Fatalf("configReadOffenders() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigReadRuleAllowsThePermittedKeys proves the rule is not a blanket
|
||||
// ban on config access: the sections the PRD does permit keep passing, and a
|
||||
// nested chat field is not mistaken for a top-level one.
|
||||
func TestConfigReadRuleAllowsThePermittedKeys(t *testing.T) {
|
||||
const permitted = `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
func project(cfg config.Config) projection {
|
||||
return projection{
|
||||
telegram: cfg.Chat.Telegram,
|
||||
email: cfg.Chat.Email,
|
||||
gateway: cfg.Services.Get("gateway").Target,
|
||||
workDir: cfg.WorkDir,
|
||||
botUser: cfg.BotUser,
|
||||
healthURL: cfg.Health.URL(),
|
||||
}
|
||||
}
|
||||
`
|
||||
if got := configReadOffenders([]byte(permitted)); len(got) != 0 {
|
||||
t.Fatalf("configReadOffenders(permitted) = %v, want none", got)
|
||||
}
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
)
|
||||
|
||||
func repoRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("runtime.Caller failed")
|
||||
}
|
||||
dir := filepath.Dir(file)
|
||||
for {
|
||||
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
||||
return dir
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
t.Fatal("could not find repo root")
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
|
||||
func buildBinary(t *testing.T) string {
|
||||
t.Helper()
|
||||
bin := filepath.Join(t.TempDir(), "archie-messaging")
|
||||
cmd := exec.CommandContext(t.Context(), "go", "build", "-o", bin, "./cmd/archie-messaging")
|
||||
cmd.Dir = repoRoot(t)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("go build archie-messaging: %v\n%s", err, string(out))
|
||||
}
|
||||
return bin
|
||||
}
|
||||
|
||||
type fakeChatServer struct {
|
||||
messaging.ChatContract
|
||||
}
|
||||
|
||||
func (f *fakeChatServer) Snapshot(ctx context.Context) (messaging.ChatSnapshot, error) {
|
||||
return messaging.ChatSnapshot{}, nil
|
||||
}
|
||||
|
||||
func startTestGateway(t *testing.T) (target string, cleanup func()) {
|
||||
t.Helper()
|
||||
lis, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("net.Listen: %v", err)
|
||||
}
|
||||
srv := grpc.NewServer()
|
||||
gatewayrpc.RegisterServer(srv, &fakeChatServer{})
|
||||
go func() {
|
||||
_ = srv.Serve(lis)
|
||||
}()
|
||||
return lis.Addr().String(), func() {
|
||||
srv.GracefulStop()
|
||||
_ = lis.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// fakeControlPlane serves the empty channel-settings resource the Messaging
|
||||
// Service reads at startup. The database is authoritative for channel
|
||||
// settings, so the process refuses to start without a State Store to read.
|
||||
type fakeControlPlane struct {
|
||||
controlpb.UnimplementedControlPlaneServiceServer
|
||||
}
|
||||
|
||||
func (f *fakeControlPlane) Catalog(context.Context, *controlpb.CatalogRequest) (*controlpb.CatalogResponse, error) {
|
||||
return &controlpb.CatalogResponse{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeControlPlane) Query(_ context.Context, request *controlpb.QueryRequest) (*controlpb.QueryResponse, error) {
|
||||
return &controlpb.QueryResponse{Resource: &controlpb.Resource{Kind: request.GetKind(), Version: 1, ValueJson: []byte("{}")}}, nil
|
||||
}
|
||||
|
||||
func startTestStateStore(t *testing.T) (target string, cleanup func()) {
|
||||
t.Helper()
|
||||
lis, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("net.Listen: %v", err)
|
||||
}
|
||||
srv := grpc.NewServer()
|
||||
controlpb.RegisterControlPlaneServiceServer(srv, &fakeControlPlane{})
|
||||
go func() {
|
||||
_ = srv.Serve(lis)
|
||||
}()
|
||||
return lis.Addr().String(), func() {
|
||||
srv.GracefulStop()
|
||||
_ = lis.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinaryHelp(t *testing.T) {
|
||||
bin := buildBinary(t)
|
||||
cmd := exec.CommandContext(t.Context(), bin, "-help")
|
||||
out, err := cmd.CombinedOutput()
|
||||
// flag.ExitOnError or flag help returns exit 0 or 2 depending on standard library
|
||||
if !strings.Contains(string(out), "gateway-target") {
|
||||
t.Fatalf("help output missing gateway-target flag: %v\n%s", err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinaryStartsAndShutsDownCleanly(t *testing.T) {
|
||||
bin := buildBinary(t)
|
||||
gatewayTarget, cleanup := startTestGateway(t)
|
||||
defer cleanup()
|
||||
stateStoreTarget, stopStateStore := startTestStateStore(t)
|
||||
defer stopStateStore()
|
||||
|
||||
cmd := exec.CommandContext(t.Context(), bin, "-gateway-target", gatewayTarget, "-state-store-target", stateStoreTarget)
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatalf("cmd.Start: %v", err)
|
||||
}
|
||||
|
||||
// Allow the process to start up.
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
// Send SIGTERM to shut down gracefully.
|
||||
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
|
||||
t.Fatalf("send SIGTERM: %v", err)
|
||||
}
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
errCh <- cmd.Wait()
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
t.Fatalf("process exited with error on SIGTERM: %v", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
_ = cmd.Process.Kill()
|
||||
t.Fatal("process did not exit in time after SIGTERM")
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestRunUnknownCommandExits2: an unrecognised subcommand is a usage error.
|
||||
func TestRunUnknownCommandExits2(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"frobnicate"}, &stderr); code != 2 {
|
||||
t.Fatalf("run(unknown) = %d, want 2", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "unknown command") {
|
||||
t.Fatalf("stderr = %q, want unknown-command message", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunNoCommandExits2: no subcommand is a usage error with the command
|
||||
// table printed (the gopls-shaped dispatch surface).
|
||||
func TestRunNoCommandExits2(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
if code := run(nil, &stderr); code != 2 {
|
||||
t.Fatalf("run(nil) = %d, want 2", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "lint") {
|
||||
t.Fatalf("stderr = %q, want lint listed in usage", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunLintCleanExits0: lint over a valid directory exits 0.
|
||||
func TestRunLintCleanExits0(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "routes.yaml"), "bug: tdd\n")
|
||||
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"lint", "-dir", dir}, &stderr); code != 0 {
|
||||
t.Fatalf("run(lint clean) = %d, want 0; stderr=%q", code, stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunLintCollisionExits1: a cross-directory collision exits 1 and names
|
||||
// the colliding key.
|
||||
func TestRunLintCollisionExits1(t *testing.T) {
|
||||
dirA := t.TempDir()
|
||||
dirB := t.TempDir()
|
||||
writeFile(t, filepath.Join(dirA, "a.yaml"), "security: security-review\n")
|
||||
writeFile(t, filepath.Join(dirB, "b.yaml"), "security: other-security\n")
|
||||
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"lint", "-dir", dirA, "-dir", dirB}, &stderr); code != 1 {
|
||||
t.Fatalf("run(lint collision) = %d, want 1; stderr=%q", code, stderr.String())
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "security") {
|
||||
t.Fatalf("stderr = %q, want the colliding label named", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunLintNoDirsExits2: lint without any -dir is a usage error.
|
||||
func TestRunLintNoDirsExits2(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"lint"}, &stderr); code != 2 {
|
||||
t.Fatalf("run(lint no dirs) = %d, want 2", code)
|
||||
}
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, path, content string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("write %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
// -eda-dir alone is a complete lint request, and its findings set the exit
|
||||
// code.
|
||||
func TestRunLintEDADir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "pb.yaml"), []byte("trigger:\n kind: bug\nactions:\n - position: module\n kind: nope\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"lint", "-eda-dir", dir}, &stderr); code != 1 {
|
||||
t.Fatalf("run(lint -eda-dir) = %d, want 1; stderr: %s", code, stderr.String())
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) }
|
||||
@@ -1,627 +0,0 @@
|
||||
// Package main smoke-tests the standalone archie-state-store process by
|
||||
// building the real binary, running it against a minimal config on the
|
||||
// loopback interface, and driving it through the State Store gRPC contract
|
||||
// exactly the way a consumer (archied, archie-gateway, archie-agent) does.
|
||||
//
|
||||
// This is the .4.7 end-to-end verification for
|
||||
// docs/prds/state-store-contract.md (rev. 2c): it proves the process owns the
|
||||
// task store, serves the combined task/event/capture
|
||||
// surface over one gRPC service, preserves error-sentinel fidelity across the
|
||||
// wire, honours a per-call deadline, survives a restart/recovery cycle, and
|
||||
// is the sole owner of the store. See §5 (binary layout), §6 (handoff),
|
||||
// §7 (error semantics), §11 (remote adapter) and §12 step 7/8 (single owner).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
)
|
||||
|
||||
// repoRoot walks up from the test's source directory to find the module root
|
||||
// (where go.mod lives). The test runs in the package directory
|
||||
// (cmd/archie-state-store), so the module root is above it; walking keeps the
|
||||
// result correct regardless of the harness working directory.
|
||||
func repoRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("cannot determine test source path")
|
||||
}
|
||||
dir := filepath.Dir(file)
|
||||
for {
|
||||
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
||||
return dir
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
t.Fatal("go.mod not found above test source")
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
|
||||
// buildBinary compiles ./cmd/archie-state-store into dir and returns the path.
|
||||
// This is the real binary the smoke test exercises, not a test double -- the
|
||||
// point of .4.7 is to verify the actual process.
|
||||
func buildBinary(t *testing.T, dir string) string {
|
||||
t.Helper()
|
||||
bin := filepath.Join(dir, "archie-state-store")
|
||||
cmd := exec.CommandContext(t.Context(), "go", "build", "-o", bin, "./cmd/archie-state-store")
|
||||
cmd.Dir = repoRoot(t)
|
||||
cmd.Env = os.Environ()
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("go build archie-state-store: %v\n%s", err, out)
|
||||
}
|
||||
if _, err := os.Stat(bin); err != nil {
|
||||
t.Fatalf("built binary not found: %v", err)
|
||||
}
|
||||
return bin
|
||||
}
|
||||
|
||||
// writeMinimalConfig writes a config.toml that passes configuration.Validate
|
||||
// while keeping the process minimal: the standalone binary takes its gRPC
|
||||
// listen address from -listen or [services.state].listen, never from the
|
||||
// target, so the config only needs the fields validation requires plus the
|
||||
// database_url it owns. The
|
||||
// forge token resolves from an env var, so no real credential is needed.
|
||||
func writeMinimalConfig(t *testing.T, dir string) string {
|
||||
t.Helper()
|
||||
return writeConfigFor(t, dir, pgtest.URL(t))
|
||||
}
|
||||
|
||||
// writeConfigFor is writeMinimalConfig naming the database at url.
|
||||
func writeConfigFor(t *testing.T, dir, url string) string {
|
||||
t.Helper()
|
||||
cfg := filepath.Join(dir, "config.toml")
|
||||
content := fmt.Sprintf(`bot_user = "archie-bot"
|
||||
database_url = %q
|
||||
[forge]
|
||||
type = "github"
|
||||
host = "https://github.example.com"
|
||||
token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" }
|
||||
[[repos]]
|
||||
owner = "acme"
|
||||
name = "widget"
|
||||
`, url)
|
||||
if err := os.WriteFile(cfg, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("write config: %v", err)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// writeConfigWithServiceListen is writeMinimalConfig plus the
|
||||
// [services.state] block that lets a test supply the listen address through
|
||||
// configuration instead of the -listen flag.
|
||||
func writeConfigWithServiceListen(t *testing.T, dir string) string {
|
||||
t.Helper()
|
||||
cfg := writeMinimalConfig(t, dir)
|
||||
f, err := os.OpenFile(cfg, os.O_APPEND|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
t.Fatalf("open config to append: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil {
|
||||
t.Fatalf("close config: %v", err)
|
||||
}
|
||||
}()
|
||||
if _, err := f.WriteString("\n[services.state]\nlisten = \"127.0.0.1:0\"\n"); err != nil {
|
||||
t.Fatalf("append [services.state]: %v", err)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// syncBuffer is a concurrency-safe buffer for a subprocess's stdout/stderr:
|
||||
// the os/exec goroutine writes while the test goroutine reads (to parse the
|
||||
// log and to report it on failure). A plain strings.Builder races under
|
||||
// -race, so both paths take the mutex.
|
||||
type syncBuffer struct {
|
||||
mu sync.Mutex
|
||||
b bytes.Buffer
|
||||
}
|
||||
|
||||
func (s *syncBuffer) Write(p []byte) (int, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.b.Write(p)
|
||||
}
|
||||
|
||||
func (s *syncBuffer) String() string {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.b.String()
|
||||
}
|
||||
|
||||
// stateStoreProcess wraps a running archie-state-store subprocess plus the
|
||||
// addresses it bound. addr is the gRPC listen address; readyAddr is the
|
||||
// readiness HTTP surface. It is stopped gracefully on cleanup by SIGTERM, the
|
||||
// signal the systemd unit ("Restart=on-failure") and the daemon use to drain
|
||||
// the server.
|
||||
type stateStoreProcess struct {
|
||||
cmd *exec.Cmd
|
||||
addr string
|
||||
readyAddr string
|
||||
log *syncBuffer
|
||||
}
|
||||
|
||||
// startStateStoreProcess starts bin with config, waiting for the gRPC listener
|
||||
// to come up and the readiness surface to answer /healthz. It parses the
|
||||
// binary's stderr log for the bound addresses because --listen 127.0.0.1:0
|
||||
// lets the process choose ephemeral ports, and the real address (not the
|
||||
// configured one) is what a consumer must dial.
|
||||
func startStateStoreProcess(t *testing.T, bin, cfg string) *stateStoreProcess {
|
||||
t.Helper()
|
||||
return startStateStore(t, bin, cfg, "127.0.0.1:0", "")
|
||||
}
|
||||
|
||||
// startStateStore is startStateStoreProcess with the listener topology and
|
||||
// bearer token exposed: the token-protected, non-loopback listener is the
|
||||
// topology every container-mode deployment profile uses
|
||||
// (docs/prds/state-store-contract.md §9), where the server installs the
|
||||
// credential interceptors on both the unary and the streaming surface.
|
||||
func startStateStore(t *testing.T, bin, cfg, listen, token string) *stateStoreProcess {
|
||||
t.Helper()
|
||||
args := []string{"-config", cfg, "-ready-addr", "127.0.0.1:0"}
|
||||
if listen != "" {
|
||||
// An empty listen means "pass no -listen at all". A test uses that to
|
||||
// prove the bound address came from [services.state].listen.
|
||||
args = append(args, "-listen", listen)
|
||||
}
|
||||
if token != "" {
|
||||
// An explicit -token wins over [services.state].target_token and the
|
||||
// STATE_STORE_TOKEN secret, so the process authenticates callers with
|
||||
// exactly this value.
|
||||
args = append(args, "-token", token)
|
||||
}
|
||||
cmd := exec.CommandContext(t.Context(), bin, args...)
|
||||
cmd.Env = append(os.Environ(), "ARCHIE_GITHUB_TOKEN=test-token")
|
||||
var log syncBuffer
|
||||
cmd.Stderr = &log
|
||||
cmd.Stdout = &log
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatalf("start state store: %v", err)
|
||||
}
|
||||
p := &stateStoreProcess{cmd: cmd, log: &log}
|
||||
t.Cleanup(func() { p.stop(t) })
|
||||
|
||||
// Recover the ephemeral ports from the JSON log lines the binary emits.
|
||||
deadline := time.Now().Add(15 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if p.parseAddrs() {
|
||||
break
|
||||
}
|
||||
if cmd.ProcessState != nil {
|
||||
t.Fatalf("state store exited early:\n%s", log.String())
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
if p.addr == "" || p.readyAddr == "" {
|
||||
t.Fatalf("did not recover bound addresses from log:\n%s", log.String())
|
||||
}
|
||||
|
||||
// The readiness surface is the liveness signal the runbook uses; poll it
|
||||
// until it answers (the gRPC listener may come up a tick ahead of it).
|
||||
deadline = time.Now().Add(15 * time.Second)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://"+p.readyAddr+"/healthz", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("build readiness request: %v", err)
|
||||
}
|
||||
for time.Now().Before(deadline) {
|
||||
if resp, err := http.DefaultClient.Do(req); err == nil {
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return p
|
||||
}
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("readiness /healthz never became healthy:\n%s", log.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseAddrs scans the captured log for the bound gRPC and readiness
|
||||
// addresses. The binary logs "archie-state-store running" with the gRPC addr
|
||||
// and "state store readiness listening" with the HTTP addr.
|
||||
func (p *stateStoreProcess) parseAddrs() bool {
|
||||
if p.addr != "" && p.readyAddr != "" {
|
||||
return true
|
||||
}
|
||||
scanner := bufio.NewScanner(strings.NewReader(p.log.String()))
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
var rec map[string]any
|
||||
if err := json.Unmarshal([]byte(line), &rec); err != nil {
|
||||
continue
|
||||
}
|
||||
if msg, _ := rec["msg"].(string); msg == "archie-state-store running" {
|
||||
if addr, ok := rec["addr"].(string); ok && addr != "" && p.addr == "" {
|
||||
p.addr = addr
|
||||
}
|
||||
}
|
||||
if msg, _ := rec["msg"].(string); msg == "state store readiness listening" {
|
||||
if addr, ok := rec["addr"].(string); ok && addr != "" && p.readyAddr == "" {
|
||||
p.readyAddr = strings.TrimPrefix(addr, "http://")
|
||||
}
|
||||
}
|
||||
}
|
||||
return p.addr != "" && p.readyAddr != ""
|
||||
}
|
||||
|
||||
// stop drains the process with SIGTERM and waits for it to exit. This is the
|
||||
// same graceful-stop path serveStateStore's signal handling implements.
|
||||
func (p *stateStoreProcess) stop(t *testing.T) {
|
||||
t.Helper()
|
||||
if p.cmd.Process == nil {
|
||||
return
|
||||
}
|
||||
_ = p.cmd.Process.Signal(syscall.SIGTERM)
|
||||
done := make(chan struct{})
|
||||
go func() { _ = p.cmd.Wait(); close(done) }()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(10 * time.Second):
|
||||
_ = p.cmd.Process.Kill()
|
||||
<-done
|
||||
}
|
||||
}
|
||||
|
||||
// contract is the combined consumer surface a daemon/gateway/agent mixes: the
|
||||
// task lifecycle + events surface (storecontract.TaskStore) and the capture surface.
|
||||
// Mapping/binding/dispatch is exercised separately in
|
||||
// TestStateStoreRealProcessRemoteSurfaces via the concrete Client, which
|
||||
// asserts against each narrow interface at compile time.
|
||||
type contract interface {
|
||||
storecontract.TaskStore
|
||||
storecontract.CaptureStore
|
||||
}
|
||||
|
||||
// dial opens a gRPC connection to the state store process and wraps it in the
|
||||
// remote contract adapter, exactly as the daemon/gateway/agent consumer does
|
||||
// (internal/infrastructure/staterpc.Client).
|
||||
func dial(t *testing.T, addr string) contract {
|
||||
t.Helper()
|
||||
conn, err := grpc.NewClient("passthrough:///"+addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatalf("dial %s: %v", addr, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = conn.Close() })
|
||||
return staterpc.NewClient(conn)
|
||||
}
|
||||
|
||||
// capture builds a CapturedEvent without pulling the whole store field set
|
||||
// into every call site.
|
||||
func capture(source, body string) storecontract.CapturedEvent {
|
||||
return storecontract.CapturedEvent{Source: source, Body: body, Authenticated: true}
|
||||
}
|
||||
|
||||
// TestStateStoreRealProcessSmoke exercises the actual archie-state-store
|
||||
// binary end-to-end: the combined task/event/capture view through one consumer
|
||||
// adapter, error-sentinel fidelity across the wire, the not-found-as-(nil,nil)
|
||||
// convention, and single-owner SQLite (the binary owns the one archie.db file
|
||||
// and the consumer dials gRPC without opening any store file).
|
||||
//
|
||||
// It also takes its listen address from [services.state].listen rather than
|
||||
// -listen, so the startup path every deployment uses is covered by the process
|
||||
// that exercises the most: before that key existed the address came from a flag
|
||||
// default with no config key, so a host whose port was already taken could
|
||||
// only be retargeted by passing -listen and
|
||||
// hand-writing an overlay whose target matched -- and the failure read as a
|
||||
// code fault, "address already in use" in the store and a gRPC handshake error
|
||||
// in every client.
|
||||
func TestStateStoreRealProcessSmoke(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
||||
}
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
bin := buildBinary(t, dir)
|
||||
cfg := writeConfigWithServiceListen(t, dir)
|
||||
st := startStateStore(t, bin, cfg, "", "")
|
||||
if st.addr == "" {
|
||||
t.Fatalf("no bound gRPC address in the log:\n%s", st.log.String())
|
||||
}
|
||||
if !strings.Contains(st.log.String(), `"listen":"127.0.0.1:0"`) {
|
||||
t.Fatalf("the resolved listen was not the configured address:\n%s", st.log.String())
|
||||
}
|
||||
cl := dial(t, st.addr)
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
// Combined task/event/capture view: a task, an event on that task, and an
|
||||
// unrelated capture written through one consumer, read back across all
|
||||
// three surfaces in a single pass (the dashboard/observability read path).
|
||||
task, err := cl.EnqueueChatTask(ctx, "acme", "widget", "verify state store", "body", "implement", "", nil)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("EnqueueChatTask = %+v, %v", task, err)
|
||||
}
|
||||
if err := cl.Transition(ctx, task.ID, task.Status, "running", "smoke started"); err != nil {
|
||||
t.Fatalf("Transition: %v", err)
|
||||
}
|
||||
if _, err := cl.InsertEvent(ctx, events.Event{Kind: "stage_finish", TaskID: task.ID, Attempt: 1, Data: map[string]any{"duration_ms": 12.0}}); err != nil {
|
||||
t.Fatalf("InsertEvent: %v", err)
|
||||
}
|
||||
// R4's document is stored as an event, so the real process must round-trip
|
||||
// it with its nested structure and its attempt key intact: the standalone
|
||||
// State Store owns the only copy, and a lost field here is a Config tab
|
||||
// that reads a run it cannot describe. The key is the daemon's own
|
||||
// (internal/daemon/daemon.go writes "document"), asserted by name below.
|
||||
if _, err := cl.InsertEvent(ctx, events.Event{
|
||||
Kind: events.KindConfigCaptured, TaskID: task.ID, Attempt: 1,
|
||||
Data: map[string]any{
|
||||
"schema": events.ConfigCapturedSchema,
|
||||
"document": map[string]any{"bot_user": "archie", "models": map[string]any{"implement": "anthropic/claude"}},
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("InsertEvent config_captured: %v", err)
|
||||
}
|
||||
if capID, err := cl.InsertCapture(ctx, capture("sentry", `{"id":1}`), 0, 0); err != nil || capID == "" {
|
||||
t.Fatalf("InsertCapture = (%q, %v)", capID, err)
|
||||
}
|
||||
|
||||
got, err := cl.TaskByID(ctx, task.ID)
|
||||
if err != nil || got == nil || got.ID != task.ID || got.Status != "running" {
|
||||
t.Fatalf("TaskByID after transition = %+v, %v", got, err)
|
||||
}
|
||||
evs, err := cl.TaskEvents(ctx, task.ID)
|
||||
if err != nil || len(evs) != 2 || evs[0].Kind != "stage_finish" {
|
||||
t.Fatalf("TaskEvents = %+v, %v", evs, err)
|
||||
}
|
||||
if evs[0].Attempt != 1 {
|
||||
t.Errorf("TaskEvents attempt = %d, want 1 across the real process", evs[0].Attempt)
|
||||
}
|
||||
if evs[1].Kind != events.KindConfigCaptured || evs[1].Attempt != 1 || evs[1].Data["schema"] != events.ConfigCapturedSchema {
|
||||
t.Errorf("config_captured event = %+v, want its kind, attempt and schema preserved", evs[1])
|
||||
}
|
||||
if _, renamed := evs[1].Data["config"]; renamed {
|
||||
t.Error(`the document crossed under "config"; the producer writes it under "document"`)
|
||||
}
|
||||
if len(evs[1].Data) != 2 {
|
||||
t.Errorf("config_captured data keys = %v, want exactly schema and document", evs[1].Data)
|
||||
}
|
||||
if doc, ok := evs[1].Data["document"].(map[string]any); !ok || doc["bot_user"] != "archie" {
|
||||
t.Errorf("config_captured payload = %#v, want the decoded document under its own key", evs[1].Data)
|
||||
}
|
||||
if caps, err := cl.ListCaptures(ctx, 10); err != nil || len(caps) != 1 || caps[0].Source != "sentry" {
|
||||
t.Fatalf("ListCaptures = %+v, %v", caps, err)
|
||||
}
|
||||
if counts, err := cl.StatusCounts(ctx); err != nil || counts["running"] != 1 {
|
||||
t.Fatalf("StatusCounts = %+v, %v", counts, err)
|
||||
}
|
||||
|
||||
t.Run("error_sentinel_fidelity", func(t *testing.T) {
|
||||
// A stale transition must rehydrate to storecontract.ErrStaleTransition across
|
||||
// the wire (§7). Because the binary is up and we dialed it, a sentinel
|
||||
// mismatch here would surface as a transport error, so a pass proves
|
||||
// the gRPC mapError/unmapError round-trip, not just the in-process
|
||||
// adapter.
|
||||
err := cl.Transition(ctx, task.ID, "queued", "merged", "")
|
||||
if !errors.Is(err, storecontract.ErrStaleTransition) {
|
||||
t.Fatalf("stale Transition = %v, want storecontract.ErrStaleTransition", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("not_found_is_not_error", func(t *testing.T) {
|
||||
// Reading a missing task returns (nil, nil), never a NotFound gRPC
|
||||
// error (§7's found=false-not-error convention).
|
||||
missing, err := cl.TaskByID(ctx, task.ID+999999)
|
||||
if err != nil || missing != nil {
|
||||
t.Fatalf("TaskByID missing = (%+v, %v), want (nil, nil)", missing, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("serves_from_postgres_only", func(t *testing.T) {
|
||||
// The binary serves every store from database_url: no SQLite file
|
||||
// appears in its directory.
|
||||
for _, name := range []string{"archie.db", "archie.db-tasks.sqlite", "archie.db-eda.sqlite"} {
|
||||
if _, err := os.Stat(filepath.Join(dir, name)); err == nil {
|
||||
t.Fatalf("state store created %s; it serves from Postgres only", name)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestStateStoreRealProcessDeadline verifies the per-call deadline behaviour
|
||||
// the state store contract requires (§6 StoreTimeout): a consumer context that
|
||||
// is cancelled before the call completes must surface a deadline/cancel error
|
||||
// rather than hang or silently succeed.
|
||||
func TestStateStoreRealProcessDeadline(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
||||
}
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
bin := buildBinary(t, dir)
|
||||
cfg := writeMinimalConfig(t, dir)
|
||||
st := startStateStoreProcess(t, bin, cfg)
|
||||
cl := dial(t, st.addr)
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
_, err := cl.StatusCounts(ctx)
|
||||
if err == nil {
|
||||
t.Fatal("StatusCounts on a cancelled context returned nil; expected a deadline/cancel error")
|
||||
}
|
||||
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("StatusCounts cancelled = %v, want context.Canceled or DeadlineExceeded", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreRealProcessRestartRecovery starts the process, writes a task,
|
||||
// stops it (SIGTERM), restarts it on the same config, and verifies the
|
||||
// task and its events were recovered from the on-disk SQLite -- the
|
||||
// restart/recovery path the runbook relies on when archie-state-store is
|
||||
// restarted as a service.
|
||||
func TestStateStoreRealProcessRestartRecovery(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
||||
}
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
bin := buildBinary(t, dir)
|
||||
cfg := writeMinimalConfig(t, dir)
|
||||
|
||||
first := startStateStoreProcess(t, bin, cfg)
|
||||
s1 := dial(t, first.addr)
|
||||
task, err := s1.EnqueueChatTask(t.Context(), "acme", "widget", "survive restart", "body", "implement", "", nil)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("first EnqueueChatTask = %+v, %v", task, err)
|
||||
}
|
||||
if _, err := s1.InsertEvent(t.Context(), events.Event{Kind: "run_started", TaskID: task.ID}); err != nil {
|
||||
t.Fatalf("first InsertEvent: %v", err)
|
||||
}
|
||||
first.stop(t)
|
||||
|
||||
// Restart on the same config; the SQLite file persists on disk.
|
||||
second := startStateStoreProcess(t, bin, cfg)
|
||||
s2 := dial(t, second.addr)
|
||||
|
||||
got, err := s2.TaskByID(t.Context(), task.ID)
|
||||
if err != nil || got == nil || got.Title != "survive restart" {
|
||||
t.Fatalf("TaskByID after restart = %+v, %v", got, err)
|
||||
}
|
||||
if evs, err := s2.TaskEvents(t.Context(), task.ID); err != nil || len(evs) != 1 {
|
||||
t.Fatalf("TaskEvents after restart = %+v, %v", evs, err)
|
||||
}
|
||||
// Stale-running work reopened on a fresh process is recovered by
|
||||
// RecoverStale, which must return without error on the restarted store.
|
||||
if _, err := s2.RecoverStale(t.Context()); err != nil {
|
||||
t.Fatalf("RecoverStale after restart: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreRealProcessTokenProtectedCaptureStream is the capture-batch
|
||||
// regression against the real binary: the token-protected, non-loopback
|
||||
// listener topology a container-mode deployment uses, read through the
|
||||
// production client wiring (staterpc.Dial) the daemon and the dashboard both
|
||||
// use. The reads the batch needs are server-streaming RPCs, whose metadata
|
||||
// the unary-only client interceptor never saw -- so before the fix this
|
||||
// failed with PermissionDenied, and no unary fallback existed to take. 20 x
|
||||
// 256 KiB is 5 MiB, past gRPC's 4 MiB unary cap, so the batch also proves the
|
||||
// streams still carry it.
|
||||
func TestStateStoreRealProcessTokenProtectedCaptureStream(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
||||
}
|
||||
t.Parallel()
|
||||
const token = "operator-secret-token"
|
||||
dir := t.TempDir()
|
||||
bin := buildBinary(t, dir)
|
||||
cfg := writeMinimalConfig(t, dir)
|
||||
st := startStateStore(t, bin, cfg, "0.0.0.0:0", token)
|
||||
|
||||
// The listener advertises the wildcard address it bound; a consumer dials
|
||||
// the host address it can reach, which is this host's loopback.
|
||||
_, port, err := net.SplitHostPort(st.addr)
|
||||
if err != nil {
|
||||
t.Fatalf("parse bound address %q: %v", st.addr, err)
|
||||
}
|
||||
cl, cleanup, err := staterpc.Dial("127.0.0.1:"+port, token)
|
||||
if err != nil {
|
||||
t.Fatalf("dial token-protected state store: %v", err)
|
||||
}
|
||||
t.Cleanup(cleanup)
|
||||
|
||||
ctx := t.Context()
|
||||
body := strings.Repeat("x", 256<<10)
|
||||
for range 20 {
|
||||
if _, err := cl.InsertCapture(ctx, capture("large", body), 0, 0); err != nil {
|
||||
t.Fatalf("InsertCapture: %v", err)
|
||||
}
|
||||
}
|
||||
captures, err := cl.ListCaptures(ctx, 20)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures over a token-protected listener: %v", err)
|
||||
}
|
||||
if len(captures) != 20 {
|
||||
t.Fatalf("got %d captures, want 20", len(captures))
|
||||
}
|
||||
for _, c := range captures {
|
||||
if c.Body != body {
|
||||
t.Fatal("capture body truncated")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreRealProcessRemoteSurfaces exercises the capture/mapping/
|
||||
// binding surfaces the standalone process fronts (the daemon/webui surfaces
|
||||
// swapped to the adapter in .4.4/.4.5), proving one process serves the full
|
||||
// combined contract a consumer mixes.
|
||||
func TestStateStoreRealProcessRemoteSurfaces(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
||||
}
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
bin := buildBinary(t, dir)
|
||||
cfg := writeMinimalConfig(t, dir)
|
||||
st := startStateStoreProcess(t, bin, cfg)
|
||||
|
||||
conn, err := grpc.NewClient("passthrough:///"+st.addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = conn.Close() })
|
||||
cl := staterpc.NewClient(conn)
|
||||
|
||||
ctx := t.Context()
|
||||
mappingID, err := cl.InsertMapping(ctx, mapping.Mapping{Name: "m1", Fields: []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}}})
|
||||
if err != nil || mappingID == "" {
|
||||
t.Fatalf("InsertMapping = (%q, %v)", mappingID, err)
|
||||
}
|
||||
if got, err := cl.GetMapping(ctx, mappingID); err != nil || got == nil || got.Name != "m1" {
|
||||
t.Fatalf("GetMapping = %+v, %v", got, err)
|
||||
}
|
||||
bindingID, err := cl.InsertBinding(ctx, binding.Binding{
|
||||
Name: "b1", Matcher: binding.Matcher{Source: "sentry"}, MappingID: mappingID,
|
||||
Workflow: "implement",
|
||||
})
|
||||
if err != nil || bindingID == "" {
|
||||
t.Fatalf("InsertBinding = (%q, %v)", bindingID, err)
|
||||
}
|
||||
// A binding starts as draft and must be edited (draft -> pending_approval)
|
||||
// then approved (pending_approval -> armed) before it is armed for a
|
||||
// source. Drive the state machine so ArmedBindingsForSource returns it.
|
||||
if err := cl.UpdateBinding(ctx, binding.Binding{
|
||||
ID: bindingID, Name: "b1", Matcher: binding.Matcher{Source: "sentry"},
|
||||
MappingID: mappingID, Workflow: "implement", Owner: "acme", Repo: "widget",
|
||||
}); err != nil {
|
||||
t.Fatalf("UpdateBinding: %v", err)
|
||||
}
|
||||
if err := cl.ApproveBinding(ctx, bindingID); err != nil {
|
||||
t.Fatalf("ApproveBinding: %v", err)
|
||||
}
|
||||
if armed, err := cl.ArmedBindingsForSource(ctx, "sentry"); err != nil || len(armed) != 1 || armed[0].ID != bindingID {
|
||||
t.Fatalf("ArmedBindingsForSource = %+v, %v", armed, err)
|
||||
}
|
||||
// Remote surfaces share error-sentinel fidelity on the wire too.
|
||||
missing := mapping.Mapping{ID: "rabsent00000000", Name: "x", Fields: []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}}}
|
||||
if err := cl.UpdateMapping(ctx, missing); !errors.Is(err, storecontract.ErrMappingNotFound) {
|
||||
t.Fatalf("UpdateMapping missing = %v, want ErrMappingNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Without -db every recovery command targets the configured PostgreSQL
|
||||
// database, so each one accepts -config and refuses, naming the key, when the
|
||||
// configuration names no database.
|
||||
func TestRecoveryWithoutDBUsesTheConfiguredDatabase(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
configPath := writeMinimalConfig(t, dir)
|
||||
raw, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read config: %v", err)
|
||||
}
|
||||
var kept []string
|
||||
for line := range strings.Lines(string(raw)) {
|
||||
if !strings.HasPrefix(line, "database_url") {
|
||||
kept = append(kept, line)
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(configPath, []byte(strings.Join(kept, "")), 0o600); err != nil {
|
||||
t.Fatalf("write config: %v", err)
|
||||
}
|
||||
tests := [][]string{
|
||||
{"backup", "-out", filepath.Join(dir, "snapshot.dump")},
|
||||
{"restore", "-from", filepath.Join(dir, "snapshot.dump")},
|
||||
{"validate"},
|
||||
{"rollback", "-kind", "settings"},
|
||||
}
|
||||
for _, args := range tests {
|
||||
t.Run(args[0], func(t *testing.T) {
|
||||
code, _, stderr := runRecoveryCmd(t, append(args, "-config", configPath)...)
|
||||
if code != 1 || !strings.Contains(stderr, "database_url") {
|
||||
t.Fatalf("%s exited %d, stderr %q; want 1 naming database_url", args[0], code, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,486 +0,0 @@
|
||||
// Package main tests the offline recovery subcommands of archie-state-store
|
||||
// against the PostgreSQL database the configuration names. They are the
|
||||
// operator's only path back when the control plane's stored settings will not
|
||||
// validate, because the daemon fails closed: the in-band remedy (replay an
|
||||
// earlier revision while the State Store is up) is unavailable in exactly the
|
||||
// case that needs it.
|
||||
//
|
||||
// The tests drive the subcommands the way an operator does -- through
|
||||
// runRecovery -- rather than through the internal helpers, so the flag surface
|
||||
// and the exit codes are part of what is verified.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archied"
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
||||
)
|
||||
|
||||
// runRecoveryCmd runs one subcommand the way main does and returns its exit
|
||||
// code with both streams captured.
|
||||
func runRecoveryCmd(t *testing.T, args ...string) (int, string, string) {
|
||||
t.Helper()
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := runRecovery(args, &stdout, &stderr)
|
||||
return code, stdout.String(), stderr.String()
|
||||
}
|
||||
|
||||
// recoveryFixture writes a config naming a fresh migrated database and returns
|
||||
// it with the task database on that database.
|
||||
func recoveryFixture(t *testing.T) (string, *pgstore.TaskDB) {
|
||||
t.Helper()
|
||||
url := pgtest.URL(t)
|
||||
configPath := writeConfigFor(t, t.TempDir(), url)
|
||||
return configPath, pgstore.On(pgstore.PoolAt(t, url))
|
||||
}
|
||||
|
||||
// seedTask writes one task so the database holds more than its settings.
|
||||
func seedTask(t *testing.T, st *pgstore.TaskDB, title string) {
|
||||
t.Helper()
|
||||
if _, err := st.EnqueueChatTask(t.Context(), "acme", "widget", title, "body", "implement", "", nil); err != nil {
|
||||
t.Fatalf("seed task %q: %v", title, err)
|
||||
}
|
||||
}
|
||||
|
||||
// execRaw runs one statement against the database the way an operator editing
|
||||
// a broken store would, bypassing every check the store applies.
|
||||
func execRaw(t *testing.T, st *pgstore.TaskDB, statement string) {
|
||||
t.Helper()
|
||||
if _, err := st.Pool.Exec(t.Context(), statement); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryValidateAcceptsAHealthyStore(t *testing.T) {
|
||||
configPath, st := recoveryFixture(t)
|
||||
seedTask(t, st, "healthy")
|
||||
seedStoreResources(t, st, configPath)
|
||||
|
||||
code, stdout, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
||||
if code != 0 {
|
||||
t.Fatalf("validate exited %d: %s", code, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "schema version") {
|
||||
t.Errorf("validate must report the schema version it verified; stdout = %q", stdout)
|
||||
}
|
||||
// The count is part of what the operator reads, and a check that reports
|
||||
// "0 stored resources" for a store full of them is worse than no check.
|
||||
if count := storedResourceCount(t, stdout); count == 0 {
|
||||
t.Errorf("validate reported no stored resources for a seeded store; stdout = %q", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// validate's verdict is only useful if it is the daemon's verdict. Boot refuses
|
||||
// on configuration.Validate over the file config with every stored resource
|
||||
// layered onto it (internal/app/archied/control_plane.go), not on the write
|
||||
// path's own per-resource decode, and the two disagree: the write path accepts
|
||||
// a scheduling policy boot rejects.
|
||||
func TestRecoveryValidateRunsTheGateBootRuns(t *testing.T) {
|
||||
configPath, st := recoveryFixture(t)
|
||||
seedStoreResources(t, st, configPath)
|
||||
|
||||
// The control: a store seeded from this config is one the daemon boots on.
|
||||
code, _, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
||||
if code != 0 {
|
||||
t.Fatalf("validate exited %d on a store the daemon starts on: %s", code, stderr)
|
||||
}
|
||||
|
||||
// The upgrade case: a release defines a kind the store does not hold yet,
|
||||
// and validate runs before the State Store has started again. The State
|
||||
// Store seeds the kind it is missing, so the store is not the reason the
|
||||
// daemon would refuse to start.
|
||||
execRaw(t, st, `DELETE FROM resources WHERE kind='provider-settings'`)
|
||||
code, stdout, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
||||
if code != 0 {
|
||||
t.Fatalf("validate refused a store missing a kind the State Store seeds; stderr = %q", stderr)
|
||||
}
|
||||
if count := storedResourceCount(t, stdout); count == 0 {
|
||||
t.Errorf("validate must still check the kinds the store does hold; stdout = %q", stdout)
|
||||
}
|
||||
|
||||
// A hand-edited value, or an older revision of one: the write path's own
|
||||
// validator never inspects dispatch.trigger, so only the config gate
|
||||
// catches it, and the daemon exits 1 on it (validateDispatch).
|
||||
execRaw(t, st, `UPDATE resources SET value='{"poll_interval":"1m","max_retries":3,"dispatch":{"trigger":"bogus"}}' WHERE kind='scheduling-policy'`)
|
||||
code, stdout, stderr = runRecoveryCmd(t, "validate", "-config", configPath)
|
||||
if code == 0 {
|
||||
t.Fatalf("validate accepted a stored policy the daemon refuses to boot with; stdout = %q", stdout)
|
||||
}
|
||||
if !strings.Contains(stderr, "dispatch.trigger") {
|
||||
t.Errorf("refusal must name what boot rejects; stderr = %q", stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A database the State Store never seeded holds no kinds at all. Its next
|
||||
// start seeds every kind from this same config, so archied layers those seeds
|
||||
// and starts: refusing it would send an operator to restore a snapshot for a
|
||||
// database the daemon boots on perfectly well.
|
||||
func TestRecoveryValidateAcceptsAnUnseededDatabase(t *testing.T) {
|
||||
configPath, _ := recoveryFixture(t)
|
||||
code, stdout, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
||||
if code != 0 {
|
||||
t.Fatalf("validate refused a store the State Store would seed on its next start; stderr = %q", stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "0 stored resources validate") {
|
||||
t.Errorf("validate must report that there was nothing stored to check; stdout = %q", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// The subcommands advertise -h as the way to read their flags, and the serve
|
||||
// path in the same binary exits 0 for the same request, so a script probing the
|
||||
// recovery surface must not read a requested help as a failure.
|
||||
func TestRecoveryHelpExitsZero(t *testing.T) {
|
||||
commands := []string{archied.RecoveryBackup, archied.RecoveryRestore, archied.RecoveryValidate, archied.RecoveryRollback}
|
||||
for _, command := range commands {
|
||||
code, _, stderr := runRecoveryCmd(t, command, "-h")
|
||||
if code != 0 {
|
||||
t.Errorf("%s -h exited %d, want 0; stderr = %q", command, code, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// validate diagnoses a deployment; it is not the deployment. Resolving the boot
|
||||
// config must not create, append to, or rotate the daemon's log file or the
|
||||
// directory it lives in: a line stamped component="daemon" in archied's log is
|
||||
// indistinguishable from the daemon having written it.
|
||||
func TestRecoveryValidateLeavesTheDaemonLogAlone(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
configPath, logFile := writeConfigWithLogFile(t, dir)
|
||||
code, _, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
||||
if code != 0 {
|
||||
t.Fatalf("validate exited %d: %s", code, stderr)
|
||||
}
|
||||
if _, err := os.Stat(logFile); !os.IsNotExist(err) {
|
||||
t.Fatalf("validate touched the daemon's log file %s (stat err = %v)", logFile, err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Dir(logFile)); !os.IsNotExist(err) {
|
||||
t.Errorf("validate created the log directory %s (stat err = %v)", filepath.Dir(logFile), err)
|
||||
}
|
||||
}
|
||||
|
||||
// validate's verdict is the daemon's verdict only if it reads the configuration
|
||||
// the daemon reads, and with no -config it reads whatever the one shared
|
||||
// helper resolves. The resolution rule itself is pinned where it lives
|
||||
// (internal/infrastructure/configuration); what only this test can pin is that
|
||||
// the command takes its default from that helper rather than deriving it.
|
||||
//
|
||||
// A relative XDG_CONFIG_HOME is the case that separates the two: the command
|
||||
// has to resolve ${XDG_CONFIG_HOME}/archie/config.toml relative to its working
|
||||
// directory, and a second derivation through os.UserConfigDir rejects a
|
||||
// relative value outright, leaving the command with no config to answer about.
|
||||
func TestRecoveryDefaultConfigResolvesWhereTheDaemonReads(t *testing.T) {
|
||||
// The second layout is the teeth: the command has to read the config at the
|
||||
// resolved path, not merely find some config somewhere under the config home.
|
||||
// A resolution that drifts finds nothing and validate has no verdict.
|
||||
tests := []struct {
|
||||
name string
|
||||
configDir string // relative to $XDG_CONFIG_HOME
|
||||
wantOK bool
|
||||
}{
|
||||
{name: "$XDG_CONFIG_HOME/archie/config.toml", configDir: "archie", wantOK: true},
|
||||
{name: "a config elsewhere in the config home is not the default", configDir: "wrong-place"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
t.Chdir(root)
|
||||
t.Setenv("XDG_CONFIG_HOME", "xdg")
|
||||
|
||||
configDir := filepath.Join(root, "xdg", tc.configDir)
|
||||
if err := os.MkdirAll(configDir, 0o700); err != nil {
|
||||
t.Fatalf("create %s: %v", configDir, err)
|
||||
}
|
||||
url := pgtest.URL(t)
|
||||
pgstore.PoolAt(t, url)
|
||||
configPath := writeConfigFor(t, configDir, url)
|
||||
code, stdout, stderr := runRecoveryCmd(t, "validate")
|
||||
if !tc.wantOK {
|
||||
if code == 0 {
|
||||
t.Fatalf("validate exited 0 with no config at the resolved path %s: %s", configPath, stdout)
|
||||
}
|
||||
return
|
||||
}
|
||||
if code != 0 {
|
||||
t.Fatalf("validate with no -config exited %d, so it did not read %s: %s", code, configPath, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "stored resources validate") {
|
||||
t.Errorf("validate reported %q, want the verdict on the database the resolved config names", stdout)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// validate exists to answer "would archied start against this database". A
|
||||
// stored value that both gates refuse -- the write path stops it at Decode
|
||||
// (workflow.ExecutionSettings.Validate rejects a negative limit) and boot stops
|
||||
// on it too -- can only get here by bypassing the write path, which is exactly
|
||||
// the state the operator has no path back from.
|
||||
func TestRecoveryValidateRefusesWhatTheDaemonRefuses(t *testing.T) {
|
||||
configPath, st := recoveryFixture(t)
|
||||
seedResource(t, st, controlplane.WorkflowExecutionSettingsKind,
|
||||
`{"max_model_tool_steps":-1,"max_runtime_seconds":60,"max_consecutive_gate_failures":3}`, "hand-edited")
|
||||
code, _, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
||||
if code == 0 {
|
||||
t.Fatal("validate accepted a stored resource the write path would refuse")
|
||||
}
|
||||
if !strings.Contains(stderr, controlplane.WorkflowExecutionSettingsKind) {
|
||||
t.Errorf("refusal must name the resource kind; stderr = %q", stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// rollback is the one operation that closes the documented gap: with the State
|
||||
// Store stopped, it replays the value an earlier revision recorded through the
|
||||
// ordinary replace, so no rollback RPC is needed and the rollback itself is
|
||||
// audited as one more revision.
|
||||
func TestRecoveryRollbackReplaysTheRevisionThroughReplace(t *testing.T) {
|
||||
configPath, st := recoveryFixture(t)
|
||||
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"anthropic/claude"}`, "first")
|
||||
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"openai/gpt"}`, "second")
|
||||
|
||||
code, stdout, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind)
|
||||
if code != 0 {
|
||||
t.Fatalf("rollback exited %d: %s", code, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, controlplane.ModelRoleAssignmentsKind) {
|
||||
t.Errorf("rollback must name the resource it replayed; stdout = %q", stdout)
|
||||
}
|
||||
resource, err := st.Resource(t.Context(), storecontract.DefaultOrgID, controlplane.ModelRoleAssignmentsKind)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(resource.Value) != `{"implement":"anthropic/claude"}` {
|
||||
t.Errorf("resource value = %s, want the first revision's value", resource.Value)
|
||||
}
|
||||
if resource.Version != 3 {
|
||||
t.Errorf("resource version = %d, want a new revision (3) rather than a rewrite of the old one", resource.Version)
|
||||
}
|
||||
history, err := st.ResourceHistory(t.Context(), storecontract.DefaultOrgID, controlplane.ModelRoleAssignmentsKind, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(history) != 3 {
|
||||
t.Fatalf("history has %d revisions, want the rollback recorded as the third", len(history))
|
||||
}
|
||||
if history[0].Source != archied.OfflineRollbackSource {
|
||||
t.Errorf("newest revision source = %q, want %q so the rollback is auditable", history[0].Source, archied.OfflineRollbackSource)
|
||||
}
|
||||
}
|
||||
|
||||
// The revision to restore may be named, which is how an operator reaches past
|
||||
// the most recent change without replaying it first.
|
||||
func TestRecoveryRollbackRestoresANamedRevision(t *testing.T) {
|
||||
configPath, st := recoveryFixture(t)
|
||||
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"anthropic/claude"}`, "first")
|
||||
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"openai/gpt"}`, "second")
|
||||
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"google/gemini"}`, "third")
|
||||
|
||||
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind, "-revision", "1")
|
||||
if code != 0 {
|
||||
t.Fatalf("rollback exited %d: %s", code, stderr)
|
||||
}
|
||||
resource, err := st.Resource(t.Context(), storecontract.DefaultOrgID, controlplane.ModelRoleAssignmentsKind)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(resource.Value) != `{"implement":"anthropic/claude"}` {
|
||||
t.Errorf("resource value = %s, want the value recorded at revision 1", resource.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryRollbackRefusesWhatItCannotReplay(t *testing.T) {
|
||||
newStore := func(t *testing.T, revisions int) (string, *pgstore.TaskDB) {
|
||||
t.Helper()
|
||||
configPath, st := recoveryFixture(t)
|
||||
for i := range revisions {
|
||||
value := `{"implement":"anthropic/claude"}`
|
||||
if i > 0 {
|
||||
value = `{"implement":"openai/gpt"}`
|
||||
}
|
||||
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, value, "seed")
|
||||
}
|
||||
return configPath, st
|
||||
}
|
||||
|
||||
t.Run("unknown_kind", func(t *testing.T) {
|
||||
configPath, _ := newStore(t, 2)
|
||||
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", "not-a-resource")
|
||||
if code == 0 {
|
||||
t.Fatal("rollback of an unknown resource kind succeeded")
|
||||
}
|
||||
if !strings.Contains(stderr, "not-a-resource") {
|
||||
t.Errorf("refusal must name the kind; stderr = %q", stderr)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no_earlier_revision", func(t *testing.T) {
|
||||
configPath, _ := newStore(t, 1)
|
||||
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind)
|
||||
if code == 0 {
|
||||
t.Fatal("rollback with no earlier revision succeeded")
|
||||
}
|
||||
if !strings.Contains(stderr, "no earlier revision") {
|
||||
t.Errorf("refusal must say there is nothing to roll back to; stderr = %q", stderr)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("current_revision", func(t *testing.T) {
|
||||
configPath, _ := newStore(t, 2)
|
||||
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind, "-revision", "2")
|
||||
if code == 0 {
|
||||
t.Fatal("rollback of the current revision succeeded")
|
||||
}
|
||||
if !strings.Contains(stderr, "is the current version") {
|
||||
t.Errorf("refusal must say the revision is already the current value; stderr = %q", stderr)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("live_state_store", func(t *testing.T) {
|
||||
configPath, st := newStore(t, 2)
|
||||
claim, err := postgres.AcquireOwnership(t.Context(), st.Pool, postgres.OwnerStateStore)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = claim.Release(t.Context()) })
|
||||
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind)
|
||||
if code == 0 {
|
||||
t.Fatal("rollback against a live State Store succeeded")
|
||||
}
|
||||
if !strings.Contains(stderr, "stop the State Store") {
|
||||
t.Errorf("refusal must say to stop the State Store; stderr = %q", stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A positional argument that is not a subcommand used to be ignored, leaving
|
||||
// an operator who mistyped a command watching a server start instead.
|
||||
func TestRecoveryRejectsAnUnknownSubcommand(t *testing.T) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := runRecovery([]string{"backp", "-out", "x"}, &stdout, &stderr)
|
||||
if code == 0 {
|
||||
t.Fatal("an unknown subcommand exited 0")
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "backp") {
|
||||
t.Errorf("refusal must name the unknown subcommand; stderr = %q", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// writeConfigWithLogFile is writeMinimalConfig plus the daemon's durable log
|
||||
// destination, which only the daemon may bring into existence.
|
||||
func writeConfigWithLogFile(t *testing.T, dir string) (string, string) {
|
||||
t.Helper()
|
||||
url := pgtest.URL(t)
|
||||
pgstore.PoolAt(t, url)
|
||||
configPath := writeConfigFor(t, dir, url)
|
||||
logFile := filepath.Join(dir, "logs", "archied.log")
|
||||
f, err := os.OpenFile(configPath, os.O_APPEND|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
t.Fatalf("open config to append: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil {
|
||||
t.Fatalf("close config: %v", err)
|
||||
}
|
||||
}()
|
||||
if _, err := fmt.Fprintf(f, "\n[log]\nfile = %q\n", logFile); err != nil {
|
||||
t.Fatalf("append [log]: %v", err)
|
||||
}
|
||||
return configPath, logFile
|
||||
}
|
||||
|
||||
// seedStoreResources seeds the store the way the State Store seeds it at boot,
|
||||
// from the same config file the operator hands validate: the command's verdict
|
||||
// is about the pair, and a store whose settings never matched the config is a
|
||||
// state boot does not recognise either.
|
||||
//
|
||||
// The seeding builds its control-plane server from the shared provider set
|
||||
// (internal/infrastructure/workflowsteps), which is what every composition root
|
||||
// registers -- including openStateStoreControlPlane, the root helper the
|
||||
// recovery path uses. This test cannot call that unexported helper, so it
|
||||
// registers the same set itself rather than an empty manager: the definitions
|
||||
// are decoded with the vocabulary the served path validates against, which is
|
||||
// what makes the seeded values the ones validate is asked about.
|
||||
func seedStoreResources(t *testing.T, st *pgstore.TaskDB, configPath string) {
|
||||
t.Helper()
|
||||
doc, err := configuration.New(nil).Resolve(configPath, "")
|
||||
if err != nil {
|
||||
t.Fatalf("resolve %s: %v", configPath, err)
|
||||
}
|
||||
steps, err := workflowsteps.NewManager()
|
||||
if err != nil {
|
||||
t.Fatalf("register workflow step vocabulary: %v", err)
|
||||
}
|
||||
server, err := controlplane.NewServer(st, steps)
|
||||
if err != nil {
|
||||
t.Fatalf("build control plane server: %v", err)
|
||||
}
|
||||
_, skipped, err := server.ImportConfig(t.Context(), doc.Config)
|
||||
if err != nil {
|
||||
t.Fatalf("seed store resources: %v", err)
|
||||
}
|
||||
if len(skipped) > 0 {
|
||||
// This helper stands in for the State Store's own seeding, which seeds
|
||||
// every kind it can and leaves the rest absent. A skipped kind here
|
||||
// would mean the fixture store is missing a resource it claims to hold,
|
||||
// and the verdict under test would be about a different store.
|
||||
t.Fatalf("seed store resources: %d kind(s) refused: %+v", len(skipped), skipped)
|
||||
}
|
||||
}
|
||||
|
||||
// storedResourceCount reads the count validate reports out of its summary line.
|
||||
func storedResourceCount(t *testing.T, stdout string) int {
|
||||
t.Helper()
|
||||
before, found := strings.CutSuffix(stdout, " stored resources validate\n")
|
||||
if !found {
|
||||
t.Fatalf("no resource count in %q", stdout)
|
||||
}
|
||||
fields := strings.Fields(before)
|
||||
count, err := strconv.Atoi(fields[len(fields)-1])
|
||||
if err != nil {
|
||||
t.Fatalf("resource count in %q: %v", stdout, err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// seedResource writes one revision of a control-plane resource through the
|
||||
// store, the way the control plane's replace does. The request ID is derived
|
||||
// from the label and the revision it produces: the store deduplicates a
|
||||
// repeated request ID, so a fixed one would make the second write of a series
|
||||
// a silent no-op.
|
||||
func seedResource(t *testing.T, st *pgstore.TaskDB, kind, value, label string) {
|
||||
t.Helper()
|
||||
current, err := st.Resource(t.Context(), storecontract.DefaultOrgID, kind)
|
||||
expected := int64(0)
|
||||
if err == nil {
|
||||
expected = current.Version
|
||||
}
|
||||
var document any
|
||||
if err := json.Unmarshal([]byte(value), &document); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encoded, err := json.Marshal(document)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := st.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: kind, Value: encoded, Actor: label, Source: "test",
|
||||
RequestID: fmt.Sprintf("%s-%d", label, expected+1), ExpectedVersion: expected,
|
||||
}); err != nil {
|
||||
t.Fatalf("seed resource %s: %v", kind, err)
|
||||
}
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
// This file enforces the objective half of the UI Service deletion gate
|
||||
// (docs/prds/ui-service-boundary.md, "The deletion gate is objective"). The
|
||||
// composition half -- no config.Holder, no concrete task store, no Gateway
|
||||
// runtime type -- is pinned by internal/app/archieui's
|
||||
// TestComposeUIServerHoldsNoDaemonState. This pins the claim that test cannot
|
||||
// see: that the linked binary carries no daemon runtime at all. Go imports
|
||||
// are atomic, so a single reference to a type living beside a runtime relinks
|
||||
// the runtime, and the severance holds only while something re-measures it.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const modulePath = "github.com/samcharles93/archie-core/"
|
||||
|
||||
// bannedCategory is one class of runtime the UI process must not link. The
|
||||
// names are the PRD's wording; the prefixes are how that class appears in
|
||||
// this module.
|
||||
type bannedCategory struct {
|
||||
name string
|
||||
prefixes []string
|
||||
}
|
||||
|
||||
var deletionGate = []bannedCategory{
|
||||
{"SQL/store implementation", []string{
|
||||
"modernc.org/sqlite",
|
||||
"github.com/jackc/pgx",
|
||||
modulePath + "internal/infrastructure/postgres",
|
||||
}},
|
||||
{"daemon runtime", []string{
|
||||
modulePath + "internal/daemon",
|
||||
modulePath + "internal/app/archied",
|
||||
modulePath + "internal/container",
|
||||
modulePath + "internal/worktree",
|
||||
modulePath + "internal/worktreerpc",
|
||||
}},
|
||||
{"workflow engine", []string{
|
||||
modulePath + "internal/domain/workflow",
|
||||
modulePath + "internal/taskrun",
|
||||
modulePath + "internal/agentexec",
|
||||
}},
|
||||
{"forge client", []string{
|
||||
modulePath + "internal/forge",
|
||||
modulePath + "internal/forgerpc",
|
||||
}},
|
||||
{"channel runtime", []string{
|
||||
modulePath + "internal/channels",
|
||||
}},
|
||||
{"model runtime", []string{
|
||||
modulePath + "internal/gateway",
|
||||
modulePath + "internal/infrastructure/modelcatalog",
|
||||
modulePath + "internal/domain/embedding",
|
||||
modulePath + "internal/infrastructure/embedding",
|
||||
}},
|
||||
{"secret runtime", []string{
|
||||
modulePath + "internal/secret",
|
||||
}},
|
||||
{"agent tooling", []string{
|
||||
modulePath + "internal/tools",
|
||||
modulePath + "internal/skill",
|
||||
modulePath + "internal/skillscript",
|
||||
modulePath + "internal/yaegiutil",
|
||||
modulePath + "internal/plugin",
|
||||
}},
|
||||
{"memory and curator runtime", []string{
|
||||
modulePath + "internal/domain/memory",
|
||||
modulePath + "internal/infrastructure/memory",
|
||||
modulePath + "internal/domain/curator",
|
||||
modulePath + "internal/infrastructure/sessioncurator",
|
||||
modulePath + "internal/infrastructure/skillcurator",
|
||||
}},
|
||||
{"broker transport", []string{
|
||||
modulePath + "internal/eventbus",
|
||||
modulePath + "internal/infrastructure/eventbus",
|
||||
modulePath + "internal/natsrpc",
|
||||
}},
|
||||
}
|
||||
|
||||
// bannedExact is banned as a package rather than as a subtree. Nothing may
|
||||
// reach the database through the UI process, but database/sql/driver and its
|
||||
// support package arrive from github.com/google/uuid's Valuer/Scanner methods
|
||||
// and bring no driver with them.
|
||||
var bannedExact = map[string]string{
|
||||
"database/sql": "SQL/store implementation",
|
||||
}
|
||||
|
||||
// gateExceptions are packages under a banned prefix that carry only contract,
|
||||
// vocabulary or view types. The severance moved the UI's references here so
|
||||
// the parent runtime would stop linking, which makes them the intended end
|
||||
// state rather than a tolerated leak.
|
||||
//
|
||||
// Each one must stay linked. An exception the UI no longer needs is a hole in
|
||||
// the fence, so this list is checked in both directions.
|
||||
var gateExceptions = []string{
|
||||
modulePath + "internal/channels/status",
|
||||
modulePath + "internal/domain/workflow/task",
|
||||
}
|
||||
|
||||
// under reports whether importPath is prefix or a package below it.
|
||||
func under(importPath, prefix string) bool {
|
||||
return importPath == prefix || strings.HasPrefix(importPath, prefix+"/")
|
||||
}
|
||||
|
||||
func TestUIProcessLinksNoDaemonRuntime(t *testing.T) {
|
||||
listed, err := exec.CommandContext(t.Context(), "go", "list", "-deps", ".").CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("go list -deps .: %v\n%s", err, listed)
|
||||
}
|
||||
|
||||
linkedExceptions := make(map[string]bool, len(gateExceptions))
|
||||
for importPath := range strings.FieldsSeq(string(listed)) {
|
||||
if excepted(importPath, linkedExceptions) {
|
||||
continue
|
||||
}
|
||||
if category, banned := bannedExact[importPath]; banned {
|
||||
reportLink(t, importPath, category)
|
||||
}
|
||||
for _, category := range deletionGate {
|
||||
for _, prefix := range category.prefixes {
|
||||
if under(importPath, prefix) {
|
||||
reportLink(t, importPath, category.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, exception := range gateExceptions {
|
||||
if !linkedExceptions[exception] {
|
||||
t.Errorf("%s is excepted from the deletion gate but is no longer linked; drop the exception instead of leaving its prefix open", exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func reportLink(t *testing.T, importPath, category string) {
|
||||
t.Helper()
|
||||
t.Errorf("archie-ui links %s (%s); the UI process reaches that capability over a contract, never in-process (docs/prds/ui-service-boundary.md, deletion gate)", importPath, category)
|
||||
}
|
||||
|
||||
// excepted reports whether importPath is allowed past the gate, recording
|
||||
// which exception admitted it so a stale one is reported.
|
||||
func excepted(importPath string, linked map[string]bool) bool {
|
||||
for _, exception := range gateExceptions {
|
||||
if under(importPath, exception) {
|
||||
linked[exception] = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -1,960 +0,0 @@
|
||||
// Package main's smoke test runs the real archie-ui binary against a State
|
||||
// Store and a Gateway reached over loopback gRPC, and drives it through HTTP
|
||||
// the way an operator's browser does.
|
||||
//
|
||||
// The in-process suite (internal/app/archieui) proves the composition; this
|
||||
// proves the process: flags and configuration file, listener bind, dashboard
|
||||
// token, SPA assets, streamed chat, an operator task action crossing to the
|
||||
// Gateway, the degradations the PRD ratified, dependency loss while running,
|
||||
// and a clean SIGTERM. See docs/prds/ui-service-boundary.md, migration
|
||||
// sequence step 6.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
const dashboardToken = "smoke-token"
|
||||
|
||||
// fakeGateway stands in for the archie-gateway process. The dashboard only
|
||||
// needs the Gateway to answer, stream, and accept an operator action, so the
|
||||
// rest satisfy messaging.ChatContract without pretending to do more.
|
||||
type fakeGateway struct {
|
||||
mu sync.Mutex
|
||||
actions []string
|
||||
}
|
||||
|
||||
func (f *fakeGateway) Snapshot(context.Context) (messaging.ChatSnapshot, error) {
|
||||
return messaging.ChatSnapshot{
|
||||
Sessions: []messaging.SessionContext{{
|
||||
SessionID: "s1",
|
||||
Source: messaging.SessionSource{Platform: "web", ChannelID: "dash"},
|
||||
}},
|
||||
Models: []string{"test/model"},
|
||||
ActiveModel: "test/model",
|
||||
ActivePersonas: map[string]string{},
|
||||
CancellationAvailable: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f *fakeGateway) GetSession(context.Context, string) (messaging.SessionContext, bool, error) {
|
||||
return messaging.SessionContext{}, false, nil
|
||||
}
|
||||
|
||||
func (f *fakeGateway) RecentMessages(context.Context, string, int) ([]messaging.Message, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakeGateway) RecentTurns(context.Context, string, int) ([]messaging.TurnRecord, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakeGateway) Route(context.Context, messaging.Inbound) (messaging.ChatReply, error) {
|
||||
return messaging.ChatReply{Text: "routed", SessionID: "s1"}, nil
|
||||
}
|
||||
|
||||
// Stream emits the contract's documented order: started, a delta, then done.
|
||||
//
|
||||
// A turn that used a tool emits its tool event between the delta and the done,
|
||||
// which is where the Gateway's own stream reports completed tool activity.
|
||||
// Whether the dashboard renders it is the operator's setting, carried to this
|
||||
// process only by the published configuration projection (GitHub #821).
|
||||
func (f *fakeGateway) Stream(ctx context.Context, in messaging.Inbound) (<-chan messaging.ChatEvent, error) {
|
||||
events := make(chan messaging.ChatEvent, 4)
|
||||
go func() {
|
||||
defer close(events)
|
||||
turn := []messaging.ChatEvent{
|
||||
{Kind: "started", SessionID: "s1"},
|
||||
{Kind: "delta", Text: "hello " + in.Message.Text, SessionID: "s1"},
|
||||
{Kind: "done", SessionID: "s1"},
|
||||
}
|
||||
if in.Message.Text == toolCallPrompt {
|
||||
turn = []messaging.ChatEvent{
|
||||
{Kind: "started", SessionID: "s1"},
|
||||
{Kind: "tool", SessionID: "s1", Tool: messaging.ToolCallEvent{
|
||||
ID: "call-1", Name: "read_file", Parameters: `{"path":"main.go"}`,
|
||||
Output: "package main",
|
||||
}},
|
||||
{Kind: "delta", Text: "read it", SessionID: "s1"},
|
||||
{Kind: "done", SessionID: "s1"},
|
||||
}
|
||||
}
|
||||
for _, event := range turn {
|
||||
select {
|
||||
case events <- event:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return events, nil
|
||||
}
|
||||
|
||||
// toolCallPrompt is the message this suite sends when it wants the fake
|
||||
// Gateway to report a completed tool call in the turn.
|
||||
const toolCallPrompt = "read main.go"
|
||||
|
||||
func (f *fakeGateway) Cancel(context.Context, string) (messaging.ChatCancellation, error) {
|
||||
return messaging.ChatCancellation{Cancelled: true}, nil
|
||||
}
|
||||
|
||||
func (f *fakeGateway) SetPersona(context.Context, string, string) (bool, error) { return false, nil }
|
||||
|
||||
func (f *fakeGateway) ApplyTaskAction(context.Context, string, int64, taskstate.Action, taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
return messaging.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeGateway) ApplyOperatorTaskAction(_ context.Context, actor taskactions.Actor, id int64, action taskstate.Action, _ taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.actions = append(f.actions, fmt.Sprintf("%d:%s", id, action))
|
||||
return messaging.TaskActionResult{TaskID: id, Action: string(action)}, nil
|
||||
}
|
||||
|
||||
func (f *fakeGateway) applied() []string {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return append([]string(nil), f.actions...)
|
||||
}
|
||||
|
||||
// repoRoot walks up from this test's source directory to the module root, so
|
||||
// the build works regardless of the harness working directory.
|
||||
func repoRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("cannot determine test source path")
|
||||
}
|
||||
dir := filepath.Dir(file)
|
||||
for {
|
||||
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
||||
return dir
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
t.Fatal("go.mod not found above test source")
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
|
||||
func buildUIBinary(t *testing.T, dir string) string {
|
||||
t.Helper()
|
||||
bin := filepath.Join(dir, "archie-ui")
|
||||
cmd := exec.CommandContext(t.Context(), "go", "build", "-o", bin, "./cmd/archie-ui")
|
||||
cmd.Dir = repoRoot(t)
|
||||
cmd.Env = os.Environ()
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("go build archie-ui: %v\n%s", err, out)
|
||||
}
|
||||
return bin
|
||||
}
|
||||
|
||||
// syncBuffer is a concurrency-safe sink for the subprocess's stderr: the
|
||||
// os/exec goroutine writes while the test reads it to recover the bound
|
||||
// address and to report it on failure.
|
||||
type syncBuffer struct {
|
||||
mu sync.Mutex
|
||||
b bytes.Buffer
|
||||
}
|
||||
|
||||
func (s *syncBuffer) Write(p []byte) (int, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.b.Write(p)
|
||||
}
|
||||
|
||||
func (s *syncBuffer) String() string {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.b.String()
|
||||
}
|
||||
|
||||
// serveGRPC runs register against a fresh loopback listener, returning the
|
||||
// dial target and a stop func so a test can take the dependency away.
|
||||
func serveGRPC(t *testing.T, register func(grpc.ServiceRegistrar)) (target string, stop func()) {
|
||||
t.Helper()
|
||||
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
server := grpc.NewServer()
|
||||
register(server)
|
||||
go func() { _ = server.Serve(listener) }()
|
||||
t.Cleanup(server.Stop)
|
||||
return listener.Addr().String(), server.Stop
|
||||
}
|
||||
|
||||
// writeUIConfig writes the config.toml an operator would keep: the service
|
||||
// endpoints the UI dials live in it, so the process is started without
|
||||
// -gateway-target or -state-target and has to read them itself. The rest is
|
||||
// what configuration validation requires of any archie config file.
|
||||
func writeUIConfig(t *testing.T, dir, stateTarget, gatewayTarget string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
content := fmt.Sprintf(`bot_user = "archie-bot"
|
||||
[forge]
|
||||
type = "github"
|
||||
host = "https://github.example.com"
|
||||
token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" }
|
||||
[[repos]]
|
||||
owner = "acme"
|
||||
name = "widget"
|
||||
[services.state]
|
||||
target = %q
|
||||
[services.gateway]
|
||||
target = %q
|
||||
`, stateTarget, gatewayTarget)
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("write ui config: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// uiProcess is a running archie-ui, plus the address it actually bound.
|
||||
type uiProcess struct {
|
||||
cmd *exec.Cmd
|
||||
addr string
|
||||
log *syncBuffer
|
||||
}
|
||||
|
||||
func startUIProcess(t *testing.T, bin, cfg string) *uiProcess {
|
||||
t.Helper()
|
||||
// The process outlives t.Context() deliberately: that context is
|
||||
// cancelled just before cleanups run, which would SIGKILL archie-ui out
|
||||
// from under the graceful-shutdown check in stop. Cancelling this one is
|
||||
// registered first, so it runs last and only backstops a process that
|
||||
// ignored SIGTERM.
|
||||
ctx, cancel := context.WithCancel(context.WithoutCancel(t.Context()))
|
||||
t.Cleanup(cancel)
|
||||
cmd := exec.CommandContext(
|
||||
ctx, bin,
|
||||
"-config", cfg,
|
||||
"-listen", "127.0.0.1:0",
|
||||
"-token", dashboardToken,
|
||||
)
|
||||
cmd.Env = append(os.Environ(), "ARCHIE_GITHUB_TOKEN=test-token")
|
||||
var log syncBuffer
|
||||
cmd.Stderr = &log
|
||||
cmd.Stdout = &log
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatalf("start archie-ui: %v", err)
|
||||
}
|
||||
p := &uiProcess{cmd: cmd, log: &log}
|
||||
t.Cleanup(func() { p.stop(t) })
|
||||
|
||||
// -listen 127.0.0.1:0 means the kernel picks the port, so the bound
|
||||
// address comes back out of the process's own startup log.
|
||||
deadline := time.Now().Add(20 * time.Second)
|
||||
for time.Now().Before(deadline) && p.addr == "" {
|
||||
if cmd.ProcessState != nil {
|
||||
t.Fatalf("archie-ui exited early:\n%s", log.String())
|
||||
}
|
||||
p.parseAddr()
|
||||
if p.addr == "" {
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
if p.addr == "" {
|
||||
t.Fatalf("did not recover the bound address from the log:\n%s", log.String())
|
||||
}
|
||||
waitHealthy(t, p)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *uiProcess) parseAddr() {
|
||||
scanner := bufio.NewScanner(strings.NewReader(p.log.String()))
|
||||
for scanner.Scan() {
|
||||
var record map[string]any
|
||||
if err := json.Unmarshal(scanner.Bytes(), &record); err != nil {
|
||||
continue
|
||||
}
|
||||
if msg, _ := record["msg"].(string); msg != "archie-ui running" {
|
||||
continue
|
||||
}
|
||||
if addr, ok := record["addr"].(string); ok && addr != "" {
|
||||
p.addr = addr
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func waitHealthy(t *testing.T, p *uiProcess) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(20 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://"+p.addr+"/healthz", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("build liveness request: %v", err)
|
||||
}
|
||||
if resp, err := http.DefaultClient.Do(req); err == nil {
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return
|
||||
}
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("/healthz never answered:\n%s", p.log.String())
|
||||
}
|
||||
|
||||
// stop ends the process the way a service manager does and reports a
|
||||
// non-graceful exit, which is how a shutdown-ordering regression surfaces.
|
||||
func (p *uiProcess) stop(t *testing.T) {
|
||||
t.Helper()
|
||||
if p.cmd.Process == nil || p.cmd.ProcessState != nil {
|
||||
return
|
||||
}
|
||||
if err := p.cmd.Process.Signal(syscall.SIGTERM); err != nil {
|
||||
t.Errorf("SIGTERM archie-ui: %v", err)
|
||||
return
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- p.cmd.Wait() }()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Errorf("archie-ui exited %v after SIGTERM, want a clean shutdown:\n%s", err, p.log.String())
|
||||
}
|
||||
case <-time.After(20 * time.Second):
|
||||
_ = p.cmd.Process.Kill()
|
||||
t.Errorf("archie-ui did not exit within 20s of SIGTERM:\n%s", p.log.String())
|
||||
}
|
||||
}
|
||||
|
||||
// reply is one dashboard response, already read and closed. The assertions
|
||||
// want the status and the bytes rather than a live body, and handing back a
|
||||
// closed value keeps the close where the read is.
|
||||
type reply struct {
|
||||
status int
|
||||
header http.Header
|
||||
body []byte
|
||||
}
|
||||
|
||||
func (r reply) cookies() []*http.Cookie {
|
||||
return (&http.Response{Header: r.header}).Cookies()
|
||||
}
|
||||
|
||||
// dashboard is a browser-shaped client for the running process.
|
||||
type dashboard struct {
|
||||
t *testing.T
|
||||
base string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// noRedirect returns a dashboard that reports a redirect rather than
|
||||
// following it, so an exchange can be inspected instead of chased. Following
|
||||
// it here would land on the clean URL without the cookie jar a browser has.
|
||||
func (d dashboard) noRedirect() dashboard {
|
||||
d.client = &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}}
|
||||
return d
|
||||
}
|
||||
|
||||
func (d dashboard) do(req *http.Request) reply {
|
||||
d.t.Helper()
|
||||
client := d.client
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
d.t.Fatalf("%s %s: %v", req.Method, req.URL.Path, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
d.t.Fatalf("read %s: %v", req.URL.Path, err)
|
||||
}
|
||||
return reply{status: resp.StatusCode, header: resp.Header, body: body}
|
||||
}
|
||||
|
||||
func (d dashboard) request(method, path, body string, authorized bool) *http.Request {
|
||||
d.t.Helper()
|
||||
var reader io.Reader = http.NoBody
|
||||
if body != "" {
|
||||
reader = strings.NewReader(body)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(d.t.Context(), method, d.base+path, reader)
|
||||
if err != nil {
|
||||
d.t.Fatalf("build %s %s: %v", method, path, err)
|
||||
}
|
||||
if authorized {
|
||||
req.Header.Set("Authorization", "Bearer "+dashboardToken)
|
||||
}
|
||||
if body != "" {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Archie-CSRF", "1")
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
func (d dashboard) get(path string) reply {
|
||||
d.t.Helper()
|
||||
return d.do(d.request(http.MethodGet, path, "", true))
|
||||
}
|
||||
|
||||
// chatFrame is one `data: {...}` frame of the chat stream, decoded into the
|
||||
// fields this suite reads. The UI process only ever receives these over HTTP,
|
||||
// so asserting on them is asserting on what the browser is handed.
|
||||
type chatFrame struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
Tool string `json:"tool"`
|
||||
}
|
||||
|
||||
// streamChat runs one chat turn over the real HTTP stream and returns every
|
||||
// frame up to and including the terminating done.
|
||||
func streamChat(t *testing.T, d dashboard, prompt string) []chatFrame {
|
||||
t.Helper()
|
||||
body, err := json.Marshal(map[string]string{"text": prompt, "channel_id": "dash"})
|
||||
if err != nil {
|
||||
t.Fatalf("encode the chat turn: %v", err)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(d.request(http.MethodPost, "/api/chat/stream", string(body), true))
|
||||
if err != nil {
|
||||
t.Fatalf("POST /api/chat/stream: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("POST /api/chat/stream = %d, want 200", resp.StatusCode)
|
||||
}
|
||||
var frames []chatFrame
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
for scanner.Scan() {
|
||||
payload, ok := strings.CutPrefix(scanner.Text(), "data: ")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
var frame chatFrame
|
||||
if err := json.Unmarshal([]byte(payload), &frame); err != nil {
|
||||
t.Fatalf("decode stream frame %q: %v", payload, err)
|
||||
}
|
||||
frames = append(frames, frame)
|
||||
if frame.Type == "done" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
t.Fatalf("read the chat stream: %v", err)
|
||||
}
|
||||
return frames
|
||||
}
|
||||
|
||||
// seedStore fills a real task database and publishes the configuration
|
||||
// projection, so everything the dashboard renders arrives over the wire from
|
||||
// a store this process never opens.
|
||||
//
|
||||
// The document is rendered by webui.BuildConfigView -- the same function the
|
||||
// daemon calls -- from a config.Config rather than hand-written as a
|
||||
// ConfigView. A suite that hand-writes the document cannot catch a producer
|
||||
// that forgets to publish a field, which is the failure mode this covers:
|
||||
// chat.show_tool_calls, the operator name and the channel flag all reach the
|
||||
// extracted process through this projection and nowhere else (GitHub #821).
|
||||
func seedStore(t *testing.T, dir string) *pgstore.TaskDB {
|
||||
t.Helper()
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
if _, err := st.EnqueueIssue(t.Context(), "acme", "widget", 7, "smoke task", "body", "", ""); err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
tasks, err := st.Tasks(t.Context(), 10)
|
||||
if err != nil || len(tasks) != 1 {
|
||||
t.Fatalf("seeded tasks = %d (%v), want 1", len(tasks), err)
|
||||
}
|
||||
seeded := tasks[0]
|
||||
seeded.PRNumber = 34
|
||||
if err := st.Update(t.Context(), &seeded); err != nil {
|
||||
t.Fatalf("record PR number: %v", err)
|
||||
}
|
||||
|
||||
publishConfig(t, st, daemonConfig(true))
|
||||
return st
|
||||
}
|
||||
|
||||
// daemonConfig is the configuration the daemon would hold for this smoke run.
|
||||
// showToolCalls is the setting the chat page's expansion is gated on.
|
||||
func daemonConfig(showToolCalls bool) config.Config {
|
||||
return config.Config{
|
||||
BotUser: "archie-bot",
|
||||
Forge: config.Forge{Type: "github", Host: "https://github.example.com"},
|
||||
Repos: []config.Repo{{Owner: "acme", Name: "widget", Base: "main"}},
|
||||
Chat: config.ChatConfig{
|
||||
ShowToolCalls: showToolCalls,
|
||||
Operator: "Sam",
|
||||
WebhookAddr: "127.0.0.1:9099",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// publishConfig renders cfg the way the daemon's configuration owner does and
|
||||
// writes the result to the store the UI process reads it back from. The UI
|
||||
// process holds no configuration, so this is the only path by which a setting
|
||||
// reaches its pages.
|
||||
func publishConfig(t *testing.T, st *pgstore.TaskDB, cfg config.Config) {
|
||||
t.Helper()
|
||||
document, err := json.Marshal(webui.BuildConfigView(webui.ConfigViewInput{Config: cfg}))
|
||||
if err != nil {
|
||||
t.Fatalf("render the configuration projection: %v", err)
|
||||
}
|
||||
if err := st.PutConfigSnapshot(t.Context(), storecontract.ConfigSnapshot{
|
||||
Schema: webui.ConfigViewSchema, Document: document,
|
||||
}); err != nil {
|
||||
t.Fatalf("publish config snapshot: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUIProcessLinksMultiIdentityTaskRowsToTheirOwningForge drives the real
|
||||
// binary against a State Store holding a multi-identity deployment's
|
||||
// projection. Task rows in such a deployment used to render no link at all:
|
||||
// the projection carried the default identity's forge alone, so the renderer
|
||||
// withheld rather than pointing every row at the wrong forge
|
||||
// (archie-core-pv6t). With per-identity forges published, each row is
|
||||
// attributed to the identity that owns it, by identity name first and repo
|
||||
// ownership second.
|
||||
//
|
||||
// Its own store and process: it publishes a different projection than the
|
||||
// single-identity smoke test, and republishing over a shared one would rewire
|
||||
// that test's page underneath it.
|
||||
func TestUIProcessLinksMultiIdentityTaskRowsToTheirOwningForge(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
// One task carrying its identity (name resolves it), one without an
|
||||
// identity but on a repo only the Gitea identity owns (ownership
|
||||
// resolves it), and one on a repo both identities claim (ambiguous, so
|
||||
// it falls back to the default forge).
|
||||
for _, seed := range []struct {
|
||||
owner, repo, identity string
|
||||
number int
|
||||
}{
|
||||
{owner: "acme", repo: "widget", identity: "gitea-bot", number: 7},
|
||||
{owner: "beta", repo: "svc", number: 8},
|
||||
{owner: "acme", repo: "shared", number: 9},
|
||||
} {
|
||||
if _, err := st.EnqueueIssue(t.Context(), seed.owner, seed.repo, seed.number, "seeded", "body", "", seed.identity); err != nil {
|
||||
t.Fatalf("seed %s/%s: %v", seed.owner, seed.repo, err)
|
||||
}
|
||||
}
|
||||
|
||||
published, err := json.Marshal(webui.ConfigView{
|
||||
Identity: webui.IdentityView{BotUser: "archie-bot", ForgeType: "github", ForgeHost: "https://github.example.com"},
|
||||
MultiIdentity: true,
|
||||
Identities: []webui.ForgeIdentityView{
|
||||
{
|
||||
Name: "gitea-bot", ForgeType: "gitea", ForgeHost: "https://gitea.example.com",
|
||||
Repos: []webui.ForgeRepoView{{Owner: "acme", Name: "widget"}, {Owner: "acme", Name: "shared"}},
|
||||
},
|
||||
{
|
||||
Name: "github-bot", ForgeType: "github", ForgeHost: "https://github.example.com",
|
||||
Repos: []webui.ForgeRepoView{{Owner: "beta", Name: "svc"}, {Owner: "acme", Name: "shared"}},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.PutConfigSnapshot(t.Context(), storecontract.ConfigSnapshot{
|
||||
Schema: webui.ConfigViewSchema, Document: published,
|
||||
}); err != nil {
|
||||
t.Fatalf("publish config snapshot: %v", err)
|
||||
}
|
||||
|
||||
stateTarget, stopState := serveGRPC(t, func(r grpc.ServiceRegistrar) {
|
||||
eda := pgstore.EDA(t, nil)
|
||||
staterpc.RegisterServer(r, staterpc.Deps{
|
||||
Tasks: st, Captures: eda, BindingDispatcher: eda, ConfigSnapshots: st,
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
})
|
||||
})
|
||||
defer stopState()
|
||||
gatewayTarget, stopGateway := serveGRPC(t, func(r grpc.ServiceRegistrar) {
|
||||
gatewayrpc.RegisterServer(r, &fakeGateway{})
|
||||
})
|
||||
defer stopGateway()
|
||||
|
||||
ui := startUIProcess(t, buildUIBinary(t, dir), writeUIConfig(t, dir, stateTarget, gatewayTarget))
|
||||
d := dashboard{t: t, base: "http://" + ui.addr}
|
||||
|
||||
got := d.get("/api/tasks")
|
||||
if got.status != http.StatusOK {
|
||||
t.Fatalf("GET /api/tasks = %d (%s), want 200", got.status, got.body)
|
||||
}
|
||||
var rows []struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
IssueNumber int `json:"issue_number"`
|
||||
RepoURL string `json:"repo_url"`
|
||||
IssueURL string `json:"issue_url"`
|
||||
}
|
||||
if err := json.Unmarshal(got.body, &rows); err != nil {
|
||||
t.Fatalf("decode task rows: %v (%s)", err, got.body)
|
||||
}
|
||||
if len(rows) != 3 {
|
||||
t.Fatalf("task rows = %d, want the 3 seeded behind the State Store (%s)", len(rows), got.body)
|
||||
}
|
||||
|
||||
want := map[string]string{
|
||||
"acme/widget#7": "https://gitea.example.com/acme/widget/issues/7",
|
||||
"beta/svc#8": "https://github.example.com/beta/svc/issues/8",
|
||||
"acme/shared#9": "https://github.example.com/acme/shared/issues/9",
|
||||
}
|
||||
for _, row := range rows {
|
||||
key := fmt.Sprintf("%s/%s#%d", row.Owner, row.Repo, row.IssueNumber)
|
||||
expected, ok := want[key]
|
||||
if !ok {
|
||||
t.Errorf("unexpected row %q (%s)", key, got.body)
|
||||
continue
|
||||
}
|
||||
if row.RepoURL == "" || row.IssueURL != expected {
|
||||
t.Errorf("row %s links = %q, %q; want %q", key, row.RepoURL, row.IssueURL, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUIProcessServesTheDashboardAgainstLiveDependencies(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
st := seedStore(t, dir)
|
||||
gateway := &fakeGateway{}
|
||||
|
||||
stateTarget, stopState := serveGRPC(t, func(r grpc.ServiceRegistrar) {
|
||||
eda := pgstore.EDA(t, nil)
|
||||
staterpc.RegisterServer(r, staterpc.Deps{
|
||||
Tasks: st, Captures: eda, BindingDispatcher: eda, ConfigSnapshots: st,
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
})
|
||||
})
|
||||
gatewayTarget, _ := serveGRPC(t, func(r grpc.ServiceRegistrar) {
|
||||
gatewayrpc.RegisterServer(r, gateway)
|
||||
})
|
||||
|
||||
ui := startUIProcess(t, buildUIBinary(t, dir), writeUIConfig(t, dir, stateTarget, gatewayTarget))
|
||||
d := dashboard{t: t, base: "http://" + ui.addr}
|
||||
|
||||
t.Run("liveness stays unauthenticated", func(t *testing.T) {
|
||||
for _, path := range []string{"/healthz", "/health"} {
|
||||
if got := d.do(d.request(http.MethodGet, path, "", false)); got.status != http.StatusOK {
|
||||
t.Errorf("GET %s = %d without a token, want 200: probes must not need the dashboard token", path, got.status)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("an unauthenticated API request is refused", func(t *testing.T) {
|
||||
if got := d.do(d.request(http.MethodGet, "/api/tasks", "", false)); got.status != http.StatusUnauthorized {
|
||||
t.Errorf("GET /api/tasks = %d without a token, want 401", got.status)
|
||||
}
|
||||
wrong := d.request(http.MethodGet, "/api/tasks", "", false)
|
||||
wrong.Header.Set("Authorization", "Bearer not-the-token")
|
||||
if got := d.do(wrong); got.status != http.StatusUnauthorized {
|
||||
t.Errorf("GET /api/tasks = %d with a wrong token, want 401", got.status)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("the token in the URL is exchanged for a cookie", func(t *testing.T) {
|
||||
got := d.noRedirect().do(d.request(http.MethodGet, "/?t="+dashboardToken, "", false))
|
||||
if got.status != http.StatusSeeOther {
|
||||
t.Fatalf("GET /?t=<token> = %d, want 303 onto the clean URL", got.status)
|
||||
}
|
||||
var carried bool
|
||||
for _, cookie := range got.cookies() {
|
||||
if cookie.Value == dashboardToken && cookie.HttpOnly {
|
||||
carried = true
|
||||
}
|
||||
}
|
||||
if !carried {
|
||||
t.Errorf("no HttpOnly cookie carried the token; it would stay in browser history (%v)", got.cookies())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("task rows carry forge links from the published projection", func(t *testing.T) {
|
||||
got := d.get("/api/tasks")
|
||||
if got.status != http.StatusOK {
|
||||
t.Fatalf("GET /api/tasks = %d (%s), want 200", got.status, got.body)
|
||||
}
|
||||
var rows []struct {
|
||||
ID int64 `json:"id"`
|
||||
RepoURL string `json:"repo_url"`
|
||||
IssueURL string `json:"issue_url"`
|
||||
PRURL string `json:"pr_url"`
|
||||
}
|
||||
if err := json.Unmarshal(got.body, &rows); err != nil {
|
||||
t.Fatalf("decode task rows: %v (%s)", err, got.body)
|
||||
}
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("task rows = %d, want the 1 seeded behind the State Store (%s)", len(rows), got.body)
|
||||
}
|
||||
const repoURL = "https://github.example.com/acme/widget"
|
||||
if rows[0].RepoURL != repoURL || rows[0].IssueURL != repoURL+"/issues/7" || rows[0].PRURL != repoURL+"/pull/34" {
|
||||
t.Errorf("forge links = %q, %q, %q; want them built from the daemon's published projection, since this process holds no configuration of its own",
|
||||
rows[0].RepoURL, rows[0].IssueURL, rows[0].PRURL)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("the SPA and its assets are served", func(t *testing.T) {
|
||||
index := d.get("/")
|
||||
if index.status != http.StatusOK || !bytes.Contains(bytes.ToLower(index.body), []byte("<!doctype html")) {
|
||||
t.Errorf("GET / = %d serving %d bytes; want the embedded index.html", index.status, len(index.body))
|
||||
}
|
||||
for path, wantType := range map[string]string{
|
||||
"/assets/index.js": "javascript",
|
||||
"/assets/index.css": "css",
|
||||
} {
|
||||
asset := d.get(path)
|
||||
if asset.status != http.StatusOK {
|
||||
t.Errorf("GET %s = %d, want the embedded asset", path, asset.status)
|
||||
continue
|
||||
}
|
||||
if contentType := asset.header.Get("Content-Type"); !strings.Contains(contentType, wantType) {
|
||||
t.Errorf("GET %s Content-Type = %q, want %s", path, contentType, wantType)
|
||||
}
|
||||
if len(asset.body) == 0 {
|
||||
t.Errorf("GET %s served an empty body", path)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("chat streams over the Gateway contract", func(t *testing.T) {
|
||||
req := d.request(http.MethodPost, "/api/chat/stream", `{"text":"there","channel_id":"dash"}`, true)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("POST /api/chat/stream: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("POST /api/chat/stream = %d, want 200", resp.StatusCode)
|
||||
}
|
||||
if contentType := resp.Header.Get("Content-Type"); !strings.Contains(contentType, "text/event-stream") {
|
||||
t.Fatalf("stream Content-Type = %q, want text/event-stream", contentType)
|
||||
}
|
||||
var kinds []string
|
||||
var text string
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
for scanner.Scan() {
|
||||
payload, ok := strings.CutPrefix(scanner.Text(), "data: ")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
var frame struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(payload), &frame); err != nil {
|
||||
t.Fatalf("decode stream frame %q: %v", payload, err)
|
||||
}
|
||||
kinds = append(kinds, frame.Type)
|
||||
if frame.Type == "delta" {
|
||||
text += frame.Text
|
||||
}
|
||||
if frame.Type == "done" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if strings.Join(kinds, ",") != "started,delta,done" {
|
||||
t.Errorf("stream frames = %v, want started,delta,done in order", kinds)
|
||||
}
|
||||
if text != "hello there" {
|
||||
t.Errorf("streamed text = %q, want the Gateway's reply relayed verbatim", text)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("the daemon's show_tool_calls setting decides whether tool calls reach the chat page", func(t *testing.T) {
|
||||
// The chat page expands a completed tool call only when the frame
|
||||
// arrives, and this process has no [chat] section of its own: the
|
||||
// setting reaches it through the published projection alone. A turn
|
||||
// streamed with the setting off must carry no tool frame, and one
|
||||
// streamed with it on must carry the tool the Gateway reported
|
||||
// (GitHub #821).
|
||||
//
|
||||
// Each case republishes before streaming, the way a config reload
|
||||
// does; the default is restored afterwards so a later subtest does
|
||||
// not inherit whichever case ran last.
|
||||
tests := []struct {
|
||||
name string
|
||||
showTools bool
|
||||
wantFrame bool
|
||||
}{
|
||||
{name: "off", showTools: false, wantFrame: false},
|
||||
{name: "on", showTools: true, wantFrame: true},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
publishConfig(t, st, daemonConfig(tc.showTools))
|
||||
|
||||
frames := streamChat(t, d, toolCallPrompt)
|
||||
var sawTool bool
|
||||
for _, frame := range frames {
|
||||
if frame.Tool == "read_file" {
|
||||
sawTool = true
|
||||
}
|
||||
if frame.Type == "done" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if sawTool != tc.wantFrame {
|
||||
t.Errorf("show_tool_calls=%v produced tool frame = %v, want %v; frames = %+v",
|
||||
tc.showTools, sawTool, tc.wantFrame, frames)
|
||||
}
|
||||
})
|
||||
}
|
||||
publishConfig(t, st, daemonConfig(true))
|
||||
})
|
||||
|
||||
t.Run("an operator task action crosses to the Gateway", func(t *testing.T) {
|
||||
listed := d.get("/api/tasks")
|
||||
var rows []struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
if err := json.Unmarshal(listed.body, &rows); err != nil || len(rows) != 1 {
|
||||
t.Fatalf("decode task rows: %v (%s)", err, listed.body)
|
||||
}
|
||||
path := fmt.Sprintf("/api/tasks/%d/action", rows[0].ID)
|
||||
action := string(taskstate.ActionCancel)
|
||||
if got := d.do(d.request(http.MethodPost, path, `{"action":"`+action+`"}`, true)); got.status != http.StatusOK {
|
||||
t.Fatalf("POST %s = %d (%s), want 200", path, got.status, got.body)
|
||||
}
|
||||
want := fmt.Sprintf("%d:%s", rows[0].ID, action)
|
||||
if applied := gateway.applied(); len(applied) != 1 || applied[0] != want {
|
||||
t.Errorf("Gateway saw %v, want exactly [%s]: the UI must not mutate task state itself", applied, want)
|
||||
}
|
||||
|
||||
// The same action without the CSRF header is refused, so a
|
||||
// cross-site form post cannot drive the daemon.
|
||||
noCSRF := d.request(http.MethodPost, path, "", true)
|
||||
noCSRF.Body = io.NopCloser(strings.NewReader(`{"action":"` + action + `"}`))
|
||||
noCSRF.Header.Set("Content-Type", "application/json")
|
||||
if got := d.do(noCSRF); got.status != http.StatusForbidden {
|
||||
t.Errorf("POST %s without the CSRF header = %d, want 403", path, got.status)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("capture intake is served without the dashboard token", func(t *testing.T) {
|
||||
accepted := d.do(d.request(http.MethodPost, "/webhooks/capture/demo", "", false))
|
||||
if accepted.status != http.StatusAccepted {
|
||||
t.Fatalf("POST /webhooks/capture/demo = %d (%s), want 202: this process is the only listener serving intake after the cutover", accepted.status, accepted.body)
|
||||
}
|
||||
listed := d.get("/api/captures")
|
||||
if listed.status != http.StatusOK || !strings.Contains(string(listed.body), `"enabled":true`) {
|
||||
t.Errorf("GET /api/captures = %d (%s), want the capture list over the State Store", listed.status, listed.body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("routes with no owner degrade explicitly", func(t *testing.T) {
|
||||
// The daemon's log feed is host-local and has no contract, so the
|
||||
// page reports itself off rather than erroring
|
||||
// (docs/prds/ui-service-boundary.md, route family table).
|
||||
logs := d.get("/api/logs")
|
||||
if logs.status != http.StatusOK || !strings.Contains(string(logs.body), `"disabled":true`) {
|
||||
t.Errorf("GET /api/logs = %d (%s), want an explicit disabled report", logs.status, logs.body)
|
||||
}
|
||||
if version := d.get("/api/version"); version.status != http.StatusNotImplemented {
|
||||
t.Errorf("GET /api/version = %d, want 501: host-local update reporting stays with the daemon", version.status)
|
||||
}
|
||||
|
||||
// Configuration renders from the published snapshot, and this
|
||||
// process is not a second writer.
|
||||
rendered := d.get("/api/config")
|
||||
if rendered.status != http.StatusOK {
|
||||
t.Fatalf("GET /api/config = %d (%s), want 200", rendered.status, rendered.body)
|
||||
}
|
||||
var view webui.ConfigView
|
||||
if err := json.Unmarshal(rendered.body, &view); err != nil {
|
||||
t.Fatalf("decode config view: %v (%s)", err, rendered.body)
|
||||
}
|
||||
if view.Identity.BotUser != "archie-bot" {
|
||||
t.Errorf("config view = %s, want the published projection rendered read-only", rendered.body)
|
||||
}
|
||||
if got := d.do(d.request(http.MethodPatch, "/api/config", `{}`, true)); got.status != http.StatusMethodNotAllowed {
|
||||
t.Errorf("PATCH /api/config = %d, want 405: configuration is read here and written through the control plane", got.status)
|
||||
}
|
||||
|
||||
// The setup checklist reads the same projection. It reported
|
||||
// nothing here while it needed a live config.Holder this process
|
||||
// never receives (archie-core-ml30).
|
||||
setup := d.get("/api/setup")
|
||||
if setup.status != http.StatusOK {
|
||||
t.Fatalf("GET /api/setup = %d (%s), want 200", setup.status, setup.body)
|
||||
}
|
||||
var checklist struct {
|
||||
Steps []struct {
|
||||
Title string `json:"title"`
|
||||
Done bool `json:"done"`
|
||||
} `json:"steps"`
|
||||
Operator string `json:"operator"`
|
||||
}
|
||||
if err := json.Unmarshal(setup.body, &checklist); err != nil {
|
||||
t.Fatalf("decode setup checklist: %v (%s)", err, setup.body)
|
||||
}
|
||||
if len(checklist.Steps) == 0 {
|
||||
t.Errorf("setup checklist is empty; the published projection carries what it reads (%s)", setup.body)
|
||||
}
|
||||
if checklist.Operator != "Sam" {
|
||||
t.Errorf("setup operator = %q, want the published name", checklist.Operator)
|
||||
}
|
||||
for _, step := range checklist.Steps {
|
||||
if step.Title == "Connect a repository" && !step.Done {
|
||||
t.Errorf("checklist says no repository is connected; the projection publishes one (%s)", setup.body)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Last: taking the State Store away is not reversible for this process.
|
||||
t.Run("losing a dependency degrades readiness without failing liveness", func(t *testing.T) {
|
||||
healthy := d.get("/health/detailed")
|
||||
if healthy.status != http.StatusOK {
|
||||
t.Fatalf("GET /health/detailed = %d (%s), want 200", healthy.status, healthy.body)
|
||||
}
|
||||
var report health.Report
|
||||
if err := json.Unmarshal(healthy.body, &report); err != nil {
|
||||
t.Fatalf("decode readiness: %v (%s)", err, healthy.body)
|
||||
}
|
||||
if report.Status != health.StatusOK {
|
||||
t.Fatalf("readiness = %s with both dependencies up, want ok (%s)", report.Status, healthy.body)
|
||||
}
|
||||
|
||||
stopState()
|
||||
degraded := d.get("/health/detailed")
|
||||
if err := json.Unmarshal(degraded.body, &report); err != nil {
|
||||
t.Fatalf("decode readiness after outage: %v (%s)", err, degraded.body)
|
||||
}
|
||||
if report.Status == health.StatusOK {
|
||||
t.Errorf("readiness stayed ok with the State Store stopped (%s)", degraded.body)
|
||||
}
|
||||
if live := d.do(d.request(http.MethodGet, "/healthz", "", false)); live.status != http.StatusOK {
|
||||
t.Errorf("GET /healthz = %d during a dependency outage, want 200: liveness must not imply dependency readiness", live.status)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) }
|
||||
@@ -1,367 +0,0 @@
|
||||
package configtemplate
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeployWorkflowOnlyPublishesRuntimeImageForTaggedRelease(t *testing.T) {
|
||||
source := readDeploymentFile(t, ".github/workflows/deploy.yml")
|
||||
|
||||
// The fallback remains necessary because Dockerfile.archied accepts a
|
||||
// runtime version, but it must not be used as the image-publish selector.
|
||||
for _, required := range []string{
|
||||
`RUNTIME_TAG="$(git tag --points-at HEAD --list 'archie/v*' | sort -V | tail -1)"`,
|
||||
`RELEASE_TAG="$(git tag --points-at HEAD --list 'v*' | sort -V | tail -1)"`,
|
||||
`echo "runtime_tag=$RUNTIME_TAG" >> "$GITHUB_OUTPUT"`,
|
||||
`echo "release_tag=$RELEASE_TAG" >> "$GITHUB_OUTPUT"`,
|
||||
} {
|
||||
if !strings.Contains(source, required) {
|
||||
t.Errorf("deploy workflow is missing runtime-image guard %q", required)
|
||||
}
|
||||
}
|
||||
|
||||
// The gateway build remains unconditional, while the runtime build is
|
||||
// guarded. Both selectors are required. A one-stream release creates only a
|
||||
// `v*` tag -- release.sh has no archie/v* arm any more -- so a
|
||||
// runtime_tag-only condition would mean the agent image is never rebuilt
|
||||
// again, and `docker compose pull agent` would fetch an ancient image
|
||||
// forever. The legacy archie/v* arm stays because history is not re-tagged.
|
||||
agentMarker := " - name: Build and push archie-agent\n"
|
||||
agentStart := strings.Index(source, agentMarker)
|
||||
if agentStart < 0 {
|
||||
t.Fatal("deploy workflow has no archie-agent build step")
|
||||
}
|
||||
agentIf := " if: steps.versions.outputs.runtime_tag != '' || steps.versions.outputs.release_tag != ''\n"
|
||||
if !strings.Contains(source[agentStart:], agentMarker+agentIf) {
|
||||
t.Fatal("archie-agent build step is not conditionally enabled")
|
||||
}
|
||||
|
||||
gatewayMarker := " - name: Build and push archied\n"
|
||||
gatewayStart := strings.Index(source, gatewayMarker)
|
||||
if gatewayStart < 0 {
|
||||
t.Fatal("deploy workflow has no archied build step")
|
||||
}
|
||||
if strings.Contains(source[gatewayStart:agentStart], "\n if:") {
|
||||
t.Fatal("archied build step is unexpectedly conditional")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeployWorkflowPublishesLatestOnlyFromARelease pins the rule that keeps a
|
||||
// release's :latest stamping from being overwritten by the next ordinary commit.
|
||||
//
|
||||
// build-and-push runs on any push to main and used to always push :latest, so a
|
||||
// release's own stamping survived only until the next commit — minutes, in an
|
||||
// active repo — and a host deploying :latest reported dev, leaving the update
|
||||
// path unable to tell current from stale (archie-core-i5nb). :latest now moves
|
||||
// only from a commit that IS a release; everything else publishes :edge and an
|
||||
// immutable sha-<commit>.
|
||||
func TestDeployWorkflowPublishesLatestOnlyFromARelease(t *testing.T) {
|
||||
source := readDeploymentFile(t, ".github/workflows/deploy.yml")
|
||||
|
||||
for _, required := range []string{
|
||||
`if [ -n "$RELEASE_TAG" ]; then`,
|
||||
`echo "ghcr.io/samcharles93/archied:latest"`,
|
||||
`echo "ghcr.io/samcharles93/archied:edge"`,
|
||||
`archied_tags<<EOF`,
|
||||
`tags: ${{ steps.versions.outputs.archied_tags }}`,
|
||||
} {
|
||||
if !strings.Contains(source, required) {
|
||||
t.Errorf("deploy workflow is missing the release-only :latest guard %q", required)
|
||||
}
|
||||
}
|
||||
|
||||
// One occurrence: the guard above, not a second unconditional copy that would
|
||||
// undo it.
|
||||
if got := strings.Count(source, "ghcr.io/samcharles93/archied:latest"); got != 1 {
|
||||
t.Errorf("archied :latest appears %d times, want exactly 1 inside the release-tag guard", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployWorkflowUsesConfiguredFormatters(t *testing.T) {
|
||||
source := readDeploymentFile(t, ".github/workflows/deploy.yml")
|
||||
for _, required := range []string{
|
||||
"github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.13.2",
|
||||
"task fmt",
|
||||
"git diff --exit-code",
|
||||
"golangci-lint run ./...",
|
||||
} {
|
||||
if !strings.Contains(source, required) {
|
||||
t.Errorf("deploy workflow is missing formatter gate %q", required)
|
||||
}
|
||||
}
|
||||
// task fmt is the one owner of the formatter sequence, and it drives go fix to
|
||||
// a fixpoint. An inline single pass here leaves the redundant loop-variable
|
||||
// copy that copyloopvar rejects -- the hazard of archie-core-wwho, which
|
||||
// surfaces in this workflow as a spurious git-diff failure rather than a lint
|
||||
// error (archie-core-6kda). A comment may name the command while explaining
|
||||
// why it is absent; only a line that is itself a command inlines it.
|
||||
for line := range strings.SplitSeq(source, "\n") {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if strings.HasPrefix(trimmed, "#") {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(trimmed, "go fix") || trimmed == "golangci-lint fmt" {
|
||||
t.Errorf("deploy workflow runs %q inline; task fmt owns the formatter sequence", trimmed)
|
||||
}
|
||||
}
|
||||
if strings.Contains(source, "gofumpt -w .") {
|
||||
t.Error("deploy workflow bypasses the configured formatter set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPullRequestsRunTheDefinitiveGateWithoutPublishAccess(t *testing.T) {
|
||||
source := readDeploymentFile(t, ".github/workflows/quality.yml")
|
||||
for _, required := range []string{
|
||||
"pull_request:",
|
||||
"contents: read",
|
||||
"task check",
|
||||
"git diff --exit-code",
|
||||
} {
|
||||
if !strings.Contains(source, required) {
|
||||
t.Errorf("quality workflow is missing %q", required)
|
||||
}
|
||||
}
|
||||
for _, forbidden := range []string{
|
||||
"packages: write",
|
||||
"docker/login-action",
|
||||
"docker/build-push-action",
|
||||
} {
|
||||
if strings.Contains(source, forbidden) {
|
||||
t.Errorf("quality workflow has publishing capability %q", forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// unpackagedCommands are commands that deliberately never ship as a host binary.
|
||||
// Naming each one with its reason keeps an omission a decision rather than an
|
||||
// oversight -- which is exactly how archied's channels were lost.
|
||||
var unpackagedCommands = map[string]string{
|
||||
"archie-agent": "runs only inside the Docker image build-and-push publishes, never as a host binary",
|
||||
"archie-prbench": "a developer benchmark run from the repository with task bench:prreview",
|
||||
}
|
||||
|
||||
// cliOnlyCommands ship in the zip but must never be restarted by the updater:
|
||||
// they are not services.
|
||||
var cliOnlyCommands = map[string]bool{"archie-playbooks": true}
|
||||
|
||||
// TestDistZipShipsEveryHostCommand is why archie-core-1c01 went unnoticed for
|
||||
// two days. The v1.30.0 extraction moved Telegram, email and webhook out of
|
||||
// archied into a new cmd/archie-messaging that nothing packaged: the release zip
|
||||
// omitted it, scripts/archie-update-install omitted it from both of its lists,
|
||||
// and install.sh's build loop omitted it too, so a host install ran on with a
|
||||
// dead Telegram bot and no error logged anywhere. The daemon looked healthy
|
||||
// throughout.
|
||||
//
|
||||
// The host component set was enumerated independently in four places (the zip's
|
||||
// build loop, the zip's copy list, install.sh's build loop, and the updater's
|
||||
// two lists), so two coordinated edits would have drifted again. Every list is
|
||||
// pinned to cmd/ here instead: adding a command fails this test until it is
|
||||
// packaged, and extracting one fails it until the previous command is removed.
|
||||
// The updater's service list is separately required to have a documented unit,
|
||||
// and its runtime guard refuses a host unit the list omits.
|
||||
func TestDistZipShipsEveryHostCommand(t *testing.T) {
|
||||
workflow := readDeploymentFile(t, ".github/workflows/deploy.yml")
|
||||
installer := readDeploymentFile(t, filepath.Join("scripts", "archie-update-install"))
|
||||
installSh := readDeploymentFile(t, "install.sh")
|
||||
|
||||
want := hostRunCommands(t)
|
||||
wantBinaries := want
|
||||
wantServices := withoutCLIOnly(want)
|
||||
|
||||
built := wordsAfter(t, workflow, "for cmd in ", "; do", nil)
|
||||
// The marker consumes the prefix of the first word only, so restore it for
|
||||
// whichever word the marker ended inside.
|
||||
var shipped []string
|
||||
for _, word := range wordsAfter(t, workflow, "cp dist/", ` "$name/"`, nil) {
|
||||
if !strings.HasPrefix(word, "dist/") {
|
||||
word = "dist/" + word
|
||||
}
|
||||
shipped = append(shipped, strings.TrimPrefix(word, "dist/"))
|
||||
}
|
||||
services := wordsAfter(t, installer, `GATEWAY_SERVICES="`, `"`, nil)
|
||||
binaries := wordsAfter(t, installer, `GATEWAY_BINARIES="`, `"`, map[string][]string{"$GATEWAY_SERVICES": services})
|
||||
installBuilt := installBuildCommands(t, installSh)
|
||||
|
||||
for _, list := range []struct {
|
||||
name string
|
||||
got []string
|
||||
want []string
|
||||
}{
|
||||
{"deploy.yml dist-zip build loop", built, wantBinaries},
|
||||
{"deploy.yml dist-zip copy list", shipped, wantBinaries},
|
||||
{"install.sh build loop", installBuilt, wantBinaries},
|
||||
{"archie-update-install GATEWAY_BINARIES", binaries, wantBinaries},
|
||||
{"archie-update-install GATEWAY_SERVICES", services, wantServices},
|
||||
} {
|
||||
if diff := setDifference(list.want, list.got); diff != "" {
|
||||
t.Errorf("%s does not match the host-run commands in cmd/: %s", list.name, diff)
|
||||
}
|
||||
}
|
||||
|
||||
// The installer now REFUSES an update when a service unit is missing and
|
||||
// tells the operator to create it from this runbook, so a service the
|
||||
// runbook never shows is a dead end rather than a documentation gap.
|
||||
// Stamping is its own list, and a third build site made it three: the zip,
|
||||
// install.sh and the updater each build the host binaries. A binary that
|
||||
// cannot say what it is cannot be compared (archie-core-k94o), so a seventh
|
||||
// command added unstamped fails here rather than waiting to be noticed.
|
||||
for name, source := range map[string]string{
|
||||
"deploy.yml": workflow,
|
||||
"install.sh": installSh,
|
||||
"archie-update-install": installer,
|
||||
} {
|
||||
if !strings.Contains(source, "internal/buildinfo.Version=") {
|
||||
t.Errorf("%s does not stamp internal/buildinfo.Version", name)
|
||||
}
|
||||
}
|
||||
for _, command := range wantBinaries {
|
||||
// archied's flag lives in the app package it delegates to, not its thin
|
||||
// cmd main; the rest report through buildinfo in their own main.
|
||||
source := filepath.Join("cmd", command, "main.go")
|
||||
if command == "archied" {
|
||||
source = filepath.Join("internal", "app", "archied", "main.go")
|
||||
}
|
||||
main := readDeploymentFile(t, source)
|
||||
if !strings.Contains(main, "buildinfo") && !strings.Contains(main, "-version") && !strings.Contains(main, "\"version\"") {
|
||||
t.Errorf("%s has no way to report its version", source)
|
||||
}
|
||||
}
|
||||
|
||||
// The installer writes the units, so the runbook no longer carries their
|
||||
// contents; it must still name each unit, or an operator cannot find one to
|
||||
// inspect or override.
|
||||
runbook := readDeploymentFile(t, filepath.Join("deployments", "systemd-user-service.md"))
|
||||
var undocumented []string
|
||||
for _, service := range wantServices {
|
||||
if !strings.Contains(runbook, service+".service") {
|
||||
undocumented = append(undocumented, service)
|
||||
}
|
||||
}
|
||||
if len(undocumented) > 0 {
|
||||
t.Errorf("deployments/systemd-user-service.md names no unit for: %s", strings.Join(undocumented, " "))
|
||||
}
|
||||
}
|
||||
|
||||
// installBuildCommands reads install.sh's build loop. The file has a second
|
||||
// `for cmd in` (the git/go preflight), so the loop is located by the go build it
|
||||
// drives rather than by the first marker -- and read from the real list, so a
|
||||
// missing command is named instead of the marker merely going absent.
|
||||
func installBuildCommands(t *testing.T, source string) []string {
|
||||
t.Helper()
|
||||
before, _, ok := strings.Cut(source, `go build -ldflags "${LDFLAGS}"`)
|
||||
if !ok {
|
||||
t.Fatal("install.sh has no go build loop to read")
|
||||
}
|
||||
prefix := before
|
||||
start := strings.LastIndex(prefix, "for cmd in ")
|
||||
if start < 0 {
|
||||
t.Fatal("install.sh build loop has no for cmd list")
|
||||
}
|
||||
rest := prefix[start+len("for cmd in "):]
|
||||
before0, _, ok0 := strings.Cut(rest, "; do")
|
||||
if !ok0 {
|
||||
t.Fatal("install.sh build loop is unterminated")
|
||||
}
|
||||
return strings.Fields(before0)
|
||||
}
|
||||
|
||||
// hostRunCommands is every command under cmd/, minus the deliberately
|
||||
// unpackaged ones. Deriving it is the point: a list written out here would drift
|
||||
// from cmd/ exactly as the packaged lists did.
|
||||
func hostRunCommands(t *testing.T) []string {
|
||||
t.Helper()
|
||||
entries, err := os.ReadDir("cmd")
|
||||
if err != nil {
|
||||
t.Fatalf("read cmd/: %v", err)
|
||||
}
|
||||
var commands []string
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
if _, skip := unpackagedCommands[entry.Name()]; skip {
|
||||
continue
|
||||
}
|
||||
commands = append(commands, entry.Name())
|
||||
}
|
||||
if len(commands) == 0 {
|
||||
t.Fatal("cmd/ holds no commands: this guard would assert nothing")
|
||||
}
|
||||
sort.Strings(commands)
|
||||
return commands
|
||||
}
|
||||
|
||||
func withoutCLIOnly(commands []string) []string {
|
||||
out := make([]string, 0, len(commands))
|
||||
for _, command := range commands {
|
||||
if cliOnlyCommands[command] {
|
||||
continue
|
||||
}
|
||||
out = append(out, command)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// wordsAfter returns the whitespace-separated words between two markers on the
|
||||
// same line. expand maps a word to the words it stands for, which is how the
|
||||
// installer's "$GATEWAY_SERVICES" reference is followed to what it names.
|
||||
func wordsAfter(t *testing.T, source, marker, terminator string, expand map[string][]string) []string {
|
||||
t.Helper()
|
||||
start := strings.Index(source, marker)
|
||||
if start < 0 {
|
||||
t.Fatalf("no %q in the file under test", marker)
|
||||
}
|
||||
rest := source[start+len(marker):]
|
||||
end := strings.Index(rest, terminator)
|
||||
if end < 0 {
|
||||
t.Fatalf("no %q after %q", terminator, marker)
|
||||
}
|
||||
var words []string
|
||||
for word := range strings.FieldsSeq(rest[:end]) {
|
||||
if replacement, ok := expand[word]; ok {
|
||||
words = append(words, replacement...)
|
||||
continue
|
||||
}
|
||||
words = append(words, word)
|
||||
}
|
||||
return words
|
||||
}
|
||||
|
||||
// setDifference names what is missing from and extra in got, so a failure says
|
||||
// which side moved rather than that two lists differ.
|
||||
func setDifference(want, got []string) string {
|
||||
present := make(map[string]bool, len(got))
|
||||
for _, item := range got {
|
||||
present[item] = true
|
||||
}
|
||||
var missing []string
|
||||
for _, item := range want {
|
||||
if !present[item] {
|
||||
missing = append(missing, item)
|
||||
}
|
||||
}
|
||||
expected := make(map[string]bool, len(want))
|
||||
for _, item := range want {
|
||||
expected[item] = true
|
||||
}
|
||||
var extra []string
|
||||
for _, item := range got {
|
||||
if !expected[item] {
|
||||
extra = append(extra, item)
|
||||
}
|
||||
}
|
||||
sort.Strings(missing)
|
||||
sort.Strings(extra)
|
||||
var problems []string
|
||||
if len(missing) > 0 {
|
||||
problems = append(problems, "missing "+strings.Join(missing, " "))
|
||||
}
|
||||
if len(extra) > 0 {
|
||||
problems = append(problems, "unexpected "+strings.Join(extra, " "))
|
||||
}
|
||||
return strings.Join(problems, "; ")
|
||||
}
|
||||
@@ -1,270 +0,0 @@
|
||||
package configtemplate
|
||||
|
||||
import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
func readDeploymentFile(t *testing.T, path string) string {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func TestExternalNATSProfileLoadsWithManagedWorkers(t *testing.T) {
|
||||
doc, err := configuration.New(nil).File("deployments/docker-nats-stack.toml")
|
||||
if err != nil {
|
||||
t.Fatalf("load external NATS deployment: %v", err)
|
||||
}
|
||||
if doc.Config.NATS.Mode != config.NATSModeExternal {
|
||||
t.Errorf("NATS mode = %q, want %q", doc.Config.NATS.Mode, config.NATSModeExternal)
|
||||
}
|
||||
if doc.Config.Containers.Image == "" {
|
||||
t.Error("managed worker image is empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallerUsesNativeDaemonAndManagedWorkerImage(t *testing.T) {
|
||||
source := readDeploymentFile(t, "install.sh")
|
||||
for _, forbidden := range []string{
|
||||
"[agent]",
|
||||
`go build -o "${ARCHIE_BIN_DIR}/archie-agent"`,
|
||||
`- archie-agent`,
|
||||
"required only for NATS container mode",
|
||||
} {
|
||||
if strings.Contains(source, forbidden) {
|
||||
t.Errorf("install.sh contains removed deployment behavior %q", forbidden)
|
||||
}
|
||||
}
|
||||
for _, required := range []string{
|
||||
`ExecStart=${ARCHIE_BIN_DIR}/archied`,
|
||||
"Docker is required for autonomous workflows",
|
||||
} {
|
||||
if !strings.Contains(source, required) {
|
||||
t.Errorf("install.sh is missing deployment contract %q", required)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkerImageIsNotDocumentedAsAStandaloneExecutor(t *testing.T) {
|
||||
source := readDeploymentFile(t, "Dockerfile")
|
||||
if strings.Contains(source, "docker run -e NATS_URL") {
|
||||
t.Error("Dockerfile documents an unsupported manually started worker")
|
||||
}
|
||||
if !strings.Contains(source, "launched only by archied") {
|
||||
t.Error("Dockerfile does not state that archied owns worker lifecycle")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormattingUsesOneConfiguredWriter(t *testing.T) {
|
||||
taskfile := readDeploymentFile(t, "Taskfile.yml")
|
||||
fmtStart := strings.Index(taskfile, " fmt:\n")
|
||||
fmtEnd := strings.Index(taskfile, " proto:install:\n")
|
||||
if fmtStart < 0 || fmtEnd < fmtStart {
|
||||
t.Fatal("Taskfile has no bounded fmt task")
|
||||
}
|
||||
fmtTask := taskfile[fmtStart:fmtEnd]
|
||||
|
||||
const (
|
||||
applyFix = "go fix ./..."
|
||||
verifyFix = "go fix -diff ./..."
|
||||
)
|
||||
fix := strings.Index(fmtTask, applyFix)
|
||||
format := strings.Index(fmtTask, "golangci-lint fmt")
|
||||
if fix < 0 || format < 0 || fix > format {
|
||||
t.Error("fmt task must run go fix before the configured golangci-lint formatters")
|
||||
}
|
||||
if strings.Contains(fmtTask, "gofumpt -") {
|
||||
t.Error("fmt task bypasses the configured formatter set with standalone gofumpt")
|
||||
}
|
||||
// One go fix pass is not a fixpoint: stditerators rewrites
|
||||
// `for i := range t.NumField()` into a field iterator that copies its loop
|
||||
// variable, and forvar only deletes that copy on the following pass. A tree
|
||||
// that stops after one pass is therefore one copyloopvar rejects, so fmt has
|
||||
// to keep applying passes and prove the plateau with a dry run that exits
|
||||
// clean only when nothing is left to fix.
|
||||
//
|
||||
// This assertion was deleted by a "reduce Taskfile bloat" commit along with
|
||||
// the loop it guarded, which put the tree back to a single pass with nothing
|
||||
// to notice. It is the reason the loop is checked for here rather than only
|
||||
// the presence of a go fix invocation.
|
||||
if !strings.Contains(fmtTask, verifyFix) {
|
||||
t.Error("fmt task does not prove that repeated go fix passes have reached a fixpoint")
|
||||
}
|
||||
if got := strings.Count(fmtTask, applyFix); got != 1 {
|
||||
t.Errorf("fmt task applies go fix %d times inline, want 1: the repetition belongs to the fixpoint loop", got)
|
||||
}
|
||||
if got := strings.Count(fmtTask, verifyFix); got != 1 {
|
||||
t.Errorf("fmt task verifies the go fix fixpoint %d times, want 1", got)
|
||||
}
|
||||
if got, owned := strings.Count(taskfile, applyFix), strings.Count(fmtTask, applyFix); got != owned {
|
||||
t.Errorf("Taskfile invokes go fix %d times, %d of them through task fmt: formatting has one owner", got, owned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildTaskSourcesTrackEmbeddedAssets(t *testing.T) {
|
||||
taskfile := readDeploymentFile(t, "Taskfile.yml")
|
||||
start := strings.Index(taskfile, "\n build:\n")
|
||||
end := strings.Index(taskfile, "\n test:\n")
|
||||
if start < 0 || end < start {
|
||||
t.Fatal("Taskfile has no bounded build task")
|
||||
}
|
||||
build := taskfile[start:end]
|
||||
sourcesAt := strings.Index(build, "sources:")
|
||||
generatesAt := strings.Index(build, "generates:")
|
||||
if sourcesAt < 0 || generatesAt < sourcesAt {
|
||||
t.Fatal("build task has no bounded sources block")
|
||||
}
|
||||
sources := build[sourcesAt:generatesAt]
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
path string
|
||||
reason string
|
||||
}{
|
||||
{
|
||||
name: "embedded dashboard",
|
||||
path: "ui/dist/**",
|
||||
reason: "ui/embed.go embeds it with //go:embed all:dist, so a dashboard changing with no .go file changing must still rebuild the binaries",
|
||||
},
|
||||
} {
|
||||
if !strings.Contains(sources, tc.path) {
|
||||
t.Errorf("build sources do not track %s: %s", tc.name, tc.reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildTaskStampsInstallTypeOnEveryCommand(t *testing.T) {
|
||||
taskfile := readDeploymentFile(t, "Taskfile.yml")
|
||||
start := strings.Index(taskfile, "\n build:\n")
|
||||
end := strings.Index(taskfile, "\n test:\n")
|
||||
if start < 0 || end < start {
|
||||
t.Fatal("Taskfile has no bounded build task")
|
||||
}
|
||||
build := taskfile[start:end]
|
||||
cmdsAt := strings.Index(build, "\n cmds:\n")
|
||||
if cmdsAt < 0 {
|
||||
t.Fatal("build task has no cmds block")
|
||||
}
|
||||
cmds := build[cmdsAt:]
|
||||
|
||||
buildLine := regexp.MustCompile(`^ - go build .*\./cmd/([a-z0-9-]+)`)
|
||||
found := 0
|
||||
for line := range strings.SplitSeq(cmds, "\n") {
|
||||
m := buildLine.FindStringSubmatch(line)
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
found++
|
||||
if !strings.Contains(line, "internal/installtype.buildType=") {
|
||||
t.Errorf("build task does not stamp installtype.buildType on %s: %s", m[1], strings.TrimSpace(line))
|
||||
}
|
||||
}
|
||||
if found == 0 {
|
||||
t.Fatal("build task has no go build commands under ./cmd/: this guard would assert nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormattersExcludeInterpretedSecretEngines(t *testing.T) {
|
||||
config := readDeploymentFile(t, ".golangci.yml")
|
||||
if !strings.Contains(config, `^examples/secret-engines/(age|sops|vault)\.go$`) {
|
||||
t.Fatal("formatter config does not protect the interpreted secret engines")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupportedProfilesUseOneExecutionTopology(t *testing.T) {
|
||||
profiles := []string{
|
||||
"deployments/single-forge-github.toml",
|
||||
"deployments/multi-forge-github-gitea.toml",
|
||||
"deployments/local-ollama-standalone.toml",
|
||||
"deployments/docker-nats-stack.toml",
|
||||
}
|
||||
for _, path := range profiles {
|
||||
source := readDeploymentFile(t, path)
|
||||
if strings.Contains(source, "\n[agent]\n") {
|
||||
t.Errorf("%s still selects a legacy agent executor", path)
|
||||
}
|
||||
}
|
||||
|
||||
for _, path := range profiles[:2] {
|
||||
source := readDeploymentFile(t, path)
|
||||
for _, required := range []string{`mode = "embedded"`, "[containers]"} {
|
||||
if !strings.Contains(source, required) {
|
||||
t.Errorf("%s is missing default topology marker %q", path, required)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
external := readDeploymentFile(t, "deployments/docker-nats-stack.toml")
|
||||
if !strings.Contains(external, `mode = "external"`) {
|
||||
t.Error("docker-nats-stack.toml does not explicitly select external NATS")
|
||||
}
|
||||
|
||||
systemd := readDeploymentFile(t, "deployments/systemd-user-service.md")
|
||||
if !strings.Contains(systemd, "Embedded NATS is the default") {
|
||||
t.Error("systemd runbook does not identify embedded NATS as the default")
|
||||
}
|
||||
if strings.Contains(systemd, "still needs NATS") {
|
||||
t.Error("systemd runbook still requires an external Compose NATS service")
|
||||
}
|
||||
}
|
||||
|
||||
// TestInstallerDelegatesConfigGenerationToArchiedSetup pins the contract the
|
||||
// installer already drifted from once: it hand-rolled a config.toml, hardcoded
|
||||
// the GitHub token key and the Gitea host, and choosing Gitea then produced a
|
||||
// config naming a token it never wrote. The durable fix is that the code writing
|
||||
// the config is the code reading it -- archied setup renders it, archied loads it
|
||||
// (docs/architecture/configuration.md) -- and nothing stopped the generating
|
||||
// branch growing back until this.
|
||||
func TestInstallerDelegatesConfigGenerationToArchiedSetup(t *testing.T) {
|
||||
source := readDeploymentFile(t, "install.sh")
|
||||
|
||||
for _, required := range []string{
|
||||
// It runs the binary it just built, so the installer cannot hold a
|
||||
// second idea of what a valid config looks like.
|
||||
`"${ARCHIE_BIN_DIR}/archied" setup`,
|
||||
// For a fresh install only: an existing config is the operator's.
|
||||
`if [ ! -f "${ARCHIE_CONFIG_DIR}/config.toml" ]; then`,
|
||||
"already exists (preserving user config)",
|
||||
// The setup run is allowed to fail, and a failure is loud. A config that
|
||||
// never gets written is the only honest outcome after that.
|
||||
"could not generate",
|
||||
} {
|
||||
if !strings.Contains(source, required) {
|
||||
t.Errorf("install.sh is missing %q", required)
|
||||
}
|
||||
}
|
||||
|
||||
// Nothing here renders TOML or splices it afterwards. The splice check is
|
||||
// per line so a legitimate sed/awk elsewhere in the installer stays allowed.
|
||||
if strings.Contains(source, "forge_block") {
|
||||
t.Error("install.sh still carries the forge_block helper: it is what hardcoded the GitHub token key and Gitea host")
|
||||
}
|
||||
redirection := regexp.MustCompile(`>>?\s*"?[^"'\s]*config\.toml`)
|
||||
for index, line := range strings.Split(source, "\n") {
|
||||
if !strings.Contains(line, "config.toml") {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(line, "sed ") || strings.Contains(line, "awk ") {
|
||||
t.Errorf("install.sh:%d splices config.toml (%s)", index+1, strings.TrimSpace(line))
|
||||
}
|
||||
if redirection.MatchString(line) {
|
||||
t.Errorf("install.sh:%d writes config.toml itself (%s)", index+1, strings.TrimSpace(line))
|
||||
}
|
||||
}
|
||||
// A TOML table header in the installer is a template by definition. The
|
||||
// systemd unit it also writes is INI, whose only sections are Unit/Service/
|
||||
// Install, so those are no part of this list.
|
||||
template := regexp.MustCompile(`(?m)^\s*\[(forge|models|providers|runners|containers|nats|web|budgets|notify|identit|chat)\]`)
|
||||
if found := template.FindString(source); found != "" {
|
||||
t.Errorf("install.sh carries a TOML template (%q): the installer must not hold its own idea of the config schema", strings.TrimSpace(found))
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The task-scoped archie-agent process owns the workflow and its local model
|
||||
// loop. agentexec therefore contains the worker-local stage runner, never a
|
||||
// second process boundary or a stage transport that archied can select.
|
||||
func TestPackageContainsNoLegacyExecutionModes(t *testing.T) {
|
||||
forbidden := map[string]bool{
|
||||
"InProcessRunner": true,
|
||||
"NewInProcessRunner": true,
|
||||
"SubprocessRunner": true,
|
||||
"NATSRunner": true,
|
||||
"ServeOne": true,
|
||||
"Invocation": true,
|
||||
"AgentRequestMessage": true,
|
||||
"AgentResponseEnvelope": true,
|
||||
"StageMessage": true,
|
||||
"StageConsumer": true,
|
||||
}
|
||||
files, err := filepath.Glob("*.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fileset := token.NewFileSet()
|
||||
for _, path := range files {
|
||||
if strings.HasSuffix(path, "_test.go") {
|
||||
continue
|
||||
}
|
||||
file, parseErr := parser.ParseFile(fileset, path, nil, 0)
|
||||
if parseErr != nil {
|
||||
t.Errorf("parse %s: %v", path, parseErr)
|
||||
continue
|
||||
}
|
||||
for _, declaration := range file.Decls {
|
||||
switch declaration := declaration.(type) {
|
||||
case *ast.GenDecl:
|
||||
for _, spec := range declaration.Specs {
|
||||
typeSpec, ok := spec.(*ast.TypeSpec)
|
||||
if ok && forbidden[typeSpec.Name.Name] {
|
||||
t.Errorf("%s declares legacy execution type %s", fileset.Position(typeSpec.Pos()), typeSpec.Name.Name)
|
||||
}
|
||||
}
|
||||
case *ast.FuncDecl:
|
||||
if declaration.Recv == nil && forbidden[declaration.Name.Name] {
|
||||
t.Errorf("%s declares legacy execution constructor %s", fileset.Position(declaration.Pos()), declaration.Name.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
var findingTool = CaptureTool{
|
||||
Name: "record_finding",
|
||||
Description: "Record one finding.",
|
||||
Parameters: json.RawMessage(`{"type":"object","properties":{"title":{"type":"string"},"severity":{"type":"string"}},"required":["title"]}`),
|
||||
RequiredFields: []string{"title"},
|
||||
MaxCalls: 2,
|
||||
}
|
||||
|
||||
type lockedBuffer struct {
|
||||
mu sync.Mutex
|
||||
buf bytes.Buffer
|
||||
}
|
||||
|
||||
func (b *lockedBuffer) Write(p []byte) (int, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.buf.Write(p)
|
||||
}
|
||||
|
||||
func (b *lockedBuffer) String() string {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.buf.String()
|
||||
}
|
||||
|
||||
func connectCaptureServer(t *testing.T, sink *lockedBuffer) *mcp.ClientSession {
|
||||
t.Helper()
|
||||
serverT, clientT := mcp.NewInMemoryTransports()
|
||||
go func() { _ = ServeCaptureMCP(t.Context(), []CaptureTool{findingTool}, sink, serverT) }()
|
||||
session, err := mcp.NewClient(&mcp.Implementation{Name: "test", Version: "0"}, nil).Connect(t.Context(), clientT, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = session.Close() })
|
||||
return session
|
||||
}
|
||||
|
||||
func callText(t *testing.T, s *mcp.ClientSession, args any) string {
|
||||
t.Helper()
|
||||
res, err := s.CallTool(t.Context(), &mcp.CallToolParams{Name: "record_finding", Arguments: args})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out strings.Builder
|
||||
for _, c := range res.Content {
|
||||
if text, ok := c.(*mcp.TextContent); ok {
|
||||
out.WriteString(text.Text)
|
||||
}
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func TestCaptureMCPServesTheStageCaptureTools(t *testing.T) {
|
||||
sink := &lockedBuffer{}
|
||||
s := connectCaptureServer(t, sink)
|
||||
tools, err := s.ListTools(t.Context(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(tools.Tools) != 1 || tools.Tools[0].Name != "record_finding" || tools.Tools[0].Description != "Record one finding." {
|
||||
t.Fatalf("tools %+v, want the stage's one capture tool", tools.Tools)
|
||||
}
|
||||
|
||||
if got := callText(t, s, map[string]any{"title": "nil deref", "severity": "important"}); !strings.Contains(got, "recorded") {
|
||||
t.Fatalf("valid call replied %q", got)
|
||||
}
|
||||
if got := callText(t, s, map[string]any{"severity": "nitpick"}); !strings.Contains(got, "rejected") {
|
||||
t.Fatalf("call missing a required field replied %q", got)
|
||||
}
|
||||
if got := callText(t, s, map[string]any{"title": "second"}); !strings.Contains(got, "recorded") {
|
||||
t.Fatalf("second valid call replied %q", got)
|
||||
}
|
||||
if got := callText(t, s, map[string]any{"title": "third"}); !strings.Contains(got, "maximum") {
|
||||
t.Fatalf("call past MaxCalls replied %q", got)
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(sink.String()), "\n")
|
||||
if len(lines) != 2 {
|
||||
t.Fatalf("sink has %d records, want only the 2 accepted calls:\n%s", len(lines), sink.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadCapturesRevalidatesEverything(t *testing.T) {
|
||||
// The captures file is writable by the harness user, so anything in it
|
||||
// may be forged. Only records that pass the same validation survive.
|
||||
path := filepath.Join(t.TempDir(), "captures.jsonl")
|
||||
content := strings.Join([]string{
|
||||
`{"tool":"record_finding","args":{"title":"real"}}`,
|
||||
`{"tool":"record_finding","args":{"severity":"no title"}}`,
|
||||
`{"tool":"grant_admin","args":{"title":"x"}}`,
|
||||
`not json at all`,
|
||||
`{"tool":"record_finding","args":{"title":"second"}}`,
|
||||
`{"tool":"record_finding","args":{"title":"over the cap"}}`,
|
||||
}, "\n")
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
captures, err := readCaptures(path, []CaptureTool{findingTool})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var titles []string
|
||||
for _, raw := range captures["record_finding"] {
|
||||
var v struct{ Title string }
|
||||
_ = json.Unmarshal(raw, &v)
|
||||
titles = append(titles, v.Title)
|
||||
}
|
||||
if !slices.Equal(titles, []string{"real", "second"}) || len(captures) != 1 {
|
||||
t.Fatalf("captures %v (titles %v), want only the two valid record_finding calls", captures, titles)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessCollectsCapturesThroughTheMCPServer(t *testing.T) {
|
||||
f := newFixture(t, "capture")
|
||||
f.req.CaptureTools = []CaptureTool{findingTool}
|
||||
f.req.Harness.MCPConfig = []string{"--mcp-config", "{{.MCPConfig}}"}
|
||||
res, _ := f.run(t, t.Context())
|
||||
if res.Status != StatusPassed {
|
||||
t.Fatalf("status %q (%s)", res.Status, res.Detail)
|
||||
}
|
||||
if got := len(res.Captures["record_finding"]); got != 1 {
|
||||
t.Fatalf("captures %v, want the one valid record the harness made", res.Captures)
|
||||
}
|
||||
inv := f.invocations(t)
|
||||
if len(inv) != 1 || !strings.Contains(inv[0], "--mcp-config ") {
|
||||
t.Fatalf("invocations %q, want the MCP config registered", inv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessRefusesCaptureToolsWithoutMCP(t *testing.T) {
|
||||
f := newFixture(t, "edit")
|
||||
f.req.CaptureTools = []CaptureTool{findingTool}
|
||||
if _, err := NewHarnessRunner(nil).Run(t.Context(), f.workspace, f.req, nil); err == nil {
|
||||
t.Fatal("a stage with capture tools ran on a harness with no MCP registration; its results would be lost")
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A capture tool sometimes needs a field only under a condition the same call
|
||||
// carries: triage must name a workflow when it says a code change is needed,
|
||||
// and must not be made to invent one when it says the opposite. A flat
|
||||
// RequiredFields entry cannot say that, so the choice used to be between a
|
||||
// meaningless forced answer and a silent default.
|
||||
func TestValidateCaptureArgsConditionalRequirement(t *testing.T) {
|
||||
spec := CaptureTool{
|
||||
Name: "decide",
|
||||
RequiredFields: []string{"needs_code_change"},
|
||||
RequiredWhenTrue: map[string][]string{
|
||||
"needs_code_change": {"workflow"},
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args string
|
||||
wantOK bool
|
||||
wantRejected string
|
||||
}{
|
||||
{
|
||||
name: "trigger true and field present",
|
||||
args: `{"needs_code_change": true, "workflow": "feasibility"}`,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "trigger true and field missing",
|
||||
args: `{"needs_code_change": true}`,
|
||||
wantRejected: "workflow",
|
||||
},
|
||||
{
|
||||
name: "trigger true and field empty",
|
||||
args: `{"needs_code_change": true, "workflow": " "}`,
|
||||
wantRejected: "workflow",
|
||||
},
|
||||
{
|
||||
name: "trigger false, field not required",
|
||||
args: `{"needs_code_change": false}`,
|
||||
wantOK: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rejection, ok := validateCaptureArgs(spec, json.RawMessage(tt.args))
|
||||
if ok != tt.wantOK {
|
||||
t.Fatalf("validateCaptureArgs ok = %v, want %v (rejection %q)", ok, tt.wantOK, rejection)
|
||||
}
|
||||
if tt.wantRejected != "" && !strings.Contains(rejection, tt.wantRejected) {
|
||||
t.Fatalf("rejection %q does not name %q", rejection, tt.wantRejected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClaudeCodeHarnessOutputFixtures(t *testing.T) {
|
||||
errorDetail := "Exit code 1\ncat: /nonexistent-file: No such file or directory"
|
||||
cases := []struct {
|
||||
name string
|
||||
wantCalls []ToolCallReport
|
||||
wantUsage Usage
|
||||
wantSession string
|
||||
}{
|
||||
{
|
||||
name: "run.jsonl",
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "Bash", Detail: "hello"},
|
||||
{Tool: "Bash", Detail: errorDetail, Failed: true},
|
||||
},
|
||||
wantUsage: Usage{PromptTokens: 102420, CompletionTokens: 248, TotalTokens: 102668, CachedTokens: 78105, CacheCreationTokens: 24309},
|
||||
wantSession: "9c22c15c-80ef-42c6-a503-8d087ec47d92",
|
||||
},
|
||||
{
|
||||
name: "killed-before-result.jsonl",
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "Bash", Detail: "hello"},
|
||||
{Tool: "Bash", Detail: errorDetail, Failed: true},
|
||||
},
|
||||
wantUsage: Usage{PromptTokens: 102420, CompletionTokens: 121, TotalTokens: 102541, CachedTokens: 78105, CacheCreationTokens: 24309},
|
||||
wantSession: "9c22c15c-80ef-42c6-a503-8d087ec47d92",
|
||||
},
|
||||
{
|
||||
name: "malformed-line.jsonl",
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "Bash", Detail: "hello"},
|
||||
{Tool: "Bash", Detail: errorDetail, Failed: true},
|
||||
},
|
||||
wantUsage: Usage{PromptTokens: 102420, CompletionTokens: 248, TotalTokens: 102668, CachedTokens: 78105, CacheCreationTokens: 24309},
|
||||
wantSession: "9c22c15c-80ef-42c6-a503-8d087ec47d92",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
stream, err := os.Open("testdata/claude/" + tc.name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
output := newClaudeCodeHarnessOutput()
|
||||
var calls []ToolCallReport
|
||||
scanner := bufio.NewScanner(stream)
|
||||
for scanner.Scan() {
|
||||
output.Line(scanner.Bytes(), func(call ToolCallReport) { calls = append(calls, call) })
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(calls, tc.wantCalls) {
|
||||
t.Errorf("tool calls = %#v, want %#v", calls, tc.wantCalls)
|
||||
}
|
||||
if got := output.SessionID(); got != tc.wantSession {
|
||||
t.Errorf("session ID = %q, want %q", got, tc.wantSession)
|
||||
}
|
||||
if got := output.Usage(); got != tc.wantUsage {
|
||||
t.Errorf("usage = %#v, want %#v", got, tc.wantUsage)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeCodeHarnessOutputToolResultContent(t *testing.T) {
|
||||
long := strings.Repeat("x", toolCallDetailBytes+25)
|
||||
cases := []struct {
|
||||
name string
|
||||
content any
|
||||
wantDetail string
|
||||
}{
|
||||
{name: "string", content: "plain result", wantDetail: "plain result"},
|
||||
{name: "text blocks", content: []map[string]string{{"type": "text", "text": "first"}, {"type": "text", "text": "second"}}, wantDetail: "first\nsecond"},
|
||||
{name: "clipped", content: long, wantDetail: strings.Repeat("x", toolCallDetailBytes) + "…"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
output := newClaudeCodeHarnessOutput()
|
||||
var calls []ToolCallReport
|
||||
report := func(call ToolCallReport) { calls = append(calls, call) }
|
||||
sendClaudeEvent(t, output, report, map[string]any{
|
||||
"type": "assistant", "session_id": "old-session",
|
||||
"message": map[string]any{
|
||||
"id": "same-message", "usage": claudeUsageFixture(2, 3, 5, 7),
|
||||
"content": []any{map[string]any{"type": "tool_use", "id": "call-1", "name": "Read", "input": map[string]any{}}},
|
||||
},
|
||||
})
|
||||
sendClaudeEvent(t, output, report, map[string]any{
|
||||
"type": "assistant", "session_id": "newer-session",
|
||||
"message": map[string]any{
|
||||
"id": "same-message", "usage": claudeUsageFixture(2, 3, 5, 7),
|
||||
"content": []any{map[string]any{"type": "tool_use", "id": "call-2", "name": "Write", "input": map[string]any{}}},
|
||||
},
|
||||
})
|
||||
sendClaudeEvent(t, output, report, map[string]any{
|
||||
"type": "user", "session_id": "latest-session",
|
||||
"message": map[string]any{"content": []any{map[string]any{"type": "tool_result", "tool_use_id": "call-1", "content": tc.content, "is_error": false}}},
|
||||
})
|
||||
sendClaudeEvent(t, output, report, map[string]any{
|
||||
"type": "user", "session_id": "",
|
||||
"message": map[string]any{"content": []any{map[string]any{"type": "tool_result", "tool_use_id": "call-2", "content": "done", "is_error": false}}},
|
||||
})
|
||||
|
||||
wantCalls := []ToolCallReport{{Tool: "Read", Detail: tc.wantDetail}, {Tool: "Write", Detail: "done"}}
|
||||
if !reflect.DeepEqual(calls, wantCalls) {
|
||||
t.Errorf("tool calls = %#v, want %#v", calls, wantCalls)
|
||||
}
|
||||
wantUsage := Usage{PromptTokens: 14, CompletionTokens: 3, TotalTokens: 17, CachedTokens: 5, CacheCreationTokens: 7}
|
||||
if got := output.Usage(); got != wantUsage {
|
||||
t.Errorf("usage = %#v, want %#v", got, wantUsage)
|
||||
}
|
||||
if got := output.SessionID(); got != "latest-session" {
|
||||
t.Errorf("session ID = %q, want latest non-empty event value", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func sendClaudeEvent(t *testing.T, output HarnessOutput, report ToolCallReporter, event map[string]any) {
|
||||
t.Helper()
|
||||
line, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output.Line(line, report)
|
||||
}
|
||||
|
||||
func claudeUsageFixture(input, output, cacheRead, cacheCreate int) map[string]any {
|
||||
return map[string]any{
|
||||
"input_tokens": input, "output_tokens": output,
|
||||
"cache_read_input_tokens": cacheRead, "cache_creation_input_tokens": cacheCreate,
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHarnessOutputFixtures(t *testing.T) {
|
||||
const missing = "cat: /nonexistent-file: No such file or directory\n"
|
||||
cases := []struct {
|
||||
adapter string
|
||||
fixture string
|
||||
// stopAt truncates the stream before the first line containing it,
|
||||
// as when the process is killed mid-run.
|
||||
stopAt string
|
||||
wantCalls []ToolCallReport
|
||||
wantUsage Usage
|
||||
wantSession string
|
||||
}{
|
||||
{
|
||||
adapter: AdapterCodex, fixture: "codex/run.jsonl",
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "command_execution", Detail: "hello\n"},
|
||||
{Tool: "command_execution", Detail: missing, Failed: true},
|
||||
},
|
||||
wantUsage: Usage{PromptTokens: 72082, CompletionTokens: 118, TotalTokens: 72200, CachedTokens: 57600},
|
||||
wantSession: "01a0db18-e119-7872-bf13-3644369044fb",
|
||||
},
|
||||
{
|
||||
adapter: AdapterCodex, fixture: "codex/mcp.jsonl",
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "record_verdict", Detail: "record_verdict recorded"},
|
||||
{Tool: "file_change", Detail: "/tmp/hfx/note.txt"},
|
||||
},
|
||||
wantUsage: Usage{PromptTokens: 96397, CompletionTokens: 203, TotalTokens: 96600, CachedTokens: 80896},
|
||||
wantSession: "01a0db19-f9d6-7ad2-870c-c29b7fd8ab0a",
|
||||
},
|
||||
{
|
||||
adapter: AdapterCodex, fixture: "codex/run.jsonl", stopAt: `"turn.completed"`,
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "command_execution", Detail: "hello\n"},
|
||||
{Tool: "command_execution", Detail: missing, Failed: true},
|
||||
},
|
||||
wantSession: "01a0db18-e119-7872-bf13-3644369044fb",
|
||||
},
|
||||
{
|
||||
adapter: AdapterPi, fixture: "pi/run.jsonl",
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "bash", Detail: "hello\n"},
|
||||
{Tool: "bash", Detail: missing + "\n\nCommand exited with code 1", Failed: true},
|
||||
},
|
||||
wantUsage: Usage{PromptTokens: 109198, CompletionTokens: 38, TotalTokens: 109236, CachedTokens: 62720},
|
||||
wantSession: "01a0db18-e183-7432-9e4b-4b16bfc83856",
|
||||
},
|
||||
{
|
||||
adapter: AdapterPi, fixture: "pi/run.jsonl", stopAt: `"agent_end"`,
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "bash", Detail: "hello\n"},
|
||||
{Tool: "bash", Detail: missing + "\n\nCommand exited with code 1", Failed: true},
|
||||
},
|
||||
wantUsage: Usage{PromptTokens: 109198, CompletionTokens: 38, TotalTokens: 109236, CachedTokens: 62720},
|
||||
wantSession: "01a0db18-e183-7432-9e4b-4b16bfc83856",
|
||||
},
|
||||
{
|
||||
adapter: AdapterOMP, fixture: "omp/run.jsonl",
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "bash", Detail: "hello\n\n\nWall time: 0.03 seconds"},
|
||||
{Tool: "bash", Detail: missing + "\n\nWall time: 0.00 seconds\n\nCommand exited with code 1", Failed: true},
|
||||
},
|
||||
wantUsage: Usage{PromptTokens: 75962, CompletionTokens: 161, TotalTokens: 76123, CachedTokens: 50304},
|
||||
wantSession: "01a0db18-e4a1-7000-9cbe-7467b0385e65",
|
||||
},
|
||||
{
|
||||
adapter: AdapterCopilot, fixture: "copilot/run.jsonl",
|
||||
wantCalls: []ToolCallReport{
|
||||
{Tool: "bash", Detail: "hello\n<shellId: 0 completed with exit code 0>"},
|
||||
{Tool: "bash", Detail: missing + "<shellId: 1 completed with exit code 1>"},
|
||||
},
|
||||
wantSession: "bdf5531e-b8a4-4baf-bed2-d914242fa387",
|
||||
},
|
||||
{
|
||||
adapter: AdapterCopilot, fixture: "copilot/mcp.jsonl",
|
||||
wantCalls: []ToolCallReport{{Tool: "record_verdict", Detail: "record_verdict recorded"}},
|
||||
wantSession: "46ce5d05-adf6-45ac-82bc-167a3c48a146",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.fixture+tc.stopAt, func(t *testing.T) {
|
||||
adapter, ok := LookupHarnessAdapter(tc.adapter)
|
||||
if !ok {
|
||||
t.Fatalf("adapter %q is not registered", tc.adapter)
|
||||
}
|
||||
stream, err := os.Open("testdata/" + tc.fixture)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
output := adapter.NewOutput()
|
||||
var calls []ToolCallReport
|
||||
report := func(call ToolCallReport) { calls = append(calls, call) }
|
||||
output.Line([]byte("{not json"), report)
|
||||
scanner := bufio.NewScanner(stream)
|
||||
scanner.Buffer(nil, 1<<20)
|
||||
for scanner.Scan() {
|
||||
if tc.stopAt != "" && strings.Contains(scanner.Text(), tc.stopAt) {
|
||||
break
|
||||
}
|
||||
output.Line(scanner.Bytes(), report)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(calls, tc.wantCalls) {
|
||||
t.Errorf("tool calls = %#v, want %#v", calls, tc.wantCalls)
|
||||
}
|
||||
if got := output.SessionID(); got != tc.wantSession {
|
||||
t.Errorf("session ID = %q, want %q", got, tc.wantSession)
|
||||
}
|
||||
if got := output.Usage(); got != tc.wantUsage {
|
||||
t.Errorf("usage = %#v, want %#v", got, tc.wantUsage)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,428 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakeHarness is a stand-in coding-agent CLI. FAKE_SCENARIO picks what it
|
||||
// does to the workspace; every invocation's arguments are appended to
|
||||
// FAKE_LOG, and it prints a session line and a tool line for the adapter.
|
||||
const fakeHarness = `#!/bin/sh
|
||||
printf '%s' "$*" | tr '\n' ' ' >> "$FAKE_LOG"
|
||||
echo >> "$FAKE_LOG"
|
||||
case "$FAKE_SCENARIO" in
|
||||
edit) echo hello > notes.txt ;;
|
||||
commit) echo x > a.txt; git add a.txt; git -c user.email=a@b.c -c user.name=a commit -qm sneaky ;;
|
||||
config) git config remote.origin.url https://attacker.invalid/repo.git ;;
|
||||
protected) echo broken >> widget_test.go ;;
|
||||
sleep) sleep 30 & echo $! > "$FAKE_LOG.child"; wait ;;
|
||||
env) env > "$FAKE_LOG.env" ;;
|
||||
capture)
|
||||
cfg=""; prev=""
|
||||
for a in "$@"; do [ "$prev" = "--mcp-config" ] && cfg="$a"; prev="$a"; done
|
||||
out=$(sed -n 's/.*"-captures","\([^"]*\)".*/\1/p' "$cfg")
|
||||
echo '{"tool":"record_finding","args":{"title":"found it"}}' >> "$out"
|
||||
echo '{"tool":"record_finding","args":{"severity":"forged, no title"}}' >> "$out" ;;
|
||||
claude) echo '{"type":"system","subtype":"init","session_id":"C42"}' ;;
|
||||
fix-on-resume) if [ -f .attempted ]; then echo ok > fixed.txt; else touch .attempted; fi ;;
|
||||
esac
|
||||
echo "session=S123"
|
||||
echo "tool=edit_file"
|
||||
`
|
||||
|
||||
// lineOutput is a test adapter over fakeHarness's output lines.
|
||||
type lineOutput struct {
|
||||
mu sync.Mutex
|
||||
session string
|
||||
}
|
||||
|
||||
func (o *lineOutput) Line(line []byte, report ToolCallReporter) {
|
||||
text := string(line)
|
||||
switch {
|
||||
case strings.HasPrefix(text, "session="):
|
||||
o.mu.Lock()
|
||||
o.session = strings.TrimPrefix(text, "session=")
|
||||
o.mu.Unlock()
|
||||
case strings.HasPrefix(text, "tool=") && report != nil:
|
||||
report(ToolCallReport{Tool: strings.TrimPrefix(text, "tool=")})
|
||||
}
|
||||
}
|
||||
|
||||
func (o *lineOutput) SessionID() string {
|
||||
o.mu.Lock()
|
||||
defer o.mu.Unlock()
|
||||
return o.session
|
||||
}
|
||||
|
||||
func (o *lineOutput) Usage() Usage { return Usage{} }
|
||||
|
||||
func runGit(t *testing.T, dir string, args ...string) {
|
||||
t.Helper()
|
||||
cmd := exec.CommandContext(t.Context(), "git", append([]string{"-c", "user.email=a@b.c", "-c", "user.name=a"}, args...)...)
|
||||
cmd.Dir = dir
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("git %v: %v\n%s", args, err, out)
|
||||
}
|
||||
}
|
||||
|
||||
// hostileSigningConfig writes a global git config that makes every commit
|
||||
// fail unless the fixture disables signing in the repository's own config.
|
||||
func hostileSigningConfig(t *testing.T) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "hostile.gitconfig")
|
||||
const cfg = "[user]\n\tname = Hostile\n\temail = hostile@example.com\n\tsigningkey = /nonexistent/signing-key\n[gpg]\n\tformat = ssh\n[commit]\n\tgpgsign = true\n"
|
||||
if err := os.WriteFile(path, []byte(cfg), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// TestFixtureSeedsARepositoryThatIgnoresHostSigningConfig pins the fixture's
|
||||
// hermeticity: it must not depend on the developer's global git identity or a
|
||||
// signing agent.
|
||||
func TestFixtureSeedsARepositoryThatIgnoresHostSigningConfig(t *testing.T) {
|
||||
t.Setenv("GIT_CONFIG_GLOBAL", hostileSigningConfig(t))
|
||||
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
||||
newFixture(t, "edit")
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
workspace string
|
||||
log string
|
||||
req Request
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T, scenario string) *fixture {
|
||||
t.Helper()
|
||||
ws := t.TempDir()
|
||||
runGit(t, ws, "init", "-q")
|
||||
runGit(t, ws, "config", "commit.gpgsign", "false")
|
||||
if err := os.WriteFile(filepath.Join(ws, "widget_test.go"), []byte("package widget\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runGit(t, ws, "add", ".")
|
||||
runGit(t, ws, "commit", "-qm", "base")
|
||||
runGit(t, ws, "remote", "add", "origin", "https://forge.invalid/repo.git")
|
||||
|
||||
bin := filepath.Join(t.TempDir(), "fake-harness")
|
||||
if err := os.WriteFile(bin, []byte(fakeHarness), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
log := filepath.Join(t.TempDir(), "invocations")
|
||||
return &fixture{workspace: ws, log: log, req: Request{
|
||||
Version: ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "implement", Model: "harness",
|
||||
Mission: "Write the notes file.",
|
||||
Budget: Budget{WallClock: 20 * time.Second},
|
||||
Harness: &HarnessSpec{
|
||||
Env: []string{"PATH=" + os.Getenv("PATH"), "FAKE_SCENARIO=" + scenario, "FAKE_LOG=" + log},
|
||||
Launch: []string{"/bin/sh", bin},
|
||||
Prompt: []string{"-p", "{{.Prompt}}"},
|
||||
Resume: []string{"--resume", "{{.SessionID}}"},
|
||||
},
|
||||
}}
|
||||
}
|
||||
|
||||
func (f *fixture) run(t *testing.T, ctx context.Context) (Result, []ToolCallReport) {
|
||||
t.Helper()
|
||||
var mu sync.Mutex
|
||||
var calls []ToolCallReport
|
||||
res, err := NewHarnessRunner(func() HarnessOutput { return &lineOutput{} }).Run(ctx, f.workspace, f.req, func(r ToolCallReport) {
|
||||
mu.Lock()
|
||||
calls = append(calls, r)
|
||||
mu.Unlock()
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run: %v", err)
|
||||
}
|
||||
return res, calls
|
||||
}
|
||||
|
||||
func (f *fixture) invocations(t *testing.T) []string {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(f.log)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var out []string
|
||||
sc := bufio.NewScanner(bytes.NewReader(raw))
|
||||
for sc.Scan() {
|
||||
out = append(out, sc.Text())
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestHarnessRunsTheMissionAndReportsChanges(t *testing.T) {
|
||||
f := newFixture(t, "edit")
|
||||
res, calls := f.run(t, t.Context())
|
||||
if res.Status != StatusPassed {
|
||||
t.Fatalf("status %q (%s), want passed", res.Status, res.StopReason)
|
||||
}
|
||||
if !slices.Equal(res.Changes, []string{"notes.txt"}) {
|
||||
t.Fatalf("changes %v, want [notes.txt]", res.Changes)
|
||||
}
|
||||
if len(calls) != 1 || calls[0].Tool != "edit_file" {
|
||||
t.Fatalf("tool calls %v, want one edit_file", calls)
|
||||
}
|
||||
inv := f.invocations(t)
|
||||
if len(inv) != 1 || !strings.HasPrefix(inv[0], "-p ") || !strings.Contains(inv[0], "Write the notes file.") {
|
||||
t.Fatalf("invocations %q, want one headless prompt carrying the mission", inv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessGuaranteesHoldWhateverTheHarnessDoes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
scenario string
|
||||
setup func(*Request)
|
||||
want string
|
||||
}{
|
||||
{"a commit fails the step", "commit", nil, "git"},
|
||||
{"rewriting the git config fails the step", "config", nil, "git"},
|
||||
{"editing a protected path fails the step", "protected", func(r *Request) { r.Protection = Protection{Suffixes: []string{"_test.go"}} }, "widget_test.go"},
|
||||
{"writing in a read-only stage fails the step", "edit", func(r *Request) { r.ReadOnly = true }, "notes.txt"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f := newFixture(t, tt.scenario)
|
||||
if tt.setup != nil {
|
||||
tt.setup(&f.req)
|
||||
}
|
||||
res, _ := f.run(t, t.Context())
|
||||
if res.Status != StatusParked || !strings.Contains(res.StopReason, StopPolicy) || !strings.Contains(res.Detail, tt.want) {
|
||||
t.Fatalf("status %q stop %q detail %q; want parked on policy naming %q", res.Status, res.StopReason, res.Detail, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessIgnoresChangesThatPredateTheRun(t *testing.T) {
|
||||
f := newFixture(t, "edit")
|
||||
if err := os.WriteFile(filepath.Join(f.workspace, "earlier.txt"), []byte("from a previous stage\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.req.ReadOnly = false
|
||||
res, _ := f.run(t, t.Context())
|
||||
if !slices.Equal(res.Changes, []string{"notes.txt"}) {
|
||||
t.Fatalf("changes %v, want only what this run changed", res.Changes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessBudgetAndCancellation(t *testing.T) {
|
||||
t.Run("the wall clock kills the harness", func(t *testing.T) {
|
||||
f := newFixture(t, "sleep")
|
||||
f.req.Budget.WallClock = 500 * time.Millisecond
|
||||
start := time.Now()
|
||||
res, _ := f.run(t, t.Context())
|
||||
if time.Since(start) > 10*time.Second {
|
||||
t.Fatalf("run took %s; the harness outlived its budget", time.Since(start))
|
||||
}
|
||||
if res.Status != StatusParked || res.StopReason != StopTimedOut {
|
||||
t.Fatalf("status %q stop %q, want parked timed_out", res.Status, res.StopReason)
|
||||
}
|
||||
assertChildDead(t, f.log+".child")
|
||||
})
|
||||
t.Run("cancelling the run kills the harness", func(t *testing.T) {
|
||||
f := newFixture(t, "sleep")
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
time.AfterFunc(300*time.Millisecond, cancel)
|
||||
start := time.Now()
|
||||
_, err := NewHarnessRunner(func() HarnessOutput { return &lineOutput{} }).Run(ctx, f.workspace, f.req, nil)
|
||||
if time.Since(start) > 10*time.Second {
|
||||
t.Fatalf("cancel took %s", time.Since(start))
|
||||
}
|
||||
if err == nil || ctx.Err() == nil {
|
||||
t.Fatalf("Run after cancel returned %v, want the cancellation", err)
|
||||
}
|
||||
assertChildDead(t, f.log+".child")
|
||||
})
|
||||
}
|
||||
|
||||
func TestHarnessGateResumesTheSession(t *testing.T) {
|
||||
gate := Gate{Commands: []Command{{Name: "fixed", Argv: []string{"test", "-f", "fixed.txt"}}}, MaxConsecutiveFailures: 3}
|
||||
|
||||
t.Run("a failed gate resumes the same session with its output", func(t *testing.T) {
|
||||
f := newFixture(t, "fix-on-resume")
|
||||
f.req.Gate = gate
|
||||
res, _ := f.run(t, t.Context())
|
||||
if res.Status != StatusPassed || res.Iterations != 2 {
|
||||
t.Fatalf("status %q after %d iterations (%s), want passed after 2", res.Status, res.Iterations, res.StopReason)
|
||||
}
|
||||
inv := f.invocations(t)
|
||||
if len(inv) != 2 || !strings.HasPrefix(inv[1], "--resume S123 -p ") || !strings.Contains(inv[1], "[fixed]") {
|
||||
t.Fatalf("invocations %q, want a resume of S123 carrying the gate failure", inv)
|
||||
}
|
||||
})
|
||||
t.Run("a gate that never passes parks after the cap", func(t *testing.T) {
|
||||
f := newFixture(t, "edit")
|
||||
f.req.Gate = gate
|
||||
res, _ := f.run(t, t.Context())
|
||||
if res.Status != StatusParked || res.Iterations != 3 {
|
||||
t.Fatalf("status %q after %d iterations, want parked after 3", res.Status, res.Iterations)
|
||||
}
|
||||
})
|
||||
t.Run("a gate retry without a resume verb is refused before any invocation", func(t *testing.T) {
|
||||
f := newFixture(t, "fix-on-resume")
|
||||
f.req.Gate = gate
|
||||
f.req.Harness.Resume = nil
|
||||
if _, err := NewHarnessRunner(nil).Run(t.Context(), f.workspace, f.req, nil); err == nil || len(f.invocations(t)) != 0 {
|
||||
t.Fatalf("err %v after %d invocations, want a refusal before the harness ran", err, len(f.invocations(t)))
|
||||
}
|
||||
})
|
||||
t.Run("a single-attempt gate needs no resume verb", func(t *testing.T) {
|
||||
f := newFixture(t, "edit")
|
||||
f.req.Gate = Gate{Commands: gate.Commands, MaxConsecutiveFailures: 1}
|
||||
f.req.Harness.Resume = nil
|
||||
res, _ := f.run(t, t.Context())
|
||||
if res.Status != StatusParked || res.Iterations != 1 {
|
||||
t.Fatalf("status %q after %d iterations, want parked after 1", res.Status, res.Iterations)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestHarnessRequestValidation(t *testing.T) {
|
||||
f := newFixture(t, "edit")
|
||||
f.req.Harness.Prompt = []string{"-p"}
|
||||
if _, err := NewHarnessRunner(nil).Run(t.Context(), f.workspace, f.req, nil); err == nil {
|
||||
t.Fatal("a prompt verb with no {{.Prompt}} placeholder ran; the mission would be silently dropped")
|
||||
}
|
||||
f.req.Harness.Prompt = []string{"-p", "{{.Prompt}}"}
|
||||
f.req.Harness.Adapter = "no-such-cli"
|
||||
if _, err := NewHarnessRunner(nil).Run(t.Context(), f.workspace, f.req, nil); err == nil {
|
||||
t.Fatal("an unknown output adapter ran; its output would be read by nothing")
|
||||
}
|
||||
f.req.Harness = nil
|
||||
if _, err := NewHarnessRunner(nil).Run(t.Context(), f.workspace, f.req, nil); err == nil {
|
||||
t.Fatal("a request without a harness spec ran on the harness runner")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessReadsOutputWithTheNamedAdapter(t *testing.T) {
|
||||
f := newFixture(t, "claude")
|
||||
f.req.Harness.Adapter = AdapterClaudeCode
|
||||
f.req.Gate = Gate{Commands: []Command{{Name: "never", Argv: []string{"false"}}}, MaxConsecutiveFailures: 2}
|
||||
if _, err := NewHarnessRunner(nil).Run(t.Context(), f.workspace, f.req, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inv := f.invocations(t)
|
||||
if len(inv) != 2 || !strings.HasPrefix(inv[1], "--resume C42 ") {
|
||||
t.Fatalf("invocations %q, want a resume of the session the Claude Code adapter read", inv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeCodeAdapterRegistersTheMCPServer(t *testing.T) {
|
||||
a, ok := LookupHarnessAdapter(AdapterClaudeCode)
|
||||
if !ok || !slices.ContainsFunc(a.MCPConfig, func(s string) bool { return strings.Contains(s, mcpConfigPlaceholder) }) {
|
||||
t.Fatalf("adapter %+v: capture tools could never reach a Claude Code stage", a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessStagesRunEveryStageOnTheTasksHarness(t *testing.T) {
|
||||
inner := &namedRunner{}
|
||||
r := HarnessStages{Runner: inner, Spec: HarnessSpec{Adapter: AdapterClaudeCode}}
|
||||
if _, err := r.Run(t.Context(), "", Request{Stage: "review"}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if inner.got.Harness == nil || inner.got.Harness.Adapter != AdapterClaudeCode {
|
||||
t.Fatalf("stage ran with harness %+v, want the task's", inner.got.Harness)
|
||||
}
|
||||
}
|
||||
|
||||
type namedRunner struct{ got Request }
|
||||
|
||||
func (r *namedRunner) Run(_ context.Context, _ string, req Request, _ ToolCallReporter) (Result, error) {
|
||||
r.got = req
|
||||
return Result{}, nil
|
||||
}
|
||||
|
||||
// assertChildDead checks the harness's own child process did not outlive
|
||||
// it: killing only the CLI would leave its subprocesses running.
|
||||
func assertChildDead(t *testing.T, pidFile string) {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(pidFile)
|
||||
if err != nil {
|
||||
t.Fatalf("the harness recorded no child: %v", err)
|
||||
}
|
||||
pid, err := strconv.Atoi(strings.TrimSpace(string(raw)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for syscall.Kill(pid, 0) == nil {
|
||||
if time.Now().After(deadline) {
|
||||
_ = syscall.Kill(pid, syscall.SIGKILL)
|
||||
t.Fatalf("harness child %d outlived the step", pid)
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessNeverInheritsTheWorkerEnvironment(t *testing.T) {
|
||||
f := newFixture(t, "env")
|
||||
// The fake's own variables are in the worker's environment too, so an
|
||||
// inherited environment would run the fake fully and expose the leak.
|
||||
t.Setenv("FAKE_SCENARIO", "env")
|
||||
t.Setenv("FAKE_LOG", f.log)
|
||||
t.Setenv("STATE_STORE_TOKEN", "worker-secret")
|
||||
t.Setenv("NATS_TOKEN", "worker-secret")
|
||||
f.req.Harness.Env = []string{
|
||||
"PATH=" + os.Getenv("PATH"),
|
||||
"FAKE_SCENARIO=env", "FAKE_LOG=" + f.log,
|
||||
"HTTPS_PROXY=http://archie:token@archie-egress:3128",
|
||||
}
|
||||
res, _ := f.run(t, t.Context())
|
||||
if res.Status != StatusPassed {
|
||||
t.Fatalf("status %q (%s)", res.Status, res.Detail)
|
||||
}
|
||||
raw, err := os.ReadFile(f.log + ".env")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := string(raw)
|
||||
if strings.Contains(env, "worker-secret") {
|
||||
t.Fatalf("the harness saw the worker's credentials:\n%s", env)
|
||||
}
|
||||
if !strings.Contains(env, "HTTPS_PROXY=http://archie:token@archie-egress:3128") {
|
||||
t.Fatalf("the harness did not get its declared environment:\n%s", env)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessUser(t *testing.T) {
|
||||
tests := []struct {
|
||||
user string
|
||||
uid uint32
|
||||
gid uint32
|
||||
wantErr bool
|
||||
}{
|
||||
{"1000", 1000, 1000, false},
|
||||
{"1000:1001", 1000, 1001, false},
|
||||
{"0", 0, 0, true},
|
||||
{"root", 0, 0, true},
|
||||
{"not-a-user-anywhere", 0, 0, true},
|
||||
{"2147483648", 0, 0, true},
|
||||
{"1000:2147483648", 0, 0, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
cred, err := harnessCredential(tt.user)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("harnessCredential(%q) = %+v, want an error", tt.user, cred)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil || cred.Uid != tt.uid || cred.Gid != tt.gid {
|
||||
t.Errorf("harnessCredential(%q) = %+v, %v; want %d:%d", tt.user, cred, err, tt.uid, tt.gid)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,392 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/agentloop"
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
func TestPluginToolSetExecutesBundledPlugin(t *testing.T) {
|
||||
set, err := pluginToolSet(Request{Plugins: []PluginSpec{{Name: "echo", Src: "package main\nfunc Run(input string) string { return input + \"!\" }"}}}, t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tool := set["echo"]
|
||||
got, err := tool.Execute(context.Background(), `{"input":"hello"}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "hello!" {
|
||||
t.Errorf("plugin output = %q, want hello!", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginToolSetExcludedFromConstrainedStages(t *testing.T) {
|
||||
requests := []Request{
|
||||
{ReadOnly: true, Plugins: []PluginSpec{{Name: "unsafe"}}},
|
||||
{Protection: Protection{Suffixes: []string{"_test.go"}}, Plugins: []PluginSpec{{Name: "unsafe"}}},
|
||||
{Gate: Gate{Commands: []Command{{Name: "test"}}}, Plugins: []PluginSpec{{Name: "unsafe"}}},
|
||||
}
|
||||
for _, request := range requests {
|
||||
set, err := pluginToolSet(request, t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(set) != 0 {
|
||||
t.Error("plugin tool exposed in constrained stage")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginToolSetRejectsAgentLoopCollisions(t *testing.T) {
|
||||
for _, name := range []string{"write", "finish"} {
|
||||
_, err := pluginToolSet(Request{Plugins: []PluginSpec{{Name: name}}}, t.TempDir())
|
||||
if err == nil {
|
||||
t.Errorf("plugin %q did not conflict with agent-loop tool", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoopRunnerMapsRequestAndCapturesOutput(t *testing.T) {
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
run: func(ctx context.Context, cfg agentloop.Config) (agentloop.Result, error) {
|
||||
if cfg.WorkDir != "/workspace" || cfg.ModelRef != "provider/model" || cfg.Mission != "mission" {
|
||||
t.Fatalf("unexpected config: workdir=%q model=%q mission=%q", cfg.WorkDir, cfg.ModelRef, cfg.Mission)
|
||||
}
|
||||
if cfg.ProtectPaths == nil || !cfg.ProtectPaths("nested/file_test.go") || !cfg.ProtectPaths("view_templ.go") {
|
||||
t.Fatal("declarative path protection was not applied")
|
||||
}
|
||||
if cfg.Completion != agentloop.CompletionForceFinish {
|
||||
t.Fatalf("Completion = %q, want forced finish recovery", cfg.Completion)
|
||||
}
|
||||
if !cfg.Compact.Enabled || cfg.Compact.ContextWindow <= 0 {
|
||||
t.Fatalf("Compact = %#v, want adaptive non-blocking history compaction", cfg.Compact)
|
||||
}
|
||||
if !strings.Contains(cfg.ExtraRules, "Confine discovery to /workspace") ||
|
||||
!strings.Contains(cfg.ExtraRules, "unless the mission explicitly requests") {
|
||||
t.Fatalf("project discovery rule missing from ExtraRules: %q", cfg.ExtraRules)
|
||||
}
|
||||
if err := cfg.Notes.Append(ctx, "verified note"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := cfg.Extra["decide"].Execute(ctx, `{"fit":true}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return agentloop.Result{
|
||||
Status: agentloop.StatusPassed, Summary: "done", TokensUsed: 10,
|
||||
Usage: chat.Usage{PromptTokens: 8, CompletionTokens: 2, TotalTokens: 10, CachedTokens: 6},
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
req := Request{
|
||||
Version: ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "assess", Model: "provider/model", ContextWindow: 120_000, Mission: "mission",
|
||||
Protection: Protection{Suffixes: []string{"_templ.go"}, Globs: []string{"*_test.go"}},
|
||||
CaptureTools: []CaptureTool{{Name: "decide", Parameters: json.RawMessage(`{"type":"object"}`), MaxCalls: 1}},
|
||||
}
|
||||
got, err := runner.Run(context.Background(), "/workspace", req, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Status != StatusPassed || got.Summary != "done" || got.TokensUsed != 10 {
|
||||
t.Fatalf("unexpected result: %#v", got)
|
||||
}
|
||||
if got.Usage.PromptTokens != 8 || got.Usage.CompletionTokens != 2 || got.Usage.CachedTokens != 6 {
|
||||
t.Fatalf("usage = %#v, want provider breakdown", got.Usage)
|
||||
}
|
||||
if len(got.AppendedNotes) != 1 || got.AppendedNotes[0] != "verified note" {
|
||||
t.Fatalf("appended notes = %v", got.AppendedNotes)
|
||||
}
|
||||
if len(got.Captures["decide"]) != 1 || string(got.Captures["decide"][0]) != `{"fit":true}` {
|
||||
t.Fatalf("captures = %v", got.Captures)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoopRunnerIncludesAvailableCentralTools(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
if err := registry.Register(tools.ToolEntry{
|
||||
Name: "memory_search",
|
||||
Handler: func(context.Context, map[string]any) (any, error) {
|
||||
return "found", nil
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
tools: registry,
|
||||
run: func(ctx context.Context, cfg agentloop.Config) (agentloop.Result, error) {
|
||||
tool, ok := cfg.Extra["memory_search"]
|
||||
if !ok {
|
||||
t.Fatal("central tool is not present in agentloop Config.Extra")
|
||||
}
|
||||
got, err := tool.Execute(ctx, `{}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != `"found"` {
|
||||
t.Fatalf("tool output = %q, want JSON string", got)
|
||||
}
|
||||
return agentloop.Result{Status: agentloop.StatusPassed}, nil
|
||||
},
|
||||
}
|
||||
_, err := runner.Run(context.Background(), "/workspace", Request{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: 1,
|
||||
Attempt: 1,
|
||||
Stage: "build",
|
||||
Model: "provider/model",
|
||||
Mission: "mission",
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoopRunnerRejectsInvalidCentralToolSchemas(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
if err := registry.Register(tools.ToolEntry{
|
||||
Name: "invalid",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
DynamicSchemaOverrides: func(tools.JSONSchema) tools.JSONSchema {
|
||||
panic("schema panic")
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
tools: registry,
|
||||
run: func(context.Context, agentloop.Config) (agentloop.Result, error) {
|
||||
t.Fatal("agent loop ran with an invalid central tool schema")
|
||||
return agentloop.Result{}, nil
|
||||
},
|
||||
}
|
||||
_, err := runner.Run(context.Background(), "/workspace", Request{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: 1,
|
||||
Attempt: 1,
|
||||
Stage: "build",
|
||||
Model: "provider/model",
|
||||
Mission: "mission",
|
||||
}, nil)
|
||||
if err == nil {
|
||||
t.Fatal("Run() error = nil, want invalid central tool schema error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtectionMatcherAppliesBasenameAndPathGlobs(t *testing.T) {
|
||||
match := protectionMatcher(Protection{Globs: []string{"*_test.go", "tests/*.rs"}}, false)
|
||||
tests := []struct {
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{path: "main_test.go", want: true},
|
||||
{path: "internal/foo/bar_test.go", want: true},
|
||||
{path: "tests/widget.rs", want: true},
|
||||
{path: "src/widget.rs", want: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := match(tt.path); got != tt.want {
|
||||
t.Errorf("match(%q) = %v, want %v", tt.path, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaptureToolRejectsInvalidDataBeforeRecording(t *testing.T) {
|
||||
captures := make(map[string][]json.RawMessage)
|
||||
tool := captureToolSet([]CaptureTool{{
|
||||
Name: "decide", RequiredFields: []string{"fit", "reasons"}, NonEmptyStrings: []string{"reasons"},
|
||||
BooleanFields: []string{"fit"},
|
||||
}}, captures)["decide"]
|
||||
output, err := tool.Execute(context.Background(), `{"fit":true,"reasons":""}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(captures["decide"]) != 0 || output != "decide rejected: reasons must be a non-empty string" {
|
||||
t.Fatalf("invalid capture output=%q captures=%v", output, captures)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaptureToolRejectsWrongFieldTypeBeforeRecording(t *testing.T) {
|
||||
captures := make(map[string][]json.RawMessage)
|
||||
tool := captureToolSet([]CaptureTool{{Name: "decide", BooleanFields: []string{"fit"}}}, captures)["decide"]
|
||||
output, err := tool.Execute(context.Background(), `{"fit":"true"}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(captures["decide"]) != 0 || output != "decide rejected: fit must be a boolean" {
|
||||
t.Fatalf("invalid capture output=%q captures=%v", output, captures)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptToolRunsAGoScriptAndReturnsItsOutput(t *testing.T) {
|
||||
workspace := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(workspace, "scripts"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := "package main\n\nimport \"fmt\"\n\nfunc main() { fmt.Println(\"hi from a script\") }\n"
|
||||
if err := os.WriteFile(filepath.Join(workspace, "scripts", "hello.go"), []byte(src), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tool := scriptToolSet(workspace)["run_go_script"]
|
||||
out, err := tool.Execute(context.Background(), `{"path":"scripts/hello.go"}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != "hi from a script\n" {
|
||||
t.Fatalf("run_go_script output = %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptToolRejectsPathsEscapingTheWorkspace(t *testing.T) {
|
||||
workspace := t.TempDir()
|
||||
tool := scriptToolSet(workspace)["run_go_script"]
|
||||
out, err := tool.Execute(context.Background(), `{"path":"../outside.go"}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != "run_go_script rejected: path escapes the workspace" {
|
||||
t.Fatalf("run_go_script output = %q, want a workspace-escape rejection", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptToolRejectsMissingPath(t *testing.T) {
|
||||
tool := scriptToolSet(t.TempDir())["run_go_script"]
|
||||
out, err := tool.Execute(context.Background(), `{}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != "run_go_script rejected: arguments must be a JSON object with a non-empty path field" {
|
||||
t.Fatalf("run_go_script output = %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeToolSetsLaterWinsOnCollision(t *testing.T) {
|
||||
first := scriptToolSet(t.TempDir())
|
||||
captures := make(map[string][]json.RawMessage)
|
||||
second := captureToolSet([]CaptureTool{{Name: "run_go_script"}}, captures)
|
||||
merged := mergeToolSets(first, second)
|
||||
if len(merged) != 1 {
|
||||
t.Fatalf("merged = %v, want exactly one tool", merged)
|
||||
}
|
||||
if _, err := merged["run_go_script"].Execute(context.Background(), `{}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(captures["run_go_script"]) != 1 {
|
||||
t.Fatalf("expected the capture tool (second set) to win, captures = %v", captures)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoopRunnerPreservesCallerCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
run: func(context.Context, agentloop.Config) (agentloop.Result, error) {
|
||||
return agentloop.Result{Status: agentloop.StatusParked, StopReason: agentloop.StopTimedOut}, nil
|
||||
},
|
||||
}
|
||||
_, err := runner.Run(ctx, "/workspace", Request{
|
||||
Version: ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "build", Model: "provider/model", Mission: "mission",
|
||||
}, nil)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("Run error = %v, want context.Canceled", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScriptToolHonorsContextCancellation proves that the run_go_script
|
||||
// tool handler discards the incoming context (the handler is declared as
|
||||
// func(_ context.Context, input string) and calls skillscript.Run without
|
||||
// forwarding ctx). Every other execution path in archie-core respects
|
||||
// context cancellation (LoopRunner checks context.Cause), but a
|
||||
// cancelled/timed-out context
|
||||
// has zero effect on run_go_script — a blocking script wedges the
|
||||
// goroutine permanently.
|
||||
func TestScriptToolHonorsContextCancellation(t *testing.T) {
|
||||
workspace := t.TempDir()
|
||||
src := `package main
|
||||
|
||||
func main() {
|
||||
select {}
|
||||
}
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(workspace, "blocker.go"), []byte(src), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // immediately cancelled — the tool should return promptly
|
||||
|
||||
tool := scriptToolSet(workspace)["run_go_script"]
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
// The result is irrelevant here -- this goroutine only exists to
|
||||
// prove Execute returns promptly once ctx is cancelled.
|
||||
_, _ = tool.Execute(ctx, `{"path":"blocker.go"}`)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
// After the fix, the tool returns quickly because the cancelled
|
||||
// context propagates through to skillscript.Run, which selects
|
||||
// on ctx.Done() and abandons the blocking EvalPath goroutine.
|
||||
t.Log("run_go_script returned promptly (context was honored)")
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Error("run_go_script ignores context cancellation: " +
|
||||
"the handler discards its context parameter (declared as _) " +
|
||||
"and calls skillscript.Run without forwarding it (issue #45)")
|
||||
}
|
||||
}
|
||||
|
||||
// A profile's allowlist removes the tools archie adds that it does not name,
|
||||
// but never a stage's capture tools, which carry its structured result.
|
||||
func TestLoopRunnerAppliesTheProfileAllowlist(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
for _, name := range []string{"whois", "memory_search"} {
|
||||
if err := registry.Register(tools.ToolEntry{
|
||||
Name: name,
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return "", nil },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var extra []string
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
tools: registry,
|
||||
AllowTools: []string{"whois"},
|
||||
run: func(_ context.Context, cfg agentloop.Config) (agentloop.Result, error) {
|
||||
for name := range cfg.Extra {
|
||||
extra = append(extra, name)
|
||||
}
|
||||
return agentloop.Result{Status: agentloop.StatusPassed}, nil
|
||||
},
|
||||
}
|
||||
_, err := runner.Run(context.Background(), t.TempDir(), Request{
|
||||
Version: ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "build", Model: "provider/model", Mission: "mission",
|
||||
CaptureTools: []CaptureTool{{Name: "report", Description: "d", Parameters: json.RawMessage(`{"type":"object"}`)}},
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
slices.Sort(extra)
|
||||
if !slices.Equal(extra, []string{"report", "whois"}) {
|
||||
t.Fatalf("agent tools = %v, want the allowed whois and the capture tool", extra)
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestProtocolRoundTrip(t *testing.T) {
|
||||
want := Request{
|
||||
Version: ProtocolVersion, TaskID: 42, Attempt: 3, Stage: "fix", Model: "openai/gpt-5",
|
||||
Mission: "fix it", Budget: Budget{MaxSteps: 12, WallClock: 2 * time.Minute},
|
||||
Gate: Gate{Commands: []Command{{Name: "go", Argv: []string{"go", "test", "./..."}}}, MaxConsecutiveFailures: 2},
|
||||
Protection: Protection{Suffixes: []string{"_templ.go"}, Globs: []string{"*_test.go"}},
|
||||
CaptureTools: []CaptureTool{{Name: "decide", Parameters: json.RawMessage(`{"type":"object"}`), MaxCalls: 1}},
|
||||
}
|
||||
b, err := json.Marshal(want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got Request
|
||||
if err := json.Unmarshal(b, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("round trip mismatch\ngot: %#v\nwant: %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestRejectsUnknownProtocol(t *testing.T) {
|
||||
req := Request{Version: ProtocolVersion + 1, TaskID: 1, Stage: "plan", Model: "provider/model"}
|
||||
if err := req.Validate(); err == nil {
|
||||
t.Fatal("Validate accepted an unsupported protocol version")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResultRejectsMismatchedIdentity(t *testing.T) {
|
||||
req := Request{Version: ProtocolVersion, TaskID: 1, Attempt: 2, Stage: "build", Model: "provider/model"}
|
||||
result := Result{
|
||||
Version: ProtocolVersion, TaskID: 1, Attempt: 3, Stage: "build", Status: StatusPassed,
|
||||
}
|
||||
if err := result.ValidateFor(req); err == nil {
|
||||
t.Fatal("ValidateFor accepted a result from another attempt")
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
func TestProvidersFromConfig(t *testing.T) {
|
||||
in := map[string]config.Provider{
|
||||
"anthropic": {Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY", BaseURL: "https://api.anthropic.com"},
|
||||
}
|
||||
want := map[string]Provider{
|
||||
"anthropic": {Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY", BaseURL: "https://api.anthropic.com"},
|
||||
}
|
||||
if got := ProvidersFromConfig(in); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ProvidersFromConfig() = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvidersFromConfigEmpty(t *testing.T) {
|
||||
if got := ProvidersFromConfig(nil); len(got) != 0 {
|
||||
t.Fatalf("ProvidersFromConfig(nil) = %#v, want empty", got)
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
|
||||
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
func TestSamplingMessagesMapsRolesAndRejectsUnsupportedInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
request protocol.SamplingRequest
|
||||
want []chat.Message
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "user and assistant text map to chat roles",
|
||||
request: protocol.SamplingRequest{Messages: []protocol.SamplingMessage{
|
||||
{Role: "user", Content: protocol.SamplingContent{Type: "text", Text: "2+2?"}},
|
||||
{Role: "assistant", Content: protocol.SamplingContent{Type: "", Text: "4"}},
|
||||
}},
|
||||
want: []chat.Message{
|
||||
{Role: chat.RoleUser, Content: "2+2?"},
|
||||
{Role: chat.RoleAssistant, Content: "4"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no messages is a refusal",
|
||||
request: protocol.SamplingRequest{},
|
||||
wantErr: "the request has no messages",
|
||||
},
|
||||
{
|
||||
name: "non-text content is a refusal",
|
||||
request: protocol.SamplingRequest{Messages: []protocol.SamplingMessage{
|
||||
{Role: "user", Content: protocol.SamplingContent{Type: "image"}},
|
||||
}},
|
||||
wantErr: "unsupported content type",
|
||||
},
|
||||
{
|
||||
name: "unknown role is a refusal",
|
||||
request: protocol.SamplingRequest{Messages: []protocol.SamplingMessage{
|
||||
{Role: "system", Content: protocol.SamplingContent{Type: "text", Text: "hi"}},
|
||||
}},
|
||||
wantErr: "unsupported role",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := SamplingMessages(tc.request)
|
||||
if tc.wantErr != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
|
||||
t.Fatalf("SamplingMessages error = %v, want it to mention %q", err, tc.wantErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("SamplingMessages error = %v, want nil", err)
|
||||
}
|
||||
if len(got) != len(tc.want) {
|
||||
t.Fatalf("SamplingMessages returned %d messages, want %d", len(got), len(tc.want))
|
||||
}
|
||||
for i := range got {
|
||||
if got[i].Role != tc.want[i].Role || got[i].Content != tc.want[i].Content {
|
||||
t.Errorf("message %d = %+v, want %+v", i, got[i], tc.want[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSamplingMaxTokens(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
requested int
|
||||
ceiling int
|
||||
reasoning bool
|
||||
wantTokens int
|
||||
}{
|
||||
{name: "requested bound is honoured", requested: 64, ceiling: 1024, wantTokens: 64},
|
||||
{name: "absent request falls back to the default", requested: 0, ceiling: 8192, wantTokens: DefaultSamplingMaxTokens},
|
||||
{name: "request is capped by the model ceiling", requested: 8192, ceiling: 1024, wantTokens: 1024},
|
||||
{name: "unknown ceiling keeps the request", requested: 8192, ceiling: 0, wantTokens: 8192},
|
||||
{name: "reasoning model gets no bound", requested: 64, ceiling: 1024, reasoning: true, wantTokens: 0},
|
||||
{name: "reasoning model with absent request gets no bound", requested: 0, ceiling: 0, reasoning: true, wantTokens: 0},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := SamplingMaxTokens(tc.requested, tc.ceiling, tc.reasoning); got != tc.wantTokens {
|
||||
t.Fatalf("SamplingMaxTokens(%d, %d, %v) = %d, want %d", tc.requested, tc.ceiling, tc.reasoning, got, tc.wantTokens)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestTaskIDFromSystemSubject pins the round trip with SubjectForSystem, and
|
||||
// that a subject which merely resembles one (wrong kind suffix, malformed
|
||||
// ID) is rejected rather than parsed into a wrong task ID -- a daemon
|
||||
// demuxing a wildcard subscription onto SubjectSystemWildcard has no other
|
||||
// check on what actually arrives.
|
||||
func TestTaskIDFromSystemSubject(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
subject string
|
||||
wantID int64
|
||||
wantOK bool
|
||||
}{
|
||||
{"round trip with SubjectForSystem", SubjectForSystem(42), 42, true},
|
||||
{"round trip with a large ID", SubjectForSystem(9_223_372_036), 9_223_372_036, true},
|
||||
{"wrong kind suffix is rejected", "archie.agent.42.response", 0, false},
|
||||
{"missing prefix is rejected", "some.other.42.system", 0, false},
|
||||
{"non-numeric ID is rejected", "archie.agent.abc.system", 0, false},
|
||||
{"zero ID is rejected", "archie.agent.0.system", 0, false},
|
||||
{"negative ID is rejected", "archie.agent.-1.system", 0, false},
|
||||
{"empty subject is rejected", "", 0, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotID, gotOK := TaskIDFromSystemSubject(tt.subject)
|
||||
if gotID != tt.wantID || gotOK != tt.wantOK {
|
||||
t.Errorf("TaskIDFromSystemSubject(%q) = (%d, %v), want (%d, %v)",
|
||||
tt.subject, gotID, gotOK, tt.wantID, tt.wantOK)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubjectSystemWildcardMatchesEverySystemSubject(t *testing.T) {
|
||||
tests := []int64{1, 42, 9_223_372_036}
|
||||
for _, taskID := range tests {
|
||||
if id, ok := TaskIDFromSystemSubject(SubjectForSystem(taskID)); !ok || id != taskID {
|
||||
t.Errorf("SubjectForSystem(%d) did not round-trip through TaskIDFromSystemSubject: got (%d, %v)", taskID, id, ok)
|
||||
}
|
||||
}
|
||||
if SubjectSystemWildcard != "archie.agent.*.system" {
|
||||
t.Errorf("SubjectSystemWildcard = %q, want the single-token wildcard NATS matches against subjectForTask's shape",
|
||||
SubjectSystemWildcard)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskIDFromEventsSubject mirrors TestTaskIDFromSystemSubject: a daemon
|
||||
// demuxing SubjectEventsWildcard has no other check on what arrives, so a
|
||||
// subject that merely resembles one must be rejected, not parsed into a
|
||||
// wrong task ID.
|
||||
func TestTaskIDFromEventsSubject(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
subject string
|
||||
wantID int64
|
||||
wantOK bool
|
||||
}{
|
||||
{"round trip with SubjectForEvents", SubjectForEvents(42), 42, true},
|
||||
{"round trip with a large ID", SubjectForEvents(9_223_372_036), 9_223_372_036, true},
|
||||
{"wrong kind suffix is rejected", "archie.agent.42.system", 0, false},
|
||||
{"missing prefix is rejected", "some.other.42.events", 0, false},
|
||||
{"non-numeric ID is rejected", "archie.agent.abc.events", 0, false},
|
||||
{"zero ID is rejected", "archie.agent.0.events", 0, false},
|
||||
{"negative ID is rejected", "archie.agent.-1.events", 0, false},
|
||||
{"empty subject is rejected", "", 0, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotID, gotOK := TaskIDFromEventsSubject(tt.subject)
|
||||
if gotID != tt.wantID || gotOK != tt.wantOK {
|
||||
t.Errorf("TaskIDFromEventsSubject(%q) = (%d, %v), want (%d, %v)",
|
||||
tt.subject, gotID, gotOK, tt.wantID, tt.wantOK)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubjectEventsWildcardMatchesEveryEventsSubject(t *testing.T) {
|
||||
tests := []int64{1, 42, 9_223_372_036}
|
||||
for _, taskID := range tests {
|
||||
if id, ok := TaskIDFromEventsSubject(SubjectForEvents(taskID)); !ok || id != taskID {
|
||||
t.Errorf("SubjectForEvents(%d) did not round-trip through TaskIDFromEventsSubject: got (%d, %v)", taskID, id, ok)
|
||||
}
|
||||
}
|
||||
if SubjectEventsWildcard != "archie.agent.*.events" {
|
||||
t.Errorf("SubjectEventsWildcard = %q, want the single-token wildcard NATS matches against subjectForTask's shape",
|
||||
SubjectEventsWildcard)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventsAndSystemSubjectsDoNotCrossMatch guards the reason a dedicated
|
||||
// events subject exists instead of reusing SubjectForSystem: the daemon
|
||||
// already owns one payload format per subject (logging.Entry on .system),
|
||||
// and a task event decoded as a log entry (or vice versa) would silently
|
||||
// corrupt whichever table receives it.
|
||||
func TestEventsAndSystemSubjectsDoNotCrossMatch(t *testing.T) {
|
||||
if _, ok := TaskIDFromSystemSubject(SubjectForEvents(7)); ok {
|
||||
t.Error("TaskIDFromSystemSubject accepted an events subject")
|
||||
}
|
||||
if _, ok := TaskIDFromEventsSubject(SubjectForSystem(7)); ok {
|
||||
t.Error("TaskIDFromEventsSubject accepted a system subject")
|
||||
}
|
||||
}
|
||||
@@ -1,238 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
|
||||
// fakePublisher is a LogPublisher that records every call and can be told to
|
||||
// fail, so tests can assert both the happy path and the "must never block or
|
||||
// fail the run" requirement.
|
||||
type fakePublisher struct {
|
||||
mu sync.Mutex
|
||||
subjects []string
|
||||
payloads [][]byte
|
||||
failWith error
|
||||
}
|
||||
|
||||
func (f *fakePublisher) Publish(subject string, data []byte) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.subjects = append(f.subjects, subject)
|
||||
f.payloads = append(f.payloads, append([]byte(nil), data...))
|
||||
return f.failWith
|
||||
}
|
||||
|
||||
func (f *fakePublisher) calls() int {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return len(f.payloads)
|
||||
}
|
||||
|
||||
// capturingHandler is a minimal slog.Handler that records every record it
|
||||
// receives, standing in for the agentworker's wrapped process stderr handler.
|
||||
type capturingHandler struct {
|
||||
mu sync.Mutex
|
||||
records []slog.Record
|
||||
}
|
||||
|
||||
func (h *capturingHandler) Enabled(context.Context, slog.Level) bool { return true }
|
||||
|
||||
func (h *capturingHandler) Handle(_ context.Context, r slog.Record) error {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.records = append(h.records, r)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *capturingHandler) WithAttrs([]slog.Attr) slog.Handler { return h }
|
||||
func (h *capturingHandler) WithGroup(string) slog.Handler { return h }
|
||||
|
||||
func (h *capturingHandler) messages() []string {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
out := make([]string, len(h.records))
|
||||
for i, r := range h.records {
|
||||
out[i] = r.Message
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestSystemLogHandlerPublishesToTheTasksSystemSubject(t *testing.T) {
|
||||
pub := &fakePublisher{}
|
||||
next := &capturingHandler{}
|
||||
handler := NewSystemLogHandler(next, pub, 42)
|
||||
logger := slog.New(handler)
|
||||
|
||||
logger.Warn("gate failed", "component", "gate")
|
||||
|
||||
if got, want := pub.calls(), 1; got != want {
|
||||
t.Fatalf("publish calls = %d, want %d", got, want)
|
||||
}
|
||||
if got, want := pub.subjects[0], SubjectForSystem(42); got != want {
|
||||
t.Errorf("subject = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
var entry logging.Entry
|
||||
if err := json.Unmarshal(pub.payloads[0], &entry); err != nil {
|
||||
t.Fatalf("payload is not a logging.Entry: %v", err)
|
||||
}
|
||||
if entry.Message != "gate failed" {
|
||||
t.Errorf("Message = %q, want %q", entry.Message, "gate failed")
|
||||
}
|
||||
if entry.Level != "WARN" {
|
||||
t.Errorf("Level = %q, want WARN", entry.Level)
|
||||
}
|
||||
if entry.Fields["component"] != "gate" {
|
||||
t.Errorf("Fields[component] = %v, want gate", entry.Fields["component"])
|
||||
}
|
||||
// The wire entry must carry the record's own event time. The daemon files
|
||||
// a task's records under the time the event happened (see
|
||||
// logging.TaskRegistry.Write), so a producer that dropped or re-stamped it
|
||||
// here would collapse a run's timing before it ever left the container.
|
||||
next.mu.Lock()
|
||||
wantTime := next.records[0].Time
|
||||
next.mu.Unlock()
|
||||
if entry.Time.IsZero() {
|
||||
t.Error("Entry.Time is the zero time, want the record's own event time")
|
||||
} else if !entry.Time.Equal(wantTime) {
|
||||
t.Errorf("Entry.Time = %s, want the record's event time %s", entry.Time, wantTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemLogHandlerAlwaysTeesToNextRegardlessOfPublishOutcome(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
failWith error
|
||||
}{
|
||||
{name: "publish succeeds"},
|
||||
{name: "publish fails", failWith: errors.New("nats: no responders")},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
pub := &fakePublisher{failWith: tt.failWith}
|
||||
next := &capturingHandler{}
|
||||
logger := slog.New(NewSystemLogHandler(next, pub, 1))
|
||||
|
||||
logger.Info("still visible on stderr")
|
||||
|
||||
// A failed publish also emits its own one-time warning onto
|
||||
// next (see TestSystemLogHandlerLogsPublishFailureOnceNotPerRecord),
|
||||
// so check membership, not position.
|
||||
found := false
|
||||
for _, msg := range next.messages() {
|
||||
found = found || msg == "still visible on stderr"
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("next handler messages = %v, want %q present regardless of publish outcome",
|
||||
next.messages(), "still visible on stderr")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSystemLogHandlerNeverFailsTheRunOnPublishError guards the explicit
|
||||
// design requirement: log shipping must never block or fail the run it is
|
||||
// reporting on. A slog.Logger call that returned an error here would
|
||||
// propagate nowhere useful (slog callers never check it), but Handle itself
|
||||
// must not return a publish error either, or a handler chain that does check
|
||||
// (as ours might grow to) would start treating a dead NATS connection as a
|
||||
// logging failure worth surfacing to the caller.
|
||||
func TestSystemLogHandlerNeverFailsTheRunOnPublishError(t *testing.T) {
|
||||
pub := &fakePublisher{failWith: errors.New("nats: no responders")}
|
||||
handler := NewSystemLogHandler(&capturingHandler{}, pub, 1)
|
||||
|
||||
rec := slog.NewRecord(time.Now(), slog.LevelInfo, "msg", 0)
|
||||
if err := handler.Handle(context.Background(), rec); err != nil {
|
||||
t.Errorf("Handle() = %v, want nil even when the publisher errors", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSystemLogHandlerLogsPublishFailureOnceNotPerRecord guards against a
|
||||
// dead NATS connection turning every subsequent log line into a second
|
||||
// failure notice on stderr, which would drown out the very output the
|
||||
// operator is trying to read.
|
||||
func TestSystemLogHandlerLogsPublishFailureOnceNotPerRecord(t *testing.T) {
|
||||
pub := &fakePublisher{failWith: errors.New("nats: no responders")}
|
||||
next := &capturingHandler{}
|
||||
logger := slog.New(NewSystemLogHandler(next, pub, 1))
|
||||
|
||||
for range 5 {
|
||||
logger.Info("line")
|
||||
}
|
||||
|
||||
failureNotices := 0
|
||||
for _, msg := range next.messages() {
|
||||
if msg != "line" {
|
||||
failureNotices++
|
||||
}
|
||||
}
|
||||
if failureNotices != 1 {
|
||||
t.Errorf("failure notices on next = %d, want exactly 1 across 5 failed publishes", failureNotices)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSystemLogHandlerMarshalFailureWarnsOnceAndNeverPublishes guards the
|
||||
// other delivery failure mode alongside a rejected Publish: an attr value
|
||||
// json.Marshal can't encode (e.g. a channel) must not silently vanish with
|
||||
// zero signal, and must not reach the publisher with malformed data either.
|
||||
func TestSystemLogHandlerMarshalFailureWarnsOnceAndNeverPublishes(t *testing.T) {
|
||||
pub := &fakePublisher{}
|
||||
next := &capturingHandler{}
|
||||
logger := slog.New(NewSystemLogHandler(next, pub, 1))
|
||||
|
||||
logger.Info("unmarshalable", "bad", make(chan int))
|
||||
|
||||
if pub.calls() != 0 {
|
||||
t.Errorf("publish calls = %d, want 0: malformed data must never reach the publisher", pub.calls())
|
||||
}
|
||||
|
||||
failureNotices := 0
|
||||
for _, msg := range next.messages() {
|
||||
if msg != "unmarshalable" {
|
||||
failureNotices++
|
||||
}
|
||||
}
|
||||
if failureNotices != 1 {
|
||||
t.Errorf("failure notices on next = %d, want exactly 1", failureNotices)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemLogHandlerWithAttrsAndGroupsReachThePublishedEntry(t *testing.T) {
|
||||
pub := &fakePublisher{}
|
||||
next := &capturingHandler{}
|
||||
handler := NewSystemLogHandler(next, pub, 7)
|
||||
|
||||
logger := slog.New(handler).With("task", "7").WithGroup("gate")
|
||||
logger.Warn("failed", "cmd", "go build ./...")
|
||||
|
||||
if pub.calls() != 1 {
|
||||
t.Fatalf("publish calls = %d, want 1", pub.calls())
|
||||
}
|
||||
var entry logging.Entry
|
||||
if err := json.Unmarshal(pub.payloads[0], &entry); err != nil {
|
||||
t.Fatalf("payload is not a logging.Entry: %v", err)
|
||||
}
|
||||
if entry.Fields["task"] != "7" {
|
||||
t.Errorf("Fields[task] = %v, want 7 (WithAttrs before WithGroup keeps its own key ungrouped)", entry.Fields["task"])
|
||||
}
|
||||
if entry.Fields["gate.cmd"] != "go build ./..." {
|
||||
t.Errorf("Fields[gate.cmd] = %v, want %q", entry.Fields["gate.cmd"], "go build ./...")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemLogHandlerEnabledDelegatesToNext(t *testing.T) {
|
||||
next := &capturingHandler{}
|
||||
handler := NewSystemLogHandler(next, &fakePublisher{}, 1)
|
||||
if !handler.Enabled(context.Background(), slog.LevelInfo) {
|
||||
t.Error("Enabled() = false, want true (next always returns true)")
|
||||
}
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
var errPublishBoom = errors.New("publish boom")
|
||||
|
||||
// eventPublisherStub is an EventPublisher that records every call and can be
|
||||
// told to fail, mirroring fakePublisher in systemlog_test.go for the same
|
||||
// reason: assert both the happy path and "one failure must not stop the
|
||||
// rest."
|
||||
type eventPublisherStub struct {
|
||||
subjects []string
|
||||
payloads [][]byte
|
||||
failWith error
|
||||
}
|
||||
|
||||
func (p *eventPublisherStub) Publish(subject string, data []byte) error {
|
||||
p.subjects = append(p.subjects, subject)
|
||||
p.payloads = append(p.payloads, append([]byte(nil), data...))
|
||||
return p.failWith
|
||||
}
|
||||
|
||||
// TestForwardTaskEventsPublishesEachEventOnItsTaskSubject is the core
|
||||
// contract: everything published on the bus while ForwardTaskEvents is
|
||||
// running arrives at pub, JSON-decodable back into the same Event, on
|
||||
// SubjectForEvents(taskID) -- regardless of which task's events happen to be
|
||||
// flowing, since taskID is a ForwardTaskEvents parameter, not read from the
|
||||
// event itself.
|
||||
func TestForwardTaskEventsPublishesEachEventOnItsTaskSubject(t *testing.T) {
|
||||
bus := events.NewBus()
|
||||
sub := bus.Subscribe(8)
|
||||
pub := &eventPublisherStub{}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
ForwardTaskEvents(sub, pub, 42, slog.New(slog.DiscardHandler))
|
||||
close(done)
|
||||
}()
|
||||
|
||||
want := []events.Event{
|
||||
{Kind: events.KindStageStart, TaskID: 42, Stage: "baseline", Attempt: 1},
|
||||
{Kind: events.KindStageFinish, TaskID: 42, Stage: "baseline", Attempt: 1, Data: map[string]any{"duration_ms": float64(120)}},
|
||||
{Kind: events.KindParked, TaskID: 42, Attempt: 1, Detail: "baseline red"},
|
||||
// R4's document rides this bridge too: a worker captures its own
|
||||
// effective configuration, and the daemon is a different process, so
|
||||
// the payload has to survive the hop or the Config tab reads nothing.
|
||||
// The daemon's own key for it is asserted below by name.
|
||||
{Kind: events.KindConfigCaptured, TaskID: 42, Attempt: 1, Data: map[string]any{
|
||||
"schema": events.ConfigCapturedSchema,
|
||||
"document": map[string]any{"bot_user": "archie"},
|
||||
}},
|
||||
}
|
||||
for _, e := range want {
|
||||
bus.Publish(e)
|
||||
}
|
||||
sub.Close()
|
||||
<-done
|
||||
|
||||
if len(pub.payloads) != len(want) {
|
||||
t.Fatalf("published %d events, want %d", len(pub.payloads), len(want))
|
||||
}
|
||||
wantSubject := SubjectForEvents(42)
|
||||
for i, payload := range pub.payloads {
|
||||
if pub.subjects[i] != wantSubject {
|
||||
t.Errorf("event %d subject = %q, want %q", i, pub.subjects[i], wantSubject)
|
||||
}
|
||||
var got events.Event
|
||||
if err := json.Unmarshal(payload, &got); err != nil {
|
||||
t.Fatalf("event %d undecodable: %v", i, err)
|
||||
}
|
||||
if got.Kind != want[i].Kind || got.TaskID != want[i].TaskID || got.Stage != want[i].Stage || got.Detail != want[i].Detail {
|
||||
t.Errorf("event %d = %+v, want %+v", i, got, want[i])
|
||||
}
|
||||
if got.Attempt != want[i].Attempt {
|
||||
t.Errorf("event %d attempt = %d, want %d (provenance must not be dropped on the hop)", i, got.Attempt, want[i].Attempt)
|
||||
}
|
||||
if got.Kind == events.KindConfigCaptured {
|
||||
if got.Data["schema"] != events.ConfigCapturedSchema {
|
||||
t.Errorf("event %d schema = %v, want %q", i, got.Data["schema"], events.ConfigCapturedSchema)
|
||||
}
|
||||
if _, renamed := got.Data["config"]; renamed {
|
||||
t.Errorf(`event %d crossed under "config"; the producer writes the document under "document"`, i)
|
||||
}
|
||||
doc, ok := got.Data["document"].(map[string]any)
|
||||
if !ok || doc["bot_user"] != "archie" {
|
||||
t.Errorf("event %d document payload = %#v, want the decoded document under its own key", i, got.Data["document"])
|
||||
}
|
||||
}
|
||||
if got.At.IsZero() {
|
||||
t.Errorf("event %d has zero At, want the bus-stamped publish time", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestForwardTaskEventsSurvivesAPublishFailure pins "one missing event must
|
||||
// never abort the ones after it": event shipping must never block or fail
|
||||
// the workflow run it is reporting on.
|
||||
func TestForwardTaskEventsSurvivesAPublishFailure(t *testing.T) {
|
||||
bus := events.NewBus()
|
||||
sub := bus.Subscribe(8)
|
||||
pub := &eventPublisherStub{failWith: errPublishBoom}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
ForwardTaskEvents(sub, pub, 7, slog.New(slog.DiscardHandler))
|
||||
close(done)
|
||||
}()
|
||||
|
||||
bus.Publish(events.Event{Kind: events.KindStageStart, TaskID: 7})
|
||||
bus.Publish(events.Event{Kind: events.KindOutcome, TaskID: 7})
|
||||
sub.Close()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("ForwardTaskEvents did not return after sub.Close(); a publish failure blocked the loop")
|
||||
}
|
||||
|
||||
if len(pub.payloads) != 2 {
|
||||
t.Fatalf("published %d events despite failures, want 2 (both attempted)", len(pub.payloads))
|
||||
}
|
||||
}
|
||||
@@ -1,692 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
aicore "github.com/samcharles93/ai-sdk/core"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
func TestToolSetConvertsRegisteredEntries(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "echo",
|
||||
Description: "echoes its input",
|
||||
Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{"msg": map[string]any{"type": "string"}}},
|
||||
Handler: func(_ context.Context, input map[string]any) (any, error) {
|
||||
return input["msg"], nil
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
set := mustBuildToolSet(t, reg)
|
||||
|
||||
tool, ok := set["echo"]
|
||||
if !ok {
|
||||
t.Fatal("ToolSet did not include the \"echo\" entry")
|
||||
}
|
||||
if tool.Name != "echo" || tool.Description != "echoes its input" {
|
||||
t.Errorf("tool = %+v", tool)
|
||||
}
|
||||
if len(tool.Parameters) == 0 {
|
||||
t.Error("Parameters is empty, want the marshaled schema")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetHandlerRoundTripsJSON(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "add",
|
||||
Handler: func(_ context.Context, input map[string]any) (any, error) {
|
||||
a, _ := input["a"].(float64)
|
||||
b, _ := input["b"].(float64)
|
||||
return a + b, nil
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
set := mustBuildToolSet(t, reg)
|
||||
out, err := set["add"].Execute(context.Background(), `{"a": 2, "b": 3}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got float64
|
||||
if err := json.Unmarshal([]byte(out), &got); err != nil {
|
||||
t.Fatalf("output %q did not decode as JSON: %v", out, err)
|
||||
}
|
||||
if got != 5 {
|
||||
t.Errorf("result = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetHandlerPropagatesHandlerError(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
wantErr := errors.New("boom")
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "fails",
|
||||
Handler: func(context.Context, map[string]any) (any, error) {
|
||||
return nil, wantErr
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
set := mustBuildToolSet(t, reg)
|
||||
_, err := set["fails"].Execute(context.Background(), `{}`)
|
||||
if err == nil {
|
||||
t.Fatal("expected error from failing handler")
|
||||
}
|
||||
}
|
||||
|
||||
// TestToolSetReportsCompletedToolCalls pins archie-core-467's task-transcript
|
||||
// counterpart: every tool invocation built through BuildToolSet must notify
|
||||
// ToolSetOptions.OnToolCall exactly once, on both the success and failure
|
||||
// path, so a workflow stage can surface it on the task timeline.
|
||||
func TestToolSetReportsCompletedToolCalls(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
handler tools.Handler
|
||||
wantFailed bool
|
||||
wantDetail string
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
handler: func(context.Context, map[string]any) (any, error) { return "wrote 3 lines", nil },
|
||||
wantFailed: false,
|
||||
wantDetail: `"wrote 3 lines"`,
|
||||
},
|
||||
{
|
||||
name: "failure",
|
||||
handler: func(context.Context, map[string]any) (any, error) { return nil, errors.New("permission denied") },
|
||||
wantFailed: true,
|
||||
wantDetail: "error: permission denied",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{Name: "write_file", Handler: tt.handler}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var reports []ToolCallReport
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{
|
||||
OnToolCall: func(rep ToolCallReport) { reports = append(reports, rep) },
|
||||
})
|
||||
_, _ = set["write_file"].Execute(context.Background(), `{}`)
|
||||
|
||||
if len(reports) != 1 {
|
||||
t.Fatalf("OnToolCall was called %d times, want 1: %+v", len(reports), reports)
|
||||
}
|
||||
got := reports[0]
|
||||
if got.Tool != "write_file" {
|
||||
t.Errorf("Tool = %q, want %q", got.Tool, "write_file")
|
||||
}
|
||||
if got.Failed != tt.wantFailed {
|
||||
t.Errorf("Failed = %v, want %v", got.Failed, tt.wantFailed)
|
||||
}
|
||||
if got.Detail != tt.wantDetail {
|
||||
t.Errorf("Detail = %q, want %q", got.Detail, tt.wantDetail)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetHandlerRejectsMalformedInput(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "noop",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return "ok", nil },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
set := mustBuildToolSet(t, reg)
|
||||
_, err := set["noop"].Execute(context.Background(), `not json`)
|
||||
if err == nil {
|
||||
t.Fatal("expected error decoding malformed input JSON")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetOmitsUnavailableTools(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "offline",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
CheckFn: func() bool { return false },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
set := mustBuildToolSet(t, reg)
|
||||
if _, ok := set["offline"]; ok {
|
||||
t.Error("ToolSet included a tool whose CheckFn reports unavailable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetEmptyRegistryReturnsEmptySet(t *testing.T) {
|
||||
set := mustBuildToolSet(t, tools.NewRegistry())
|
||||
if len(set) != 0 {
|
||||
t.Errorf("len(set) = %d, want 0", len(set))
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetNilRegistryReturnsEmptySet(t *testing.T) {
|
||||
set := mustBuildToolSet(t, nil)
|
||||
if len(set) != 0 {
|
||||
t.Errorf("len(set) = %d, want 0 for nil registry", len(set))
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetUsesObjectSchemaForParameterlessTools(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "no_args",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
parameters := mustBuildToolSet(t, reg)["no_args"].Parameters
|
||||
var schema map[string]any
|
||||
if err := json.Unmarshal(parameters, &schema); err != nil {
|
||||
t.Fatalf("Parameters = %s, want JSON object: %v", parameters, err)
|
||||
}
|
||||
if schema["type"] != "object" {
|
||||
t.Fatalf("Parameters = %s, want object schema", parameters)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetHandlerReturningNilOutputProducesNullJSON(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "voidy",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
set := mustBuildToolSet(t, reg)
|
||||
out, err := set["voidy"].Execute(context.Background(), `{}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != "null" {
|
||||
t.Errorf("output = %q, want \"null\"", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildToolSetReportsInvalidDynamicSchemas(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
entry tools.ToolEntry
|
||||
}{
|
||||
{
|
||||
name: "schema override panic",
|
||||
entry: tools.ToolEntry{
|
||||
Name: "panics",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
DynamicSchemaOverrides: func(tools.JSONSchema) tools.JSONSchema {
|
||||
panic("schema panic")
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "unmarshalable schema",
|
||||
entry: tools.ToolEntry{
|
||||
Name: "invalid",
|
||||
Schema: tools.JSONSchema{"bad": func() {}},
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "availability panic",
|
||||
entry: tools.ToolEntry{
|
||||
Name: "unavailable",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
CheckFn: func() bool {
|
||||
panic("availability panic")
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
if err := registry.Register(tt.entry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var (
|
||||
err error
|
||||
panicked any
|
||||
)
|
||||
func() {
|
||||
defer func() { panicked = recover() }()
|
||||
_, err = BuildToolSet(registry, ToolSetOptions{})
|
||||
}()
|
||||
if panicked != nil {
|
||||
t.Fatalf("BuildToolSet() panicked: %v", panicked)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("BuildToolSet() error = nil, want invalid schema error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func mustBuildToolSet(t *testing.T, registry *tools.Registry) map[string]*aicore.Tool {
|
||||
t.Helper()
|
||||
return mustBuildToolSetWith(t, registry, ToolSetOptions{})
|
||||
}
|
||||
|
||||
func mustBuildToolSetWith(t *testing.T, registry *tools.Registry, opts ToolSetOptions) map[string]*aicore.Tool {
|
||||
t.Helper()
|
||||
set, err := BuildToolSet(registry, opts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// registryReturning builds a single-entry registry whose tool returns payload.
|
||||
func registryReturning(t *testing.T, entry tools.ToolEntry, payload string) *tools.Registry {
|
||||
t.Helper()
|
||||
reg := tools.NewRegistry()
|
||||
entry.Handler = func(context.Context, map[string]any) (any, error) { return payload, nil }
|
||||
if err := reg.Register(entry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return reg
|
||||
}
|
||||
|
||||
// TestToolSetCapsResults covers the limit that decides how much of a tool
|
||||
// result reaches the model. The workspace tools truncate themselves, so the
|
||||
// entries that matter here are the ones that do not: skill bodies and MCP
|
||||
// results were previously handed over whole.
|
||||
func TestToolSetCapsResults(t *testing.T) {
|
||||
const payloadSize = 4000
|
||||
payload := strings.Repeat("z", payloadSize)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
entryLimit int // ToolEntry.MaxResultSizeChars
|
||||
defaultLimit int // ToolSetOptions.MaxResultChars
|
||||
wantTruncated bool
|
||||
}{
|
||||
{name: "under the default passes through", defaultLimit: payloadSize * 2},
|
||||
{name: "over the default is capped", defaultLimit: 100, wantTruncated: true},
|
||||
{name: "per-entry limit overrides a larger default", entryLimit: 100, defaultLimit: payloadSize * 2, wantTruncated: true},
|
||||
{name: "per-entry limit overrides a smaller default", entryLimit: payloadSize * 2, defaultLimit: 100},
|
||||
{name: "negative default disables capping", defaultLimit: -1},
|
||||
{name: "zero default disables capping", defaultLimit: 0},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
reg := registryReturning(t, tools.ToolEntry{
|
||||
Name: "big",
|
||||
MaxResultSizeChars: tc.entryLimit,
|
||||
}, payload)
|
||||
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{MaxResultChars: tc.defaultLimit})
|
||||
out, err := set["big"].Execute(context.Background(), "{}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
truncated := strings.Contains(out, "truncated")
|
||||
if truncated != tc.wantTruncated {
|
||||
t.Fatalf("truncated = %v, want %v (result %d bytes)", truncated, tc.wantTruncated, len(out))
|
||||
}
|
||||
if !tc.wantTruncated && len(out) < payloadSize {
|
||||
t.Errorf("result is %d bytes, want the whole %d-byte payload", len(out), payloadSize)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestToolSetSpillsOversizeResults proves an oversized result is displaced to
|
||||
// disk rather than lost: the model gets a path it can read back with the read
|
||||
// tool, which is the whole point of spilling instead of truncating.
|
||||
func TestToolSetSpillsOversizeResults(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
payload := strings.Repeat("q", 4000)
|
||||
|
||||
reg := registryReturning(t, tools.ToolEntry{Name: "big"}, payload)
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{MaxResultChars: 100, SpillDir: dir})
|
||||
|
||||
out, err := set["big"].Execute(context.Background(), "{}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(out, "spilled") {
|
||||
t.Fatalf("result = %q, want a spill reference", out)
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil || len(entries) != 1 {
|
||||
t.Fatalf("spill directory entries = %v, err = %v; want one spill", entries, err)
|
||||
}
|
||||
spillPath := filepath.Join(dir, entries[0].Name())
|
||||
if !strings.Contains(out, spillPath) {
|
||||
t.Errorf("result %q does not name the spill path %q", out, spillPath)
|
||||
}
|
||||
body, err := os.ReadFile(spillPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(body), payload) {
|
||||
t.Error("spill file does not hold the full payload")
|
||||
}
|
||||
}
|
||||
|
||||
// TestToolLimitsCreatesSpillDir covers the gap that made spilling dead code in
|
||||
// production: the configured directory was never created, so every oversized
|
||||
// result silently fell back to inline truncation.
|
||||
func TestToolLimitsCreatesSpillDir(t *testing.T) {
|
||||
// A nested path proves the whole tree is created, not just a leaf.
|
||||
dir := filepath.Join(t.TempDir(), "work", "tool-spill")
|
||||
limits := ToolLimits{MaxResultChars: 100, SpillDir: dir}
|
||||
|
||||
if err := limits.EnsureSpillDir(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("spill directory was not created: %v", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
t.Fatalf("%s is not a directory", dir)
|
||||
}
|
||||
|
||||
// And a spill actually lands there once it exists.
|
||||
reg := registryReturning(t, tools.ToolEntry{Name: "big"}, strings.Repeat("z", 5000))
|
||||
set := mustBuildToolSetWith(t, reg, limits.Options())
|
||||
if _, err := set["big"].Execute(context.Background(), "{}"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) == 0 {
|
||||
t.Error("no spill file was written into the created directory")
|
||||
}
|
||||
}
|
||||
|
||||
// TestToolLimitsEnsureSpillDirNoop covers the disabled case: no directory
|
||||
// configured is not an error, it selects inline truncation.
|
||||
func TestToolLimitsEnsureSpillDirNoop(t *testing.T) {
|
||||
if err := (ToolLimits{}).EnsureSpillDir(); err != nil {
|
||||
t.Errorf("EnsureSpillDir() with no directory = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Tool result volume is bounded per invocation only. Repeated large results
|
||||
// may each be spilled or truncated, but accumulation never stops the turn.
|
||||
func TestToolSetPerResultLimitNeverStopsFurtherCalls(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
spillDir bool
|
||||
}{
|
||||
{name: "results spilled to disk", spillDir: true},
|
||||
{name: "results truncated inline", spillDir: false},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := ""
|
||||
if tc.spillDir {
|
||||
dir = t.TempDir()
|
||||
}
|
||||
reg := registryReturning(t, tools.ToolEntry{Name: "big"}, strings.Repeat("q", 60_000))
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{MaxResultChars: 100, SpillDir: dir})
|
||||
|
||||
for range 5 {
|
||||
_, err := set["big"].Execute(context.Background(), "{}")
|
||||
if err != nil {
|
||||
t.Fatalf("later tool call was stopped by accumulated output: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestToolSetExcludesToolsets covers the task-agent case: a task runs in its
|
||||
// own worktree, so the chat workspace's file and shell tools must not follow it
|
||||
// in and bypass the agent loop's read-only and protected-path enforcement.
|
||||
func TestToolSetExcludesToolsets(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
for _, e := range []tools.ToolEntry{
|
||||
{Name: "shell", Toolset: "workspace"},
|
||||
{Name: "memory_edit", Toolset: "memory"},
|
||||
} {
|
||||
e.Handler = func(context.Context, map[string]any) (any, error) { return "ok", nil }
|
||||
if err := reg.Register(e); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
full := mustBuildToolSetWith(t, reg, ToolSetOptions{})
|
||||
if len(full) != 2 {
|
||||
t.Fatalf("with no exclusions the set has %d tools, want 2", len(full))
|
||||
}
|
||||
|
||||
limited := mustBuildToolSetWith(t, reg, ToolSetOptions{ExcludeToolsets: []string{"workspace"}})
|
||||
if _, ok := limited["shell"]; ok {
|
||||
t.Error("workspace tool survived the exclusion")
|
||||
}
|
||||
if _, ok := limited["memory_edit"]; !ok {
|
||||
t.Error("memory tool was dropped, want only the workspace toolset excluded")
|
||||
}
|
||||
}
|
||||
|
||||
// testApprover is a scripted ApprovalRequester for the tool-approval gate
|
||||
// tests. It returns whatever decision and error the test wired up, so each
|
||||
// test exercises one branch of the gate.
|
||||
type testApprover struct {
|
||||
decision tools.ApprovalDecision
|
||||
err error
|
||||
}
|
||||
|
||||
func (a *testApprover) RequestApproval(context.Context, string, string) (tools.ApprovalDecision, error) {
|
||||
return a.decision, a.err
|
||||
}
|
||||
|
||||
// approvalEntry returns a single-entry registry whose tool is marked
|
||||
// RequiresApproval and records whether its handler ran.
|
||||
func approvalEntry(t *testing.T, reg *tools.Registry, ran *bool) {
|
||||
t.Helper()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "gated",
|
||||
Description: "requires human consent",
|
||||
Classification: tools.RequiresApproval,
|
||||
Handler: func(context.Context, map[string]any) (any, error) {
|
||||
*ran = true
|
||||
return "ran", nil
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetApprovalApprovedExecutes(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
ran := false
|
||||
approvalEntry(t, reg, &ran)
|
||||
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{Approval: &testApprover{decision: tools.ApprovalApproved}})
|
||||
out, err := set["gated"].Execute(context.Background(), `{}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !ran {
|
||||
t.Error("handler did not run after approval was granted")
|
||||
}
|
||||
if out != `"ran"` {
|
||||
t.Errorf("output = %q, want \"ran\"", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetApprovalDeniedReturnsError(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
ran := false
|
||||
approvalEntry(t, reg, &ran)
|
||||
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{Approval: &testApprover{decision: tools.ApprovalDenied}})
|
||||
_, err := set["gated"].Execute(context.Background(), `{}`)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error when approval is denied")
|
||||
}
|
||||
if !errors.Is(err, tools.ErrApprovalDenied) {
|
||||
t.Errorf("error = %v, want it to wrap ErrApprovalDenied", err)
|
||||
}
|
||||
if ran {
|
||||
t.Error("handler ran despite the denial")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetApprovalPropagatesApproverError(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
ran := false
|
||||
approvalEntry(t, reg, &ran)
|
||||
|
||||
wantErr := errors.New("approver exploded")
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{Approval: &testApprover{err: wantErr}})
|
||||
_, err := set["gated"].Execute(context.Background(), `{}`)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error from the approver")
|
||||
}
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Errorf("error = %v, want it to wrap the approver error", err)
|
||||
}
|
||||
if ran {
|
||||
t.Error("handler ran despite the approver error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetApprovalMissingApproverReturnsError(t *testing.T) {
|
||||
ran := false
|
||||
entry := tools.ToolEntry{
|
||||
Name: "gated",
|
||||
Description: "requires human consent",
|
||||
Classification: tools.RequiresApproval,
|
||||
Handler: func(context.Context, map[string]any) (any, error) {
|
||||
ran = true
|
||||
return "should not run", nil
|
||||
},
|
||||
}
|
||||
// The build-time gate omits approval-requiring tools from a set built
|
||||
// without an approver, so exercise the runtime gate directly: the execute
|
||||
// closure must still refuse to run the tool.
|
||||
tool := aicore.NewTool(entry.Name, entry.Description, json.RawMessage(`{"type":"object","properties":{}}`), toolExecute(entry, ToolSetOptions{}))
|
||||
_, err := tool.Execute(context.Background(), `{}`)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error when no approver is configured")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no approver") {
|
||||
t.Errorf("error = %v, want it to name the missing approver", err)
|
||||
}
|
||||
if ran {
|
||||
t.Error("handler ran despite there being no approver")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetWithoutApprovalFlagExecutesNormally(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "plain",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return "ok", nil },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Even with an approver that would deny, a non-gated tool must run.
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{Approval: &testApprover{decision: tools.ApprovalDenied}})
|
||||
out, err := set["plain"].Execute(context.Background(), `{}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != `"ok"` {
|
||||
t.Errorf("output = %q, want \"ok\"", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildToolSetOmitsApprovalToolsWithoutApprover(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "gated",
|
||||
Description: "requires human consent",
|
||||
Classification: tools.RequiresApproval,
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "plain",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{})
|
||||
if _, ok := set["gated"]; ok {
|
||||
t.Error("ToolSet included an approval-requiring tool with no approver")
|
||||
}
|
||||
if _, ok := set["plain"]; !ok {
|
||||
t.Error("ToolSet dropped a non-approval tool")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildToolSetIncludesApprovalToolsWithApprover(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
if err := reg.Register(tools.ToolEntry{
|
||||
Name: "gated",
|
||||
Description: "requires human consent",
|
||||
Classification: tools.RequiresApproval,
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{Approval: &testApprover{decision: tools.ApprovalApproved}})
|
||||
if _, ok := set["gated"]; !ok {
|
||||
t.Error("ToolSet omitted an approval-requiring tool with an approver configured")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolSetApprovalFailsClosedOnUnknownDecision(t *testing.T) {
|
||||
reg := tools.NewRegistry()
|
||||
ran := false
|
||||
approvalEntry(t, reg, &ran)
|
||||
|
||||
// ApprovalDecision 999 is not a recognised constant. The gate must
|
||||
// fail closed: an unknown value from a faulty approver must not
|
||||
// execute the tool.
|
||||
unknownDecision := tools.ApprovalDecision(999)
|
||||
set := mustBuildToolSetWith(t, reg, ToolSetOptions{Approval: &testApprover{decision: unknownDecision}})
|
||||
tool, ok := set["gated"]
|
||||
if !ok {
|
||||
t.Fatal("tool not in set")
|
||||
}
|
||||
_, err := tool.Execute(context.Background(), `{}`)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown decision, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unexpected decision") {
|
||||
t.Errorf("error = %v, want 'unexpected decision'", err)
|
||||
}
|
||||
if ran {
|
||||
t.Error("tool executed despite unknown approval decision")
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestApplicationOwnsNoTransportWireMechanics(t *testing.T) {
|
||||
files, err := filepath.Glob("*.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, path := range files {
|
||||
if strings.HasSuffix(path, "_test.go") {
|
||||
continue
|
||||
}
|
||||
file, err := parser.ParseFile(token.NewFileSet(), path, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, imported := range file.Imports {
|
||||
name, err := strconv.Unquote(imported.Path.Value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
switch {
|
||||
case name == "encoding/json":
|
||||
t.Errorf("%s imports JSON wire encoding", path)
|
||||
case name == "github.com/samcharles93/archie-core/internal/domain/workintake":
|
||||
t.Errorf("%s imports NATS topology subjects", path)
|
||||
case name == "github.com/nats-io/nats.go" || strings.HasPrefix(name, "github.com/nats-io/nats.go/"):
|
||||
t.Errorf("%s imports raw NATS SDK package %q", path, name)
|
||||
}
|
||||
}
|
||||
ast.Inspect(file, func(node ast.Node) bool {
|
||||
selector, ok := node.(*ast.SelectorExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
if selector.Sel.Name == "ReplyAddress" || selector.Sel.Name == "Subject" || selector.Sel.Name == "Data" {
|
||||
t.Errorf("%s inspects transport wire selector %s", path, selector.Sel.Name)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Full-task request/reply is the worker's only production entry point. A
|
||||
// single-stage loop would recreate the obsolete split where archied owns the
|
||||
// workflow and remotely invokes each stage.
|
||||
func TestApplicationContainsNoSingleStageWorkerLoop(t *testing.T) {
|
||||
for _, path := range []string{"single_stage_request.go", "stage_loop.go"} {
|
||||
matches, err := filepath.Glob(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(matches) != 0 {
|
||||
t.Errorf("legacy single-stage worker file still exists: %s", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
// guardrailRunYAML is a repository-none workflow with one agent stage: a task
|
||||
// run reaches AgentStage.handleResult once, and the engine that recorded that
|
||||
// outcome is observable by its call count and by the decision a low threshold
|
||||
// earns.
|
||||
const guardrailRunYAML = "id: guardrail\nrepository: none\nsteps:\n" +
|
||||
" - type: agent.run\n settings:\n mission: first\n"
|
||||
|
||||
// TestProductionWorkerDependenciesBuildGuardrails pins the producer: the
|
||||
// process that runs agent stages is archie-agent, so its composition root must
|
||||
// build the guardrail engine every TaskContext records against. A daemon-built
|
||||
// engine can never reach this process -- it is a separate binary -- so before
|
||||
// this wiring TaskContext.Guardrails was nil on every production run and no
|
||||
// decision was ever recorded.
|
||||
func TestProductionWorkerDependenciesBuildGuardrails(t *testing.T) {
|
||||
deps, err := productionWorkerDependencies()
|
||||
if err != nil {
|
||||
t.Fatalf("productionWorkerDependencies: %v", err)
|
||||
}
|
||||
if deps.guardrails == nil {
|
||||
t.Fatal("worker composition built no guardrail engine: TaskContext.Guardrails stays nil and no agent-stage decision is recorded")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTaskRecordsGuardrailDecision is the end-to-end half: a workflow run
|
||||
// whose TaskContext carries the engine records its agent stage's outcome. Before
|
||||
// the wiring the engine was never assigned, so the run recorded zero -- the
|
||||
// guardrail engine observed nothing for any task.
|
||||
func TestRunTaskRecordsGuardrailDecision(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
st := pgstore.Open(t)
|
||||
|
||||
task, err := st.EnqueueChatTask(ctx, "", "", "guardrail recording", "", "guardrail", "", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
if err := st.Transition(ctx, task.ID, task.Status, workflow.StatusRunning, "started"); err != nil {
|
||||
t.Fatalf("claim task: %v", err)
|
||||
}
|
||||
task.Status = workflow.StatusRunning
|
||||
task.WorkflowDefinitionYAML = guardrailRunYAML
|
||||
task.WorkflowDefinitionDigest = workflow.DigestDefinition(guardrailRunYAML)
|
||||
if err := st.Update(ctx, task); err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
|
||||
workDir := t.TempDir()
|
||||
// A threshold a single stage reaches turns the recorded outcome into an
|
||||
// actual guardrail decision, so the test proves the run consulted the
|
||||
// engine rather than merely holding a reference.
|
||||
engine := tools.NewGuardrailEngine(tools.ToolCallGuardrailConfig{
|
||||
NoProgressWarnAfter: 1,
|
||||
HardStopAfterWarnRepeat: 1,
|
||||
})
|
||||
request := taskrun.Request{
|
||||
Task: task,
|
||||
Repo: config.Repo{},
|
||||
Cfg: config.Config{Models: map[string]string{"builder": "p/m"}}.ForTask(),
|
||||
WorkflowDefinition: guardrailRunYAML,
|
||||
}
|
||||
dependencies := taskDependencies{
|
||||
store: st,
|
||||
trees: &fakeScratchTrees{dir: workDir},
|
||||
steps: testSteps(t),
|
||||
guardrails: engine,
|
||||
}
|
||||
fakeRunner := runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner {
|
||||
return stubRunner{}
|
||||
})
|
||||
if _, err := runTask(ctx, request, dependencies, fakeRunner, workDir, slog.New(slog.DiscardHandler)); err != nil {
|
||||
t.Fatalf("runTask: %v", err)
|
||||
}
|
||||
|
||||
if got := engine.TotalCalls(); got != 1 {
|
||||
t.Fatalf("guardrail engine recorded %d tool outcomes, want 1 (the agent stage): TaskContext.Guardrails is not wired to the engine the run was given", got)
|
||||
}
|
||||
if !engine.HardStopped() {
|
||||
t.Fatal("guardrail engine issued no decision for the run's agent stage: TaskContext.Guardrails is not wired to the engine the run was given")
|
||||
}
|
||||
}
|
||||
@@ -1,451 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
protocolmcp "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
)
|
||||
|
||||
type fakeMCPEngine struct {
|
||||
id string
|
||||
entries []tools.ToolEntry
|
||||
startErr error
|
||||
discoverErr error
|
||||
stopErr error
|
||||
starts int
|
||||
discovers int
|
||||
stops int
|
||||
}
|
||||
|
||||
func (f *fakeMCPEngine) Manifest() plugin.Manifest {
|
||||
return plugin.Manifest{
|
||||
ID: "mcp." + f.id,
|
||||
Name: f.id,
|
||||
Version: "1.0.0",
|
||||
APIVersion: plugin.HostAPIVersion,
|
||||
Capabilities: []plugin.CapabilityKind{"tools"},
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fakeMCPEngine) Start(context.Context) error {
|
||||
f.starts++
|
||||
return f.startErr
|
||||
}
|
||||
|
||||
func (f *fakeMCPEngine) Discover(context.Context) ([]tools.ToolEntry, error) {
|
||||
f.discovers++
|
||||
return f.entries, f.discoverErr
|
||||
}
|
||||
|
||||
func (f *fakeMCPEngine) Stop(context.Context) error {
|
||||
f.stops++
|
||||
return f.stopErr
|
||||
}
|
||||
|
||||
func testTool(name string) tools.ToolEntry {
|
||||
return tools.ToolEntry{
|
||||
Name: name,
|
||||
Handler: func(context.Context, map[string]any) (any, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildMCPProviderSelectsTransport(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
server config.MCPServer
|
||||
wantTransport string
|
||||
wantType any
|
||||
wantCommand string
|
||||
wantArgs []string
|
||||
wantEnv []string
|
||||
wantEndpoint string
|
||||
wantMessageEndpoint string
|
||||
wantHeaders map[string]string
|
||||
wantErr string
|
||||
wantUnknown bool
|
||||
}{
|
||||
{
|
||||
name: "default stdio trims command",
|
||||
server: config.MCPServer{Name: " local ", Command: " server ", Args: []string{"--flag"}},
|
||||
wantTransport: "stdio",
|
||||
wantType: (*protocolmcp.StdioTransport)(nil),
|
||||
wantCommand: "server",
|
||||
wantArgs: []string{"--flag"},
|
||||
},
|
||||
{
|
||||
name: "explicit stdio",
|
||||
server: config.MCPServer{Name: "local", Transport: " STDIO ", Command: "server"},
|
||||
wantTransport: "stdio",
|
||||
wantType: (*protocolmcp.StdioTransport)(nil),
|
||||
wantCommand: "server",
|
||||
},
|
||||
{
|
||||
// Task containers are ephemeral, so an npx-launched MCP server
|
||||
// must be pointed at the shared mcp-npm cache volume mounted on
|
||||
// every container -- otherwise it re-resolves/re-downloads from
|
||||
// the registry on every single task.
|
||||
name: "npx stdio server gets the persistent npm cache",
|
||||
server: config.MCPServer{Name: "local", Command: "npx", Args: []string{"-y", "some-mcp-server"}},
|
||||
wantTransport: "stdio",
|
||||
wantType: (*protocolmcp.StdioTransport)(nil),
|
||||
wantCommand: "npx",
|
||||
wantArgs: []string{"-y", "some-mcp-server"},
|
||||
wantEnv: []string{
|
||||
"NPM_CONFIG_CACHE=" + storage.MCPNPMCacheMountDir,
|
||||
"NPM_CONFIG_PREFER_OFFLINE=true",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "non-npx stdio server gets no npm cache env",
|
||||
server: config.MCPServer{Name: "local", Command: "/usr/local/bin/my-mcp-server"},
|
||||
wantTransport: "stdio",
|
||||
wantType: (*protocolmcp.StdioTransport)(nil),
|
||||
wantCommand: "/usr/local/bin/my-mcp-server",
|
||||
},
|
||||
{
|
||||
name: "http",
|
||||
server: config.MCPServer{Name: "remote", Transport: "http", URL: " https://example.test/mcp ", Headers: map[string]string{"Authorization": "Bearer test"}},
|
||||
wantTransport: "http",
|
||||
wantType: (*protocolmcp.HTTPTransport)(nil),
|
||||
wantEndpoint: "https://example.test/mcp",
|
||||
wantHeaders: map[string]string{"Authorization": "Bearer test"},
|
||||
},
|
||||
{
|
||||
name: "streamable http alias",
|
||||
server: config.MCPServer{Name: "remote", Transport: "StreamableHTTP", URL: "https://example.test/mcp"},
|
||||
wantTransport: "streamablehttp",
|
||||
wantType: (*protocolmcp.HTTPTransport)(nil),
|
||||
wantEndpoint: "https://example.test/mcp",
|
||||
},
|
||||
{
|
||||
name: "sse",
|
||||
server: config.MCPServer{Name: "events", Transport: "sse", SSEEndpoint: " https://example.test/events ", MessageEndpoint: " https://example.test/messages ", Headers: map[string]string{"X-Test": "value"}},
|
||||
wantTransport: "sse",
|
||||
wantType: (*protocolmcp.SSETransport)(nil),
|
||||
wantEndpoint: "https://example.test/events",
|
||||
wantMessageEndpoint: "https://example.test/messages",
|
||||
wantHeaders: map[string]string{"X-Test": "value"},
|
||||
},
|
||||
{
|
||||
name: "stdio requires command",
|
||||
server: config.MCPServer{Name: "local", Transport: "stdio"},
|
||||
wantTransport: "stdio",
|
||||
wantErr: `MCP stdio server "local" requires a command`,
|
||||
},
|
||||
{
|
||||
name: "http requires url",
|
||||
server: config.MCPServer{Name: "remote", Transport: "http"},
|
||||
wantTransport: "http",
|
||||
wantErr: `MCP http server "remote" requires a url`,
|
||||
},
|
||||
{
|
||||
name: "sse requires endpoint",
|
||||
server: config.MCPServer{Name: "events", Transport: "sse"},
|
||||
wantTransport: "sse",
|
||||
wantErr: `MCP sse server "events" requires an sse_endpoint`,
|
||||
},
|
||||
{
|
||||
name: "unknown transport is a nonfatal skip",
|
||||
server: config.MCPServer{Name: "other", Transport: "websocket"},
|
||||
wantTransport: "websocket",
|
||||
wantErr: "unknown MCP transport: websocket",
|
||||
wantUnknown: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
provider, transportType, err := buildMCPProvider(tc.server, nil)
|
||||
if transportType != tc.wantTransport {
|
||||
t.Fatalf("transport = %q, want %q", transportType, tc.wantTransport)
|
||||
}
|
||||
if tc.wantErr != "" {
|
||||
if err == nil || err.Error() != tc.wantErr {
|
||||
t.Fatalf("error = %v, want %q", err, tc.wantErr)
|
||||
}
|
||||
if errors.Is(err, errUnknownMCPTransport) != tc.wantUnknown {
|
||||
t.Fatalf("errors.Is(err, errUnknownMCPTransport) = %v, want %v", errors.Is(err, errUnknownMCPTransport), tc.wantUnknown)
|
||||
}
|
||||
if provider != nil {
|
||||
t.Fatalf("provider = %T, want nil on invalid configuration", provider)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("buildMCPProvider() error = %v", err)
|
||||
}
|
||||
if provider == nil {
|
||||
t.Fatal("buildMCPProvider() returned nil provider")
|
||||
}
|
||||
transport := reflect.ValueOf(provider).Elem().FieldByName("transport")
|
||||
if transport.IsNil() {
|
||||
t.Fatal("provider transport is nil")
|
||||
}
|
||||
if got, want := transport.Elem().Type(), reflect.TypeOf(tc.wantType); got != want {
|
||||
t.Fatalf("transport type = %v, want %v", got, want)
|
||||
}
|
||||
assertMCPTransportConfig(t, transport.Elem(), tc.wantCommand, tc.wantArgs, tc.wantEnv, tc.wantEndpoint, tc.wantMessageEndpoint, tc.wantHeaders)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertMCPTransportConfig(
|
||||
t *testing.T,
|
||||
transport reflect.Value,
|
||||
wantCommand string,
|
||||
wantArgs []string,
|
||||
wantEnv []string,
|
||||
wantEndpoint string,
|
||||
wantMessageEndpoint string,
|
||||
wantHeaders map[string]string,
|
||||
) {
|
||||
t.Helper()
|
||||
config := transport.Elem().FieldByName("config")
|
||||
assertStringField := func(name, want string) {
|
||||
t.Helper()
|
||||
if want == "" {
|
||||
return
|
||||
}
|
||||
if got := config.FieldByName(name).String(); got != want {
|
||||
t.Errorf("transport config %s = %q, want %q", name, got, want)
|
||||
}
|
||||
}
|
||||
assertStringField("Command", wantCommand)
|
||||
if wantEndpoint != "" {
|
||||
field := "Endpoint"
|
||||
if config.FieldByName(field).Kind() == reflect.Invalid {
|
||||
field = "SSEEndpoint"
|
||||
}
|
||||
assertStringField(field, wantEndpoint)
|
||||
}
|
||||
assertStringField("MessageEndpoint", wantMessageEndpoint)
|
||||
|
||||
if wantArgs != nil {
|
||||
args := config.FieldByName("Args")
|
||||
got := make([]string, args.Len())
|
||||
for i := range args.Len() {
|
||||
got[i] = args.Index(i).String()
|
||||
}
|
||||
if !reflect.DeepEqual(got, wantArgs) {
|
||||
t.Errorf("transport config Args = %v, want %v", got, wantArgs)
|
||||
}
|
||||
}
|
||||
if wantHeaders != nil {
|
||||
headers := config.FieldByName("Headers")
|
||||
for key, want := range wantHeaders {
|
||||
if got := headers.MapIndex(reflect.ValueOf(key)).String(); got != want {
|
||||
t.Errorf("transport config Headers[%q] = %q, want %q", key, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Env only exists on StdioTransportConfig; other transports have no
|
||||
// registry-fetch dependency for this check to apply to.
|
||||
if envField := config.FieldByName("Env"); envField.Kind() != reflect.Invalid {
|
||||
got := make([]string, envField.Len())
|
||||
for i := range envField.Len() {
|
||||
got[i] = envField.Index(i).String()
|
||||
}
|
||||
if len(got) == 0 {
|
||||
got = nil
|
||||
}
|
||||
if !reflect.DeepEqual(got, wantEnv) {
|
||||
t.Errorf("transport config Env = %v, want %v", got, wantEnv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartMCPProviderLifecycle(t *testing.T) {
|
||||
startErr := errors.New("start failed")
|
||||
discoverErr := errors.New("discover failed")
|
||||
log := slog.New(slog.DiscardHandler)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
provider *fakeMCPEngine
|
||||
wantErr error
|
||||
wantTools int
|
||||
wantStarts int
|
||||
wantDiscovers int
|
||||
wantStops int
|
||||
wantRegistered []string
|
||||
}{
|
||||
{
|
||||
name: "start failure does not attempt discovery or stop",
|
||||
provider: &fakeMCPEngine{id: "start", startErr: startErr},
|
||||
wantErr: startErr,
|
||||
wantStarts: 1,
|
||||
},
|
||||
{
|
||||
name: "discovery failure stops started provider",
|
||||
provider: &fakeMCPEngine{id: "discover", discoverErr: discoverErr, stopErr: errors.New("ignored cleanup failure")},
|
||||
wantErr: discoverErr,
|
||||
wantStarts: 1,
|
||||
wantDiscovers: 1,
|
||||
wantStops: 1,
|
||||
},
|
||||
{
|
||||
name: "registration failure is nonfatal",
|
||||
provider: &fakeMCPEngine{id: "registration", entries: []tools.ToolEntry{
|
||||
testTool("available"),
|
||||
{Name: "invalid"},
|
||||
testTool("available"),
|
||||
}},
|
||||
wantTools: 3,
|
||||
wantStarts: 1,
|
||||
wantDiscovers: 1,
|
||||
wantRegistered: []string{"available"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
gotTools, err := startMCPProvider(t.Context(), tc.name, tc.provider, registry, log)
|
||||
if !errors.Is(err, tc.wantErr) {
|
||||
t.Fatalf("error = %v, want %v", err, tc.wantErr)
|
||||
}
|
||||
if gotTools != tc.wantTools {
|
||||
t.Fatalf("tool count = %d, want %d", gotTools, tc.wantTools)
|
||||
}
|
||||
if tc.provider.starts != tc.wantStarts || tc.provider.discovers != tc.wantDiscovers || tc.provider.stops != tc.wantStops {
|
||||
t.Fatalf("lifecycle counts = start:%d discover:%d stop:%d, want start:%d discover:%d stop:%d", tc.provider.starts, tc.provider.discovers, tc.provider.stops, tc.wantStarts, tc.wantDiscovers, tc.wantStops)
|
||||
}
|
||||
for _, name := range tc.wantRegistered {
|
||||
if _, ok := registry.Get(name); !ok {
|
||||
t.Errorf("tool %q was not registered", name)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartMCPProvidersPreservesOptionalProviderBehavior(t *testing.T) {
|
||||
configErr := errors.New("invalid config")
|
||||
startErr := errors.New("start failed")
|
||||
discoverErr := errors.New("discover failed")
|
||||
stopErr := errors.New("stop failed")
|
||||
providers := map[string]*fakeMCPEngine{
|
||||
"start-failure": {id: "start-failure", startErr: startErr},
|
||||
"discover-failure": {id: "discover-failure", discoverErr: discoverErr},
|
||||
"valid-one": {id: "valid-one", entries: []tools.ToolEntry{testTool("shared")}, stopErr: stopErr},
|
||||
"valid-two": {id: "valid-two", entries: []tools.ToolEntry{testTool("shared")}},
|
||||
}
|
||||
buildCalls := make(map[string]int)
|
||||
build := func(server config.MCPServer, _ protocolmcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
buildCalls[server.Name]++
|
||||
switch server.Name {
|
||||
case "unknown":
|
||||
return nil, "websocket", fmt.Errorf("%w: websocket", errUnknownMCPTransport)
|
||||
case "invalid":
|
||||
return nil, "stdio", configErr
|
||||
default:
|
||||
return providers[server.Name], "stdio", nil
|
||||
}
|
||||
}
|
||||
|
||||
servers := []config.MCPServer{
|
||||
{Name: " "},
|
||||
{Name: "unknown"},
|
||||
{Name: "invalid"},
|
||||
{Name: "start-failure"},
|
||||
{Name: "discover-failure"},
|
||||
{Name: "valid-one"},
|
||||
{Name: "valid-two"},
|
||||
}
|
||||
set, err := startMCPProvidersWith(t.Context(), servers, nil, slog.New(slog.DiscardHandler), build)
|
||||
if err != nil {
|
||||
t.Fatalf("error = %v, want nil when valid providers remain available", err)
|
||||
}
|
||||
if set == nil {
|
||||
t.Fatal("provider set = nil, want valid providers to remain available")
|
||||
}
|
||||
if got := len(set.providers); got != 2 {
|
||||
t.Fatalf("available providers = %d, want 2", got)
|
||||
}
|
||||
if _, ok := set.registry.Get("shared"); !ok {
|
||||
t.Fatal("tool from valid provider was not available")
|
||||
}
|
||||
if buildCalls[" "] != 0 {
|
||||
t.Fatal("empty-name config reached provider builder")
|
||||
}
|
||||
if providers["start-failure"].stops != 0 {
|
||||
t.Fatal("start failure unexpectedly stopped provider")
|
||||
}
|
||||
if providers["discover-failure"].stops != 1 {
|
||||
t.Fatalf("discovery failure stop count = %d, want 1", providers["discover-failure"].stops)
|
||||
}
|
||||
|
||||
set.cleanup(t.Context(), slog.New(slog.DiscardHandler))
|
||||
if providers["valid-one"].stops != 1 || providers["valid-two"].stops != 1 {
|
||||
t.Fatalf("cleanup stop counts = (%d, %d), want (1, 1)", providers["valid-one"].stops, providers["valid-two"].stops)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartMCPProvidersReturnsNilWithoutAvailableProviders(t *testing.T) {
|
||||
firstErr := errors.New("first provider failed")
|
||||
secondErr := errors.New("second provider failed")
|
||||
tests := []struct {
|
||||
name string
|
||||
servers []config.MCPServer
|
||||
build mcpProviderBuilder
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "no configured servers",
|
||||
},
|
||||
{
|
||||
name: "unknown transport is skipped without error",
|
||||
servers: []config.MCPServer{{Name: "unknown"}},
|
||||
build: func(config.MCPServer, protocolmcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
return nil, "unknown", errUnknownMCPTransport
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "first provider error is returned",
|
||||
servers: []config.MCPServer{{Name: "first"}, {Name: "second"}},
|
||||
build: func(server config.MCPServer, _ protocolmcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
if server.Name == "first" {
|
||||
return nil, "stdio", firstErr
|
||||
}
|
||||
return nil, "stdio", secondErr
|
||||
},
|
||||
wantErr: firstErr,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
build := tc.build
|
||||
if build == nil {
|
||||
build = func(config.MCPServer, protocolmcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
t.Fatal("builder called without configured servers")
|
||||
return nil, "", nil
|
||||
}
|
||||
}
|
||||
set, err := startMCPProvidersWith(t.Context(), tc.servers, nil, slog.New(slog.DiscardHandler), build)
|
||||
if set != nil {
|
||||
t.Fatalf("provider set = %#v, want nil", set)
|
||||
}
|
||||
if tc.wantErr == nil {
|
||||
if err != nil {
|
||||
t.Fatalf("error = %v, want nil", err)
|
||||
}
|
||||
} else if reflect.ValueOf(err).Pointer() != reflect.ValueOf(tc.wantErr).Pointer() {
|
||||
t.Fatalf("error = %v, want exact first error %v", err, tc.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
// capturedChatRequest is the JSON body the OpenAI-compatible provider sent for
|
||||
// the last call, which is where the max-token parameter is chosen.
|
||||
type capturedChatRequest struct {
|
||||
mu sync.Mutex
|
||||
body map[string]any
|
||||
}
|
||||
|
||||
func (c *capturedChatRequest) get() map[string]any {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.body
|
||||
}
|
||||
|
||||
// newCapturingChatAPI answers every completion and records the wire body, so a
|
||||
// test can assert the parameter the provider actually emitted rather than an
|
||||
// option the handler merely set.
|
||||
func newCapturingChatAPI(t *testing.T, capture *capturedChatRequest) *httptest.Server {
|
||||
t.Helper()
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
capture.mu.Lock()
|
||||
capture.body = body
|
||||
capture.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"test",`+
|
||||
`"choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],`+
|
||||
`"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
return api
|
||||
}
|
||||
|
||||
// newSamplingModelServer stands in for an OpenAI-compatible provider so a
|
||||
// sampling request can be answered without a real backend.
|
||||
func newSamplingModelServer(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"gpt-5.6",`+
|
||||
`"choices":[{"index":0,"message":{"role":"assistant","content":"four"},"finish_reason":"stop"}],`+
|
||||
`"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
return api
|
||||
}
|
||||
|
||||
func testSamplingRequest() mcp.SamplingRequest {
|
||||
return mcp.SamplingRequest{
|
||||
Messages: []mcp.SamplingMessage{{
|
||||
Role: "user",
|
||||
Content: mcp.SamplingContent{Type: "text", Text: "2+2?"},
|
||||
}},
|
||||
MaxTokens: 16,
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskSamplingHandlerAnswersFromWorkerModel proves a task-scoped MCP
|
||||
// server's sampling request is answered by the worker's own model. A task
|
||||
// hosts the same configured MCP servers as a chat turn, so without this the
|
||||
// server is answered on the chat path and refused on the task path.
|
||||
func TestTaskSamplingHandlerAnswersFromWorkerModel(t *testing.T) {
|
||||
api := newSamplingModelServer(t)
|
||||
llm := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
})
|
||||
handler := taskSamplingHandler(llm, config.TaskConfig{Models: map[string]string{"builder": "openai/gpt-5.6"}})
|
||||
|
||||
result, err := handler(t.Context(), testSamplingRequest())
|
||||
if err != nil {
|
||||
t.Fatalf("sampling handler = %v, want a completion", err)
|
||||
}
|
||||
if result.Model != "openai/gpt-5.6" {
|
||||
t.Errorf("result.Model = %q, want the task's builder model", result.Model)
|
||||
}
|
||||
if result.Role != "assistant" || result.Content.Type != "text" || result.Content.Text != "four" {
|
||||
t.Errorf("result = %+v, want an assistant text completion", result)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskSamplingHandlerRefusesCleanly proves a worker with no usable model
|
||||
// answers the request with an error instead of panicking or dropping it: the
|
||||
// server sees a failed request, not a dead session.
|
||||
func TestTaskSamplingHandlerRefusesCleanly(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
llm bool
|
||||
model string
|
||||
want string
|
||||
}{
|
||||
{name: "no model runtime", llm: false, model: "openai/gpt-5.6", want: "no model runtime"},
|
||||
{name: "no builder model configured", llm: true, want: "no builder model"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
llm := agentexec.NewRuntime(nil)
|
||||
if tc.llm {
|
||||
llm = agentexec.NewRuntime(map[string]agentexec.Provider{"openai": {Class: "openai"}})
|
||||
}
|
||||
models := map[string]string{}
|
||||
if tc.model != "" {
|
||||
models["builder"] = tc.model
|
||||
}
|
||||
_, err := taskSamplingHandler(llm, config.TaskConfig{Models: models})(t.Context(), testSamplingRequest())
|
||||
if err == nil {
|
||||
t.Fatal("sampling handler returned no error with no usable model")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tc.want) {
|
||||
t.Errorf("error = %q, want it to mention %q", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskSamplingBoundFollowsModelClass proves the worker consumes the
|
||||
// reasoning flag carried in TaskConfig.ModelLimits: a reasoning-class builder
|
||||
// model gets no max-token bound (its provider rejects the max_tokens parameter
|
||||
// the chat-completions provider emits), while a classic model gets the
|
||||
// request's own bound.
|
||||
func TestTaskSamplingBoundFollowsModelClass(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reasoning bool
|
||||
wantBound bool
|
||||
wantValue int
|
||||
}{
|
||||
{name: "reasoning model", reasoning: true},
|
||||
{name: "classic model", wantBound: true, wantValue: 16},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
capture := &capturedChatRequest{}
|
||||
api := newCapturingChatAPI(t, capture)
|
||||
llm := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
})
|
||||
cfg := config.TaskConfig{
|
||||
Models: map[string]string{"builder": "openai/gpt-5.6"},
|
||||
ModelLimits: map[string]config.ModelLimits{
|
||||
"openai/gpt-5.6": {MaxOutputTokens: 128000, Reasoning: tc.reasoning},
|
||||
},
|
||||
}
|
||||
if _, err := taskSamplingHandler(llm, cfg)(t.Context(), testSamplingRequest()); err != nil {
|
||||
t.Fatalf("sampling handler error = %v", err)
|
||||
}
|
||||
bound, present := capture.get()["max_tokens"]
|
||||
if present != tc.wantBound {
|
||||
t.Fatalf("request body max_tokens present = %v (value %v), want %v; body = %v", present, bound, tc.wantBound, capture.get())
|
||||
}
|
||||
if present && bound != float64(tc.wantValue) {
|
||||
t.Fatalf("request body max_tokens = %v, want %d; body = %v", bound, tc.wantValue, capture.get())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildMCPProviderWiresTaskSamplingHandler proves the worker's MCP
|
||||
// provider builder hands the provider the sampling handler, so the seam a task
|
||||
// run builds providers through is the one that answers sampling -- not only
|
||||
// the handler in isolation.
|
||||
func TestBuildMCPProviderWiresTaskSamplingHandler(t *testing.T) {
|
||||
handler := taskSamplingHandler(agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai"},
|
||||
}), config.TaskConfig{Models: map[string]string{"builder": "openai/gpt-5.6"}})
|
||||
|
||||
provider, _, err := buildMCPProvider(config.MCPServer{Name: "sampling", Command: "server"}, handler)
|
||||
if err != nil {
|
||||
t.Fatalf("buildMCPProvider: %v", err)
|
||||
}
|
||||
field := reflect.ValueOf(provider).Elem().FieldByName("samplingHandler")
|
||||
if field.IsNil() {
|
||||
t.Fatal("task MCP provider carries no sampling handler: a task-scoped server's sampling/createMessage request is refused")
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
)
|
||||
|
||||
// TestArchieAgentMCPOverStdio drives the real archie-agent binary's mcp
|
||||
// subcommand with the MCP SDK's client, the way a harness CLI does.
|
||||
func TestArchieAgentMCPOverStdio(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
bin := filepath.Join(dir, "archie-agent")
|
||||
build := exec.CommandContext(t.Context(), "go", "build", "-o", bin, "github.com/samcharles93/archie-core/cmd/archie-agent")
|
||||
if out, err := build.CombinedOutput(); err != nil {
|
||||
t.Fatalf("build archie-agent: %v\n%s", err, out)
|
||||
}
|
||||
spec, _ := json.Marshal([]agentexec.CaptureTool{{
|
||||
Name: "record_finding", Description: "Record one finding.",
|
||||
Parameters: json.RawMessage(`{"type":"object","properties":{"title":{"type":"string"}},"required":["title"]}`),
|
||||
RequiredFields: []string{"title"},
|
||||
}})
|
||||
specPath, captures := filepath.Join(dir, "tools.json"), filepath.Join(dir, "captures.jsonl")
|
||||
if err := os.WriteFile(specPath, spec, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(captures, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
transport := &mcp.CommandTransport{Command: exec.CommandContext(t.Context(), bin, "mcp", "-spec", specPath, "-captures", captures)}
|
||||
session, err := mcp.NewClient(&mcp.Implementation{Name: "harness", Version: "0"}, nil).Connect(t.Context(), transport, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer session.Close()
|
||||
res, err := session.CallTool(t.Context(), &mcp.CallToolParams{Name: "record_finding", Arguments: map[string]any{"title": "nil deref"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if text, ok := res.Content[0].(*mcp.TextContent); !ok || !strings.Contains(text.Text, "recorded") {
|
||||
t.Fatalf("reply %+v", res.Content)
|
||||
}
|
||||
_ = session.Close()
|
||||
raw, err := os.ReadFile(captures)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(raw), `"title":"nil deref"`) {
|
||||
t.Fatalf("captures file %q, want the recorded call", raw)
|
||||
}
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
)
|
||||
|
||||
type persistentRunnerFunc func(context.Context, string, agentexec.Request, agentexec.ToolCallReporter) (agentexec.Result, error)
|
||||
|
||||
func (f persistentRunnerFunc) Run(ctx context.Context, workspace string, request agentexec.Request, report agentexec.ToolCallReporter) (agentexec.Result, error) {
|
||||
return f(ctx, workspace, request, report)
|
||||
}
|
||||
|
||||
func TestPersistentRunnerRecordsStageMemoryAndPluginOverride(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pluginsDir := filepath.Join(dir, "plugins")
|
||||
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(pluginsDir, "check.go"), []byte("override"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
runner := persistentRunner{
|
||||
Runner: persistentRunnerFunc(func(_ context.Context, _ string, request agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
|
||||
if got := request.Plugins[0].Src; got != "override" {
|
||||
t.Fatalf("plugin source = %q, want persistent override", got)
|
||||
}
|
||||
return agentexec.Result{Version: agentexec.ProtocolVersion, TaskID: request.TaskID, Attempt: request.Attempt, Stage: request.Stage, Status: agentexec.StatusPassed, AppendedNotes: []string{"remember me"}}, nil
|
||||
}),
|
||||
enabled: true,
|
||||
sessionPath: filepath.Join(dir, "session.jsonl"),
|
||||
memoryPath: filepath.Join(dir, "memory.jsonl"),
|
||||
pluginsDir: pluginsDir,
|
||||
}
|
||||
_, err := runner.Run(context.Background(), t.TempDir(), agentexec.Request{
|
||||
TaskID: 7, Attempt: 2, Stage: "implement",
|
||||
Plugins: []agentexec.PluginSpec{{Name: "check", Src: "bundled"}},
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, path := range []string{runner.sessionPath, runner.memoryPath} {
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasSuffix(string(content), "\n") {
|
||||
t.Errorf("%s is not newline-terminated JSONL", path)
|
||||
}
|
||||
}
|
||||
memory, err := os.ReadFile(runner.memoryPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(memory), `"content":"remember me"`) {
|
||||
t.Errorf("memory record = %s, want appended note", memory)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadProjectMemoryRestoresEarlierNotes(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "memory.jsonl")
|
||||
if err := os.WriteFile(path, []byte("{\"content\":\"first fact\"}\n{\"content\":\"second fact\"}\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := readProjectMemory(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, fact := range []string{"first fact", "second fact"} {
|
||||
if !strings.Contains(got, fact) {
|
||||
t.Errorf("memory prompt %q missing %q", got, fact)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadProjectMemoryAcceptsLargePersistedNote(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "memory.jsonl")
|
||||
note := strings.Repeat("x", 70*1024)
|
||||
encoded := `{"content":"` + note + `"}` + "\n"
|
||||
if err := os.WriteFile(path, []byte(encoded), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := readProjectMemory(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(got, note) {
|
||||
t.Error("large persisted note was not restored")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPersistentRunnerRejectsFailedResultBeforePersistence(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
runner := persistentRunner{
|
||||
Runner: persistentRunnerFunc(func(_ context.Context, _ string, _ agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
|
||||
return agentexec.Result{AppendedNotes: []string{"untrusted"}}, os.ErrPermission
|
||||
}),
|
||||
enabled: true,
|
||||
sessionPath: filepath.Join(dir, "session.jsonl"),
|
||||
memoryPath: filepath.Join(dir, "memory.jsonl"),
|
||||
pluginsDir: filepath.Join(dir, "plugins"),
|
||||
}
|
||||
_, err := runner.Run(context.Background(), t.TempDir(), agentexec.Request{}, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected runner error")
|
||||
}
|
||||
for _, path := range []string{runner.sessionPath, runner.memoryPath} {
|
||||
if _, statErr := os.Stat(path); !os.IsNotExist(statErr) {
|
||||
t.Errorf("%s persisted rejected output", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) }
|
||||
@@ -1,90 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func echoTarget(t *testing.T, reply string) string {
|
||||
t.Helper()
|
||||
ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = ln.Close() })
|
||||
go func() {
|
||||
for {
|
||||
c, err := ln.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, _ = io.WriteString(c, reply)
|
||||
_ = c.Close()
|
||||
}
|
||||
}()
|
||||
return ln.Addr().String()
|
||||
}
|
||||
|
||||
func freeAddr(t *testing.T) string {
|
||||
t.Helper()
|
||||
ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := ln.Addr().String()
|
||||
_ = ln.Close()
|
||||
return addr
|
||||
}
|
||||
|
||||
func TestRunRelayForwardsEachListenerToItsTarget(t *testing.T) {
|
||||
proxy, nats := freeAddr(t), freeAddr(t)
|
||||
forwards := map[string]string{proxy: echoTarget(t, "proxy"), nats: echoTarget(t, "nats")}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- RunRelay(ctx, forwards) }()
|
||||
|
||||
for listen, want := range map[string]string{proxy: "proxy", nats: "nats"} {
|
||||
var got []byte
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
c, err := (&net.Dialer{}).DialContext(t.Context(), "tcp", listen)
|
||||
if err == nil {
|
||||
_ = c.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
got, _ = io.ReadAll(c)
|
||||
_ = c.Close()
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("relay never listened on %s: %v", listen, err)
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if string(got) != want {
|
||||
t.Fatalf("%s reached %q, want %q", listen, got, want)
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("RunRelay after cancel: %v", err)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("RunRelay did not stop on cancel")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunRelayFailsWhenAListenerCannotBind(t *testing.T) {
|
||||
taken, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer taken.Close()
|
||||
err = RunRelay(t.Context(), map[string]string{taken.Addr().String(): "127.0.0.1:1", freeAddr(t): "127.0.0.1:1"})
|
||||
if err == nil {
|
||||
t.Fatal("RunRelay started with a listener that could not bind")
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
||||
)
|
||||
|
||||
// stepVocabularyProbe is a provider set of one, used only to prove the
|
||||
// manager a root builds is live: registering through it must reach its
|
||||
// vocabulary.
|
||||
type stepVocabularyProbe struct{}
|
||||
|
||||
func (stepVocabularyProbe) Name() string { return "probe" }
|
||||
|
||||
func (stepVocabularyProbe) StepTypes() []workflow.StepType {
|
||||
return []workflow.StepType{{Name: "probe.registered", Factory: func(yaml.Node) (workflow.Stage, error) {
|
||||
return workflow.Stage{Name: "probe.registered"}, nil
|
||||
}}}
|
||||
}
|
||||
|
||||
// TestProductionWorkerDependenciesRegisterTheSharedStepVocabulary pins the
|
||||
// executing side's composition root. productionWorkerDependencies is reached
|
||||
// only by Run, so nothing else fails if it stops registering the shared
|
||||
// provider set (internal/infrastructure/workflowsteps) and hands the worker a
|
||||
// manager with no shipped stage: every compile then fails at run time, in a
|
||||
// container, per task.
|
||||
func TestProductionWorkerDependenciesRegisterTheSharedStepVocabulary(t *testing.T) {
|
||||
dependencies, err := productionWorkerDependencies()
|
||||
if err != nil {
|
||||
t.Fatalf("productionWorkerDependencies: %v", err)
|
||||
}
|
||||
requireSharedStepVocabulary(t, dependencies.steps)
|
||||
}
|
||||
|
||||
// requireSharedStepVocabulary holds a composition root's manager to both halves
|
||||
// of the contract: it resolves the shipped vocabulary, and it is live enough to
|
||||
// take a provider registered through it.
|
||||
func requireSharedStepVocabulary(t *testing.T, steps *workflow.Manager) {
|
||||
t.Helper()
|
||||
if steps == nil {
|
||||
t.Fatal("the composition root registered no workflow step vocabulary")
|
||||
}
|
||||
got := steps.StepTypes()
|
||||
// Containment, not equality: adding a provider to the shared set
|
||||
// (workflowsteps.Providers) is the documented way a step type arrives, and
|
||||
// that must not have to edit this guard. What it holds is that the shipped
|
||||
// stages are all present, which is what a root regressing to
|
||||
// workflow.NewManager() loses.
|
||||
for name := range workflow.BuiltinStepRegistry() {
|
||||
if !slices.Contains(got, name) {
|
||||
t.Errorf("the composition root resolves %v, which is missing the shipped stage %q", got, name)
|
||||
}
|
||||
}
|
||||
// The other half is provenance: every name the root resolves must be
|
||||
// declared by the shared provider set, not by a near-identical provider of
|
||||
// the root's own, because that set is what ties this process's vocabulary to
|
||||
// the State Store that validates against it. It is content, not identity --
|
||||
// the shipped provider rebuilds its factories on every call.
|
||||
declared := sharedStepTypeNames()
|
||||
for _, name := range got {
|
||||
if !slices.Contains(declared, name) {
|
||||
t.Errorf("the composition root resolves %q, which no provider in the shared set declares", name)
|
||||
}
|
||||
}
|
||||
if err := steps.Register(stepVocabularyProbe{}); err != nil {
|
||||
t.Fatalf("register a provider through the root's manager: %v", err)
|
||||
}
|
||||
if _, resolved := steps.Registry()["probe.registered"]; !resolved {
|
||||
t.Fatal("a provider registered through the root's manager did not reach its vocabulary")
|
||||
}
|
||||
}
|
||||
|
||||
// sharedStepTypeNames returns the step types the shared provider set declares,
|
||||
// read through the same Providers() seam the composition roots register.
|
||||
func sharedStepTypeNames() []string {
|
||||
names := make([]string, 0)
|
||||
for _, provider := range workflowsteps.Providers() {
|
||||
for _, stepType := range provider.StepTypes() {
|
||||
names = append(names, stepType.Name)
|
||||
}
|
||||
}
|
||||
slices.Sort(names)
|
||||
return names
|
||||
}
|
||||
@@ -1,994 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
"github.com/nats-io/nats-server/v2/server"
|
||||
natssrv "github.com/nats-io/nats-server/v2/test"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
"github.com/samcharles93/archie-core/internal/worktreerpc"
|
||||
)
|
||||
|
||||
// testSteps builds the production workflow step vocabulary for tests that are
|
||||
// not about the vocabulary itself (the composition-level agreement test in
|
||||
// internal/app/controlplane is).
|
||||
func testSteps(t *testing.T) *workflow.Manager {
|
||||
t.Helper()
|
||||
steps, err := workflowsteps.NewManager()
|
||||
if err != nil {
|
||||
t.Fatalf("build workflow step vocabulary: %v", err)
|
||||
}
|
||||
return steps
|
||||
}
|
||||
|
||||
// ── regression: archie-core#520 -- worktree ownership must be
|
||||
// reconciled before push, since the agent commits as root inside the
|
||||
// container and the daemon reads those same loose objects as its own
|
||||
// non-root host user ──────────────────────────────────────────────────
|
||||
|
||||
type fakePushTrees struct {
|
||||
pushed bool
|
||||
pushErr error
|
||||
}
|
||||
|
||||
func (f *fakePushTrees) Push(context.Context) error {
|
||||
f.pushed = true
|
||||
return f.pushErr
|
||||
}
|
||||
|
||||
func TestWorktreeOwnerIDParsesEnvValue(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
env string
|
||||
want int
|
||||
}{
|
||||
{"unset", "", -1},
|
||||
{"zero is a valid uid (root)", "0", 0},
|
||||
{"typical non-root uid", "1000", 1000},
|
||||
{"garbage falls back to unset", "not-a-number", -1},
|
||||
{"negative falls back to unset", "-1", -1},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
if got := worktreeOwnerID(c.env); got != c.want {
|
||||
t.Errorf("worktreeOwnerID(%q) = %d, want %d", c.env, got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPushReconcilesOwnershipWhenConfigured chowns to the test
|
||||
// process's own uid/gid -- a permission no-op that succeeds without root --
|
||||
// so this proves the walk runs and still reaches the underlying push,
|
||||
// without needing privilege to chown to an arbitrary owner.
|
||||
func TestHybridTreesPushReconcilesOwnershipWhenConfigured(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "file.txt"), []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "sub", "nested.txt"), []byte("y"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fake := &fakePushTrees{}
|
||||
trees := &hybridTrees{
|
||||
push: fake,
|
||||
localDir: dir,
|
||||
worktreeUID: os.Getuid(),
|
||||
worktreeGID: os.Getgid(),
|
||||
}
|
||||
|
||||
if err := trees.Push(t.Context(), dir, "branch"); err != nil {
|
||||
t.Fatalf("Push: %v", err)
|
||||
}
|
||||
if !fake.pushed {
|
||||
t.Fatal("Push did not reach the underlying remote push")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPushReconcilesOwnershipWithDanglingSymlink: a repo can
|
||||
// legitimately track a symlink whose target doesn't exist inside the
|
||||
// container (e.g. a relative link to something outside the checkout, or to
|
||||
// a tool the image doesn't ship). Chowning must not follow it -- a
|
||||
// following chown fails on a dangling target, which would abort a push
|
||||
// that would otherwise have succeeded, a regression worse than the bug
|
||||
// being fixed here.
|
||||
func TestHybridTreesPushReconcilesOwnershipWithDanglingSymlink(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.Symlink(filepath.Join(dir, "does-not-exist"), filepath.Join(dir, "broken-link")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fake := &fakePushTrees{}
|
||||
trees := &hybridTrees{
|
||||
push: fake,
|
||||
localDir: dir,
|
||||
worktreeUID: os.Getuid(),
|
||||
worktreeGID: os.Getgid(),
|
||||
}
|
||||
|
||||
if err := trees.Push(t.Context(), dir, "branch"); err != nil {
|
||||
t.Fatalf("Push: %v", err)
|
||||
}
|
||||
if !fake.pushed {
|
||||
t.Fatal("Push did not reach the underlying remote push")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPushSkipsReconciliationWhenNotConfigured: an unset
|
||||
// WORKTREE_UID/GID (archie-core's non-container and test callers) must not
|
||||
// attempt any chown at all -- a nonexistent dir would otherwise fail the
|
||||
// walk, so this proves the walk is skipped entirely, not just tolerant of
|
||||
// this one input.
|
||||
func TestHybridTreesPushSkipsReconciliationWhenNotConfigured(t *testing.T) {
|
||||
fake := &fakePushTrees{}
|
||||
trees := &hybridTrees{
|
||||
push: fake,
|
||||
localDir: filepath.Join(t.TempDir(), "does-not-exist"),
|
||||
worktreeUID: -1,
|
||||
worktreeGID: -1,
|
||||
}
|
||||
|
||||
if err := trees.Push(t.Context(), trees.localDir, "branch"); err != nil {
|
||||
t.Fatalf("Push: %v", err)
|
||||
}
|
||||
if !fake.pushed {
|
||||
t.Fatal("Push did not reach the underlying remote push")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPrepareResolvesTheTargetBranch pins the container half of the
|
||||
// workflow prepare seam: the daemon has already positioned the bind-mounted
|
||||
// worktree, so the adapter resolves its directory and returns the requested
|
||||
// resume target verbatim -- the branch a remediation continues -- while a fresh
|
||||
// prepare returns the branch the daemon persisted on the task row.
|
||||
func TestHybridTreesPrepareResolvesTheTargetBranch(t *testing.T) {
|
||||
trees := &hybridTrees{localDir: "/work/issue-1", branch: "archie/fresh"}
|
||||
|
||||
dir, branch, err := trees.Prepare(t.Context(), "o", "r", "main", 1, "title", "", "", workflow.PrepareTarget("archie/pr-branch"))
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare(resume): %v", err)
|
||||
}
|
||||
if dir != "/work/issue-1" || branch != "archie/pr-branch" {
|
||||
t.Fatalf("Prepare(resume) = %q/%q, want /work/issue-1/archie/pr-branch", dir, branch)
|
||||
}
|
||||
|
||||
dir, branch, err = trees.Prepare(t.Context(), "o", "r", "main", 1, "title", "", "", workflow.PrepareFresh)
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare(fresh): %v", err)
|
||||
}
|
||||
if dir != "/work/issue-1" || branch != "archie/fresh" {
|
||||
t.Fatalf("Prepare(fresh) = %q/%q, want /work/issue-1/archie/fresh", dir, branch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHybridTreesCommitReconcilesOwnershipEvenWhenCommitFails(t *testing.T) {
|
||||
missing := filepath.Join(t.TempDir(), "does-not-exist")
|
||||
trees := &hybridTrees{
|
||||
local: &worktree.Manager{},
|
||||
localDir: missing,
|
||||
worktreeUID: os.Getuid(),
|
||||
worktreeGID: os.Getgid(),
|
||||
}
|
||||
|
||||
_, err := trees.CommitAll(t.Context(), missing, "test commit")
|
||||
if err == nil {
|
||||
t.Fatal("CommitAll error = nil, want commit and ownership reconciliation failure")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "reconcile worktree ownership after commit") {
|
||||
t.Fatalf("CommitAll error = %q, want ownership reconciliation context", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPushSurfacesOwnershipReconciliationFailure: a configured
|
||||
// but broken reconciliation (the dir push is asked to fix vanished) must
|
||||
// fail loudly rather than forward to a push that will only fail later with
|
||||
// a much more confusing git-internal permission error.
|
||||
func TestHybridTreesPushSurfacesOwnershipReconciliationFailure(t *testing.T) {
|
||||
fake := &fakePushTrees{}
|
||||
missing := filepath.Join(t.TempDir(), "does-not-exist")
|
||||
trees := &hybridTrees{
|
||||
push: fake,
|
||||
localDir: missing,
|
||||
worktreeUID: os.Getuid(),
|
||||
worktreeGID: os.Getgid(),
|
||||
}
|
||||
|
||||
err := trees.Push(t.Context(), missing, "branch")
|
||||
if err == nil {
|
||||
t.Fatal("expected an error reconciling ownership of a missing directory")
|
||||
}
|
||||
if fake.pushed {
|
||||
t.Fatal("Push must not reach the underlying remote push when reconciliation fails")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyToolLimitsWiresConfiguredPolicyIntoLoopRunner guards against
|
||||
// regressing archie-core's tool-result cap and spill dir back to inert
|
||||
// zero values on the worker path (agentexec-1 / tools-core-11): a
|
||||
// *LoopRunner built for a task must carry the daemon's configured
|
||||
// ToolPolicy, not the zero value NewLoopRunner leaves it at.
|
||||
func TestApplyToolLimitsWiresConfiguredPolicyIntoLoopRunner(t *testing.T) {
|
||||
runner := agentexec.NewLoopRunner(agentexec.NewRuntime(nil), slog.New(slog.DiscardHandler))
|
||||
policy := config.ToolPolicy{MaxResultChars: 12345, SpillDir: "/var/tmp/archie-spill"}
|
||||
|
||||
applyToolLimits(runner, policy, []string{"whois"})
|
||||
|
||||
want := agentexec.ToolLimits{MaxResultChars: 12345, SpillDir: "/var/tmp/archie-spill"}
|
||||
if runner.Limits != want {
|
||||
t.Fatalf("runner.Limits = %+v, want %+v", runner.Limits, want)
|
||||
}
|
||||
if len(runner.AllowTools) != 1 || runner.AllowTools[0] != "whois" {
|
||||
t.Fatalf("runner.AllowTools = %v, want the profile's allowlist", runner.AllowTools)
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyToolLimitsIgnoresNonLoopRunner confirms a test-fake runner (which
|
||||
// is not a *agentexec.LoopRunner) is left untouched rather than panicking.
|
||||
func TestApplyToolLimitsIgnoresNonLoopRunner(t *testing.T) {
|
||||
applyToolLimits(panicRunner{t}, config.ToolPolicy{MaxResultChars: 1}, nil)
|
||||
}
|
||||
|
||||
type panicRunner struct{ t *testing.T }
|
||||
|
||||
func (r panicRunner) Run(context.Context, string, agentexec.Request, agentexec.ToolCallReporter) (agentexec.Result, error) {
|
||||
r.t.Fatal("bootstrap workflow must not invoke an LLM runner")
|
||||
return agentexec.Result{}, nil
|
||||
}
|
||||
|
||||
type prCapturingForge struct {
|
||||
prs []struct{ owner, repo, title, head, base, body string }
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) Comment(context.Context, string, string, int, string) (int64, error) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) CloseIssue(context.Context, string, string, int, string) error { return nil }
|
||||
|
||||
func (f *prCapturingForge) CreatePR(_ context.Context, owner, repo, title, head, base, body string) (int, error) {
|
||||
f.prs = append(f.prs, struct{ owner, repo, title, head, base, body string }{owner, repo, title, head, base, body})
|
||||
return 5, nil
|
||||
}
|
||||
func (f *prCapturingForge) SetStateLabel(context.Context, string, string, int, string, []string) {}
|
||||
func (f *prCapturingForge) AcceptInvitations(context.Context) error { panic("unexpected call") }
|
||||
|
||||
func (f *prCapturingForge) AssignedIssues(context.Context, string, string, string) ([]forge.Issue, error) {
|
||||
panic("unexpected call")
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) IssuesWithLabel(context.Context, string, string, string) ([]forge.Issue, error) {
|
||||
panic("unexpected call")
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) PRState(context.Context, string, string, int) (string, error) {
|
||||
panic("unexpected call")
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) CreateIssue(context.Context, string, string, string, string, []string) (int, error) {
|
||||
panic("unexpected call")
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) React(context.Context, string, string, int, string) error {
|
||||
panic("unexpected call")
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) VerifyPush(context.Context, string, string) error {
|
||||
panic("unexpected call")
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) LinkBranch(context.Context, string, string, int, string) error { return nil }
|
||||
|
||||
func (f *prCapturingForge) CreateReviewComments(context.Context, string, string, int, string, []workflow.ReviewComment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *prCapturingForge) ReplyToReview(context.Context, string, string, int, int64, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type remoteManager struct {
|
||||
manager *worktree.Manager
|
||||
dir string
|
||||
branch string
|
||||
}
|
||||
|
||||
func (r *remoteManager) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string, target workflow.PrepareTarget) (string, string, error) {
|
||||
dir, branch, err := r.manager.Prepare(ctx, owner, repo, base, issue, title, body, labels, worktree.Target(target))
|
||||
if err == nil {
|
||||
r.dir = dir
|
||||
r.branch = branch
|
||||
}
|
||||
return dir, branch, err
|
||||
}
|
||||
|
||||
func (r *remoteManager) Push(ctx context.Context) error {
|
||||
return r.manager.Push(ctx, r.dir, r.branch)
|
||||
}
|
||||
|
||||
func newLocalRemote(t *testing.T, owner, repo string) string {
|
||||
t.Helper()
|
||||
host := t.TempDir()
|
||||
bare := filepath.Join(host, owner, repo+".git")
|
||||
seed := filepath.Join(t.TempDir(), "seed")
|
||||
run := func(dir string, args ...string) {
|
||||
t.Helper()
|
||||
cmd := exec.CommandContext(t.Context(), "git", args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("git %v: %v\n%s", args, err, out)
|
||||
}
|
||||
}
|
||||
if err := os.MkdirAll(bare, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run(host, "init", "--bare", "-b", "main", bare)
|
||||
if err := os.MkdirAll(seed, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run(seed, "init", "-b", "main")
|
||||
run(seed, "config", "user.name", "seeder")
|
||||
run(seed, "config", "user.email", "seed@example.com")
|
||||
run(seed, "config", "commit.gpgsign", "false")
|
||||
if err := os.WriteFile(filepath.Join(seed, "README.md"), []byte("seed\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run(seed, "add", "-A")
|
||||
run(seed, "commit", "-m", "seed")
|
||||
run(seed, "push", "file://"+bare, "main")
|
||||
return host
|
||||
}
|
||||
|
||||
// hostileSigningConfig writes a global git config that makes every commit
|
||||
// fail unless the fixture disables signing in the repository's own config.
|
||||
func hostileSigningConfig(t *testing.T) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "hostile.gitconfig")
|
||||
const cfg = "[user]\n\tname = Hostile\n\temail = hostile@example.com\n\tsigningkey = /nonexistent/signing-key\n[gpg]\n\tformat = ssh\n[commit]\n\tgpgsign = true\n"
|
||||
if err := os.WriteFile(path, []byte(cfg), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// TestNewLocalRemoteIgnoresHostSigningConfig pins the fixture's hermeticity:
|
||||
// it must not depend on the developer's global git identity or a signing agent.
|
||||
func TestNewLocalRemoteIgnoresHostSigningConfig(t *testing.T) {
|
||||
t.Setenv("GIT_CONFIG_GLOBAL", hostileSigningConfig(t))
|
||||
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
||||
newLocalRemote(t, "acme", "todo")
|
||||
}
|
||||
|
||||
func startEmbeddedTaskRPCServer(t *testing.T) *server.Server {
|
||||
t.Helper()
|
||||
srv := natssrv.RunRandClientPortServer()
|
||||
t.Cleanup(srv.Shutdown)
|
||||
if err := srv.EnableJetStream(&server.JetStreamConfig{StoreDir: t.TempDir()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return srv
|
||||
}
|
||||
|
||||
func connectTaskRPC(t *testing.T, url string) *natsio.Conn {
|
||||
t.Helper()
|
||||
connection, err := natsio.Connect(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(connection.Close)
|
||||
return connection
|
||||
}
|
||||
|
||||
// startStateStoreGRPC serves the State Store gRPC contract over a loopback
|
||||
// TCP listener fronting local, returning the dial address. The agent is fully
|
||||
// on gRPC for its store calls (docs/prds/state-store-contract.md §12 step 4),
|
||||
// so an agentworker test that drives executeTaskRequest needs a real State
|
||||
// Store server rather than the deleted NATS storerpc path.
|
||||
func startStateStoreGRPC(t *testing.T, local *pgstore.TaskDB) string {
|
||||
t.Helper()
|
||||
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := grpc.NewServer()
|
||||
eda := pgstore.EDA(t, nil)
|
||||
staterpc.RegisterServer(server, staterpc.Deps{
|
||||
Tasks: local, Captures: eda, Mappings: eda, Bindings: eda,
|
||||
BindingDispatcher: eda, BindingTaskCreator: local, WorkflowCalls: local,
|
||||
Steps: local, Canceller: local,
|
||||
})
|
||||
go func() { _ = server.Serve(listener) }()
|
||||
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
|
||||
return listener.Addr().String()
|
||||
}
|
||||
|
||||
func TestRunTaskExecutesBootstrapWorkflowEndToEnd(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not installed")
|
||||
}
|
||||
ctx := t.Context()
|
||||
host := newLocalRemote(t, "acme", "widget")
|
||||
st := pgstore.Open(t)
|
||||
if _, err := st.EnqueueIssue(ctx, "acme", "widget", 1, "feat: bootstrap test", "", "bootstrap", ""); err != nil {
|
||||
t.Fatalf("enqueue: %v", err)
|
||||
}
|
||||
task, err := st.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim: (%v, %v)", task, err)
|
||||
}
|
||||
fg := &prCapturingForge{}
|
||||
manager := &worktree.Manager{WorkDir: t.TempDir(), Token: "unused", BotUser: "archie-bot", BotEmail: "archie-bot@example.com", BaseURL: "file://" + host}
|
||||
remote := &remoteManager{manager: manager}
|
||||
hostDir, _, err := remote.Prepare(ctx, "acme", "widget", "main", 1, task.Title, "", "bootstrap", workflow.PrepareFresh)
|
||||
if err != nil {
|
||||
t.Fatalf("prepare: %v", err)
|
||||
}
|
||||
req := taskrun.Request{
|
||||
Task: task,
|
||||
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
|
||||
Cfg: config.Config{BotUser: "archie-bot", BotEmail: "archie-bot@example.com"}.ForTask(),
|
||||
}
|
||||
fakeRunner := runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner { return panicRunner{t} })
|
||||
response, err := runTask(ctx, req, taskDependencies{forge: fg, store: st, trees: remote, steps: testSteps(t)}, fakeRunner, hostDir, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatalf("runTask: %v", err)
|
||||
}
|
||||
if response.Status != workflow.StatusPROpen || len(fg.prs) != 1 {
|
||||
t.Fatalf("response status/PRs = (%q, %d), want (%q, 1)", response.Status, len(fg.prs), workflow.StatusPROpen)
|
||||
}
|
||||
stored, err := st.TaskByIssue(ctx, "acme", "widget", 1)
|
||||
if err != nil || stored == nil || stored.Status != workflow.StatusPROpen || stored.PRNumber != 5 {
|
||||
t.Fatalf("stored task = (%+v, %v), want PR-open task #5", stored, err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(hostDir, ".archie", "bootstrap.md")); err != nil {
|
||||
t.Fatalf("expected bootstrap marker file: %v", err)
|
||||
}
|
||||
repo, err := git.PlainOpen(hostDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
head, err := repo.Head()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
commit, err := repo.CommitObject(head.Hash())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if commit.Author.Name != "archie-bot" || commit.Author.Email != "archie-bot@example.com" ||
|
||||
commit.Committer.Name != "archie-bot" || commit.Committer.Email != "archie-bot@example.com" {
|
||||
t.Fatalf("commit identity = author %q <%s>, committer %q <%s>",
|
||||
commit.Author.Name, commit.Author.Email, commit.Committer.Name, commit.Committer.Email)
|
||||
}
|
||||
|
||||
// The change the run produced is captured at the push, through the
|
||||
// container's hybridTrees, and persisted as durable provenance attributed
|
||||
// to the attempt -- and captured once more by OpenPR, the first moment a
|
||||
// capture can carry the pull-request number the view links. This is the
|
||||
// only end-to-end path that matters: every production workflow.Run executes
|
||||
// here.
|
||||
timeline, err := st.TaskEvents(ctx, task.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskEvents: %v", err)
|
||||
}
|
||||
var captured []events.Event
|
||||
for _, e := range timeline {
|
||||
if e.Kind == events.KindChangesCaptured {
|
||||
captured = append(captured, e)
|
||||
}
|
||||
}
|
||||
if len(captured) != 2 {
|
||||
t.Fatalf("changes_captured events = %d, want the push capture and the one OpenPR takes (%d)", len(captured), stored.PRNumber)
|
||||
}
|
||||
for i, e := range captured {
|
||||
if e.Attempt != task.Attempt || task.Attempt == 0 {
|
||||
t.Errorf("capture %d attempt = %d, want the task's own attempt %d", i, e.Attempt, task.Attempt)
|
||||
}
|
||||
}
|
||||
if captured[0].Stage != "commit-push" {
|
||||
t.Errorf("capture stage = %q, want the capturing stage", captured[0].Stage)
|
||||
}
|
||||
data, err := json.Marshal(captured[0].Data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var payload struct {
|
||||
CapturedAfter string `json:"captured_after"`
|
||||
PRNumber int `json:"pr_number"`
|
||||
Files []struct {
|
||||
Path string `json:"path"`
|
||||
Status string `json:"status"`
|
||||
} `json:"files"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
t.Fatalf("decode capture: %v", err)
|
||||
}
|
||||
if payload.CapturedAfter != "commit-push" {
|
||||
t.Errorf("captured_after = %q, want commit-push", payload.CapturedAfter)
|
||||
}
|
||||
if payload.PRNumber != 0 {
|
||||
t.Errorf("the push capture carries pr_number %d, want 0: no PR existed when it was taken", payload.PRNumber)
|
||||
}
|
||||
if len(payload.Files) != 1 || payload.Files[0].Path != ".archie/bootstrap.md" || payload.Files[0].Status != "added" {
|
||||
t.Errorf("captured files = %+v, want the bootstrap marker as an added file", payload.Files)
|
||||
}
|
||||
|
||||
finalData, err := json.Marshal(captured[1].Data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var final struct {
|
||||
CapturedAfter string `json:"captured_after"`
|
||||
PRNumber int `json:"pr_number"`
|
||||
}
|
||||
if err := json.Unmarshal(finalData, &final); err != nil {
|
||||
t.Fatalf("decode the final capture: %v", err)
|
||||
}
|
||||
if final.CapturedAfter != "open-pr" || final.PRNumber != stored.PRNumber || final.PRNumber == 0 {
|
||||
t.Errorf("final capture = %q pr %d, want open-pr carrying the stored PR number %d, without which the changed-files view has nothing to link",
|
||||
final.CapturedAfter, final.PRNumber, stored.PRNumber)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTaskRestoresWorktreeOwnershipOnEveryExit pins the half of the
|
||||
// worktree-ownership contract that archie-core#520 left open. CommitAll and
|
||||
// Push hand the worktree back to the daemon's own UID on the SUCCESS path;
|
||||
// every other way a container run can end -- a gate failure that parks the
|
||||
// task, a stage error, an early return -- used to leave the worktree owned by
|
||||
// root, because the agent runs as root inside the container. The daemon then
|
||||
// could not clean or reset those paths on the next attempt (a root-owned
|
||||
// directory cannot be unlinked from by its non-root owner), so a retry failed
|
||||
// before it could start no matter what the worktree refresh did.
|
||||
//
|
||||
// The run below exits early, which is the point: the restore has to be
|
||||
// deferred over the whole run rather than attached to the paths that happen to
|
||||
// commit. It is observed through a supplementary group -- a non-root process
|
||||
// may move a file it owns into a group it is a member of, so this proves the
|
||||
// chown ran without needing root.
|
||||
func TestRunTaskRestoresWorktreeOwnershipOnEveryExit(t *testing.T) {
|
||||
probeGroup := supplementaryGroup(t)
|
||||
workDir := t.TempDir()
|
||||
probe := filepath.Join(workDir, "probe.txt")
|
||||
if err := os.WriteFile(probe, []byte("x\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Setenv("WORKTREE_UID", strconv.Itoa(os.Getuid()))
|
||||
t.Setenv("WORKTREE_GID", strconv.Itoa(probeGroup))
|
||||
|
||||
// A runner factory that yields nothing fails the run after the trees (and
|
||||
// the ownership defer) are already in place.
|
||||
_, err := runTask(t.Context(), taskrun.Request{
|
||||
Task: &workflow.Task{ID: 77, Branch: "feat/77-probe", Workflow: "implement"},
|
||||
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
|
||||
}, taskDependencies{steps: testSteps(t)}, runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner {
|
||||
return nil
|
||||
}), workDir, slog.New(slog.DiscardHandler))
|
||||
if err == nil {
|
||||
t.Fatal("runTask error = nil, want the missing-runner failure this exit is built around")
|
||||
}
|
||||
|
||||
info, statErr := os.Stat(probe)
|
||||
if statErr != nil {
|
||||
t.Fatal(statErr)
|
||||
}
|
||||
gid, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
t.Fatal("no syscall.Stat_t for the ownership probe")
|
||||
}
|
||||
if int(gid.Gid) != probeGroup {
|
||||
t.Errorf("probe.txt gid = %d, want %d -- a run that never reached CommitAll or Push left the worktree owned by the container's own user", gid.Gid, probeGroup)
|
||||
}
|
||||
}
|
||||
|
||||
// supplementaryGroup returns a group this process is a member of but that is
|
||||
// not its primary gid, so a test can observe a chown a non-root process is
|
||||
// allowed to make.
|
||||
func supplementaryGroup(t *testing.T) int {
|
||||
t.Helper()
|
||||
groups, err := os.Getgroups()
|
||||
if err != nil {
|
||||
t.Skipf("os.Getgroups: %v", err)
|
||||
}
|
||||
for _, gid := range groups {
|
||||
if gid != os.Getgid() {
|
||||
return gid
|
||||
}
|
||||
}
|
||||
t.Skip("process belongs to no supplementary group to chown into")
|
||||
return 0
|
||||
}
|
||||
|
||||
// TestExecuteTaskRequestUsesInfrastructureRPCDependencies preserves the
|
||||
// application-to-infrastructure proof: the application asks Transport for its
|
||||
// forge/store/tree contracts, and those concrete clients cross embedded NATS
|
||||
// to daemon-side RPC servers before the bootstrap workflow can complete.
|
||||
func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not installed")
|
||||
}
|
||||
ctx := t.Context()
|
||||
host := newLocalRemote(t, "acme", "rpc-widget")
|
||||
st := pgstore.Open(t)
|
||||
if _, err := st.EnqueueIssue(ctx, "acme", "rpc-widget", 2, "feat: RPC bootstrap test", "", "bootstrap", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := st.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim = (%+v, %v)", task, err)
|
||||
}
|
||||
forge := &prCapturingForge{}
|
||||
daemonTrees := &worktree.Manager{
|
||||
WorkDir: t.TempDir(),
|
||||
Token: "unused-for-file-remotes",
|
||||
BotUser: "archie-bot",
|
||||
BotEmail: "archie-bot@example.com",
|
||||
BaseURL: "file://" + host,
|
||||
}
|
||||
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "rpc-widget", "main", 2, task.Title, "", "bootstrap", worktree.Fresh)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task.Branch = branch
|
||||
grants := worktreerpc.NewGrants()
|
||||
grant, revokeGrant, err := grants.Issue(task)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(revokeGrant)
|
||||
|
||||
srv := startEmbeddedTaskRPCServer(t)
|
||||
serverConn := connectTaskRPC(t, srv.ClientURL())
|
||||
unsubForge, err := (&forgerpc.Server{Forge: forge, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(unsubForge)
|
||||
unsubTrees, err := (&worktreerpc.Server{Trees: daemonTrees, Grants: grants, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(unsubTrees)
|
||||
|
||||
transport, err := agentnats.Connect(ctx, agentnats.Config{URL: srv.ClientURL(), StateStoreURL: startStateStoreGRPC(t, st)}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(transport.Close)
|
||||
|
||||
request := taskrun.Request{
|
||||
Task: task,
|
||||
Repo: config.Repo{Owner: "acme", Name: "rpc-widget", Base: "main"},
|
||||
Cfg: config.Config{}.ForTask(),
|
||||
WorktreeGrant: grant,
|
||||
}
|
||||
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler), testSteps(t), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Status != workflow.StatusPROpen || len(forge.prs) != 1 {
|
||||
t.Fatalf("response status/PRs = (%q, %d), want (%q, 1)", response.Status, len(forge.prs), workflow.StatusPROpen)
|
||||
}
|
||||
// archie-core-1786751948782-3-837a8fd0 point 4: every response reports
|
||||
// what this worker actually is, since it's the only channel the daemon
|
||||
// has to learn what's really running (see daemon.AgentStatus).
|
||||
if response.AgentVersion != Version() {
|
||||
t.Errorf("response.AgentVersion = %q, want Version() = %q", response.AgentVersion, Version())
|
||||
}
|
||||
if response.AgentInstallType != installtype.Type() {
|
||||
t.Errorf("response.AgentInstallType = %q, want installtype.Type() = %q", response.AgentInstallType, installtype.Type())
|
||||
}
|
||||
stored, err := st.TaskByIssue(ctx, "acme", "rpc-widget", 2)
|
||||
if err != nil || stored == nil || stored.Status != workflow.StatusPROpen || stored.PRNumber != 5 {
|
||||
t.Fatalf("stored task = (%+v, %v), want RPC-persisted PR-open task #5", stored, err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(hostDir, ".archie", "bootstrap.md")); err != nil {
|
||||
t.Fatalf("expected bootstrap marker file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestExecuteTaskRequestForwardsWorkflowEventsOverNATS is the regression test
|
||||
// for the "No events yet" dashboard bug (archie-core-518): a workflow run
|
||||
// through the NATS/container path publishes its stage/outcome events to an
|
||||
// in-process *events.Bus nothing on the daemon side could see, because
|
||||
// runTask never wired TaskContext.Bus. This proves the fix end to end -- a
|
||||
// bystander subscribed directly to SubjectForEvents(task.ID) on the same
|
||||
// embedded NATS server executeTaskRequest uses receives real events the
|
||||
// bootstrap workflow emits, exactly as the daemon's own subscriber would.
|
||||
func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not installed")
|
||||
}
|
||||
ctx := t.Context()
|
||||
host := newLocalRemote(t, "acme", "events-widget")
|
||||
st := pgstore.Open(t)
|
||||
if _, err := st.EnqueueIssue(ctx, "acme", "events-widget", 3, "feat: events bootstrap test", "", "bootstrap", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := st.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim = (%+v, %v)", task, err)
|
||||
}
|
||||
forge := &prCapturingForge{}
|
||||
daemonTrees := &worktree.Manager{
|
||||
WorkDir: t.TempDir(),
|
||||
Token: "unused-for-file-remotes",
|
||||
BotUser: "archie-bot",
|
||||
BotEmail: "archie-bot@example.com",
|
||||
BaseURL: "file://" + host,
|
||||
}
|
||||
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "events-widget", "main", 3, task.Title, "", "bootstrap", worktree.Fresh)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task.Branch = branch
|
||||
grants := worktreerpc.NewGrants()
|
||||
grant, revokeGrant, err := grants.Issue(task)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(revokeGrant)
|
||||
|
||||
srv := startEmbeddedTaskRPCServer(t)
|
||||
serverConn := connectTaskRPC(t, srv.ClientURL())
|
||||
unsubForge, err := (&forgerpc.Server{Forge: forge, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(unsubForge)
|
||||
unsubTrees, err := (&worktreerpc.Server{Trees: daemonTrees, Grants: grants, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(unsubTrees)
|
||||
|
||||
// Stand in for the daemon's own SubjectEventsWildcard subscriber: bind
|
||||
// to the exact per-task subject before the workflow runs, so nothing
|
||||
// published during the run can be missed.
|
||||
var (
|
||||
mu sync.Mutex
|
||||
received []events.Event
|
||||
)
|
||||
bystander := connectTaskRPC(t, srv.ClientURL())
|
||||
eventSub, err := bystander.Subscribe(agentexec.SubjectForEvents(task.ID), func(msg *natsio.Msg) {
|
||||
var e events.Event
|
||||
if err := json.Unmarshal(msg.Data, &e); err != nil {
|
||||
t.Errorf("undecodable event message: %v", err)
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
received = append(received, e)
|
||||
mu.Unlock()
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = eventSub.Unsubscribe() })
|
||||
if err := bystander.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
transport, err := agentnats.Connect(ctx, agentnats.Config{URL: srv.ClientURL(), StateStoreURL: startStateStoreGRPC(t, st)}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(transport.Close)
|
||||
|
||||
request := taskrun.Request{
|
||||
Task: task,
|
||||
Repo: config.Repo{Owner: "acme", Name: "events-widget", Base: "main"},
|
||||
Cfg: config.Config{}.ForTask(),
|
||||
WorktreeGrant: grant,
|
||||
}
|
||||
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler), testSteps(t), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Status != workflow.StatusPROpen {
|
||||
t.Fatalf("response status = %q, want %q", response.Status, workflow.StatusPROpen)
|
||||
}
|
||||
|
||||
deadline := time.After(2 * time.Second)
|
||||
for {
|
||||
mu.Lock()
|
||||
sawOutcome := false
|
||||
for _, e := range received {
|
||||
if e.Kind == events.KindOutcome {
|
||||
sawOutcome = true
|
||||
break
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
if sawOutcome {
|
||||
break
|
||||
}
|
||||
select {
|
||||
case <-deadline:
|
||||
t.Fatal("no \"outcome\" event arrived on SubjectForEvents(task.ID) within 2s")
|
||||
case <-time.After(10 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
sawOutcome := false
|
||||
for _, e := range received {
|
||||
if e.TaskID != task.ID {
|
||||
t.Errorf("event %+v carries TaskID %d, want %d", e, e.TaskID, task.ID)
|
||||
}
|
||||
if e.Kind == events.KindOutcome {
|
||||
sawOutcome = true
|
||||
}
|
||||
}
|
||||
if !sawOutcome {
|
||||
t.Errorf("received %d events %+v, want at least one %q", len(received), received, events.KindOutcome)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouteTaskUsesRequestKindOverride is the regression for
|
||||
// workflow-engine-1: a kind -> workflow override loaded by the daemon is
|
||||
// carried in the taskrun request and applied by the worker process, so
|
||||
// workflow.Route honours it instead of silently falling back to the
|
||||
// built-in default (bug -> tdd).
|
||||
func TestRouteTaskUsesRequestKindOverride(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
workflow.SetKindWorkflows(nil)
|
||||
workflow.SetLabelWorkflows(nil)
|
||||
})
|
||||
|
||||
registry := workflow.Registry{
|
||||
"tdd": workflow.Workflow{Name: "tdd"},
|
||||
"bootstrap": workflow.Workflow{Name: "bootstrap"},
|
||||
"implement": workflow.Workflow{Name: "implement"},
|
||||
}
|
||||
req := taskrun.Request{
|
||||
Task: &workflow.Task{Labels: "bug"},
|
||||
KindWorkflows: workflow.KindWorkflows{workintake.KindBug: "bootstrap"},
|
||||
}
|
||||
|
||||
wf := routeTask(req, registry)
|
||||
if wf.Name != "bootstrap" {
|
||||
t.Fatalf("routeTask() = %q, want %q (request kind override not applied)", wf.Name, "bootstrap")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouteTaskUsesRequestLabelOverride is the label counterpart: an
|
||||
// arbitrary-label binding carried in the request also reaches the worker's
|
||||
// routing decision.
|
||||
func TestRouteTaskUsesRequestLabelOverride(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
workflow.SetKindWorkflows(nil)
|
||||
workflow.SetLabelWorkflows(nil)
|
||||
})
|
||||
|
||||
registry := workflow.Registry{
|
||||
"implement": workflow.Workflow{Name: "implement"},
|
||||
"security-review": workflow.Workflow{Name: "security-review"},
|
||||
}
|
||||
req := taskrun.Request{
|
||||
Task: &workflow.Task{Labels: "security"},
|
||||
LabelWorkflows: workflow.LabelWorkflows{"security": "security-review"},
|
||||
}
|
||||
|
||||
wf := routeTask(req, registry)
|
||||
if wf.Name != "security-review" {
|
||||
t.Fatalf("routeTask() = %q, want %q (request label override not applied)", wf.Name, "security-review")
|
||||
}
|
||||
}
|
||||
|
||||
// changeStatsReader mirrors the unexported optional interface package workflow
|
||||
// type-asserts on TaskContext.Trees to capture what an attempt changed. It is
|
||||
// restated here because an unexported interface cannot be named from this
|
||||
// package; the assertion below is therefore the only way this side can pin
|
||||
// that the in-container Trees is capable at all.
|
||||
type changeStatsReader interface {
|
||||
ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error)
|
||||
}
|
||||
|
||||
// TestHybridTreesCanReportWhatChanged is the in-container half of the change
|
||||
// capture. Every production workflow.Run executes in archie-agent against a
|
||||
// hybridTrees, so a Trees without this capability would make the capture a
|
||||
// silent no-op in exactly the deployment that produces all the real data --
|
||||
// the daemon-side manager being capable would prove nothing.
|
||||
func TestHybridTreesCanReportWhatChanged(t *testing.T) {
|
||||
var trees any = &hybridTrees{local: &worktree.Manager{}}
|
||||
if _, ok := trees.(changeStatsReader); !ok {
|
||||
t.Fatal("hybridTrees cannot report a diffstat; every capture inside a container would silently no-op")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesForwardsChangedFileStatsToItsLocalManager drives the
|
||||
// capability against a real worktree, so satisfaction of the interface is not
|
||||
// mistaken for a working forwarder.
|
||||
func TestHybridTreesForwardsChangedFileStatsToItsLocalManager(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not installed")
|
||||
}
|
||||
ctx := t.Context()
|
||||
host := newLocalRemote(t, "acme", "diffstat")
|
||||
manager := &worktree.Manager{
|
||||
WorkDir: t.TempDir(), Token: "unused",
|
||||
BotUser: "archie-bot", BotEmail: "archie-bot@example.com", BaseURL: "file://" + host,
|
||||
}
|
||||
dir, branch, err := manager.Prepare(ctx, "acme", "diffstat", "main", 4, "feat: widget", "", "", worktree.Fresh)
|
||||
if err != nil {
|
||||
t.Fatalf("prepare: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "widget.txt"), []byte("one\ntwo\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := manager.CommitAll(ctx, dir, "feat: widget"); err != nil {
|
||||
t.Fatalf("commit: %v", err)
|
||||
}
|
||||
|
||||
trees := &hybridTrees{local: manager, localDir: dir, branch: branch}
|
||||
stats, err := trees.ChangedFileStats(ctx, dir, "main")
|
||||
if err != nil {
|
||||
t.Fatalf("ChangedFileStats: %v", err)
|
||||
}
|
||||
if len(stats.Files) != 1 || stats.Files[0].Path != "widget.txt" {
|
||||
t.Fatalf("files = %+v, want the one committed file", stats.Files)
|
||||
}
|
||||
if stats.Totals != (task.ChangeTotals{Files: 1, Additions: 2}) {
|
||||
t.Errorf("totals = %+v, want the file's two added lines", stats.Totals)
|
||||
}
|
||||
if stats.HeadSHA == "" || stats.BaseSHA == "" {
|
||||
t.Errorf("SHAs = (%q, %q), want both resolved", stats.BaseSHA, stats.HeadSHA)
|
||||
}
|
||||
}
|
||||
|
||||
// uncommittedChangeReporter mirrors the unexported optional interface package
|
||||
// workflow type-asserts on TaskContext.Trees to tell a change that is not
|
||||
// committed yet from a branch with no change. It is restated here for the same
|
||||
// reason changeStatsReader is: an unexported interface cannot be named from this
|
||||
// package, so this assertion is the only way this side can pin that the
|
||||
// in-container Trees can answer at all.
|
||||
type uncommittedChangeReporter interface {
|
||||
HasUncommittedChanges(ctx context.Context, dir string) (bool, error)
|
||||
}
|
||||
|
||||
// TestHybridTreesCanReportUncommittedWork is the in-container half of the
|
||||
// diff-rules placement guard. Every production workflow.Run executes in
|
||||
// archie-agent against a hybridTrees, and a workflow whose only Trees cannot
|
||||
// answer would fail the guard for the wrong reason -- or, worse, look capable
|
||||
// while forwarding nothing.
|
||||
func TestHybridTreesCanReportUncommittedWork(t *testing.T) {
|
||||
var trees any = &hybridTrees{local: &worktree.Manager{}}
|
||||
if _, ok := trees.(uncommittedChangeReporter); !ok {
|
||||
t.Fatal("hybridTrees cannot report uncommitted work, so the in-container diff-rules guard would refuse every run")
|
||||
}
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
)
|
||||
|
||||
// stubRunner is the agent runner that lets a test workflow end in an
|
||||
// agent.run tail without an LLM: the call step under test needs no agent,
|
||||
// but the workflow must still end with an outcome.
|
||||
type stubRunner struct{}
|
||||
|
||||
func (stubRunner) Run(_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
|
||||
return agentexec.Result{
|
||||
Version: agentexec.ProtocolVersion, TaskID: req.TaskID, Attempt: req.Attempt,
|
||||
Stage: req.Stage, Status: agentexec.StatusPassed, Summary: "reported",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// fakeScratchTrees is the Trees for a scratch task: the workspace already
|
||||
// exists (the call step needs none, the agent.run tail works in it), and
|
||||
// nothing publishes.
|
||||
type fakeScratchTrees struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
func (f *fakeScratchTrees) Prepare(context.Context, string, string, string, int, string, string, string, workflow.PrepareTarget) (string, string, error) {
|
||||
return f.dir, "", nil
|
||||
}
|
||||
|
||||
func (*fakeScratchTrees) CommitAll(context.Context, string, string) (bool, error) { return false, nil }
|
||||
func (*fakeScratchTrees) Push(context.Context) error { return nil }
|
||||
func (*fakeScratchTrees) Diff(context.Context, string, string) (string, error) { return "", nil }
|
||||
func (*fakeScratchTrees) ChangedFiles(context.Context, string, string) ([]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*fakeScratchTrees) ChangedLines(context.Context, string, string) (int, error) { return 0, nil }
|
||||
func (*fakeScratchTrees) Snapshot(context.Context, string, string) error { return nil }
|
||||
|
||||
// TestExecuteTaskRequestStartsWorkflowCallCallee proves the engine's
|
||||
// task.Caller wiring end to end: a run of a repository-none workflow carrying
|
||||
// one workflow.call step starts a real callee task row through the same
|
||||
// State Store gRPC contract the production worker dials
|
||||
// (docs/prds/workflow-calls.md), and the caller carries on to its tail.
|
||||
func TestExecuteTaskRequestStartsWorkflowCallCallee(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
st := pgstore.Open(t)
|
||||
|
||||
const callerYAML = "id: caller\nrepository: none\ninputs:\n src_ip: {type: string}\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n inputs: {src_ip: \"inputs.src_ip\"}\n wait: false\n - type: agent.run\n settings:\n mission: report\n"
|
||||
// A real caller row, claimed and running, so the store's enqueue reads
|
||||
// the same row shape production does.
|
||||
task, err := st.EnqueueChatTask(ctx, "", "", "workflow.call integration", "", "caller", "", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("seed caller task: %v", err)
|
||||
}
|
||||
if err := st.Transition(ctx, task.ID, task.Status, workflow.StatusRunning, "started"); err != nil {
|
||||
t.Fatalf("claim caller: %v", err)
|
||||
}
|
||||
task.Status = workflow.StatusRunning
|
||||
task.Inputs = map[string]any{"src_ip": "10.0.0.9"}
|
||||
task.WorkflowDefinitionYAML = callerYAML
|
||||
task.WorkflowDefinitionDigest = workflow.DigestDefinition(callerYAML)
|
||||
if err := st.Update(ctx, task); err != nil {
|
||||
t.Fatalf("seed caller task: %v", err)
|
||||
}
|
||||
|
||||
natsSrv := startEmbeddedTaskRPCServer(t)
|
||||
transport, err := agentnats.Connect(ctx, agentnats.Config{
|
||||
URL: natsSrv.ClientURL(),
|
||||
StateStoreURL: startStateStoreGRPC(t, st),
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(transport.Close)
|
||||
|
||||
workDir := t.TempDir()
|
||||
request := taskrun.Request{
|
||||
Task: task,
|
||||
Repo: config.Repo{},
|
||||
Cfg: config.Config{Models: map[string]string{"builder": "p/m"}}.ForTask(),
|
||||
WorkflowDefinition: callerYAML,
|
||||
}
|
||||
dependencies := taskDependencies{
|
||||
store: transport.Store(rpcTimeout),
|
||||
calls: transport.Calls(rpcTimeout),
|
||||
trees: &fakeScratchTrees{dir: workDir},
|
||||
steps: testSteps(t),
|
||||
}
|
||||
fakeRunner := runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner { return stubRunner{} })
|
||||
response, err := runTask(ctx, request, dependencies, fakeRunner, workDir, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Status != workflow.StatusCompleted {
|
||||
t.Fatalf("response status = %q (park reason %q), want completed", response.Status, response.Task.ParkReason)
|
||||
}
|
||||
|
||||
callee := calleeTaskOf(t, ctx, st, task.ID)
|
||||
if callee.Workflow != "callee" || callee.CallParentTaskID != task.ID || callee.CallDepth != 1 {
|
||||
t.Fatalf("callee = %+v, want workflow callee linked to task %d at depth 1", callee, task.ID)
|
||||
}
|
||||
if callee.Inputs["src_ip"] != "10.0.0.9" {
|
||||
t.Fatalf("callee inputs = %+v, want the call's resolved reference", callee.Inputs)
|
||||
}
|
||||
if callee.Org != org.OrgID("default") {
|
||||
t.Fatalf("callee org = %q, want the caller's %q", callee.Org, task.Org)
|
||||
}
|
||||
}
|
||||
|
||||
// calleeTaskOf reads the one callee task whose call_parent_task_id is
|
||||
// callerTaskID, straight off the pool the test owns.
|
||||
func calleeTaskOf(t *testing.T, ctx context.Context, st *pgstore.TaskDB, callerTaskID int64) *workflow.Task {
|
||||
t.Helper()
|
||||
rows, err := st.Pool.Query(ctx,
|
||||
`SELECT id, workflow, call_parent_task_id, call_depth, org_id, inputs
|
||||
FROM tasks WHERE call_parent_task_id = $1`, callerTaskID)
|
||||
if err != nil {
|
||||
t.Fatalf("callee query: %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var callees []*workflow.Task
|
||||
for rows.Next() {
|
||||
var id, parent, depth int64
|
||||
var wf, orgID, inputs string
|
||||
if err := rows.Scan(&id, &wf, &parent, &depth, &orgID, &inputs); err != nil {
|
||||
t.Fatalf("callee scan: %v", err)
|
||||
}
|
||||
decoded, _ := workflowtask.DecodeInputs(inputs)
|
||||
callees = append(callees, &workflow.Task{
|
||||
ID: id, Workflow: wf, CallParentTaskID: parent, CallDepth: int(depth),
|
||||
Org: org.OrgID(orgID), Inputs: decoded,
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
t.Fatalf("callee rows: %v", err)
|
||||
}
|
||||
if len(callees) != 1 {
|
||||
t.Fatalf("callee tasks of %d = %+v, want exactly one", callerTaskID, callees)
|
||||
}
|
||||
return callees[0]
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestVersionReportsDevWhenUnstamped guards the unstamped-build fallback:
|
||||
// an empty ldflags value (e.g. a local `go build`, or a release pipeline
|
||||
// that forgot -X for this package) must read as "dev", the same
|
||||
// placeholder cmd/archied's own version vars use, not as an empty string
|
||||
// that would silently sail through daemon.AgentStatus as a real version.
|
||||
func TestVersionReportsDevWhenUnstamped(t *testing.T) {
|
||||
orig := version
|
||||
t.Cleanup(func() { version = orig })
|
||||
version = ""
|
||||
|
||||
if got := Version(); got != "dev" {
|
||||
t.Errorf("Version() = %q, want %q", got, "dev")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionReportsStampedValue(t *testing.T) {
|
||||
orig := version
|
||||
t.Cleanup(func() { version = orig })
|
||||
version = "1.9.11"
|
||||
|
||||
if got := Version(); got != "1.9.11" {
|
||||
t.Errorf("Version() = %q, want %q", got, "1.9.11")
|
||||
}
|
||||
}
|
||||
@@ -1,233 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
||||
)
|
||||
|
||||
type workerPublisher struct{ messages *[][]byte }
|
||||
|
||||
func (p workerPublisher) Publish(_ string, data []byte) error {
|
||||
if p.messages != nil {
|
||||
*p.messages = append(*p.messages, append([]byte(nil), data...))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type workerSubscription struct {
|
||||
close func()
|
||||
closeErr error
|
||||
}
|
||||
|
||||
func (s workerSubscription) Close() error {
|
||||
s.close()
|
||||
return s.closeErr
|
||||
}
|
||||
|
||||
type workerTransportStub struct {
|
||||
events *[]string
|
||||
subscribeErr error
|
||||
subCloseErr error
|
||||
publisher agentexec.LogPublisher
|
||||
eventPublisher agentexec.EventPublisher
|
||||
taskID int64
|
||||
}
|
||||
|
||||
func (t *workerTransportStub) Close() { *t.events = append(*t.events, "transport-close") }
|
||||
func (t *workerTransportStub) LogPublisher() agentexec.LogPublisher {
|
||||
*t.events = append(*t.events, "log-publisher")
|
||||
if t.publisher != nil {
|
||||
return t.publisher
|
||||
}
|
||||
return workerPublisher{}
|
||||
}
|
||||
|
||||
func (t *workerTransportStub) EventPublisher() agentexec.EventPublisher {
|
||||
*t.events = append(*t.events, "event-publisher")
|
||||
if t.eventPublisher != nil {
|
||||
return t.eventPublisher
|
||||
}
|
||||
return workerPublisher{}
|
||||
}
|
||||
|
||||
func (t *workerTransportStub) SubscribeTasks(_ context.Context, taskID int64, _ agentnats.TaskHandler, _ *slog.Logger) (agentnats.Subscription, error) {
|
||||
*t.events = append(*t.events, "subscribe")
|
||||
t.taskID = taskID
|
||||
if t.subscribeErr != nil {
|
||||
return nil, t.subscribeErr
|
||||
}
|
||||
return workerSubscription{
|
||||
close: func() { *t.events = append(*t.events, "subscription-close") },
|
||||
closeErr: t.subCloseErr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (*workerTransportStub) Forger(string, time.Duration) workflow.Forger { return nil }
|
||||
|
||||
func (*workerTransportStub) Store(time.Duration) workflow.Store { return nil }
|
||||
|
||||
func (*workerTransportStub) Calls(time.Duration) task.Caller { return nil }
|
||||
|
||||
func (*workerTransportStub) Trees(string, string, time.Duration) agentnats.RemoteTrees { return nil }
|
||||
|
||||
func TestRunPassesCompleteTransportConfiguration(t *testing.T) {
|
||||
var got agentnats.Config
|
||||
var events []string
|
||||
transport := &workerTransportStub{events: &events}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
dependencies := workerDependencies{
|
||||
markSafe: func(context.Context, string, *slog.Logger) bool { return true },
|
||||
connect: func(_ context.Context, config agentnats.Config, _ *slog.Logger) (workerTransport, error) {
|
||||
got = config
|
||||
return transport, nil
|
||||
},
|
||||
bootID: func(string) (int64, error) { return 42, nil },
|
||||
wait: func(context.Context, *slog.Logger) { cancel() },
|
||||
}
|
||||
settings := Settings{NATSURL: "nats://test", NATSToken: "secret", WorkDir: "/worktree"}
|
||||
if err := run(ctx, settings, slog.New(slog.DiscardHandler), dependencies); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.URL != settings.NATSURL || got.Token != settings.NATSToken {
|
||||
t.Fatalf("connection config = %#v, want URL/token from settings", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPreservesStartupAndReverseCleanupOrder(t *testing.T) {
|
||||
var events []string
|
||||
transport := &workerTransportStub{events: &events}
|
||||
dependencies := workerDependencies{
|
||||
markSafe: func(context.Context, string, *slog.Logger) bool {
|
||||
events = append(events, "git-safety")
|
||||
return true
|
||||
},
|
||||
connect: func(context.Context, agentnats.Config, *slog.Logger) (workerTransport, error) {
|
||||
events = append(events, "connect")
|
||||
return transport, nil
|
||||
},
|
||||
bootID: func(string) (int64, error) {
|
||||
events = append(events, "boot-id")
|
||||
return 42, nil
|
||||
},
|
||||
wait: func(context.Context, *slog.Logger) {
|
||||
events = append(events, "wait")
|
||||
},
|
||||
}
|
||||
if err := run(t.Context(), Settings{NATSURL: "nats://test", WorkDir: "/worktree"}, slog.New(slog.DiscardHandler), dependencies); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"git-safety", "connect", "boot-id", "log-publisher", "subscribe", "wait", "subscription-close", "transport-close"}
|
||||
if len(events) != len(want) {
|
||||
t.Fatalf("events = %v, want %v", events, want)
|
||||
}
|
||||
for i := range want {
|
||||
if events[i] != want[i] {
|
||||
t.Fatalf("events = %v, want %v", events, want)
|
||||
}
|
||||
}
|
||||
if transport.taskID != 42 {
|
||||
t.Fatalf("subscription task ID = %d, want 42", transport.taskID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunWarnsWhenNormalSubscriptionCleanupFails(t *testing.T) {
|
||||
closeErr := errors.New("unsubscribe failed")
|
||||
var events []string
|
||||
transport := &workerTransportStub{events: &events, subCloseErr: closeErr}
|
||||
dependencies := workerDependencies{
|
||||
markSafe: func(context.Context, string, *slog.Logger) bool { return true },
|
||||
connect: func(context.Context, agentnats.Config, *slog.Logger) (workerTransport, error) {
|
||||
return transport, nil
|
||||
},
|
||||
bootID: func(string) (int64, error) { return 42, nil },
|
||||
wait: func(context.Context, *slog.Logger) {},
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := run(t.Context(), Settings{NATSURL: "nats://test"}, slog.New(slog.NewTextHandler(&output, nil)), dependencies); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "task run unsubscribe failed") || !strings.Contains(output.String(), closeErr.Error()) {
|
||||
t.Fatalf("cleanup log = %q, want warning containing %q", output.String(), closeErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunStartupFailuresCleanUpOwnedResources(t *testing.T) {
|
||||
connectErr := errors.New("connect failed")
|
||||
bootErr := errors.New("invalid boot metadata")
|
||||
subscribeErr := errors.New("subscribe failed")
|
||||
tests := []struct {
|
||||
name string
|
||||
connectErr error
|
||||
bootErr error
|
||||
subscribeErr error
|
||||
wantErr error
|
||||
wantOperation string
|
||||
wantClosed bool
|
||||
}{
|
||||
{name: "connect", connectErr: connectErr, wantErr: connectErr, wantOperation: "nats connect failed"},
|
||||
{name: "boot metadata", bootErr: bootErr, wantErr: bootErr, wantOperation: "task boot failed", wantClosed: true},
|
||||
{name: "subscribe", subscribeErr: subscribeErr, wantErr: subscribeErr, wantOperation: "taskrun subscribe failed", wantClosed: true},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var events []string
|
||||
var published [][]byte
|
||||
transport := &workerTransportStub{events: &events, subscribeErr: test.subscribeErr, publisher: workerPublisher{messages: &published}}
|
||||
dependencies := workerDependencies{
|
||||
markSafe: func(context.Context, string, *slog.Logger) bool { return true },
|
||||
connect: func(context.Context, agentnats.Config, *slog.Logger) (workerTransport, error) {
|
||||
return transport, test.connectErr
|
||||
},
|
||||
bootID: func(string) (int64, error) {
|
||||
if test.bootErr != nil {
|
||||
return 0, test.bootErr
|
||||
}
|
||||
return 42, nil
|
||||
},
|
||||
wait: func(context.Context, *slog.Logger) { t.Fatal("wait called") },
|
||||
}
|
||||
var output bytes.Buffer
|
||||
err := run(t.Context(), Settings{NATSURL: "nats://test"}, slog.New(slog.NewTextHandler(&output, nil)), dependencies)
|
||||
if !errors.Is(err, test.wantErr) {
|
||||
t.Fatalf("error = %v, want %v", err, test.wantErr)
|
||||
}
|
||||
var startupErr *StartupError
|
||||
if !errors.As(err, &startupErr) || startupErr.Operation != test.wantOperation {
|
||||
t.Fatalf("startup error = %#v, want operation %q", startupErr, test.wantOperation)
|
||||
}
|
||||
closed := len(events) > 0 && events[len(events)-1] == "transport-close"
|
||||
if closed != test.wantClosed {
|
||||
t.Fatalf("transport closed = %v, want %v; events %v", closed, test.wantClosed, events)
|
||||
}
|
||||
if test.bootErr != nil {
|
||||
for _, event := range events {
|
||||
if event == "subscribe" {
|
||||
t.Fatalf("invalid boot metadata reached task subscription: events %v", events)
|
||||
}
|
||||
}
|
||||
}
|
||||
if got := strings.Count(output.String(), test.wantOperation); got != 1 {
|
||||
t.Fatalf("operation log count = %d, want 1; output %q", got, output.String())
|
||||
}
|
||||
if test.subscribeErr != nil {
|
||||
var joined strings.Builder
|
||||
for _, data := range published {
|
||||
joined.WriteString(string(data))
|
||||
}
|
||||
if !strings.Contains(joined.String(), test.wantOperation) {
|
||||
t.Fatalf("task-scoped logs %q do not contain %q", joined.String(), test.wantOperation)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsAccessResetArgs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
want bool
|
||||
}{
|
||||
{name: "org reset", args: []string{"access", "reset", "--org", "acme"}, want: true},
|
||||
{name: "instance reset", args: []string{"access", "reset", "--instance"}, want: true},
|
||||
{name: "config first", args: []string{"access", "reset", "-config", "/tmp/config.toml", "--org", "acme"}, want: true},
|
||||
{name: "setup is not a reset", args: []string{"setup"}, want: false},
|
||||
{name: "bare access", args: []string{"access"}, want: false},
|
||||
{name: "other subcommand", args: []string{"access", "grant"}, want: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsAccessResetArgs(tt.args); got != tt.want {
|
||||
t.Fatalf("IsAccessResetArgs(%v) = %v, want %v", tt.args, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunAccessResetUsage(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
wantCode int
|
||||
wantText string
|
||||
}{
|
||||
{name: "no target", args: nil, wantCode: 2, wantText: "exactly one of --org"},
|
||||
{name: "both targets", args: []string{"--org", "acme", "--instance"}, wantCode: 2, wantText: "exactly one of --org"},
|
||||
{name: "unexpected argument", args: []string{"--instance", "extra"}, wantCode: 2, wantText: "unexpected argument"},
|
||||
{name: "unknown flag", args: []string{"--workspace", "net"}, wantCode: 2},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var stderr strings.Builder
|
||||
code := RunAccessReset(t.Context(), tt.args, &stderr)
|
||||
if code != tt.wantCode {
|
||||
t.Fatalf("RunAccessReset(%v) = %d, want %d (stderr: %s)", tt.args, code, tt.wantCode, stderr.String())
|
||||
}
|
||||
if tt.wantText != "" && !strings.Contains(stderr.String(), tt.wantText) {
|
||||
t.Fatalf("stderr = %q, want it to name %q", stderr.String(), tt.wantText)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/client"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
)
|
||||
|
||||
// setupContainers must discover the bridge before embedded NATS starts, but
|
||||
// its cleanup registration comes afterwards. boot.cleanup is LIFO, so this
|
||||
// ordering stops worker containers before closing the daemon client and the
|
||||
// embedded server they are connected to.
|
||||
func TestSetupBackendsRegistersContainerCleanupAfterNATS(t *testing.T) {
|
||||
fileset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fileset, "bootstrap.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setupContainers := methodBody(t, file, "setupContainers")
|
||||
if methodCallPosition(setupContainers, "addCleanup") != token.NoPos {
|
||||
t.Fatal("setupContainers registers cleanup before NATS; LIFO would stop the broker before workers")
|
||||
}
|
||||
setupBackends := methodBody(t, file, "setupBackends")
|
||||
connect := methodCallPosition(setupBackends, "connectNATS")
|
||||
register := methodCallPosition(setupBackends, "addCleanup")
|
||||
if connect == token.NoPos || register == token.NoPos || register < connect {
|
||||
t.Fatalf("setupBackends call positions: connectNATS=%d addCleanup=%d, want container cleanup registered after connect", connect, register)
|
||||
}
|
||||
}
|
||||
|
||||
// methodBody returns the body of the func or method named name. It matches on
|
||||
// name alone, so a package-level function (such as Run in main.go) resolves as
|
||||
// well as a method; names are unique per package.
|
||||
func methodBody(t *testing.T, file *ast.File, name string) *ast.BlockStmt {
|
||||
t.Helper()
|
||||
for _, decl := range file.Decls {
|
||||
fn, ok := decl.(*ast.FuncDecl)
|
||||
if ok && fn.Name.Name == name {
|
||||
return fn.Body
|
||||
}
|
||||
}
|
||||
t.Fatalf("method %s not found", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func methodCallPosition(body *ast.BlockStmt, name string) token.Pos {
|
||||
position := token.NoPos
|
||||
ast.Inspect(body, func(node ast.Node) bool {
|
||||
call, ok := node.(*ast.CallExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
selector, ok := call.Fun.(*ast.SelectorExpr)
|
||||
if ok && selector.Sel.Name == name && position == token.NoPos {
|
||||
position = call.Pos()
|
||||
}
|
||||
return true
|
||||
})
|
||||
return position
|
||||
}
|
||||
|
||||
// This is the native-daemon deployment seam: Docker supplies the host-side
|
||||
// gateway of the worker bridge, embedded NATS binds that address, and the
|
||||
// runtime credential shared with workers is required by the same listener.
|
||||
func TestConnectEmbeddedNATSUsesManagedWorkerBridge(t *testing.T) {
|
||||
const gateway = "127.0.0.1"
|
||||
dockerAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case strings.HasSuffix(r.URL.Path, "/networks/workers"):
|
||||
writeDockerResponse(t, w, map[string]any{
|
||||
"Name": "workers",
|
||||
"Driver": "bridge",
|
||||
"Scope": "local",
|
||||
"IPAM": map[string]any{
|
||||
"Config": []map[string]string{{
|
||||
"Subnet": "127.0.0.0/8",
|
||||
"Gateway": gateway,
|
||||
}},
|
||||
},
|
||||
})
|
||||
case strings.HasSuffix(r.URL.Path, "/containers/json"):
|
||||
writeDockerResponse(t, w, []any{})
|
||||
default:
|
||||
http.Error(w, fmt.Sprintf("unexpected Docker API path %s", r.URL.Path), http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(dockerAPI.Close)
|
||||
|
||||
dockerClient, err := client.New(client.WithHost(dockerAPI.URL), client.WithAPIVersion("1.55"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = dockerClient.Close() })
|
||||
|
||||
pool, err := container.NewPool(t.Context(), container.Config{
|
||||
DockerClient: dockerClient,
|
||||
Network: "workers",
|
||||
RequireHostGateway: true,
|
||||
}, nil, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatalf("container.NewPool = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = pool.Close() })
|
||||
|
||||
b := &boot{
|
||||
cfg: config.Config{
|
||||
StateDir: t.TempDir(),
|
||||
NATS: config.NATSConfig{Mode: config.NATSModeEmbedded},
|
||||
},
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
containerPool: pool,
|
||||
}
|
||||
if err := b.connectNATS(t.Context()); err != nil {
|
||||
t.Fatalf("connectNATS = %v", err)
|
||||
}
|
||||
t.Cleanup(b.cleanup)
|
||||
|
||||
if !strings.HasPrefix(b.natsURL, "nats://"+gateway+":") {
|
||||
t.Errorf("connected NATS URL = %q, want bridge gateway %s", b.natsURL, gateway)
|
||||
}
|
||||
if b.natsToken == "" {
|
||||
t.Fatal("connected NATS token is empty")
|
||||
}
|
||||
if unauthenticated, connectErr := natsio.Connect(b.natsURL); connectErr == nil {
|
||||
unauthenticated.Close()
|
||||
t.Fatal("unauthenticated worker connection succeeded")
|
||||
}
|
||||
authenticated, err := natsio.Connect(b.natsURL, natsio.Token(b.natsToken))
|
||||
if err != nil {
|
||||
t.Fatalf("authenticated worker connection = %v", err)
|
||||
}
|
||||
authenticated.Close()
|
||||
}
|
||||
|
||||
func writeDockerResponse(t *testing.T, w http.ResponseWriter, value any) {
|
||||
t.Helper()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(value); err != nil {
|
||||
t.Errorf("encode Docker response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectExternalNATSEmptyURLReturnsConfigurationError(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
b.cfg.NATS = config.NATSConfig{Mode: config.NATSModeExternal}
|
||||
|
||||
err := b.connectNATS(t.Context())
|
||||
if err == nil || !strings.Contains(err.Error(), "nats.url is required") {
|
||||
t.Fatalf("connectNATS() error = %v, want useful missing-url error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupObservabilityDoesNotTouchDaemonBeforeBuild(t *testing.T) {
|
||||
fileset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fileset, "bootstrap.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setup := methodBody(t, file, "setupObservability")
|
||||
var daemonAssignments int
|
||||
ast.Inspect(setup, func(node ast.Node) bool {
|
||||
assign, ok := node.(*ast.AssignStmt)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
for _, lhs := range assign.Lhs {
|
||||
if selector, ok := lhs.(*ast.SelectorExpr); ok {
|
||||
if receiver, ok := selector.X.(*ast.SelectorExpr); ok && receiver.Sel.Name == "d" {
|
||||
daemonAssignments++
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
if daemonAssignments != 0 {
|
||||
t.Fatalf("setupObservability assigns daemon fields %d times; daemon is built later", daemonAssignments)
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
func TestChatRepoEnvSingleIdentity(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Forge: config.Forge{Host: "https://github.com", Type: "github"},
|
||||
Repos: []config.Repo{
|
||||
{Owner: "my-org", Name: "frontend-app", Base: "develop"},
|
||||
{Owner: "my-org", Name: "backend-api"},
|
||||
},
|
||||
}
|
||||
got := chatRepoEnv(cfg, "")
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("expected 2 repos, got %d", len(got))
|
||||
}
|
||||
if got[0].FullName != "my-org/frontend-app" || got[0].Forge != "https://github.com" || got[0].DefaultBranch != "develop" {
|
||||
t.Errorf("repo 0 mismatch: %+v", got[0])
|
||||
}
|
||||
// Base empty -> the daemon's effective default branch (main), not a guess.
|
||||
if got[1].DefaultBranch != "main" {
|
||||
t.Errorf("expected default branch main, got %q", got[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRepoEnvMultiIdentityMatchesActive(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Identities: []config.IdentityConfig{
|
||||
{Name: "alpha", Forge: config.Forge{Host: "https://gitea.example.com"}, Repos: []config.Repo{{Owner: "alpha", Name: "app"}}},
|
||||
{Name: "beta", Forge: config.Forge{Host: "https://github.com"}, Repos: []config.Repo{{Owner: "beta", Name: "svc"}}},
|
||||
},
|
||||
}
|
||||
got := chatRepoEnv(cfg, "beta")
|
||||
if len(got) != 1 || got[0].FullName != "beta/svc" || got[0].Forge != "https://github.com" {
|
||||
t.Fatalf("expected the active identity's scope, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRepoEnvMultiIdentityFallsBackToFirstWithRepos(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Identities: []config.IdentityConfig{
|
||||
{Name: "a", Forge: config.Forge{}, Repos: nil},
|
||||
{Name: "b", Forge: config.Forge{Host: "https://github.com"}, Repos: []config.Repo{{Owner: "b", Name: "svc"}}},
|
||||
},
|
||||
}
|
||||
got := chatRepoEnv(cfg, "no-such-identity")
|
||||
if len(got) != 1 || got[0].FullName != "b/svc" {
|
||||
t.Fatalf("expected fallback to the first identity with repos, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRepoEnvPreservesEmptyForge(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Repos: []config.Repo{{Owner: "acme", Name: "widgets"}},
|
||||
}
|
||||
got := chatRepoEnv(cfg, "")
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("expected 1 repo, got %d", len(got))
|
||||
}
|
||||
if got[0].Forge != "" {
|
||||
t.Errorf("expected empty forge to be preserved (so the prompt renders 'forge unknown'), got %q", got[0].Forge)
|
||||
}
|
||||
if got[0].DefaultBranch != "main" {
|
||||
t.Errorf("expected default branch main, got %q", got[0].DefaultBranch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRepoEnvEmpty(t *testing.T) {
|
||||
if got := chatRepoEnv(config.Config{}, ""); len(got) != 0 {
|
||||
t.Fatalf("expected no repos, got %+v", got)
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
// TestWebhookUserIdentityNeverReturnsTheRoutePath guards
|
||||
// docs/prds/memory-engine-unification.md §3 and §4's specific webhook
|
||||
// requirement: internal/channels/webhook/webhook.go carries the configured route
|
||||
// path in Inbound.BudgetKey, never in SenderID, and no platform may turn one into
|
||||
// a user identity however plausible it looks as one.
|
||||
func TestWebhookUserIdentityNeverReturnsTheRoutePath(t *testing.T) {
|
||||
resolver := userIdentityResolver()
|
||||
id, ok := resolver(messaging.Inbound{
|
||||
Platform: "webhook",
|
||||
Message: messaging.Message{SenderID: "/github", Sender: "webhook"},
|
||||
})
|
||||
if ok || id != "" {
|
||||
t.Fatalf("webhook resolver = (%q, %v), want (\"\", false)", id, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUserIdentityResolverPerPlatform documents the composition's per-platform
|
||||
// choice: telegram and email carry a native sender id, the dashboard ("web") and
|
||||
// webhook do not, and a platform nobody has decided about fails closed.
|
||||
//
|
||||
// It is keyed on the message's platform rather than a resolver per channel,
|
||||
// because one Gateway Router serves every channel: closing over a channel name
|
||||
// resolved every turn as "web" (archie-core-c1qx).
|
||||
func TestUserIdentityResolverPerPlatform(t *testing.T) {
|
||||
tests := []struct {
|
||||
platform string
|
||||
senderID string
|
||||
wantOK bool
|
||||
}{
|
||||
{"telegram", "12345", true},
|
||||
{"email", "person@example.com", true},
|
||||
{"web", "anything", false},
|
||||
{"webhook", "/github", false},
|
||||
{"unknown-future-channel", "anything", false},
|
||||
{"", "anything", false},
|
||||
}
|
||||
resolver := userIdentityResolver()
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.platform, func(t *testing.T) {
|
||||
id, ok := resolver(messaging.Inbound{Platform: tt.platform, Message: messaging.Message{SenderID: tt.senderID}})
|
||||
if ok != tt.wantOK {
|
||||
t.Fatalf("%s resolver ok = %v, want %v", tt.platform, ok, tt.wantOK)
|
||||
}
|
||||
if ok && string(id) != tt.senderID {
|
||||
t.Fatalf("%s resolver id = %q, want %q", tt.platform, id, tt.senderID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestUserIdentityResolverFailsClosedOnEmptySenderID guards §3's "fail closed":
|
||||
// an empty SenderID (a channel that carries no per-caller id for this particular
|
||||
// message) must resolve to no identity, never an empty-string identity that
|
||||
// Subject would treat as present.
|
||||
func TestUserIdentityResolverFailsClosedOnEmptySenderID(t *testing.T) {
|
||||
resolver := userIdentityResolver()
|
||||
id, ok := resolver(messaging.Inbound{Platform: "telegram", Message: messaging.Message{SenderID: ""}})
|
||||
if ok || id != "" {
|
||||
t.Fatalf("telegram resolver with no sender id = (%q, %v), want (\"\", false)", id, ok)
|
||||
}
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChatModelManagerBuildsUniqueCatalogAndSelectsChatDefault(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{
|
||||
"builder": "provider/zeta",
|
||||
"chat": "provider/alpha",
|
||||
"review": "provider/zeta",
|
||||
"empty": "",
|
||||
})
|
||||
|
||||
models := manager.Models()
|
||||
if len(models) != 2 || models[0] != "provider/alpha" || models[1] != "provider/zeta" {
|
||||
t.Fatalf("Models() = %v, want sorted unique model references", models)
|
||||
}
|
||||
models[0] = "mutated"
|
||||
if got := manager.Models()[0]; got != "provider/alpha" {
|
||||
t.Fatalf("Models() exposed internal state: first model = %q", got)
|
||||
}
|
||||
if got := manager.ActiveModel(); got != "provider/alpha" {
|
||||
t.Fatalf("ActiveModel() = %q, want chat model", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelManagerFallsBackToBuilder(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{
|
||||
"builder": "provider/builder",
|
||||
"review": "provider/reviewer",
|
||||
})
|
||||
|
||||
if got := manager.ActiveModel(); got != "provider/builder" {
|
||||
t.Fatalf("ActiveModel() = %q, want builder fallback", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelManagerSwitchesOnlyToConfiguredModels(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{
|
||||
"chat": "provider/one",
|
||||
"review": "provider/two",
|
||||
})
|
||||
|
||||
if err := manager.SetActiveModel(context.Background(), "provider/two"); err != nil {
|
||||
t.Fatalf("SetActiveModel() error = %v", err)
|
||||
}
|
||||
if got := manager.ActiveModel(); got != "provider/two" {
|
||||
t.Fatalf("ActiveModel() = %q, want switched model", got)
|
||||
}
|
||||
|
||||
if err := manager.SetActiveModel(context.Background(), "provider/unknown"); err == nil {
|
||||
t.Fatal("SetActiveModel() error = nil, want unknown-model error")
|
||||
}
|
||||
if got := manager.ActiveModel(); got != "provider/two" {
|
||||
t.Fatalf("failed switch changed ActiveModel() to %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelManagerHonorsCancelledContext(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{
|
||||
"chat": "provider/one",
|
||||
"review": "provider/two",
|
||||
})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
if err := manager.SetActiveModel(ctx, "provider/two"); err == nil {
|
||||
t.Fatal("SetActiveModel() error = nil, want context cancellation")
|
||||
}
|
||||
if got := manager.ActiveModel(); got != "provider/one" {
|
||||
t.Fatalf("cancelled switch changed ActiveModel() to %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelManagerSeparatesProvidersAndModels(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{
|
||||
"chat": "openrouter/openai/gpt-5.6",
|
||||
"builder": "openai/gpt-5.6",
|
||||
"review": "anthropic/claude-opus",
|
||||
})
|
||||
|
||||
providers := manager.Providers()
|
||||
if len(providers) != 3 ||
|
||||
providers[0] != "anthropic" ||
|
||||
providers[1] != "openai" ||
|
||||
providers[2] != "openrouter" {
|
||||
t.Fatalf("Providers() = %v", providers)
|
||||
}
|
||||
if got := manager.ActiveProvider(); got != "openrouter" {
|
||||
t.Fatalf("ActiveProvider() = %q, want openrouter", got)
|
||||
}
|
||||
if got := manager.ModelsForProvider("openai"); len(got) != 1 || got[0] != "openai/gpt-5.6" {
|
||||
t.Fatalf("openai models = %v", got)
|
||||
}
|
||||
if got := manager.ModelsForProvider("openrouter"); len(got) != 1 || got[0] != "openrouter/openai/gpt-5.6" {
|
||||
t.Fatalf("openrouter models = %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelManagerProviderSwitchSelectsValidModel(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{
|
||||
"chat": "openrouter/openai/gpt-5.6",
|
||||
"builder": "openai/gpt-5.6",
|
||||
"review": "openai/o3",
|
||||
})
|
||||
|
||||
if err := manager.SetActiveProvider(context.Background(), "openai"); err != nil {
|
||||
t.Fatalf("SetActiveProvider() error = %v", err)
|
||||
}
|
||||
if got := manager.ActiveProvider(); got != "openai" {
|
||||
t.Fatalf("ActiveProvider() = %q, want openai", got)
|
||||
}
|
||||
if got := manager.ActiveModel(); got != "openai/gpt-5.6" {
|
||||
t.Fatalf("ActiveModel() = %q, want first configured openai model", got)
|
||||
}
|
||||
|
||||
if err := manager.SetActiveProvider(context.Background(), "missing"); err == nil {
|
||||
t.Fatal("SetActiveProvider() error = nil, want unknown-provider error")
|
||||
}
|
||||
if got := manager.ActiveProvider(); got != "openai" {
|
||||
t.Fatalf("failed switch changed provider to %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelManagerIncludesExplicitChatCatalog(t *testing.T) {
|
||||
manager := newChatModelManager(
|
||||
map[string]string{"chat": "openai/gpt-5.6-sol"},
|
||||
[]string{
|
||||
"openrouter/openai/gpt-5.6-sol",
|
||||
"anthropic/claude-opus",
|
||||
"openai/gpt-5.6-sol",
|
||||
},
|
||||
)
|
||||
|
||||
if got := manager.Models(); len(got) != 3 {
|
||||
t.Fatalf("Models() = %v, want workflow model plus explicit catalog", got)
|
||||
}
|
||||
if got := manager.ModelsForProvider("openrouter"); len(got) != 1 ||
|
||||
got[0] != "openrouter/openai/gpt-5.6-sol" {
|
||||
t.Fatalf("OpenRouter catalog = %v", got)
|
||||
}
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
func TestComposeChatContractRejectsInvalidSettings(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
settings config.Services
|
||||
}{
|
||||
{name: "missing target", settings: config.Services{}},
|
||||
{name: "blank target", settings: config.Services{config.ServiceNameGateway: {Target: " \t\n"}}},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
chat, cleanup, err := composeChatContract(tt.settings, &secret.Registry{})
|
||||
if cleanup != nil {
|
||||
t.Cleanup(cleanup)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("invalid settings were accepted")
|
||||
}
|
||||
if chat != nil || cleanup != nil {
|
||||
t.Fatal("invalid settings published an adapter or cleanup")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeChatContractNonLoopbackFailsClosed(t *testing.T) {
|
||||
_, _, err := composeChatContract(
|
||||
config.Services{config.ServiceNameGateway: {Target: "gateway.example.com:8585"}},
|
||||
&secret.Registry{},
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatal("non-loopback target without a token should fail closed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeChatContractRemoteAndCleanup(t *testing.T) {
|
||||
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
server := grpc.NewServer()
|
||||
gatewayrpc.RegisterServer(server, compositionChat{snapshot: gateway.ChatSnapshot{ActiveModel: "remote-model"}})
|
||||
serveDone := make(chan error, 1)
|
||||
go func() { serveDone <- server.Serve(listener) }()
|
||||
t.Cleanup(func() {
|
||||
server.Stop()
|
||||
_ = listener.Close()
|
||||
if err := <-serveDone; err != nil {
|
||||
t.Errorf("serve chat: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
chat, cleanup, err := composeChatContract(
|
||||
config.Services{config.ServiceNameGateway: {Target: listener.Addr().String()}},
|
||||
&secret.Registry{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cleanup == nil {
|
||||
t.Fatal("remote composition returned no cleanup")
|
||||
}
|
||||
t.Cleanup(cleanup)
|
||||
if _, ok := chat.(*gatewayrpc.Client); !ok {
|
||||
t.Fatalf("remote composition returned %T", chat)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
snapshot, err := chat.Snapshot(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if snapshot.ActiveModel != "remote-model" {
|
||||
t.Fatalf("Snapshot used wrong adapter: ActiveModel = %q", snapshot.ActiveModel)
|
||||
}
|
||||
cleanup()
|
||||
if _, err := chat.Snapshot(ctx); status.Code(err) != codes.Canceled {
|
||||
t.Fatalf("Snapshot after cleanup: code = %v, want Canceled; error = %v", status.Code(err), err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGatewayResolvedToken pins the daemon-side wiring rather than the
|
||||
// precedence rule itself: config.Services.ResolvedToken owns the rule and is
|
||||
// tested there, but only this level proves the gateway reads it through the
|
||||
// secret registry and lands on the env var its registration names.
|
||||
func TestGatewayResolvedToken(t *testing.T) {
|
||||
secrets := &secret.Registry{}
|
||||
explicit := config.Services{config.ServiceNameGateway: {TargetToken: "file-token"}}
|
||||
if got := explicit.ResolvedToken(config.ServiceNameGateway, secrets.Getenv); got != "file-token" {
|
||||
t.Fatalf("resolved token = %q, want file-token", got)
|
||||
}
|
||||
t.Setenv("GATEWAY_TOKEN", "environment-token")
|
||||
empty := config.Services{config.ServiceNameGateway: {}}
|
||||
if got := empty.ResolvedToken(config.ServiceNameGateway, secrets.Getenv); got != "environment-token" {
|
||||
t.Fatalf("resolved token = %q, want environment-token", got)
|
||||
}
|
||||
if got := explicit.ResolvedToken(config.ServiceNameGateway, secrets.Getenv); got != "file-token" {
|
||||
t.Fatalf("explicit target_token should win over the environment, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
type compositionChat struct {
|
||||
gateway.ChatContract
|
||||
snapshot gateway.ChatSnapshot
|
||||
}
|
||||
|
||||
func (c compositionChat) Snapshot(context.Context) (gateway.ChatSnapshot, error) {
|
||||
return c.snapshot, nil
|
||||
}
|
||||
@@ -1,175 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
|
||||
// TestMakeUpdateServiceWiresInstallType is the regression test for
|
||||
// archie-core-522's fail-closed guarantee actually reaching production: a
|
||||
// Service built without InstallType always refuses to install (see
|
||||
// releaseupdate.ErrUnknownInstallType), so wiring it here is what makes
|
||||
// /update work at all for a correctly-stamped build, not just a detail.
|
||||
func TestMakeUpdateServiceWiresInstallType(t *testing.T) {
|
||||
cfg := config.Config{}
|
||||
cfg.Chat.Telegram.UpdateCheckCommand = []string{"archie-update-check"}
|
||||
cfg.Chat.Telegram.UpdateInstallCommand = []string{"archie-update-install"}
|
||||
|
||||
service := makeUpdateService(chatSetup{Cfg: config.NewHolder(cfg)})
|
||||
|
||||
if service == nil {
|
||||
t.Fatal("makeUpdateService returned nil despite a configured check command")
|
||||
}
|
||||
if service.InstallType != installtype.Type() {
|
||||
t.Errorf("Service.InstallType = %q, want installtype.Type() = %q", service.InstallType, installtype.Type())
|
||||
}
|
||||
if service.Enrich == nil {
|
||||
t.Fatal("Service.Enrich is nil; component InstallType/Reference will never be populated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestComponentInstallTypeEnricherDaemon(t *testing.T) {
|
||||
enrich := componentInstallTypeEnricher(nil, config.NATSConfig{})
|
||||
|
||||
gotType, gotRef := enrich(releaseupdate.ComponentDaemon)
|
||||
if gotType != installtype.Type() || gotRef != "" {
|
||||
t.Errorf("daemon = (%q, %q), want (%q, \"\")", gotType, gotRef, installtype.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestComponentInstallTypeEnricherAgentBeforeAnyObservation(t *testing.T) {
|
||||
var status daemon.AgentStatus
|
||||
enrich := componentInstallTypeEnricher(&status, config.NATSConfig{})
|
||||
|
||||
gotType, gotRef := enrich(releaseupdate.ComponentAgent)
|
||||
if gotType != "" || gotRef != "" {
|
||||
t.Errorf("agent before any Observe = (%q, %q), want (\"\", \"\")", gotType, gotRef)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComponentInstallTypeEnricherAgentAfterObservation(t *testing.T) {
|
||||
var status daemon.AgentStatus
|
||||
status.Observe("1.9.11", "container")
|
||||
enrich := componentInstallTypeEnricher(&status, config.NATSConfig{})
|
||||
|
||||
gotType, _ := enrich(releaseupdate.ComponentAgent)
|
||||
if gotType != "container" {
|
||||
t.Errorf("agent InstallType = %q, want the observed install type %q", gotType, "container")
|
||||
}
|
||||
}
|
||||
|
||||
func TestComponentInstallTypeEnricherAgentNilStatus(t *testing.T) {
|
||||
enrich := componentInstallTypeEnricher(nil, config.NATSConfig{})
|
||||
|
||||
gotType, gotRef := enrich(releaseupdate.ComponentAgent)
|
||||
if gotType != "" || gotRef != "" {
|
||||
t.Errorf("agent with nil AgentStatus = (%q, %q), want (\"\", \"\")", gotType, gotRef)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComponentInstallTypeEnricherNATS(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
nats config.NATSConfig
|
||||
wantType string
|
||||
wantRef string
|
||||
}{
|
||||
{name: "embedded", nats: config.NATSConfig{Mode: "embedded"}, wantType: "embedded", wantRef: ""},
|
||||
{name: "external", nats: config.NATSConfig{Mode: "external", URL: "nats://nats.internal:4222"}, wantType: "external", wantRef: "nats://nats.internal:4222"},
|
||||
{name: "off", nats: config.NATSConfig{Mode: "off"}, wantType: "", wantRef: ""},
|
||||
{name: "unset", nats: config.NATSConfig{}, wantType: "", wantRef: ""},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
enrich := componentInstallTypeEnricher(nil, test.nats)
|
||||
gotType, gotRef := enrich(releaseupdate.ComponentNATS)
|
||||
if gotType != test.wantType || gotRef != test.wantRef {
|
||||
t.Errorf("nats(%+v) = (%q, %q), want (%q, %q)", test.nats, gotType, gotRef, test.wantType, test.wantRef)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComponentInstallTypeEnricherUnknownComponent(t *testing.T) {
|
||||
enrich := componentInstallTypeEnricher(nil, config.NATSConfig{})
|
||||
|
||||
gotType, gotRef := enrich("something-else")
|
||||
if gotType != "" || gotRef != "" {
|
||||
t.Errorf("unknown component = (%q, %q), want (\"\", \"\")", gotType, gotRef)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDaemonRunningVersionsVouchesOnlyForItselfWithoutAgentStatus: the point
|
||||
// of checking a pending update report against running versions is that the
|
||||
// value checked against is ground truth. This process's own compiled-in
|
||||
// version is; a guess at the agent's is not, because runtimeVersion records
|
||||
// what archied's release pipeline stamped, not what an agent container is
|
||||
// actually running. Before any task has reported an observed agent version
|
||||
// (agentStatus nil, or nothing observed yet), including one would recreate
|
||||
// the false success the check exists to catch, so its absence is the
|
||||
// assertion.
|
||||
func TestDaemonRunningVersionsVouchesOnlyForItselfWithoutAgentStatus(t *testing.T) {
|
||||
running := daemonRunningVersions(nil)
|
||||
|
||||
if got := running[releaseupdate.ComponentDaemon]; got != gatewayVersion {
|
||||
t.Errorf("running[%q] = %q, want gatewayVersion = %q", releaseupdate.ComponentDaemon, got, gatewayVersion)
|
||||
}
|
||||
if _, ok := running[releaseupdate.ComponentAgent]; ok {
|
||||
t.Errorf("running vouches for %q, which this process cannot verify: %#v",
|
||||
releaseupdate.ComponentAgent, running)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDaemonRunningVersionsIncludesObservedAgent: once a task response has
|
||||
// actually self-reported an agent version (daemon.AgentStatus.Observe), that
|
||||
// -- not archied's release pipeline's expectation -- is ground truth for
|
||||
// what is running, so it belongs in the verification set.
|
||||
func TestDaemonRunningVersionsIncludesObservedAgent(t *testing.T) {
|
||||
var status daemon.AgentStatus
|
||||
status.Observe("1.9.11", "container")
|
||||
|
||||
running := daemonRunningVersions(&status)
|
||||
|
||||
if got := running[releaseupdate.ComponentAgent]; got != "1.9.11" {
|
||||
t.Errorf("running[%q] = %q, want the observed version %q", releaseupdate.ComponentAgent, got, "1.9.11")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMakeUpdateServiceProbesTheDaemonsOwnHealthAddress: the watchdog is
|
||||
// verifying archied's restart, so it has to probe archied. The dashboard's
|
||||
// listener is the wrong answer twice over -- it can be switched off, and in
|
||||
// Phase 3 it belongs to another process, which would report health for
|
||||
// something the watchdog never restarted (archie-core-1r4g).
|
||||
func TestMakeUpdateServiceProbesTheDaemonsOwnHealthAddress(t *testing.T) {
|
||||
for _, tc := range []struct{ name, listen, want string }{
|
||||
{name: "default loopback", listen: "127.0.0.1:8485", want: "http://127.0.0.1:8485"},
|
||||
{name: "wildcard bind", listen: "0.0.0.0:8485", want: "http://localhost:8485"},
|
||||
{name: "operator-chosen port", listen: "127.0.0.1:9500", want: "http://127.0.0.1:9500"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := config.Config{}
|
||||
cfg.Health.Listen = tc.listen
|
||||
// The dashboard's own address must not leak into the probe.
|
||||
cfg.Web.Listen = "127.0.0.1:8484"
|
||||
cfg.Chat.Telegram.UpdateCheckCommand = []string{"archie-update-check"}
|
||||
cfg.Chat.Telegram.UpdateInstallCommand = []string{"archie-update-install"}
|
||||
|
||||
service := makeUpdateService(chatSetup{Cfg: config.NewHolder(cfg)})
|
||||
|
||||
if service == nil {
|
||||
t.Fatal("makeUpdateService returned nil despite a configured check command")
|
||||
}
|
||||
installer, ok := service.Installer.(releaseupdate.CommandInstaller)
|
||||
if !ok {
|
||||
t.Fatalf("Service.Installer = %T, want releaseupdate.CommandInstaller", service.Installer)
|
||||
}
|
||||
if installer.HealthURL != tc.want {
|
||||
t.Errorf("Installer.HealthURL = %q, want %q", installer.HealthURL, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
func TestRemoteChatCompositionPreservesChannelTurnLedger(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
DatabaseURL: pgtest.URL(t),
|
||||
Services: config.Services{config.ServiceNameGateway: {Target: "127.0.0.1:1"}},
|
||||
}
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
b := &boot{cfg: cfg, log: slog.Default(), cfgHolder: config.NewHolder(cfg), stateStore: st, personas: gateway.NewPersonaRegistry(gateway.DefaultPersonas())}
|
||||
t.Cleanup(b.cleanup)
|
||||
if err := b.openStores(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b.chatSessionStore == nil {
|
||||
t.Fatal("startup did not open the channel conversation store")
|
||||
}
|
||||
sessions := b.chatSessionStore
|
||||
if err := b.setupLLMAndChat(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b.chatSessionStore != sessions {
|
||||
t.Fatal("remote web chat replaced the channel conversation store")
|
||||
}
|
||||
ledger, ok := b.chatSessionStore.(gateway.TurnLedger)
|
||||
if !ok {
|
||||
t.Fatal("channel conversation store does not implement TurnLedger")
|
||||
}
|
||||
if err := ledger.RecoverTurns(t.Context(), "test-owner"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -1,273 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
)
|
||||
|
||||
// recordingStream captures the turn's reported progress in arrival order.
|
||||
type recordingStream struct {
|
||||
events []string
|
||||
}
|
||||
|
||||
func (s *recordingStream) Delta(text string) {
|
||||
s.events = append(s.events, "text:"+text)
|
||||
}
|
||||
|
||||
func (s *recordingStream) ToolCall(event gateway.ToolCallEvent) {
|
||||
s.events = append(s.events, "tool:"+event.Name+":"+event.Parameters+":"+event.Summary())
|
||||
}
|
||||
|
||||
func (s *recordingStream) Media(event gateway.MediaEvent) {
|
||||
// Path is recorded alongside the type: a local attachment that lost
|
||||
// its path on the way through would still look like a media event.
|
||||
s.events = append(s.events, "media:"+event.ToolName+":"+event.Attachment.Type+":"+event.Attachment.Path)
|
||||
}
|
||||
|
||||
func streamOf(parts ...core.StreamPart) <-chan core.StreamPart {
|
||||
ch := make(chan core.StreamPart, len(parts))
|
||||
for _, part := range parts {
|
||||
ch <- part
|
||||
}
|
||||
close(ch)
|
||||
return ch
|
||||
}
|
||||
|
||||
func TestDrainChatStream(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
parts []core.StreamPart
|
||||
wantText string
|
||||
wantEvents []string
|
||||
}{
|
||||
{
|
||||
name: "text only",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartTextDelta, TextDelta: "hello "},
|
||||
{Type: core.StreamPartTextDelta, TextDelta: "world"},
|
||||
},
|
||||
wantText: "hello world",
|
||||
wantEvents: []string{"text:hello ", "text:world"},
|
||||
},
|
||||
{
|
||||
name: "a tool result is reported between the text around it",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartTextDelta, TextDelta: "checking"},
|
||||
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "shell", Input: `{"cmd":"true"}`}},
|
||||
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolCallID: "1", ToolName: "shell", Output: "exit 0"}},
|
||||
{Type: core.StreamPartTextDelta, TextDelta: " done"},
|
||||
},
|
||||
wantText: "checking done",
|
||||
wantEvents: []string{"text:checking", `tool:shell:{"cmd":"[string]"}:exit 0`, "text: done"},
|
||||
},
|
||||
{
|
||||
name: "a failed tool is reported as a failure, not silence",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "shell", Error: "exit status 2"}},
|
||||
},
|
||||
wantEvents: []string{"tool:shell::failed: exit status 2"},
|
||||
},
|
||||
{
|
||||
// The tool call part carries no outcome yet: reporting it as
|
||||
// well would show every tool twice.
|
||||
name: "the tool call part alone reports nothing",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolName: "shell"}},
|
||||
},
|
||||
wantEvents: nil,
|
||||
},
|
||||
{
|
||||
name: "parts with nothing to render are skipped",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartStartStep},
|
||||
{Type: core.StreamPartTextDelta, TextDelta: ""},
|
||||
{Type: core.StreamPartReasoningDelta, ReasoningDelta: "thinking"},
|
||||
{Type: core.StreamPartToolResult},
|
||||
{Type: core.StreamPartFinishStep},
|
||||
{Type: core.StreamPartFinish},
|
||||
},
|
||||
wantEvents: nil,
|
||||
},
|
||||
{
|
||||
// A tool's output carrying a tools.MultimodalResult envelope
|
||||
// must report both the ordinary tool-call event (so the turn
|
||||
// narrates what ran) and a Media event per URL, so the
|
||||
// channel has something to actually deliver.
|
||||
name: "a tool result carrying MultimodalResult URLs reports Media",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "generate_video"}},
|
||||
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "1",
|
||||
ToolName: "generate_video",
|
||||
Output: `{"is_multimodal":true,"urls":[{"type":"video","url":"https://x/v"}]}`,
|
||||
}},
|
||||
},
|
||||
wantEvents: []string{
|
||||
`tool:generate_video::{"is_multimodal":true,"urls":[{"type":"video","url":"https://x/v"}]}`,
|
||||
"media:generate_video:video:",
|
||||
},
|
||||
},
|
||||
{
|
||||
// A local file has no URL and must not acquire one: the
|
||||
// channel uploads it. Losing Path here is exactly the
|
||||
// silent non-delivery send_file exists to end.
|
||||
name: "a tool result carrying a local path reports Media with that path",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "send_file"}},
|
||||
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "1",
|
||||
ToolName: "send_file",
|
||||
Output: `{"is_multimodal":true,"urls":[{"type":"document","path":"/tmp/a.md"}]}`,
|
||||
}},
|
||||
},
|
||||
wantEvents: []string{
|
||||
"tool:send_file::" + `{"is_multimodal":true,"urls":[{"type":"document","path":"/tmp/a.md"}]}`,
|
||||
"media:send_file:document:/tmp/a.md",
|
||||
},
|
||||
},
|
||||
{
|
||||
// A ref naming neither a URL nor a path is not deliverable,
|
||||
// and an empty attachment would render as a delivered file.
|
||||
name: "a media ref with no source reports no Media event",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "odd_tool"}},
|
||||
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "1",
|
||||
ToolName: "odd_tool",
|
||||
Output: `{"is_multimodal":true,"urls":[{"type":"document"}]}`,
|
||||
}},
|
||||
},
|
||||
wantEvents: []string{"tool:odd_tool::" + `{"is_multimodal":true,"urls":[{"type":"document"}]}`},
|
||||
},
|
||||
{
|
||||
// An ordinary JSON tool result that happens to be a JSON
|
||||
// object but isn't a MultimodalResult (no is_multimodal, or
|
||||
// false) must not be misread as carrying media.
|
||||
name: "a plain JSON tool result reports no Media event",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "read_file"}},
|
||||
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "1",
|
||||
ToolName: "read_file",
|
||||
Output: `{"path":"a.txt","content":"hello"}`,
|
||||
}},
|
||||
},
|
||||
wantEvents: []string{`tool:read_file::{"path":"a.txt","content":"hello"}`},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
sink := &recordingStream{}
|
||||
text := drainChatStream(streamOf(tc.parts...), sink, nil)
|
||||
|
||||
if text != tc.wantText {
|
||||
t.Fatalf("text = %q, want %q", text, tc.wantText)
|
||||
}
|
||||
if len(sink.events) != len(tc.wantEvents) {
|
||||
t.Fatalf("events = %v, want %v", sink.events, tc.wantEvents)
|
||||
}
|
||||
for i := range tc.wantEvents {
|
||||
if sink.events[i] != tc.wantEvents[i] {
|
||||
t.Fatalf("events = %v, want %v", sink.events, tc.wantEvents)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDrainChatStreamCorrelatesBoundsAndRedactsToolParameters(t *testing.T) {
|
||||
secret := strings.Repeat("s", 200)
|
||||
sink := &recordingStream{}
|
||||
drainChatStream(streamOf(
|
||||
core.StreamPart{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{
|
||||
ToolCallID: "call-2",
|
||||
ToolName: "shell",
|
||||
Input: `{"cmd":"curl -H 'Authorization: Bearer ` + secret + `'","nested":{"api_token":"` + secret + `"}}`,
|
||||
}},
|
||||
core.StreamPart{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{
|
||||
ToolCallID: "call-1",
|
||||
ToolName: "read",
|
||||
Input: `{"path":"/tmp/one"}`,
|
||||
}},
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "call-1",
|
||||
ToolName: "read",
|
||||
Output: "one",
|
||||
}},
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "call-2",
|
||||
ToolName: "shell",
|
||||
Output: "ok",
|
||||
}},
|
||||
), sink, nil)
|
||||
|
||||
if len(sink.events) != 2 {
|
||||
t.Fatalf("events = %v, want two completed calls", sink.events)
|
||||
}
|
||||
if sink.events[0] != `tool:read:{"path":"[string]"}:one` {
|
||||
t.Fatalf("first event = %q, want parameters correlated by call ID", sink.events[0])
|
||||
}
|
||||
if strings.Contains(sink.events[1], secret) {
|
||||
t.Fatalf("second event exposed secret parameters: %q", sink.events[1])
|
||||
}
|
||||
if !strings.Contains(sink.events[1], `"api_token":"[redacted]"`) {
|
||||
t.Fatalf("second event = %q, want redacted token", sink.events[1])
|
||||
}
|
||||
if len([]rune(sink.events[1])) > 180 {
|
||||
t.Fatalf("second event is %d runes, want bounded parameters", len([]rune(sink.events[1])))
|
||||
}
|
||||
}
|
||||
|
||||
// The stream must be drained to close even when nobody is rendering it:
|
||||
// ai-sdk writes to FullStream synchronously, so an abandoned stream stalls
|
||||
// the generating goroutine.
|
||||
func TestDrainChatStreamWithoutASinkStillCollectsText(t *testing.T) {
|
||||
text := drainChatStream(streamOf(
|
||||
core.StreamPart{Type: core.StreamPartTextDelta, TextDelta: "a"},
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "shell", Output: "ok"}},
|
||||
core.StreamPart{Type: core.StreamPartTextDelta, TextDelta: "b"},
|
||||
), nil, nil)
|
||||
|
||||
if text != "ab" {
|
||||
t.Fatalf("text = %q, want %q", text, "ab")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDrainChatStreamCarriesTheToolsRegisteredIcon pins the seam between the
|
||||
// tool registry and what a chat surface renders. The icon is looked up by
|
||||
// tool name at report time, so a tool that registers one gets it and a tool
|
||||
// that does not is reported without one rather than with a generic stand-in.
|
||||
func TestDrainChatStreamCarriesTheToolsRegisteredIcon(t *testing.T) {
|
||||
sink := &iconRecordingStream{}
|
||||
drainChatStream(streamOf(
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "shell", Output: "ok"}},
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "mcp.github.search", Output: "ok"}},
|
||||
), sink, map[string]string{"shell": "💻"})
|
||||
|
||||
want := []string{"shell:💻", "mcp.github.search:"}
|
||||
if len(sink.icons) != len(want) {
|
||||
t.Fatalf("icons = %v, want %v", sink.icons, want)
|
||||
}
|
||||
for i, w := range want {
|
||||
if sink.icons[i] != w {
|
||||
t.Errorf("icons[%d] = %q, want %q", i, sink.icons[i], w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type iconRecordingStream struct {
|
||||
icons []string
|
||||
}
|
||||
|
||||
func (s *iconRecordingStream) Delta(string) {}
|
||||
|
||||
func (s *iconRecordingStream) ToolCall(event gateway.ToolCallEvent) {
|
||||
s.icons = append(s.icons, event.Name+":"+event.Emoji)
|
||||
}
|
||||
|
||||
func (s *iconRecordingStream) Media(gateway.MediaEvent) {}
|
||||
@@ -1,116 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
)
|
||||
|
||||
// TestBuildTurnMessagesAttachesMediaPartsToTheFinalUserMessage: inbound
|
||||
// media only reaches the model when the turn's request is turned into
|
||||
// message parts; the attachment belongs on the message the user just sent.
|
||||
func TestBuildTurnMessagesAttachesMediaPartsToTheFinalUserMessage(t *testing.T) {
|
||||
request := gateway.TurnModelRequest{
|
||||
Messages: []gateway.CompressedMessage{
|
||||
{Role: "system", Content: "sys"},
|
||||
{Role: "user", Content: "what is this?"},
|
||||
},
|
||||
Media: []gateway.MediaAttachment{
|
||||
{Type: "image", MIMEType: "image/png", Data: []byte("png-bytes")},
|
||||
{Type: "image", URL: "https://example.invalid/pic.jpg"},
|
||||
{Type: "document", MIMEType: "application/pdf", FileName: "report.pdf", Data: []byte("pdf-bytes")},
|
||||
},
|
||||
}
|
||||
|
||||
messages := buildTurnMessages(request)
|
||||
if len(messages) != 2 {
|
||||
t.Fatalf("messages = %d, want 2", len(messages))
|
||||
}
|
||||
if messages[0].Content != "sys" || len(messages[0].Parts) != 0 {
|
||||
t.Errorf("system message = %#v, want plain content and no parts", messages[0])
|
||||
}
|
||||
if messages[1].Content != "" {
|
||||
t.Errorf("user content = %q, want it moved into a text part", messages[1].Content)
|
||||
}
|
||||
parts := messages[1].Parts
|
||||
if len(parts) != 4 {
|
||||
t.Fatalf("user parts = %d, want text plus three media parts: %#v", len(parts), parts)
|
||||
}
|
||||
if text, ok := parts[0].(chat.TextPart); !ok || text.Text != "what is this?" {
|
||||
t.Errorf("part 0 = %#v, want the caption text", parts[0])
|
||||
}
|
||||
image, ok := parts[1].(chat.ImagePart)
|
||||
if !ok || string(image.Data) != "png-bytes" || image.MediaType != "image/png" {
|
||||
t.Errorf("part 1 = %#v, want an image part carrying the png bytes", parts[1])
|
||||
}
|
||||
linked, ok := parts[2].(chat.ImagePart)
|
||||
if !ok || linked.URL != "https://example.invalid/pic.jpg" {
|
||||
t.Errorf("part 2 = %#v, want an image part pointing at the URL", parts[2])
|
||||
}
|
||||
file, ok := parts[3].(chat.FilePart)
|
||||
if !ok {
|
||||
t.Fatalf("part 3 = %#v, want a file part", parts[3])
|
||||
}
|
||||
if string(file.Data) != "pdf-bytes" || file.MediaType != "application/pdf" || file.Name != "report.pdf" {
|
||||
t.Errorf("file part = %#v, want the pdf bytes with type and name", file)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildTurnMessagesSkipsUndeliverableMedia: an attachment carrying
|
||||
// neither bytes nor a URL names nothing the model can consume, so it must
|
||||
// be dropped rather than sent as an empty part.
|
||||
func TestBuildTurnMessagesSkipsUndeliverableMedia(t *testing.T) {
|
||||
request := gateway.TurnModelRequest{
|
||||
Messages: []gateway.CompressedMessage{
|
||||
{Role: "user", Content: "look"},
|
||||
},
|
||||
Media: []gateway.MediaAttachment{
|
||||
{Type: "video"},
|
||||
{Type: "image", MIMEType: "image/png", Data: []byte("png")},
|
||||
},
|
||||
}
|
||||
messages := buildTurnMessages(request)
|
||||
fileSeen := false
|
||||
for _, p := range messages[0].Parts {
|
||||
if _, ok := p.(chat.FilePart); ok {
|
||||
fileSeen = true
|
||||
}
|
||||
}
|
||||
if fileSeen {
|
||||
t.Errorf("parts = %#v, want the empty attachment dropped", messages[0].Parts)
|
||||
}
|
||||
if len(messages[0].Parts) != 2 {
|
||||
t.Fatalf("user parts = %d, want text plus the one deliverable image", len(messages[0].Parts))
|
||||
}
|
||||
if text, ok := messages[0].Parts[0].(chat.TextPart); !ok || text.Text != "look" {
|
||||
t.Errorf("part 0 = %#v, want the text part", messages[0].Parts[0])
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildTurnMessagesWithoutMediaKeepsPlainContent is the regression
|
||||
// guard for the overwhelmingly common case: text-only turns stay exactly as
|
||||
// they were, with Content and no parts.
|
||||
func TestBuildTurnMessagesWithoutMediaKeepsPlainContent(t *testing.T) {
|
||||
request := gateway.TurnModelRequest{
|
||||
Messages: []gateway.CompressedMessage{
|
||||
{Role: "system", Content: "sys"},
|
||||
{Role: "user", Content: "plain"},
|
||||
{Role: "assistant", Content: "reply"},
|
||||
},
|
||||
}
|
||||
messages := buildTurnMessages(request)
|
||||
for i, want := range []struct {
|
||||
role chat.Role
|
||||
content string
|
||||
}{
|
||||
{chat.RoleSystem, "sys"},
|
||||
{chat.RoleUser, "plain"},
|
||||
{chat.RoleAssistant, "reply"},
|
||||
} {
|
||||
if messages[i].Content != want.content || messages[i].Parts != nil {
|
||||
t.Errorf("message %d = %#v, want content %q and no parts", i, messages[i], want.content)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestStartGatewayRuntimeSetsUpMemoryBeforeGatewayChat pins the ordering
|
||||
// docs/prds/memory-engine-unification.md §4's "Prerequisite" note requires.
|
||||
// startGatewayRuntime (gateway.go) constructs the only chat turn runner in
|
||||
// production via setupGatewayChat, and a turn runner captures b.memEngines at
|
||||
// construction time via boot.memoryStore(). Built the other way round it
|
||||
// captures a nil engine and the chat <memory> block never appears, silently,
|
||||
// because renderMemory treats a nil engine the same as a failed read. Nothing
|
||||
// at runtime observes the order, so it is asserted by source position, the
|
||||
// same way TestSetupBackendsRegistersContainerCleanupAfterNATS in
|
||||
// bootstrap_nats_test.go pins cleanup ordering for the same reason.
|
||||
func TestStartGatewayRuntimeSetsUpMemoryBeforeGatewayChat(t *testing.T) {
|
||||
fileset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fileset, "gateway.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run := methodBody(t, file, "startGatewayRuntime")
|
||||
memoryEngine := methodCallPosition(run, "setupMemoryEngine")
|
||||
chat := methodCallPosition(run, "setupGatewayChat")
|
||||
if memoryEngine == token.NoPos || chat == token.NoPos {
|
||||
t.Fatalf("startGatewayRuntime call positions: setupMemoryEngine=%v setupGatewayChat=%v, want both present", memoryEngine, chat)
|
||||
}
|
||||
if chat < memoryEngine {
|
||||
t.Fatal("startGatewayRuntime wires setupGatewayChat before setupMemoryEngine; the turn runner would capture a nil memory engine")
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// TestPublishedProjectionAgreesWithFrontEndsOnConfiguredChannels: "is a chat
|
||||
// channel configured" is answered twice -- once by config.ChatConfig.FrontEnds,
|
||||
// which the channel status surface on /api/channels is built from, and once by
|
||||
// the ChatView.ChannelConfigured boolean this process publishes for the
|
||||
// dashboard's setup checklist. A deployment that FrontEnds calls configured
|
||||
// must not be told by the checklist to connect a channel it already has. Both
|
||||
// sides derive from that one definition; this test is what keeps them from
|
||||
// being re-decided separately.
|
||||
func TestPublishedProjectionAgreesWithFrontEndsOnConfiguredChannels(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg config.Config
|
||||
}{
|
||||
{name: "nothing configured", cfg: config.Config{}},
|
||||
{
|
||||
name: "telegram token",
|
||||
cfg: config.Config{Chat: config.ChatConfig{
|
||||
Telegram: config.TelegramConfig{Token: config.SecretRef{Engine: "env", Key: "TELEGRAM_TOKEN"}},
|
||||
}},
|
||||
},
|
||||
{
|
||||
name: "telegram token through the secret engine",
|
||||
cfg: config.Config{Chat: config.ChatConfig{
|
||||
Telegram: config.TelegramConfig{Token: config.SecretRef{Engine: "builtin", Key: "telegram"}},
|
||||
}},
|
||||
},
|
||||
{
|
||||
name: "webhook gateway",
|
||||
cfg: config.Config{Chat: config.ChatConfig{WebhookAddr: "127.0.0.1:9099"}},
|
||||
},
|
||||
{
|
||||
name: "inbound mail gateway",
|
||||
cfg: config.Config{Chat: config.ChatConfig{Email: config.EmailConfig{ListenAddr: "127.0.0.1:2525"}}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
b := &boot{
|
||||
cfg: tc.cfg,
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
stateStore: st,
|
||||
cfgHolder: config.NewHolder(tc.cfg),
|
||||
}
|
||||
b.setupObservability(t.Context())
|
||||
|
||||
// What the dashboard's /api/channels page is built from.
|
||||
frontEndsSayConfigured := false
|
||||
for _, frontEnd := range tc.cfg.Chat.FrontEnds() {
|
||||
if frontEnd.Configured {
|
||||
frontEndsSayConfigured = true
|
||||
}
|
||||
}
|
||||
|
||||
// What the dashboard's setup checklist reads, through the
|
||||
// same round trip a reading process performs.
|
||||
document, err := json.Marshal(webui.BuildConfigView(b.configViewInput(t.Context())))
|
||||
if err != nil {
|
||||
t.Fatalf("render the published projection: %v", err)
|
||||
}
|
||||
var published webui.ConfigView
|
||||
if err := json.Unmarshal(document, &published); err != nil {
|
||||
t.Fatalf("decode the published projection: %v", err)
|
||||
}
|
||||
|
||||
if published.Chat.ChannelConfigured != frontEndsSayConfigured {
|
||||
t.Errorf("published Chat.ChannelConfigured = %v, but ChatConfig.FrontEnds reports a configured front-end = %v (%+v)",
|
||||
published.Chat.ChannelConfigured, frontEndsSayConfigured, published.Chat)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,167 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// TestPublishConfigSnapshotRendersTheDaemonsConfiguration: the daemon is the
|
||||
// configuration owner, so it is the process that renders the dashboard's
|
||||
// projection and publishes it for the UI process to read. It builds that view
|
||||
// from its own state rather than from a webui.Server it never serves
|
||||
// (archie-core-ml30).
|
||||
func TestPublishConfigSnapshotRendersTheDaemonsConfiguration(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
cfg := config.Config{
|
||||
BotUser: "archie-bot",
|
||||
Repos: []config.Repo{{Owner: "acme", Name: "widget", Base: "main"}},
|
||||
Chat: config.ChatConfig{
|
||||
Operator: "Sam",
|
||||
ShowToolCalls: true,
|
||||
WebhookAddr: "127.0.0.1:9099",
|
||||
},
|
||||
}
|
||||
b := &boot{
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
stateStore: st,
|
||||
cfgHolder: config.NewHolder(cfg),
|
||||
lastReload: func() config.ReloadStatus {
|
||||
return config.ReloadStatus{LastError: "poll_interval must be positive"}
|
||||
},
|
||||
}
|
||||
b.currentProvenance.Store(&configuration.Provenance{Origins: []configuration.Origin{
|
||||
{Path: "/etc/archie/config.toml", Role: configuration.RoleMain, Layer: configuration.LayerBase},
|
||||
}})
|
||||
|
||||
b.publishConfigSnapshot(t.Context())
|
||||
|
||||
snapshot, found, err := st.ConfigSnapshot(context.Background())
|
||||
if err != nil || !found {
|
||||
t.Fatalf("ConfigSnapshot = (found %v, %v), want the published document", found, err)
|
||||
}
|
||||
if snapshot.Schema != webui.ConfigViewSchema {
|
||||
t.Fatalf("Schema = %q, want %q", snapshot.Schema, webui.ConfigViewSchema)
|
||||
}
|
||||
|
||||
var view webui.ConfigView
|
||||
if err := json.Unmarshal(snapshot.Document, &view); err != nil {
|
||||
t.Fatalf("decode published document: %v", err)
|
||||
}
|
||||
if view.Identity.BotUser != "archie-bot" || len(view.Repositories) != 1 {
|
||||
t.Errorf("view = %+v, want the daemon's identity and repositories", view)
|
||||
}
|
||||
// The chat page and the setup checklist are rendered by a process that
|
||||
// holds no configuration, so the projection has to carry what they read.
|
||||
if !view.Chat.ShowToolCalls {
|
||||
t.Error("Chat.ShowToolCalls = false; the chat page would never expand tool calls")
|
||||
}
|
||||
if view.Chat.Operator != "Sam" {
|
||||
t.Errorf("Chat.Operator = %q, want Sam", view.Chat.Operator)
|
||||
}
|
||||
if !view.Chat.ChannelConfigured {
|
||||
t.Error("Chat.ChannelConfigured = false despite a configured webhook address")
|
||||
}
|
||||
if len(view.Provenance) != 1 || view.Provenance[0].Path != "/etc/archie/config.toml" {
|
||||
t.Errorf("Provenance = %+v, want the daemon's file chain", view.Provenance)
|
||||
}
|
||||
if view.Reload == nil || view.Reload.LastError != "poll_interval must be positive" {
|
||||
t.Errorf("Reload = %+v, want the last reload outcome", view.Reload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishedSnapshotCarriesPerIdentityForges(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
b := &boot{
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
stateStore: st,
|
||||
cfgHolder: config.NewHolder(config.Config{
|
||||
BotUser: "archie-bot",
|
||||
Forge: config.Forge{Type: "github", Host: "https://github.example"},
|
||||
Repos: []config.Repo{{Owner: "acme", Name: "widget", Base: "main"}},
|
||||
Identities: []config.IdentityConfig{
|
||||
{
|
||||
Name: "gitea-bot", BotUser: "archie-gitea",
|
||||
Forge: config.Forge{Type: "gitea", Host: "https://gitea.example"},
|
||||
Repos: []config.Repo{{Owner: "beta", Name: "svc", Base: "main"}},
|
||||
},
|
||||
},
|
||||
}),
|
||||
}
|
||||
b.publishConfigSnapshot(t.Context())
|
||||
|
||||
snapshot, found, err := st.ConfigSnapshot(context.Background())
|
||||
if err != nil || !found {
|
||||
t.Fatalf("ConfigSnapshot = (found %v, %v), want the published document", found, err)
|
||||
}
|
||||
var view webui.ConfigView
|
||||
if err := json.Unmarshal(snapshot.Document, &view); err != nil {
|
||||
t.Fatalf("decode published document: %v", err)
|
||||
}
|
||||
if !view.MultiIdentity {
|
||||
t.Error("MultiIdentity = false with a configured identity")
|
||||
}
|
||||
if len(view.Identities) != 1 {
|
||||
t.Fatalf("Identities = %+v, want the configured identity", view.Identities)
|
||||
}
|
||||
got := view.Identities[0]
|
||||
if got.Name != "gitea-bot" || got.ForgeType != "gitea" || got.ForgeHost != "https://gitea.example" {
|
||||
t.Errorf("Identities[0] = %+v, want the gitea-bot forge coordinates", got)
|
||||
}
|
||||
if len(got.Repos) != 1 || got.Repos[0].Owner != "beta" || got.Repos[0].Name != "svc" {
|
||||
t.Errorf("Identities[0].Repos = %+v, want the repositories it owns", got.Repos)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPublishConfigSnapshotWithoutConfiguration: called before the daemon has
|
||||
// a configuration Holder, publishing is a no-op rather than a panic.
|
||||
func TestPublishConfigSnapshotWithoutConfiguration(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
b := &boot{log: slog.New(slog.DiscardHandler), stateStore: st}
|
||||
b.publishConfigSnapshot(t.Context())
|
||||
|
||||
if _, found, err := st.ConfigSnapshot(context.Background()); found || err != nil {
|
||||
t.Fatalf("ConfigSnapshot = (found %v, %v), want nothing published", found, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveSettingsApplyRepublishesTheConfigSnapshot: the dashboard renders the
|
||||
// published projection, not the daemon's Holder, so a live apply that changes
|
||||
// the running config must republish it or the page keeps showing the old
|
||||
// values until the next reload.
|
||||
func TestLiveSettingsApplyRepublishesTheConfigSnapshot(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
b, _ := newLiveApplyBoot(t, fileConfig())
|
||||
b.stateStore = st
|
||||
if err := b.applyWorkflowExecutionSettings(t.Context(), workflow.ExecutionSettings{MaxModelToolSteps: 25, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 4}, 2); err != nil {
|
||||
t.Fatalf("live update: %v", err)
|
||||
}
|
||||
|
||||
snapshot, found, err := st.ConfigSnapshot(context.Background())
|
||||
if err != nil || !found {
|
||||
t.Fatalf("ConfigSnapshot = (found %v, %v), want the live apply to publish one", found, err)
|
||||
}
|
||||
var view webui.ConfigView
|
||||
if err := json.Unmarshal(snapshot.Document, &view); err != nil {
|
||||
t.Fatalf("decode published document: %v", err)
|
||||
}
|
||||
if view.Budgets.MaxSteps != 25 || view.Budgets.GateMaxFailures != 4 {
|
||||
t.Errorf("published budgets = %+v, want the live-applied limits", view.Budgets)
|
||||
}
|
||||
}
|
||||
@@ -1,374 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
)
|
||||
|
||||
// The limits this package's fixtures boot or run with, and the documents the
|
||||
// control plane stores them as. Named so a test that asserts "the running
|
||||
// limits stayed" and a test that asserts "the update was applied" cannot
|
||||
// disagree about what "running" means.
|
||||
var (
|
||||
bootedSettings = workflow.ExecutionSettings{MaxModelToolSteps: 10, MaxRuntime: 5 * time.Minute, MaxConsecutiveGateFailures: 3, MaxTaskRuntime: 4 * time.Hour}
|
||||
bootedDocument = `{"max_model_tool_steps": 10, "max_runtime_seconds": 300, "max_consecutive_gate_failures": 3}`
|
||||
)
|
||||
|
||||
// applyStatusRecorder is the State Store end of the apply-status surface: it
|
||||
// keeps what a process reported, so a test can assert what the settings page
|
||||
// would render.
|
||||
type applyStatusRecorder struct {
|
||||
mu sync.Mutex
|
||||
writes []storecontract.ApplyStatus
|
||||
}
|
||||
|
||||
func (r *applyStatusRecorder) PutApplyStatus(_ context.Context, status storecontract.ApplyStatus) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.writes = append(r.writes, status)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *applyStatusRecorder) ListApplyStatus(context.Context) ([]storecontract.ApplyStatus, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return append([]storecontract.ApplyStatus(nil), r.writes...), nil
|
||||
}
|
||||
|
||||
// last returns the most recent record written for kind.
|
||||
func (r *applyStatusRecorder) last(kind string) (storecontract.ApplyStatus, bool) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
for _, v := range slices.Backward(r.writes) {
|
||||
if v.Kind == kind {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return storecontract.ApplyStatus{}, false
|
||||
}
|
||||
|
||||
// awaitCount waits until n records have been written for kind. The live apply
|
||||
// runs on the watch goroutine, so a test that drives it through the stream has
|
||||
// nothing to synchronise on but the record it is waiting for.
|
||||
func (r *applyStatusRecorder) awaitCount(t *testing.T, kind string, n int) []storecontract.ApplyStatus {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
r.mu.Lock()
|
||||
written := make([]storecontract.ApplyStatus, 0, n)
|
||||
for _, status := range r.writes {
|
||||
if status.Kind == kind {
|
||||
written = append(written, status)
|
||||
}
|
||||
}
|
||||
r.mu.Unlock()
|
||||
if len(written) >= n {
|
||||
return written
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("%s: %d records reported, want %d", kind, len(written), n)
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// newLiveApplyBoot builds a boot running the given snapshot, whose apply status
|
||||
// reports land in a recorder.
|
||||
func newLiveApplyBoot(t *testing.T, running config.Config) (*boot, *applyStatusRecorder) {
|
||||
t.Helper()
|
||||
recorder := &applyStatusRecorder{}
|
||||
b := &boot{
|
||||
cfg: running,
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
cfgHolder: config.NewHolder(running),
|
||||
processName: applystatus.Daemon,
|
||||
}
|
||||
b.applyStatus = applystatus.New(b.processName, recorder, b.log)
|
||||
return b, recorder
|
||||
}
|
||||
|
||||
// settingsWatchStub answers Query with one stored settings document and Watch
|
||||
// with a stream carrying the given documents. It is the seam the daemon really
|
||||
// meets a live update through: the control plane stores documents, and
|
||||
// controlplane.Client decodes them before the apply path sees anything.
|
||||
type settingsWatchStub struct {
|
||||
stored string
|
||||
version int64
|
||||
documents []string
|
||||
}
|
||||
|
||||
func (*settingsWatchStub) Catalog(context.Context, *pb.CatalogRequest, ...grpc.CallOption) (*pb.CatalogResponse, error) {
|
||||
return &pb.CatalogResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *settingsWatchStub) Query(context.Context, *pb.QueryRequest, ...grpc.CallOption) (*pb.QueryResponse, error) {
|
||||
return &pb.QueryResponse{Resource: &pb.Resource{Kind: controlplane.WorkflowExecutionSettingsKind, Version: s.version, ValueJson: []byte(s.stored)}}, nil
|
||||
}
|
||||
|
||||
func (*settingsWatchStub) History(context.Context, *pb.HistoryRequest, ...grpc.CallOption) (*pb.HistoryResponse, error) {
|
||||
panic("unexpected History")
|
||||
}
|
||||
|
||||
func (*settingsWatchStub) Audit(context.Context, *pb.AuditRequest, ...grpc.CallOption) (*pb.AuditResponse, error) {
|
||||
return &pb.AuditResponse{}, nil
|
||||
}
|
||||
|
||||
func (*settingsWatchStub) Command(context.Context, *pb.CommandRequest, ...grpc.CallOption) (*pb.CommandResponse, error) {
|
||||
panic("unexpected Command")
|
||||
}
|
||||
|
||||
func (s *settingsWatchStub) Watch(ctx context.Context, request *pb.WatchRequest, _ ...grpc.CallOption) (grpc.ServerStreamingClient[pb.WatchResponse], error) {
|
||||
_ = request
|
||||
responses := make([]*pb.WatchResponse, 0, len(s.documents))
|
||||
for i, document := range s.documents {
|
||||
responses = append(responses, &pb.WatchResponse{Resource: &pb.Resource{
|
||||
Kind: controlplane.WorkflowExecutionSettingsKind,
|
||||
Version: s.version + int64(i) + 1,
|
||||
ValueJson: []byte(document),
|
||||
}})
|
||||
}
|
||||
return &watchStreamStub{responses: responses, done: ctx.Done()}, nil
|
||||
}
|
||||
|
||||
// watchStreamStub is the client end of a Watch stream. Recv is the only method
|
||||
// the settings watch calls; the embedded ClientStream is nil, so any other call
|
||||
// panics rather than pretending to work.
|
||||
type watchStreamStub struct {
|
||||
grpc.ClientStream
|
||||
responses []*pb.WatchResponse
|
||||
done <-chan struct{}
|
||||
next int
|
||||
// onDeliver, when set, runs as the stream delivers each response, so a
|
||||
// stub standing in for the store can store what it just delivered.
|
||||
onDeliver func()
|
||||
}
|
||||
|
||||
func (s *watchStreamStub) Recv() (*pb.WatchResponse, error) {
|
||||
if s.next >= len(s.responses) {
|
||||
if s.done == nil {
|
||||
return nil, io.EOF
|
||||
}
|
||||
<-s.done
|
||||
return nil, context.Canceled
|
||||
}
|
||||
response := s.responses[s.next]
|
||||
s.next++
|
||||
if s.onDeliver != nil {
|
||||
s.onDeliver()
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// TestLiveExecutionSettingsUpdateIsCheckedBeforeItReplacesTheRunningLimits is
|
||||
// archie-core-nwa0. docs/prds/runtime-control-plane.md ("API") requires a live
|
||||
// change to replace a running component only after the new one has been
|
||||
// started and checked; if the check fails the old one keeps running. A live
|
||||
// workflow-execution-settings update replaces the configuration snapshot every
|
||||
// new task is built from, so a candidate whose snapshot this process cannot run
|
||||
// must leave the running limits and the running snapshot in place, and the
|
||||
// failure must reach the apply-status surface carrying the version that is
|
||||
// still live.
|
||||
func TestLiveExecutionSettingsUpdateIsCheckedBeforeItReplacesTheRunningLimits(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
candidate workflow.ExecutionSettings
|
||||
wantRunning workflow.ExecutionSettings
|
||||
wantVersion int64
|
||||
}{
|
||||
{
|
||||
name: "an update whose snapshot this process can run replaces the running limits",
|
||||
candidate: workflow.ExecutionSettings{MaxModelToolSteps: 25, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 4},
|
||||
wantRunning: workflow.ExecutionSettings{MaxModelToolSteps: 25, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 4},
|
||||
wantVersion: 2,
|
||||
},
|
||||
{
|
||||
// Zero is the documented "this individual limit is off", so an
|
||||
// all-zero update is a legal live change, not a rejection, and the
|
||||
// check must not invent a bound the store does not enforce.
|
||||
name: "an update that disables every limit is accepted",
|
||||
candidate: workflow.ExecutionSettings{},
|
||||
wantRunning: workflow.ExecutionSettings{},
|
||||
wantVersion: 2,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
b, status := newLiveApplyBoot(t, fileConfig())
|
||||
if err := b.applyWorkflowExecutionSettings(t.Context(), bootedSettings, 1); err != nil {
|
||||
t.Fatalf("applying the running settings: %v", err)
|
||||
}
|
||||
if err := b.applyWorkflowExecutionSettings(t.Context(), tt.candidate, 2); err != nil {
|
||||
t.Fatalf("live update: %v", err)
|
||||
}
|
||||
|
||||
got := b.executionSettings.Load()
|
||||
if got == nil {
|
||||
t.Fatal("no settings are recorded as running")
|
||||
}
|
||||
if *got != tt.wantRunning {
|
||||
t.Errorf("running settings = %+v, want %+v", *got, tt.wantRunning)
|
||||
}
|
||||
|
||||
// The snapshot a new task is built from has to move with the
|
||||
// settings: it is the half of the component the daemon runs.
|
||||
wantBudgets := budgetsFor(tt.wantRunning)
|
||||
if budgets := b.cfgHolder.Get().Budgets; budgets != wantBudgets {
|
||||
t.Errorf("running budgets = %+v, want %+v", budgets, wantBudgets)
|
||||
}
|
||||
|
||||
last, ok := status.last(controlplane.WorkflowExecutionSettingsKind)
|
||||
if !ok {
|
||||
t.Fatal("the live update reported no apply status")
|
||||
}
|
||||
if last.AppliedVersion != tt.wantVersion {
|
||||
t.Errorf("apply status version = %d, want %d", last.AppliedVersion, tt.wantVersion)
|
||||
}
|
||||
if last.Error != "" {
|
||||
t.Errorf("apply status error = %q, want none for an applied update", last.Error)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveExecutionSettingsUpdateIsRefusedWhenTheSnapshotIsNotRunnable covers
|
||||
// the refusal half of the same check. The candidate is the running snapshot
|
||||
// with new limits written into it, and configuration.Validate does not read
|
||||
// limits, so the only input that can fail the check is a snapshot this process
|
||||
// would not run -- which is what this test's daemon is running. A live update
|
||||
// must not publish a snapshot this process cannot run, must leave the component
|
||||
// it would have replaced in place, and must report the refusal against the
|
||||
// version still live.
|
||||
func TestLiveExecutionSettingsUpdateIsRefusedWhenTheSnapshotIsNotRunnable(t *testing.T) {
|
||||
unrunnable := fileConfig()
|
||||
unrunnable.Containers.Image = ""
|
||||
applyExecutionBudgets(&unrunnable, bootedSettings)
|
||||
b, status := newLiveApplyBoot(t, unrunnable)
|
||||
// The daemon is already running these limits. Seeded directly rather than
|
||||
// applied: the ordinary apply refuses this snapshot too, which is the point
|
||||
// of the check.
|
||||
b.executionSettings.Store(&bootedSettings)
|
||||
b.applyStatus.Report(t.Context(), controlplane.WorkflowExecutionSettingsKind, 1, nil)
|
||||
|
||||
budgets := budgetsFor(bootedSettings)
|
||||
update := workflow.ExecutionSettings{MaxModelToolSteps: 25, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 4}
|
||||
if err := b.applyWorkflowExecutionSettings(t.Context(), update, 2); err == nil {
|
||||
t.Fatal("an update whose snapshot this process cannot run was applied")
|
||||
}
|
||||
|
||||
if got := b.executionSettings.Load(); got == nil || *got != bootedSettings {
|
||||
t.Errorf("running settings = %v, want the refused update to leave %+v running", got, bootedSettings)
|
||||
}
|
||||
if got := b.cfgHolder.Get().Budgets; got != budgets {
|
||||
t.Errorf("running budgets = %+v, want the refused update to leave %+v running", got, budgets)
|
||||
}
|
||||
|
||||
last, ok := status.last(controlplane.WorkflowExecutionSettingsKind)
|
||||
if !ok {
|
||||
t.Fatal("the refused update reported no apply status")
|
||||
}
|
||||
if last.Error == "" {
|
||||
t.Error("the refusal was not reported through apply status")
|
||||
}
|
||||
if last.AppliedVersion != 1 {
|
||||
t.Errorf("apply status version = %d, want the version still live (1)", last.AppliedVersion)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveExecutionSettingsRefusalIsReportedThroughTheClient is the other half
|
||||
// of the same item, over the seam the daemon actually meets a refusal through.
|
||||
// controlplane.Client decodes every document it is handed, so an update this
|
||||
// process cannot run arrives as a watch error, never as invalid settings: the
|
||||
// watch branch must report that refusal against the version still live instead
|
||||
// of only logging it, or the settings page shows a live resource as healthy
|
||||
// while nothing has been applied. (The stream does not stay ended: the watch
|
||||
// re-establishes it, which control_plane_watch_test.go covers. This stub
|
||||
// answers every re-established stream with the same documents, so this test
|
||||
// reads the record the refusal wrote rather than counting how often it was
|
||||
// written.)
|
||||
func TestLiveExecutionSettingsRefusalIsReportedThroughTheClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
document string
|
||||
wantRunning workflow.ExecutionSettings
|
||||
wantVersion int64
|
||||
wantErrorContains string
|
||||
}{
|
||||
{
|
||||
name: "an update the process can run is applied",
|
||||
document: `{"max_model_tool_steps": 25, "max_runtime_seconds": 3600, "max_consecutive_gate_failures": 4}`,
|
||||
wantRunning: workflow.ExecutionSettings{MaxModelToolSteps: 25, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 4, MaxTaskRuntime: 4 * time.Hour},
|
||||
wantVersion: 2,
|
||||
},
|
||||
{
|
||||
name: "an update the client refuses is reported against the version still live",
|
||||
document: `{"max_model_tool_steps": -1, "max_runtime_seconds": 3600, "max_consecutive_gate_failures": 4}`,
|
||||
wantRunning: bootedSettings,
|
||||
wantVersion: 1,
|
||||
wantErrorContains: "max model/tool steps must not be negative",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
b, status := newLiveApplyBoot(t, fileConfig())
|
||||
b.controlPlane = controlplane.NewRPCClient(&settingsWatchStub{stored: bootedDocument, version: 1, documents: []string{tt.document}})
|
||||
|
||||
if err := b.startWorkflowExecutionSettings(t.Context()); err != nil {
|
||||
t.Fatalf("startWorkflowExecutionSettings: %v", err)
|
||||
}
|
||||
// One record for boot, one for the watched update.
|
||||
records := status.awaitCount(t, controlplane.WorkflowExecutionSettingsKind, 2)
|
||||
|
||||
got := b.executionSettings.Load()
|
||||
if got == nil {
|
||||
t.Fatal("no settings are recorded as running")
|
||||
}
|
||||
if *got != tt.wantRunning {
|
||||
t.Errorf("running settings = %+v, want %+v", *got, tt.wantRunning)
|
||||
}
|
||||
if budgets := b.cfgHolder.Get().Budgets; budgets != budgetsFor(tt.wantRunning) {
|
||||
t.Errorf("running budgets = %+v, want %+v", budgets, budgetsFor(tt.wantRunning))
|
||||
}
|
||||
|
||||
last := records[len(records)-1]
|
||||
if last.AppliedVersion != tt.wantVersion {
|
||||
t.Errorf("apply status version = %d, want %d", last.AppliedVersion, tt.wantVersion)
|
||||
}
|
||||
if tt.wantErrorContains == "" && last.Error != "" {
|
||||
t.Errorf("apply status error = %q, want none for an applied update", last.Error)
|
||||
}
|
||||
if tt.wantErrorContains != "" && !strings.Contains(last.Error, tt.wantErrorContains) {
|
||||
t.Errorf("apply status error = %q, want it to carry %q", last.Error, tt.wantErrorContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// budgetsFor is the configuration snapshot a set of limits produces. Named so
|
||||
// the tests and applyExecutionBudgets cannot drift on the mapping.
|
||||
func budgetsFor(settings workflow.ExecutionSettings) config.Budgets {
|
||||
return config.Budgets{
|
||||
MaxSteps: settings.MaxModelToolSteps,
|
||||
WallClock: config.Duration(settings.MaxRuntime),
|
||||
GateMaxFailures: settings.MaxConsecutiveGateFailures,
|
||||
TaskWallClock: config.Duration(settings.MaxTaskRuntime),
|
||||
}
|
||||
}
|
||||
@@ -1,203 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// controlPlaneStub answers Query from a fixed resource map. queryErr, when
|
||||
// set, fails every Query so a test can drive the unreachable-State-Store
|
||||
// path. A kind the map does not carry is answered the way the server answers
|
||||
// one with no stored value -- codes.NotFound (controlplane.mapError) -- so a
|
||||
// test can drive the absent-resource state a skipped seed leaves behind.
|
||||
type controlPlaneStub struct {
|
||||
values map[string]any
|
||||
queryErr error
|
||||
}
|
||||
|
||||
func (*controlPlaneStub) Catalog(context.Context, *pb.CatalogRequest, ...grpc.CallOption) (*pb.CatalogResponse, error) {
|
||||
return &pb.CatalogResponse{}, nil
|
||||
}
|
||||
|
||||
func (c *controlPlaneStub) Query(_ context.Context, request *pb.QueryRequest, _ ...grpc.CallOption) (*pb.QueryResponse, error) {
|
||||
if c.queryErr != nil {
|
||||
return nil, c.queryErr
|
||||
}
|
||||
value, ok := c.values[request.Kind]
|
||||
if !ok {
|
||||
return nil, status.Error(codes.NotFound, "resource not found")
|
||||
}
|
||||
encoded, err := json.Marshal(value)
|
||||
return &pb.QueryResponse{Resource: &pb.Resource{Kind: request.Kind, Version: 3, ValueJson: encoded}}, err
|
||||
}
|
||||
|
||||
func (*controlPlaneStub) History(context.Context, *pb.HistoryRequest, ...grpc.CallOption) (*pb.HistoryResponse, error) {
|
||||
panic("unexpected History")
|
||||
}
|
||||
|
||||
func (*controlPlaneStub) Audit(context.Context, *pb.AuditRequest, ...grpc.CallOption) (*pb.AuditResponse, error) {
|
||||
return &pb.AuditResponse{}, nil
|
||||
}
|
||||
|
||||
func (*controlPlaneStub) Command(context.Context, *pb.CommandRequest, ...grpc.CallOption) (*pb.CommandResponse, error) {
|
||||
panic("unexpected Command")
|
||||
}
|
||||
|
||||
func (*controlPlaneStub) Watch(context.Context, *pb.WatchRequest, ...grpc.CallOption) (grpc.ServerStreamingClient[pb.WatchResponse], error) {
|
||||
panic("unexpected Watch")
|
||||
}
|
||||
|
||||
// databaseOwnedResources is a control-plane projection whose every value
|
||||
// differs from fileConfig below, so any field that comes back with the file
|
||||
// value after a reload is a field the reload reverted.
|
||||
func databaseOwnedResources() map[string]any {
|
||||
return map[string]any{
|
||||
controlplane.ProviderSettingsKind: map[string]any{"main": map[string]any{"class": "openai", "base_url": "https://db.example.com"}},
|
||||
controlplane.ModelRoleAssignmentsKind: map[string]string{"builder": "main/db-model"},
|
||||
controlplane.RepositoryPoliciesKind: []config.Repo{{Owner: "acme", Name: "from-database"}},
|
||||
controlplane.ChannelSettingsKind: map[string]any{"operator": "database", "max_steps": 20, "models": []string{"main/db-model"}, "email": map[string]any{}, "webhook": map[string]any{}, "telegram": map[string]any{"allowed_user_ids": []int64{42}}, "rate_limit": map[string]any{}, "workspace": "/workspace"},
|
||||
controlplane.SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "max_retries": 7, "dispatch": map[string]any{"trigger": "assignee"}},
|
||||
controlplane.ToolSettingsKind: map[string]any{"mcp_servers": []map[string]any{}, "policy": map[string]any{}, "web_fetch": map[string]any{}, "minimax": map[string]any{}},
|
||||
controlplane.PluginSettingsKind: map[string]any{"plugin_dir": "/db/plugins", "module_dir": "/db/modules", "secret_engine_dir": "/db/secrets", "skills_dir": "/db/skills"},
|
||||
controlplane.ContainerRuntimePoliciesKind: config.ContainerConfig{Image: "archie:from-database", PullPolicy: "missing"},
|
||||
}
|
||||
}
|
||||
|
||||
// fileConfig is what the loader produces from config.toml alone: the layer a
|
||||
// SIGHUP reload re-resolves, holding none of the database's values. It is a
|
||||
// config the daemon would run, so it passes configuration.Validate -- the live
|
||||
// apply path runs that same check over the snapshot it is about to publish --
|
||||
// and every field validation reads is filled in the way the loader's defaults
|
||||
// fill it, so a kind with no stored value can fall back to this document (see
|
||||
// TestRuntimeConfigKeepsTheFileValueWhenAKindHasNoStoredResource).
|
||||
func fileConfig() config.Config {
|
||||
return config.Config{
|
||||
BotUser: "widget",
|
||||
Forge: config.Forge{Type: "github"},
|
||||
Dispatch: config.Dispatch{Trigger: "assignee"},
|
||||
Providers: map[string]config.Provider{"file": {Class: "openai"}},
|
||||
Models: map[string]string{"builder": "file/model"},
|
||||
Repos: []config.Repo{{Owner: "acme", Name: "from-file"}},
|
||||
Chat: config.ChatConfig{Operator: "file"},
|
||||
PollInterval: config.Duration(time.Minute),
|
||||
MaxRetries: 1,
|
||||
PluginDir: "/file/plugins",
|
||||
Containers: config.ContainerConfig{Image: "archie:from-file", PullPolicy: "missing"},
|
||||
Budgets: config.Budgets{MaxSteps: 1, WallClock: config.Duration(time.Minute)},
|
||||
}
|
||||
}
|
||||
|
||||
func newReloadBoot(t *testing.T, stub *controlPlaneStub) *boot {
|
||||
t.Helper()
|
||||
b := &boot{
|
||||
cfg: fileConfig(),
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
controlPlane: controlplane.NewRPCClient(stub),
|
||||
}
|
||||
b.cfgHolder = config.NewHolder(b.cfg)
|
||||
return b
|
||||
}
|
||||
|
||||
func TestRuntimeConfigResolvesStoredProviderReferences(t *testing.T) {
|
||||
for _, providerID := range []string{"main", "secondary"} {
|
||||
t.Run(providerID, func(t *testing.T) {
|
||||
refKey := "ARCHIE_TEST_STORED_PROVIDER_" + strings.ToUpper(providerID)
|
||||
t.Setenv(refKey, "provider-secret")
|
||||
resources := databaseOwnedResources()
|
||||
resources[controlplane.ProviderSettingsKind] = map[string]any{providerID: map[string]any{
|
||||
"class": "openai", "api_key_ref": map[string]any{"engine": "env", "key": refKey},
|
||||
}}
|
||||
b := newReloadBoot(t, &controlPlaneStub{values: resources})
|
||||
b.secrets = secret.NewRegistry()
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := b.cfgHolder.Get().Providers[providerID]
|
||||
if got.APIKey != (secret.SecretRef{}) || got.APIKeyEnv != providerSecretEnvName("root", providerID) {
|
||||
t.Fatalf("effective provider credential = %+v, want a resolved process-local env name", got)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Unsetenv(got.APIKeyEnv) })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestReloadConfigKeepsDatabaseOwnedSettings is archie-core-ju85. The SIGHUP
|
||||
// callback re-resolves the file document alone; without re-applying the
|
||||
// control plane over it, every database-owned setting reverts to its file
|
||||
// value until the process restarts, and the reload still logs success.
|
||||
func TestReloadConfigKeepsDatabaseOwnedSettings(t *testing.T) {
|
||||
b := newReloadBoot(t, &controlPlaneStub{values: databaseOwnedResources()})
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
if err := b.applyWorkflowExecutionSettings(t.Context(), workflow.ExecutionSettings{
|
||||
MaxModelToolSteps: 99, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 5,
|
||||
}, 4); err != nil {
|
||||
t.Fatalf("applyWorkflowExecutionSettings: %v", err)
|
||||
}
|
||||
|
||||
if err := b.reloadConfig(t.Context(), &configuration.Document{Config: fileConfig()}); err != nil {
|
||||
t.Fatalf("reloadConfig: %v", err)
|
||||
}
|
||||
|
||||
got := b.cfgHolder.Get()
|
||||
for _, tt := range []struct {
|
||||
field string
|
||||
got any
|
||||
want any
|
||||
}{
|
||||
{"Providers", got.Providers["main"].BaseURL, "https://db.example.com"},
|
||||
{"Models", got.Models["builder"], "main/db-model"},
|
||||
{"Repos", got.Repos[0].Name, "from-database"},
|
||||
{"Chat", got.Chat.Operator, "database"},
|
||||
{"PollInterval", time.Duration(got.PollInterval), 2 * time.Minute},
|
||||
{"MaxRetries", got.MaxRetries, 7},
|
||||
{"PluginDir", got.PluginDir, "/db/plugins"},
|
||||
{"Containers", got.Containers.Image, "archie:from-database"},
|
||||
{"Budgets", got.Budgets.MaxSteps, 99},
|
||||
} {
|
||||
if tt.got != tt.want {
|
||||
t.Errorf("%s reverted to its file value after reload: got %v, want %v", tt.field, tt.got, tt.want)
|
||||
}
|
||||
}
|
||||
if got.BotUser != "widget" {
|
||||
t.Errorf("file-owned BotUser = %q, want the reloaded file value", got.BotUser)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReloadConfigFailsWhenControlPlaneUnreachable pins the direction of the
|
||||
// failure: the running config already holds the database's values, so a
|
||||
// control plane that cannot answer must abort the reload rather than publish
|
||||
// the file's values over them.
|
||||
func TestReloadConfigFailsWhenControlPlaneUnreachable(t *testing.T) {
|
||||
stub := &controlPlaneStub{values: databaseOwnedResources()}
|
||||
b := newReloadBoot(t, stub)
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
|
||||
stub.queryErr = context.DeadlineExceeded
|
||||
err := b.reloadConfig(t.Context(), &configuration.Document{Config: fileConfig()})
|
||||
if err == nil {
|
||||
t.Fatal("reloadConfig with an unreachable control plane: expected an error")
|
||||
}
|
||||
if got := b.cfgHolder.Get(); got.Models["builder"] != "main/db-model" {
|
||||
t.Fatalf("running config was replaced on the error path: Models = %v", got.Models)
|
||||
}
|
||||
}
|
||||
@@ -1,273 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// resourceWatchStub answers Query with one stored version per kind and Watch
|
||||
// with the documents queued for the watched kind. It is the store end of the
|
||||
// seam the runtime-resource watch meets a live update through, and it is the
|
||||
// store: delivering a document stores it, so the layering the apply runs
|
||||
// reads the update back from Query the way production does. A kind the map
|
||||
// does not carry is answered codes.NotFound, the state a skipped seed leaves
|
||||
// behind.
|
||||
type resourceWatchStub struct {
|
||||
values map[string]any
|
||||
// version is the stored version until a watch delivers an update, which
|
||||
// is the version the store then answers Query with -- the apply the
|
||||
// delivery triggers layers and reports the version the store now holds.
|
||||
version int64
|
||||
// storedVersion, when present, is the version Query answers a kind with.
|
||||
storedVersion map[string]int64
|
||||
// watch queues, per kind, the documents a live write delivers after the
|
||||
// stored version, one per stream: the first open delivers the first
|
||||
// document, the second open the second, and so on.
|
||||
watch map[string][]string
|
||||
// opens counts how often each kind's stream was opened, so queued
|
||||
// documents are delivered in order across the reconnects keepWatch runs.
|
||||
opens map[string]int
|
||||
}
|
||||
|
||||
func (s *resourceWatchStub) versionOf(kind string) int64 {
|
||||
if stored, ok := s.storedVersion[kind]; ok {
|
||||
return stored
|
||||
}
|
||||
return s.version
|
||||
}
|
||||
|
||||
func (*resourceWatchStub) Catalog(context.Context, *pb.CatalogRequest, ...grpc.CallOption) (*pb.CatalogResponse, error) {
|
||||
return &pb.CatalogResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *resourceWatchStub) Query(_ context.Context, request *pb.QueryRequest, _ ...grpc.CallOption) (*pb.QueryResponse, error) {
|
||||
value, ok := s.values[request.Kind]
|
||||
if !ok {
|
||||
return nil, status.Error(codes.NotFound, "resource not found")
|
||||
}
|
||||
encoded, err := json.Marshal(value)
|
||||
return &pb.QueryResponse{Resource: &pb.Resource{Kind: request.Kind, Version: s.versionOf(request.Kind), ValueJson: encoded}}, err
|
||||
}
|
||||
|
||||
func (s *resourceWatchStub) Watch(_ context.Context, request *pb.WatchRequest, _ ...grpc.CallOption) (grpc.ServerStreamingClient[pb.WatchResponse], error) {
|
||||
// One queued document per stream: a stream ends after the document it
|
||||
// carries, and the next one arrives on the reconnect -- the shape a store
|
||||
// whose writes are spaced apart produces, and the shape that keeps the
|
||||
// apply each delivery triggers reading the document back from Query
|
||||
// before the store has moved on to the next one.
|
||||
open := s.opens[request.Kind]
|
||||
s.opens[request.Kind] = open + 1
|
||||
if open >= len(s.watch[request.Kind]) {
|
||||
return &watchStreamStub{}, nil
|
||||
}
|
||||
document := s.watch[request.Kind][open]
|
||||
version := s.version + int64(open) + 1
|
||||
if version <= request.AfterVersion {
|
||||
return &watchStreamStub{}, nil
|
||||
}
|
||||
// The stub is the store: a document is stored when the stream delivers
|
||||
// it, so the apply that delivery triggers layers the document back from
|
||||
// Query.
|
||||
response := &pb.WatchResponse{Resource: &pb.Resource{
|
||||
Kind: request.Kind, Version: version, ValueJson: []byte(document),
|
||||
}}
|
||||
return &watchStreamStub{responses: []*pb.WatchResponse{response}, onDeliver: func() {
|
||||
s.values[request.Kind] = json.RawMessage(document)
|
||||
s.storedVersion[request.Kind] = version
|
||||
}}, nil
|
||||
}
|
||||
|
||||
func (*resourceWatchStub) History(context.Context, *pb.HistoryRequest, ...grpc.CallOption) (*pb.HistoryResponse, error) {
|
||||
panic("unexpected History")
|
||||
}
|
||||
|
||||
func (*resourceWatchStub) Audit(context.Context, *pb.AuditRequest, ...grpc.CallOption) (*pb.AuditResponse, error) {
|
||||
return &pb.AuditResponse{}, nil
|
||||
}
|
||||
|
||||
func (*resourceWatchStub) Command(context.Context, *pb.CommandRequest, ...grpc.CallOption) (*pb.CommandResponse, error) {
|
||||
panic("unexpected Command")
|
||||
}
|
||||
|
||||
// newResourceWatchBoot builds a boot whose apply status lands in a recorder,
|
||||
// with the chat model runtime a provider or role update must rebuild.
|
||||
func newResourceWatchBoot(t *testing.T, stub *resourceWatchStub) (*boot, *applyStatusRecorder) {
|
||||
t.Helper()
|
||||
b, recorder := newLiveApplyBoot(t, fileConfig())
|
||||
b.controlPlane = controlplane.NewRPCClient(stub)
|
||||
b.secrets = secret.NewRegistry()
|
||||
b.chatModels = newChatModelManager(fileConfig().Models)
|
||||
b.setLLM(agentexec.NewRuntime(executionProviders(fileConfig())))
|
||||
return b, recorder
|
||||
}
|
||||
|
||||
// schedulingDocument renders a scheduling-policy document the layering reads
|
||||
// back, so the applied and refused updates in these tests cannot disagree on
|
||||
// what "the stored scheduling policy" is.
|
||||
func schedulingDocument(interval string) string {
|
||||
return `{"poll_interval": "` + interval + `", "max_retries": 7, "dispatch": {"trigger": "assignee"}}`
|
||||
}
|
||||
|
||||
// TestRuntimeResourceWatchReLayersTheStoredChangeAndRepublishes is the live
|
||||
// apply for the four kinds the daemon watches (archie-core-zfb0.1): a new
|
||||
// stored version of scheduling-policy re-runs the layering a reload runs and
|
||||
// republishes through config.Holder, so the change takes effect without a
|
||||
// restart, the apply status records the version that was applied, and every
|
||||
// other database-owned setting the layering read is still in the running
|
||||
// config.
|
||||
func TestRuntimeResourceWatchReLayersTheStoredChangeAndRepublishes(t *testing.T) {
|
||||
values := databaseOwnedResources()
|
||||
stub := &resourceWatchStub{values: values, version: 3, storedVersion: map[string]int64{}, opens: map[string]int{}, watch: map[string][]string{
|
||||
controlplane.SchedulingPolicyKind: {schedulingDocument("5m")},
|
||||
}}
|
||||
b, status := newResourceWatchBoot(t, stub)
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
if err := b.startRuntimeResourceWatches(ctx, b.runtimeVersions); err != nil {
|
||||
t.Fatalf("startRuntimeResourceWatches: %v", err)
|
||||
}
|
||||
// One record for boot's layering, one for the watched update.
|
||||
records := status.awaitCount(t, controlplane.SchedulingPolicyKind, 2)
|
||||
|
||||
cfg := b.cfgHolder.Get()
|
||||
if got := time.Duration(cfg.PollInterval); got != 5*time.Minute {
|
||||
t.Errorf("running PollInterval = %s, want the watched update (5m)", got)
|
||||
}
|
||||
if got := cfg.Repos[0].Name; got != "from-database" {
|
||||
t.Errorf("running Repos[0] = %q, want the other kinds' last values to survive the re-layering", got)
|
||||
}
|
||||
last := records[len(records)-1]
|
||||
if last.AppliedVersion != stub.version+1 {
|
||||
t.Errorf("apply status version = %d, want the delivered version %d", last.AppliedVersion, stub.version+1)
|
||||
}
|
||||
if last.Error != "" {
|
||||
t.Errorf("apply status error = %q, want none for an applied update", last.Error)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRuntimeResourceWatchKeepsLastKnownGoodWhenTheUpdateIsRefused: an update
|
||||
// the layered document cannot run is refused before it is published, the
|
||||
// running config keeps the last version that was applied, and the refusal is
|
||||
// reported through apply status against the version still live.
|
||||
func TestRuntimeResourceWatchKeepsLastKnownGoodWhenTheUpdateIsRefused(t *testing.T) {
|
||||
stub := &resourceWatchStub{values: databaseOwnedResources(), version: 3, storedVersion: map[string]int64{}, opens: map[string]int{}, watch: map[string][]string{
|
||||
controlplane.SchedulingPolicyKind: {schedulingDocument("5m"), schedulingDocument("0s")},
|
||||
}}
|
||||
b, status := newResourceWatchBoot(t, stub)
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
if err := b.startRuntimeResourceWatches(ctx, b.runtimeVersions); err != nil {
|
||||
t.Fatalf("startRuntimeResourceWatches: %v", err)
|
||||
}
|
||||
// One record for boot's layering, one for the applied update, one for the
|
||||
// refusal. The recorder keeps the version that is still live on a refusal,
|
||||
// so the third record carries the second update's version.
|
||||
records := status.awaitCount(t, controlplane.SchedulingPolicyKind, 3)
|
||||
|
||||
if got := time.Duration(b.cfgHolder.Get().PollInterval); got != 5*time.Minute {
|
||||
t.Errorf("running PollInterval = %s, want the last update that validated (5m)", got)
|
||||
}
|
||||
last := records[len(records)-1]
|
||||
if last.AppliedVersion != stub.version+1 {
|
||||
t.Errorf("apply status version = %d, want the version still live (%d)", last.AppliedVersion, stub.version+1)
|
||||
}
|
||||
if !strings.Contains(last.Error, "poll_interval must be positive") {
|
||||
t.Errorf("apply status error = %q, want it to carry the refusal", last.Error)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveModelRoleUpdateRebuildsTheChatModelRuntime: providers and model
|
||||
// role assignments also feed the gateway chat runtime, so an update to either
|
||||
// kind swaps the runtime (ai-sdk's Runtime caches the provider instances it
|
||||
// built) and re-derives the model list the chat surfaces offer.
|
||||
func TestLiveModelRoleUpdateRebuildsTheChatModelRuntime(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
kind string
|
||||
}{
|
||||
{"a provider settings update", controlplane.ProviderSettingsKind},
|
||||
{"a model role assignments update", controlplane.ModelRoleAssignmentsKind},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
values := databaseOwnedResources()
|
||||
values[controlplane.ModelRoleAssignmentsKind] = map[string]string{
|
||||
"builder": "main/db-model", "chat": "main/db-model",
|
||||
}
|
||||
stub := &resourceWatchStub{values: values, version: 3, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _ := newResourceWatchBoot(t, stub)
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
before := b.chatLLM()
|
||||
|
||||
b.applyRuntimeResourceUpdate(t.Context(), tt.kind, controlplane.AppliedResource{Version: 4})
|
||||
|
||||
if after := b.chatLLM(); after == before {
|
||||
t.Error("the chat runtime was not rebuilt; the next turn keeps the previous provider set")
|
||||
}
|
||||
if !slices.Contains(b.chatModels.Models(), "main/db-model") {
|
||||
t.Errorf("chat models = %v, want the new role assignment's reference", b.chatModels.Models())
|
||||
}
|
||||
if got := b.chatModels.ActiveModel(); got != "main/db-model" {
|
||||
t.Errorf("chat active model = %q, want the chat role's model after the update", got)
|
||||
}
|
||||
// The running config carries the update, not just the chat runtime.
|
||||
if got := b.cfgHolder.Get().Models["chat"]; got != "main/db-model" {
|
||||
t.Errorf("running chat role = %q, want the applied assignment", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveContainerRuntimePoliciesReLayerAndResizeTheDispatcher pins the
|
||||
// container kind's live seam (archie-core-zfb0.2) at the apply path: a stored
|
||||
// container-runtime-policies document re-layers into the running config the
|
||||
// pool reads per acquire, and the daemon resizes its running dispatcher from
|
||||
// the same publish rather than requiring a restart.
|
||||
func TestLiveContainerRuntimePoliciesReLayerAndResizeTheDispatcher(t *testing.T) {
|
||||
values := databaseOwnedResources()
|
||||
values[controlplane.ContainerRuntimePoliciesKind] = map[string]any{
|
||||
"image": "archie:live", "max_concurrency": 5, "pull_policy": "missing",
|
||||
}
|
||||
stub := &resourceWatchStub{values: values, version: 3, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _ := newResourceWatchBoot(t, stub)
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
// A running daemon, so the resize arm executes against a real dispatcher
|
||||
// rather than taking the boot-window nil guard.
|
||||
b.d = &daemon.Daemon{Cfg: b.cfgHolder, Log: b.log}
|
||||
|
||||
b.applyRuntimeResourceUpdate(t.Context(), controlplane.ContainerRuntimePoliciesKind, controlplane.AppliedResource{Version: 4})
|
||||
|
||||
got := b.cfgHolder.Get().Containers
|
||||
if got.MaxConcurrency != 5 {
|
||||
t.Errorf("running max_concurrency = %d, want the stored 5", got.MaxConcurrency)
|
||||
}
|
||||
if got.Image != "archie:live" {
|
||||
t.Errorf("running image = %q, want the stored archie:live", got.Image)
|
||||
}
|
||||
}
|
||||
@@ -1,440 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/grpc/test/bufconn"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// restartAdminToken is the administrative credential the State Store's
|
||||
// TaskGrants interceptor accepts. PutApplyStatus and ListApplyStatus are
|
||||
// administrative (docs/prds/control-plane-apply-status.md): a task-scoped
|
||||
// grant is denied by authorizesTaskScopedCall's deny-by-default arm, so the
|
||||
// harness that reads and writes apply status must dial as the administrator.
|
||||
const restartAdminToken = "eju6-admin-token"
|
||||
|
||||
// documentedStaleWindow is the window the operator-facing contract documents:
|
||||
// every process re-stamps its records every 30 seconds, and "a record is stale
|
||||
// once it is older than 90 seconds" (docs/prds/control-plane-apply-status.md,
|
||||
// "What is reported"). It is a fixture literal on purpose. Reading
|
||||
// applystatus.StaleAfter here would derive the aged row from the constant the
|
||||
// reader is supposed to honour, so the fixture would move with any mutation of
|
||||
// that constant and the reading below would agree with the reader by
|
||||
// construction -- a test that passes for a window measured in hours is a test
|
||||
// that never went stale at all.
|
||||
const documentedStaleWindow = 90 * time.Second
|
||||
|
||||
// restartEditedSettings is the change the dashboard writes. It differs from
|
||||
// fileConfig()'s [budgets] on all three fields, so a process that comes back on
|
||||
// any file value is detectably not on the stored one.
|
||||
var restartEditedSettings = workflow.ExecutionSettings{
|
||||
MaxModelToolSteps: 40, MaxRuntime: 10 * time.Minute, MaxConsecutiveGateFailures: 2, MaxTaskRuntime: 4 * time.Hour,
|
||||
}
|
||||
|
||||
// controlPlaneRestartFixture is a real State Store for the restart test: the
|
||||
// production control plane and apply-status surfaces registered on the real
|
||||
// gRPC service, behind TaskGrants' admin/task-grant interceptors, over a real
|
||||
// SQLite store. It is the composition state_store.go serves, not a stub.
|
||||
type controlPlaneRestartFixture struct {
|
||||
listener *bufconn.Listener
|
||||
admin *staterpc.Client
|
||||
}
|
||||
|
||||
// newControlPlaneRestartFixture opens the store, seeds it the way
|
||||
// archie-state-store does on a fresh database, serves the contract over
|
||||
// bufconn, and returns an administrative client for the dashboard and for
|
||||
// reading apply status back.
|
||||
func newControlPlaneRestartFixture(t *testing.T) *controlPlaneRestartFixture {
|
||||
t.Helper()
|
||||
local := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = local.Close() })
|
||||
|
||||
steps, err := stepVocabulary()
|
||||
if err != nil {
|
||||
t.Fatalf("build step vocabulary: %v", err)
|
||||
}
|
||||
control, err := controlplane.NewServer(local, steps)
|
||||
if err != nil {
|
||||
t.Fatalf("build control plane server: %v", err)
|
||||
}
|
||||
// The seed the State Store runs before it serves: every kind lands at
|
||||
// version 1 holding the file document's value. A kind the validator refused
|
||||
// would be absent, and the assertions below would then be testing a missing
|
||||
// resource rather than a restart, so this test names the two kinds it needs.
|
||||
versions, _, err := control.ImportConfig(t.Context(), fileConfig())
|
||||
if err != nil {
|
||||
t.Fatalf("seed control plane resources: %v", err)
|
||||
}
|
||||
for _, kind := range []string{controlplane.WorkflowExecutionSettingsKind, controlplane.PluginSettingsKind} {
|
||||
if versions[kind] != 1 {
|
||||
t.Fatalf("%s seeded at version %d, want 1", kind, versions[kind])
|
||||
}
|
||||
}
|
||||
|
||||
grants := &staterpc.TaskGrants{}
|
||||
listener := bufconn.Listen(1 << 20)
|
||||
server := grpc.NewServer(
|
||||
grpc.ChainUnaryInterceptor(grants.UnaryInterceptor(restartAdminToken)),
|
||||
grpc.ChainStreamInterceptor(grants.StreamInterceptor(restartAdminToken)),
|
||||
)
|
||||
staterpc.RegisterServer(server, staterpc.Deps{
|
||||
ControlPlane: control, Tasks: local, ApplyStatus: local, Grants: grants,
|
||||
})
|
||||
go func() { _ = server.Serve(listener) }()
|
||||
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
|
||||
|
||||
return &controlPlaneRestartFixture{
|
||||
listener: listener,
|
||||
admin: dialStateStore(t, listener, restartAdminToken),
|
||||
}
|
||||
}
|
||||
|
||||
// dial dials a fresh administrative connection, what a restarted process would
|
||||
// open to the State Store it does not own.
|
||||
func (f *controlPlaneRestartFixture) dial(t *testing.T) *staterpc.Client {
|
||||
t.Helper()
|
||||
return dialStateStore(t, f.listener, restartAdminToken)
|
||||
}
|
||||
|
||||
// dialStateStore is the production client wiring (staterpc.Dial, the only dial
|
||||
// path the daemon and dashboard use) against the non-loopback bridge address
|
||||
// every container-mode profile uses, with the dialer redirected to bufconn.
|
||||
func dialStateStore(t *testing.T, listener *bufconn.Listener, token string) *staterpc.Client {
|
||||
t.Helper()
|
||||
client, cleanup, err := staterpc.Dial("172.17.0.1:9090", token, grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
|
||||
return listener.DialContext(ctx)
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("dial state store: %v", err)
|
||||
}
|
||||
t.Cleanup(cleanup)
|
||||
return client
|
||||
}
|
||||
|
||||
// startRestartProcess composes the consuming half of archied the way run()
|
||||
// composes it: boot.runtimeConfig layers the stored restart-required kinds over
|
||||
// the file document, and the watched execution settings are applied and
|
||||
// recorded from there. Apply status is reported through the State Store
|
||||
// contract by the process's own reporter.
|
||||
func startRestartProcess(ctx context.Context, t *testing.T, client *staterpc.Client) *boot {
|
||||
t.Helper()
|
||||
base := fileConfig()
|
||||
b := &boot{
|
||||
cfg: base,
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
processName: applystatus.Daemon,
|
||||
cfgHolder: config.NewHolder(base),
|
||||
controlPlane: controlplane.NewRPCClient(client.ControlPlane()),
|
||||
}
|
||||
b.applyStatus = applystatus.New(b.processName, client, b.log)
|
||||
if err := b.loadRuntimeConfig(ctx); err != nil {
|
||||
t.Fatalf("load runtime config: %v", err)
|
||||
}
|
||||
if err := b.startWorkflowExecutionSettings(ctx); err != nil {
|
||||
t.Fatalf("start workflow execution settings: %v", err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// TestRestartKeepsADatabaseOwnedSettingsChange is archie-core-eju6, the
|
||||
// end-to-end restart test docs/prds/runtime-control-plane.md ("First
|
||||
// implementation") lists as a deliverable. It drives settings changes through
|
||||
// the dashboard's own control-plane handler, reads one back from the audit
|
||||
// trail, restarts the process that consumes it, and asserts the restarted
|
||||
// process is running the stored values rather than the file's -- across both
|
||||
// halves of boot.runtimeConfig: the queried restart-required kinds and the
|
||||
// watched execution budgets (archie-core-ju85).
|
||||
//
|
||||
// The restart is a fresh boot composition dialing a fresh connection to the
|
||||
// State Store that holds the change. State Store persistence is what an OS
|
||||
// restart preserves; re-creating the consumer is what an OS restart does to
|
||||
// the consumer, so nothing in the property under test is substituted.
|
||||
func TestRestartKeepsADatabaseOwnedSettingsChange(t *testing.T) {
|
||||
fixture := newControlPlaneRestartFixture(t)
|
||||
dashboard := &webui.Server{ControlPlane: fixture.admin.ControlPlane(), ApplyStatus: fixture.admin}
|
||||
|
||||
// A process is already running on the seeded values. It is stopped before
|
||||
// the edit so the restart below -- not the live watch -- is what reads the
|
||||
// change back.
|
||||
firstCtx, stopFirst := context.WithCancel(t.Context())
|
||||
first := startRestartProcess(firstCtx, t, fixture.dial(t))
|
||||
stopFirst()
|
||||
if got := first.cfgHolder.Get().Budgets; got != budgetsFor(workflow.ExecutionSettings{MaxModelToolSteps: 1, MaxRuntime: time.Minute, MaxTaskRuntime: 4 * time.Hour}) {
|
||||
t.Fatalf("first process budgets = %+v, want the seeded file budgets", got)
|
||||
}
|
||||
if got := first.cfgHolder.Get().PluginDir; got != fileConfig().PluginDir {
|
||||
t.Fatalf("first process plugin dir = %q, want the file's", got)
|
||||
}
|
||||
|
||||
// 1. The dashboard writes the change. Its handler, not this test, supplies
|
||||
// the attribution: webAudit attributes to identity.SystemID.
|
||||
expectedVersion := int64(1)
|
||||
executionSettingsDocument := []byte(`{"max_model_tool_steps":40,"max_runtime_seconds":600,"max_consecutive_gate_failures":2,"max_task_runtime_seconds":14400}`)
|
||||
if version := dashboardReplace(t, dashboard, controlplane.WorkflowExecutionSettingsKind, executionSettingsDocument, expectedVersion); version != 2 {
|
||||
t.Fatalf("workflow execution settings after edit = version %d, want 2", version)
|
||||
}
|
||||
pluginDocument := []byte(`{"plugin_dir":"/db/plugins","module_dir":"/db/modules","secret_engine_dir":"/db/secrets","skills_dir":"/db/skills"}`)
|
||||
if version := dashboardReplace(t, dashboard, controlplane.PluginSettingsKind, pluginDocument, expectedVersion); version != 2 {
|
||||
t.Fatalf("plugin settings after edit = version %d, want 2", version)
|
||||
}
|
||||
|
||||
// 2. The change lands in the audit trail as an ordinary operator edit: the
|
||||
// newest revision names the dashboard and the System identity, and carries
|
||||
// the version it replaced.
|
||||
revisions := dashboardHistory(t, dashboard, controlplane.WorkflowExecutionSettingsKind)
|
||||
if len(revisions) != 2 {
|
||||
t.Fatalf("revisions = %d, want the seed and the edit", len(revisions))
|
||||
}
|
||||
edit := revisions[0]
|
||||
if edit.Version != 2 || edit.Actor != string(identity.SystemID) || edit.Source != "archie-ui" || edit.RequestID == "" {
|
||||
t.Fatalf("newest revision audit = %+v, want the dashboard's System-attributed edit at version 2", edit)
|
||||
}
|
||||
if revisions[1].Actor != "system:migration" || revisions[1].Version != 1 {
|
||||
t.Fatalf("older revision = %+v, want the migration seed it replaced", revisions[1])
|
||||
}
|
||||
// The value rides along, so a restore is an ordinary replace of it.
|
||||
if got := decodeExecutionSettingsRevision(t, edit.Value); got != restartEditedSettings {
|
||||
t.Fatalf("revision value = %+v, want the edited %+v", got, restartEditedSettings)
|
||||
}
|
||||
|
||||
// 3-4. Restart the consumer. It re-resolves the same file document and
|
||||
// must come back on the stored values for both the queried kind and the
|
||||
// watched budgets.
|
||||
restartCtx, stopRestart := context.WithCancel(t.Context())
|
||||
defer stopRestart()
|
||||
restarted := startRestartProcess(restartCtx, t, fixture.dial(t))
|
||||
|
||||
settings := restarted.executionSettings.Load()
|
||||
if settings == nil {
|
||||
t.Fatal("restarted process recorded no workflow execution settings")
|
||||
}
|
||||
if *settings != restartEditedSettings {
|
||||
t.Errorf("restarted settings = %+v, want the stored %+v", *settings, restartEditedSettings)
|
||||
}
|
||||
if got := restarted.cfgHolder.Get().Budgets; got != budgetsFor(restartEditedSettings) {
|
||||
t.Errorf("restarted budgets = %+v, want the stored %+v", got, budgetsFor(restartEditedSettings))
|
||||
}
|
||||
if got := restarted.cfgHolder.Get().PluginDir; got != "/db/plugins" {
|
||||
t.Errorf("restarted plugin dir = %q, want the stored /db/plugins, not the file's %q", got, fileConfig().PluginDir)
|
||||
}
|
||||
|
||||
// The SIGHUP reload re-resolves the file document alone. It runs the same
|
||||
// boot.runtimeConfig, so it must not revert either half to its file value
|
||||
// (archie-core-ju85).
|
||||
if err := restarted.reloadConfig(restartCtx, &configuration.Document{Config: fileConfig()}); err != nil {
|
||||
t.Fatalf("reload: %v", err)
|
||||
}
|
||||
if got := restarted.cfgHolder.Get().Budgets; got != budgetsFor(restartEditedSettings) {
|
||||
t.Errorf("budgets after reload = %+v, want the stored %+v re-applied", got, budgetsFor(restartEditedSettings))
|
||||
}
|
||||
if got := restarted.cfgHolder.Get().PluginDir; got != "/db/plugins" {
|
||||
t.Errorf("plugin dir after reload = %q, want the stored /db/plugins", got)
|
||||
}
|
||||
|
||||
// 5. The restarted process reports the version it applied, for both halves,
|
||||
// and a record that has stopped being re-stamped reads as not reporting.
|
||||
reported, err := fixture.admin.ListApplyStatus(t.Context())
|
||||
if err != nil {
|
||||
t.Fatalf("list apply status: %v", err)
|
||||
}
|
||||
for _, kind := range []string{controlplane.WorkflowExecutionSettingsKind, controlplane.PluginSettingsKind} {
|
||||
record := applyStatusFor(t, reported, applystatus.Daemon, kind)
|
||||
if record.AppliedVersion != 2 {
|
||||
t.Errorf("%s apply status version = %d, want the applied 2", kind, record.AppliedVersion)
|
||||
}
|
||||
if record.Error != "" {
|
||||
t.Errorf("%s apply status error = %q, want none for an applied version", kind, record.Error)
|
||||
}
|
||||
}
|
||||
|
||||
statusView := dashboardApplyStatus(t, dashboard)
|
||||
live := applyStatusViewFor(t, statusView, applystatus.Daemon, controlplane.WorkflowExecutionSettingsKind)
|
||||
if live.State != "current" {
|
||||
t.Errorf("a record re-stamped at restart reads %q, want current", live.State)
|
||||
}
|
||||
|
||||
// One record per (process, kind), replaced in place: an old report for the
|
||||
// same key replaces the live one rather than sitting beside it.
|
||||
stale := time.Now().Add(-documentedStaleWindow - time.Second)
|
||||
if err := fixture.admin.PutApplyStatus(t.Context(), storecontract.ApplyStatus{
|
||||
Process: applystatus.Daemon, Kind: controlplane.WorkflowExecutionSettingsKind,
|
||||
AppliedVersion: 2, ReportedAt: stale,
|
||||
}); err != nil {
|
||||
t.Fatalf("put stale apply status: %v", err)
|
||||
}
|
||||
statusView = dashboardApplyStatus(t, dashboard)
|
||||
aged := applyStatusViewFor(t, statusView, applystatus.Daemon, controlplane.WorkflowExecutionSettingsKind)
|
||||
if aged.State != "unknown" {
|
||||
t.Errorf("a record older than %v reads %q, want unknown rather than current", documentedStaleWindow, aged.State)
|
||||
}
|
||||
if count := countApplyStatusViews(statusView, applystatus.Daemon, controlplane.WorkflowExecutionSettingsKind); count != 1 {
|
||||
t.Errorf("rows for (%s, %s) = %d, want the report replaced in place", applystatus.Daemon, controlplane.WorkflowExecutionSettingsKind, count)
|
||||
}
|
||||
|
||||
// The administrative credential is what admits the report: a task-scoped
|
||||
// grant, which the daemon issues to agent containers, is refused by both
|
||||
// apply-status RPCs.
|
||||
grant, err := fixture.admin.RegisterTaskGrant(t.Context(), 1, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("register task grant: %v", err)
|
||||
}
|
||||
taskScoped := dialStateStore(t, fixture.listener, grant)
|
||||
if _, err := taskScoped.ListApplyStatus(t.Context()); status.Code(err) != codes.PermissionDenied {
|
||||
t.Errorf("ListApplyStatus with a task-scoped grant = %v, want PermissionDenied", err)
|
||||
}
|
||||
if err := taskScoped.PutApplyStatus(t.Context(), storecontract.ApplyStatus{Process: applystatus.Daemon, Kind: controlplane.PluginSettingsKind, AppliedVersion: 9}); status.Code(err) != codes.PermissionDenied {
|
||||
t.Errorf("PutApplyStatus with a task-scoped grant = %v, want PermissionDenied", err)
|
||||
}
|
||||
}
|
||||
|
||||
type controlPlaneRevision struct {
|
||||
Version int64 `json:"version"`
|
||||
Value json.RawMessage `json:"value"`
|
||||
Actor string `json:"actor"`
|
||||
Source string `json:"source"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
|
||||
type applyStatusRecord struct {
|
||||
Process string `json:"process"`
|
||||
Kind string `json:"kind"`
|
||||
AppliedVersion int64 `json:"applied_version"`
|
||||
Error string `json:"error"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
// decodeExecutionSettingsRevision reads a revision's value in the same shape
|
||||
// the stored document uses, so the audit assertion compares values rather than
|
||||
// just versions.
|
||||
func decodeExecutionSettingsRevision(t *testing.T, value json.RawMessage) workflow.ExecutionSettings {
|
||||
t.Helper()
|
||||
var document struct {
|
||||
MaxModelToolSteps int `json:"max_model_tool_steps"`
|
||||
MaxRuntimeSeconds int64 `json:"max_runtime_seconds"`
|
||||
MaxConsecutiveGateFailures int `json:"max_consecutive_gate_failures"`
|
||||
MaxTaskRuntimeSeconds int64 `json:"max_task_runtime_seconds"`
|
||||
}
|
||||
if err := json.Unmarshal(value, &document); err != nil {
|
||||
t.Fatalf("decode revision value: %v (%s)", err, value)
|
||||
}
|
||||
return workflow.ExecutionSettings{
|
||||
MaxModelToolSteps: document.MaxModelToolSteps,
|
||||
MaxRuntime: time.Duration(document.MaxRuntimeSeconds) * time.Second,
|
||||
MaxConsecutiveGateFailures: document.MaxConsecutiveGateFailures,
|
||||
MaxTaskRuntime: time.Duration(document.MaxTaskRuntimeSeconds) * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
// dashboardReplace writes a resource the way the dashboard does -- through the
|
||||
// HTTP handler, which supplies the operator attribution the request body never
|
||||
// carries -- and returns the new version.
|
||||
func dashboardReplace(t *testing.T, server *webui.Server, kind string, value []byte, expectedVersion int64) int64 {
|
||||
t.Helper()
|
||||
body, err := json.Marshal(map[string]any{"value": json.RawMessage(value), "expected_version": expectedVersion})
|
||||
if err != nil {
|
||||
t.Fatalf("encode %s request: %v", kind, err)
|
||||
}
|
||||
request := httptest.NewRequestWithContext(t.Context(), http.MethodPost,
|
||||
"/api/control-plane/resources/"+kind+"/commands/replace", bytes.NewReader(body))
|
||||
recorder := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("dashboard replace %s = %d, want 200: %s", kind, recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var response struct {
|
||||
Resource struct {
|
||||
Version int64 `json:"version"`
|
||||
} `json:"resource"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("decode %s replace response: %v (%s)", kind, err, recorder.Body.String())
|
||||
}
|
||||
return response.Resource.Version
|
||||
}
|
||||
|
||||
// dashboardHistory reads a resource's audit trail through the dashboard route,
|
||||
// so the edit observed is the one the UI would show.
|
||||
func dashboardHistory(t *testing.T, server *webui.Server, kind string) []controlPlaneRevision {
|
||||
t.Helper()
|
||||
var view struct {
|
||||
Revisions []controlPlaneRevision `json:"revisions"`
|
||||
}
|
||||
dashboardGET(t, server, "/api/control-plane/resources/"+kind+"/history", &view)
|
||||
return view.Revisions
|
||||
}
|
||||
|
||||
// dashboardApplyStatus reads the apply-status page the dashboard renders.
|
||||
func dashboardApplyStatus(t *testing.T, server *webui.Server) []applyStatusRecord {
|
||||
t.Helper()
|
||||
var view struct {
|
||||
Records []applyStatusRecord `json:"records"`
|
||||
}
|
||||
dashboardGET(t, server, "/api/control-plane/apply-status", &view)
|
||||
return view.Records
|
||||
}
|
||||
|
||||
func dashboardGET(t *testing.T, server *webui.Server, path string, target any) {
|
||||
t.Helper()
|
||||
recorder := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(recorder, httptest.NewRequestWithContext(t.Context(), http.MethodGet, path, nil))
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("GET %s = %d, want 200: %s", path, recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), target); err != nil {
|
||||
t.Fatalf("decode %s: %v (%s)", path, err, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func applyStatusFor(t *testing.T, statuses []storecontract.ApplyStatus, process, kind string) storecontract.ApplyStatus {
|
||||
t.Helper()
|
||||
for _, record := range statuses {
|
||||
if record.Process == process && record.Kind == kind {
|
||||
return record
|
||||
}
|
||||
}
|
||||
t.Fatalf("no apply status for (%s, %s) among %+v", process, kind, statuses)
|
||||
return storecontract.ApplyStatus{}
|
||||
}
|
||||
|
||||
func applyStatusViewFor(t *testing.T, records []applyStatusRecord, process, kind string) applyStatusRecord {
|
||||
t.Helper()
|
||||
for _, record := range records {
|
||||
if record.Process == process && record.Kind == kind {
|
||||
return record
|
||||
}
|
||||
}
|
||||
t.Fatalf("no apply-status row for (%s, %s) among %+v", process, kind, records)
|
||||
return applyStatusRecord{}
|
||||
}
|
||||
|
||||
func countApplyStatusViews(records []applyStatusRecord, process, kind string) int {
|
||||
count := 0
|
||||
for _, record := range records {
|
||||
if record.Process == process && record.Kind == kind {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
@@ -1,259 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
// staleDatabaseOwnedSettings are TOML fragments, each carrying one value a
|
||||
// control-plane resource is seeded from and replaces wholesale. They are the
|
||||
// values docs/prds/runtime-control-plane.md,
|
||||
// "Bootstrap, migration, and recovery" says must not block a process: "after
|
||||
// migration, settings in TOML are ignored and cannot block State Store
|
||||
// startup".
|
||||
var staleDatabaseOwnedSettings = []struct {
|
||||
name string
|
||||
body string
|
||||
}{
|
||||
{
|
||||
name: "provider base_url carrying userinfo",
|
||||
body: "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n",
|
||||
},
|
||||
{
|
||||
name: "negative poll_interval",
|
||||
body: "poll_interval = \"-5s\"\n",
|
||||
},
|
||||
{
|
||||
name: "unrecognised dispatch.trigger",
|
||||
body: "[dispatch]\ntrigger = \"labels\"\n",
|
||||
},
|
||||
{
|
||||
name: "negative containers.volume_ttl",
|
||||
body: "[containers]\nvolume_ttl = \"-1m\"\n",
|
||||
},
|
||||
{
|
||||
name: "malformed repos test_glob",
|
||||
body: "[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n",
|
||||
},
|
||||
}
|
||||
|
||||
// TestLoadConfigAcceptsStaleDatabaseOwnedValues is the first of the two layers
|
||||
// archie-state-store boots through. loadConfig resolves the file and is where
|
||||
// the loader used to reject these values; it must not, because the setting
|
||||
// belongs to the control plane. The second layer is the boot itself, covered by
|
||||
// TestStateStoreBootsWithStaleDatabaseOwnedValues -- passing only this one is
|
||||
// not the item's claim.
|
||||
func TestLoadConfigAcceptsStaleDatabaseOwnedValues(t *testing.T) {
|
||||
for _, tt := range staleDatabaseOwnedSettings {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path := t.TempDir() + "/config.toml"
|
||||
writeConfig(t, path, "bot_user = \"widget\"\n"+tt.body)
|
||||
|
||||
b := &boot{log: slog.New(slog.DiscardHandler)}
|
||||
if err := b.loadConfig(t.Context(), path, ""); err != nil {
|
||||
t.Fatalf("loadConfig: %v (a stale database-owned value must not fail boot)", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// staleBootGrace is how long one boot in
|
||||
// TestStateStoreBootsWithStaleDatabaseOwnedValues may run before the test cancels
|
||||
// it. A refusal arrives as an error before the cancel (first select); a boot
|
||||
// that serves returns only after the cancel (second select). The bound must be
|
||||
// long enough for a full boot -- now Postgres pool + migration, then SQLite and
|
||||
// the control-plane seed -- to finish, because cancelling mid-seed surfaces a
|
||||
// transaction error that reads as a refusal rather than as the interrupt it is.
|
||||
// 2s matches startupGrace, the sibling single-boot bound.
|
||||
//
|
||||
// staleShutdownGrace bounds the shutdown, and is deliberately not the 5s the
|
||||
// single-boot regression test uses: this table runs five boots back to back on a
|
||||
// host that may be busy, and the select returns the moment the process does, so
|
||||
// the bound costs nothing when shutdown is quick and only decides how long a
|
||||
// failure takes to report.
|
||||
const (
|
||||
staleBootGrace = 2 * time.Second
|
||||
staleShutdownGrace = 30 * time.Second
|
||||
)
|
||||
|
||||
// TestStateStoreBootsWithStaleDatabaseOwnedValues is the process-level half of
|
||||
// the item, and the layer that actually decides it: archie-state-store seeds
|
||||
// every control-plane resource from the same file config on a fresh database
|
||||
// (state_store.go, control.ImportConfig), and that seed is what runs the
|
||||
// resource's own validator. A seed that does not validate is skipped and
|
||||
// reported, not fatal -- the kind stays absent, the file's value is the one in
|
||||
// effect, and the operator's fix in config.toml is what clears it.
|
||||
func TestStateStoreBootsWithStaleDatabaseOwnedValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, tt := range staleDatabaseOwnedSettings {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
body := fmt.Sprintf(
|
||||
"bot_user = 'archie'\ndatabase_url = %q\n%s[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n",
|
||||
pgtest.URL(t), tt.body,
|
||||
)
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Loopback, so no token is required. Cancellation is the assertion:
|
||||
// a boot that refuses the config returns before it, and one that
|
||||
// serves returns only when the context ends. Cancel first, then
|
||||
// assert -- racing startup against a wall clock is a coin flip.
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "127.0.0.1:0"})
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
t.Fatalf("RunStateStore returned before cancellation, want it to serve: %v", err)
|
||||
case <-time.After(staleBootGrace):
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("RunStateStore returned %v, want nil or context.Canceled", err)
|
||||
}
|
||||
case <-time.After(staleShutdownGrace):
|
||||
t.Fatal("RunStateStore did not return within the shutdown grace after cancellation")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRuntimeConfigKeepsTheFileValueWhenAKindHasNoStoredResource is the other
|
||||
// end of a seed the control plane refused: the kind stays ABSENT rather than
|
||||
// being stored (ImportConfig skips it), and boot must then fall back to the
|
||||
// file document's value. Treating an absent kind as fatal is how a config the
|
||||
// file layer blesses left the daemon unbootable with a database-named error,
|
||||
// and left editing config.toml unable to fix it.
|
||||
func TestRuntimeConfigKeepsTheFileValueWhenAKindHasNoStoredResource(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
kind string
|
||||
check func(t *testing.T, cfg config.Config)
|
||||
}{
|
||||
{
|
||||
name: "repository policies",
|
||||
kind: controlplane.RepositoryPoliciesKind,
|
||||
check: func(t *testing.T, cfg config.Config) {
|
||||
t.Helper()
|
||||
if len(cfg.Repos) != 1 || cfg.Repos[0].Name != "from-file" {
|
||||
t.Errorf("Repos = %+v, want the file's from-file", cfg.Repos)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "scheduling policy",
|
||||
kind: controlplane.SchedulingPolicyKind,
|
||||
check: func(t *testing.T, cfg config.Config) {
|
||||
t.Helper()
|
||||
if cfg.PollInterval != config.Duration(time.Minute) {
|
||||
t.Errorf("PollInterval = %v, want the file's 1m", cfg.PollInterval)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "container runtime policies",
|
||||
kind: controlplane.ContainerRuntimePoliciesKind,
|
||||
check: func(t *testing.T, cfg config.Config) {
|
||||
t.Helper()
|
||||
if cfg.Containers.Image != "archie:from-file" {
|
||||
t.Errorf("Containers.Image = %q, want the file's archie:from-file", cfg.Containers.Image)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "providers",
|
||||
kind: controlplane.ProviderSettingsKind,
|
||||
check: func(t *testing.T, cfg config.Config) {
|
||||
t.Helper()
|
||||
if got := cfg.Providers["file"].Class; got != "openai" {
|
||||
t.Errorf("Providers[file].Class = %q, want the file's openai", got)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
resources := databaseOwnedResources()
|
||||
delete(resources, tt.kind)
|
||||
b := newReloadBoot(t, &controlPlaneStub{values: resources})
|
||||
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v (a kind with no stored value must leave the file's value in effect)", err)
|
||||
}
|
||||
tt.check(t, b.cfgHolder.Get())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDuplicateRepositoriesFailOnTheFilesOwnMessage is the end of the path the
|
||||
// fix-closure review found. A duplicate [[repos]] entry is refused by the
|
||||
// repository-policies validator, so the kind is never seeded and stays absent --
|
||||
// and boot must then fail on the FILE's copy of the value, naming the duplicate,
|
||||
// with the file as the fix. It used to fail on "control-plane resource not
|
||||
// found": a database-named error that editing config.toml could not clear.
|
||||
func TestDuplicateRepositoriesFailOnTheFilesOwnMessage(t *testing.T) {
|
||||
resources := databaseOwnedResources()
|
||||
delete(resources, controlplane.RepositoryPoliciesKind)
|
||||
b := newReloadBoot(t, &controlPlaneStub{values: resources})
|
||||
b.cfg.Repos = []config.Repo{{Owner: "acme", Name: "app"}, {Owner: "acme", Name: "app"}}
|
||||
|
||||
err := b.loadRuntimeConfig(t.Context())
|
||||
if err == nil {
|
||||
t.Fatal("loadRuntimeConfig accepted a duplicate repository list")
|
||||
}
|
||||
if !errors.Is(err, configuration.ErrInvalidInput) {
|
||||
t.Errorf("loadRuntimeConfig = %v, want it to wrap configuration.ErrInvalidInput", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "duplicates") {
|
||||
t.Errorf("loadRuntimeConfig = %v, want the duplicate named the way configuration.Validate names it", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "not found") {
|
||||
t.Errorf("loadRuntimeConfig = %v, want no absent-resource error: the file's value is the one in effect", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRuntimeConfigFailsClosedOnAnInvalidDatabaseValue is the other half of the
|
||||
// same change: the checks that no longer run at load run on the effective
|
||||
// document, at the one place database resources are layered in. A stored value
|
||||
// that will not validate must stop archied starting rather than be published as
|
||||
// the running configuration -- a state nothing can recover from in band.
|
||||
func TestRuntimeConfigFailsClosedOnAnInvalidDatabaseValue(t *testing.T) {
|
||||
resources := databaseOwnedResources()
|
||||
resources[controlplane.ProviderSettingsKind] = map[string]any{
|
||||
"main": map[string]any{"class": "openai", "base_url": "https://token@example.com/v1"},
|
||||
}
|
||||
b := newReloadBoot(t, &controlPlaneStub{values: resources})
|
||||
|
||||
err := b.loadRuntimeConfig(t.Context())
|
||||
if err == nil {
|
||||
t.Fatal("loadRuntimeConfig accepted a database value that does not validate")
|
||||
}
|
||||
if !errors.Is(err, configuration.ErrInvalidInput) {
|
||||
t.Errorf("loadRuntimeConfig = %v, want it to wrap configuration.ErrInvalidInput", err)
|
||||
}
|
||||
if got := b.cfgHolder.Get().Providers["main"].BaseURL; got == "https://token@example.com/v1" {
|
||||
t.Error("the invalid database value was published as the running configuration")
|
||||
}
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"slices"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/test/bufconn"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
||||
)
|
||||
|
||||
// outageStore is the State Store's resource half with a switch that makes every
|
||||
// read fail after taking stall to do it. It stands in for the outage the
|
||||
// reconnect exists for: the control plane takes the stream and then answers its
|
||||
// first read with mapError, so the client is handed a stream that ends carrying
|
||||
// no document -- while every reopen is accepted, because gRPC accepts before
|
||||
// the handler runs.
|
||||
//
|
||||
// The stall is the other shape that outage takes, and the shape a store that has
|
||||
// stopped answering actually has: it does not error at once, it stops reading
|
||||
// and the call times out. An attempt that ends that way delivered nothing and
|
||||
// lasted longer than the minimum retry interval, which is a failed attempt
|
||||
// whichever way the store failed it.
|
||||
type outageStore struct {
|
||||
*pgstore.TaskDB
|
||||
|
||||
mu sync.Mutex
|
||||
failing bool
|
||||
stall time.Duration
|
||||
}
|
||||
|
||||
// down starts the outage, which takes stall to surface.
|
||||
func (s *outageStore) down(stall time.Duration) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.failing, s.stall = true, stall
|
||||
}
|
||||
|
||||
func (s *outageStore) Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) {
|
||||
s.mu.Lock()
|
||||
failing, stall := s.failing, s.stall
|
||||
s.mu.Unlock()
|
||||
if failing {
|
||||
time.Sleep(stall)
|
||||
return storecontract.Resource{}, errors.New("state store unreachable")
|
||||
}
|
||||
return s.TaskDB.Resource(ctx, orgID, kind)
|
||||
}
|
||||
|
||||
// TestWatchRetryGrowsWhileTheStateStoreIsDown is the outage this reconnect
|
||||
// exists for, end to end: a real controlplane.Server over bufconn, a real
|
||||
// controlplane.Client, a real store, and the store then stops answering -- in
|
||||
// each of the shapes it stops answering in, from the store that errors at once
|
||||
// to the one that takes as long to fail as the delay it would be charged.
|
||||
//
|
||||
// The server accepts every reopened stream and fails its first read, so the
|
||||
// client is handed an update for that failure that carries no version at all:
|
||||
// an attempt that ends this way delivered nothing, and the reconnect behind it
|
||||
// doubles the delay. That charge has to reach a store that fails slowly too.
|
||||
// The delay a slow failure is charged rests on the healthy window being derived
|
||||
// from the delay the attempt would otherwise be charged, not on the minimum: a
|
||||
// store that takes 300ms to time out outlived a window pinned at the 250ms
|
||||
// minimum on every attempt, so the delay never grew -- measured across 3s of
|
||||
// that shape, 1.7 watch RPCs a second with retry_in flat at 250ms, which is the
|
||||
// request loop this reconnect exists to avoid, reached by the likelier shape.
|
||||
//
|
||||
// The first retry is the minimum in every shape: the boot stream is a healthy
|
||||
// attempt, because the store answered it for longer than the window that
|
||||
// attempt would have been charged with before the outage began. The ladder's
|
||||
// top is reached by the delay alone and is pinned by
|
||||
// TestReconnectDelayFollowsAttemptHealth, which does not have to wait 30s for it.
|
||||
func TestWatchRetryGrowsWhileTheStateStoreIsDown(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
stall time.Duration
|
||||
want []time.Duration
|
||||
}{
|
||||
{
|
||||
name: "the store errors at once",
|
||||
want: []time.Duration{controlPlaneWatchRetryMin, 2 * controlPlaneWatchRetryMin, 4 * controlPlaneWatchRetryMin},
|
||||
},
|
||||
{
|
||||
name: "the store stalls before it times out",
|
||||
stall: 300 * time.Millisecond,
|
||||
want: []time.Duration{
|
||||
controlPlaneWatchRetryMin, 2 * controlPlaneWatchRetryMin,
|
||||
4 * controlPlaneWatchRetryMin, 8 * controlPlaneWatchRetryMin,
|
||||
},
|
||||
},
|
||||
{
|
||||
// The window an attempt is judged against is strictly wider than the
|
||||
// delay it would be charged with, and this is the shape that says so:
|
||||
// a failure that takes exactly that delay to surface is still a store
|
||||
// that did not answer the read.
|
||||
name: "the store fails as slowly as the delay it would be charged",
|
||||
stall: 2 * controlPlaneWatchRetryMin,
|
||||
want: []time.Duration{
|
||||
controlPlaneWatchRetryMin, 2 * controlPlaneWatchRetryMin,
|
||||
4 * controlPlaneWatchRetryMin, 8 * controlPlaneWatchRetryMin,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
database := &outageStore{TaskDB: pgstore.Open(t)}
|
||||
defer database.Close()
|
||||
steps, err := workflowsteps.NewManager()
|
||||
if err != nil {
|
||||
t.Fatalf("build the workflow step vocabulary: %v", err)
|
||||
}
|
||||
server, err := controlplane.NewServer(database, steps)
|
||||
if err != nil {
|
||||
t.Fatalf("build the control plane: %v", err)
|
||||
}
|
||||
if _, err := server.ImportWorkflowExecutionSettings(t.Context(), bootedSettings); err != nil {
|
||||
t.Fatalf("seed the stored settings: %v", err)
|
||||
}
|
||||
|
||||
log, retries := newRetryLog()
|
||||
b, _ := newLiveApplyBoot(t, fileConfig())
|
||||
b.log = log
|
||||
b.controlPlane = controlplane.NewClient(dialControlPlane(t, server))
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
if err := b.startWorkflowExecutionSettings(ctx); err != nil {
|
||||
t.Fatalf("startWorkflowExecutionSettings: %v", err)
|
||||
}
|
||||
|
||||
// The store answers the boot stream for longer than the window that
|
||||
// attempt would be charged with, which is what makes it the healthy
|
||||
// attempt the first retry is not charged for.
|
||||
time.Sleep(controlPlaneWatchHealthyWindow(controlPlaneWatchRetryMin))
|
||||
database.down(tt.stall)
|
||||
|
||||
delays := awaitRetries(t, retries, len(tt.want))
|
||||
if got := delays[:len(tt.want)]; !slices.Equal(got, tt.want) {
|
||||
t.Errorf("reconnects waited %v, want %v: the delay is decided by the attempt that just ended, so the healthy boot attempt leaves the %v minimum behind and every attempt of the outage doubles the delay before it. A delay that never grows is this watch back at a request loop: pinned at %v by a healthy window fixed at the minimum, which a store that fails as slowly as this one outlives on every attempt, and pinned at %v by a reset on every reopen the store accepted, which charges each attempt of the outage from the minimum again",
|
||||
got, tt.want, controlPlaneWatchRetryMin, controlPlaneWatchRetryMin, 2*controlPlaneWatchRetryMin)
|
||||
}
|
||||
// Each logged retry is the wait the loop then took. A loop that logged
|
||||
// one delay and waited another would reopen sooner than it said it
|
||||
// would, and the attribute alone cannot show that.
|
||||
for i := 0; i < len(tt.want)-1; i++ {
|
||||
if gap := retries.gap(i); gap < tt.want[i] {
|
||||
t.Errorf("the retry logged as %v was followed by the next stream ending %v later, want at least that delay: the loop waits the delay it charged", tt.want[i], gap)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// dialControlPlane serves server over bufconn and returns the client
|
||||
// connection a watch dials it through. The transport is real: the stream's
|
||||
// context is the RPC's, and an unaccepted stream is not something a stub can
|
||||
// stand in for.
|
||||
func dialControlPlane(t *testing.T, server pb.ControlPlaneServiceServer) *grpc.ClientConn {
|
||||
t.Helper()
|
||||
listener := bufconn.Listen(1 << 20)
|
||||
grpcServer := grpc.NewServer()
|
||||
pb.RegisterControlPlaneServiceServer(grpcServer, server)
|
||||
go func() { _ = grpcServer.Serve(listener) }()
|
||||
t.Cleanup(func() {
|
||||
grpcServer.Stop()
|
||||
_ = listener.Close()
|
||||
})
|
||||
conn, err := grpc.NewClient("passthrough:///control-plane",
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
|
||||
return listener.DialContext(ctx)
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("dial the control plane: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = conn.Close() })
|
||||
return conn
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,153 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/drain"
|
||||
)
|
||||
|
||||
const testInterval = 5 * time.Millisecond
|
||||
|
||||
// countShutdown returns a shutdown func plus a channel signalled on each call.
|
||||
func countShutdown(t *testing.T) (func(), *chan struct{}) {
|
||||
t.Helper()
|
||||
calls := make(chan struct{}, 1)
|
||||
return func() {
|
||||
select {
|
||||
case calls <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}, &calls
|
||||
}
|
||||
|
||||
func TestMonitorTriggersShutdownOnValidDecision(t *testing.T) {
|
||||
shutdown, calls := countShutdown(t)
|
||||
err := monitorDrainRequests(
|
||||
context.Background(),
|
||||
func() (drain.Decision, error) { return drain.DecisionValid, nil },
|
||||
shutdown,
|
||||
slog.New(slog.DiscardHandler),
|
||||
testInterval,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("monitorDrainRequests() error = %v, want nil", err)
|
||||
}
|
||||
select {
|
||||
case <-*calls:
|
||||
default:
|
||||
t.Fatal("shutdown was not invoked on a valid drain decision")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonitorDoesNotShutdownOnStaleDecision(t *testing.T) {
|
||||
shutdown, calls := countShutdown(t)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// First poll returns stale; the monitor waits one interval. Cancel the
|
||||
// context before the next poll, then assert shutdown was never invoked and
|
||||
// the monitor returned the cancellation.
|
||||
go func() {
|
||||
time.Sleep(testInterval * 3)
|
||||
cancel()
|
||||
}()
|
||||
|
||||
err := monitorDrainRequests(
|
||||
ctx,
|
||||
func() (drain.Decision, error) { return drain.DecisionStale, nil },
|
||||
shutdown,
|
||||
slog.New(slog.DiscardHandler),
|
||||
testInterval,
|
||||
)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("monitorDrainRequests() error = %v, want context.Canceled", err)
|
||||
}
|
||||
select {
|
||||
case <-*calls:
|
||||
t.Fatal("shutdown was invoked on a stale drain decision")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonitorDoesNotShutdownOnNoMarker(t *testing.T) {
|
||||
shutdown, calls := countShutdown(t)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
time.Sleep(testInterval * 3)
|
||||
cancel()
|
||||
}()
|
||||
|
||||
err := monitorDrainRequests(
|
||||
ctx,
|
||||
func() (drain.Decision, error) { return drain.DecisionNone, nil },
|
||||
shutdown,
|
||||
slog.New(slog.DiscardHandler),
|
||||
testInterval,
|
||||
)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("monitorDrainRequests() error = %v, want context.Canceled", err)
|
||||
}
|
||||
select {
|
||||
case <-*calls:
|
||||
t.Fatal("shutdown was invoked with no drain marker")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonitorDoesNotShutdownOnCheckError(t *testing.T) {
|
||||
shutdown, calls := countShutdown(t)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
time.Sleep(testInterval * 3)
|
||||
cancel()
|
||||
}()
|
||||
|
||||
boom := errors.New("marker unreadable")
|
||||
err := monitorDrainRequests(
|
||||
ctx,
|
||||
func() (drain.Decision, error) { return drain.DecisionStale, boom },
|
||||
shutdown,
|
||||
slog.New(slog.DiscardHandler),
|
||||
testInterval,
|
||||
)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("monitorDrainRequests() error = %v, want context.Canceled", err)
|
||||
}
|
||||
select {
|
||||
case <-*calls:
|
||||
t.Fatal("shutdown was invoked despite a check error")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonitorShutdownAfterStaleThenValid(t *testing.T) {
|
||||
shutdown, calls := countShutdown(t)
|
||||
callsMade := 0
|
||||
err := monitorDrainRequests(
|
||||
context.Background(),
|
||||
func() (drain.Decision, error) {
|
||||
callsMade++
|
||||
if callsMade == 1 {
|
||||
return drain.DecisionStale, nil
|
||||
}
|
||||
return drain.DecisionValid, nil
|
||||
},
|
||||
shutdown,
|
||||
slog.New(slog.DiscardHandler),
|
||||
testInterval,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("monitorDrainRequests() error = %v, want nil", err)
|
||||
}
|
||||
select {
|
||||
case <-*calls:
|
||||
default:
|
||||
t.Fatal("shutdown was not invoked after a marker became valid")
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type stubPlaybookLedger struct{}
|
||||
|
||||
func (stubPlaybookLedger) RecordPlaybookDispatch(context.Context, string, string, string, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (stubPlaybookLedger) DeletePlaybookDispatches(context.Context, string) error { return nil }
|
||||
|
||||
func loadBootPlaybooks(t *testing.T, document string) *playbook.Store {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "pb.yaml"), []byte(document), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := &boot{cfg: config.Config{EDAPlaybookDir: dir}, modules: module.New()}
|
||||
if err := b.loadEDAPlaybooks(b.cfg, slog.New(slog.DiscardHandler)); err != nil {
|
||||
t.Fatalf("loadEDAPlaybooks: %v", err)
|
||||
}
|
||||
return b.playbooks
|
||||
}
|
||||
|
||||
const actionPlaybookDoc = `trigger:
|
||||
kind: bug
|
||||
actions:
|
||||
- position: module
|
||||
kind: log
|
||||
args:
|
||||
message: '"build started"'
|
||||
`
|
||||
|
||||
// An action playbook never runs without the dispatch ledger, and the
|
||||
// operator is told which ones are inert rather than finding out by silence.
|
||||
func TestPlaybookLedger(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
source any
|
||||
document string
|
||||
wantLedger bool
|
||||
wantWarn bool
|
||||
}{
|
||||
{name: "ledger wired", source: stubPlaybookLedger{}, document: actionPlaybookDoc, wantLedger: true},
|
||||
{name: "no ledger warns per action playbook", source: struct{}{}, document: actionPlaybookDoc, wantWarn: true},
|
||||
{name: "no ledger, workflow playbook only", source: struct{}{}, document: "trigger:\n kind: bug\nactions:\n - position: workflow\n workflow: tdd\n"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
got := playbookLedger(tt.source, loadBootPlaybooks(t, tt.document), slog.New(slog.NewTextHandler(&buf, nil)))
|
||||
if (got != nil) != tt.wantLedger {
|
||||
t.Fatalf("ledger = %v, want wired=%v", got, tt.wantLedger)
|
||||
}
|
||||
out := buf.String()
|
||||
warned := strings.Contains(out, "level=WARN") && strings.Contains(out, "pb.yaml") && strings.Contains(out, "will not run")
|
||||
if warned != tt.wantWarn {
|
||||
t.Fatalf("warning output = %q, want warned=%v", out, tt.wantWarn)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Autonomous execution has one production composition: managed task
|
||||
// containers over embedded or external NATS. These old switches must not
|
||||
// quietly restore a host or no-broker path in the composition root.
|
||||
func TestCompositionContainsNoAutonomousExecutionOptOut(t *testing.T) {
|
||||
for _, file := range []string{"bootstrap.go", "main.go"} {
|
||||
source, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, forbidden := range []string{"NATSModeOff", "Containers.Enabled"} {
|
||||
if strings.Contains(string(source), forbidden) {
|
||||
t.Errorf("%s contains removed autonomous execution switch %q", file, forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,212 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/sdnotify"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
func TestGatewayServerOptsLoopbackIsInsecure(t *testing.T) {
|
||||
opts, loopback, err := gatewayServerOpts("127.0.0.1:8585", "")
|
||||
if err != nil {
|
||||
t.Fatalf("gatewayServerOpts(loopback, no token) error: %v", err)
|
||||
}
|
||||
if !loopback {
|
||||
t.Fatal("loopback listener should report loopback")
|
||||
}
|
||||
// Only the inbound size limit; a loopback listener installs no auth
|
||||
// interceptors (the count is the only handle on opaque ServerOptions).
|
||||
if len(opts) != 1 {
|
||||
t.Fatalf("loopback listener should install only the inbound size limit, got %d options", len(opts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayServerOptsNonLoopbackRequiresToken(t *testing.T) {
|
||||
if _, _, err := gatewayServerOpts("0.0.0.0:8585", ""); err == nil {
|
||||
t.Fatal("non-loopback listener without a token should fail closed")
|
||||
}
|
||||
opts, loopback, err := gatewayServerOpts("0.0.0.0:8585", "secret")
|
||||
if err != nil {
|
||||
t.Fatalf("non-loopback listener with a token error: %v", err)
|
||||
}
|
||||
if loopback {
|
||||
t.Fatal("non-loopback listener should not report loopback")
|
||||
}
|
||||
if len(opts) != 3 {
|
||||
t.Fatalf("non-loopback listener should install three server options (inbound size limit plus unary and stream auth interceptors), got %d", len(opts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayServerOptsMalformedListen(t *testing.T) {
|
||||
if _, _, err := gatewayServerOpts("not-an-address", ""); err == nil {
|
||||
t.Fatal("malformed listen address should error")
|
||||
}
|
||||
}
|
||||
|
||||
// stubGatewayContract satisfies messaging.ChatContract with zero values: the
|
||||
// serve test exercises the listener, the READY announcement and one read RPC,
|
||||
// not the chat runtime.
|
||||
type stubGatewayContract struct{}
|
||||
|
||||
func (stubGatewayContract) Snapshot(context.Context) (messaging.ChatSnapshot, error) {
|
||||
return messaging.ChatSnapshot{}, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) GetSession(context.Context, string) (messaging.SessionContext, bool, error) {
|
||||
return messaging.SessionContext{}, false, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) RecentMessages(context.Context, string, int) ([]messaging.Message, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) RecentTurns(context.Context, string, int) ([]messaging.TurnRecord, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) Route(context.Context, messaging.Inbound) (messaging.ChatReply, error) {
|
||||
return messaging.ChatReply{}, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) Stream(context.Context, messaging.Inbound) (<-chan messaging.ChatEvent, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) Cancel(context.Context, string) (messaging.ChatCancellation, error) {
|
||||
return messaging.ChatCancellation{}, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) SetPersona(context.Context, string, string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) ApplyTaskAction(context.Context, string, int64, taskstate.Action, taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
return messaging.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) ApplyOperatorTaskAction(context.Context, taskactions.Actor, int64, taskstate.Action, taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
return messaging.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
// TestGatewayServingAnnouncesReady: archie-gateway run as a Type=notify unit
|
||||
// must send READY=1 at the moment it declares itself serving, or systemd kills
|
||||
// a healthy process once TimeoutStartSec expires (archie-core-1174). The
|
||||
// announcement is judged against the same evidence archied's run loop uses:
|
||||
// the contract answers over the bound listener, so READY asserts a serving
|
||||
// process rather than a bound socket.
|
||||
func TestGatewayServingAnnouncesReady(t *testing.T) {
|
||||
listener := newNotifyListener(t)
|
||||
b := &boot{log: slog.New(slog.DiscardHandler)}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
|
||||
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
|
||||
serveCh := make(chan error, 1)
|
||||
go func() { serveCh <- b.serveGatewayListener(ctx, ln, true, stubGatewayContract{}, nil) }()
|
||||
|
||||
if !listener.waitForState(t, 5*time.Second, sdnotify.ReadyState) {
|
||||
t.Fatal("no READY=1 datagram within 5s of the Gateway serving")
|
||||
}
|
||||
|
||||
conn, err := grpc.NewClient("passthrough:///"+ln.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
if _, err := pb.NewChatServiceClient(conn).Snapshot(t.Context(), &pb.SnapshotRequest{}); err != nil {
|
||||
t.Fatalf("Snapshot over gRPC: %v", err)
|
||||
}
|
||||
|
||||
cancel()
|
||||
if err := <-serveCh; err != nil {
|
||||
t.Fatalf("serveGatewayListener returned: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// recordingGatewayContract captures the inbound a Stream call reached the
|
||||
// Gateway with, so a test can prove the request's payload survived the wire.
|
||||
type recordingGatewayContract struct {
|
||||
stubGatewayContract
|
||||
mu sync.Mutex
|
||||
last messaging.Inbound
|
||||
}
|
||||
|
||||
func (c *recordingGatewayContract) Stream(_ context.Context, in messaging.Inbound) (<-chan messaging.ChatEvent, error) {
|
||||
c.mu.Lock()
|
||||
c.last = in
|
||||
c.mu.Unlock()
|
||||
events := make(chan messaging.ChatEvent)
|
||||
close(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
// TestGatewayAcceptsAnInboundAttachmentOverTheDefaultGRPCLimit: the inbound
|
||||
// request now carries a channel attachment's bytes, and gRPC's default receive
|
||||
// limit is 4 MB -- well under the messaging contract's ceiling. A maximal
|
||||
// attachment must reach the turn, not fail the RPC with ResourceExhausted.
|
||||
func TestGatewayAcceptsAnInboundAttachmentOverTheDefaultGRPCLimit(t *testing.T) {
|
||||
rec := &recordingGatewayContract{}
|
||||
ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
opts, _, err := gatewayServerOpts(ln.Addr().String(), "")
|
||||
if err != nil {
|
||||
t.Fatalf("gatewayServerOpts: %v", err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
serveCh := make(chan error, 1)
|
||||
go func() { serveCh <- serveGateway(ctx, ln, rec, nil, opts) }()
|
||||
defer func() {
|
||||
cancel()
|
||||
if err := <-serveCh; err != nil {
|
||||
t.Errorf("serveGateway: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
conn, err := grpc.NewClient("passthrough:///"+ln.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
raw := make([]byte, 5<<20) // larger than gRPC's 4 MB default receive limit
|
||||
stream, err := pb.NewChatServiceClient(conn).Stream(t.Context(), &pb.StreamRequest{
|
||||
Message: &pb.Message{Media: []*pb.Media{{Type: "image", MimeType: "image/jpeg", Data: raw}}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Stream with a %d-byte attachment: %v", len(raw), err)
|
||||
}
|
||||
if _, err := stream.Recv(); !errors.Is(err, io.EOF) {
|
||||
t.Fatalf("Recv after an empty stream: %v, want EOF", err)
|
||||
}
|
||||
|
||||
rec.mu.Lock()
|
||||
got := rec.last
|
||||
rec.mu.Unlock()
|
||||
if len(got.Media) != 1 {
|
||||
t.Fatalf("turn received %d attachments, want 1", len(got.Media))
|
||||
}
|
||||
if len(got.Media[0].Data) != len(raw) {
|
||||
t.Fatalf("turn received %d attachment bytes, want %d", len(got.Media[0].Data), len(raw))
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
)
|
||||
|
||||
// TestHealthSurfaceLivenessWaitsForBoot: the update watchdog restarts archied
|
||||
// and takes the first 200 as proof the new release came up. The listener
|
||||
// binds early so the port is not simply refused during boot, which means an
|
||||
// unconditional 200 would confirm a release that had not started yet.
|
||||
func TestHealthSurfaceLivenessWaitsForBoot(t *testing.T) {
|
||||
surface := &healthSurface{}
|
||||
handler := surface.handler()
|
||||
|
||||
for _, path := range []string{"/healthz", "/health"} {
|
||||
res := httptest.NewRecorder()
|
||||
handler.ServeHTTP(res, httptest.NewRequestWithContext(t.Context(), http.MethodGet, path, nil))
|
||||
if res.Code != http.StatusServiceUnavailable {
|
||||
t.Errorf("GET %s while booting = %d, want 503", path, res.Code)
|
||||
}
|
||||
}
|
||||
|
||||
surface.markServing()
|
||||
|
||||
for _, path := range []string{"/healthz", "/health"} {
|
||||
res := httptest.NewRecorder()
|
||||
handler.ServeHTTP(res, httptest.NewRequestWithContext(t.Context(), http.MethodGet, path, nil))
|
||||
if res.Code != http.StatusOK {
|
||||
t.Errorf("GET %s once serving = %d, want 200", path, res.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestHealthSurfaceDetailedReportsProbes: /health/detailed is the readiness
|
||||
// answer, and an unwired or degraded registry must never read as ready.
|
||||
func TestHealthSurfaceDetailedReportsProbes(t *testing.T) {
|
||||
var registry *health.Registry
|
||||
surface := &healthSurface{registry: func() *health.Registry { return registry }}
|
||||
surface.markServing()
|
||||
handler := surface.handler()
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
handler.ServeHTTP(res, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/health/detailed", nil))
|
||||
if res.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("detailed with no probes = %d, want 503", res.Code)
|
||||
}
|
||||
|
||||
registry = health.NewRegistry(failingProbe{})
|
||||
res = httptest.NewRecorder()
|
||||
handler.ServeHTTP(res, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/health/detailed", nil))
|
||||
if res.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("detailed with a failing probe = %d, want 503", res.Code)
|
||||
}
|
||||
|
||||
registry = health.NewRegistry(passingProbe{})
|
||||
res = httptest.NewRecorder()
|
||||
handler.ServeHTTP(res, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/health/detailed", nil))
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("detailed with a passing probe = %d, want 200", res.Code)
|
||||
}
|
||||
}
|
||||
|
||||
type failingProbe struct{}
|
||||
|
||||
func (failingProbe) Name() string { return "test" }
|
||||
|
||||
func (failingProbe) Check(context.Context) health.Result {
|
||||
return health.Result{Status: health.StatusDegraded, Detail: "down"}
|
||||
}
|
||||
|
||||
type passingProbe struct{}
|
||||
|
||||
func (passingProbe) Name() string { return "test" }
|
||||
|
||||
func (passingProbe) Check(context.Context) health.Result {
|
||||
return health.Result{Status: health.StatusOK}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package archied
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestResolveServiceListen pins the precedence: an explicit -listen wins, else
|
||||
// the configured address, and an address is never invented.
|
||||
//
|
||||
// The error cases are the ones that matter. net.Listen reads "" as "any free
|
||||
// port", so defaulting here would bind the service somewhere its clients do not
|
||||
// look and report success while doing it -- the exact failure this helper exists
|
||||
// to make impossible.
|
||||
func TestResolveServiceListen(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, flagValue, configured, want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "the flag wins over the configured address",
|
||||
flagValue: "127.0.0.1:9191", configured: "127.0.0.1:9090",
|
||||
want: "127.0.0.1:9191",
|
||||
},
|
||||
{
|
||||
name: "the configured address is used when no flag was passed",
|
||||
configured: "127.0.0.1:9191",
|
||||
want: "127.0.0.1:9191",
|
||||
},
|
||||
{
|
||||
name: "a whitespace flag is not an address",
|
||||
flagValue: " ", configured: "127.0.0.1:9191",
|
||||
want: "127.0.0.1:9191",
|
||||
},
|
||||
{
|
||||
name: "both empty is an error, never a silent ephemeral port",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "whitespace on both sides is an error too",
|
||||
flagValue: " ", configured: "\t",
|
||||
wantErr: true,
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := resolveServiceListen("state", tt.flagValue, tt.configured)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Fatalf("err = nil, want an error; got %q", got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Fatalf("listen = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,98 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
func newSamplingTestRuntime(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"gpt-5.6",`+
|
||||
`"choices":[{"index":0,"message":{"role":"assistant","content":"four"},"finish_reason":"stop"}],`+
|
||||
`"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
return api
|
||||
}
|
||||
|
||||
// TestMCPSamplingHandlerAnswersFromConfiguredModel proves a server-initiated
|
||||
// sampling request is answered by the daemon's own chat model and recorded on
|
||||
// /status like every other model call this process makes.
|
||||
func TestMCPSamplingHandlerAnswersFromConfiguredModel(t *testing.T) {
|
||||
api := newSamplingTestRuntime(t)
|
||||
|
||||
b := newBootstrap()
|
||||
b.setLLM(agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
}))
|
||||
b.chatModels = newChatModelManager(map[string]string{"chat": "openai/gpt-5.6"})
|
||||
b.providerOutcomes = newProviderOutcomeRecorder()
|
||||
|
||||
result, err := b.mcpSamplingHandler()(t.Context(), mcp.SamplingRequest{
|
||||
Messages: []mcp.SamplingMessage{{
|
||||
Role: "user",
|
||||
Content: mcp.SamplingContent{Type: "text", Text: "2+2?"},
|
||||
}},
|
||||
MaxTokens: 16,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("sampling handler = %v, want a completion", err)
|
||||
}
|
||||
if result.Model != "openai/gpt-5.6" {
|
||||
t.Errorf("result.Model = %q, want the configured chat model", result.Model)
|
||||
}
|
||||
if result.Role != "assistant" || result.Content.Type != "text" || result.Content.Text != "four" {
|
||||
t.Errorf("result = %+v, want an assistant text completion", result)
|
||||
}
|
||||
outcome, attempted := b.providerOutcomes.LastChatModelOutcome()
|
||||
if !attempted || outcome.Model != "openai/gpt-5.6" || outcome.Err != "" {
|
||||
t.Errorf("recorded outcome = %+v (attempted %v), want the sampling call recorded as successful", outcome, attempted)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMCPSamplingHandlerRefusesCleanlyWithNoModel proves a daemon with no
|
||||
// usable model answers the request with an error instead of panicking or
|
||||
// dropping it: the server sees a failed request, not a dead session.
|
||||
func TestMCPSamplingHandlerRefusesCleanlyWithNoModel(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
wired bool
|
||||
want string
|
||||
}{
|
||||
{name: "no model runtime", wired: false, want: "no model runtime"},
|
||||
{name: "no chat model configured", wired: true, want: "no chat model"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
if tc.wired {
|
||||
b.setLLM(agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai"},
|
||||
}))
|
||||
}
|
||||
b.chatModels = newChatModelManager(nil)
|
||||
|
||||
_, err := b.mcpSamplingHandler()(t.Context(), mcp.SamplingRequest{
|
||||
Messages: []mcp.SamplingMessage{{
|
||||
Role: "user",
|
||||
Content: mcp.SamplingContent{Type: "text", Text: "hi"},
|
||||
}},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("sampling handler returned no error with no usable model")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tc.want) {
|
||||
t.Errorf("error = %q, want it to mention %q", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,250 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// catalogServer answers the model catalog read with a models.dev-shaped
|
||||
// document. It is the outside dependency the refresh loop actually meets:
|
||||
// modelcatalog.Load fetches this and caches what it read.
|
||||
func catalogServer(t *testing.T, document func() map[string]any) *httptest.Server {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(document())
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
// catalogProvider is one usable provider entry: an environment variable that
|
||||
// resolves (so modelcatalog keeps it) and the tool-callable models it offers.
|
||||
func catalogProvider(id, env string, models ...string) map[string]any {
|
||||
entries := make(map[string]any, len(models))
|
||||
for _, model := range models {
|
||||
entries[model] = map[string]any{
|
||||
"id": model, "name": strings.ToUpper(model), "tool_call": true,
|
||||
"limit": map[string]any{"context": 128000, "output": 16000},
|
||||
}
|
||||
}
|
||||
return map[string]any{
|
||||
"id": id, "name": strings.ToUpper(id), "npm": "@ai-sdk/openai-compatible",
|
||||
"api": "https://" + id + ".test/v1", "env": []string{env}, "models": entries,
|
||||
}
|
||||
}
|
||||
|
||||
// newCatalogBoot builds the smallest boot the refresh path runs against: the
|
||||
// control plane reads behind runtimeConfig, the secrets registry the catalog
|
||||
// resolves keys through, the chat runtime a refresh must reach, and a catalog
|
||||
// endpoint a test controls.
|
||||
func newCatalogBoot(t *testing.T, url string) *boot {
|
||||
t.Helper()
|
||||
b := newReloadBoot(t, &controlPlaneStub{values: databaseOwnedResources()})
|
||||
b.secrets = secret.NewRegistry()
|
||||
b.chatModels = newChatModelManager(fileConfig().Models)
|
||||
b.setLLM(agentexec.NewRuntime(executionProviders(fileConfig())))
|
||||
b.catalogURL = url
|
||||
b.catalogCachePath = filepath.Join(t.TempDir(), "models.json")
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func awaitCatalog(t *testing.T, b *boot, ref string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for !slices.Contains(b.chatModels.Models(), ref) {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("chat models = %v, want %s to appear within the refresh interval", b.chatModels.Models(), ref)
|
||||
}
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRefreshModelCatalogRepublishesEveryProjectionSite: a catalog read after
|
||||
// boot must reach everything derived from it -- the running config a workflow
|
||||
// stage resolves model limits from, the dashboard's published catalog view,
|
||||
// and the chat runtime's model list and details. A value that reaches the
|
||||
// boot struct alone parses and does nothing.
|
||||
func TestRefreshModelCatalogRepublishesEveryProjectionSite(t *testing.T) {
|
||||
srv := catalogServer(t, func() map[string]any {
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-tool")}
|
||||
})
|
||||
t.Setenv("ACME_API_KEY", "resolved-by-the-secret-registry")
|
||||
b := newCatalogBoot(t, srv.URL)
|
||||
|
||||
if snapshot, _ := b.catalogState(); len(snapshot.Providers) != 0 {
|
||||
t.Fatalf("boot catalog = %+v, want the empty snapshot this test starts from", snapshot.Providers)
|
||||
}
|
||||
|
||||
if err := b.refreshModelCatalog(t.Context()); err != nil {
|
||||
t.Fatalf("refreshModelCatalog: %v", err)
|
||||
}
|
||||
|
||||
if got := b.cfgHolder.Get().ModelLimits["acme/acme-tool"]; got.ContextWindow != 128000 || got.MaxOutputTokens != 16000 {
|
||||
t.Errorf("running model limits = %+v, want the refreshed catalog's capacity", got)
|
||||
}
|
||||
if _, models := b.catalogState(); !slices.Contains(models, "acme/acme-tool") {
|
||||
t.Errorf("catalog models = %v, want the refreshed reference", models)
|
||||
}
|
||||
|
||||
view := webui.BuildConfigView(b.configViewInput(t.Context()))
|
||||
if len(view.Catalog) != 1 || view.Catalog[0].ID != "acme" {
|
||||
t.Fatalf("published catalog = %+v, want the refreshed provider", view.Catalog)
|
||||
}
|
||||
if !slices.Contains(view.Catalog[0].Models, "acme-tool") {
|
||||
t.Errorf("published catalog models = %v, want the refreshed model", view.Catalog[0].Models)
|
||||
}
|
||||
|
||||
if !slices.Contains(b.chatModels.Models(), "acme/acme-tool") {
|
||||
t.Errorf("chat models = %v, want the refreshed reference offered", b.chatModels.Models())
|
||||
}
|
||||
if _, ok := b.chatModels.ModelDetails("acme/acme-tool"); !ok {
|
||||
t.Error("chat model details missing the refreshed model")
|
||||
}
|
||||
if got := b.chatModels.ProviderDisplayName("acme"); got != "ACME" {
|
||||
t.Errorf("provider display name = %q, want the refreshed catalog's name", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestModelCatalogRefreshLoopPicksUpANewModelWhileTheDaemonRuns is the
|
||||
// without-a-restart proof: the process is already running on the catalog it
|
||||
// booted with, and a model that appears upstream afterwards is offered by the
|
||||
// next tick.
|
||||
func TestModelCatalogRefreshLoopPicksUpANewModelWhileTheDaemonRuns(t *testing.T) {
|
||||
var calls atomic.Int64
|
||||
srv := catalogServer(t, func() map[string]any {
|
||||
if calls.Add(1) == 1 {
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-one")}
|
||||
}
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-one", "acme-two")}
|
||||
})
|
||||
t.Setenv("ACME_API_KEY", "resolved-by-the-secret-registry")
|
||||
b := newCatalogBoot(t, srv.URL)
|
||||
if err := b.refreshModelCatalog(t.Context()); err != nil {
|
||||
t.Fatalf("initial refresh: %v", err)
|
||||
}
|
||||
if slices.Contains(b.chatModels.Models(), "acme/acme-two") {
|
||||
t.Fatal("the second model was offered before upstream published it")
|
||||
}
|
||||
|
||||
b.catalogRefreshInterval = 5 * time.Millisecond
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
b.startModelCatalogRefresh(ctx)
|
||||
|
||||
awaitCatalog(t, b, "acme/acme-two")
|
||||
if got := b.cfgHolder.Get().ModelLimits["acme/acme-two"]; got.ContextWindow != 128000 {
|
||||
t.Errorf("running model limits for the new model = %+v, want the refreshed capacity", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRefreshModelCatalogKeepsTheLoadedCatalogWhenTheReadFails: the catalog
|
||||
// service having a bad minute must not blank the model list the process is
|
||||
// already running on. The failed read is reported; the snapshot stays.
|
||||
func TestRefreshModelCatalogKeepsTheLoadedCatalogWhenTheReadFails(t *testing.T) {
|
||||
srv := catalogServer(t, func() map[string]any {
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-one")}
|
||||
})
|
||||
t.Setenv("ACME_API_KEY", "resolved-by-the-secret-registry")
|
||||
b := newCatalogBoot(t, srv.URL)
|
||||
if err := b.refreshModelCatalog(t.Context()); err != nil {
|
||||
t.Fatalf("refreshModelCatalog: %v", err)
|
||||
}
|
||||
|
||||
// The endpoint stops answering and the cache is gone, so the read has no
|
||||
// fallback left.
|
||||
srv.Close()
|
||||
if err := os.Remove(b.catalogCachePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Fatalf("remove cache: %v", err)
|
||||
}
|
||||
if err := b.refreshModelCatalog(t.Context()); err == nil {
|
||||
t.Fatal("refreshModelCatalog error = nil, want the failed read reported")
|
||||
}
|
||||
|
||||
if _, models := b.catalogState(); !slices.Contains(models, "acme/acme-one") {
|
||||
t.Errorf("catalog models = %v, want the loaded catalog kept", models)
|
||||
}
|
||||
if got := b.cfgHolder.Get().ModelLimits["acme/acme-one"]; got.ContextWindow != 128000 {
|
||||
t.Errorf("running model limits = %+v, want the last-known-good catalog", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBootCatalogReachesTheRunningConfig is the projection-site check for the
|
||||
// boot half: the catalog boot read must be in the config the process runs on,
|
||||
// not only in the boot struct the chat runtime was built from. Otherwise the
|
||||
// first live update is the first snapshot to carry a catalog model limit.
|
||||
func TestBootCatalogReachesTheRunningConfig(t *testing.T) {
|
||||
srv := catalogServer(t, func() map[string]any {
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-one")}
|
||||
})
|
||||
t.Setenv("ACME_API_KEY", "resolved-by-the-secret-registry")
|
||||
b := newCatalogBoot(t, srv.URL)
|
||||
|
||||
b.loadCatalog(t.Context(), filepath.Join(t.TempDir(), "config.toml"))
|
||||
|
||||
if got := b.cfgHolder.Get().ModelLimits["acme/acme-one"]; got.ContextWindow != 128000 {
|
||||
t.Errorf("running model limits = %+v, want the catalog boot loaded", got)
|
||||
}
|
||||
if _, models := b.catalogState(); !slices.Contains(models, "acme/acme-one") {
|
||||
t.Errorf("catalog models = %v, want the boot catalog", models)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatModelManagerReplacesItsCatalogRatherThanMerging: a refresh must
|
||||
// drop what the previous snapshot contributed. A manager that only merged
|
||||
// would keep offering a model the catalog no longer publishes, and would
|
||||
// describe a provider that no longer exists.
|
||||
func TestChatModelManagerReplacesItsCatalogRatherThanMerging(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{"chat": "acme/one"})
|
||||
manager.SetModelCatalog(
|
||||
modelcatalog.Snapshot{Providers: []modelcatalog.Provider{
|
||||
{ID: "acme", Name: "Acme", Models: []modelcatalog.Model{{ID: "one"}, {ID: "two"}}},
|
||||
}},
|
||||
[]string{"acme/one", "acme/two"},
|
||||
)
|
||||
if !slices.Contains(manager.Models(), "acme/two") {
|
||||
t.Fatalf("chat models = %v, want the catalog's models offered", manager.Models())
|
||||
}
|
||||
if got := manager.ProviderDisplayName("acme"); got != "Acme" {
|
||||
t.Fatalf("provider display name = %q, want the catalog's name", got)
|
||||
}
|
||||
|
||||
manager.SetModelCatalog(
|
||||
modelcatalog.Snapshot{Providers: []modelcatalog.Provider{
|
||||
{ID: "acme", Name: "Acme", Models: []modelcatalog.Model{{ID: "one"}}},
|
||||
}},
|
||||
[]string{"acme/one"},
|
||||
)
|
||||
|
||||
if slices.Contains(manager.Models(), "acme/two") {
|
||||
t.Errorf("chat models = %v, want the withdrawn model dropped", manager.Models())
|
||||
}
|
||||
if _, ok := manager.ModelDetails("acme/two"); ok {
|
||||
t.Error("chat model details still carry the withdrawn model")
|
||||
}
|
||||
if got := manager.ProviderDisplayName("gone"); got != "" {
|
||||
t.Errorf("provider display name for an absent provider = %q, want empty", got)
|
||||
}
|
||||
if !slices.Contains(manager.Models(), "acme/one") {
|
||||
t.Errorf("chat models = %v, want the surviving model kept", manager.Models())
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
)
|
||||
|
||||
func TestApplyModelCatalogNormalizesEveryExecutionScope(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Providers: map[string]config.Provider{
|
||||
"openai": {Class: "custom", APIKeyEnv: "CUSTOM_KEY"},
|
||||
},
|
||||
Identities: []config.IdentityConfig{
|
||||
{Name: "one", Providers: map[string]config.Provider{
|
||||
"identity-only": {Class: "openai-compatible"},
|
||||
}},
|
||||
},
|
||||
}
|
||||
snapshot := modelcatalog.Snapshot{Providers: []modelcatalog.Provider{
|
||||
{
|
||||
ID: "openai", Class: "openai", APIKeyEnv: "OPENAI_API_KEY",
|
||||
BaseURL: "https://api.openai.test/v1",
|
||||
Models: []modelcatalog.Model{{ID: "gpt-tool", ContextWindow: 128_000, MaxOutputTokens: 16_000}},
|
||||
},
|
||||
{
|
||||
ID: "anthropic", Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY",
|
||||
Models: []modelcatalog.Model{{ID: "claude-tool"}},
|
||||
},
|
||||
}}
|
||||
|
||||
models := applyModelCatalog(&cfg, snapshot)
|
||||
|
||||
if got := cfg.Providers["openai"]; got.Class != "custom" || got.APIKeyEnv != "CUSTOM_KEY" {
|
||||
t.Fatalf("explicit global provider was overwritten: %#v", got)
|
||||
}
|
||||
if _, ok := cfg.Providers["anthropic"]; !ok {
|
||||
t.Fatal("discovered provider missing from global execution config")
|
||||
}
|
||||
if _, ok := cfg.Identities[0].Providers["anthropic"]; !ok {
|
||||
t.Fatal("discovered provider missing from identity execution config")
|
||||
}
|
||||
if _, ok := cfg.Identities[0].Providers["identity-only"]; !ok {
|
||||
t.Fatal("explicit identity provider was removed")
|
||||
}
|
||||
wantModels := []string{"anthropic/claude-tool", "openai/gpt-tool"}
|
||||
if !reflect.DeepEqual(models, wantModels) {
|
||||
t.Fatalf("models = %v, want %v", models, wantModels)
|
||||
}
|
||||
if got := cfg.ModelLimits["openai/gpt-tool"]; got.ContextWindow != 128_000 || got.MaxOutputTokens != 16_000 {
|
||||
t.Fatalf("model limits = %+v, want catalog capacity", got)
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
func TestCatalogViewListsEachAvailableProviderWithItsModels(t *testing.T) {
|
||||
snapshot := modelcatalog.Snapshot{Providers: []modelcatalog.Provider{
|
||||
{ID: "openai", Name: "OpenAI", Class: "openai", APIKeyEnv: "OPENAI_API_KEY", Models: []modelcatalog.Model{{ID: "gpt-b"}, {ID: "gpt-a"}}},
|
||||
{ID: "anthropic", Name: "Anthropic", Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY", BaseURL: "https://api.example", Models: []modelcatalog.Model{{ID: "claude"}}},
|
||||
}}
|
||||
want := []webui.CatalogProviderView{
|
||||
{ID: "anthropic", Name: "Anthropic", Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY", BaseURL: "https://api.example", Models: []string{"claude"}},
|
||||
{ID: "openai", Name: "OpenAI", Class: "openai", APIKeyEnv: "OPENAI_API_KEY", Models: []string{"gpt-a", "gpt-b"}},
|
||||
}
|
||||
if got := catalogView(snapshot); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("catalogView =\n%#v\nwant\n%#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalogViewOfAnEmptySnapshotIsEmptyNotNil(t *testing.T) {
|
||||
got := catalogView(modelcatalog.Snapshot{})
|
||||
if got == nil || len(got) != 0 {
|
||||
t.Fatalf("catalogView(empty) = %#v, want an empty slice", got)
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
// capturedChatRequest is the JSON body the OpenAI-compatible provider sent for
|
||||
// the last call, which is where the max-token parameter name is chosen.
|
||||
type capturedChatRequest struct {
|
||||
mu sync.Mutex
|
||||
body map[string]any
|
||||
}
|
||||
|
||||
func (c *capturedChatRequest) get() map[string]any {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.body
|
||||
}
|
||||
|
||||
// newCapturingChatAPI answers every completion and records the wire body, so a
|
||||
// test can assert the parameter the provider actually emitted rather than an
|
||||
// option the composition root merely set.
|
||||
func newCapturingChatAPI(t *testing.T, capture *capturedChatRequest) *httptest.Server {
|
||||
t.Helper()
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
capture.mu.Lock()
|
||||
capture.body = body
|
||||
capture.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"test",`+
|
||||
`"choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],`+
|
||||
`"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
return api
|
||||
}
|
||||
|
||||
// assertWireBound asserts the max-token parameter the provider emitted: absent
|
||||
// entirely for a reasoning model (its only accepted form with this ai-sdk
|
||||
// release), present with the caller's bound for a classic one.
|
||||
func assertWireBound(t *testing.T, capture *capturedChatRequest, wantBound bool, wantValue int) {
|
||||
t.Helper()
|
||||
bound, present := capture.get()["max_tokens"]
|
||||
if present != wantBound {
|
||||
t.Fatalf("request body max_tokens present = %v (value %v), want %v; body = %v", present, bound, wantBound, capture.get())
|
||||
}
|
||||
if present && bound != float64(wantValue) {
|
||||
t.Fatalf("request body max_tokens = %v, want %d; body = %v", bound, wantValue, capture.get())
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatTurnParameterNameFollowsModelClass proves the chat turn path reaches
|
||||
// the provider without a max-token bound for a reasoning-class model, while a
|
||||
// classic model still carries one. The bound itself is unchanged; only the
|
||||
// parameter the chat-completions provider would serialise it into differs, and
|
||||
// reasoning models reject `max_tokens` with HTTP 400.
|
||||
func TestChatTurnParameterNameFollowsModelClass(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reasoning bool
|
||||
wantBound bool
|
||||
modelRef string
|
||||
maxOutput int
|
||||
wantValue int
|
||||
}{
|
||||
{name: "reasoning model", reasoning: true, wantBound: false, modelRef: "openai/gpt-5.4", maxOutput: 4096},
|
||||
{name: "classic model", reasoning: false, wantBound: true, modelRef: "openai/gpt-4o", maxOutput: 4096, wantValue: 4096},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
capture := &capturedChatRequest{}
|
||||
api := newCapturingChatAPI(t, capture)
|
||||
llm := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
})
|
||||
model := newChatTurnModel(func() *runtime.Runtime { return llm }, tools.NewRegistry(), 1, func() agentexec.ToolLimits { return agentexec.ToolLimits{} }, nil)
|
||||
|
||||
prepared, err := model.Prepare(t.Context(), gateway.TurnPrepareContext{
|
||||
Model: tc.modelRef, Reasoning: tc.reasoning,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
_, err = prepared.Generate(t.Context(), gateway.TurnModelRequest{
|
||||
Messages: []gateway.CompressedMessage{{Role: "user", Content: "hi"}},
|
||||
MaxOutputTokens: tc.maxOutput,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error = %v", err)
|
||||
}
|
||||
|
||||
assertWireBound(t, capture, tc.wantBound, tc.wantValue)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMCPSamplingParameterNameFollowsModelClass proves the second place the
|
||||
// process sets a max-token bound -- server-requested sampling, which also
|
||||
// applies a default when the request is silent -- preserves it for a classic
|
||||
// model and drops it entirely for a reasoning one.
|
||||
func TestMCPSamplingParameterNameFollowsModelClass(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
modelRef string
|
||||
model modelcatalog.Model
|
||||
wantBound bool
|
||||
wantValue int
|
||||
}{
|
||||
{
|
||||
name: "reasoning model",
|
||||
modelRef: "openai/gpt-5.4",
|
||||
model: modelcatalog.Model{ID: "gpt-5.4", Reasoning: true, MaxOutputTokens: 128000},
|
||||
},
|
||||
{
|
||||
name: "classic model",
|
||||
modelRef: "openai/gpt-4o",
|
||||
model: modelcatalog.Model{ID: "gpt-4o", MaxOutputTokens: 128000},
|
||||
wantBound: true,
|
||||
wantValue: agentexec.DefaultSamplingMaxTokens,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
capture := &capturedChatRequest{}
|
||||
api := newCapturingChatAPI(t, capture)
|
||||
b := newBootstrap()
|
||||
b.setLLM(agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
}))
|
||||
b.chatModels = newChatModelManager(map[string]string{"chat": tc.modelRef})
|
||||
b.chatModels.SetModelCatalog(modelcatalog.Snapshot{
|
||||
Providers: []modelcatalog.Provider{{ID: "openai", Models: []modelcatalog.Model{tc.model}}},
|
||||
}, []string{tc.modelRef})
|
||||
b.providerOutcomes = newProviderOutcomeRecorder()
|
||||
|
||||
_, err := b.mcpSamplingHandler()(t.Context(), mcp.SamplingRequest{
|
||||
Messages: []mcp.SamplingMessage{{Role: "user", Content: mcp.SamplingContent{Type: "text", Text: "hi"}}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("sampling handler error = %v", err)
|
||||
}
|
||||
|
||||
assertWireBound(t, capture, tc.wantBound, tc.wantValue)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEmbeddedNATSEndpointRoundTripIsPrivateAndAtomic(t *testing.T) {
|
||||
stateDir := t.TempDir()
|
||||
if err := writeEmbeddedNATSEndpoint(stateDir, "nats://127.0.0.1:4222", "secret"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := readEmbeddedNATSEndpoint(stateDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.URL != "nats://127.0.0.1:4222" || got.Token != "secret" {
|
||||
t.Fatalf("endpoint = %+v", got)
|
||||
}
|
||||
if want := filepath.Join(stateDir, "nats", "endpoint.json"); embeddedNATSEndpointPath(stateDir) != want {
|
||||
t.Fatalf("endpoint path = %q, want %q", embeddedNATSEndpointPath(stateDir), want)
|
||||
}
|
||||
info, err := os.Stat(embeddedNATSEndpointPath(stateDir))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Mode().Perm() != 0o600 {
|
||||
t.Fatalf("endpoint permissions = %o, want 600", info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"context"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/test/bufconn"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storepkg"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
)
|
||||
|
||||
// contributedLayer ships one workflow definition file's portable YAML.
|
||||
func contributedLayer(t *testing.T, path, yaml string) []byte {
|
||||
t.Helper()
|
||||
var raw bytes.Buffer
|
||||
writer := tar.NewWriter(&raw)
|
||||
if err := writer.WriteHeader(&tar.Header{Typeflag: tar.TypeReg, Name: path, Size: int64(len(yaml))}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := writer.Write([]byte(yaml)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return raw.Bytes()
|
||||
}
|
||||
|
||||
func packageDescriptor() storepkg.Descriptor {
|
||||
return storepkg.Descriptor{
|
||||
APIVersion: storepkg.APIVersion, DisplayName: "Review pack", Version: "1",
|
||||
Contributes: storepkg.Contributions{Workflows: []string{"workflows/review.yaml"}},
|
||||
Files: []storepkg.File{{Path: "workflows/review.yaml", Mode: 0o644}},
|
||||
}
|
||||
}
|
||||
|
||||
// TestPackageContributionsLiveRoundTrip drives the State Store's own
|
||||
// composition — the projections stateStoreDeps wires, the install and remove
|
||||
// RPCs the dashboard calls, and the workflow-definitions resource the daemon
|
||||
// reads per dispatch — over one real gRPC connection: installing a package
|
||||
// makes its workflow a usable org resource, and removing it stops being
|
||||
// usable, both without restarting anything.
|
||||
func TestPackageContributionsLiveRoundTrip(t *testing.T) {
|
||||
db := pgstore.Open(t)
|
||||
projections, err := newPackageProjections(db.Pool)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const yaml = "id: review\nsteps:\n - type: bootstrap.apply\n"
|
||||
service := storepkg.Service{
|
||||
Registry: packageLayerRegistry{descriptor: packageDescriptor(), layer: contributedLayer(t, "workflows/review.yaml", yaml)},
|
||||
Store: postgres.NewInstalledPackages(db.Pool),
|
||||
Projections: projections,
|
||||
}
|
||||
listener := bufconn.Listen(1 << 20)
|
||||
server := grpc.NewServer()
|
||||
staterpc.RegisterServer(server, staterpc.Deps{Packages: service})
|
||||
go func() { _ = server.Serve(listener) }()
|
||||
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
|
||||
conn, err := grpc.NewClient("passthrough:///state",
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { return listener.DialContext(ctx) }))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = conn.Close() })
|
||||
client := staterpc.NewClient(conn)
|
||||
const digest = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
|
||||
if _, err := client.InstallPackage(t.Context(), "org-a", "review", "localhost:5000/review", digest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The contribution is usable: the org resource the daemon's
|
||||
// WorkflowDefinitions client reads carries it now, no restart involved.
|
||||
resource, err := postgres.NewResources(db.Pool).Resource(t.Context(), "org-a", controlplane.WorkflowDefinitionsKind)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
collection, err := workflow.DecodeDefinitionCollection(resource.Value, workflow.BuiltinStepRegistry())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := collection.DefinitionByID("review"); !ok {
|
||||
t.Fatal("installed package's workflow is not an org workflow after install")
|
||||
}
|
||||
if err := client.RemoveInstalled(t.Context(), "org-a", "review"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resource, err = postgres.NewResources(db.Pool).Resource(t.Context(), "org-a", controlplane.WorkflowDefinitionsKind)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
collection, err = workflow.DecodeDefinitionCollection(resource.Value, workflow.BuiltinStepRegistry())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := collection.DefinitionByID("review"); ok {
|
||||
t.Fatal("removed package's workflow still dispatches: the org resource kept it")
|
||||
}
|
||||
if entries, err := postgres.NewPackageContributions(db.Pool).Entries(t.Context(), "org-a", "review"); err != nil || len(entries) != 0 {
|
||||
t.Fatalf("contributions ledger after removal = %#v, %v", entries, err)
|
||||
}
|
||||
}
|
||||
|
||||
type packageLayerRegistry struct {
|
||||
descriptor storepkg.Descriptor
|
||||
layer []byte
|
||||
}
|
||||
|
||||
func (r packageLayerRegistry) Fetch(context.Context, string, string) (storepkg.Descriptor, []byte, error) {
|
||||
return r.descriptor, r.layer, nil
|
||||
}
|
||||
|
||||
// TestStateStoreDepsWiresPackageProjections pins the wiring the live round
|
||||
// trip drives: stateStoreDeps builds projections on the State Store's pool and
|
||||
// carries them into the packages service it composes.
|
||||
func TestStateStoreDepsWiresPackageProjections(t *testing.T) {
|
||||
file := "state_store.go"
|
||||
fset := token.NewFileSet()
|
||||
parsed, err := parser.ParseFile(fset, file, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var calls []string
|
||||
ast.Inspect(parsed, func(node ast.Node) bool {
|
||||
if call, ok := node.(*ast.CallExpr); ok {
|
||||
if fn, ok := call.Fun.(*ast.Ident); ok {
|
||||
calls = append(calls, fn.Name)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
found := false
|
||||
for _, call := range calls {
|
||||
if call == "newPackageProjections" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("stateStoreDeps (%s body) wires no package projections", file)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package archied
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseListenAddrPreservesEphemeralPort(t *testing.T) {
|
||||
host, port := parseListenAddr("127.0.0.1:0", "0.0.0.0", 8644)
|
||||
if host != "127.0.0.1" || port != 0 {
|
||||
t.Fatalf("parseListenAddr() = (%q, %d), want (%q, %d)", host, port, "127.0.0.1", 0)
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
// TestMain runs the archied test binary against a throwaway PostgreSQL 18
|
||||
// container, mirroring internal/infrastructure/postgres. The State Store's
|
||||
// openStateStore fails closed without a working database_url, so the tests
|
||||
// that exercise it (e.g. TestStateStoreDataSurvivesRestart) need a real pool.
|
||||
func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) }
|
||||
@@ -1,297 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/module"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// docs/prds/plugin-settings-live.md: a plugin or secret-engine file dropped
|
||||
// into the running configuration's directory loads without a restart, an
|
||||
// edited file replaces what runs, and a removed file keeps running with the
|
||||
// removal reported through apply status until the process restarts.
|
||||
|
||||
const reconcilerPluginSource = `package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "hello" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
}
|
||||
`
|
||||
|
||||
const reconcilerSecretSource = `package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/secret"
|
||||
|
||||
var Engine = secret._Engine{
|
||||
WName: func() string { return "hello" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
WResolve: func(key string) (string, error) { return "one:" + key, nil },
|
||||
}
|
||||
`
|
||||
|
||||
const reconcilerEditedSecretSource = `package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/secret"
|
||||
|
||||
var Engine = secret._Engine{
|
||||
WName: func() string { return "hello" },
|
||||
WVersion: func() string { return "2.0.0" },
|
||||
WResolve: func(key string) (string, error) { return "two:" + key, nil },
|
||||
}
|
||||
`
|
||||
|
||||
// reconcilerHarness is one running directory set: a started plugin host, a
|
||||
// fresh secret registry, an empty module registry, and the config that names
|
||||
// the two directories a test writes into.
|
||||
type reconcilerHarness struct {
|
||||
root *plugin.Host
|
||||
secrets *secret.Registry
|
||||
cfg config.Config
|
||||
plugins string
|
||||
secretsD string
|
||||
mods *module.ModuleRegistry
|
||||
}
|
||||
|
||||
func newReconcilerHarness(t *testing.T) *reconcilerHarness {
|
||||
t.Helper()
|
||||
h := &reconcilerHarness{
|
||||
root: plugin.NewHost(),
|
||||
secrets: secret.NewRegistry(),
|
||||
mods: module.New(),
|
||||
plugins: t.TempDir(),
|
||||
secretsD: t.TempDir(),
|
||||
}
|
||||
if err := h.root.Start(t.Context()); err != nil {
|
||||
t.Fatalf("start host: %v", err)
|
||||
}
|
||||
h.cfg = config.Config{PluginDir: h.plugins, SecretEngineDir: h.secretsD}
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *reconcilerHarness) reconciler() *pluginReconciler {
|
||||
return newPluginReconciler(slog.New(slog.DiscardHandler), pluginReconcileTargets{
|
||||
host: h.root,
|
||||
secrets: h.secrets,
|
||||
modules: h.mods,
|
||||
dirs: func() config.Config { return h.cfg },
|
||||
})
|
||||
}
|
||||
|
||||
func writeReconcilerFile(t *testing.T, path, source string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(source), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginReconcilerLoadsADroppedPluginAndSecretEngine(t *testing.T) {
|
||||
h := newReconcilerHarness(t)
|
||||
r := h.reconciler()
|
||||
// Boot loaded the (empty) directories; seed is what the daemon does after
|
||||
// its own boot load so the first tick does not re-load the boot set.
|
||||
r.seed()
|
||||
|
||||
writeReconcilerFile(t, filepath.Join(h.plugins, "hello.go"), reconcilerPluginSource)
|
||||
writeReconcilerFile(t, filepath.Join(h.secretsD, "hello.go"), reconcilerSecretSource)
|
||||
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("reconcile: %v", err)
|
||||
}
|
||||
if !h.root.Has("hello") {
|
||||
t.Error("dropped plugin was not loaded in place")
|
||||
}
|
||||
engine, ok := h.secrets.Get("hello")
|
||||
if !ok {
|
||||
t.Fatal("dropped secret engine was not loaded in place")
|
||||
}
|
||||
value, err := engine.Resolve("k")
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v", err)
|
||||
}
|
||||
if value != "one:k" {
|
||||
t.Errorf("Resolve() = %q, want one:k", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginReconcilerReplacesAnEditedSecretEngine(t *testing.T) {
|
||||
h := newReconcilerHarness(t)
|
||||
r := h.reconciler()
|
||||
r.seed()
|
||||
|
||||
path := filepath.Join(h.secretsD, "hello.go")
|
||||
writeReconcilerFile(t, path, reconcilerSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("first reconcile: %v", err)
|
||||
}
|
||||
|
||||
writeReconcilerFile(t, path, reconcilerEditedSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("second reconcile: %v", err)
|
||||
}
|
||||
engine, ok := h.secrets.Get("hello")
|
||||
if !ok {
|
||||
t.Fatal("edited secret engine disappeared")
|
||||
}
|
||||
value, err := engine.Resolve("k")
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v", err)
|
||||
}
|
||||
if value != "two:k" {
|
||||
t.Errorf("Resolve() after edit = %q, want the replaced two:k", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginReconcilerReportsARemovedFileThatKeepsRunning(t *testing.T) {
|
||||
h := newReconcilerHarness(t)
|
||||
r := h.reconciler()
|
||||
r.seed()
|
||||
|
||||
pluginPath := filepath.Join(h.plugins, "hello.go")
|
||||
secretPath := filepath.Join(h.secretsD, "hello.go")
|
||||
writeReconcilerFile(t, pluginPath, reconcilerPluginSource)
|
||||
writeReconcilerFile(t, secretPath, reconcilerSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("first reconcile: %v", err)
|
||||
}
|
||||
|
||||
if err := os.Remove(pluginPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Remove(secretPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := r.reconcile(t.Context())
|
||||
if err == nil {
|
||||
t.Fatal("a reconcile after two removals reported no problem")
|
||||
}
|
||||
for _, want := range []string{"hello.go", "removed", "restart"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("removal report %q does not mention %q", err, want)
|
||||
}
|
||||
}
|
||||
// The running code is not unloaded: Yaegi has no unload path, so both the
|
||||
// module and the engine keep serving until the process restarts.
|
||||
if !h.root.Has("hello") {
|
||||
t.Error("a removed plugin stopped running")
|
||||
}
|
||||
if _, ok := h.secrets.Get("hello"); !ok {
|
||||
t.Error("a removed secret engine was unregistered")
|
||||
}
|
||||
|
||||
// Re-adding the file clears the removal: the replacement takes over and no
|
||||
// old interpreter is left serving the path.
|
||||
writeReconcilerFile(t, pluginPath, reconcilerPluginSource)
|
||||
writeReconcilerFile(t, secretPath, reconcilerSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("reconcile after re-adding the files: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginSettingsLiveUpdateLoadsTheNewDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeReconcilerFile(t, filepath.Join(dir, "hello.go"), reconcilerPluginSource)
|
||||
|
||||
running := fileConfig()
|
||||
b, status := newLiveApplyBoot(t, running)
|
||||
resources := databaseOwnedResources()
|
||||
resources[controlplane.PluginSettingsKind] = map[string]any{
|
||||
"plugin_dir": dir, "module_dir": "/db/modules", "secret_engine_dir": "/db/secrets", "skills_dir": "",
|
||||
}
|
||||
b.controlPlane = controlplane.NewRPCClient(&controlPlaneStub{values: resources})
|
||||
b.secrets = secret.NewRegistry()
|
||||
host := plugin.NewHost()
|
||||
if err := host.Start(t.Context()); err != nil {
|
||||
t.Fatalf("start host: %v", err)
|
||||
}
|
||||
b.pluginReconciler = newPluginReconciler(b.log, pluginReconcileTargets{
|
||||
host: host,
|
||||
secrets: b.secrets,
|
||||
modules: module.New(),
|
||||
dirs: func() config.Config { return b.cfgHolder.Get() },
|
||||
})
|
||||
b.pluginReconciler.seed()
|
||||
|
||||
b.applyRuntimeResourceUpdate(t.Context(), controlplane.PluginSettingsKind, controlplane.AppliedResource{Version: 2})
|
||||
|
||||
if got := b.cfgHolder.Get().PluginDir; got != dir {
|
||||
t.Fatalf("running plugin dir = %q, want the stored %q", got, dir)
|
||||
}
|
||||
if !host.Has("hello") {
|
||||
t.Fatal("a stored plugin_dir change did not load its plugin without a restart")
|
||||
}
|
||||
last, ok := status.last(controlplane.PluginSettingsKind)
|
||||
if !ok {
|
||||
t.Fatal("the live plugin-settings update reported no apply status")
|
||||
}
|
||||
if last.Error != "" {
|
||||
t.Errorf("apply status error = %q, want none for a live directory load", last.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginSettingsSkillsDirChangeIsRefusedOnTheLivePath(t *testing.T) {
|
||||
running := fileConfig()
|
||||
b, status := newLiveApplyBoot(t, running)
|
||||
// The stored document's skills_dir (/db/skills) differs from the running
|
||||
// empty value, and skills_dir has no live consumer.
|
||||
b.controlPlane = controlplane.NewRPCClient(&controlPlaneStub{values: databaseOwnedResources()})
|
||||
|
||||
b.applyRuntimeResourceUpdate(t.Context(), controlplane.PluginSettingsKind, controlplane.AppliedResource{Version: 2})
|
||||
|
||||
if got := b.cfgHolder.Get().PluginDir; got != fileConfig().PluginDir {
|
||||
t.Errorf("a refused plugin-settings document published plugin_dir %q, want the file's %q", got, fileConfig().PluginDir)
|
||||
}
|
||||
last, ok := status.last(controlplane.PluginSettingsKind)
|
||||
if !ok {
|
||||
t.Fatal("the refused plugin-settings update reported no apply status")
|
||||
}
|
||||
if !strings.Contains(last.Error, "skills_dir") {
|
||||
t.Errorf("apply status error = %q, want it to name skills_dir", last.Error)
|
||||
}
|
||||
if last.AppliedVersion != 2 {
|
||||
t.Errorf("apply status version = %d, want the refused version 2", last.AppliedVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginReconcilerReportsTheApplyStatusTruth(t *testing.T) {
|
||||
h := newReconcilerHarness(t)
|
||||
var reported []error
|
||||
reported = nil
|
||||
r := newPluginReconciler(slog.New(slog.DiscardHandler), pluginReconcileTargets{
|
||||
host: h.root,
|
||||
secrets: h.secrets,
|
||||
modules: h.mods,
|
||||
dirs: func() config.Config { return h.cfg },
|
||||
report: func(_ context.Context, err error) { reported = append(reported, err) },
|
||||
})
|
||||
r.seed()
|
||||
|
||||
writeReconcilerFile(t, filepath.Join(h.secretsD, "hello.go"), reconcilerSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("clean reconcile: %v", err)
|
||||
}
|
||||
if len(reported) != 1 || reported[0] != nil {
|
||||
t.Fatalf("clean reconcile reported %v, want one nil (a live add is applied)", reported)
|
||||
}
|
||||
|
||||
if err := os.Remove(filepath.Join(h.secretsD, "hello.go")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.reconcile(t.Context()); err == nil {
|
||||
t.Fatal("removal reconcile reported no error")
|
||||
}
|
||||
if len(reported) != 2 || reported[1] == nil {
|
||||
t.Fatalf("removal reconcile reported %v, want the removal as the second record", reported)
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// The State Store seeds provider-settings from the config it booted with, so
|
||||
// building its secret registry must leave every provider's source reference
|
||||
// in place rather than rewriting it to a process-local env name.
|
||||
func TestConfiguredSecretRegistryKeepsProviderReferences(t *testing.T) {
|
||||
ref := secret.SecretRef{Engine: "env", Key: "OPENAI_KEY"}
|
||||
t.Setenv("OPENAI_KEY", "sk-test")
|
||||
cfg := config.Config{Providers: map[string]config.Provider{"openai": {Class: "openai", APIKey: ref}}}
|
||||
|
||||
if _, err := configuredSecretRegistry(&cfg, slog.New(slog.DiscardHandler)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := cfg.Providers["openai"]
|
||||
if got.APIKey != ref || got.APIKeyEnv != "" {
|
||||
t.Fatalf("provider rewritten: api_key=%+v api_key_env=%q", got.APIKey, got.APIKeyEnv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateStoreBootSeedsProviderSourceReference(t *testing.T) {
|
||||
url := pgtest.URL(t)
|
||||
store := pgstore.On(pgstore.PoolAt(t, url))
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
ref := secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_PROVIDER_SOURCE"}
|
||||
t.Setenv(ref.Key, "provider-secret")
|
||||
body := fmt.Sprintf("bot_user = 'archie'\ndatabase_url = %q\n[providers.openai]\nclass = 'openai'\napi_key = {engine = 'env', key = %q}\n", url, ref.Key)
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "127.0.0.1:0"}) }()
|
||||
deadline := time.After(10 * time.Second)
|
||||
for {
|
||||
resource, err := store.Resource(t.Context(), storecontract.DefaultOrgID, controlplane.ProviderSettingsKind)
|
||||
if err == nil {
|
||||
if strings.Contains(string(resource.Value), "ARCHIE_PROVIDER_") || !strings.Contains(string(resource.Value), ref.Key) {
|
||||
t.Fatalf("stored provider settings = %s, want source reference and no derived env name", resource.Value)
|
||||
}
|
||||
break
|
||||
}
|
||||
select {
|
||||
case bootErr := <-errCh:
|
||||
t.Fatalf("RunStateStore failed before provider import: %v", bootErr)
|
||||
case <-deadline:
|
||||
t.Fatalf("provider settings were not imported: %v", err)
|
||||
case <-time.After(25 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
if err := <-errCh; err != nil && !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("RunStateStore shutdown: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
// TestConnectNATSCreatesReactionStream pins the two JetStream streams the
|
||||
// daemon must provision alongside each other: ARCHIE_TASKS (work-queue task
|
||||
// distribution) and ARCHIE_REACTIONS (fan-out reaction delivery). Each case
|
||||
// asserts the ACTUAL stream configuration the embedded broker reports, not
|
||||
// just the Config struct that composed it.
|
||||
//
|
||||
// ARCHIE_REACTIONS must be a LimitsPolicy stream so two consumers on
|
||||
// overlapping filter subjects both receive a reaction (the trap documented in
|
||||
// CLAUDE.md), and it must carry a finite retention limit so acknowledged
|
||||
// reactions do not accumulate without bound. ARCHIE_TASKS must remain a
|
||||
// WorkQueuePolicy stream with no age/count/byte limit: acked messages are
|
||||
// discarded, so its only bound is outstanding work, and imposing a limit
|
||||
// could silently drop undelivered tasks.
|
||||
func TestConnectNATSCreatesReactionStream(t *testing.T) {
|
||||
b := &boot{
|
||||
cfg: config.Config{
|
||||
StateDir: t.TempDir(),
|
||||
NATS: config.NATSConfig{Mode: config.NATSModeEmbedded},
|
||||
},
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
if err := b.connectNATS(t.Context()); err != nil {
|
||||
t.Fatalf("connectNATS = %v", err)
|
||||
}
|
||||
t.Cleanup(b.cleanup)
|
||||
|
||||
conn, err := natsio.Connect(b.natsURL, natsio.Token(b.natsToken))
|
||||
if err != nil {
|
||||
t.Fatalf("raw nats connect = %v", err)
|
||||
}
|
||||
t.Cleanup(conn.Close)
|
||||
|
||||
js, err := jetstream.New(conn)
|
||||
if err != nil {
|
||||
t.Fatalf("jetstream.New = %v", err)
|
||||
}
|
||||
|
||||
streamCases := []struct {
|
||||
name string
|
||||
stream string
|
||||
retention jetstream.RetentionPolicy
|
||||
subjects []string
|
||||
wantMaxAge time.Duration
|
||||
}{
|
||||
{
|
||||
name: "reactions fan-out with finite retention",
|
||||
stream: "ARCHIE_REACTIONS",
|
||||
retention: jetstream.LimitsPolicy,
|
||||
subjects: []string{"archie.reaction.>"},
|
||||
wantMaxAge: reactionStreamMaxAge,
|
||||
},
|
||||
{
|
||||
name: "tasks work-queue unchanged and unbounded",
|
||||
stream: "ARCHIE_TASKS",
|
||||
retention: jetstream.WorkQueuePolicy,
|
||||
subjects: []string{"archie.task.>"},
|
||||
// wantMaxAge zero asserts ARCHIE_TASKS stays unbounded by age.
|
||||
},
|
||||
}
|
||||
for _, tc := range streamCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
stream, err := js.Stream(t.Context(), tc.stream)
|
||||
if err != nil {
|
||||
t.Fatalf("js.Stream(%s) = %v", tc.stream, err)
|
||||
}
|
||||
info, err := stream.Info(t.Context())
|
||||
if err != nil {
|
||||
t.Fatalf("stream.Info(%s) = %v", tc.stream, err)
|
||||
}
|
||||
if info.Config.Retention != tc.retention {
|
||||
t.Fatalf("%s retention = %v, want %v", tc.stream, info.Config.Retention, tc.retention)
|
||||
}
|
||||
for _, subject := range tc.subjects {
|
||||
if !slices.Contains(info.Config.Subjects, subject) {
|
||||
t.Errorf("%s subjects = %v, want to contain %s", tc.stream, info.Config.Subjects, subject)
|
||||
}
|
||||
}
|
||||
if info.Config.MaxAge != tc.wantMaxAge {
|
||||
t.Fatalf("%s MaxAge = %v, want %v", tc.stream, info.Config.MaxAge, tc.wantMaxAge)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Fan-out proof: two consumers on the same overlapping filter subject
|
||||
// both receive the one published message. A work-queue stream would
|
||||
// deliver it to only one of them.
|
||||
reactionStream, err := js.Stream(t.Context(), "ARCHIE_REACTIONS")
|
||||
if err != nil {
|
||||
t.Fatalf("js.Stream(ARCHIE_REACTIONS) = %v", err)
|
||||
}
|
||||
if _, err := js.PublishMsg(t.Context(), &natsio.Msg{
|
||||
Subject: "archie.reaction.test",
|
||||
Data: []byte("x"),
|
||||
}); err != nil {
|
||||
t.Fatalf("publish to archie.reaction.* = %v, want it to land in ARCHIE_REACTIONS", err)
|
||||
}
|
||||
|
||||
got := make(chan string, 2)
|
||||
fetch := func() {
|
||||
consumer, err := reactionStream.CreateOrUpdateConsumer(t.Context(), jetstream.ConsumerConfig{
|
||||
FilterSubject: "archie.reaction.>",
|
||||
AckPolicy: jetstream.AckExplicitPolicy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("create reaction consumer: %v", err)
|
||||
return
|
||||
}
|
||||
batch, err := consumer.Fetch(1, jetstream.FetchMaxWait(5*time.Second))
|
||||
if err != nil {
|
||||
t.Errorf("reaction consumer fetch: %v", err)
|
||||
return
|
||||
}
|
||||
for msg := range batch.Messages() {
|
||||
if msg == nil {
|
||||
continue
|
||||
}
|
||||
got <- string(msg.Data())
|
||||
_ = msg.Ack()
|
||||
}
|
||||
}
|
||||
go fetch()
|
||||
go fetch()
|
||||
|
||||
var received []string
|
||||
deadline := time.After(10 * time.Second)
|
||||
for len(received) < 2 {
|
||||
select {
|
||||
case data := <-got:
|
||||
received = append(received, data)
|
||||
case <-deadline:
|
||||
t.Fatalf("fan-out: received %v, want both consumers to receive x", received)
|
||||
}
|
||||
}
|
||||
for _, data := range received {
|
||||
if data != "x" {
|
||||
t.Errorf("reaction consumer received %q, want x", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
func TestDiskProbePath_PrefersStateDir(t *testing.T) {
|
||||
cfg := config.Config{StateDir: "/var/lib/archie", WorkDir: "/srv/archie"}
|
||||
if got := diskProbePath(cfg); got != "/var/lib/archie" {
|
||||
t.Fatalf("diskProbePath = %q, want /var/lib/archie", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskProbePath_FallsBackToWorkDir(t *testing.T) {
|
||||
cfg := config.Config{WorkDir: "/srv/archie"}
|
||||
if got := diskProbePath(cfg); got != "/srv/archie" {
|
||||
t.Fatalf("diskProbePath = %q, want /srv/archie", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskProbePath_FallsBackToCwd(t *testing.T) {
|
||||
if got := diskProbePath(config.Config{}); got != "." {
|
||||
t.Fatalf("diskProbePath = %q, want .", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPingChat_ReportsUnwiredChat pins that a daemon with no Gateway contract
|
||||
// degrades its gateway probe rather than passing it: chat is served entirely
|
||||
// by the Gateway now, so an unwired contract means no chat at all.
|
||||
func TestPingChat_ReportsUnwiredChat(t *testing.T) {
|
||||
if err := pingChat(context.Background(), nil); err == nil {
|
||||
t.Fatal("pingChat(nil chat) = nil, want an error")
|
||||
}
|
||||
if err := pingChat(context.Background(), &webui.ChatService{}); err == nil {
|
||||
t.Fatal("pingChat(unwired contract) = nil, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupReadinessProbes_WiresEverySubsystem proves the composition root
|
||||
// registers all five readiness probes and that the registry surfaces them in
|
||||
// the detailed report. It does not assert their status -- real state depends
|
||||
// on the store, config, disk, model and gateway in the running daemon -- only
|
||||
// that every subsystem the epic names is actually wired.
|
||||
func TestSetupReadinessProbes_WiresEverySubsystem(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
cfg := config.Config{
|
||||
BotUser: "archie-bot",
|
||||
PollInterval: config.Duration(60_000_000_000), // 60s, satisfies validate
|
||||
Dispatch: config.Dispatch{Trigger: "assignee"},
|
||||
Forge: config.Forge{Type: "none"},
|
||||
Containers: config.ContainerConfig{
|
||||
Image: "ghcr.io/samcharles93/archie-agent:latest",
|
||||
},
|
||||
Providers: map[string]config.Provider{
|
||||
"openai": {Class: "openai"},
|
||||
},
|
||||
}
|
||||
|
||||
b := &boot{
|
||||
st: st,
|
||||
cfgHolder: config.NewHolder(cfg),
|
||||
chat: &webui.ChatService{
|
||||
Contract: &gateway.LocalChatAdapter{
|
||||
Router: &gateway.Router{},
|
||||
Sessions: gateway.NewSessionStoreMemory(),
|
||||
Models: newChatModelManager(map[string]string{"chat": "openai/gpt-4o"}, nil),
|
||||
},
|
||||
},
|
||||
chatModels: newChatModelManager(map[string]string{"chat": "openai/gpt-4o"}, nil),
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
b.setupReadinessProbes()
|
||||
if b.healthRegistry == nil {
|
||||
t.Fatal("b.healthRegistry is nil after setupReadinessProbes")
|
||||
}
|
||||
|
||||
report := b.healthRegistry.Run(context.Background())
|
||||
got := map[string]int{}
|
||||
for _, c := range report.Components {
|
||||
got[c.Name]++
|
||||
}
|
||||
for _, name := range []string{"state_db", "config", "disk", "model", "gateway"} {
|
||||
if got[name] != 1 {
|
||||
t.Errorf("component %q registered %d times, want 1: %+v", name, got[name], report.Components)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,276 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
// writeConfig writes a TOML config file. The minimal shape (bot_user +
|
||||
// one repo) is the same fixture loader_test.go uses to satisfy
|
||||
// validation.
|
||||
func writeConfig(t *testing.T, path, body string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func minimalConfigTOML(botUser string) string {
|
||||
return "bot_user = \"" + botUser + "\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"
|
||||
}
|
||||
|
||||
// invalidConfigTOML carries a value the bootstrap document still owns, so
|
||||
// resolving it fails. The settings the control plane owns are no longer judged
|
||||
// at load, so using one of those here would silently turn these tests into a
|
||||
// false pass.
|
||||
func invalidConfigTOML() string {
|
||||
return "bot_user = \"widget\"\n[memory]\nengine = \"not-a-real-engine\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"
|
||||
}
|
||||
|
||||
func TestReloadAppliesAndPublishes(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
writeConfig(t, path, minimalConfigTOML("first"))
|
||||
|
||||
loader := configuration.New(slog.New(slog.DiscardHandler))
|
||||
var published config.Config
|
||||
controller := newReloadController(loader, path, "", func(_ context.Context, doc *configuration.Document) error {
|
||||
published = doc.Config
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := controller.Reload(t.Context()); err != nil {
|
||||
t.Fatalf("first reload: %v", err)
|
||||
}
|
||||
if published.BotUser != "first" {
|
||||
t.Fatalf("published BotUser = %q, want first", published.BotUser)
|
||||
}
|
||||
|
||||
writeConfig(t, path, minimalConfigTOML("second"))
|
||||
if err := controller.Reload(t.Context()); err != nil {
|
||||
t.Fatalf("second reload: %v", err)
|
||||
}
|
||||
if published.BotUser != "second" {
|
||||
t.Fatalf("published BotUser = %q, want second", published.BotUser)
|
||||
}
|
||||
if st := controller.Status(); st.LastReloadAt == "" || st.LastError != "" {
|
||||
t.Fatalf("Status after success = %+v, want LastReloadAt set and no error", st)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReloadValidationFailureDoesNotApply pins the safety property of
|
||||
// the whole reload feature: a reload that fails validation must not
|
||||
// publish. If apply were called on the error path, a bad file would
|
||||
// half-apply or corrupt the running config.
|
||||
func TestReloadValidationFailureDoesNotApply(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
writeConfig(t, path, minimalConfigTOML("widget"))
|
||||
|
||||
loader := configuration.New(slog.New(slog.DiscardHandler))
|
||||
applied := 0
|
||||
controller := newReloadController(loader, path, "", func(context.Context, *configuration.Document) error { applied++; return nil })
|
||||
|
||||
if err := controller.Reload(t.Context()); err != nil {
|
||||
t.Fatalf("first reload: %v", err)
|
||||
}
|
||||
if applied != 1 {
|
||||
t.Fatalf("apply called %d times after successful reload, want 1", applied)
|
||||
}
|
||||
|
||||
writeConfig(t, path, invalidConfigTOML())
|
||||
err := controller.Reload(t.Context())
|
||||
if err == nil {
|
||||
t.Fatal("reload with invalid config: expected an error")
|
||||
}
|
||||
if applied != 1 {
|
||||
t.Fatalf("apply called %d times after failed reload, want still 1", applied)
|
||||
}
|
||||
if st := controller.Status(); st.LastError == "" || st.LastErrorAt == "" {
|
||||
t.Fatalf("Status after failed reload = %+v, want LastError and LastErrorAt set", st)
|
||||
}
|
||||
|
||||
// A subsequent valid reload clears the error.
|
||||
writeConfig(t, path, minimalConfigTOML("widget"))
|
||||
if err := controller.Reload(t.Context()); err != nil {
|
||||
t.Fatalf("recovery reload: %v", err)
|
||||
}
|
||||
if st := controller.Status(); st.LastError != "" || st.LastReloadAt == "" {
|
||||
t.Fatalf("Status after recovery = %+v, want no error and a reload time", st)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReloadApplyFailureIsRecorded pins the other half of the safety
|
||||
// property: a file that loads cleanly can still fail to apply, because the
|
||||
// database layer reloadConfig re-applies comes from the control plane. That
|
||||
// failure must be recorded and returned, not reported as a successful
|
||||
// reload.
|
||||
func TestReloadApplyFailureIsRecorded(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
writeConfig(t, path, minimalConfigTOML("widget"))
|
||||
|
||||
loader := configuration.New(slog.New(slog.DiscardHandler))
|
||||
controller := newReloadController(loader, path, "", func(context.Context, *configuration.Document) error {
|
||||
return errors.New("runtime settings unavailable")
|
||||
})
|
||||
|
||||
if err := controller.Reload(t.Context()); err == nil {
|
||||
t.Fatal("reload with a failing apply: expected an error")
|
||||
}
|
||||
st := controller.Status()
|
||||
if st.LastError == "" || st.LastErrorAt == "" {
|
||||
t.Fatalf("Status after a failed apply = %+v, want LastError and LastErrorAt set", st)
|
||||
}
|
||||
if st.LastReloadAt != "" {
|
||||
t.Fatalf("Status after a failed apply recorded a reload time: %+v", st)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReloadLoopAppliesOnSignalAndStopsOnCancel(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
writeConfig(t, path, minimalConfigTOML("widget"))
|
||||
|
||||
loader := configuration.New(slog.New(slog.DiscardHandler))
|
||||
applied := make(chan struct{}, 1)
|
||||
controller := newReloadController(loader, path, "", func(context.Context, *configuration.Document) error {
|
||||
applied <- struct{}{}
|
||||
return nil
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ch := make(chan os.Signal, 1)
|
||||
var wg sync.WaitGroup
|
||||
wg.Go(func() {
|
||||
reloadLoop(ctx, ch, controller, slog.New(slog.DiscardHandler))
|
||||
})
|
||||
|
||||
ch <- syscall.SIGHUP
|
||||
select {
|
||||
case <-applied:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("reload not applied after signal")
|
||||
}
|
||||
|
||||
cancel()
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestChangedNonReloadableFields(t *testing.T) {
|
||||
base := config.Config{
|
||||
BotUser: "a",
|
||||
MaxRetries: 3,
|
||||
PollInterval: config.Duration(30 * time.Second),
|
||||
Repos: []config.Repo{{Owner: "x", Name: "y"}},
|
||||
}
|
||||
|
||||
// Unchanged -> empty.
|
||||
if got := changedNonReloadableFields(base, base); len(got) != 0 {
|
||||
t.Fatalf("unchanged: got %v, want []", got)
|
||||
}
|
||||
|
||||
// Reloadable-only changes -> empty.
|
||||
reloadableOnly := base
|
||||
reloadableOnly.PollInterval = config.Duration(60 * time.Second)
|
||||
reloadableOnly.Repos = []config.Repo{{Owner: "x", Name: "z"}}
|
||||
reloadableOnly.Budgets = config.Budgets{MaxSteps: 99}
|
||||
reloadableOnly.Dispatch = config.Dispatch{Trigger: "label"}
|
||||
reloadableOnly.DiffCapLines = new(1234)
|
||||
reloadableOnly.Models = map[string]string{"builder": "provider/model"}
|
||||
reloadableOnly.Notify = config.Notify{Webhook: "https://notify.example/hook"}
|
||||
reloadableOnly.Label = "archie"
|
||||
reloadableOnly.BotUser = "new-bot"
|
||||
reloadableOnly.Forge = config.Forge{Host: "https://new-forge.example"}
|
||||
reloadableOnly.MaxRetries = 5
|
||||
if got := changedNonReloadableFields(base, reloadableOnly); len(got) != 0 {
|
||||
t.Fatalf("reloadable-only: got %v, want []", got)
|
||||
}
|
||||
|
||||
// Containers sub-field granularity: the fields the pool re-reads per
|
||||
// acquire, and the concurrency cap the dispatcher resizes on, are
|
||||
// reloadable; RegistryAuth stays frozen in the startup-built pool.
|
||||
c1 := base
|
||||
c1.Containers = config.ContainerConfig{Image: "img1", MaxConcurrency: 2, VolumeTTL: config.Duration(24 * time.Hour)}
|
||||
c2 := c1
|
||||
c2.Containers.Image = "img2"
|
||||
if got := changedNonReloadableFields(c1, c2); len(got) != 0 {
|
||||
t.Fatalf("Image change: got %v, want [] (the pool reads it per acquire)", got)
|
||||
}
|
||||
c3 := c1
|
||||
c3.Containers.MaxConcurrency = 4
|
||||
if got := changedNonReloadableFields(c1, c3); len(got) != 0 {
|
||||
t.Fatalf("MaxConcurrency change: got %v, want [] (the pool reads it and the dispatcher resizes)", got)
|
||||
}
|
||||
c4 := c1
|
||||
c4.Containers.VolumeTTL = config.Duration(48 * time.Hour)
|
||||
if got := changedNonReloadableFields(c1, c4); len(got) != 0 {
|
||||
t.Fatalf("VolumeTTL change: got %v, want []", got)
|
||||
}
|
||||
c5 := c1
|
||||
c5.Containers.RegistryAuth = config.SecretRef{Engine: "env", Key: "REGISTRY_AUTH"}
|
||||
if got := changedNonReloadableFields(c1, c5); len(got) != 1 || got[0] != "Containers.RegistryAuth" {
|
||||
t.Fatalf("RegistryAuth change: got %v, want [Containers.RegistryAuth] (the pool resolves it at boot)", got)
|
||||
}
|
||||
|
||||
// NATS is not reloadable: a URL change warns (the daemon's client is
|
||||
// startup-built; containers get the frozen ConnectedNATS).
|
||||
natsChanged := base
|
||||
natsChanged.NATS = config.NATSConfig{URL: "nats://new:4222"}
|
||||
if got := changedNonReloadableFields(base, natsChanged); len(got) != 1 || got[0] != "NATS" {
|
||||
t.Fatalf("NATS change: got %v, want [NATS]", got)
|
||||
}
|
||||
|
||||
// Forge.Host is reloadable but Forge.Type is not: only the host is
|
||||
// carried into TaskContext by ForTask.
|
||||
forgeTypeChanged := base
|
||||
forgeTypeChanged.Forge = config.Forge{Type: "gitea"}
|
||||
if got := changedNonReloadableFields(base, forgeTypeChanged); len(got) != 1 || got[0] != "Forge.Type" {
|
||||
t.Fatalf("Forge.Type change: got %v, want [Forge.Type]", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReloadableFieldsCoverForTaskSnapshot pins the allowlist against
|
||||
// the per-dispatch snapshot: every field ForTask carries into a
|
||||
// TaskContext is re-read at dispatch, so a reload applies to new tasks.
|
||||
// If a field is added to ForTask later, this test fails until the
|
||||
// allowlist is updated -- a hand-maintained list that mirrors ForTask
|
||||
// otherwise drifts silently into a wrong 'requires a restart' warning.
|
||||
func TestReloadableFieldsCoverForTaskSnapshot(t *testing.T) {
|
||||
tt := reflect.TypeFor[config.TaskConfig]()
|
||||
for field := range tt.Fields() {
|
||||
name := field.Name
|
||||
if name == "Forge" {
|
||||
// TaskConfig.Forge is TaskForge{Host}; it maps to the
|
||||
// Config.Forge.Host sub-field allowlist entry.
|
||||
if !reloadableSubFields["Forge"]["Host"] {
|
||||
t.Errorf("ForTask field Forge (Host) is not allowlisted as reloadable")
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !reloadableFields[name] {
|
||||
t.Errorf("ForTask field %s is not allowlisted as reloadable", name)
|
||||
}
|
||||
}
|
||||
// Label is not carried by ForTask; BotUser now is, but it is also read
|
||||
// on the poll path independently of the task snapshot. Pin both by hand
|
||||
// so a refactor that moves either read site -- including one that drops
|
||||
// BotUser from ForTask again -- does not silently drop the allowlist
|
||||
// entry the poll path still depends on.
|
||||
for _, name := range []string{"Label", "BotUser"} {
|
||||
if !reloadableFields[name] {
|
||||
t.Errorf("poll-path field %s is not allowlisted as reloadable", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// TestBuildDaemonCarriesWorkflowRoutingBindings pins the composition hop in the
|
||||
// cross-process routing fix: the resolved bindings loaded by loadWorkflowRouting
|
||||
// must survive buildDaemon onto the daemon that puts them into taskrun.Request.
|
||||
//
|
||||
// Without this, a nil here reverts the entire fix while every other test stays
|
||||
// green: the daemon test sets d.KindWorkflows itself, and the worker test drives
|
||||
// routeTask directly, so neither observes how the daemon obtained them. That is
|
||||
// the "tested in isolation, never verified wired" shape this repo has been bitten
|
||||
// by before.
|
||||
func TestBuildDaemonCarriesWorkflowRoutingBindings(t *testing.T) {
|
||||
storeA := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = storeA.Close() })
|
||||
storeB := openSecondStore(t)
|
||||
|
||||
b := &boot{
|
||||
cfg: config.Config{},
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
st: storeA,
|
||||
stateStore: storeB,
|
||||
kindWorkflows: workflow.KindWorkflows{workintake.KindBug: "custom-bug"},
|
||||
labelWorkflows: workflow.LabelWorkflows{"security": "security-review"},
|
||||
}
|
||||
b.buildDaemon()
|
||||
|
||||
if got := b.d.KindWorkflows[workintake.KindBug]; got != "custom-bug" {
|
||||
t.Fatalf("Daemon.KindWorkflows[bug] = %q, want %q; buildDaemon dropped the kind routing bindings, so the worker would route with built-in defaults", got, "custom-bug")
|
||||
}
|
||||
if got := b.d.LabelWorkflows["security"]; got != "security-review" {
|
||||
t.Fatalf("Daemon.LabelWorkflows[security] = %q, want %q; buildDaemon dropped the label routing bindings", got, "security-review")
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
// fakeTaskCreator records CreateTask calls so a test can assert the
|
||||
// scheduling engine's workflow runner reaches a real gateway.TaskCreator
|
||||
// rather than a no-op.
|
||||
type fakeTaskCreator struct {
|
||||
calls []gateway.SpawnRequest
|
||||
}
|
||||
|
||||
func (f *fakeTaskCreator) CreateTask(_ context.Context, req gateway.SpawnRequest) (int64, error) {
|
||||
f.calls = append(f.calls, req)
|
||||
return int64(len(f.calls)), nil
|
||||
}
|
||||
|
||||
func newTestBoot(t *testing.T, tasks gateway.TaskCreator) *boot {
|
||||
t.Helper()
|
||||
bus := events.NewBus()
|
||||
t.Cleanup(bus.Close)
|
||||
return &boot{
|
||||
log: slog.New(slog.NewTextHandler(os.Stderr, nil)),
|
||||
bus: bus,
|
||||
doc: &configuration.Document{},
|
||||
cfg: config.Config{},
|
||||
chatTasks: tasks,
|
||||
defaultChatIdentity: "primary",
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupSchedulingConstructsEngine is the red case for app-archied-2 /
|
||||
// scheduling-1 / config-2: schedulingConfig translated a valid
|
||||
// [scheduling] input but nothing at boot ever constructed the ticker
|
||||
// engine, so the whole scheduling/cron family was reachable from no
|
||||
// binary. setupScheduling is the composition site; this asserts it
|
||||
// actually produces a running-capable engine wired to a real
|
||||
// gateway.TaskCreator, not just a translated config struct.
|
||||
func TestSetupSchedulingConstructsEngine(t *testing.T) {
|
||||
creator := &fakeTaskCreator{}
|
||||
b := newTestBoot(t, creator)
|
||||
|
||||
if err := b.setupScheduling(); err != nil {
|
||||
t.Fatalf("setupScheduling() error = %v", err)
|
||||
}
|
||||
if b.schedulingEngine == nil {
|
||||
t.Fatal("setupScheduling() left b.schedulingEngine nil; the ticker engine was never constructed")
|
||||
}
|
||||
if len(b.cleanups) == 0 {
|
||||
t.Fatal("setupScheduling() registered no cleanup; engine/store would leak at shutdown")
|
||||
}
|
||||
|
||||
if err := b.schedulingEngine.Start(t.Context()); err != nil {
|
||||
t.Fatalf("schedulingEngine.Start() error = %v", err)
|
||||
}
|
||||
b.cleanup()
|
||||
}
|
||||
|
||||
// TestSetupSchedulingWorkflowRunnerReachesTaskCreator proves the "workflow"
|
||||
// kind runner registered on the router is backed by the real
|
||||
// gateway.TaskCreator this composition root already has, not a stub.
|
||||
func TestSetupSchedulingWorkflowRunnerReachesTaskCreator(t *testing.T) {
|
||||
creator := &fakeTaskCreator{}
|
||||
b := newTestBoot(t, creator)
|
||||
|
||||
if err := b.setupScheduling(); err != nil {
|
||||
t.Fatalf("setupScheduling() error = %v", err)
|
||||
}
|
||||
t.Cleanup(b.cleanup)
|
||||
|
||||
submitter := spawnTaskSubmitter{creator: b.chatTasks, identity: b.defaultChatIdentity}
|
||||
if err := submitter.Submit(context.Background(), "job-1", "title", "body"); err != nil {
|
||||
t.Fatalf("Submit() error = %v", err)
|
||||
}
|
||||
if len(creator.calls) != 1 {
|
||||
t.Fatalf("CreateTask called %d times, want 1", len(creator.calls))
|
||||
}
|
||||
if got := creator.calls[0]; got.Title != "title" || got.Body != "body" || got.Identity != "primary" {
|
||||
t.Errorf("CreateTask request = %+v, want Title=title Body=body Identity=primary", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupSchedulingWithoutTaskCreatorLeavesEngineNil documents the one
|
||||
// deliberate degrade: no chat task creator configured means no runner can
|
||||
// honestly back the "workflow" kind, so the engine is left unstarted rather
|
||||
// than built with no reachable job kind.
|
||||
func TestSetupSchedulingWithoutTaskCreatorLeavesEngineNil(t *testing.T) {
|
||||
b := newTestBoot(t, nil)
|
||||
|
||||
if err := b.setupScheduling(); err != nil {
|
||||
t.Fatalf("setupScheduling() error = %v", err)
|
||||
}
|
||||
if b.schedulingEngine != nil {
|
||||
t.Fatal("setupScheduling() built an engine with no chat task creator configured")
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
func TestSchedulingConfigTranslatesExternalInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input configuration.SchedulingInput
|
||||
wantInterval time.Duration
|
||||
wantMaxParallel int
|
||||
wantJobTimeout time.Duration
|
||||
}{
|
||||
{
|
||||
name: "defaults",
|
||||
wantInterval: time.Minute,
|
||||
wantMaxParallel: 4,
|
||||
wantJobTimeout: time.Hour,
|
||||
},
|
||||
{
|
||||
name: "configured",
|
||||
input: configuration.SchedulingInput{
|
||||
Interval: configuration.Duration(30 * time.Second),
|
||||
MaxParallel: 2,
|
||||
JobTimeout: configuration.Duration(5 * time.Minute),
|
||||
},
|
||||
wantInterval: 30 * time.Second,
|
||||
wantMaxParallel: 2,
|
||||
wantJobTimeout: 5 * time.Minute,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := schedulingConfig(tc.input, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Interval != tc.wantInterval || got.MaxParallel != tc.wantMaxParallel || got.JobTimeout != tc.wantJobTimeout {
|
||||
t.Errorf("schedulingConfig() = %+v, want interval=%v max_parallel=%d job_timeout=%v",
|
||||
got, tc.wantInterval, tc.wantMaxParallel, tc.wantJobTimeout)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedulingConfigRejectsInvalidInput(t *testing.T) {
|
||||
tests := []configuration.SchedulingInput{
|
||||
{Interval: configuration.Duration(-time.Second)},
|
||||
{MaxParallel: -1},
|
||||
{JobTimeout: configuration.Duration(-time.Second)},
|
||||
{EventsSink: "kafka"},
|
||||
}
|
||||
for _, input := range tests {
|
||||
if _, err := schedulingConfig(input, nil); !errors.Is(err, configuration.ErrInvalidInput) {
|
||||
t.Errorf("schedulingConfig(%+v) error = %v, want ErrInvalidInput", input, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedulingConfigSelectsEventSink(t *testing.T) {
|
||||
bus := events.NewBus()
|
||||
t.Cleanup(bus.Close)
|
||||
|
||||
for _, mode := range []string{"", configuration.EventsSinkNone} {
|
||||
cfg, err := schedulingConfig(configuration.SchedulingInput{EventsSink: mode}, bus)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Events != nil {
|
||||
t.Errorf("events_sink %q produced a sink", mode)
|
||||
}
|
||||
}
|
||||
cfg, err := schedulingConfig(configuration.SchedulingInput{EventsSink: configuration.EventsSinkBus}, bus)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Events == nil {
|
||||
t.Fatal("events_sink bus produced no sink")
|
||||
}
|
||||
}
|
||||
@@ -1,349 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/sdnotify"
|
||||
)
|
||||
|
||||
// The protocol-level properties of the notify client -- the '@' address form,
|
||||
// the disabled and unusable socket contract, and the half-of-WatchdogSec
|
||||
// heartbeat -- are pinned in internal/sdnotify, where the client now lives.
|
||||
// These tests pin what is the daemon's: that its three serving entry points
|
||||
// announce, that the sampler samples daemon.LastPollAt, and that the whole
|
||||
// path stays silent and harmless without a NOTIFY_SOCKET.
|
||||
|
||||
// notifyRecorder captures every record at every level. The other recording
|
||||
// handler in this package keeps warnings only; these tests need "this path
|
||||
// logged nothing at all" as positive evidence that a disabled notifier stayed
|
||||
// silent, so the level has to be part of what is captured.
|
||||
type notifyRecorder struct {
|
||||
mu sync.Mutex
|
||||
records []slog.Record
|
||||
}
|
||||
|
||||
func (h *notifyRecorder) Enabled(context.Context, slog.Level) bool { return true }
|
||||
|
||||
func (h *notifyRecorder) Handle(_ context.Context, r slog.Record) error {
|
||||
// The Record is the caller's to reuse once Handle returns, so keep a copy.
|
||||
rec := r.Clone()
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.records = append(h.records, rec)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *notifyRecorder) WithAttrs([]slog.Attr) slog.Handler { return h }
|
||||
func (h *notifyRecorder) WithGroup(string) slog.Handler { return h }
|
||||
|
||||
func (h *notifyRecorder) all() []slog.Record {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return append([]slog.Record(nil), h.records...)
|
||||
}
|
||||
|
||||
// at returns the captured records at one level.
|
||||
func (h *notifyRecorder) at(level slog.Level) []slog.Record {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
var out []slog.Record
|
||||
for _, r := range h.records {
|
||||
if r.Level == level {
|
||||
out = append(out, r)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// notifyListener is a stand-in for systemd's NOTIFY_SOCKET: a unixgram socket
|
||||
// that collects the state lines the daemon sends. It is bound to the real
|
||||
// NOTIFY_SOCKET for the duration of the test.
|
||||
type notifyListener struct {
|
||||
conn *net.UnixConn
|
||||
}
|
||||
|
||||
func newNotifyListener(t *testing.T) *notifyListener {
|
||||
t.Helper()
|
||||
// A unix socket path is capped near 108 bytes; the short file name keeps the
|
||||
// path inside t.TempDir() under that for any test name.
|
||||
return listenNotify(t, filepath.Join(t.TempDir(), "n.sock"))
|
||||
}
|
||||
|
||||
// listenNotify binds a notify socket at addr and points NOTIFY_SOCKET at it.
|
||||
func listenNotify(t *testing.T, addr string) *notifyListener {
|
||||
t.Helper()
|
||||
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: addr, Net: "unixgram"})
|
||||
if err != nil {
|
||||
t.Fatalf("listen for notify datagrams at %q: %v", addr, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = conn.Close() })
|
||||
t.Setenv(sdnotify.NotifySocketEnv, addr)
|
||||
return ¬ifyListener{conn: conn}
|
||||
}
|
||||
|
||||
// states drains every state line received within window.
|
||||
func (l *notifyListener) states(t *testing.T, window time.Duration) []string {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(window)
|
||||
var got []string
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
if err := l.conn.SetReadDeadline(deadline); err != nil {
|
||||
t.Fatalf("set notify read deadline: %v", err)
|
||||
}
|
||||
n, _, err := l.conn.ReadFromUnix(buf)
|
||||
if err != nil {
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||
return got
|
||||
}
|
||||
t.Fatalf("read notify datagram: %v", err)
|
||||
}
|
||||
got = append(got, string(buf[:n]))
|
||||
}
|
||||
}
|
||||
|
||||
// countState counts the datagrams carrying want among the received states.
|
||||
func countState(states []string, want string) int {
|
||||
n := 0
|
||||
for _, state := range states {
|
||||
if state == want {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// waitForState reads datagrams until one carries want or the window closes.
|
||||
// The states drain above collects a window's worth; a boot-path test waiting
|
||||
// for one state returns as soon as it arrives.
|
||||
func (l *notifyListener) waitForState(t *testing.T, window time.Duration, want string) bool {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(window)
|
||||
buf := make([]byte, 1024)
|
||||
for time.Now().Before(deadline) {
|
||||
if err := l.conn.SetReadDeadline(time.Now().Add(50 * time.Millisecond)); err != nil {
|
||||
t.Fatalf("set notify read deadline: %v", err)
|
||||
}
|
||||
n, _, err := l.conn.ReadFromUnix(buf)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if string(buf[:n]) == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// newNotifyTestBoot builds the composition the notify path reads: a boot whose
|
||||
// config holder supplies the run loop's poll cadence, and a real daemon whose
|
||||
// own poll loop stamps the progress marker the heartbeat sampler samples.
|
||||
func newNotifyTestBoot(t *testing.T, cadence time.Duration) (*boot, *daemon.Daemon) {
|
||||
t.Helper()
|
||||
cfg := config.Config{PollInterval: config.Duration(cadence)}
|
||||
holder := config.NewHolder(cfg)
|
||||
st := pgstore.Open(t)
|
||||
log := slog.New(slog.DiscardHandler)
|
||||
d := &daemon.Daemon{Cfg: holder, Store: st, Log: log}
|
||||
return &boot{cfg: cfg, cfgHolder: holder, d: d, log: log}, d
|
||||
}
|
||||
|
||||
// pollPasses drives the daemon's poll passes the way Run does, one Cycle per
|
||||
// tick, while on is set. The progress marker is stamped by the daemon inside
|
||||
// Cycle, never by the test: what the sampler samples is the loop's own
|
||||
// evidence, not a value the test wrote.
|
||||
func pollPasses(ctx context.Context, d *daemon.Daemon, on *atomic.Bool, every time.Duration) {
|
||||
ticker := time.NewTicker(every)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if on.Load() {
|
||||
d.Cycle(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotifyReadyAndWatchdogHeartbeat is the run-loop path end to end: a real
|
||||
// NOTIFY_SOCKET, the daemon's own boot path announcing READY, and the run
|
||||
// loop's own progress marker producing WATCHDOG heartbeats.
|
||||
func TestNotifyReadyAndWatchdogHeartbeat(t *testing.T) {
|
||||
listener := newNotifyListener(t)
|
||||
t.Setenv(sdnotify.WatchdogUsecEnv, "20000") // 20ms watchdog: heartbeat every 10ms
|
||||
|
||||
b, d := newNotifyTestBoot(t, 50*time.Millisecond)
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
|
||||
var polling atomic.Bool
|
||||
polling.Store(true)
|
||||
go pollPasses(ctx, d, &polling, 5*time.Millisecond)
|
||||
|
||||
b.announceReady()
|
||||
b.startWatchdog(ctx)
|
||||
|
||||
states := listener.states(t, 300*time.Millisecond)
|
||||
if !slices.Contains(states, sdnotify.ReadyState) {
|
||||
t.Errorf("states received = %v, want %q among them", states, sdnotify.ReadyState)
|
||||
}
|
||||
if countState(states, sdnotify.WatchdogState) == 0 {
|
||||
t.Errorf("states received = %v, want at least one %q among them", states, sdnotify.WatchdogState)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWatchdogWithholdsHeartbeatWhenRunLoopStalls is the semantic that makes
|
||||
// the heartbeat worth having: the beats stop once the loop stops making
|
||||
// progress, so a wedged event loop trips systemd's watchdog instead of being
|
||||
// kept alive by an independent ticker.
|
||||
func TestWatchdogWithholdsHeartbeatWhenRunLoopStalls(t *testing.T) {
|
||||
listener := newNotifyListener(t)
|
||||
t.Setenv(sdnotify.WatchdogUsecEnv, "20000") // 20ms watchdog: heartbeat every 10ms
|
||||
|
||||
const cadence = 50 * time.Millisecond
|
||||
b, d := newNotifyTestBoot(t, cadence)
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
|
||||
var polling atomic.Bool
|
||||
polling.Store(true)
|
||||
go pollPasses(ctx, d, &polling, 5*time.Millisecond)
|
||||
|
||||
b.startWatchdog(ctx)
|
||||
if beats := countState(listener.states(t, 200*time.Millisecond), sdnotify.WatchdogState); beats == 0 {
|
||||
t.Fatal("no WATCHDOG=1 within 200ms while the run loop was polling")
|
||||
}
|
||||
|
||||
// From here the loop is inside a pass that never returns: nothing stamps
|
||||
// the progress marker again.
|
||||
polling.Store(false)
|
||||
|
||||
// Discard the beats the last stamp still licenses. The marker counts as
|
||||
// stale only once it is older than the loop's own cadence (50ms), and
|
||||
// samples are 10ms apart, so 150ms covers every remaining beat with margin.
|
||||
listener.states(t, 150*time.Millisecond)
|
||||
|
||||
if late := listener.states(t, 300*time.Millisecond); len(late) != 0 {
|
||||
t.Errorf("received %v after the run loop stalled, want no heartbeats", late)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotifyUnsetIsNoOp pins the deployment contract: with no NOTIFY_SOCKET the
|
||||
// process behaves exactly as it did before sd_notify existed. Docker, a plain
|
||||
// terminal, and a systemd unit with no WatchdogSec all take this path, and none
|
||||
// of them may see a datagram, a warning, or a failed start.
|
||||
func TestNotifyUnsetIsNoOp(t *testing.T) {
|
||||
t.Setenv(sdnotify.WatchdogUsecEnv, "20000")
|
||||
// t.Setenv cannot unset a variable: register the restoration first, then
|
||||
// remove it for real so "unset" means unset.
|
||||
t.Setenv(sdnotify.NotifySocketEnv, "")
|
||||
if err := os.Unsetenv(sdnotify.NotifySocketEnv); err != nil {
|
||||
t.Fatalf("unset %s: %v", sdnotify.NotifySocketEnv, err)
|
||||
}
|
||||
|
||||
rec := ¬ifyRecorder{}
|
||||
b, _ := newNotifyTestBoot(t, 50*time.Millisecond)
|
||||
b.log = slog.New(rec)
|
||||
|
||||
b.announceReady()
|
||||
b.startWatchdog(t.Context())
|
||||
|
||||
if got := rec.all(); len(got) != 0 {
|
||||
t.Errorf("with NOTIFY_SOCKET unset the notify path logged %d records, want none: %v", len(got), got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotifySocketFailureIsNotFatal pins the other half of the deployment
|
||||
// contract: a NOTIFY_SOCKET that cannot be written to is logged and ignored.
|
||||
// A daemon that refused to serve because systemd's socket was unavailable
|
||||
// would be a regression on every deployment that has no systemd at all.
|
||||
func TestNotifySocketFailureIsNotFatal(t *testing.T) {
|
||||
missing := filepath.Join(t.TempDir(), "absent", "n.sock")
|
||||
t.Setenv(sdnotify.NotifySocketEnv, missing)
|
||||
t.Setenv(sdnotify.WatchdogUsecEnv, "20000")
|
||||
|
||||
rec := ¬ifyRecorder{}
|
||||
b, d := newNotifyTestBoot(t, 50*time.Millisecond)
|
||||
b.log = slog.New(rec)
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
|
||||
var polling atomic.Bool
|
||||
polling.Store(true)
|
||||
go pollPasses(ctx, d, &polling, 5*time.Millisecond)
|
||||
|
||||
b.announceReady()
|
||||
b.startWatchdog(ctx)
|
||||
|
||||
if warns := rec.at(slog.LevelWarn); len(warns) != 1 {
|
||||
t.Errorf("announceReady against an unusable socket logged %d warnings, want exactly 1", len(warns))
|
||||
}
|
||||
|
||||
// The sampler keeps trying rather than dying on the first failure, so more
|
||||
// warnings arrive while the loop progresses.
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if len(rec.at(slog.LevelWarn)) > 1 {
|
||||
break
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
if warns := rec.at(slog.LevelWarn); len(warns) < 2 {
|
||||
t.Errorf("the watchdog logged %d warnings, want the heartbeat still being attempted", len(warns))
|
||||
}
|
||||
if errs := rec.at(slog.LevelError); len(errs) != 0 {
|
||||
t.Errorf("an unusable notify socket logged %d errors, want none: %v", len(errs), errs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreBootAnnouncesReady: archie-state-store run as a Type=notify
|
||||
// unit must send READY=1 once its boot is over and it is serving the contract,
|
||||
// or systemd kills a healthy process once TimeoutStartSec expires
|
||||
// (archie-core-1174). The real RunStateStore composition drives it, over a
|
||||
// real State Store database, the way the provider-seeding boot test does.
|
||||
func TestStateStoreBootAnnouncesReady(t *testing.T) {
|
||||
listener := newNotifyListener(t)
|
||||
url := pgtest.URL(t)
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
body := fmt.Sprintf("bot_user = 'archie'\ndatabase_url = %q\n", url)
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "127.0.0.1:0"}) }()
|
||||
|
||||
if !listener.waitForState(t, 15*time.Second, sdnotify.ReadyState) {
|
||||
select {
|
||||
case bootErr := <-errCh:
|
||||
t.Fatalf("RunStateStore exited before announcing ready: %v", bootErr)
|
||||
default:
|
||||
t.Fatal("no READY=1 datagram within 15s of boot")
|
||||
}
|
||||
}
|
||||
|
||||
cancel()
|
||||
if err := <-errCh; err != nil && !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("RunStateStore shutdown: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,284 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/setup"
|
||||
)
|
||||
|
||||
func TestSetupSubcommandIsRecognised(t *testing.T) {
|
||||
if !IsSetupArgs([]string{setupCommand, "--defaults"}) {
|
||||
t.Errorf("IsSetupArgs() did not recognise %q", setupCommand)
|
||||
}
|
||||
if IsSetupArgs([]string{"-once"}) {
|
||||
t.Error("IsSetupArgs() claimed a normal daemon flag")
|
||||
}
|
||||
if IsSetupArgs(nil) {
|
||||
t.Error("IsSetupArgs() claimed an empty argument list")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSetup_DefaultsWritesLoadableConfig(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.toml")
|
||||
|
||||
stdin, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer stdin.Close()
|
||||
|
||||
var stdout, stderr strings.Builder
|
||||
code := RunSetup([]string{"--defaults", "-config", cfgPath}, stdin, &stdout, &stderr)
|
||||
if code != 0 {
|
||||
t.Fatalf("RunSetup exit = %d, stderr:\n%s", code, stderr.String())
|
||||
}
|
||||
|
||||
info, err := os.Stat(cfgPath)
|
||||
if err != nil {
|
||||
t.Fatalf("config not written: %v", err)
|
||||
}
|
||||
if info.Mode().Perm() != 0o600 {
|
||||
t.Errorf("config mode = %v, want 0600", info.Mode().Perm())
|
||||
}
|
||||
|
||||
doc, err := configuration.New(nil).File(cfgPath)
|
||||
if err != nil {
|
||||
t.Fatalf("generated config does not load: %v", err)
|
||||
}
|
||||
if doc.Config.BotUser != "archie-bot" {
|
||||
t.Errorf("bot_user = %q, want archie-bot", doc.Config.BotUser)
|
||||
}
|
||||
|
||||
// The defaults baseline is keyless (ollama), so nothing may be written
|
||||
// to the env file.
|
||||
if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) {
|
||||
t.Errorf("env file should not exist after a defaults run, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunSetupRejectsSecretValueFlags pins that no secret value can be passed
|
||||
// on the command line. Secrets are set in a secret engine; this flow only ever
|
||||
// writes a reference to one. A flag that carried a value would be a second,
|
||||
// silent way to set a secret -- and would put it in shell history and in every
|
||||
// process listing, which is why install.sh prompts with read_secret instead.
|
||||
func TestRunSetupRejectsSecretValueFlags(t *testing.T) {
|
||||
for _, flagName := range []string{"-forge-token", "-provider-api-key", "-telegram-token"} {
|
||||
t.Run(flagName, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.toml")
|
||||
devNull, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = devNull.Close() }()
|
||||
|
||||
var stdout, stderr strings.Builder
|
||||
code := RunSetup([]string{"--defaults", "-config", cfgPath, flagName, "leaked-value"}, devNull, &stdout, &stderr)
|
||||
if code != 2 {
|
||||
t.Fatalf("RunSetup with %s = exit %d, want 2 (unknown flag); stderr: %s", flagName, code, stderr.String())
|
||||
}
|
||||
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
|
||||
t.Errorf("config written despite a rejected flag: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) {
|
||||
t.Errorf("env file written despite a rejected flag: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSetup_GuidedRequiresTTY(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.toml")
|
||||
|
||||
stdin, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer stdin.Close()
|
||||
|
||||
var stdout, stderr strings.Builder
|
||||
code := RunSetup([]string{"-config", cfgPath}, stdin, &stdout, &stderr)
|
||||
if code == 0 {
|
||||
t.Fatalf("RunSetup exit = 0, want non-zero for a guided run without a TTY")
|
||||
}
|
||||
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
|
||||
t.Errorf("config should not be written when a guided non-TTY run is refused, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallValidatedConfig_ValidationFailureWritesNothing(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.toml")
|
||||
sink := setup.NewEnvFileSink(filepath.Join(dir, "env"))
|
||||
if err := sink.Put("env", "ARCHIE_GITHUB_TOKEN", "secret"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err := installValidatedConfig(configuration.New(nil), cfgPath, sink, []byte("this is not toml"))
|
||||
if err == nil {
|
||||
t.Fatal("installValidatedConfig = nil error, want a load failure")
|
||||
}
|
||||
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
|
||||
t.Errorf("config written despite validation failure: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) {
|
||||
t.Errorf("secret written despite validation failure: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret pins the write
|
||||
// order the setup package's SecretSink contract depends on: the config is
|
||||
// installed before any secret is committed, so a secret is never left on disk
|
||||
// pointing at a config that was never written.
|
||||
//
|
||||
// This is not covered by the validation-failure test: inverting the two writes
|
||||
// leaves every other test in the package green, so this assertion is what
|
||||
// actually holds the invariant.
|
||||
func TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// The parent directory does not exist, so validation succeeds and the
|
||||
// config write fails.
|
||||
cfgPath := filepath.Join(dir, "missing", "config.toml")
|
||||
envPath := filepath.Join(dir, "env")
|
||||
sink := setup.NewEnvFileSink(envPath)
|
||||
if err := sink.Put("env", "ARCHIE_GITHUB_TOKEN", "secret"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rendered := []byte("bot_user = \"widget\"\nwork_dir = \"/tmp/archie-setup-work\"\n")
|
||||
|
||||
// Prove the fixture is loadable BEFORE relying on the write failing.
|
||||
// Asserted by loading it rather than by matching the error text below: a
|
||||
// reworded error must not silently disarm this guard and let the test pass
|
||||
// for the wrong reason.
|
||||
fixture := filepath.Join(dir, "fixture.toml")
|
||||
if err := os.WriteFile(fixture, rendered, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := configuration.New(nil).File(fixture); err != nil {
|
||||
t.Fatalf("fixture config does not load, so this test would not exercise the write path: %v", err)
|
||||
}
|
||||
|
||||
if err := installValidatedConfig(configuration.New(nil), cfgPath, sink, rendered); err == nil {
|
||||
t.Fatal("installValidatedConfig = nil error, want a write failure")
|
||||
}
|
||||
if _, statErr := os.Stat(envPath); !os.IsNotExist(statErr) {
|
||||
t.Errorf("secret committed even though the config was never written: %v", statErr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunSetup_SecretRefFlagsWriteReferencesWithoutEnvFile pins the reference
|
||||
// form end to end: naming where a secret is stored writes the reference, and
|
||||
// setup stores nothing, so no env file appears. The engine here (bws) is one
|
||||
// setup has no writer for -- which is the point, the value is not setup's.
|
||||
func TestRunSetup_SecretRefFlagsWriteReferencesWithoutEnvFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.toml")
|
||||
devNull, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = devNull.Close() }()
|
||||
|
||||
var stdout, stderr strings.Builder
|
||||
code := RunSetup([]string{
|
||||
"--defaults", "-config", cfgPath,
|
||||
"-provider", "openai", "-model", "gpt-5.4",
|
||||
"-provider-secret-ref", "bws:OPENAI_API_KEY",
|
||||
"-forge-secret-ref", "bws:ARCHIE_GITHUB_TOKEN",
|
||||
}, devNull, &stdout, &stderr)
|
||||
if code != 0 {
|
||||
t.Fatalf("RunSetup exit = %d, stderr: %s", code, stderr.String())
|
||||
}
|
||||
|
||||
doc, err := configuration.New(nil).File(cfgPath)
|
||||
if err != nil {
|
||||
t.Fatalf("generated config does not load: %v", err)
|
||||
}
|
||||
if got, want := doc.Config.Providers["openai"].APIKey, (config.SecretRef{Engine: "bws", Key: "OPENAI_API_KEY"}); got != want {
|
||||
t.Errorf("providers.openai.api_key = %+v, want %+v", got, want)
|
||||
}
|
||||
if got, want := doc.Config.Forge.Token, (config.SecretRef{Engine: "bws", Key: "ARCHIE_GITHUB_TOKEN"}); got != want {
|
||||
t.Errorf("forge.token = %+v, want %+v", got, want)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) {
|
||||
t.Errorf("env file written: a reference means setup stores no value (stat err: %v)", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunSetup_RejectsReferencesNothingWouldUse pins the no-silent-no-op rule.
|
||||
// A reference for a keyless provider, or for a disabled forge, would sit in the
|
||||
// config looking configured while nothing read it -- the failure shape this
|
||||
// whole flow exists to remove -- so it is refused instead.
|
||||
func TestRunSetup_RejectsReferencesNothingWouldUse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
}{
|
||||
{name: "provider ref with the keyless self-hosted provider", args: []string{"-provider-secret-ref", "bws:OPENAI_API_KEY"}},
|
||||
{name: "forge ref with the forge disabled", args: []string{"-forge-type", "none", "-forge-secret-ref", "bws:ARCHIE_GITHUB_TOKEN"}},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.toml")
|
||||
devNull, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = devNull.Close() }()
|
||||
|
||||
args := append([]string{"--defaults", "-config", cfgPath}, tc.args...)
|
||||
var stdout, stderr strings.Builder
|
||||
if code := RunSetup(args, devNull, &stdout, &stderr); code == 0 {
|
||||
t.Fatalf("RunSetup = exit 0, want a refusal; stderr: %s", stderr.String())
|
||||
}
|
||||
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
|
||||
t.Errorf("config written despite the refusal: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements pins the properties
|
||||
// archied needs of whatever setup emits, asserted on the GENERATED config.
|
||||
//
|
||||
// It replaces a test that read config.example.toml and reconstructed install.sh's
|
||||
// shell heredoc. Testing hand-written files is testing an artifact a human or an
|
||||
// agent may legitimately rewrite at any time -- and that test in fact broke the
|
||||
// moment install.sh stopped generating config, because the heredoc it parsed no
|
||||
// longer existed. The generated output is the contract; the template is an input.
|
||||
func TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.toml")
|
||||
devNull, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = devNull.Close() }()
|
||||
|
||||
var stdout, stderr strings.Builder
|
||||
if code := RunSetup([]string{"--defaults", "-config", cfgPath}, devNull, &stdout, &stderr); code != 0 {
|
||||
t.Fatalf("RunSetup exit = %d, stderr: %s", code, stderr.String())
|
||||
}
|
||||
|
||||
doc, err := configuration.New(nil).File(cfgPath)
|
||||
if err != nil {
|
||||
t.Fatalf("generated config does not load: %v", err)
|
||||
}
|
||||
// openStateStoreAdapter refuses an empty target, so this is the difference
|
||||
// between a config that boots and one that does not.
|
||||
if doc.Config.Services.Get(config.ServiceNameState).Target == "" {
|
||||
t.Error("generated config leaves services.state.target empty; the daemon would not boot")
|
||||
}
|
||||
if doc.Config.BotUser == "" {
|
||||
t.Error("generated config leaves bot_user empty")
|
||||
}
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// testWatchdogLeash is deliberately short so the watchdog tests are fast and
|
||||
// deterministic. The real production leash is drain timeout plus grace
|
||||
// period -- far longer than anything a unit test should wait for.
|
||||
const testWatchdogLeash = 30 * time.Millisecond
|
||||
|
||||
// testWatchdogExitTimeout bounds how long a test waits for the watchdog's
|
||||
// forced exit to be observed. It is deliberately generous: the leash only
|
||||
// starts the timer, and the firing goroutine must then be scheduled onto a
|
||||
// core before the injected exit is observable. A deadline of a few leash
|
||||
// lengths is a wall-clock race against the Go scheduler, and it fails
|
||||
// intermittently on a loaded machine -- the test then parks real work behind a
|
||||
// gate failure that has nothing to do with the change under test. The test
|
||||
// still completes in ~30ms; this is a ceiling, not a sleep.
|
||||
const testWatchdogExitTimeout = 5 * time.Second
|
||||
|
||||
// testWatchdogStandDownPeriod is the shorter wait used where the test asserts
|
||||
// the ABSENCE of a forced exit. Load cannot make that assertion lie: a slow
|
||||
// runner only delays the very exit the test is checking for, so a tight bound
|
||||
// is safe in this direction and keeps the suite fast. It still leaves the
|
||||
// leash many times over for a broken disarm to reveal itself, which is the
|
||||
// regression this half of the pair exists to catch.
|
||||
const testWatchdogStandDownPeriod = testWatchdogLeash * 10
|
||||
|
||||
func discardWatchdogLogger() *slog.Logger {
|
||||
return slog.New(slog.DiscardHandler)
|
||||
}
|
||||
|
||||
// TestShutdownWatchdogHangingPastLeashForcesExit proves the backstop: once
|
||||
// graceful shutdown begins and never completes (the shutdown machinery is
|
||||
// hung, so the disarm is never called), the watchdog force-exits the process
|
||||
// after the leash expires.
|
||||
//
|
||||
// The timing contract asserted here is the one that actually matters and the
|
||||
// one scheduling jitter cannot falsify: the exit must NOT happen before the
|
||||
// leash, and it must happen. Waiting for it with a generous ceiling keeps the
|
||||
// second half honest under load; asserting elapsed >= leash keeps the first
|
||||
// half strict, because delaying the goroutine only ever makes it later, never
|
||||
// earlier.
|
||||
func TestShutdownWatchdogHangingPastLeashForcesExit(t *testing.T) {
|
||||
exited := make(chan int, 1)
|
||||
w := newShutdownWatchdog(testWatchdogLeash, discardWatchdogLogger())
|
||||
w.exit = func(code int) { exited <- code }
|
||||
|
||||
began := make(chan struct{})
|
||||
_ = w.watch(began)
|
||||
|
||||
// Graceful shutdown begins and never completes: no disarm is called.
|
||||
start := time.Now()
|
||||
close(began)
|
||||
|
||||
select {
|
||||
case code := <-exited:
|
||||
if code != 1 {
|
||||
t.Fatalf("watchdog force-exited with code %d, want 1", code)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed < testWatchdogLeash {
|
||||
t.Fatalf("watchdog force-exited after %s, before the %s leash had expired", elapsed, testWatchdogLeash)
|
||||
}
|
||||
case <-time.After(testWatchdogExitTimeout):
|
||||
t.Fatalf("watchdog did not force-exit within %s (leash %s)", testWatchdogExitTimeout, testWatchdogLeash)
|
||||
}
|
||||
}
|
||||
|
||||
// TestShutdownWatchdogCompletingWithinLeashDoesNotExit proves the inverse:
|
||||
// a shutdown that completes within the leash (the disarm is called before the
|
||||
// leash expires) does not trigger the watchdog.
|
||||
//
|
||||
// The disarm closes the watchdog's done channel, so the goroutine stands down
|
||||
// and no exit can follow. This asserts the ABSENCE of a forced exit, which is
|
||||
// the direction load cannot falsify -- a slow runner only delays the exit
|
||||
// being checked for -- so a tight bound is both safe and honest here.
|
||||
func TestShutdownWatchdogCompletingWithinLeashDoesNotExit(t *testing.T) {
|
||||
exited := make(chan int, 1)
|
||||
w := newShutdownWatchdog(testWatchdogLeash, discardWatchdogLogger())
|
||||
w.exit = func(code int) { exited <- code }
|
||||
|
||||
began := make(chan struct{})
|
||||
disarm := w.watch(began)
|
||||
|
||||
// Shutdown begins and completes immediately, well within the leash.
|
||||
close(began)
|
||||
disarm()
|
||||
|
||||
select {
|
||||
case code := <-exited:
|
||||
t.Fatalf("watchdog force-exited with code %d despite completing within leash", code)
|
||||
case <-time.After(testWatchdogStandDownPeriod):
|
||||
// Correct: a shutdown that completed within the leash did not
|
||||
// trigger the forced exit.
|
||||
}
|
||||
}
|
||||
|
||||
// TestBootStartShutdownWatchdogDisarmRunsLast pins the wiring invariant that
|
||||
// makes the backstop sound: the disarm the watchdog registers must be the
|
||||
// FIRST cleanup so it runs LAST in the LIFO shutdown chain. If it were
|
||||
// registered after a subsystem's cleanup, a hung subsystem would let the
|
||||
// disarm run early and the watchdog would stand down before the backstop was
|
||||
// needed.
|
||||
func TestBootStartShutdownWatchdogDisarmRunsLast(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
ctx := t.Context()
|
||||
b.startShutdownWatchdog(ctx)
|
||||
|
||||
if len(b.cleanups) != 1 {
|
||||
t.Fatalf("startShutdownWatchdog registered %d cleanups, want exactly the disarm", len(b.cleanups))
|
||||
}
|
||||
|
||||
// A later subsystem's shutdown cleanup is appended after the disarm, so in
|
||||
// the LIFO chain it runs BEFORE the disarm.
|
||||
var ran []string
|
||||
b.addCleanup(func() { ran = append(ran, "subsystem") })
|
||||
b.cleanup()
|
||||
|
||||
if len(ran) != 1 || ran[0] != "subsystem" {
|
||||
t.Fatalf("cleanup order = %v, want the disarm to run after the subsystem cleanup", ran)
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
func TestComposeStateStoreClientRequiresTarget(t *testing.T) {
|
||||
_, _, err := composeStateStoreClient(config.Services{}, &secret.Registry{})
|
||||
if err == nil {
|
||||
t.Fatal("empty target should error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "services.state.target is required") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeStateStoreClientNonLoopbackFailsClosed(t *testing.T) {
|
||||
// Keep this test independent of credentials inherited from the test runner.
|
||||
t.Setenv("STATE_STORE_TOKEN", "")
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
target string
|
||||
}{
|
||||
{name: "wildcard without token", target: "0.0.0.0:9090"},
|
||||
{name: "hostname without token", target: "store.example.com:9090"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, _, err := composeStateStoreClient(config.Services{config.ServiceNameState: {Target: tt.target}}, &secret.Registry{})
|
||||
if err == nil {
|
||||
t.Fatalf("non-loopback %q without a token should fail closed (docs/prds/state-store-contract.md §9)", tt.target)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeStateStoreClientLoopbackSucceeds(t *testing.T) {
|
||||
st, cleanup, err := composeStateStoreClient(config.Services{config.ServiceNameState: {Target: "127.0.0.1:9090"}}, &secret.Registry{})
|
||||
if err != nil {
|
||||
t.Fatalf("loopback without token should succeed: %v", err)
|
||||
}
|
||||
if st == nil {
|
||||
t.Fatal("expected a non-nil store adapter")
|
||||
}
|
||||
if cleanup == nil {
|
||||
t.Fatal("expected a cleanup func")
|
||||
}
|
||||
cleanup() // must not panic or fail; Close() on the adapter is a no-op (§11)
|
||||
}
|
||||
|
||||
func TestComposeStateStoreClientTokenFromTargetToken(t *testing.T) {
|
||||
// Non-loopback with a configured token must not fail closed; the interceptor
|
||||
// is what carries it, so the client is dialable.
|
||||
st, _, err := composeStateStoreClient(config.Services{config.ServiceNameState: {Target: "0.0.0.0:9090", TargetToken: "secret"}}, &secret.Registry{})
|
||||
if err != nil {
|
||||
t.Fatalf("non-loopback with a token should succeed: %v", err)
|
||||
}
|
||||
if st == nil {
|
||||
t.Fatal("expected a non-nil store adapter")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenStateStoreAdapterRequiresTarget(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
b.secrets = &secret.Registry{}
|
||||
// After .4.6 the daemon and gateway no longer own archie.db in-process, so
|
||||
// an empty [services.state].target is a composition error, not the old
|
||||
// local task-store default (docs/prds/state-store-contract.md §12 step 7).
|
||||
if err := b.openStateStoreAdapter(); err == nil {
|
||||
t.Fatal("empty services.state.target should error: the in-process store path is deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenStateStoreAdapterRemoteFailsClosed(t *testing.T) {
|
||||
// Keep this test independent of credentials inherited from the test runner.
|
||||
t.Setenv("STATE_STORE_TOKEN", "")
|
||||
b := newBootstrap()
|
||||
b.secrets = &secret.Registry{}
|
||||
b.cfg.Services = config.Services{config.ServiceNameState: {Target: "0.0.0.0:9090"}}
|
||||
if err := b.openStateStoreAdapter(); err == nil {
|
||||
t.Fatal("non-loopback without a token should fail closed")
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreSingleOwnerComposition proves the single-owner SQLite
|
||||
// invariant at the composition root: both production consumers (the daemon
|
||||
// Run path and the gateway RunGateway) resolve their task-store surface from
|
||||
// openStateStoreAdapter, which REQUIRES [services.state].target and never
|
||||
// opens archie.db directly. After .4.6 the in-process store path is deleted
|
||||
// (docs/prds/state-store-contract.md §12 step 7), so an empty target is a
|
||||
// startup error rather than the old local default -- if it were not, the
|
||||
// daemon or gateway would open the single SQLite file behind the store
|
||||
// service's back, violating no-dual-store-ownership.
|
||||
func TestStateStoreSingleOwnerComposition(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
b.secrets = &secret.Registry{}
|
||||
|
||||
// The daemon openStores path must not construct a local task store.
|
||||
// openStores only resolves secrets and the forge client, then opens chat
|
||||
// sessions; the task store surface comes from the remote adapter, so no
|
||||
// archie.db task file is touched. This asserts openStateStoreAdapter (the
|
||||
// only task-store constructor) rejects an empty target.
|
||||
if err := b.openStateStoreAdapter(); err == nil {
|
||||
t.Fatal("empty services.state.target must error: the in-process store path is deleted (single owner = archie-state-store)")
|
||||
}
|
||||
if b.stateStore != nil {
|
||||
t.Fatalf("stateStore = %p, want nil (no local store opened)", b.stateStore)
|
||||
}
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
taskactionstore "github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
)
|
||||
|
||||
// openSecondStore opens a second, independent *pgstore.TaskDB on a fresh temp
|
||||
// path so a boot consumer test can prove the composition routes through
|
||||
// b.stateStore rather than b.st: the two differ by identity, so an assertion
|
||||
// that a consumer field holds storeB (not storeA) can only pass if the wiring
|
||||
// reads b.stateStore.
|
||||
// stateStoreAdapter stands in for the real adapter (*staterpc.Client), which
|
||||
// fronts both halves of the contract over one connection: the task store and
|
||||
// the event-capture store. A plain *pgstore.TaskDB no longer satisfies the
|
||||
// event-capture surfaces, so a test that asserts "everything routes through
|
||||
// the adapter" needs something that implements them, exactly as the client
|
||||
// does in production.
|
||||
type stateStoreAdapter struct {
|
||||
*pgstore.TaskDB
|
||||
*postgres.EDA
|
||||
}
|
||||
|
||||
func openSecondStore(t *testing.T) *stateStoreAdapter {
|
||||
t.Helper()
|
||||
st := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
return &stateStoreAdapter{TaskDB: st, EDA: pgstore.EDA(t, nil)}
|
||||
}
|
||||
|
||||
// TestBuildDaemonRoutesTaskStoreThroughStateStore proves the daemon's task
|
||||
// lifecycle consumer resolves its store surfaces from b.stateStore (the State
|
||||
// Store contract adapter -- always the remote *staterpc.Client after .4.6)
|
||||
// and not from b.st directly. This is the .4.5 TaskStore-composite swap
|
||||
// (docs/prds/state-store-contract.md §12 step 6): the daemon's Store field and
|
||||
// its mapping/binding dispatch surfaces all come from the adapter.
|
||||
func TestBuildDaemonRoutesTaskStoreThroughStateStore(t *testing.T) {
|
||||
storeA := pgstore.Open(t)
|
||||
t.Cleanup(func() { _ = storeA.Close() })
|
||||
storeB := openSecondStore(t)
|
||||
|
||||
b := &boot{
|
||||
cfg: config.Config{},
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
st: storeA,
|
||||
stateStore: storeB,
|
||||
}
|
||||
b.buildDaemon()
|
||||
|
||||
if b.d.Store != storeB {
|
||||
t.Fatalf("daemon Store = %p, want b.stateStore (%p); task lifecycle did not route through the State Store adapter", b.d.Store, storeB)
|
||||
}
|
||||
if b.d.Mappings != storeB {
|
||||
t.Fatalf("daemon Mappings = %p, want b.stateStore (%p)", b.d.Mappings, storeB)
|
||||
}
|
||||
if b.d.Bindings != storeB {
|
||||
t.Fatalf("daemon Bindings = %p, want b.stateStore (%p)", b.d.Bindings, storeB)
|
||||
}
|
||||
if b.d.BindingDispatcher != storeB {
|
||||
t.Fatalf("daemon BindingDispatcher = %p, want b.stateStore (%p)", b.d.BindingDispatcher, storeB)
|
||||
}
|
||||
if b.d.BindingTaskCreator != storeB {
|
||||
t.Fatalf("daemon BindingTaskCreator = %p, want b.stateStore (%p)", b.d.BindingTaskCreator, storeB)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskActionsRoutesThroughStateStore proves the daemon's operator task
|
||||
// action service wraps b.stateStore (the contract adapter), so an operator
|
||||
// approve/cancel/archive action mutates the same store the daemon lifecycle
|
||||
// uses.
|
||||
func TestTaskActionsRoutesThroughStateStore(t *testing.T) {
|
||||
storeB := openSecondStore(t)
|
||||
|
||||
b := &boot{
|
||||
cfg: config.Config{},
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
stateStore: storeB,
|
||||
}
|
||||
svc := b.taskActions()
|
||||
shadow, ok := svc.Store.(taskactionstore.Store)
|
||||
if !ok {
|
||||
t.Fatalf("taskactions Service.Store = %T, want taskactionstore.Store", svc.Store)
|
||||
}
|
||||
if shadow.TaskStore != storeB {
|
||||
t.Fatalf("taskactions TaskStore = %p, want b.stateStore (%p)", shadow.TaskStore, storeB)
|
||||
}
|
||||
}
|
||||
@@ -1,243 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/test/bufconn"
|
||||
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
)
|
||||
|
||||
// keepaliveDialerMargin is the headroom the enforcement floor keeps under the
|
||||
// dialer's ping interval: the floor is half that interval, so a ping lands a
|
||||
// factor of two clear of a strike rather than on the boundary.
|
||||
const keepaliveDialerMargin = 2
|
||||
|
||||
// TestStateStoreServerOptsHoldTheDialersKeepaliveMargin is the cheap half of
|
||||
// the keepalive guard, and the only half that runs when someone changes the
|
||||
// policy: it asserts in microseconds what the stream case below proves over the
|
||||
// control's tear-down time (measured: 41.0s), so a server half that stops
|
||||
// tolerating the dialer's pings fails immediately instead of at the end of a
|
||||
// 50s window.
|
||||
//
|
||||
// A grpc.ServerOption is a closure over unexported grpc state, so neither the
|
||||
// option stateStoreServerOpts returns nor the one ServerKeepaliveOption builds
|
||||
// can be read back once built. What is asserted is therefore the two facts the
|
||||
// chain is made of: the shape of the option list (a loopback listener installs
|
||||
// exactly one, the enforcement policy, and nothing else) and the value that
|
||||
// option is built from, staterpc.ServerKeepalivePolicy. Both numbers in the
|
||||
// agreement are read from staterpc, the package that dials the client and
|
||||
// serves the option, so no assertion here restates a value one of the halves
|
||||
// owns.
|
||||
func TestStateStoreServerOptsHoldTheDialersKeepaliveMargin(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
opts, loopback, err := stateStoreServerOpts("127.0.0.1:9090", "", &staterpc.TaskGrants{})
|
||||
if err != nil {
|
||||
t.Fatalf("stateStoreServerOpts: %v", err)
|
||||
}
|
||||
if !loopback {
|
||||
t.Fatal("127.0.0.1 must be reported as loopback")
|
||||
}
|
||||
// The token interceptors belong to the non-loopback branch, so exactly one
|
||||
// option here is the enforcement policy and nothing else: fewer means the
|
||||
// server serves grpc's default policy (MinTime 5m), which answers the
|
||||
// dialer's pings with GOAWAY too_many_pings and tears a quiet watch down at
|
||||
// ~41s.
|
||||
if len(opts) != 1 {
|
||||
t.Fatalf("the loopback listener installs %d server options, want 1 (the keepalive enforcement policy)", len(opts))
|
||||
}
|
||||
|
||||
floor := staterpc.ServerKeepalivePolicy()
|
||||
dialer := staterpc.ClientKeepaliveParams()
|
||||
|
||||
if floor.MinTime != 5*time.Second {
|
||||
t.Fatalf("the enforcement floor is %s, want 5s: grpc's default 5m is the defect itself, and 10s would sit on the dialer's own KeepaliveMinPingTime clamp, where a ping a microsecond early is a strike", floor.MinTime)
|
||||
}
|
||||
if floor.PermitWithoutStream {
|
||||
t.Fatal("PermitWithoutStream = true: the dialer never pings a connection with no streams, so permitting them only relaxes a setting nothing exercises -- and a streamless ping from any other client would draw strikes against the dialer's traffic")
|
||||
}
|
||||
|
||||
// The relationship, not the literal, is what makes the pair correct: the
|
||||
// dialer's keepalive loop arms its timer one Time ahead and re-arms only
|
||||
// later (on read activity), so consecutive pings are never closer than Time
|
||||
// apart. A floor below that interval is never reached; a floor equal to it
|
||||
// is reached by any ping that arrives early.
|
||||
if floor.MinTime >= dialer.Time {
|
||||
t.Fatalf("the enforcement floor %s is not below the dialer's %s ping interval: every ping would land on the strike boundary", floor.MinTime, dialer.Time)
|
||||
}
|
||||
if floor.MinTime*keepaliveDialerMargin > dialer.Time {
|
||||
t.Fatalf("the enforcement floor %s must be at most %s (a %dx margin under the dialer's %s ping interval): the margin is what a loaded machine's timer delay eats before a ping draws a strike", floor.MinTime, dialer.Time/keepaliveDialerMargin, keepaliveDialerMargin, dialer.Time)
|
||||
}
|
||||
}
|
||||
|
||||
// keepaliveWindow is how long each case below holds one control-plane watch
|
||||
// open. It has to outlast the point where grpc's default enforcement policy
|
||||
// tears the link down: the dialer pings every 10s (staterpc's client keepalive
|
||||
// time, which is also grpc's KeepaliveMinPingTime floor, so a client cannot
|
||||
// ping sooner), the default EnforcementPolicy.MinTime is 5 minutes, and the
|
||||
// server sends GOAWAY too_many_pings once the strikes pass 2 (maxPingStrikes).
|
||||
// Measured: the connection dies 41.0s into the stream. The remaining ~9s are
|
||||
// there because a loaded machine delays timers, which can only move that
|
||||
// tear-down later.
|
||||
const keepaliveWindow = 50 * time.Second
|
||||
|
||||
// TestStateStoreServerOptionsTolerateTheDialersKeepalive holds one watch open
|
||||
// against two servers: one with grpc's default enforcement policy, one with the
|
||||
// options the State Store process actually serves with. The default policy is
|
||||
// the defect -- a server that counts every one of the dialer's pings as a
|
||||
// strike tears down a connection that carries no traffic for ~40s, and a watch
|
||||
// is silent by design while the watched resource's version is unchanged
|
||||
// (internal/app/controlplane's Watch; the dashboard holds the same shape over
|
||||
// its own connection in internal/webui/api_control_plane.go, writing nothing
|
||||
// between versions). That case is also what keeps the other one honest: a
|
||||
// policy that never strikes has nothing to tear down.
|
||||
//
|
||||
// The pair's cases run concurrently (t.Parallel) and share no state: each has
|
||||
// its own bufconn listener, grpc.Server, dialed client and deadline, and each
|
||||
// asserts only on its own stream, so neither one's timing depends on the
|
||||
// other's scheduling. TestStateStoreServerOptsHoldTheDialersKeepaliveMargin
|
||||
// above pins the same agreement in microseconds; this case is the end-to-end
|
||||
// proof, which is the part a value assertion cannot give.
|
||||
func TestStateStoreServerOptionsTolerateTheDialersKeepalive(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skipf("holds a watch open for %s of wall clock; runs in the full suite (task test:full)", keepaliveWindow)
|
||||
}
|
||||
// The options the standalone State Store serves its local profile with,
|
||||
// taken from the composition rather than hand-built here.
|
||||
served, loopback, err := stateStoreServerOpts("127.0.0.1:9090", "", &staterpc.TaskGrants{})
|
||||
if err != nil {
|
||||
t.Fatalf("stateStoreServerOpts: %v", err)
|
||||
}
|
||||
if !loopback {
|
||||
t.Fatal("127.0.0.1 must be reported as loopback")
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
opts []grpc.ServerOption
|
||||
tornDown bool
|
||||
}{
|
||||
{
|
||||
// The control: without an enforcement policy coherent with the
|
||||
// dialer, the same client on the same stream dies mid-window.
|
||||
name: "grpc's default enforcement policy tears the dialer's watch down",
|
||||
tornDown: true,
|
||||
},
|
||||
{
|
||||
name: "the state store's server options keep the dialer's watch up",
|
||||
opts: served,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
elapsed, recvErr, survived := holdWatch(t, tc.opts, keepaliveWindow)
|
||||
// The server never writes, so the stream can only end because the
|
||||
// connection was torn down under it, or because it outlived the window.
|
||||
// Which one happened is the select in holdWatch, not the error the
|
||||
// window's closure surfaces: see holdWatch for why reading the error
|
||||
// made this case flaky.
|
||||
t.Logf("watch lasted %s, survived the window: %v, ended with %v", elapsed, survived, recvErr)
|
||||
if survived {
|
||||
if tc.tornDown {
|
||||
t.Fatalf("the dialer's watch was still open after %s: the default EnforcementPolicy (MinTime 5m) must strike pings that arrive 10s apart and answer with GOAWAY too_many_pings, so this case is not reaching the strike budget and the case below would prove nothing", keepaliveWindow)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !tc.tornDown {
|
||||
t.Fatalf("the dialer's watch ended after %s: %v\nA watch carrying no traffic must survive on the pings staterpc.Dial sends; a server policy that strikes them answers a quiet connection with a dead one at ~40s, which is worse than the keepalive it was added for", elapsed, recvErr)
|
||||
}
|
||||
if !strings.Contains(recvErr.Error(), "too_many_pings") {
|
||||
t.Fatalf("the dialer's watch ended after %s with %v: want GOAWAY too_many_pings, not some other connection loss", elapsed, recvErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// quietWatch is the server half of a control-plane watch that never writes: it
|
||||
// holds one stream open and sends nothing, the state a watch is in while the
|
||||
// watched resource's version is unchanged.
|
||||
type quietWatch struct {
|
||||
controlpb.UnimplementedControlPlaneServiceServer
|
||||
started chan struct{}
|
||||
}
|
||||
|
||||
func (w *quietWatch) Watch(_ *controlpb.WatchRequest, stream controlpb.ControlPlaneService_WatchServer) error {
|
||||
close(w.started)
|
||||
<-stream.Context().Done()
|
||||
return stream.Context().Err()
|
||||
}
|
||||
|
||||
// keepaliveSlack keeps the stream's own deadline clear of the observation window.
|
||||
// They must not be the same duration. The question holdWatch answers is whether
|
||||
// the link outlives the window, and with the two identical the client deadline
|
||||
// resolves microseconds after the timer either way: Recv returns
|
||||
// DeadlineExceeded and the answer becomes a race between two timers rather than
|
||||
// a fact about the link. Measured -- the same 50s window reported the healthy
|
||||
// case as "ended" at 49.998s while the timer was still pending.
|
||||
const keepaliveSlack = 5 * time.Second
|
||||
|
||||
// holdWatch serves opts over bufconn, dials it with the shipped dialer
|
||||
// (staterpc.Dial, so the client keepalive is exactly the one the daemon, the
|
||||
// dashboard and the agent container run), holds one watch stream open for
|
||||
// window, and reports how long the stream lasted, how it ended, and whether it
|
||||
// outlived the window.
|
||||
//
|
||||
// That last report is the property under test, and it is decided by which arm of
|
||||
// a select wins rather than by the flavour of the error the window's closure
|
||||
// surfaces. Reading the error instead -- asking for DeadlineExceeded -- made the
|
||||
// tolerant case flaky under load: the client deadline, the server's stream
|
||||
// context and the transport all resolve within microseconds of one another, so a
|
||||
// watch that was still perfectly open was reported as Canceled or Unavailable,
|
||||
// and a healthy link failed a test about unhealthy links. Measured on a loaded
|
||||
// 16-core host at 49.999s into a 50s window.
|
||||
func holdWatch(t *testing.T, opts []grpc.ServerOption, window time.Duration) (time.Duration, error, bool) {
|
||||
t.Helper()
|
||||
listener := bufconn.Listen(1 << 20)
|
||||
watch := &quietWatch{started: make(chan struct{})}
|
||||
server := grpc.NewServer(opts...)
|
||||
controlpb.RegisterControlPlaneServiceServer(server, watch)
|
||||
served := make(chan struct{})
|
||||
go func() {
|
||||
defer close(served)
|
||||
_ = server.Serve(listener)
|
||||
}()
|
||||
t.Cleanup(func() {
|
||||
server.Stop()
|
||||
_ = listener.Close()
|
||||
<-served
|
||||
})
|
||||
|
||||
client := dialStateStore(t, listener, restartAdminToken)
|
||||
ctx, cancel := context.WithTimeout(t.Context(), window+keepaliveSlack)
|
||||
defer cancel()
|
||||
stream, err := client.ControlPlane().Watch(ctx, &controlpb.WatchRequest{Kind: "any"})
|
||||
if err != nil {
|
||||
t.Fatalf("open watch: %v", err)
|
||||
}
|
||||
select {
|
||||
case <-watch.started:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("the server never started the watch stream")
|
||||
}
|
||||
start := time.Now()
|
||||
// Buffered so the receiving goroutine cannot outlive the call: cancel runs on
|
||||
// the way out, Recv returns, and the send still has somewhere to land.
|
||||
recv := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := stream.Recv()
|
||||
recv <- err
|
||||
}()
|
||||
select {
|
||||
case err := <-recv:
|
||||
return time.Since(start), err, false
|
||||
case <-time.After(window):
|
||||
return time.Since(start), nil, true
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
// TestOpenStateStorePoolFailsClosedWithoutURL pins D1.2: the State Store is
|
||||
// Postgres-only, so a missing database_url must stop it from starting rather
|
||||
// than fall back to SQLite.
|
||||
func TestOpenStateStorePoolFailsClosedWithoutURL(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
b.cfg = config.Config{}
|
||||
if err := b.openStateStorePool(t.Context()); err == nil {
|
||||
t.Fatal("openStateStorePool without database_url succeeded; want fail closed")
|
||||
} else if !strings.Contains(err.Error(), "database_url is required") {
|
||||
t.Fatalf("openStateStorePool error = %v, want it to name the missing database_url", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpenStateStorePoolFailsClosedOnInvalidURL pins the invalid half of D1.2:
|
||||
// a URL that will not connect (here, one that does not parse) is the same
|
||||
// failure as an absent one, surfaced at boot rather than at first query.
|
||||
func TestOpenStateStorePoolFailsClosedOnInvalidURL(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
b.cfg = config.Config{DatabaseURL: "://not-a-url"}
|
||||
if err := b.openStateStorePool(t.Context()); err == nil {
|
||||
t.Fatal("openStateStorePool with an unparseable database_url succeeded; want fail closed")
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpenStateStorePoolRefusesASecondOwner pins the serve-ownership claim: a
|
||||
// second State Store against the same database fails to start while the first
|
||||
// holds it, and starts once the first has shut down.
|
||||
func TestOpenStateStorePoolRefusesASecondOwner(t *testing.T) {
|
||||
url := pgtest.URL(t)
|
||||
first := newBootstrap()
|
||||
first.cfg = config.Config{DatabaseURL: url}
|
||||
if err := first.openStateStorePool(t.Context()); err != nil {
|
||||
t.Fatalf("first openStateStorePool: %v", err)
|
||||
}
|
||||
second := newBootstrap()
|
||||
second.cfg = config.Config{DatabaseURL: url}
|
||||
err := second.openStateStorePool(t.Context())
|
||||
second.cleanup()
|
||||
first.cleanup()
|
||||
if !errors.Is(err, postgres.ErrOwned) {
|
||||
t.Fatalf("second openStateStorePool = %v, want ErrOwned", err)
|
||||
}
|
||||
third := newBootstrap()
|
||||
third.cfg = config.Config{DatabaseURL: url}
|
||||
if err := third.openStateStorePool(t.Context()); err != nil {
|
||||
t.Fatalf("openStateStorePool after the owner shut down: %v", err)
|
||||
}
|
||||
third.cleanup()
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
// pgRecoveryFixture is a migrated database and the config file naming it,
|
||||
// which is all the Postgres recovery path is given: it resolves database_url
|
||||
// from the configuration exactly as the State Store does.
|
||||
func pgRecoveryFixture(t *testing.T) (configPath string, pool *pgxpool.Pool, url string) {
|
||||
t.Helper()
|
||||
url = pgtest.URL(t)
|
||||
dir := t.TempDir()
|
||||
configPath = filepath.Join(dir, "config.toml")
|
||||
body := fmt.Sprintf("bot_user = 'archie'\ndatabase_url = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n",
|
||||
url)
|
||||
if err := os.WriteFile(configPath, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pool, err := postgres.Open(t.Context(), url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
if err := postgres.Migrate(t.Context(), pool, postgres.Migrations()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return configPath, pool, url
|
||||
}
|
||||
|
||||
func requirePGTools(t *testing.T) {
|
||||
t.Helper()
|
||||
for _, tool := range []string{"pg_dump", "pg_restore"} {
|
||||
if _, err := exec.LookPath(tool); err != nil {
|
||||
t.Skipf("%s is not on PATH; this test needs the PostgreSQL 18 client tools", tool)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func putResource(t *testing.T, pool *pgxpool.Pool, kind, value, label string) {
|
||||
t.Helper()
|
||||
resources := postgres.NewResources(pool)
|
||||
expected := int64(0)
|
||||
if current, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, kind); err == nil {
|
||||
expected = current.Version
|
||||
}
|
||||
if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: kind, Value: []byte(value), Actor: label, Source: "test",
|
||||
RequestID: fmt.Sprintf("%s-%d", label, expected+1), ExpectedVersion: expected,
|
||||
}); err != nil {
|
||||
t.Fatalf("put %s: %v", kind, err)
|
||||
}
|
||||
}
|
||||
|
||||
func holdRole(t *testing.T, url, role string) {
|
||||
t.Helper()
|
||||
pool, err := postgres.Open(t.Context(), url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
claim, err := postgres.AcquireOwnership(t.Context(), pool, role)
|
||||
if err != nil {
|
||||
t.Fatalf("hold %s: %v", role, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = claim.Release(t.Context()) })
|
||||
}
|
||||
|
||||
func TestPostgresRecoveryRequiresADatabaseURL(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
configPath := filepath.Join(dir, "config.toml")
|
||||
if err := os.WriteFile(configPath, []byte("bot_user = 'archie'\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, op := range []string{RecoveryBackup, RecoveryRestore, RecoveryValidate, RecoveryRollback} {
|
||||
t.Run(op, func(t *testing.T) {
|
||||
_, err := RunStateStoreRecovery(t.Context(), StateStoreRecoveryOptions{
|
||||
Operation: op, Config: configPath, Out: filepath.Join(dir, "o"), From: filepath.Join(dir, "f"), Kind: "k",
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "database_url") {
|
||||
t.Fatalf("%s without database_url = %v, want a refusal naming database_url", op, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresRecoveryBackupAndRestore(t *testing.T) {
|
||||
requirePGTools(t)
|
||||
configPath, pool, url := pgRecoveryFixture(t)
|
||||
putResource(t, pool, controlplane.ModelRoleAssignmentsKind, `{"implement":"anthropic/claude"}`, "snapshot")
|
||||
snapshot := filepath.Join(t.TempDir(), "snapshot.dump")
|
||||
|
||||
if _, err := RunStateStoreRecovery(t.Context(), StateStoreRecoveryOptions{Operation: RecoveryBackup, Config: configPath, Out: snapshot}); err != nil {
|
||||
t.Fatalf("backup: %v", err)
|
||||
}
|
||||
putResource(t, pool, controlplane.ModelRoleAssignmentsKind, `{"implement":"openai/gpt"}`, "later")
|
||||
|
||||
for _, role := range postgres.ServiceOwners {
|
||||
t.Run("refused while "+role+" serves", func(t *testing.T) {
|
||||
holdRole(t, url, role)
|
||||
_, err := RunStateStoreRecovery(t.Context(), StateStoreRecoveryOptions{Operation: RecoveryRestore, Config: configPath, From: snapshot})
|
||||
if !errors.Is(err, postgres.ErrOwned) {
|
||||
t.Fatalf("restore with %s serving = %v, want ErrOwned", role, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
summary, err := RunStateStoreRecovery(t.Context(), StateStoreRecoveryOptions{Operation: RecoveryRestore, Config: configPath, From: snapshot})
|
||||
if err != nil {
|
||||
t.Fatalf("restore: %v", err)
|
||||
}
|
||||
if !strings.Contains(summary, "after the snapshot") {
|
||||
t.Errorf("restore must say writes after the snapshot are lost; summary = %q", summary)
|
||||
}
|
||||
current, err := postgres.NewResources(pool).Resource(t.Context(), storecontract.DefaultOrgID, controlplane.ModelRoleAssignmentsKind)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if current.Version != 1 {
|
||||
t.Errorf("restored version = %d, want the snapshot's 1", current.Version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresRecoveryValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(t *testing.T, pool *pgxpool.Pool)
|
||||
wantErr string
|
||||
}{
|
||||
{name: "healthy store", setup: func(*testing.T, *pgxpool.Pool) {}},
|
||||
{name: "schema newer than this binary", wantErr: "newer than this binary", setup: func(t *testing.T, pool *pgxpool.Pool) {
|
||||
if _, err := pool.Exec(t.Context(), "INSERT INTO goose_db_version (version_id, is_applied) VALUES ($1, true)", postgres.SupportedSchemaVersion()+1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{name: "stored value boot refuses", wantErr: "dispatch.trigger", setup: func(t *testing.T, pool *pgxpool.Pool) {
|
||||
putResource(t, pool, "scheduling-policy", `{"poll_interval":"1m","max_retries":3,"dispatch":{"trigger":"bogus"}}`, "bad")
|
||||
}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
configPath, pool, _ := pgRecoveryFixture(t)
|
||||
tt.setup(t, pool)
|
||||
summary, err := RunStateStoreRecovery(t.Context(), StateStoreRecoveryOptions{Operation: RecoveryValidate, Config: configPath})
|
||||
if tt.wantErr == "" {
|
||||
if err != nil {
|
||||
t.Fatalf("validate: %v", err)
|
||||
}
|
||||
if !strings.Contains(summary, fmt.Sprintf("schema version %d", postgres.SupportedSchemaVersion())) {
|
||||
t.Errorf("summary = %q, want the schema version", summary)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Fatalf("validate = %v, want refusal containing %q", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresRecoveryRollback(t *testing.T) {
|
||||
configPath, pool, url := pgRecoveryFixture(t)
|
||||
kind := controlplane.ModelRoleAssignmentsKind
|
||||
putResource(t, pool, kind, `{"implement":"anthropic/claude"}`, "first")
|
||||
putResource(t, pool, kind, `{"implement":"openai/gpt"}`, "second")
|
||||
options := StateStoreRecoveryOptions{Operation: RecoveryRollback, Config: configPath, Kind: kind}
|
||||
|
||||
t.Run("refused while the State Store serves", func(t *testing.T) {
|
||||
holdRole(t, url, postgres.OwnerStateStore)
|
||||
if _, err := RunStateStoreRecovery(t.Context(), options); !errors.Is(err, postgres.ErrOwned) {
|
||||
t.Fatalf("rollback with the State Store serving = %v, want ErrOwned", err)
|
||||
}
|
||||
})
|
||||
|
||||
if _, err := RunStateStoreRecovery(t.Context(), options); err != nil {
|
||||
t.Fatalf("rollback: %v", err)
|
||||
}
|
||||
current, err := postgres.NewResources(pool).Resource(t.Context(), storecontract.DefaultOrgID, kind)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(current.Value) != `{"implement": "anthropic/claude"}` && string(current.Value) != `{"implement":"anthropic/claude"}` {
|
||||
t.Errorf("value = %s, want the first revision's", current.Value)
|
||||
}
|
||||
if current.Version != 3 {
|
||||
t.Errorf("version = %d, want the rollback recorded as revision 3", current.Version)
|
||||
}
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// startupGrace is how long RunStateStore is given to either refuse the config
|
||||
// or settle into serving before the test cancels it. It bounds "did startup
|
||||
// reject us" (fast, deterministic) rather than "is the machine fast enough"
|
||||
// (I/O-bound, which is what made the previous 200ms budget a coin flip).
|
||||
const startupGrace = 2 * time.Second
|
||||
|
||||
// shutdownGrace bounds how long a cancelled RunStateStore may take to return.
|
||||
const shutdownGrace = 5 * time.Second
|
||||
|
||||
func TestExternalServicesDoNotRequireBridgeNetwork(t *testing.T) {
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case strings.HasSuffix(r.URL.Path, "/_ping"):
|
||||
w.Header().Set("API-Version", "1.55")
|
||||
_, _ = w.Write([]byte("OK"))
|
||||
case strings.HasSuffix(r.URL.Path, "/containers/json"):
|
||||
writeDockerResponse(t, w, []any{})
|
||||
default:
|
||||
http.Error(w, "bridge discovery must not be required", http.StatusBadRequest)
|
||||
}
|
||||
}))
|
||||
defer api.Close()
|
||||
t.Setenv("DOCKER_HOST", api.URL)
|
||||
t.Setenv("DOCKER_API_VERSION", "1.55")
|
||||
t.Setenv("DOCKER_TLS_VERIFY", "")
|
||||
cfg := config.Config{}
|
||||
cfg.NATS.Mode = config.NATSModeExternal
|
||||
cfg.Containers.Network = "host"
|
||||
cfg.Services = config.Services{config.ServiceNameState: {Target: "state.example.test:9090"}}
|
||||
pool, _, cleanup := startContainers(t.Context(), config.NewHolder(cfg), secret.NewRegistry(), slog.New(slog.DiscardHandler))
|
||||
defer cleanup()
|
||||
if pool == nil {
|
||||
t.Fatal("external services on host networking must construct a worker pool")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunStateStoreAcceptsEnvironmentToken(t *testing.T) {
|
||||
t.Setenv("STATE_STORE_TOKEN", "environment-token")
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
data := fmt.Sprintf("bot_user = 'archie'\ndatabase_url = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n[[repos]]\nowner = 'acme'\nname = 'widget'\n", pgtest.URL(t))
|
||||
if err := os.WriteFile(path, []byte(data), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Cancellation is the assertion: RunStateStore must start (the env token
|
||||
// satisfies the non-loopback listener) and then return promptly when the
|
||||
// context ends. It must NOT be raced against a wall-clock budget -- SQLite
|
||||
// schema init is I/O, so a test that gives startup a few hundred
|
||||
// milliseconds is a coin flip, not a check. Cancel first, then assert.
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "0.0.0.0:0"}) }()
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
// Returning before cancellation means startup refused the config, which
|
||||
// is the failure this test exists to catch.
|
||||
t.Fatalf("RunStateStore returned before cancellation, want it to serve: %v", err)
|
||||
case <-time.After(startupGrace):
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("RunStateStore returned %v, want nil or context.Canceled", err)
|
||||
}
|
||||
case <-time.After(shutdownGrace):
|
||||
t.Fatal("RunStateStore did not return within the shutdown grace after cancellation")
|
||||
}
|
||||
}
|
||||
@@ -1,219 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
|
||||
func TestStateStoreServerOptsLoopbackIsInsecure(t *testing.T) {
|
||||
opts, loopback, err := stateStoreServerOpts("127.0.0.1:9090", "", &staterpc.TaskGrants{})
|
||||
if err != nil {
|
||||
t.Fatalf("stateStoreServerOpts(loopback, no token) error: %v", err)
|
||||
}
|
||||
if !loopback {
|
||||
t.Fatal("loopback listener should report loopback")
|
||||
}
|
||||
if len(opts) != 1 {
|
||||
t.Fatalf("loopback listener should install only the keepalive enforcement policy (no token interceptors), got %d server opts", len(opts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateStoreServerOptsNonLoopbackRequiresToken(t *testing.T) {
|
||||
if _, _, err := stateStoreServerOpts("0.0.0.0:9090", "", &staterpc.TaskGrants{}); err == nil {
|
||||
t.Fatal("non-loopback listener without a token should fail closed")
|
||||
}
|
||||
opts, loopback, err := stateStoreServerOpts("0.0.0.0:9090", "secret", &staterpc.TaskGrants{})
|
||||
if err != nil {
|
||||
t.Fatalf("non-loopback listener with a token error: %v", err)
|
||||
}
|
||||
if loopback {
|
||||
t.Fatal("non-loopback listener should not report loopback")
|
||||
}
|
||||
if len(opts) != 3 {
|
||||
t.Fatalf("non-loopback listener should install three server options (the keepalive enforcement policy and the unary and stream task-grant interceptors), got %d", len(opts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateStoreServerOptsMalformedListen(t *testing.T) {
|
||||
if _, _, err := stateStoreServerOpts("not-an-address", "", &staterpc.TaskGrants{}); err == nil {
|
||||
t.Fatal("malformed listen address should error")
|
||||
}
|
||||
}
|
||||
|
||||
// TestServeStateStoreServesContract verifies the standalone composition's
|
||||
// serveStateStore + stateStoreDeps wiring actually serves the StateStore gRPC
|
||||
// contract over a real loopback listener: the same service .4.2 serves
|
||||
// in-process, now extracted into its own process. A read-only StatusCounts
|
||||
// call proves the service is registered and wired to the opened store.
|
||||
func TestServeStateStoreServesContract(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
defer st.Close()
|
||||
|
||||
b := newBootstrap()
|
||||
b.st = st
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
defer listener.Close()
|
||||
|
||||
serveCh := make(chan error, 1)
|
||||
go func() { serveCh <- serveStateStore(ctx, listener, b.stateStoreDeps(&staterpc.TaskGrants{}), nil) }()
|
||||
|
||||
conn, err := grpc.NewClient("passthrough:///"+listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := pb.NewStateStoreServiceClient(conn)
|
||||
if _, err := client.StatusCounts(t.Context(), &pb.StatusCountsRequest{}); err != nil {
|
||||
t.Fatalf("StatusCounts over gRPC: %v", err)
|
||||
}
|
||||
|
||||
cancel()
|
||||
select {
|
||||
case err := <-serveCh:
|
||||
if err != nil {
|
||||
t.Fatalf("serveStateStore returned: %v", err)
|
||||
}
|
||||
case <-t.Context().Done():
|
||||
t.Fatal("serveStateStore did not return after cancel")
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreDepsServeTaskLogs proves the standalone State Store process is
|
||||
// where a task-log read is served from: the daemon process holds no dashboard
|
||||
// listener any more, so the service the UI process dials is the only place this
|
||||
// contract can have a server-side reader, and it must be the daemon's own
|
||||
// registry over the state directory.
|
||||
//
|
||||
// Without this wiring the RPC exists and answers Unavailable for every task,
|
||||
// which the dashboard reports as "this process cannot read logs" -- honest, but
|
||||
// still not a working download (archie-core-iaqx).
|
||||
func TestStateStoreDepsServeTaskLogs(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
logs := logging.NewTaskRegistry(filepath.Join(t.TempDir(), "logs", "tasks"), logging.NewFeed(10), logging.TaskSinkOptions{})
|
||||
b.taskLogs = logs
|
||||
|
||||
deps := b.stateStoreDeps(&staterpc.TaskGrants{})
|
||||
if deps.TaskLogs == nil {
|
||||
t.Fatal("stateStoreDeps leaves TaskLogs nil; the State Store process is the only server-side reader the dashboard can reach")
|
||||
}
|
||||
if deps.TaskLogs != storecontract.TaskLogStore(logs) {
|
||||
t.Fatalf("TaskLogs = %T, want the daemon's own registry over the state directory", deps.TaskLogs)
|
||||
}
|
||||
// A boot without a registry must not fabricate one: an empty reader would
|
||||
// answer found=false for every attempt, which reads as "the attempt has no
|
||||
// log" rather than "this process cannot read logs".
|
||||
if plain := (&boot{}).stateStoreDeps(&staterpc.TaskGrants{}); plain.TaskLogs != nil {
|
||||
t.Errorf("TaskLogs = %T with no registry on the boot, want nil so the RPC reports unavailability", plain.TaskLogs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreDepsServePlaybookDispatcher verifies stateStoreDeps lifts the
|
||||
// opened *pgstore.TaskDB's PlaybookDispatcher surface onto Deps, so the
|
||||
// standalone State Store has a server for the two playbook RPCs. It does not
|
||||
// dial those RPCs here -- it inspects the assembled Deps only. The nil check
|
||||
// is the load-bearing part: a boot without a store must leave
|
||||
// PlaybookDispatcher nil (the server then answers codes.Unavailable) rather
|
||||
// than fabricating a dispatcher.
|
||||
func TestStateStoreDepsServePlaybookDispatcher(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
defer st.Close()
|
||||
|
||||
b := newBootstrap()
|
||||
b.st = st
|
||||
// The playbook ledger, sources and event types are served by the
|
||||
// event-capture store; the task store no longer serves them.
|
||||
eda := pgstore.EDA(t, nil)
|
||||
b.eda = eda
|
||||
deps := b.stateStoreDeps(&staterpc.TaskGrants{})
|
||||
if deps.PlaybookDispatcher == nil {
|
||||
t.Fatal("stateStoreDeps leaves PlaybookDispatcher nil; the standalone State Store is the only production server for the playbook dispatch ledger")
|
||||
}
|
||||
if deps.PlaybookDispatcher != storecontract.PlaybookDispatcher(eda) {
|
||||
t.Fatalf("PlaybookDispatcher = %T, want the opened event-capture store", deps.PlaybookDispatcher)
|
||||
}
|
||||
if deps.Sources == nil || deps.EventTypes == nil {
|
||||
t.Fatalf("stateStoreDeps Sources = %v, EventTypes = %v; want both served, or intake verifies nothing and nothing dispatches", deps.Sources, deps.EventTypes)
|
||||
}
|
||||
// A boot without a store must not fabricate one: nil keeps the RPCs honest
|
||||
// as unavailable rather than depending on a nil receiver.
|
||||
if plain := (&boot{}).stateStoreDeps(&staterpc.TaskGrants{}); plain.PlaybookDispatcher != nil {
|
||||
t.Errorf("PlaybookDispatcher = %T with no store on the boot, want nil so the RPCs report unavailability", plain.PlaybookDispatcher)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreDataSurvivesRestart is the .4.7 restart/recovery check: a
|
||||
// task written by one archie-state-store process must still be there for a
|
||||
// fresh process opening the same archie.db path, proving b.openStateStore
|
||||
// (RunStateStore's own opener, not a test-only shortcut) round-trips through
|
||||
// the file rather than an in-memory or per-process store.
|
||||
func TestStateStoreDataSurvivesRestart(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
DatabaseURL: pgtest.URL(t),
|
||||
}
|
||||
|
||||
first := newBootstrap()
|
||||
first.cfg = cfg
|
||||
if err := first.openStateStore(t.Context()); err != nil {
|
||||
t.Fatalf("first openStateStore: %v", err)
|
||||
}
|
||||
task, err := first.st.EnqueueChatTask(t.Context(), "acme", "widget", "restart check", "body", "implement", "", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("EnqueueChatTask: %v", err)
|
||||
}
|
||||
first.cleanup() // closes the SQLite handle, simulating process exit
|
||||
|
||||
second := newBootstrap()
|
||||
second.cfg = cfg
|
||||
if err := second.openStateStore(t.Context()); err != nil {
|
||||
t.Fatalf("second openStateStore: %v", err)
|
||||
}
|
||||
defer second.cleanup()
|
||||
|
||||
got, err := second.st.TaskByID(t.Context(), task.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskByID after restart: %v", err)
|
||||
}
|
||||
if got == nil {
|
||||
t.Fatal("task did not survive a restart against the same db path")
|
||||
}
|
||||
if got.Title != task.Title {
|
||||
t.Fatalf("restarted task title = %q, want %q", got.Title, task.Title)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpenStoresNeverOwnsTaskDB is the .4.7 single-owner-SQLite assertion:
|
||||
// the daemon/gateway composition path (openStores, run by Run/RunGateway)
|
||||
// must never populate b.st -- that field is reserved for the standalone
|
||||
// archie-state-store binary's openStateStore (state_store.go). Regressing
|
||||
// this would mean archie.db is opened by two processes at once.
|
||||
func TestOpenStoresNeverOwnsTaskDB(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
b.cfg = config.Config{DatabaseURL: pgtest.URL(t)}
|
||||
if err := b.openStores(t.Context()); err != nil {
|
||||
t.Fatalf("openStores: %v", err)
|
||||
}
|
||||
defer b.cleanup()
|
||||
|
||||
if b.st != nil {
|
||||
t.Fatal("openStores populated b.st -- the daemon/gateway path must not own archie.db directly (docs/prds/state-store-contract.md §12 step 7)")
|
||||
}
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
)
|
||||
|
||||
func toolCallEvent(taskID int64, attempt int) events.Event {
|
||||
return events.Event{
|
||||
Kind: events.KindToolCall,
|
||||
TaskID: taskID,
|
||||
Attempt: attempt,
|
||||
Stage: "implement",
|
||||
Detail: "all checks passed",
|
||||
Data: map[string]any{"tool": "shell", "failed": false},
|
||||
At: time.Now().UTC(),
|
||||
Workflow: "tdd",
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertEventProjectsToolCall pins the projection (test a): one tool_call
|
||||
// event through the decorated store persists the event AND exactly one
|
||||
// tool_calls row, with the row's fields taken from the event -- the tool name
|
||||
// from data, the output summary in result for a succeeded call.
|
||||
func TestInsertEventProjectsToolCall(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
eda := pgstore.EDA(t, nil)
|
||||
ts := newToolCallProjectingTaskStore(st, eda, slog.Default())
|
||||
|
||||
id, err := ts.InsertEvent(t.Context(), toolCallEvent(42, 2))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertEvent() error = %v", err)
|
||||
}
|
||||
if id == 0 {
|
||||
t.Fatal("InsertEvent() returned id 0; the event itself must still persist")
|
||||
}
|
||||
|
||||
rows, err := eda.TaskToolCalls(t.Context(), 42)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskToolCalls() error = %v", err)
|
||||
}
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("TaskToolCalls() returned %d rows, want exactly one per tool_call event: %+v", len(rows), rows)
|
||||
}
|
||||
row := rows[0]
|
||||
if row.TaskID != 42 || row.Attempt != 2 {
|
||||
t.Errorf("row task/attempt = %d/%d, want 42/2", row.TaskID, row.Attempt)
|
||||
}
|
||||
if row.Tool != "shell" {
|
||||
t.Errorf("row tool = %q, want \"shell\" from the event's data", row.Tool)
|
||||
}
|
||||
if row.Result != "all checks passed" {
|
||||
t.Errorf("row result = %q, want the event detail of a succeeded call", row.Result)
|
||||
}
|
||||
if row.Error != "" {
|
||||
t.Errorf("row error = %q, want empty for a succeeded call", row.Error)
|
||||
}
|
||||
if row.CalledAt.IsZero() {
|
||||
t.Error("row called_at is zero; it must carry the event time")
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertEventProjectsFailedToolCall pins the failure column: a tool_call
|
||||
// event whose call failed stores its detail under error, not result, so the
|
||||
// transcript can distinguish the two without re-parsing text.
|
||||
func TestInsertEventProjectsFailedToolCall(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
eda := pgstore.EDA(t, nil)
|
||||
ts := newToolCallProjectingTaskStore(st, eda, slog.Default())
|
||||
|
||||
e := toolCallEvent(42, 2)
|
||||
e.Detail = "error: no such file"
|
||||
e.Data = map[string]any{"tool": "read_file", "failed": true}
|
||||
if _, err := ts.InsertEvent(t.Context(), e); err != nil {
|
||||
t.Fatalf("InsertEvent() error = %v", err)
|
||||
}
|
||||
|
||||
rows, err := eda.TaskToolCalls(t.Context(), 42)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskToolCalls() error = %v", err)
|
||||
}
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("TaskToolCalls() returned %d rows, want 1", len(rows))
|
||||
}
|
||||
if rows[0].Error != "error: no such file" {
|
||||
t.Errorf("row error = %q, want the failed call's detail", rows[0].Error)
|
||||
}
|
||||
if rows[0].Result != "" {
|
||||
t.Errorf("row result = %q, want empty for a failed call", rows[0].Result)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertEventLeavesOtherKindsUnprojected pins the kind gate: only
|
||||
// tool_call events reach the collection; the task timeline's other kinds must
|
||||
// not grow tool_calls rows.
|
||||
func TestInsertEventLeavesOtherKindsUnprojected(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
eda := pgstore.EDA(t, nil)
|
||||
ts := newToolCallProjectingTaskStore(st, eda, slog.Default())
|
||||
|
||||
if _, err := ts.InsertEvent(t.Context(), events.Event{
|
||||
Kind: events.KindStageFinish, TaskID: 42, Attempt: 2,
|
||||
Data: map[string]any{"duration_ms": 12.0},
|
||||
}); err != nil {
|
||||
t.Fatalf("InsertEvent() error = %v", err)
|
||||
}
|
||||
|
||||
rows, err := eda.TaskToolCalls(t.Context(), 42)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskToolCalls() error = %v", err)
|
||||
}
|
||||
if len(rows) != 0 {
|
||||
t.Fatalf("TaskToolCalls() returned %d rows for a stage_finish event, want 0", len(rows))
|
||||
}
|
||||
}
|
||||
|
||||
// failingToolCallWriter stands in for a broken event-capture store, so the
|
||||
// never-returned-failure contract can be tested without sabotaging the real
|
||||
// store.
|
||||
type failingToolCallWriter struct{ err error }
|
||||
|
||||
func (f failingToolCallWriter) InsertToolCall(context.Context, postgres.ToolCall) error {
|
||||
return f.err
|
||||
}
|
||||
|
||||
// TestInsertEventSucceedsWhenProjectionFails pins the load-bearing half of the
|
||||
// contract: a tool_calls write failure is never returned from InsertEvent --
|
||||
// event persistence is the contract, the projection is observability -- and
|
||||
// the failure is recorded, not swallowed silently.
|
||||
func TestInsertEventSucceedsWhenProjectionFails(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
var buf bytes.Buffer
|
||||
ts := newToolCallProjectingTaskStore(st, failingToolCallWriter{
|
||||
err: errors.New("disk on fire"),
|
||||
}, slog.New(slog.NewTextHandler(&buf, nil)))
|
||||
|
||||
id, err := ts.InsertEvent(t.Context(), toolCallEvent(42, 2))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertEvent() error = %v; a projection failure must never fail the event write", err)
|
||||
}
|
||||
if id == 0 {
|
||||
t.Fatal("InsertEvent() returned id 0; the event must still persist")
|
||||
}
|
||||
if got := ts.toolCallProjectionFailures(); got != 1 {
|
||||
t.Errorf("toolCallProjectionFailures() = %d, want 1: the failure must be counted, not swallowed", got)
|
||||
}
|
||||
if !strings.Contains(buf.String(), "disk on fire") {
|
||||
t.Errorf("log output %q does not carry the projection failure; it was swallowed silently", buf.String())
|
||||
}
|
||||
// The event row is what the whole surface exists for -- it must be there.
|
||||
persisted, err := st.TaskEvents(t.Context(), 42)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskEvents() error = %v", err)
|
||||
}
|
||||
if len(persisted) != 1 {
|
||||
t.Fatalf("TaskEvents() returned %d events, want 1: the event persisted despite the projection failure", len(persisted))
|
||||
}
|
||||
}
|
||||
|
||||
// TestStateStoreDepsProjectToolCalls pins the wire-in: with both stores open,
|
||||
// the composition serves Tasks as the projecting decorator; with no
|
||||
// event-capture store, Tasks is passed through untouched rather than wrapped
|
||||
// around a nil writer.
|
||||
func TestStateStoreDepsProjectToolCalls(t *testing.T) {
|
||||
st := pgstore.Open(t)
|
||||
|
||||
b := newBootstrap()
|
||||
b.st = st
|
||||
b.eda = pgstore.EDA(t, nil)
|
||||
deps := b.stateStoreDeps(&staterpc.TaskGrants{})
|
||||
if _, ok := deps.Tasks.(*toolCallProjectingTaskStore); !ok {
|
||||
t.Fatalf("deps.Tasks = %T, want the tool_call projecting decorator", deps.Tasks)
|
||||
}
|
||||
|
||||
plain := newBootstrap()
|
||||
plain.st = st
|
||||
plainDeps := plain.stateStoreDeps(&staterpc.TaskGrants{})
|
||||
if _, ok := plainDeps.Tasks.(*toolCallProjectingTaskStore); ok {
|
||||
t.Fatal("deps.Tasks is decorated with no event-capture store on the boot; the projection would write through a nil writer")
|
||||
}
|
||||
}
|
||||
@@ -1,482 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// TestProviderOutcomeRecorderRecordsTheLastCallOnly pins the producer behind
|
||||
// /status' chat-model line: it reports what actually happened, and the moment
|
||||
// something else happens it reports that instead. There is no probe anywhere
|
||||
// in this path -- a chat command must not pay a network round trip, and a
|
||||
// probe would answer for the probe's connection, not the daemon's.
|
||||
func TestProviderOutcomeRecorderRecordsTheLastCallOnly(t *testing.T) {
|
||||
failure := errors.New("401 Unauthorized")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
record func(*providerOutcomeRecorder)
|
||||
wantAttempted bool
|
||||
wantErr string
|
||||
wantModel string
|
||||
}{
|
||||
{
|
||||
name: "nothing recorded yet",
|
||||
record: func(*providerOutcomeRecorder) {},
|
||||
wantAttempted: false,
|
||||
},
|
||||
{
|
||||
name: "successful call",
|
||||
record: func(r *providerOutcomeRecorder) { r.record("openai/gpt-5.6", nil) },
|
||||
wantAttempted: true,
|
||||
wantModel: "openai/gpt-5.6",
|
||||
},
|
||||
{
|
||||
name: "failed call",
|
||||
record: func(r *providerOutcomeRecorder) { r.record("openai/gpt-5.6", failure) },
|
||||
wantAttempted: true,
|
||||
wantErr: "401 Unauthorized",
|
||||
wantModel: "openai/gpt-5.6",
|
||||
},
|
||||
{
|
||||
name: "a later success replaces an earlier failure",
|
||||
record: func(r *providerOutcomeRecorder) {
|
||||
r.record("openai/gpt-5.6", failure)
|
||||
r.record("deepseek/deepseek-v4-pro", nil)
|
||||
},
|
||||
wantAttempted: true,
|
||||
wantModel: "deepseek/deepseek-v4-pro",
|
||||
},
|
||||
{
|
||||
name: "a later failure replaces an earlier success",
|
||||
record: func(r *providerOutcomeRecorder) {
|
||||
r.record("openai/gpt-5.6", nil)
|
||||
r.record("openai/gpt-5.6", failure)
|
||||
},
|
||||
wantAttempted: true,
|
||||
wantErr: "401 Unauthorized",
|
||||
wantModel: "openai/gpt-5.6",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
recorder := newProviderOutcomeRecorder()
|
||||
before := time.Now().Add(-time.Second)
|
||||
tc.record(recorder)
|
||||
|
||||
outcome, attempted := recorder.LastChatModelOutcome()
|
||||
if attempted != tc.wantAttempted {
|
||||
t.Fatalf("LastChatModelOutcome() attempted = %v, want %v", attempted, tc.wantAttempted)
|
||||
}
|
||||
if !tc.wantAttempted {
|
||||
return
|
||||
}
|
||||
if outcome.Err != tc.wantErr {
|
||||
t.Errorf("outcome.Err = %q, want %q", outcome.Err, tc.wantErr)
|
||||
}
|
||||
if outcome.Model != tc.wantModel {
|
||||
t.Errorf("outcome.Model = %q, want %q", outcome.Model, tc.wantModel)
|
||||
}
|
||||
if outcome.At.Before(before) || outcome.At.After(time.Now()) {
|
||||
t.Errorf("outcome.At = %v, want a time within the call's window", outcome.At)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// sendChatTurn is written to tolerate a nil recorder (a call path built
|
||||
// without one must not panic), and a nil recorder must claim nothing.
|
||||
var absent *providerOutcomeRecorder
|
||||
absent.record("openai/gpt-5.6", failure)
|
||||
if _, attempted := absent.LastChatModelOutcome(); attempted {
|
||||
t.Error("a nil recorder reported an attempted call; it has nothing to report")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSendChatTurnRecordsEachCallOutcome pins the wire between the chat path
|
||||
// and /status: sendChatTurn is the single point every chat-model call in this
|
||||
// process passes through, and it must record what happened there rather than
|
||||
// trusting each caller to remember. A recorder that is never written is the
|
||||
// failure this pins -- /status would say "no calls attempted yet" forever
|
||||
// while chat worked, which reads as a broken provider rather than a broken
|
||||
// wire.
|
||||
func TestSendChatTurnRecordsEachCallOutcome(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reply string
|
||||
status int
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "successful call",
|
||||
reply: `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"gpt-5.6","choices":[{"index":0,"message":{"role":"assistant","content":"hello"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`,
|
||||
status: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "failed call",
|
||||
reply: `{"error":{"message":"invalid api key","type":"invalid_request_error"}}`,
|
||||
status: http.StatusUnauthorized,
|
||||
wantErr: "invalid api key",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var requestedPath string
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestedPath = r.URL.Path
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(tc.status)
|
||||
_, _ = io.WriteString(w, tc.reply)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
|
||||
llm := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
})
|
||||
if llm == nil {
|
||||
t.Fatal("NewRuntime = nil, want a runtime for the configured provider")
|
||||
}
|
||||
|
||||
recorder := newProviderOutcomeRecorder()
|
||||
_, err := sendChatTurn(t.Context(), llm, "openai/gpt-5.6", core.GenerateOptions{
|
||||
Messages: []chat.Message{{Role: chat.RoleUser, Content: "hi"}},
|
||||
MaxSteps: 1,
|
||||
}, nil, recorder, nil)
|
||||
|
||||
if tc.wantErr == "" && err != nil {
|
||||
t.Fatalf("sendChatTurn = %v, want success (request path %q)", err, requestedPath)
|
||||
}
|
||||
if tc.wantErr != "" && (err == nil || !strings.Contains(err.Error(), tc.wantErr)) {
|
||||
t.Fatalf("sendChatTurn = %v, want an error containing %q", err, tc.wantErr)
|
||||
}
|
||||
|
||||
outcome, attempted := recorder.LastChatModelOutcome()
|
||||
if !attempted {
|
||||
t.Fatal("recorded outcome = attempted false, want the call recorded at sendChatTurn")
|
||||
}
|
||||
if outcome.Model != "openai/gpt-5.6" {
|
||||
t.Errorf("outcome.Model = %q, want the model the call used", outcome.Model)
|
||||
}
|
||||
if got := outcome.Err != ""; got != (tc.wantErr != "") {
|
||||
t.Errorf("outcome.Err = %q, want an error report = %v", outcome.Err, tc.wantErr != "")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestStatusHealthOmitsSourcesThisProcessDoesNotOwn pins "truthful or
|
||||
// absent": with no subsystem wired, the report is empty rather than full of
|
||||
// zeroes that read as measurements. This is the shape the standalone Gateway
|
||||
// process produces -- it owns no container pool, no poll loop and no channel
|
||||
// manager, and must not pretend otherwise.
|
||||
func TestStatusHealthOmitsSourcesThisProcessDoesNotOwn(t *testing.T) {
|
||||
report := statusHealth{}.Health()
|
||||
|
||||
if report.Broker != nil {
|
||||
t.Errorf("Broker = %+v, want nil when this process holds no broker connection", report.Broker)
|
||||
}
|
||||
if report.Containers != nil {
|
||||
t.Errorf("Containers = %+v, want nil when this process owns no container pool", report.Containers)
|
||||
}
|
||||
if report.ChatModel != nil {
|
||||
t.Errorf("ChatModel = %+v, want nil when this process records no model calls", report.ChatModel)
|
||||
}
|
||||
if report.Channels != nil {
|
||||
t.Errorf("Channels = %+v, want nil when this process owns no channel manager", report.Channels)
|
||||
}
|
||||
if report.LastPoll != nil {
|
||||
t.Errorf("LastPoll = %+v, want nil when this process does not poll", report.LastPoll)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStatusHealthReportsOnlyBrokerAndChatModel pins what every /status
|
||||
// surface actually serves. The only production router.Health is the Gateway's,
|
||||
// and the facts the Gateway holds are the ones this boot is given here. A
|
||||
// producer wired to the pool or the poll loop would build a section nothing in
|
||||
// this process can serve -- the operator would read a full health list in a
|
||||
// process that has no container pool, and would read it as measured.
|
||||
func TestStatusHealthReportsOnlyBrokerAndChatModel(t *testing.T) {
|
||||
srv := startEmbeddedNATS(t)
|
||||
conn, err := natsio.Connect(srv.ClientURL())
|
||||
if err != nil {
|
||||
t.Fatalf("nats connect: %v", err)
|
||||
}
|
||||
t.Cleanup(conn.Close)
|
||||
|
||||
recorder := newProviderOutcomeRecorder()
|
||||
recorder.record("openai/gpt-5.6", nil)
|
||||
|
||||
// The pool and the daemon are present here on purpose: the daemon
|
||||
// composition holds both, so a producer reading them would claim a
|
||||
// container and poll line from a process whose router cannot serve it.
|
||||
b := &boot{
|
||||
taskActionsConn: conn,
|
||||
providerOutcomes: recorder,
|
||||
containerPool: &container.Pool{},
|
||||
d: &daemon.Daemon{},
|
||||
}
|
||||
|
||||
report := newStatusHealth(b).Health()
|
||||
|
||||
if report.Broker == nil || !report.Broker.Connected {
|
||||
t.Errorf("Broker = %+v, want this process's own connected broker connection", report.Broker)
|
||||
}
|
||||
if report.ChatModel == nil || !report.ChatModel.Attempted {
|
||||
t.Errorf("ChatModel = %+v, want the recorded call", report.ChatModel)
|
||||
}
|
||||
if report.Containers != nil {
|
||||
t.Errorf("Containers = %+v, want nil: no /status surface is served from this boot's pool", report.Containers)
|
||||
}
|
||||
if report.Channels != nil {
|
||||
t.Errorf("Channels = %+v, want nil: no /status surface is served from this boot's channels", report.Channels)
|
||||
}
|
||||
if report.LastPoll != nil {
|
||||
t.Errorf("LastPoll = %v, want nil: no /status surface is served from this boot's poll loop", report.LastPoll)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStatusHealthReportsEveryWiredSource pins that each wired source reaches
|
||||
// the report, and that a source reporting "nothing has happened yet" stays
|
||||
// distinguishable from an absent source.
|
||||
func TestStatusHealthReportsEveryWiredSource(t *testing.T) {
|
||||
recorder := newProviderOutcomeRecorder()
|
||||
recorder.record("openai/gpt-5.6", errors.New("401 Unauthorized"))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
check func(*testing.T, gateway.HealthReport)
|
||||
src statusHealth
|
||||
}{
|
||||
{
|
||||
name: "broker",
|
||||
src: statusHealth{broker: func() (bool, bool) { return true, true }},
|
||||
check: func(t *testing.T, r gateway.HealthReport) {
|
||||
if r.Broker == nil || !r.Broker.Connected {
|
||||
t.Errorf("Broker = %+v, want connected", r.Broker)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "chat model recorder",
|
||||
src: statusHealth{chatModel: recorder},
|
||||
check: func(t *testing.T, r gateway.HealthReport) {
|
||||
if r.ChatModel == nil || !r.ChatModel.Attempted {
|
||||
t.Fatalf("ChatModel = %+v, want the recorded call", r.ChatModel)
|
||||
}
|
||||
if r.ChatModel.Outcome.Err != "401 Unauthorized" {
|
||||
t.Errorf("ChatModel.Outcome.Err = %q, want the recorded error", r.ChatModel.Outcome.Err)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "chat model recorder with no calls yet",
|
||||
src: statusHealth{chatModel: newProviderOutcomeRecorder()},
|
||||
check: func(t *testing.T, r gateway.HealthReport) {
|
||||
if r.ChatModel == nil {
|
||||
t.Fatal("ChatModel = nil, want a present section saying nothing has been attempted")
|
||||
}
|
||||
if r.ChatModel.Attempted {
|
||||
t.Error("ChatModel.Attempted = true, want false before any call")
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
tc.check(t, tc.src.Health())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupGatewayChatCarriesStatusHealth pins the last hop: the router the
|
||||
// composition root builds must expose the health source, or /status answers
|
||||
// from the queue and runtime sections alone while the composition root
|
||||
// believes health is wired.
|
||||
//
|
||||
// Asserted on the source rather than by calling setupGatewayChat, which needs
|
||||
// a fully built boot (stores, runtime, memory engines) to run. That is the
|
||||
// same technique composition_order_test.go uses for wiring nothing at runtime
|
||||
// observes. The predecessor of this test drove buildTelegramRouter, which the
|
||||
// Messaging Service extraction deleted along with the daemon's own routers;
|
||||
// setupGatewayChat is now the sole production constructor of a Router.
|
||||
func TestSetupGatewayChatCarriesStatusHealth(t *testing.T) {
|
||||
fileset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fileset, "gateway_runtime.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := methodBody(t, file, "setupGatewayChat")
|
||||
|
||||
var wired bool
|
||||
ast.Inspect(body, func(n ast.Node) bool {
|
||||
assign, ok := n.(*ast.AssignStmt)
|
||||
if !ok || len(assign.Lhs) != 1 {
|
||||
return true
|
||||
}
|
||||
sel, ok := assign.Lhs[0].(*ast.SelectorExpr)
|
||||
if !ok || sel.Sel.Name != "Health" {
|
||||
return true
|
||||
}
|
||||
if ident, ok := sel.X.(*ast.Ident); ok && ident.Name == "router" {
|
||||
wired = true
|
||||
}
|
||||
return true
|
||||
})
|
||||
if !wired {
|
||||
t.Fatal("setupGatewayChat never assigns router.Health; /status would lose its health section")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouterWithHealthAnswersStatus is the behavioural half: a router holding a
|
||||
// health source renders it into /status.
|
||||
func TestRouterWithHealthAnswersStatus(t *testing.T) {
|
||||
sessions := gateway.NewSessionStoreMemory()
|
||||
t.Cleanup(func() { _ = sessions.Close() })
|
||||
|
||||
st := pgstore.Open(t)
|
||||
router := gateway.NewRouter(st, nil, "web")
|
||||
router.InitSessions(sessions)
|
||||
router.Health = statusHealth{broker: func() (bool, bool) { return true, true }}
|
||||
|
||||
reply, err := router.Route(context.Background(), gateway.Inbound{
|
||||
Message: messaging.Message{Role: messaging.RoleUser, Text: "/status"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Route(/status): %v", err)
|
||||
}
|
||||
if !strings.Contains(reply, "Broker: connected") {
|
||||
t.Errorf("reply = %q, want the wired broker line", reply)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecordKeepsTheLatestOutcomeUnderReordering is the regression case for a
|
||||
// /status chat-model line that reports an older call as the latest. The
|
||||
// completion time is taken before the lock, so two concurrent calls can reach
|
||||
// the lock in the opposite order to their completion; an unconditional store
|
||||
// then overwrites the newer outcome with the older one.
|
||||
func TestRecordKeepsTheLatestOutcomeUnderReordering(t *testing.T) {
|
||||
r := &providerOutcomeRecorder{}
|
||||
newer := time.Now().UTC()
|
||||
older := newer.Add(-time.Minute)
|
||||
|
||||
// The later call completes and stores first, as it would when the earlier
|
||||
// call is descheduled between stamping and locking.
|
||||
r.recordAt("openai/gpt-5.6", nil, newer)
|
||||
r.recordAt("openai/gpt-4o", errors.New("401 Unauthorized"), older)
|
||||
|
||||
got, attempted := r.LastChatModelOutcome()
|
||||
if !attempted {
|
||||
t.Fatal("LastChatModelOutcome reports no attempt after two records")
|
||||
}
|
||||
if got.Model != "openai/gpt-5.6" || got.Err != "" {
|
||||
t.Fatalf("outcome = %+v, want the newer successful call to survive", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecordStoresTheFirstOutcomeWhateverItsAge pins that the ordering guard
|
||||
// does not swallow the first record, whose timestamp has nothing to compare
|
||||
// against.
|
||||
func TestRecordStoresTheFirstOutcomeWhateverItsAge(t *testing.T) {
|
||||
r := &providerOutcomeRecorder{}
|
||||
r.recordAt("openai/gpt-4o", nil, time.Now().UTC().Add(-time.Hour))
|
||||
|
||||
if _, attempted := r.LastChatModelOutcome(); !attempted {
|
||||
t.Fatal("the first recorded outcome was dropped")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCuratorRunnerRefusesWithoutARuntime pins that a curator asking for a
|
||||
// completion on a daemon with no configured provider gets an error. The
|
||||
// runtime constructor returns nil in that case, and a nil *runtime.Runtime
|
||||
// panics on its first method call, which would take the daemon down. Nothing
|
||||
// is recorded: no provider call was attempted, and saying one failed would be
|
||||
// a /status that blames the provider for a local misconfiguration.
|
||||
func TestCuratorRunnerRefusesWithoutARuntime(t *testing.T) {
|
||||
recorder := &providerOutcomeRecorder{}
|
||||
runner := curatorLLMRunner{outcomes: recorder}
|
||||
|
||||
if _, err := runner.Chat(context.Background(), curator.ChatRequest{Model: "openai/gpt-4o"}); err == nil {
|
||||
t.Fatal("Chat with no runtime returned no error")
|
||||
}
|
||||
if _, attempted := recorder.LastChatModelOutcome(); attempted {
|
||||
t.Error("a local misconfiguration was recorded as a provider call")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCuratorRunnerRecordsItsOutcome pins that curator model calls reach
|
||||
// /status. They do not pass through sendChatTurn, so before this a daemon
|
||||
// whose only recent model traffic was curator work reported "no calls
|
||||
// attempted yet" while the provider was demonstrably reachable.
|
||||
func TestCuratorRunnerRecordsItsOutcome(t *testing.T) {
|
||||
recorder := &providerOutcomeRecorder{}
|
||||
// A provider pointed at a closed local port fails fast without touching
|
||||
// the network. The outcome is what matters here: a failed provider call is
|
||||
// exactly what /status must not miss.
|
||||
rt := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: "http://127.0.0.1:1", APIKeyEnv: "ARCHIE_TEST_MISSING_KEY"},
|
||||
})
|
||||
if rt == nil {
|
||||
t.Fatal("NewRuntime returned nil for a configured provider")
|
||||
}
|
||||
runner := curatorLLMRunner{llm: func() *runtime.Runtime { return rt }, outcomes: recorder}
|
||||
|
||||
_, err := runner.Chat(context.Background(), curator.ChatRequest{Model: "openai/gpt-4o"})
|
||||
if err == nil {
|
||||
t.Fatal("Chat against a closed port returned no error")
|
||||
}
|
||||
|
||||
got, attempted := recorder.LastChatModelOutcome()
|
||||
if !attempted {
|
||||
t.Fatal("curator call was not recorded; /status would report no calls attempted")
|
||||
}
|
||||
if got.Model != "openai/gpt-4o" || got.Err == "" {
|
||||
t.Fatalf("outcome = %+v, want the failed curator call", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTitleGenerationDoesNotRecordChatModelHealth is the regression case for
|
||||
// /status blaming the provider for a cosmetic failure. Title generation runs
|
||||
// detached after a first turn under its own 30s bound and its error is
|
||||
// swallowed by the caller, so a title that merely timed out must not become
|
||||
// the process-wide "Chat model: failed" that an operator reads as an outage.
|
||||
func TestTitleGenerationDoesNotRecordChatModelHealth(t *testing.T) {
|
||||
recorder := &providerOutcomeRecorder{}
|
||||
rt := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: "http://127.0.0.1:1", APIKeyEnv: "ARCHIE_TEST_MISSING_KEY"},
|
||||
})
|
||||
if rt == nil {
|
||||
t.Fatal("NewRuntime returned nil for a configured provider")
|
||||
}
|
||||
gen := &chatTitleGenerator{llm: func() *runtime.Runtime { return rt }, chatModels: newChatModelManager(map[string]string{"chat": "openai/gpt-4o"}, nil, nil)}
|
||||
|
||||
if _, err := gen.GenerateTitle(context.Background(), "s1", "hello"); err == nil {
|
||||
t.Fatal("GenerateTitle against a closed port returned no error")
|
||||
}
|
||||
if _, attempted := recorder.LastChatModelOutcome(); attempted {
|
||||
t.Error("a failed title proposal was recorded as chat-model health")
|
||||
}
|
||||
}
|
||||
@@ -1,272 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
||||
)
|
||||
|
||||
// stepVocabularyProbe is a provider set of one, used only to prove the manager
|
||||
// a root builds is live: registering through it must reach its vocabulary.
|
||||
type stepVocabularyProbe struct{}
|
||||
|
||||
func (stepVocabularyProbe) Name() string { return "probe" }
|
||||
|
||||
func (stepVocabularyProbe) StepTypes() []workflow.StepType {
|
||||
return []workflow.StepType{{Name: "probe.registered", Factory: func(yaml.Node) (workflow.Stage, error) {
|
||||
return workflow.Stage{Name: "probe.registered"}, nil
|
||||
}}}
|
||||
}
|
||||
|
||||
// sharedStepTypeNames returns the step types the shared provider set
|
||||
// (internal/infrastructure/workflowsteps) declares. It is read through the same
|
||||
// Providers() seam the composition roots register, rather than from
|
||||
// workflow.BuiltinStepRegistry(): the shipped stages arrive by registration, and
|
||||
// a provider added to that set is the documented way a step type enters every
|
||||
// binary at once.
|
||||
func sharedStepTypeNames() []string {
|
||||
names := make([]string, 0)
|
||||
for _, provider := range workflowsteps.Providers() {
|
||||
for _, stepType := range provider.StepTypes() {
|
||||
names = append(names, stepType.Name)
|
||||
}
|
||||
}
|
||||
slices.Sort(names)
|
||||
return names
|
||||
}
|
||||
|
||||
// requireSharedStepVocabulary holds a composition root's manager to both halves
|
||||
// of the contract: it resolves the shipped vocabulary, and it is live enough to
|
||||
// take a provider registered through it.
|
||||
func requireSharedStepVocabulary(t *testing.T, steps *workflow.Manager) {
|
||||
t.Helper()
|
||||
if steps == nil {
|
||||
t.Fatal("the composition root registered no workflow step vocabulary")
|
||||
}
|
||||
got := steps.StepTypes()
|
||||
// Containment, not equality: adding a provider to the shared set is the
|
||||
// documented way a step type arrives, and that must not have to edit this
|
||||
// guard. What the guard holds is that no shipped stage is missing, which is
|
||||
// what a root regressing to workflow.NewManager() loses.
|
||||
for name := range workflow.BuiltinStepRegistry() {
|
||||
if !slices.Contains(got, name) {
|
||||
t.Errorf("the composition root resolves %v, which is missing the shipped stage %q", got, name)
|
||||
}
|
||||
}
|
||||
// The other half is provenance: every name the root resolves must be
|
||||
// declared by the shared provider set, not by a near-identical provider of
|
||||
// the root's own. That set is what ties this process's vocabulary to the
|
||||
// other side of the contract. It is content, not identity -- the shipped
|
||||
// provider rebuilds its factories on every call -- so a root that stopped
|
||||
// importing the shared set and hand-rolled an equal one would still pass.
|
||||
declared := sharedStepTypeNames()
|
||||
for _, name := range got {
|
||||
if !slices.Contains(declared, name) {
|
||||
t.Errorf("the composition root resolves %q, which no provider in the shared set declares", name)
|
||||
}
|
||||
}
|
||||
if err := steps.Register(stepVocabularyProbe{}); err != nil {
|
||||
t.Fatalf("register a provider through the root's manager: %v", err)
|
||||
}
|
||||
if _, resolved := steps.Registry()["probe.registered"]; !resolved {
|
||||
t.Fatal("a provider registered through the root's manager did not reach its vocabulary")
|
||||
}
|
||||
}
|
||||
|
||||
// TestStepVocabularyRegistersTheSharedProviderSet pins the unit the daemon root
|
||||
// resolves its vocabulary from. Handing this function an empty manager
|
||||
// (workflow.NewManager) would leave the daemon unable to decode the stored
|
||||
// definitions it pins onto tasks.
|
||||
func TestStepVocabularyRegistersTheSharedProviderSet(t *testing.T) {
|
||||
steps, err := stepVocabulary()
|
||||
if err != nil {
|
||||
t.Fatalf("stepVocabulary: %v", err)
|
||||
}
|
||||
requireSharedStepVocabulary(t, steps)
|
||||
}
|
||||
|
||||
// TestArchiedRootsResolveTheStepVocabularyFromThatFunction pins both halves of
|
||||
// each root's wiring: the root reaches the vocabulary through stepVocabulary,
|
||||
// and the manager it returns is the one handed to the constructor that resolves
|
||||
// a step type with it. Neither root can be executed in a unit test -- Run owns
|
||||
// a SQLite file and a listen address, openDaemonWorkflowDefinitions dials the
|
||||
// State Store -- so both are asserted by source position, the way
|
||||
// composition_order_test.go pins the memory-engine ordering for the same
|
||||
// reason. A root that built its own manager, or built one and handed the
|
||||
// constructor a different one, would resolve a vocabulary without the shipped
|
||||
// stages.
|
||||
func TestArchiedRootsResolveTheStepVocabularyFromThatFunction(t *testing.T) {
|
||||
for _, link := range []struct{ file, function, constructor string }{
|
||||
// The validating side: the State Store's control plane server.
|
||||
{"state_store.go", "openStateStoreControlPlane", "NewServer"},
|
||||
// The executing side's client, built by the daemon root alone.
|
||||
{"step_vocabulary.go", "openDaemonWorkflowDefinitions", "NewWorkflowDefinitionsClient"},
|
||||
} {
|
||||
t.Run(link.function+"->stepVocabulary->"+link.constructor, func(t *testing.T) {
|
||||
body := parsedBody(t, link.file, link.function)
|
||||
manager := stepVocabularyAssignment(body)
|
||||
if manager == "" {
|
||||
t.Fatalf("%s does not build its step vocabulary through stepVocabulary()", link.function)
|
||||
}
|
||||
if !callPassesIdent(body, link.constructor, manager) {
|
||||
t.Errorf("%s does not hand the manager stepVocabulary() returned (%s) to %s()", link.function, manager, link.constructor)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestOnlyTheDaemonRootRegistersAStepVocabulary pins the cut the shape applies
|
||||
// to every other process: a root that cannot resolve a step type registers no
|
||||
// vocabulary and cannot be failed by one. openStateStoreAdapter is shared by the
|
||||
// daemon and the gateway, and the gateway resolves no step type -- its
|
||||
// control-plane surfaces are catalog, runtime settings and personas -- so the
|
||||
// vocabulary belongs to the daemon root alone, exactly as
|
||||
// internal/app/archiemessaging's dead vocabulary was removed. Asserted by
|
||||
// source position for the same reason as above.
|
||||
func TestOnlyTheDaemonRootRegistersAStepVocabulary(t *testing.T) {
|
||||
if pos := identCallPosition(parsedBody(t, "bootstrap.go", "openStateStoreAdapter"), "stepVocabulary"); pos != token.NoPos {
|
||||
t.Error("openStateStoreAdapter registers a step vocabulary; it is shared with the gateway root, which never resolves a step type")
|
||||
}
|
||||
if fileMentionsIdent(t, "gateway.go", "stepVocabulary") {
|
||||
t.Error("the gateway root file mentions stepVocabulary; the gateway resolves no workflow step type")
|
||||
}
|
||||
|
||||
// The daemon root opens the state stage -- the shared adapter, the
|
||||
// daemon-only definitions client and the access chain -- through one
|
||||
// call, so the sequence costs Run a single branch. The guarantee is the
|
||||
// one this pin always held, relocated to where the sequence now lives:
|
||||
// adapter first, then the client built on its transport, both before
|
||||
// buildDaemon captures the client.
|
||||
run := parsedBody(t, "main.go", "Run")
|
||||
stage := methodCallPosition(run, "openStateStage")
|
||||
build := methodCallPosition(run, "buildDaemon")
|
||||
if stage == token.NoPos {
|
||||
t.Fatal("Run never opens the daemon's state stage; every task pin would fall back to the shipped definitions")
|
||||
}
|
||||
if build == token.NoPos || stage > build {
|
||||
t.Errorf("Run opens the state stage at %v, after buildDaemon at %v: the daemon would capture no definitions surface", stage, build)
|
||||
}
|
||||
// No half of the stage may also be opened from Run directly: a second
|
||||
// call site would be a copy of the sequence outside the order asserted
|
||||
// below.
|
||||
for _, direct := range []string{"openStateStoreAdapter", "openDaemonWorkflowDefinitions", "openDaemonStateSurfaces", "openAccessChain"} {
|
||||
if pos := methodCallPosition(run, direct); pos != token.NoPos {
|
||||
t.Errorf("Run calls %s directly at %v; the state stage opens through openStateStage alone", direct, pos)
|
||||
}
|
||||
}
|
||||
|
||||
sequence := parsedBody(t, "step_vocabulary.go", "openDaemonStateSurfaces")
|
||||
adapter := methodCallPosition(sequence, "openStateStoreAdapter")
|
||||
wiring := methodCallPosition(sequence, "openDaemonWorkflowDefinitions")
|
||||
if wiring == token.NoPos {
|
||||
t.Fatal("openDaemonStateSurfaces never builds the daemon's workflow-definitions client; every task pin would fall back to the shipped definitions")
|
||||
}
|
||||
if adapter == token.NoPos || wiring < adapter {
|
||||
t.Errorf("openDaemonStateSurfaces builds the workflow-definitions client at %v, at or before openStateStoreAdapter at %v: it would wrap the transport the adapter has not opened yet", wiring, adapter)
|
||||
}
|
||||
}
|
||||
|
||||
func parsedBody(t *testing.T, file, function string) *ast.BlockStmt {
|
||||
t.Helper()
|
||||
parsed, err := parser.ParseFile(token.NewFileSet(), file, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return methodBody(t, parsed, function)
|
||||
}
|
||||
|
||||
// stepVocabularyAssignment returns the identifier assigned from a direct call
|
||||
// to stepVocabulary() in body, or "" when body never calls it.
|
||||
func stepVocabularyAssignment(body *ast.BlockStmt) string {
|
||||
manager := ""
|
||||
ast.Inspect(body, func(node ast.Node) bool {
|
||||
assign, ok := node.(*ast.AssignStmt)
|
||||
if !ok || len(assign.Lhs) == 0 || len(assign.Rhs) == 0 || manager != "" {
|
||||
return true
|
||||
}
|
||||
call, ok := assign.Rhs[0].(*ast.CallExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
callee, ok := call.Fun.(*ast.Ident)
|
||||
if !ok || callee.Name != "stepVocabulary" {
|
||||
return true
|
||||
}
|
||||
if lhs, ok := assign.Lhs[0].(*ast.Ident); ok {
|
||||
manager = lhs.Name
|
||||
}
|
||||
return true
|
||||
})
|
||||
return manager
|
||||
}
|
||||
|
||||
// callPassesIdent reports whether some call to name (bare or selected) passes an
|
||||
// identifier called ident as an argument.
|
||||
func callPassesIdent(body *ast.BlockStmt, name, ident string) bool {
|
||||
found := false
|
||||
ast.Inspect(body, func(node ast.Node) bool {
|
||||
call, ok := node.(*ast.CallExpr)
|
||||
if !ok || !callNameIs(call, name) {
|
||||
return true
|
||||
}
|
||||
for _, argument := range call.Args {
|
||||
if passed, ok := argument.(*ast.Ident); ok && passed.Name == ident {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return found
|
||||
}
|
||||
|
||||
func callNameIs(call *ast.CallExpr, name string) bool {
|
||||
switch fun := call.Fun.(type) {
|
||||
case *ast.Ident:
|
||||
return fun.Name == name
|
||||
case *ast.SelectorExpr:
|
||||
return fun.Sel.Name == name
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// fileMentionsIdent reports whether any declaration in file names ident.
|
||||
func fileMentionsIdent(t *testing.T, file, ident string) bool {
|
||||
t.Helper()
|
||||
parsed, err := parser.ParseFile(token.NewFileSet(), file, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mentions := false
|
||||
ast.Inspect(parsed, func(node ast.Node) bool {
|
||||
if found, ok := node.(*ast.Ident); ok && found.Name == ident {
|
||||
mentions = true
|
||||
}
|
||||
return true
|
||||
})
|
||||
return mentions
|
||||
}
|
||||
|
||||
// identCallPosition returns the position of the first call to a bare function
|
||||
// name in body, or token.NoPos when the body never calls it.
|
||||
func identCallPosition(body *ast.BlockStmt, name string) token.Pos {
|
||||
position := token.NoPos
|
||||
ast.Inspect(body, func(node ast.Node) bool {
|
||||
call, ok := node.(*ast.CallExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
ident, ok := call.Fun.(*ast.Ident)
|
||||
if ok && ident.Name == name && position == token.NoPos {
|
||||
position = call.Pos()
|
||||
}
|
||||
return true
|
||||
})
|
||||
return position
|
||||
}
|
||||
Loaded 100 of 651 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user