124 lines
4.8 KiB
Go
124 lines
4.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"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"
|
|
"git.a-wan.net/alan/pipelines/internal/summarise"
|
|
)
|
|
|
|
func TestScheduledRunPreservesWriteScopeAndWorkflowPermissions(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
allowTools bool
|
|
declaredTools string
|
|
}{
|
|
{"restricted coding", true, "edit"},
|
|
{"no tools", false, ""},
|
|
{"run flag does not grant undeclared tools", true, ""},
|
|
{"workflow tools remain authoritative", false, "edit"},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
ctx, s, r, pid := scheduleFixture(t)
|
|
version := 1
|
|
if test.declaredTools != "" {
|
|
source := strings.Replace(pipeline.Template([]string{"one"}, true), "```tools\n```", "```tools\n"+test.declaredTools+"\n```", 1)
|
|
var err error
|
|
version, err = s.Submit(ctx, "research", source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
spec := protocol.SavedSchedule{Name: "restricted run", PipelineID: pid, Version: version, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": "scoped coding", "pwd": "/untrusted/input/folder"}, WritePaths: []string{"internal/one.go", "README.md"}, AllowTools: test.allowTools, Rule: protocol.Schedule{Every: "1m"}, NotifyMode: "off", Title: "Scoped task"}
|
|
entry, err := s.SaveSchedule(ctx, spec, time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries, err := s.ListSchedules(ctx, pid)
|
|
if err != nil || len(entries) != 1 || entries[0].AllowTools != test.allowTools || !slices.Equal(entries[0].WritePaths, spec.WritePaths) {
|
|
t.Fatal("saved scope roundtrip", entries, err)
|
|
}
|
|
calls := 0
|
|
r.execute = func(_ context.Context, def stage.Definition, input stage.Values) (stage.Result, error) {
|
|
calls++
|
|
if input["pwd"] != entry.Workspace {
|
|
t.Error("saved input.pwd overrode server workspace", input["pwd"])
|
|
}
|
|
tools, explicit, err := stage.ToolNames(def)
|
|
if err != nil || !explicit || strings.Join(tools, ",") != test.declaredTools {
|
|
t.Error("run flag widened stage permissions", tools, explicit, err)
|
|
}
|
|
// Scope is runtime policy, not prompt input: verify the committed run
|
|
// request at the fake executor boundary, before any native/model work.
|
|
runs, err := s.ListRuns(ctx)
|
|
if err != nil || len(runs) != 1 {
|
|
t.Error(runs, err)
|
|
return stage.Result{}, nil
|
|
}
|
|
detail, err := s.GetRun(ctx, runs[0].ID)
|
|
expectedTools := test.allowTools || test.declaredTools != ""
|
|
if err != nil || detail.AllowTools != expectedTools || !slices.Equal(detail.WritePaths, spec.WritePaths) || detail.Workspace != entry.Workspace || detail.Input["pwd"] != entry.Workspace {
|
|
t.Error("dispatch dropped scope", detail, err)
|
|
}
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"done": true}}, nil
|
|
}
|
|
if err = r.scheduleTick(dueOf(t, entry), false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
runs, err := s.ListRuns(ctx)
|
|
if err != nil || len(runs) != 1 || runs[0].Status != "succeeded" || calls != 1 {
|
|
t.Fatal(runs, calls, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestScheduleRechecksWriteScopeBeforeTitleOrExecution(t *testing.T) {
|
|
ctx, s, r, pid := scheduleFixture(t)
|
|
workspace := t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(workspace, "allowed"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
spec := protocol.SavedSchedule{Name: "restricted", PipelineID: pid, Version: 1, Enabled: true, Workspace: workspace, Input: map[string]any{"prompt": "task"}, WritePaths: []string{"allowed/new.go"}, AllowTools: true, Rule: protocol.Schedule{Every: "1m"}, NotifyMode: "off"}
|
|
entry, err := s.SaveSchedule(ctx, spec, time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// A scope valid when saved can become unsafe before dispatch.
|
|
if err = os.Rename(filepath.Join(workspace, "allowed"), filepath.Join(workspace, "original")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = os.Symlink(t.TempDir(), filepath.Join(workspace, "allowed")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.title = func(context.Context, string) summarise.Title {
|
|
t.Error("invalid scope reached title model")
|
|
return summarise.Title{}
|
|
}
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
t.Error("invalid scope reached executor")
|
|
return stage.Result{}, nil
|
|
}
|
|
if err = r.scheduleTick(dueOf(t, entry), false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
// The exact same shared policy rejects manual admission as well.
|
|
if _, err = r.start(protocol.Message{PipelineID: pid, Version: 1, Workspace: workspace, Input: spec.Input, WritePaths: spec.WritePaths, AllowTools: true, RequestKey: "manual-unsafe"}); err == nil {
|
|
t.Fatal("manual path validation diverged")
|
|
}
|
|
runs, err := s.ListRuns(ctx)
|
|
if err != nil || len(runs) != 0 {
|
|
t.Fatal("invalid scope admitted work", runs, err)
|
|
}
|
|
}
|