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:
2026-10-18 14:50:18 +01:00
parent 66b5f60b28
commit 2cdb9a9e93
15 changed files with 1335 additions and 5 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ func main() {
// Each new run gets a bounded one-line title from the configured default
// harness; the summariser falls back to the prompt's first line, so title
// generation never blocks or fails run creation.
options := server.Options{ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason}
options := server.Options{ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason, VerificationFixtures: server.VerificationFixturesFromEnvironment(os.Getenv)}
recorded := ""
if value, err := store.OpenCodeEndpoint(ctx); err != nil {
log.Printf("OpenCode endpoint setting unreadable: %v", err)
+47
View File
@@ -0,0 +1,47 @@
# Delivery stabilization
Date: 2026-10-18.
The server now has draft-only `delivery=true` support for OpenCode model stages.
At run time it derives the canonical `main` worktree and linked-worktree Git
metadata from Git rather than accepting paths from a stage prompt. It verifies
the committed item tree in a disposable candidate worktree before fast-forwarding
canonical `main`; declared checks are bounded configuration, not proof that any
deployed definition has used them. It is denied for Pi (until equivalent
capability enforcement exists), foreign repositories, missing main worktrees,
and malformed capabilities. This item does not publish or deploy a definition.
The source also supports an optional controlled-fixture verification declaration
(`backend`, `revision`, `capability`, and `fixture`). Ordinary server setup wires
an explicit, bounded `reconnect-foreground` HTTP fixture registry when
`PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_URL` and its expected `BACKEND`,
`REVISION`, `CAPABILITY`, and `STATE` values are configured. There is no default
remote URL. The server, not the model, probes that registered endpoint and
collects its returned backend identity, build revision, capability, fixture ID,
and state before comparing provenance. Unknown fixtures, incomplete setup,
wrong backend/revision/capability/state, non-200 responses, cancellation, and
oversized or malformed responses fail closed. A model result's labels are not
evidence. This is controlled local/backend fixture infrastructure, not evidence
of a rendered layout or a live Android device.
Definitions must be scratch-validated against the deployed server only after
the server support is deployed; no future delivery or verification definition
is activated by this source change.
`internal/author/skill/references/format.md` and
`internal/author/skill/references/queue.md` are stale: a later docs fold should
describe `delivery=true`, OpenCode-only availability, candidate verification,
and the required activation order (deploy server support before scratch-validating
and then publishing a definition).
OpenCode V2 resolves external resources at a containing directory, so delivery
uses the verified repository metadata directory rather than an impossible
single-lock-file scope. It does not grant a parent, home, foreign repository,
or global policy. Unrestricted shell permissions are not an OS sandbox.
Activation remains draft-only: deploy compatible server support, configure the
controlled fixture endpoint and matching provenance, scratch-validate the
fixture, then publish a definition. No definition is activated by this source
change.
Recovery checklist: retain `connection-reconnect` and unfinished feature
branches; merge only complete committed trees that pass mapped checks; do not
infer delivery from a dirty worktree or an old deployed-backend capture.
+185
View File
@@ -0,0 +1,185 @@
package server
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// verifyDelivery proves that the stage landed a complete, committed tree before
// its success can settle the run. It re-derives the capability after the stage:
// a stage result and its input cannot select either the landing path or checks.
const maxDeliveryCheckOutput = 64 << 10
const deliveryCleanupTimeout = 10 * time.Second
// verifyDelivery verifies the committed item tree in a disposable worktree
// before fast-forwarding canonical main. Settlement deliberately performs only
// non-mutating repository checks after that mutation: a declared check must
// never be the first signal that the landed tree is known-red.
func verifyDelivery(ctx context.Context, workspace string, access stage.LandingAccess, checks []string) error {
lock, err := acquireDeliveryLock(ctx, access.LockPath)
if err != nil {
return err
}
defer lock.Close()
current, err := landingAccess(workspace)
if err != nil {
return fmt.Errorf("delivery access changed: %w", err)
}
if current != access {
return fmt.Errorf("delivery access changed during stage")
}
for _, directory := range []string{workspace, access.MainWorktree} {
status, err := gitOutput(directory, "status", "--porcelain=v1", "--untracked-files=all")
if err != nil {
return fmt.Errorf("delivery checkout status: %w", err)
}
if status != "" {
return fmt.Errorf("delivery checkout is not clean: %s", strings.Split(status, "\n")[0])
}
}
itemHead, err := gitOutput(workspace, "rev-parse", "HEAD")
if err != nil {
return fmt.Errorf("delivery item commit: %w", err)
}
if err := runDeliveryCandidate(ctx, workspace, itemHead, checks); err != nil {
return err
}
mainHead, err := gitOutput(access.MainWorktree, "rev-parse", "HEAD")
if err != nil {
return fmt.Errorf("delivery main commit: %w", err)
}
if err := exec.CommandContext(ctx, "git", "-C", workspace, "merge-base", "--is-ancestor", mainHead, itemHead).Run(); err != nil {
return fmt.Errorf("delivery item commit cannot fast-forward canonical main")
}
if output, err := exec.CommandContext(ctx, "git", "-C", access.MainWorktree, "merge", "--ff-only", itemHead).CombinedOutput(); err != nil {
return fmt.Errorf("delivery fast-forward canonical main: %w: %s", err, strings.TrimSpace(string(output)))
}
for _, directory := range []string{workspace, access.MainWorktree} {
status, err := gitOutput(directory, "status", "--porcelain=v1", "--untracked-files=all")
if err != nil {
return fmt.Errorf("delivery settlement status: %w", err)
}
if status != "" {
return fmt.Errorf("delivery checkout is not clean after checks: %s", strings.Split(status, "\n")[0])
}
}
// Re-derive at the settlement boundary. A success means exactly the checked
// item commit is now reachable from the same canonical main worktree.
current, err = landingAccess(workspace)
if err != nil || current != access {
return fmt.Errorf("delivery access changed during settlement")
}
mainHead, err = gitOutput(access.MainWorktree, "rev-parse", "HEAD")
if err != nil {
return fmt.Errorf("delivery main commit: %w", err)
}
if err := exec.CommandContext(ctx, "git", "-C", workspace, "merge-base", "--is-ancestor", itemHead, mainHead).Run(); err != nil {
return fmt.Errorf("delivery item commit is not an ancestor of canonical main")
}
return nil
}
func acquireDeliveryLock(ctx context.Context, path string) (*os.File, error) {
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, fmt.Errorf("delivery lock: %w", err)
}
for {
err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err == nil {
return file, nil
}
if !errors.Is(err, syscall.EWOULDBLOCK) && !errors.Is(err, syscall.EAGAIN) {
_ = file.Close()
return nil, fmt.Errorf("delivery lock: %w", err)
}
select {
case <-ctx.Done():
_ = file.Close()
return nil, fmt.Errorf("delivery lock: %w", ctx.Err())
case <-time.After(20 * time.Millisecond):
}
}
}
func runDeliveryCandidate(ctx context.Context, workspace, itemHead string, checks []string) error {
candidate, err := os.MkdirTemp(filepath.Dir(workspace), ".pipelines-delivery-")
if err != nil {
return fmt.Errorf("create delivery candidate: %w", err)
}
if err := os.Remove(candidate); err != nil {
return fmt.Errorf("prepare delivery candidate: %w", err)
}
if output, err := boundedCommandOutput(ctx, workspace, "worktree", "add", "--detach", candidate, itemHead); err != nil {
cleanupDeliveryCandidate(workspace, candidate)
return fmt.Errorf("create delivery candidate: %w: %s", err, strings.TrimSpace(string(output)))
}
defer cleanupDeliveryCandidate(workspace, candidate)
for _, check := range checks {
if err := runDeliveryCheck(ctx, candidate, check); err != nil {
return err
}
}
return nil
}
// cleanupDeliveryCandidate always uses its own bounded context: the stage context
// may already be canceled when a check fails or times out.
func cleanupDeliveryCandidate(workspace, candidate string) {
ctx, cancel := context.WithTimeout(context.Background(), deliveryCleanupTimeout)
defer cancel()
_ = exec.CommandContext(ctx, "git", "-C", workspace, "worktree", "remove", "--force", candidate).Run()
_ = os.RemoveAll(candidate)
}
func boundedCommandOutput(ctx context.Context, directory string, args ...string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "git", append([]string{"-C", directory}, args...)...)
var output boundedDeliveryOutput
cmd.Stdout = &output
cmd.Stderr = &output
err := cmd.Run()
return []byte(output.String()), err
}
func runDeliveryCheck(ctx context.Context, directory, check string) error {
cmd := exec.CommandContext(ctx, "sh", "-c", check)
cmd.Dir = directory
var output boundedDeliveryOutput
cmd.Stdout = &output
cmd.Stderr = &output
if err := cmd.Run(); err != nil {
return fmt.Errorf("delivery check %q failed: %w: %s", check, err, output.String())
}
return nil
}
type boundedDeliveryOutput struct{ bytes.Buffer }
func (b *boundedDeliveryOutput) Write(p []byte) (int, error) {
remaining := maxDeliveryCheckOutput - b.Len()
if remaining > 0 {
_, _ = b.Buffer.Write(p[:min(remaining, len(p))])
}
return len(p), nil
}
func (b *boundedDeliveryOutput) String() string {
text := strings.TrimSpace(b.Buffer.String())
if b.Len() == maxDeliveryCheckOutput {
return text + " (output truncated)"
}
return text
}
var _ io.Writer = (*boundedDeliveryOutput)(nil)
+130
View File
@@ -0,0 +1,130 @@
package server
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// landingAccess derives delivery paths from Git's verified worktree metadata,
// never from stage input. A missing canonical main worktree fails closed.
func landingAccess(workspace string) (stage.LandingAccess, error) {
workspace, err := canonicalDirectory(workspace)
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery workspace: %w", err)
}
gitDir, err := gitOutput(workspace, "rev-parse", "--git-dir")
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery workspace is not a Git worktree: %w", err)
}
commonDir, err := gitOutput(workspace, "rev-parse", "--git-common-dir")
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery repository metadata: %w", err)
}
gitDir, err = canonicalDirectory(resolveGitPath(workspace, gitDir))
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery Git metadata: %w", err)
}
commonDir, err = canonicalDirectory(resolveGitPath(workspace, commonDir))
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery common Git metadata: %w", err)
}
main, err := mainWorktree(workspace, commonDir)
if err != nil {
return stage.LandingAccess{}, err
}
// Delivery originates in a linked item worktree. Its metadata must be under
// the repository's canonical common directory, never a standalone or foreign
// checkout. Git's common directory is the bounded capability needed for refs,
// worktree state, and the landing lock.
if !pathWithin(filepath.Join(commonDir, "worktrees"), gitDir) {
return stage.LandingAccess{}, fmt.Errorf("delivery workspace is not a linked Git worktree")
}
return stage.LandingAccess{MainWorktree: main, GitMetadata: commonDir, LockPath: filepath.Join(commonDir, "pipelines-landing.lock")}, nil
}
func mainWorktree(workspace, commonDir string) (string, error) {
list, err := gitOutput(workspace, "worktree", "list", "--porcelain")
if err != nil {
return "", fmt.Errorf("list repository worktrees: %w", err)
}
for _, record := range strings.Split(strings.TrimSpace(list), "\n\n") {
lines := strings.Split(record, "\n")
if len(lines) == 0 || !strings.HasPrefix(lines[0], "worktree ") || !worktreeHasBranch(lines, "refs/heads/main") {
continue
}
candidate, err := canonicalDirectory(strings.TrimPrefix(lines[0], "worktree "))
if err != nil || candidate == workspace {
continue
}
candidateGitDir, err := gitOutput(candidate, "rev-parse", "--git-dir")
if err != nil {
continue
}
candidateGitDir, err = canonicalDirectory(resolveGitPath(candidate, candidateGitDir))
if err != nil || candidateGitDir != commonDir {
continue
}
candidateCommon, err := gitOutput(candidate, "rev-parse", "--git-common-dir")
if err != nil {
continue
}
candidateCommon, err = canonicalDirectory(resolveGitPath(candidate, candidateCommon))
if err == nil && candidateCommon == commonDir {
return candidate, nil
}
}
return "", fmt.Errorf("no canonical main worktree in the delivery repository")
}
func worktreeHasBranch(lines []string, branch string) bool {
for _, line := range lines[1:] {
if line == "branch "+branch {
return true
}
}
return false
}
func canonicalDirectory(path string) (string, error) {
resolved, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
info, err := os.Stat(resolved)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", fmt.Errorf("not a directory")
}
return filepath.Clean(resolved), nil
}
func resolveGitPath(workspace, value string) string {
if filepath.IsAbs(value) {
return value
}
return filepath.Join(workspace, value)
}
func pathWithin(parent, child string) bool {
rel, err := filepath.Rel(parent, child)
return err == nil && rel != "." && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
}
func gitOutput(directory string, args ...string) (string, error) {
cmd := exec.Command("git", append([]string{"-C", directory}, args...)...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
data, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(stderr.String()))
}
return strings.TrimSpace(string(data)), nil
}
+304
View File
@@ -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))
}
+59 -2
View File
@@ -58,8 +58,11 @@ type runner struct {
// directory. It defaults to stage.ExportSession; a failure is best-effort and
// never changes the stage's status, outcome, target or result validation.
exportSession func(context.Context, stage.SessionExportRequest) (stage.SessionExportResult, error)
wg sync.WaitGroup
schedulerWG sync.WaitGroup
// verifyTarget is installed only by server wiring for a controlled fixture
// workflow. Leaving it nil fails closed for target-aware definitions.
verifyTarget stage.VerificationRunner
wg sync.WaitGroup
schedulerWG sync.WaitGroup
// attentionMu guards attentionSent, the per-run set of run-attention events
// already delivered — by a stage's own notify policy or by the run-level
// default — so neither republishes the same genuine event.
@@ -1079,7 +1082,22 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
}
}
operations := &stage.OperationLog{}
var landing stage.LandingAccess
delivery, deliveryErr := stage.DeliveryEnabled(def)
if deliveryErr != nil {
finish("failed", "error", deliveryErr.Error())
return
}
if delivery {
access, accessErr := landingAccess(workspace)
if accessErr != nil {
finish("failed", "error", "delivery access denied: "+accessErr.Error())
return
}
landing = access
}
client := r.agentClient(id, sequence, state.artifactDirs, workspace, artifactsDir, operations, request.WritePaths, tools)
client.LandingAccess = landing
client.OnSession = func(session string, active bool) error {
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
@@ -1222,6 +1240,45 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
step.Output["salvage"] = result.Output
}
}
if err == nil && result.Outcome == "success" {
target, targetErr := stage.VerificationTargetFor(def)
if targetErr != nil {
err = targetErr
} else if target != nil {
if r.verifyTarget == nil {
err = fmt.Errorf("verification target %q requires a server-controlled fixture runner", target.Fixture)
} else {
evidence, verificationErr := r.verifyTarget(stageCtx, *target)
if verificationErr != nil {
err = verificationErr
} else {
err = stage.VerifyTargetEvidence(*target, evidence)
}
}
}
if err != nil {
step.Status = "failed"
step.Diagnostic = err.Error()
step.Outcome = "error"
step.Output = stage.Values{"error": err.Error(), "stage": key, "input": input}
}
}
if err == nil && delivery && result.Outcome == "success" {
checks, checksErr := stage.DeliveryChecks(def)
if checksErr != nil {
err = checksErr
} else {
gateCtx, gateCancel := context.WithTimeout(stageCtx, modelConfig.Timeout)
err = verifyDelivery(gateCtx, workspace, landing, checks)
gateCancel()
}
if err != nil {
step.Status = "failed"
step.Diagnostic = err.Error()
step.Outcome = "error"
step.Output = stage.Values{"error": err.Error(), "stage": key, "input": input}
}
}
// A failed or timed-out step carries the session pointer even when the
// bounded transcript would otherwise be only its stage_limits envelope.
if err != nil && stageSession != "" {
+135
View File
@@ -0,0 +1,135 @@
package server
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/stage"
)
const (
verificationFixtureID = "reconnect-foreground"
verificationFixtureResponseLimit int64 = 64 << 10
verificationFixtureTimeout = 10 * time.Second
)
// VerificationFixture is a server-owned, explicitly registered probe. Its
// endpoint and expected provenance come from server configuration, never a
// stage definition or result.
type VerificationFixture struct {
ID, Endpoint, Backend, Revision, Capability, State string
}
// VerificationFixturesFromEnvironment returns the single supported fixture
// registration when its endpoint is explicitly configured. There is no default
// endpoint: an unconfigured server fails target-aware verification closed.
func VerificationFixturesFromEnvironment(getenv func(string) string) []VerificationFixture {
if getenv == nil {
getenv = os.Getenv
}
endpoint := strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_URL"))
if endpoint == "" {
return nil
}
return []VerificationFixture{{
ID: verificationFixtureID,
Endpoint: endpoint,
Backend: strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_BACKEND")),
Revision: strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_REVISION")),
Capability: strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_CAPABILITY")),
State: strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_STATE")),
}}
}
type verificationFixtureRegistry struct {
fixtures map[string]VerificationFixture
err error
}
func newVerificationFixtureRegistry(fixtures []VerificationFixture) *verificationFixtureRegistry {
r := &verificationFixtureRegistry{fixtures: make(map[string]VerificationFixture, len(fixtures))}
for _, fixture := range fixtures {
if err := validVerificationFixture(fixture); err != nil {
r.err = err
return r
}
if _, exists := r.fixtures[fixture.ID]; exists {
r.err = fmt.Errorf("duplicate verification fixture %q", fixture.ID)
return r
}
r.fixtures[fixture.ID] = fixture
}
return r
}
func validVerificationFixture(fixture VerificationFixture) error {
if fixture.ID != verificationFixtureID {
return fmt.Errorf("unsupported verification fixture %q", fixture.ID)
}
for _, value := range []string{fixture.Endpoint, fixture.Backend, fixture.Revision, fixture.Capability, fixture.State} {
if value == "" || len(value) > 256 {
return fmt.Errorf("verification fixture %q has incomplete configuration", fixture.ID)
}
}
u, err := url.Parse(fixture.Endpoint)
if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") {
return fmt.Errorf("verification fixture %q has invalid endpoint", fixture.ID)
}
return nil
}
func (r *verificationFixtureRegistry) verify(ctx context.Context, target stage.VerificationTarget) (stage.VerificationEvidence, error) {
if r.err != nil {
return stage.VerificationEvidence{}, r.err
}
fixture, exists := r.fixtures[target.Fixture]
if !exists {
return stage.VerificationEvidence{}, fmt.Errorf("unknown verification fixture %q", target.Fixture)
}
if fixture.Backend != target.Backend || fixture.Revision != target.Revision || fixture.Capability != target.Capability {
return stage.VerificationEvidence{}, fmt.Errorf("verification target does not match registered fixture %q", target.Fixture)
}
probeCtx, cancel := context.WithTimeout(ctx, verificationFixtureTimeout)
defer cancel()
req, err := http.NewRequestWithContext(probeCtx, http.MethodGet, fixture.Endpoint, nil)
if err != nil {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q request: %w", fixture.ID, err)
}
client := &http.Client{Timeout: verificationFixtureTimeout, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
response, err := client.Do(req)
if err != nil {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q probe: %w", fixture.ID, err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q probe returned %s", fixture.ID, response.Status)
}
var observed struct {
Backend string `json:"backend"`
Revision string `json:"revision"`
Capability string `json:"capability"`
Fixture string `json:"fixture"`
State string `json:"state"`
}
body, err := io.ReadAll(io.LimitReader(response.Body, verificationFixtureResponseLimit+1))
if err != nil {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q response: %w", fixture.ID, err)
}
if int64(len(body)) > verificationFixtureResponseLimit {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q response exceeds %d bytes", fixture.ID, verificationFixtureResponseLimit)
}
if err := json.Unmarshal(body, &observed); err != nil {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q response: %w", fixture.ID, err)
}
if observed.Backend != fixture.Backend || observed.Revision != fixture.Revision || observed.Capability != fixture.Capability || observed.Fixture != fixture.ID || observed.State != fixture.State {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q returned incompatible backend, revision, capability, fixture, or state", fixture.ID)
}
return stage.VerificationEvidence{Backend: observed.Backend, Revision: observed.Revision, Capability: observed.Capability, Fixture: observed.Fixture}, nil
}
@@ -0,0 +1,148 @@
package server
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"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 TestHandlerRunsRegisteredVerificationFixture(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var mu sync.Mutex
response := map[string]string{"backend": "branch-api", "revision": "abc123", "capability": "controlled-state", "fixture": verificationFixtureID, "state": "ready"}
calls := 0
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
calls++
_ = json.NewEncoder(w).Encode(response)
}))
defer fixture.Close()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "fixture.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
source := pipeline.Template([]string{"worker"}, true)
source = strings.Replace(source, "```configs", "```verification\nbackend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture="+verificationFixtureID+"\n```\n\n```configs", 1)
if _, err := store.Submit(ctx, "controlled-fixture", source); err != nil {
t.Fatal(err)
}
items, err := store.List(ctx)
if err != nil {
t.Fatal(err)
}
handler := HandlerWithOptions(ctx, Options{VerificationFixtures: []VerificationFixture{{ID: verificationFixtureID, Endpoint: fixture.URL, Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", State: "ready"}}}, store).(*runHandler)
defer func() {
cancel()
WaitRuns(handler)
}()
r := handler.runner
if r.verifyTarget == nil {
t.Fatal("normal server construction did not wire controlled fixture verification")
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
// These forged model labels must not influence fixture provenance.
return stage.Result{Outcome: "success", Output: stage.Values{"verification": map[string]any{"backend": "old-api", "revision": "stale", "capability": "forged", "fixture": "other"}}}, nil
}
start := func(key string) *protocol.RunDetail {
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: key, Workspace: t.TempDir(), Input: map[string]any{"prompt": "verify"}})
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
detail, err := store.GetRun(ctx, run.ID)
if err != nil {
t.Fatal(err)
}
return detail
}
if detail := start("compatible"); detail.Status != "succeeded" {
t.Fatalf("compatible controlled fixture did not settle: %+v", detail)
}
mu.Lock()
response["revision"] = "old-build"
mu.Unlock()
if detail := start("stale"); detail.Status != "failed" || !strings.Contains(detail.Steps[0].Diagnostic, "incompatible") {
t.Fatalf("stale fixture provenance settled as success: %+v", detail)
}
mu.Lock()
if calls != 2 {
t.Fatalf("fixture probes = %d, want one per target-aware stage", calls)
}
mu.Unlock()
}
func TestVerificationFixtureCancellationClosesProbe(t *testing.T) {
started := make(chan struct{})
canceled := make(chan struct{})
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
close(started)
<-r.Context().Done()
close(canceled)
}))
defer fixture.Close()
registry := newVerificationFixtureRegistry([]VerificationFixture{{ID: verificationFixtureID, Endpoint: fixture.URL, Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", State: "ready"}})
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() {
_, err := registry.verify(ctx, stage.VerificationTarget{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: verificationFixtureID})
done <- err
}()
select {
case <-started:
case <-time.After(time.Second):
t.Fatal("fixture probe did not start")
}
cancel()
select {
case err := <-done:
if err == nil {
t.Fatal("canceled fixture probe succeeded")
}
case <-time.After(time.Second):
t.Fatal("canceled fixture probe did not return")
}
select {
case <-canceled:
case <-time.After(time.Second):
t.Fatal("canceled fixture request was not cleaned up")
}
}
func TestVerificationFixtureRejectsUnknownAndMismatchedProvenance(t *testing.T) {
base := map[string]string{"backend": "branch-api", "revision": "abc123", "capability": "controlled-state", "fixture": verificationFixtureID, "state": "ready"}
for field, wrong := range map[string]string{"backend": "old-api", "revision": "old-build", "capability": "other-state", "state": "stale"} {
t.Run(field, func(t *testing.T) {
response := make(map[string]string, len(base))
for key, value := range base {
response[key] = value
}
response[field] = wrong
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(response)
}))
defer fixture.Close()
registry := newVerificationFixtureRegistry([]VerificationFixture{{ID: verificationFixtureID, Endpoint: fixture.URL, Backend: base["backend"], Revision: base["revision"], Capability: base["capability"], State: base["state"]}})
_, err := registry.verify(context.Background(), stage.VerificationTarget{Backend: base["backend"], Revision: base["revision"], Capability: base["capability"], Fixture: verificationFixtureID})
if err == nil || !strings.Contains(err.Error(), "incompatible") {
t.Fatalf("mismatched %s provenance was accepted: %v", field, err)
}
})
}
registry := newVerificationFixtureRegistry(nil)
if _, err := registry.verify(context.Background(), stage.VerificationTarget{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: "unknown"}); err == nil || !strings.Contains(err.Error(), "unknown") {
t.Fatalf("unknown fixture was accepted: %v", err)
}
}
+8
View File
@@ -49,6 +49,10 @@ type Options struct {
// read time with no model call, which keeps tests and unconfigured servers
// deterministic.
StepVerdicts func(context.Context, map[string]any) string
// VerificationFixtures are explicit server-owned HTTP fixtures for optional
// target-aware verification. Definitions can name only a registered ID; they
// cannot select an endpoint or supply provenance.
VerificationFixtures []VerificationFixture
}
// Handler serves the persistent connection with default options. Canceling ctx
@@ -81,6 +85,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
if runErr == nil && options.StepVerdicts != nil {
runs.verdict = options.StepVerdicts
}
if runErr == nil {
fixtures := newVerificationFixtureRegistry(options.VerificationFixtures)
runs.verifyTarget = fixtures.verify
}
if runErr == nil {
runs.schedulerWG.Add(1)
go runs.scheduleLoop()
+1
View File
@@ -19,6 +19,7 @@ type AgentClient struct {
// ArtifactAccess is supplied only by the runner. Write is this visit's
// directory; Read contains this and completed earlier visits in the same run.
ArtifactAccess ArtifactAccess
LandingAccess LandingAccess
OnSession func(string, bool) error
SessionID string
Transcript string
+14 -2
View File
@@ -59,7 +59,7 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
}
for block := range def.Blocks {
switch block {
case "output", "prompt", "edges", "configs", "tools", "permissions":
case "output", "prompt", "edges", "configs", "tools", "permissions", "checks", "verification":
default:
return ModelSpec{}, fmt.Errorf("unexpected block %q for model stage", block)
}
@@ -70,6 +70,9 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
if _, err := StagePermissions(def); err != nil {
return ModelSpec{}, err
}
if _, err := VerificationTargetFor(def); err != nil {
return ModelSpec{}, err
}
for _, key := range []string{"provider", "model"} {
if def.Configs[key] == "" {
return ModelSpec{}, fmt.Errorf("missing config %s", key)
@@ -77,7 +80,7 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
}
for key := range def.Configs {
switch key {
case "provider", "model", "retries", "timeout", "budget_usd", "budget_tokens", "harness":
case "provider", "model", "retries", "timeout", "budget_usd", "budget_tokens", "harness", "delivery":
default:
return ModelSpec{}, fmt.Errorf("unknown config %q", key)
}
@@ -92,6 +95,15 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
if backend != "opencode" && backend != "pi" {
return ModelSpec{}, fmt.Errorf("harness must be opencode or pi")
}
if delivery := def.Configs["delivery"]; delivery != "" && delivery != "true" {
return ModelSpec{}, fmt.Errorf("delivery must be true when set")
}
if def.Configs["delivery"] == "true" && backend != "opencode" {
return ModelSpec{}, fmt.Errorf("delivery is supported only by the opencode harness")
}
if _, present := def.Blocks["checks"]; present && def.Configs["delivery"] != "true" {
return ModelSpec{}, fmt.Errorf("checks block is supported only by delivery stages")
}
if backend == "pi" {
tools, _, _ := ToolNames(def)
if _, err := piTools(tools); err != nil {
+75
View File
@@ -14,6 +14,77 @@ type ArtifactAccess struct {
Read []string
}
// LandingAccess is the server-derived external capability for a verified
// delivery operation. Empty or malformed values grant nothing.
type LandingAccess struct {
MainWorktree string
GitMetadata string
LockPath string
}
func (a LandingAccess) valid() bool {
for _, path := range []string{a.MainWorktree, a.GitMetadata, a.LockPath} {
if path == "" || !filepath.IsAbs(path) || filepath.Clean(path) != path || strings.ContainsAny(path, "*?\\") {
return false
}
}
// The server grants the canonical repository metadata directory as OpenCode
// V2's smallest external-directory unit. Keep the lock inside that directory
// so a caller cannot turn a valid metadata capability into access elsewhere.
return a.GitMetadata == filepath.Join(a.MainWorktree, ".git") &&
a.LockPath == filepath.Join(a.GitMetadata, "pipelines-landing.lock")
}
// DeliveryEnabled is the narrow, declarative capability check used before the
// runner supplies any landing paths. Only a validated model definition can ask
// for it; runtime paths are still derived by the server.
func DeliveryEnabled(def Definition) (bool, error) {
delivery := def.Configs["delivery"]
if delivery == "" {
return false, nil
}
if delivery != "true" {
return false, fmt.Errorf("delivery must be true when set")
}
if EffectiveType(def.Type) != ModelType {
return false, fmt.Errorf("delivery is supported only by model stages")
}
if _, err := (ModelStage{}).Spec(def); err != nil {
return false, fmt.Errorf("invalid delivery model stage: %w", err)
}
if _, err := DeliveryChecks(def); err != nil {
return false, err
}
return true, nil
}
// DeliveryChecks returns the bounded, definition-declared checks that the
// runner executes against a committed delivery candidate before canonical main
// is changed. They
// are intentionally a block rather than a generic runner setting: each
// workflow owns its repository-specific verification commands.
func DeliveryChecks(def Definition) ([]string, error) {
text := strings.TrimSpace(def.Blocks["checks"])
if text == "" {
return nil, fmt.Errorf("delivery stages require a nonempty checks block")
}
var checks []string
for _, line := range strings.Split(text, "\n") {
check := strings.TrimSpace(line)
if check == "" {
continue
}
if len(check) > 4096 {
return nil, fmt.Errorf("delivery check exceeds 4096 bytes")
}
checks = append(checks, check)
}
if len(checks) == 0 || len(checks) > 20 {
return nil, fmt.Errorf("delivery stages require 1 to 20 checks")
}
return checks, nil
}
// ToolNames validates model-stage permissions, not general graph semantics.
// A present empty block explicitly denies all tools.
func ToolNames(def Definition) ([]string, bool, error) {
@@ -155,6 +226,10 @@ func (c *AgentClient) permissionRules() []map[string]string {
allow("external_directory", filepath.ToSlash(filepath.Join(c.ArtifactAccess.Write, "*")))
allow("edit", filepath.ToSlash(filepath.Join(c.ArtifactAccess.Write, "*")))
}
if c.LandingAccess.valid() {
allow("external_directory", filepath.ToSlash(filepath.Join(c.LandingAccess.MainWorktree, "*")))
allow("external_directory", filepath.ToSlash(filepath.Join(c.LandingAccess.GitMetadata, "*")))
}
for _, pattern := range []string{"../*", "*/../*"} {
rules = append(rules, map[string]string{"action": "external_directory", "resource": pattern, "effect": "deny"})
}
+104
View File
@@ -174,3 +174,107 @@ func TestArtifactPermissionsAreExactAndToolScoped(t *testing.T) {
t.Fatal("missing artifact scope enabled an external directory")
}
}
func TestLandingPermissionsAreExactAndServerDerived(t *testing.T) {
access := LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/pipelines/.git", LockPath: "/repos/pipelines/.git/pipelines-landing.lock"}
client := &AgentClient{Tools: []string{"read", "shell"}, LandingAccess: access}
rules := client.permissionRules()
for _, path := range []string{"/repos/pipelines/go.mod", "/repos/pipelines/.git/worktrees/item/HEAD", "/repos/pipelines/.git/pipelines-landing.lock"} {
if permissionEffect(rules, "external_directory", path) != "allow" {
t.Fatalf("verified landing path denied: %q", path)
}
}
for _, path := range []string{"/repos/other/go.mod", "/repos/pipelines/../other/go.mod", "/Users/alan/.git/config", "/.git/config"} {
if permissionEffect(rules, "external_directory", path) != "deny" {
t.Fatalf("unverified landing path allowed: %q", path)
}
}
client.LandingAccess.MainWorktree = "/repos/pipelines/../other"
if permissionEffect(client.permissionRules(), "external_directory", "/repos/other/go.mod") != "deny" {
t.Fatal("non-canonical landing capability allowed")
}
client.LandingAccess = LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/pipelines/.git", LockPath: "/tmp/pipelines-landing.lock"}
if permissionEffect(client.permissionRules(), "external_directory", "/tmp/pipelines-landing.lock") != "deny" {
t.Fatal("landing lock outside verified metadata allowed")
}
client.LandingAccess = LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/other/.git", LockPath: "/repos/other/.git/pipelines-landing.lock"}
if permissionEffect(client.permissionRules(), "external_directory", "/repos/other/.git/config") != "deny" {
t.Fatal("unrelated Git metadata allowed")
}
}
func TestDeliveryEnabledRequiresValidatedModelStage(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
def.Blocks["checks"] = "go test ./..."
if enabled, err := DeliveryEnabled(def); err != nil || !enabled {
t.Fatalf("valid delivery stage = %v, %v", enabled, err)
}
for name, change := range map[string]func(*Definition){
"malformed value": func(d *Definition) { d.Configs["delivery"] = "yes" },
"non-model": func(d *Definition) { d.Type = HumanType },
"invalid model": func(d *Definition) { d.Configs["unexpected"] = "value" },
} {
t.Run(name, func(t *testing.T) {
copy := def
copy.Configs = map[string]string{}
for key, value := range def.Configs {
copy.Configs[key] = value
}
change(&copy)
if enabled, err := DeliveryEnabled(copy); err == nil || enabled {
t.Fatalf("invalid delivery stage = %v, %v", enabled, err)
}
})
}
}
func TestDeliveryRejectsPiBeforeAgentExecution(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
def.Configs["harness"] = "pi"
def.Blocks["checks"] = "true"
if err := (ModelStage{}).Validate(def); err == nil || !strings.Contains(err.Error(), "opencode harness") {
t.Fatal("Pi delivery was accepted", err)
}
delete(def.Configs, "delivery")
delete(def.Blocks, "checks")
if err := (ModelStage{}).Validate(def); err != nil {
t.Fatal("Pi non-delivery was changed", err)
}
}
func TestDeliveryChecksRequireBoundedDeliveryOnlyDeclaration(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
for name, mutate := range map[string]func(*Definition){
"missing": func(d *Definition) {},
"empty": func(d *Definition) { d.Blocks["checks"] = "\n" },
"non delivery": func(d *Definition) { d.Blocks["checks"] = "go test ./..." },
"too many checks": func(d *Definition) { d.Blocks["checks"] = strings.Repeat("true\n", 21) },
"oversized check": func(d *Definition) { d.Blocks["checks"] = strings.Repeat("x", 4097) },
} {
t.Run(name, func(t *testing.T) {
copy := def
copy.Blocks = map[string]string{}
copy.Configs = map[string]string{}
for key, value := range def.Blocks {
copy.Blocks[key] = value
}
for key, value := range def.Configs {
copy.Configs[key] = value
}
mutate(&copy)
if name == "non delivery" {
delete(copy.Configs, "delivery")
if _, err := (ModelStage{}).Spec(copy); err == nil {
t.Fatal("non-delivery checks block was accepted")
}
return
}
if _, err := DeliveryEnabled(copy); err == nil {
t.Fatal("invalid delivery checks were accepted")
}
})
}
}
+97
View File
@@ -0,0 +1,97 @@
package stage
import (
"context"
"fmt"
"strings"
)
// VerificationTarget declares the controlled fixture that can substantiate a
// target-specific behavior claim. It is intentionally data-only: the runner
// does not execute fixture input supplied by a stage result.
type VerificationTarget struct {
Backend string
Revision string
Capability string
Fixture string
}
// VerificationEvidence is returned by a server-controlled fixture workflow,
// never parsed from a model's result.
type VerificationEvidence struct {
Backend string
Revision string
Capability string
Fixture string
}
// VerificationRunner is the narrow server seam for a controlled fixture or
// workflow. The runner selects it during server setup; model output cannot
// select a backend, revision, fixture, or executable.
type VerificationRunner func(context.Context, VerificationTarget) (VerificationEvidence, error)
// VerificationTargetFor parses an optional, bounded verification block. A
// declared target requires all provenance fields so an old or other-backend
// fixture cannot be presented as evidence for the current target.
func VerificationTargetFor(def Definition) (*VerificationTarget, error) {
text, declared := def.Blocks["verification"]
if !declared {
return nil, nil
}
if len(text) > 4096 {
return nil, fmt.Errorf("verification block exceeds 4096 bytes")
}
values := map[string]string{}
for _, line := range strings.Split(text, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
key, value, ok := strings.Cut(line, "=")
key, value = strings.TrimSpace(key), strings.TrimSpace(value)
if !ok || value == "" || len(value) > 256 {
return nil, fmt.Errorf("invalid verification entry %q", line)
}
switch key {
case "backend", "revision", "capability", "fixture":
default:
return nil, fmt.Errorf("unknown verification entry %q", key)
}
if _, exists := values[key]; exists {
return nil, fmt.Errorf("duplicate verification entry %q", key)
}
values[key] = value
}
target := &VerificationTarget{Backend: values["backend"], Revision: values["revision"], Capability: values["capability"], Fixture: values["fixture"]}
if target.Backend == "" || target.Revision == "" || target.Capability == "" || target.Fixture == "" {
return nil, fmt.Errorf("verification requires backend, revision, capability, and fixture")
}
return target, nil
}
// VerifyTargetEvidence compares server-collected controlled-fixture provenance
// with the workflow declaration. It deliberately accepts no model result: a
// stage cannot substantiate its own target-specific behavior claim.
func VerifyTargetEvidence(target VerificationTarget, evidence VerificationEvidence) error {
for key, want := range map[string]string{
"backend": target.Backend, "revision": target.Revision, "capability": target.Capability, "fixture": target.Fixture,
} {
got := map[string]string{
"backend": evidence.Backend, "revision": evidence.Revision, "capability": evidence.Capability, "fixture": evidence.Fixture,
}[key]
if got != want {
return fmt.Errorf("verification evidence %s does not match declared target", key)
}
}
return nil
}
// VerifyTargetResult is retained for callers that only need declaration
// validation. Result-provided provenance is intentionally not evidence.
func VerifyTargetResult(def Definition, _ Values) error {
target, err := VerificationTargetFor(def)
if err != nil || target == nil {
return err
}
return fmt.Errorf("verification target %q requires a server-controlled fixture runner", target.Fixture)
}
+27
View File
@@ -0,0 +1,27 @@
package stage
import "testing"
func TestTargetVerificationRejectsWrongBackendEvidence(t *testing.T) {
def := modelDefinition()
def.Blocks["verification"] = "backend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture=reconnect-foreground"
if err := (ModelStage{}).Validate(def); err != nil {
t.Fatal(err)
}
target, err := VerificationTargetFor(def)
if err != nil {
t.Fatal(err)
}
good := VerificationEvidence{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}
if err := VerifyTargetEvidence(*target, good); err != nil {
t.Fatalf("matching fixture evidence rejected: %v", err)
}
wrongBackend := VerificationEvidence{Backend: "old-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}
if err := VerifyTargetEvidence(*target, wrongBackend); err == nil {
t.Fatal("wrong-backend fixture evidence accepted")
}
def.Blocks["verification"] = "backend=branch-api\nrevision=abc123"
if err := (ModelStage{}).Validate(def); err == nil {
t.Fatal("incomplete target verification declaration accepted")
}
}