Files
archie/internal/daemon/architecture_test.go
T
sam 92ea5dee56 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

172 lines
5.5 KiB
Go

package daemon
import (
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"strconv"
"strings"
"testing"
)
const workflowImportPath = "github.com/samcharles93/archie-core/internal/domain/workflow"
// Autonomous workflow execution belongs exclusively to the task-scoped
// archie-agent worker. A local workflow.Run call in daemon silently restores
// the host-authority fallback this boundary exists to remove.
func TestDaemonNeverExecutesWorkflowLocally(t *testing.T) {
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
}
workflowNames := make(map[string]bool)
dotImport := false
for _, spec := range file.Imports {
importPath, unquoteErr := strconv.Unquote(spec.Path.Value)
if unquoteErr != nil || importPath != workflowImportPath {
continue
}
switch {
case spec.Name == nil:
workflowNames["workflow"] = true
case spec.Name.Name == ".":
dotImport = true
case spec.Name.Name != "_":
workflowNames[spec.Name.Name] = true
}
}
ast.Inspect(file, func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
if dotImport {
if ident, ok := call.Fun.(*ast.Ident); ok && ident.Name == "Run" {
reportLocalWorkflowRun(t, fileset, call)
}
}
selector, ok := call.Fun.(*ast.SelectorExpr)
if !ok || selector.Sel.Name != "Run" {
return true
}
pkg, ok := selector.X.(*ast.Ident)
if ok && workflowNames[pkg.Name] {
reportLocalWorkflowRun(t, fileset, call)
}
return true
})
}
}
func reportLocalWorkflowRun(t *testing.T, fileset *token.FileSet, call *ast.CallExpr) {
t.Helper()
position := fileset.Position(call.Pos())
t.Errorf("%s calls workflow.Run; autonomous workflows must use the full-task worker handoff", position)
}
// Daemon and IdentityRunner carry routing and authority state only. Keeping a
// stage runner or workflow registry on either type makes a host execution path
// one call away from returning.
func TestDaemonCarriesNoLocalWorkflowExecutionState(t *testing.T) {
file, err := parser.ParseFile(token.NewFileSet(), "daemon.go", nil, 0)
if err != nil {
t.Fatal(err)
}
forbiddenExecution := map[string]bool{"Agent": true, "Workflows": true, "CustomStages": true}
// Composition handles the daemon must not carry. Each was an exported field
// the composition root assigned and nothing read: 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/MCP wiring. A re-add is decorative wiring.
forbiddenHandles := map[string]bool{"Curators": true, "CapabilityHost": true, "ToolRegistry": true}
for _, declaration := range file.Decls {
gen, ok := declaration.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range gen.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok || (typeSpec.Name.Name != "Daemon" && typeSpec.Name.Name != "IdentityRunner") {
continue
}
structure, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
for _, field := range structure.Fields.List {
for _, name := range field.Names {
if forbiddenExecution[name.Name] {
t.Errorf("%s carries local workflow execution field %s", typeSpec.Name.Name, name.Name)
}
if forbiddenHandles[name.Name] {
t.Errorf("%s carries unread composition handle %s; the composition root owns it", typeSpec.Name.Name, name.Name)
}
}
}
}
}
}
// A repository's base branch has exactly one default and Repo.BaseBranch() is
// where it lives: an empty base means "main". Handing the raw field to the
// worktree manager skips that mapping, so a repository stored with no base
// reaches git with "" and the branch is never cut from main
// (archie-core-lwzd).
//
// This is a wiring invariant rather than a behaviour test because the value is
// an argument: the worktree manager's own tests cover what it does with a base,
// and a daemon-level test would need a real clone to reach the call.
func TestDaemonPreparesWorktreesFromTheMappedBaseBranch(t *testing.T) {
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatal(err)
}
fileset := token.NewFileSet()
checked := 0
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
}
ast.Inspect(file, func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
// Prepare carries owner, repo, base, issue, title, body, labels and
// the daemon's prepare target: nine arguments.
if !ok || len(call.Args) != 9 {
return true
}
selector, ok := call.Fun.(*ast.SelectorExpr)
if !ok || selector.Sel.Name != "Prepare" {
return true
}
checked++
base, ok := call.Args[3].(*ast.SelectorExpr)
if ok && base.Sel.Name == "Base" {
t.Errorf("%s: worktree.Prepare receives the raw base field; pass BaseBranch() so an empty base means main",
fileset.Position(call.Pos()))
}
return true
})
}
// A rename or a move must not leave this passing without having looked.
if checked == 0 {
t.Error("no worktree.Prepare call found, so the base-branch invariant went unchecked")
}
}