Files
alan 05ae530442 Make Pipelines Docker-only with OpenCode and Pi
Install current verified harness releases inside Docker, preserve persistent accounts and settings, and provide separate interactive and pipeline model defaults with terminal onboarding and fuzzy pickers.

Add Docker-aware authoring transport, Pi stage execution, bounded workflow controls, reliable final-result readback and usage settlement, and finite short-lived CLI pipe draining. Keep dated installation evidence and packaged authoring guidance consistent.

Verified with a real Docker installation, both harness stages and interactive authors, the built-in plan/implement/review starter, independent host race/vet checks, installer tests, and packaged skill tests. Generated release receipts stay ignored in clean checkouts.
2026-10-11 23:00:25 +01:00

201 lines
7.4 KiB
Go

package pipeline
import (
"context"
"encoding/json"
"git.a-wan.net/alan/pipelines/internal/protocol"
"path/filepath"
"reflect"
"strings"
"testing"
"unicode/utf8"
)
func TestFileChangeMigrationAndAtomicImmutableFinish(t *testing.T) {
ctx := context.Background()
db := filepath.Join(t.TempDir(), "old.db")
s, err := Open(db)
if err != nil {
t.Fatal(err)
}
if _, err = s.Submit(ctx, "audit", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := s.List(ctx)
def, _ := s.Get(ctx, items[0].ID, 1)
id, _, err := s.CreateRun(ctx, def, "key", "workspace", false, nil, nil)
if err != nil {
t.Fatal(err)
}
if err = s.BeginStep(ctx, id, 1, "one", nil); err != nil {
t.Fatal(err)
}
// Simulate the pre-diff database while retaining its existing history.
if _, err = s.db.Exec("DROP TABLE step_file_changes"); err != nil {
t.Fatal(err)
}
s.Close()
s, err = Open(db)
if err != nil {
t.Fatal(err)
}
defer s.Close()
run, err := s.GetRun(ctx, id)
if err != nil || run.Steps[0].FileChanges != nil {
t.Fatal("historical step misleading", run, err)
}
c := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "file", Kind: "added", Patch: "+new\n"}}}
step := protocol.RunStep{Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd, FileChanges: c}
if err = s.FinishStep(ctx, id, step); err != nil {
t.Fatal(err)
}
step.FileChanges = &protocol.StageFileChanges{Status: "complete"}
if err = s.FinishStep(ctx, id, step); err == nil {
t.Fatal("finished evidence overwritten")
}
run, err = s.GetRun(ctx, id)
if err != nil || !reflect.DeepEqual(run.Steps[0].FileChanges, c) {
t.Fatal("diff lost/changed", run, err)
}
if err = s.BeginStep(ctx, id, 2, "one", nil); err != nil {
t.Fatal(err)
}
if _, err = s.db.Exec(`CREATE TRIGGER fail_diff BEFORE INSERT ON step_file_changes BEGIN SELECT RAISE(ABORT,'fixture'); END`); err != nil {
t.Fatal(err)
}
step.Sequence = 2
step.FileChanges = c
if err = s.FinishStep(ctx, id, step); err == nil {
t.Fatal("expected write failure")
}
run, err = s.GetRun(ctx, id)
if err != nil || run.Steps[1].Status != "running" || run.Steps[1].FileChanges != nil {
t.Fatal("partial finish committed", run, err)
}
}
// TestFinishStepBoundsOversizedStructuredTranscript proves a >32 KiB structured
// transcript is stored as a valid bounded JSON envelope instead of a malformed
// prefix: late stage_limits and failure/format diagnostics survive, the preview
// never splits a multibyte rune, credentials stay redacted, and the exact
// step.Output is stored unchanged.
func TestFinishStepBoundsOversizedStructuredTranscript(t *testing.T) {
ctx := context.Background()
s, err := Open(filepath.Join(t.TempDir(), "transcript.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err = s.Submit(ctx, "audit", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := s.List(ctx)
def, _ := s.Get(ctx, items[0].ID, 1)
id, _, err := s.CreateRun(ctx, def, "key", "workspace", false, nil, nil)
if err != nil {
t.Fatal(err)
}
if err = s.BeginStep(ctx, id, 1, "one", nil); err != nil {
t.Fatal(err)
}
// The huge "data" field sorts before "stage_limits", so a naive byte cut
// would lose the late limits; the multibyte filler checks rune boundaries.
transcript, err := json.Marshal(map[string]any{
"data": []any{map[string]any{"content": strings.Repeat("é", 20000)}},
"opencode_failures": []any{map[string]any{"diagnostic": "provider failed", "stdout": "token=private-secret"}},
"format_repairs": []any{map[string]any{"method": "validation", "accepted": false}},
"stage_limits": map[string]any{"timeout": "1h0m0s", "retries": 2},
})
if err != nil {
t.Fatal(err)
}
if len(transcript) <= 32<<10 {
t.Fatalf("fixture transcript not over the bound: %d", len(transcript))
}
output := map[string]any{"summary": "done", "nested": map[string]any{"value": float64(42)}}
step := protocol.RunStep{Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd, SessionID: "ses_transcript", Transcript: string(transcript), Output: output}
if err = s.FinishStep(ctx, id, step); err != nil {
t.Fatal(err)
}
run, err := s.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
stored := run.Steps[0]
// GetRun decodes JSON numbers with UseNumber, so compare the canonical JSON
// encoding rather than reflect.DeepEqual against the original float64 values.
outputJSON, _ := json.Marshal(output)
storedJSON, _ := json.Marshal(stored.Output)
if string(storedJSON) != string(outputJSON) {
t.Fatalf("step output changed: %v", stored.Output)
}
if len(stored.Transcript) > 32<<10 || !json.Valid([]byte(stored.Transcript)) {
t.Fatalf("bounded transcript is not valid bounded JSON (%d bytes): %s", len(stored.Transcript), stored.Transcript)
}
if strings.Contains(stored.Transcript, "private-secret") {
t.Fatal("credential leaked into the bounded transcript")
}
var envelope struct {
Format string `json:"format"`
Scope string `json:"scope"`
Truncated bool `json:"truncated"`
OriginalBytes int `json:"original_bytes"`
Preview string `json:"preview"`
RetainedSession string `json:"retained_session"`
StageLimits json.RawMessage `json:"stage_limits"`
OpenCodeFail json.RawMessage `json:"opencode_failures"`
FormatRepairs json.RawMessage `json:"format_repairs"`
}
if err := json.Unmarshal([]byte(stored.Transcript), &envelope); err != nil {
t.Fatal(err)
}
if envelope.Format != "json" || !envelope.Truncated || envelope.OriginalBytes != len(transcript) || envelope.RetainedSession != "ses_transcript" {
t.Fatalf("bounded envelope metadata incorrect: %+v", envelope)
}
if len(envelope.Preview) == 0 || !utf8.ValidString(envelope.Preview) {
t.Fatal("preview missing or not rune-safe")
}
var limits struct {
Timeout string `json:"timeout"`
Retries int `json:"retries"`
}
if json.Unmarshal(envelope.StageLimits, &limits) != nil || limits.Timeout != "1h0m0s" || limits.Retries != 2 {
t.Fatalf("late stage_limits dropped: %s", envelope.StageLimits)
}
if !strings.Contains(string(envelope.OpenCodeFail), "provider failed") || len(envelope.FormatRepairs) == 0 {
t.Fatalf("failure/format diagnostics dropped: %s / %s", envelope.OpenCodeFail, envelope.FormatRepairs)
}
}
// TestFinishStepLeavesSmallTranscriptUnchanged guards the common path: at or
// under the bound the transcript is stored byte-for-byte.
func TestFinishStepLeavesSmallTranscriptUnchanged(t *testing.T) {
ctx := context.Background()
s, err := Open(filepath.Join(t.TempDir(), "small.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err = s.Submit(ctx, "audit", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := s.List(ctx)
def, _ := s.Get(ctx, items[0].ID, 1)
id, _, err := s.CreateRun(ctx, def, "key", "workspace", false, nil, nil)
if err != nil {
t.Fatal(err)
}
if err = s.BeginStep(ctx, id, 1, "one", nil); err != nil {
t.Fatal(err)
}
small := `{"data":[{"role":"assistant","content":[{"type":"text","text":"ok"}]}]}`
step := protocol.RunStep{Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd, SessionID: "ses_small", Transcript: small}
if err = s.FinishStep(ctx, id, step); err != nil {
t.Fatal(err)
}
run, err := s.GetRun(ctx, id)
if err != nil || run.Steps[0].Transcript != small {
t.Fatalf("small transcript altered: %q", run.Steps[0].Transcript)
}
}