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)