Files
pipelines/internal/server/tools_test.go
T

97 lines
3.3 KiB
Go

package server
import (
"context"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
"path/filepath"
"strings"
"testing"
"time"
)
func TestWebStagesCanRunWithoutMutationApproval(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "web.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
source := strings.Replace(pipeline.Template([]string{"research", "process"}, true), "```tools\n```", "```tools\nwebsearch\nwebfetch\n```", 1)
if _, err := store.Submit(ctx, "web", source); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
r, _ := newRunner(ctx, store)
r.execute = func(ctx context.Context, def stage.Definition, input stage.Values) (stage.Result, error) {
tools, explicit, err := stage.ToolNames(def)
if err != nil || !explicit {
t.Error("missing per-stage policy", err)
}
if def.Key == "research" && len(tools) != 2 {
t.Error("research tools missing")
}
if def.Key == "process" && len(tools) != 0 {
t.Error("processing inherited web access")
}
return stage.Result{Outcome: "success", Output: stage.Values{"done": true}}, nil
}
request := protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "web", Input: map[string]any{"prompt": "task"}}
run, err := r.start(request)
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
detail, _ := store.GetRun(ctx, run.ID)
if detail.AllowTools || detail.Status != "succeeded" || len(detail.Steps) != 2 {
t.Fatal("web stage required broad approval", detail)
}
// Scheduling uses the same prompt-only request and accepts declared web tools.
scheduled := "# settings\n```schedule\nevery=15m\n```\n```prompt\ntask\n```\n" + source
if _, err := store.Submit(ctx, "web", scheduled); err != nil {
t.Fatal(err)
}
d, _ := store.Get(ctx, items[0].ID, 0)
due, _ := store.ScheduleDue(ctx, d, 0, d.Graph.Settings.Schedules[0], time.Now())
if err := r.scheduleTick(due, false); err != nil {
t.Fatal(err)
}
r.wg.Wait()
runs, _ := store.ListRuns(ctx)
if len(runs) != 2 {
t.Fatal("scheduled web run not started", runs)
}
}
func TestMutatingToolsRequireManualApproval(t *testing.T) {
for _, tool := range []string{"edit", "shell"} {
ctx, cancel := context.WithCancel(context.Background())
store, err := pipeline.Open(filepath.Join(t.TempDir(), "tools.db"))
if err != nil {
t.Fatal(err)
}
source := strings.Replace(pipeline.Template([]string{"one"}, true), "```tools\n```", "```tools\n"+tool+"\n```", 1)
if _, err := store.Submit(ctx, "tools", source); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
r, _ := newRunner(ctx, store)
request := protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "unsafe", Input: map[string]any{"prompt": "task"}}
if _, err := r.start(request); err == nil || !strings.Contains(err.Error(), "manual tool approval") {
t.Fatal("mutation without approval", tool, err)
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
request.AllowTools = true
if _, err := r.start(request); err != nil {
t.Fatal("approved run rejected", err)
}
r.wg.Wait()
cancel()
store.Close()
}
}