prepareWorkspace chose between Manager.Prepare (refresh onto base) and a separate Manager.Resume, and Manager.Resume only opened an already-prepared clone. A remediation whose local worktree the terminal cleanup removed, an expired volume dropped, or a different host never had could therefore never be resumed, and the retry path had no way to select the mode that continues pushed work. Unify the two matched methods into Prepare(..., Target): Fresh resets onto origin/<base>, a non-empty Target resets onto origin/<that branch>, and either target clones a missing worktree. The daemon's retry path selects the target from the task row and passes the persisted branch, so a resume lands on the branch the open PR lives on and fails closed when it is not on the remote. Delete the container-mode no-op resume (hybridTrees.Resume and the resumableTrees capability) now that the daemon owns it. The remediate workflow no longer stages a resume, and the retired remediate.resume step word stays resolvable as an inert stage: a stored definition is validated as a whole collection, so a removed word would fail every workflow to decode, not just its own.
162 lines
4.8 KiB
Go
162 lines
4.8 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)
|
|
// 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")
|
|
}
|
|
}
|