Files
archie/internal/plugin/host_contract_test.go
T
sam 0e37a0f536 refactor(daemon): drop assigned-but-unread composition handles
Three exported Daemon fields were wired by the composition root and read
nowhere: CapabilityHost (*plugin.Host), Curators (*curator.Registry) and
ToolRegistry (*tools.Registry). No field access exists anywhere in the
repo, and git history shows no read ever existed.

Each handle has a live owner outside the daemon: the curator
registry/runtime is boot-owned (the curator isolation invariant keeps it
off the agent loop), the capability host is lifecycle-owned by the
composition root and consumed by the control plane, and the tool registry
is consumed by chat, curator and MCP wiring. Keeping the daemon copies was
decorative wiring, so the fields, their composition assignments and the
now-unused plugin/curator imports go together.

The daemon architecture test now refuses the three names, and the plugin
host contract test refuses a CapabilityHost: handle on the daemon alongside
the PluginRegistry: one it already refused.
2026-10-02 12:57:53 +10:00

181 lines
4.8 KiB
Go

package plugin_test
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
func TestCapabilityHostContractExists(t *testing.T) {
t.Parallel()
pluginDir := filepath.Clean(".")
entries, err := os.ReadDir(pluginDir)
if err != nil {
t.Fatal(err)
}
types := make(map[string]bool)
hostMethods := make(map[string]bool)
functions := make(map[string]bool)
for _, entry := range entries {
if entry.IsDir() || filepath.Ext(entry.Name()) != ".go" || strings.HasSuffix(entry.Name(), "_test.go") {
continue
}
file, err := parser.ParseFile(token.NewFileSet(), filepath.Join(pluginDir, entry.Name()), nil, 0)
if err != nil {
t.Fatal(err)
}
for _, decl := range file.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
for _, spec := range decl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if ok {
types[typeSpec.Name.Name] = true
}
}
case *ast.FuncDecl:
if receiverNameForContract(decl) == "Host" {
hostMethods[decl.Name.Name] = true
}
if decl.Recv == nil {
functions[decl.Name.Name] = true
}
}
}
}
for _, name := range []string{"Manifest", "Module", "Host"} {
if !types[name] {
t.Errorf("plugin package does not define %s", name)
}
}
for _, name := range []string{"Register", "Start", "Stop"} {
if !hostMethods[name] {
t.Errorf("plugin.Host does not define %s", name)
}
}
if !functions["AdaptLegacy"] {
t.Error("plugin package does not define AdaptLegacy")
}
}
func TestCapabilityHostContractHasNoGenericCapabilityHooks(t *testing.T) {
t.Parallel()
disallowed := []string{"AddHook", "RegisterHook", "RegisterCapability", "Service", "Services"}
slices.Sort(disallowed)
pluginType := parseNamedInterface(t, "plugin.go", "Plugin")
for _, field := range pluginType.Methods.List {
for _, name := range field.Names {
if _, found := slices.BinarySearch(disallowed, name.Name); found {
t.Errorf("plugin.Plugin contains forbidden generic hook %s", name.Name)
}
}
}
}
func TestArchiedWiresCapabilityHostLifecycle(t *testing.T) {
t.Parallel()
// The daemon composition lives in internal/app/archied, whose wiring is
// split across main.go (driver) and bootstrap.go (phase methods); the
// contract reads the whole package so the enforcement survives that
// split.
archiedDir := filepath.Join("..", "..", "internal", "app", "archied")
var text strings.Builder
entries, err := os.ReadDir(archiedDir)
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
if entry.IsDir() || filepath.Ext(entry.Name()) != ".go" || strings.HasSuffix(entry.Name(), "_test.go") {
continue
}
source, err := os.ReadFile(filepath.Join(archiedDir, entry.Name()))
if err != nil {
t.Fatal(err)
}
text.Write(source)
text.WriteString("\n")
}
wiring := text.String()
for _, required := range []string{
"plugin.NewHost()",
"plugin.AdaptLegacy(",
"capabilityHost.Register(",
"capabilityHost.Start(",
"capabilityHost.Stop(",
} {
if !strings.Contains(wiring, required) {
t.Errorf("cmd/archied does not contain capability-host wiring %q", required)
}
}
before, _, ok := strings.Cut(wiring, "capabilityHost.Start(")
if !ok {
t.Fatal("capability host Start call not found")
}
if !strings.Contains(before, "capabilityHost.Stop(") {
t.Error("capability host cleanup must be registered before Start so failed rollback cleanup is retried")
}
// The host lifecycle belongs to the composition root, which the control
// plane and gateway consume. A CapabilityHost field on the daemon would be
// assigned there and read nowhere -- the same defect PluginRegistry: had --
// so both spellings are refused.
for _, forbidden := range []string{"PluginRegistry:", "CapabilityHost:"} {
if strings.Contains(wiring, forbidden) {
t.Errorf("daemon composition carries an unread capability handle %q; the composition root owns the host lifecycle", forbidden)
}
}
}
func parseNamedInterface(t *testing.T, filename, typeName string) *ast.InterfaceType {
t.Helper()
file, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
if err != nil {
t.Fatal(err)
}
for _, decl := range file.Decls {
gen, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range gen.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok || typeSpec.Name.Name != typeName {
continue
}
iface, ok := typeSpec.Type.(*ast.InterfaceType)
if !ok {
t.Fatalf("%s is not an interface", typeName)
}
return iface
}
}
t.Fatalf("%s not found in %s", typeName, filename)
return nil
}
func receiverNameForContract(decl *ast.FuncDecl) string {
if decl.Recv == nil || len(decl.Recv.List) == 0 {
return ""
}
switch receiver := decl.Recv.List[0].Type.(type) {
case *ast.Ident:
return receiver.Name
case *ast.StarExpr:
ident, _ := receiver.X.(*ast.Ident)
if ident != nil {
return ident.Name
}
}
return ""
}