Preserve saved schedule run-level write scope

This commit is contained in:
2026-10-21 01:03:39 +01:00
parent 8c3f116190
commit fe02d153f1
8 changed files with 279 additions and 28 deletions
+2 -26
View File
@@ -152,32 +152,8 @@ func (r *runner) startWithSchedule(request protocol.Message, origin *pipeline.Sc
if err != nil {
return nil, err
}
if len(request.WritePaths) > 20 {
return nil, fmt.Errorf("too many write paths")
}
for _, path := range request.WritePaths {
if filepath.IsAbs(path) || filepath.Clean(path) != path || path == ".." || strings.HasPrefix(path, ".."+string(os.PathSeparator)) || strings.ContainsAny(path, "*?\\") {
return nil, fmt.Errorf("optional write paths must be exact run-directory-relative paths")
}
parent := filepath.Join(workspace, path)
for {
_, err := os.Lstat(parent)
if err == nil {
break
}
if !os.IsNotExist(err) {
return nil, err
}
next := filepath.Dir(parent)
if next == parent {
return nil, fmt.Errorf("write path is unavailable")
}
parent = next
}
resolved, err := filepath.EvalSymlinks(parent)
if err != nil || (resolved != workspace && !strings.HasPrefix(resolved, workspace+string(os.PathSeparator))) {
return nil, fmt.Errorf("write path must stay within the run directory")
}
if err = pipeline.ValidateRunWritePaths(workspace, request.WritePaths); err != nil {
return nil, err
}
request.Input = maps.Clone(request.Input)
if request.Input == nil {
+123
View File
@@ -0,0 +1,123 @@
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)
}
}
+10 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"net/http/httptest"
"path/filepath"
"slices"
"strings"
"testing"
"time"
@@ -55,12 +56,15 @@ func TestScheduleAPIOverWebSocket(t *testing.T) {
return response
}
// Saving run information is not definition authoring, and works on run-only servers.
spec := protocol.SavedSchedule{Name: "first", PipelineID: items[0].ID, Version: 1, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": "first research", "arbitrary": "retained"}, Rule: protocol.Schedule{Every: "15m"}, Model: "openai/gpt", NotifyMode: "off", Title: "Research"}
spec := protocol.SavedSchedule{Name: "first", PipelineID: items[0].ID, Version: 1, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": "first research", "arbitrary": "retained"}, WritePaths: []string{"internal/one.go"}, AllowTools: true, Rule: protocol.Schedule{Every: "15m"}, Model: "openai/gpt", NotifyMode: "off", Title: "Research"}
response := rpc(protocol.Message{Type: protocol.SaveSchedule, Schedule: &spec, RequestKey: "editor-first"})
if response.Type != protocol.SavedScheduleReply || response.Schedule == nil || response.Schedule.ID == 0 || response.Schedule.Revision != 1 {
t.Fatal(response)
}
a := *response.Schedule
if !a.AllowTools || !slices.Equal(a.WritePaths, spec.WritePaths) {
t.Fatal("scope omitted from save reply", a)
}
response = rpc(protocol.Message{Type: protocol.SaveSchedule, Schedule: &spec, RequestKey: "editor-first"})
if response.Schedule == nil || response.Schedule.ID != a.ID || response.Schedule.Revision != a.Revision {
t.Fatal("lost reply duplicated timer", response)
@@ -82,6 +86,9 @@ func TestScheduleAPIOverWebSocket(t *testing.T) {
if response.Type != protocol.Schedules || len(response.Schedules) != 2 {
t.Fatal(response)
}
if !response.Schedules[0].AllowTools || !slices.Equal(response.Schedules[0].WritePaths, spec.WritePaths) {
t.Fatal("scope omitted from list reply", response)
}
a.Enabled = false
response = rpc(protocol.Message{Type: protocol.SaveSchedule, Schedule: &a})
if response.Schedule == nil || response.Schedule.Enabled || response.Schedule.Revision != 2 {
@@ -121,6 +128,8 @@ func TestScheduleAPIOverWebSocket(t *testing.T) {
func(s *protocol.SavedSchedule) { s.Model = "invalid" },
func(s *protocol.SavedSchedule) { s.NotifyMode = "invalid" },
func(s *protocol.SavedSchedule) { s.OutputFormat = "invalid" },
func(s *protocol.SavedSchedule) { s.WritePaths = []string{"../outside.go"} },
func(s *protocol.SavedSchedule) { s.WritePaths = []string{"*.go"} },
} {
bad := spec
mutate(&bad)
+1 -1
View File
@@ -72,7 +72,7 @@ func (r *runner) scheduleTick(now time.Time, recovery bool) error {
if !claimed || status != "claimed" {
continue
}
request := protocol.Message{PipelineID: item.PipelineID, Version: item.Version, RequestKey: fmt.Sprintf("saved-schedule:%d:%d", item.ID, due.UnixNano()), Input: item.Input, Workspace: item.Workspace, Model: item.Model, StageModels: item.StageModels, NotifyMode: item.NotifyMode, Title: item.Title, OutputFormat: item.OutputFormat}
request := protocol.Message{PipelineID: item.PipelineID, Version: item.Version, RequestKey: fmt.Sprintf("saved-schedule:%d:%d", item.ID, due.UnixNano()), Input: item.Input, Workspace: item.Workspace, WritePaths: item.WritePaths, AllowTools: item.AllowTools, Model: item.Model, StageModels: item.StageModels, NotifyMode: item.NotifyMode, Title: item.Title, OutputFormat: item.OutputFormat}
run, err := r.startWithSchedule(request, &pipeline.ScheduleAdmission{ID: item.ID, Revision: item.Revision, Due: due, Name: item.Name})
diagnostic := ""
status = "started"