Files
archie/internal/plugin/host_contract_test.go
T
sam 19b400bf07 chore(plugin): delete the unwired capability-host health surface (#882)
* chore(plugin): delete the unwired capability-host health surface

plugin.Host.Health, plugin.Host.Manifests, ModuleStatus and their private
safeHealth helper were implemented, exhaustively tested, and called from nowhere
but their own tests. AGENTS.md forbids speculative surfaces, and no settled
design places plugin module status anywhere that exists:

- docs/prds/status-health-surface.md Phase 2 is self-described as "a proposed
  resolution, awaiting Sam's sign-off -- not yet implemented", and does not
  mention plugin status even in that unapproved list.
- The readiness subsystem enumerates exactly five probes -- state_db, config,
  disk, model, gateway (internal/app/archied/readiness.go) -- with no plugin
  probe.
- docs/architecture/plugins-and-extensions.md names no operator-visible plugin
  status surface.
- The only bead naming it is closed.

plugin.Host itself stays: it is the daemon's live capability host, constructed
and Start/Stop-ed from internal/app/archied. Only the unconsumed observability
methods go.

host_contract_test.go encoded the defect -- it asserted plugin.Host *defines*
"Health" and "Manifests" -- so those two names are removed from its list, which
is now exactly the shipped lifecycle: Register, Start, Stop. The three tests
covering the deleted surface are removed with it.

The Yaegi symbol table (internal/plugin/pluginextract) was regenerated rather
than hand-edited, so ModuleStatus leaves the reflected type set with it.

Reachability evidence: Host.Health and Host.Manifests appear only in their own
tests; plugin.Host is otherwise reached from bootstrap.go and gateway.go for
Register/Start/Stop. Note that "task reachability" does NOT flag these -- the
address-taken constructor is a known blind spot of the whole-program RTA.

* chore(plugin): delete the dead Module.Health chain wired to nothing

Extends the capability-host observability deletion: plugin.Module.Health
was a required interface method with real implementations in
tools/provider/mcp and tools/provider/builtin, aggregated by
tools/provider.Registry.Health via a provider-package-local safeHealth,
and backed by per-module LifecycleState bookkeeping in plugin.Host
(registeredModule.state, setModuleState). None of it had a caller before
this change -- Host.Health was the only thing that would ever have read
a registered module's Health(), and Host.Health was already deleted as
unreachable. registeredModule.state was write-only: nothing ever read it.

Deleted:
- plugin.Module.Health, plugin.Health/HealthStatus (+ consts),
  plugin.LifecycleState (+ consts), legacyModule.Health,
  registeredModule.state, Host.setModuleState and its call sites.
- tools/provider.Registry.Health, tools/provider.safeHealth.
- tools/provider/mcp.Provider.Health, tools/provider/builtin.Provider.Health.
- Tests exclusively covering the above, and fakes' now-unnecessary Health
  methods (plugin/host_test.go, pluginextract/reachability_test.go,
  tools/provider/{provider,optional}_test.go, tools/provider/mcp/provider_test.go,
  app/agentworker/mcp_providers_test.go).

internal/plugin/pluginextract's generated file was regenerated via
`go generate ./internal/plugin/...` and the yaegi wrapper nil-guards
(dropped by fresh generation, as documented in reachability_test.go)
were reapplied by hand to WManifest/WStart/WStop/WName/WVersion,
following this repo's established post-regen pattern.
2026-09-19 14:10:39 +10:00

176 lines
4.5 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(",
"CapabilityHost:",
} {
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")
}
if strings.Contains(wiring, "PluginRegistry:") {
t.Error("daemon composition exposes divergent legacy plugin inventory")
}
}
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 ""
}