532ba48927
A fresh run started from a repository's main checkout gets its own worktree and branch next to the repository, so runs never share a tree or see the operator's uncommitted work, and several can run at once. Retries and runs started in a linked worktree keep their directory. A clean, landed run tree is removed after success; anything else is kept. The delivery gate commits what the land stage left uncommitted in a run worktree, and no longer requires the operator's main checkout to be clean: Git's fast-forward refuses safely when a local change conflicts.
415 lines
16 KiB
Go
415 lines
16 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
)
|
|
|
|
func TestLandingAccessAcceptsOnlyVerifiedSameRepositoryMainWorktree(t *testing.T) {
|
|
root := t.TempDir()
|
|
main := filepath.Join(root, "main")
|
|
runGit(t, root, "init", "-b", "main", main)
|
|
runGit(t, main, "config", "user.email", "test@example.invalid")
|
|
runGit(t, main, "config", "user.name", "test")
|
|
if err := os.WriteFile(filepath.Join(main, "README"), []byte("base\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, main, "add", "README")
|
|
runGit(t, main, "commit", "-m", "base")
|
|
item := filepath.Join(root, "item")
|
|
runGit(t, main, "worktree", "add", "-b", "item", item)
|
|
access, err := landingAccess(item)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
canonicalMain, err := canonicalDirectory(main)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if access.MainWorktree != canonicalMain || access.GitMetadata != filepath.Join(canonicalMain, ".git") || access.LockPath != filepath.Join(canonicalMain, ".git", "pipelines-landing.lock") {
|
|
t.Fatalf("unexpected access: %+v", access)
|
|
}
|
|
foreign := filepath.Join(root, "foreign")
|
|
runGit(t, root, "init", "-b", "main", foreign)
|
|
if _, err := landingAccess(foreign); err == nil {
|
|
t.Fatal("foreign repository without an item worktree gained landing access")
|
|
}
|
|
if _, err := landingAccess(main); err == nil {
|
|
t.Fatal("main worktree gained linked-worktree delivery access")
|
|
}
|
|
if err := os.RemoveAll(item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, main, "worktree", "prune")
|
|
if _, err := landingAccess(main); err == nil {
|
|
t.Fatal("repository without a linked delivery worktree gained landing access")
|
|
}
|
|
secondItem := filepath.Join(root, "second-item")
|
|
runGit(t, main, "worktree", "add", "-b", "second-item", secondItem)
|
|
runGit(t, main, "branch", "-m", "main", "trunk")
|
|
if _, err := landingAccess(secondItem); err == nil {
|
|
t.Fatal("repository without a canonical main worktree gained landing access")
|
|
}
|
|
}
|
|
|
|
func TestVerifyDeliveryRequiresCleanLandedMainAndPassingChecks(t *testing.T) {
|
|
root := t.TempDir()
|
|
main := filepath.Join(root, "main")
|
|
runGit(t, root, "init", "-b", "main", main)
|
|
runGit(t, main, "config", "user.email", "test@example.invalid")
|
|
runGit(t, main, "config", "user.name", "test")
|
|
if err := os.WriteFile(filepath.Join(main, "README"), []byte("base\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, main, "add", "README")
|
|
runGit(t, main, "commit", "-m", "base")
|
|
item := filepath.Join(root, "item")
|
|
runGit(t, main, "worktree", "add", "-b", "item", item)
|
|
if err := os.WriteFile(filepath.Join(item, "delivered"), []byte("complete\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, item, "add", "delivered")
|
|
runGit(t, item, "commit", "-m", "delivery")
|
|
runGit(t, main, "merge", "--ff-only", "item")
|
|
access, err := landingAccess(item)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := verifyDelivery(context.Background(), item, access, []string{"test -f delivered"}, ""); err != nil {
|
|
t.Fatalf("complete landed delivery rejected: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(item, "missing-helper"), []byte("dirty\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := verifyDelivery(context.Background(), item, access, []string{"true"}, ""); err == nil {
|
|
t.Fatal("dirty task-owned helper was accepted")
|
|
}
|
|
if err := os.Remove(filepath.Join(item, "missing-helper")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := verifyDelivery(context.Background(), item, access, []string{"false"}, ""); err == nil {
|
|
t.Fatal("failed required check was accepted")
|
|
}
|
|
if err := os.WriteFile(filepath.Join(item, "unlanded"), []byte("no\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, item, "add", "unlanded")
|
|
runGit(t, item, "commit", "-m", "unlanded")
|
|
committed := runGitOutput(t, item, "rev-parse", "HEAD")
|
|
note, err := verifyDelivery(context.Background(), item, access, []string{"test -f unlanded"}, "")
|
|
if err != nil {
|
|
t.Fatalf("complete unlanded item was not landed after candidate verification: %v", err)
|
|
}
|
|
if note != "" {
|
|
t.Fatalf("fast-forwardable item reported a catch-up merge: %s", note)
|
|
}
|
|
mainHead := runGitOutput(t, main, "rev-parse", "HEAD")
|
|
itemHead := runGitOutput(t, item, "rev-parse", "HEAD")
|
|
if mainHead != itemHead || itemHead != committed {
|
|
t.Fatal("verified item was not fast-forwarded into canonical main unchanged")
|
|
}
|
|
}
|
|
|
|
// newDeliveryRepo creates a canonical main worktree with one base commit and a
|
|
// linked item worktree on branch item.
|
|
func newDeliveryRepo(t *testing.T) (main, item string) {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
main = filepath.Join(root, "main")
|
|
runGit(t, root, "init", "-b", "main", main)
|
|
runGit(t, main, "config", "user.email", "test@example.invalid")
|
|
runGit(t, main, "config", "user.name", "test")
|
|
commitFile(t, main, "README", "base\n")
|
|
item = filepath.Join(root, "item")
|
|
runGit(t, main, "worktree", "add", "-b", "item", item)
|
|
return main, item
|
|
}
|
|
|
|
func commitFile(t *testing.T, directory, name, content string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(directory, name), []byte(content), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, directory, "add", name)
|
|
runGit(t, directory, "commit", "-m", "change "+name)
|
|
}
|
|
|
|
func TestVerifyDeliveryCatchesUpWithUnrelatedMainChange(t *testing.T) {
|
|
main, item := newDeliveryRepo(t)
|
|
commitFile(t, item, "work", "item\n")
|
|
itemBefore := runGitOutput(t, item, "rev-parse", "HEAD")
|
|
commitFile(t, main, "other", "concurrent\n")
|
|
mainBefore := runGitOutput(t, main, "rev-parse", "HEAD")
|
|
access, err := landingAccess(item)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The check only passes on the merged tree, proving it ran after the merge.
|
|
note, err := verifyDelivery(context.Background(), item, access, []string{"test -f work && test -f other"}, "")
|
|
if err != nil {
|
|
t.Fatalf("cleanly mergeable item was rejected: %v", err)
|
|
}
|
|
if !strings.Contains(note, "merged") {
|
|
t.Fatalf("catch-up merge was not reported: %q", note)
|
|
}
|
|
itemHead := runGitOutput(t, item, "rev-parse", "HEAD")
|
|
if runGitOutput(t, main, "rev-parse", "HEAD") != itemHead {
|
|
t.Fatal("canonical main was not fast-forwarded to the merge")
|
|
}
|
|
parents := strings.Fields(runGitOutput(t, item, "rev-list", "--parents", "-n", "1", "HEAD"))
|
|
if len(parents) != 3 || parents[1] != itemBefore || parents[2] != mainBefore {
|
|
t.Fatalf("expected a merge of item %s and main %s, got %v", itemBefore, mainBefore, parents)
|
|
}
|
|
if status := runGitOutput(t, item, "status", "--porcelain"); status != "" {
|
|
t.Fatalf("item workspace is not clean: %s", status)
|
|
}
|
|
}
|
|
|
|
func TestVerifyDeliveryAbortsConflictingCatchUp(t *testing.T) {
|
|
main, item := newDeliveryRepo(t)
|
|
commitFile(t, item, "README", "item\n")
|
|
itemBefore := runGitOutput(t, item, "rev-parse", "HEAD")
|
|
commitFile(t, main, "README", "main\n")
|
|
mainBefore := runGitOutput(t, main, "rev-parse", "HEAD")
|
|
access, err := landingAccess(item)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = verifyDelivery(context.Background(), item, access, []string{"true"}, "")
|
|
if err == nil || !strings.Contains(err.Error(), "main moved and the item no longer merges cleanly: README") {
|
|
t.Fatalf("conflicting catch-up did not fail clearly: %v", err)
|
|
}
|
|
if runGitOutput(t, item, "rev-parse", "HEAD") != itemBefore {
|
|
t.Fatal("conflicting catch-up changed the item commit")
|
|
}
|
|
if status := runGitOutput(t, item, "status", "--porcelain", "--untracked-files=all"); status != "" {
|
|
t.Fatalf("conflicting catch-up left the workspace dirty: %s", status)
|
|
}
|
|
if exec.Command("git", "-C", item, "rev-parse", "-q", "--verify", "MERGE_HEAD").Run() == nil {
|
|
t.Fatal("conflicting catch-up left a merge in progress")
|
|
}
|
|
if runGitOutput(t, main, "rev-parse", "HEAD") != mainBefore {
|
|
t.Fatal("conflicting catch-up changed canonical main")
|
|
}
|
|
}
|
|
|
|
func TestVerifyDeliveryFailingChecksAfterCatchUpLeaveMainUnchanged(t *testing.T) {
|
|
main, item := newDeliveryRepo(t)
|
|
commitFile(t, item, "work", "item\n")
|
|
commitFile(t, main, "other", "concurrent\n")
|
|
mainBefore := runGitOutput(t, main, "rev-parse", "HEAD")
|
|
access, err := landingAccess(item)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Passes on the item tree alone, fails on the merged tree.
|
|
_, err = verifyDelivery(context.Background(), item, access, []string{"test -f work", "test ! -f other"}, "")
|
|
if err == nil || !strings.Contains(err.Error(), "merged") || !strings.Contains(err.Error(), "test ! -f other") {
|
|
t.Fatalf("failing merged-tree check did not fail with catch-up context: %v", err)
|
|
}
|
|
if runGitOutput(t, main, "rev-parse", "HEAD") != mainBefore {
|
|
t.Fatal("failing merged-tree check changed canonical main")
|
|
}
|
|
if status := runGitOutput(t, main, "status", "--porcelain", "--untracked-files=all"); status != "" {
|
|
t.Fatalf("canonical main checkout changed: %s", status)
|
|
}
|
|
}
|
|
|
|
func TestVerifyDeliveryFailingCandidateLeavesMainUnchanged(t *testing.T) {
|
|
root := t.TempDir()
|
|
main := filepath.Join(root, "main")
|
|
runGit(t, root, "init", "-b", "main", main)
|
|
runGit(t, main, "config", "user.email", "test@example.invalid")
|
|
runGit(t, main, "config", "user.name", "test")
|
|
if err := os.WriteFile(filepath.Join(main, "README"), []byte("base\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, main, "add", "README")
|
|
runGit(t, main, "commit", "-m", "base")
|
|
item := filepath.Join(root, "item")
|
|
runGit(t, main, "worktree", "add", "-b", "item", item)
|
|
if err := os.WriteFile(filepath.Join(item, "candidate"), []byte("candidate\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, item, "add", "candidate")
|
|
runGit(t, item, "commit", "-m", "candidate")
|
|
access, err := landingAccess(item)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := runGitOutput(t, main, "rev-parse", "HEAD")
|
|
if _, err := verifyDelivery(context.Background(), item, access, []string{"test -f candidate", "false"}, ""); err == nil {
|
|
t.Fatal("failing candidate check landed main")
|
|
}
|
|
if after := runGitOutput(t, main, "rev-parse", "HEAD"); after != before {
|
|
t.Fatal("failing candidate check changed canonical main")
|
|
}
|
|
}
|
|
|
|
func TestRunnerSettlesSuccessfulDeliveryAfterCandidateLanding(t *testing.T) {
|
|
ctx := context.Background()
|
|
root := t.TempDir()
|
|
main := filepath.Join(root, "main")
|
|
runGit(t, root, "init", "-b", "main", main)
|
|
runGit(t, main, "config", "user.email", "test@example.invalid")
|
|
runGit(t, main, "config", "user.name", "test")
|
|
if err := os.WriteFile(filepath.Join(main, "README"), []byte("base\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, main, "add", "README")
|
|
runGit(t, main, "commit", "-m", "base")
|
|
item := filepath.Join(root, "item")
|
|
runGit(t, main, "worktree", "add", "-b", "item", item)
|
|
if err := os.WriteFile(filepath.Join(item, "unlanded"), []byte("no\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, item, "add", "unlanded")
|
|
runGit(t, item, "commit", "-m", "unlanded")
|
|
|
|
store, err := pipeline.Open(filepath.Join(root, "runner.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
source := pipeline.Template([]string{"committer"}, true)
|
|
source = strings.Replace(source, "```configs", "```checks\ntrue\n```\n\n```configs", 1)
|
|
source = strings.Replace(source, "timeout=1h", "timeout=1h\ndelivery=true", 1)
|
|
if _, err := store.Submit(ctx, "delivery", source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, err := newRunner(ctx, store)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
|
|
}
|
|
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "unlanded", Workspace: item, Input: map[string]any{"prompt": "task"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
detail, err := store.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if detail.Status != "succeeded" || len(detail.Steps) != 1 {
|
|
t.Fatalf("complete delivery did not settle successfully: %+v", detail)
|
|
}
|
|
if runGitOutput(t, main, "rev-parse", "HEAD") != runGitOutput(t, item, "rev-parse", "HEAD") {
|
|
t.Fatal("runner did not land the verified item commit")
|
|
}
|
|
}
|
|
|
|
func TestRunnerRejectsWrongBackendVerificationEvidence(t *testing.T) {
|
|
ctx := context.Background()
|
|
source := pipeline.Template([]string{"worker"}, true)
|
|
source = strings.Replace(source, "```configs", "```verification\nbackend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture=reconnect-foreground\n```\n\n```configs", 1)
|
|
r, store, id := submitRunner(t, ctx, source)
|
|
defer store.Close()
|
|
r.verifyTarget = func(context.Context, stage.VerificationTarget) (stage.VerificationEvidence, error) {
|
|
return stage.VerificationEvidence{Backend: "old-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}, nil
|
|
}
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
|
|
}
|
|
run := startRun(t, r, id, "wrong-backend-evidence")
|
|
detail, err := store.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if detail.Status != "failed" || len(detail.Steps) != 1 || !strings.Contains(detail.Steps[0].Diagnostic, "backend") {
|
|
t.Fatalf("wrong-backend evidence settled as success: %+v", detail)
|
|
}
|
|
}
|
|
|
|
func TestRunnerIgnoresModelSuppliedVerificationEvidence(t *testing.T) {
|
|
ctx := context.Background()
|
|
source := pipeline.Template([]string{"worker"}, true)
|
|
source = strings.Replace(source, "```configs", "```verification\nbackend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture=reconnect-foreground\n```\n\n```configs", 1)
|
|
r, store, id := submitRunner(t, ctx, source)
|
|
defer store.Close()
|
|
r.verifyTarget = func(context.Context, stage.VerificationTarget) (stage.VerificationEvidence, error) {
|
|
return stage.VerificationEvidence{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}, nil
|
|
}
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"verification": map[string]any{"backend": "old-api", "revision": "stale", "capability": "invented", "fixture": "other"}}}, nil
|
|
}
|
|
run := startRun(t, r, id, "model-supplied-evidence")
|
|
detail, err := store.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if detail.Status != "succeeded" || len(detail.Steps) != 1 {
|
|
t.Fatalf("controlled verification did not settle: %+v", detail)
|
|
}
|
|
}
|
|
|
|
func TestMainWorktreeRejectsNearMatchAndLinkedMain(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
branch string
|
|
}{
|
|
{name: "mainline is not main", branch: "mainline"},
|
|
{name: "linked main is not canonical", branch: "main"},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
root := t.TempDir()
|
|
primary := filepath.Join(root, "primary")
|
|
runGit(t, root, "init", "-b", "trunk", primary)
|
|
runGit(t, primary, "config", "user.email", "test@example.invalid")
|
|
runGit(t, primary, "config", "user.name", "test")
|
|
if err := os.WriteFile(filepath.Join(primary, "README"), []byte("base\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, primary, "add", "README")
|
|
runGit(t, primary, "commit", "-m", "base")
|
|
item := filepath.Join(root, "item")
|
|
runGit(t, primary, "worktree", "add", "-b", "item", item)
|
|
candidate := filepath.Join(root, test.branch)
|
|
runGit(t, primary, "worktree", "add", "-b", test.branch, candidate)
|
|
|
|
commonDir, err := gitOutput(item, "rev-parse", "--git-common-dir")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
commonDir, err = canonicalDirectory(resolveGitPath(item, commonDir))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := mainWorktree(item, commonDir); err == nil {
|
|
t.Fatalf("%s gained canonical main access", test.name)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func runGit(t *testing.T, directory string, args ...string) {
|
|
t.Helper()
|
|
cmd := exec.Command("git", append([]string{"-C", directory}, args...)...)
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v: %v: %s", args, err, output)
|
|
}
|
|
}
|
|
|
|
func runGitOutput(t *testing.T, directory string, args ...string) string {
|
|
t.Helper()
|
|
output, err := exec.Command("git", append([]string{"-C", directory}, args...)...).Output()
|
|
if err != nil {
|
|
t.Fatalf("git %v: %v", args, err)
|
|
}
|
|
return strings.TrimSpace(string(output))
|
|
}
|