Bind legacy schedule workspaces only at execution startup
This commit is contained in:
@@ -83,14 +83,6 @@ func (s *Store) importLegacySchedules() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
workspace, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
workspace, err = filepath.EvalSymlinks(workspace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
for _, l := range all {
|
||||
if l.graph.Settings == nil {
|
||||
@@ -98,7 +90,9 @@ func (s *Store) importLegacySchedules() error {
|
||||
}
|
||||
for slot, rule := range l.graph.Settings.Schedules {
|
||||
if rule.Timezone == "" {
|
||||
rule.Timezone = time.Local.String()
|
||||
// Preserve execution-server-local semantics without capturing an
|
||||
// offline opener's timezone. ScheduleLocation resolves Local there.
|
||||
rule.Timezone = "Local"
|
||||
}
|
||||
next, err := NextSchedule(rule, now)
|
||||
if err != nil {
|
||||
@@ -111,7 +105,10 @@ func (s *Store) importLegacySchedules() error {
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
spec := protocol.SavedSchedule{Name: fmt.Sprintf("%s (legacy %d)", l.name, slot+1), PipelineID: l.id, Version: l.version, Enabled: true, Workspace: workspace, Input: map[string]any{"prompt": l.graph.Settings.Prompt}, Rule: rule}
|
||||
// Opening a store does not identify the execution server: maintenance
|
||||
// and offline clients can migrate it from any working directory.
|
||||
// Only runner startup may bind this trusted legacy exception.
|
||||
spec := protocol.SavedSchedule{Name: fmt.Sprintf("%s (legacy %d)", l.name, slot+1), PipelineID: l.id, Version: l.version, Enabled: true, Input: map[string]any{"prompt": l.graph.Settings.Prompt}, Rule: rule}
|
||||
text, err := json.Marshal(spec)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -135,6 +132,23 @@ func (s *Store) importLegacySchedules() error {
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// BindLegacyScheduleWorkspace captures the old runtime-default workspace once,
|
||||
// from the execution server before admission APIs or the scheduler are available.
|
||||
// Store.Open never supplies it. Blank workspaces are allowed only on imported
|
||||
// legacy rows; normal saves always require an explicit accessible directory.
|
||||
// Only this missing field is filled: revisions, cadence, enabled state, tombstones,
|
||||
// provenance and any workspace already chosen by the operator remain untouched.
|
||||
func (s *Store) BindLegacyScheduleWorkspace(ctx context.Context, workspace string) error {
|
||||
resolved, err := ResolveRunWorkspace(workspace, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
_, err = s.db.ExecContext(ctx, `UPDATE saved_schedules SET spec_json=json_set(spec_json,'$.workspace',?) WHERE legacy_version IS NOT NULL AND legacy_slot IS NOT NULL AND deleted=0 AND COALESCE(json_extract(spec_json,'$.workspace'),'')=''`, resolved)
|
||||
return err
|
||||
}
|
||||
|
||||
const savedScheduleSelect = `SELECT id,revision,spec_json,next_due,created_at,updated_at FROM saved_schedules`
|
||||
|
||||
func scanSavedSchedule(row interface{ Scan(...any) error }) (protocol.SavedSchedule, error) {
|
||||
@@ -391,7 +405,7 @@ type ScheduleAdmission struct {
|
||||
}
|
||||
|
||||
// ResolveRunWorkspace shares exactly the server's manual-run folder semantics.
|
||||
// Only the one-time legacy import may capture the old process-default directory.
|
||||
// Only execution-server startup may capture the old legacy default directory.
|
||||
func ResolveRunWorkspace(selected string, required bool) (string, error) {
|
||||
if selected == "" {
|
||||
if required {
|
||||
|
||||
@@ -236,9 +236,12 @@ func TestLegacyScheduleImportPreservesHistoryAndNeverResurrects(t *testing.T) {
|
||||
if err != nil || len(entries) != 2 {
|
||||
t.Fatal(entries, err)
|
||||
}
|
||||
if entries[0].NextDue != due || entries[0].Input["prompt"] != "Legacy research prompt" || entries[0].Workspace == "" || entries[0].Version != 1 || entries[0].Rule.Timezone == "" {
|
||||
if entries[0].NextDue != due || entries[0].Input["prompt"] != "Legacy research prompt" || entries[0].Workspace != "" || entries[0].Version != 1 || entries[0].Rule.Timezone == "" {
|
||||
t.Fatal(entries[0])
|
||||
}
|
||||
if entries[0].Rule.Timezone != "Local" || entries[1].Rule.Timezone != "Europe/London" {
|
||||
t.Fatal("migration changed timezone semantics", entries)
|
||||
}
|
||||
run, err := s.GetRun(ctx, historical)
|
||||
if err != nil || !run.Scheduled || run.ScheduleID != entries[0].ID {
|
||||
t.Fatal(run, err)
|
||||
@@ -251,9 +254,30 @@ func TestLegacyScheduleImportPreservesHistoryAndNeverResurrects(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries[1].Enabled = false
|
||||
entries[1].Workspace = t.TempDir()
|
||||
if _, err = s.SaveSchedule(ctx, entries[1], now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
beforeBind, err := s.ListSchedules(ctx, pid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.BindLegacyScheduleWorkspace(ctx, t.TempDir()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
afterBind, err := s.ListSchedules(ctx, pid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
beforeJSON, _ := json.Marshal(beforeBind)
|
||||
afterJSON, _ := json.Marshal(afterBind)
|
||||
if string(beforeJSON) != string(afterJSON) {
|
||||
t.Fatal("runtime binding overwrote an operator-edited paused row", beforeBind, afterBind)
|
||||
}
|
||||
var deletedWorkspace string
|
||||
if err = s.db.QueryRow(`SELECT json_extract(spec_json,'$.workspace') FROM saved_schedules WHERE id=? AND deleted=1`, entries[0].ID).Scan(&deletedWorkspace); err != nil || deletedWorkspace != "" {
|
||||
t.Fatal("binding changed a tombstone", deletedWorkspace, err)
|
||||
}
|
||||
other, err := Open(s.path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
)
|
||||
|
||||
func TestLegacyScheduleBindsExecutionDefaultNotOfflineOpenDirectory(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "legacy.db")
|
||||
seed, err := pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = seed.Submit(ctx, "legacy research", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
seed.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := seed.List(ctx)
|
||||
if err != nil {
|
||||
seed.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
pid := items[0].ID
|
||||
seed.Close()
|
||||
// Construct an old published schedule and its exact persisted cadence in a
|
||||
// scratch DB. New publication APIs intentionally cannot create this source.
|
||||
source := "# settings\n```schedule\nevery=15m\n```\n```prompt\nLegacy research\n```\n" + pipeline.Template([]string{"one"}, true)
|
||||
graph, err := pipeline.Parse(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encoded, err := json.Marshal(graph)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(source)))
|
||||
due := time.Now().Add(15 * time.Minute).UTC()
|
||||
db, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(`UPDATE pipeline_versions SET source=?,graph_json=?,content_hash=? WHERE pipeline_id=? AND version=1`, source, string(encoded), hash, pid); err != nil {
|
||||
db.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(`INSERT INTO schedule_state(pipeline_id,version,slot,next_due) VALUES(?,1,0,?)`, pid, due.Format(time.RFC3339Nano)); err != nil {
|
||||
db.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(`DELETE FROM schedule_migrations`); err != nil {
|
||||
db.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.Close()
|
||||
|
||||
// An offline tool is allowed to open/migrate the database from arbitrary A.
|
||||
a, b, c := t.TempDir(), t.TempDir(), t.TempDir()
|
||||
t.Chdir(a)
|
||||
store, err := pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
entries, err := store.ListSchedules(ctx, pid)
|
||||
if err != nil || len(entries) != 1 || entries[0].Workspace != "" || entries[0].NextDue != due.Format(time.RFC3339Nano) {
|
||||
t.Fatal("offline open captured an untrusted default", entries, err)
|
||||
}
|
||||
original := entries[0]
|
||||
// Runtime B, not Store.Open's A, is the only trusted old default.
|
||||
t.Chdir(b)
|
||||
resolvedB, err := pipeline.ResolveRunWorkspace(b, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runCtx, cancel := context.WithCancel(ctx)
|
||||
r, err := newRunner(runCtx, store)
|
||||
if err != nil {
|
||||
cancel()
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { cancel(); r.schedulerWG.Wait(); r.wg.Wait() }()
|
||||
entries, err = store.ListSchedules(ctx, pid)
|
||||
if err != nil || len(entries) != 1 || entries[0].Workspace != resolvedB || entries[0].ID != original.ID || entries[0].Revision != original.Revision || entries[0].NextDue != original.NextDue || entries[0].Input["prompt"] != original.Input["prompt"] {
|
||||
t.Fatal("runtime did not bind only the missing folder", entries, err)
|
||||
}
|
||||
bound := entries[0]
|
||||
bound.Workspace = ""
|
||||
beforeJSON, _ := json.Marshal(original)
|
||||
afterJSON, _ := json.Marshal(bound)
|
||||
if string(beforeJSON) != string(afterJSON) {
|
||||
t.Fatal("binding changed other saved information", original, bound)
|
||||
}
|
||||
detail, err := store.Get(ctx, pid, 1)
|
||||
if err != nil || detail.Source != source || detail.ContentHash != hash {
|
||||
t.Fatal("binding changed immutable history", detail, err)
|
||||
}
|
||||
// Keep even test execution explicitly silent; the inherited workspace and
|
||||
// cadence stay unchanged by this ordinary run-information edit.
|
||||
entry := entries[0]
|
||||
entry.NotifyMode = "off"
|
||||
if _, err = store.SaveSchedule(ctx, entry, time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.execute = func(_ context.Context, _ stage.Definition, input stage.Values) (stage.Result, error) {
|
||||
if input["pwd"] != resolvedB {
|
||||
t.Error("dispatch used an offline/client cwd", input["pwd"])
|
||||
}
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{"done": true}}, nil
|
||||
}
|
||||
if err = r.scheduleTick(due, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
runs, err := store.ListRuns(ctx)
|
||||
if err != nil || len(runs) != 1 || runs[0].Workspace != resolvedB || !runs[0].Scheduled || runs[0].ScheduleID != original.ID {
|
||||
t.Fatal(runs, err)
|
||||
}
|
||||
cancel()
|
||||
r.schedulerWG.Wait()
|
||||
r.wg.Wait()
|
||||
store.Close()
|
||||
|
||||
// Restart from a different runtime C must not rebind explicit B.
|
||||
t.Chdir(c)
|
||||
reopened, err := pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
ctx2, cancel2 := context.WithCancel(ctx)
|
||||
defer cancel2()
|
||||
r2, err := newRunner(ctx2, reopened)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { cancel2(); r2.schedulerWG.Wait(); r2.wg.Wait() }()
|
||||
entries, err = reopened.ListSchedules(ctx, pid)
|
||||
if err != nil || len(entries) != 1 || entries[0].ID != original.ID || entries[0].Workspace != resolvedB {
|
||||
t.Fatal("restart rebound a saved default", entries, err)
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,16 @@ func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
|
||||
if err := s.AcquireRunnerLock(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Store.Open is also used by offline tools, whose cwd is not execution
|
||||
// authority. Bind imported legacy defaults only here, before serving requests
|
||||
// or starting scheduler workers, and never rebind an explicit saved folder.
|
||||
workspace, err := pipeline.ResolveRunWorkspace("", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = s.BindLegacyScheduleWorkspace(ctx, workspace); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sessions, err := s.ActiveSessionOwners(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user