Files
archie/internal/daemon/architecture_test.go
T
sam 3d49d919d9 fix(daemon): cut worktrees from the mapped base branch
Repo.BaseBranch() is where an empty base means "main", and the daemon
handed the raw field to worktree.Prepare -- the one call site that did,
beside the workflow step that passes the mapped value. A repository stored
with no base therefore reached git with "" and its branch was never cut
from main.

Reachable now because the document editor stopped prefilling base (its
per-path skeletons retired in aa650e2c), which had been masking the missing
mapping rather than fixing it (archie-core-lwzd).

The guard is a wiring invariant in architecture_test.go: it walks the
package for the worktree.Prepare call and refuses the raw field, and fails
if the call is gone rather than passing unchecked.
2026-09-22 04:00:55 +10:00

160 lines
4.7 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)
}
forbidden := map[string]bool{"Agent": true, "Workflows": true, "CustomStages": 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 forbidden[name.Name] {
t.Errorf("%s carries local workflow execution field %s", 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)
if !ok || len(call.Args) != 8 {
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")
}
}