c42a19f7d3
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
263 lines
9.7 KiB
Go
263 lines
9.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/examples"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
"io"
|
|
"mime"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
func TestStageNotificationEventsAndArtifactAudit(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "notify.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
workspace := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(workspace, "report.txt"), []byte("created file"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var calls atomic.Int32
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
count := calls.Add(1)
|
|
filename, _ := new(mime.WordDecoder).DecodeHeader(r.Header.Get("Filename"))
|
|
body, _ := io.ReadAll(r.Body)
|
|
if filename == "report.txt" && string(body) != "created file" {
|
|
t.Error("wrong file content")
|
|
}
|
|
if filename == "digest.md" && string(body) != "# Digest" {
|
|
t.Error("wrong markdown")
|
|
}
|
|
ack := map[string]any{"id": fmt.Sprintf("msg%d", count), "event": "message"}
|
|
if filename != "" {
|
|
ack["attachment"] = map[string]string{"name": filename, "url": "https://ntfy.example.com/file/" + filename}
|
|
}
|
|
_ = json.NewEncoder(w).Encode(ack)
|
|
}))
|
|
defer srv.Close()
|
|
t.Setenv("PIPELINES_NTFY_URL", srv.URL+"/pipelines")
|
|
if _, err := store.Submit(ctx, "notifications", pipeline.Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
d, _ := store.Get(ctx, items[0].ID, 0)
|
|
r, _ := newRunner(ctx, store)
|
|
for i, test := range []struct {
|
|
status, outcome, target, on string
|
|
attachments bool
|
|
expected int32
|
|
}{
|
|
{"completed", "success", protocol.SuccessEnd, "success", true, 2},
|
|
{"completed", "blocked", protocol.FailureEnd, "failure", false, 1},
|
|
{"failed", "error", protocol.FailureEnd, "failure", false, 1},
|
|
{"completed", "error", protocol.FailureEnd, "success", false, 0},
|
|
} {
|
|
id, _, err := store.CreateRun(ctx, d, fmt.Sprintf("case%d", i), workspace, false, nil, map[string]any{"prompt": "task"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.BeginStep(ctx, id, 1, "one", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
step := protocol.RunStep{Sequence: 1, Stage: "one", Status: test.status, Outcome: test.outcome, Target: test.target, Output: map[string]any{}}
|
|
policy := &protocol.NotificationPolicy{On: []string{test.on}, Destination: "anime"}
|
|
if test.attachments {
|
|
policy.Artifacts = []string{"markdown", "files"}
|
|
step.Output = map[string]any{"markdown": "# Digest", "artifacts": []any{"report.txt"}}
|
|
}
|
|
if err := store.FinishStep(ctx, id, step); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := calls.Load()
|
|
r.notifyStep(id, d, workspace, policy, step)
|
|
detail, err := store.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls.Load()-before != test.expected || len(detail.Steps[0].Notifications) != int(test.expected) {
|
|
t.Fatal("event/attachments mismatch", detail, calls.Load()-before)
|
|
}
|
|
for _, receipt := range detail.Steps[0].Notifications {
|
|
if receipt.Status != "sent" || receipt.MessageID == "" {
|
|
t.Fatal("notification receipt not persisted", receipt)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Regress the exact research -> report terminal-path recipe from the authoring guide.
|
|
// A completion-alert request must not require native delivery or change run routing.
|
|
func TestResearchTerminalNotificationRecipe(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "recipe.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
var source string
|
|
for _, template := range examples.Catalog() {
|
|
if template.Key == "research" {
|
|
source = template.Source
|
|
}
|
|
}
|
|
if source == "" {
|
|
t.Fatal("research template missing")
|
|
}
|
|
source = strings.Replace(source, "# stage research", "# stage research\n```notify\non=failure\n```", 1)
|
|
source = strings.Replace(source, "# stage report", "# stage report\n```notify\non=success,failure\n```", 1)
|
|
if _, err = store.Submit(ctx, "research notification recipe", source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil || len(items) != 1 {
|
|
t.Fatal("recipe missing", err)
|
|
}
|
|
id := items[0].ID
|
|
var calls atomic.Int32
|
|
var deliveryFailure atomic.Bool
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
calls.Add(1)
|
|
if req.Header.Get("Filename") != "" {
|
|
t.Error("event alert unexpectedly changed output to add attachments")
|
|
}
|
|
if deliveryFailure.Load() {
|
|
http.Error(w, "unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"id": "recipe-receipt", "event": "message"})
|
|
}))
|
|
defer srv.Close()
|
|
t.Setenv("PIPELINES_NTFY_URL", srv.URL+"/pipelines")
|
|
t.Setenv("PIPELINES_NTFY_TOKEN", "")
|
|
t.Setenv("PIPELINES_NTFY_TOKEN_FILE", "")
|
|
r, err := newRunner(ctx, store)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for index, test := range []struct {
|
|
name, stage, outcome, status string
|
|
executionError, deliveryError bool
|
|
}{
|
|
{"success", "report", "success", "succeeded", false, false},
|
|
{"research blocked", "research", "blocked", "failed", false, false},
|
|
{"research execution error", "research", "error", "failed", true, false},
|
|
{"report blocked", "report", "blocked", "failed", false, false},
|
|
{"report execution error", "report", "error", "failed", true, false},
|
|
{"delivery error preserves success", "report", "success", "succeeded", false, true},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
deliveryFailure.Store(test.deliveryError)
|
|
r.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) {
|
|
if def.Key == test.stage {
|
|
if test.executionError {
|
|
return stage.Result{}, errors.New("fixture execution failed")
|
|
}
|
|
return stage.Result{Outcome: test.outcome, Output: stage.Values{"report": "retained evidence"}}, nil
|
|
}
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"question": "topic", "evidence": []any{"source fact"}}}, nil
|
|
}
|
|
before := calls.Load()
|
|
run, err := r.start(protocol.Message{PipelineID: id, Version: 1, RequestKey: fmt.Sprintf("recipe-%d", index), Workspace: t.TempDir(), Input: map[string]any{"prompt": "research a topic"}})
|
|
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 != test.status || calls.Load()-before != 1 {
|
|
t.Fatal("wrong verdict or duplicate/missing terminal alert", detail.Status, calls.Load()-before)
|
|
}
|
|
last := detail.Steps[len(detail.Steps)-1]
|
|
if last.Stage != test.stage || len(last.Notifications) != 1 {
|
|
t.Fatal("notification not retained on terminal stage", last)
|
|
}
|
|
for _, step := range detail.Steps[:len(detail.Steps)-1] {
|
|
if len(step.Notifications) != 0 {
|
|
t.Fatal("intermediate success alerted", step)
|
|
}
|
|
}
|
|
want := "sent"
|
|
if test.deliveryError {
|
|
want = "failed"
|
|
}
|
|
if last.Notifications[0].Status != want {
|
|
t.Fatal("delivery receipt wrong", last.Notifications)
|
|
}
|
|
if !test.executionError && detail.Output["report"] != "retained evidence" {
|
|
t.Fatal("notification changed output handoff", detail.Output)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNotificationFailureDoesNotChangeRunRouting(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "routing.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
source := strings.Replace(pipeline.Template([]string{"one"}, true), "# stage one", "# stage one\n```notify\non=success\ndestination=anime\nartifacts=markdown\n```", 1)
|
|
if _, err := store.Submit(ctx, "test", source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
r, _ := newRunner(ctx, store)
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Result"}}, nil
|
|
}
|
|
t.Setenv("PIPELINES_NTFY_URL", "")
|
|
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "once", Input: map[string]any{"prompt": "task"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
detail, _ := store.GetRun(ctx, run.ID)
|
|
if detail.Status != "succeeded" || len(detail.Steps[0].Notifications) != 1 || detail.Steps[0].Notifications[0].Status != "failed" {
|
|
t.Fatal("notification altered routing or lost failure audit", detail)
|
|
}
|
|
}
|
|
|
|
func TestNotificationArtifactPathSafety(t *testing.T) {
|
|
root := t.TempDir()
|
|
os.WriteFile(filepath.Join(root, "safe.txt"), []byte("safe"), 0600)
|
|
os.WriteFile(filepath.Join(root, ".env"), []byte("secret"), 0600)
|
|
os.Symlink(filepath.Join(root, ".env"), filepath.Join(root, "alias.txt"))
|
|
outside := filepath.Join(t.TempDir(), "outside.txt")
|
|
os.WriteFile(outside, []byte("secret"), 0600)
|
|
os.Symlink(outside, filepath.Join(root, "external.txt"))
|
|
os.Mkdir(filepath.Join(root, "directory"), 0700)
|
|
os.WriteFile(filepath.Join(root, "large.bin"), []byte(strings.Repeat("x", (1<<20)+1)), 0600)
|
|
if err := syscall.Mkfifo(filepath.Join(root, "pipe"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"../outside.txt", outside, ".env", "alias.txt", "external.txt", "directory", "large.bin", "pipe", "pipelines.db", "secret.key", "secrets/file.txt", "sub/../safe.txt"} {
|
|
if _, err := notificationArtifact(root, name); err == nil {
|
|
t.Fatal("unsafe artifact accepted", name)
|
|
}
|
|
}
|
|
if data, err := notificationArtifact(root, "safe.txt"); err != nil || string(data) != "safe" {
|
|
t.Fatal("safe artifact rejected", err)
|
|
}
|
|
}
|