Stabilize guarded model delivery
Add validated OpenCode-only delivery capabilities, canonical linked-worktree access checks, isolated candidate verification, and guarded main settlement. Wire the bounded reconnect-foreground controlled HTTP fixture through ordinary server construction so target verification obtains server-controlled provenance and rejects unknown or incompatible fixture evidence. Checked packaged templates and packaged authoring skill references; no updates are needed because this draft-only infrastructure activates no definition. Recorded the stale reference and activation guidance in docs/notes/delivery-stabilization.md for the docs fold.
This commit is contained in:
@@ -0,0 +1,304 @@
|
||||
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")
|
||||
if err := verifyDelivery(context.Background(), item, access, []string{"test -f unlanded"}); err != nil {
|
||||
t.Fatalf("complete unlanded item was not landed after candidate verification: %v", err)
|
||||
}
|
||||
mainHead := runGitOutput(t, main, "rev-parse", "HEAD")
|
||||
itemHead := runGitOutput(t, item, "rev-parse", "HEAD")
|
||||
if mainHead != itemHead {
|
||||
t.Fatal("verified item was not fast-forwarded into canonical main")
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user