f061675fc1
- Claude stage CLIs now run in their own session (setsid) with the prompt from a file and stream-json written straight to cli-<n>.out in the evidence directory; owner.json records pid/pgid, session, invocation and output. A server shutdown leaves the CLI running and owned (ErrDetached). Start-up recovery re-adopts it (stage.ClaudeStageReattachable): the next process re-reads the output, follows the live CLI until it exits, or reads the result it left while the server was down; one that ended without a result marks the run interrupted with a clear reason. The outcome repair follow-up is re-adopted too (or asked again if its CLI died without a reply). - Workspace overlap: two folders outside any repository overlap only when they are the same folder or the inner one lies within the outer run's declared write paths; nested-repository rules are unchanged. - Scheduled admissions refused for a busy workspace are recorded as "delayed" and retried every 5 min for up to 2 h (env-configurable), then started or skipped with the reason; the operator gets a low-priority app notice (and ntfy when enabled) when a scheduled run is delayed or skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
137 lines
4.6 KiB
Go
137 lines
4.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
)
|
|
|
|
func scheduleNotices(t *testing.T, s *pipeline.Store) []protocol.AppNotification {
|
|
t.Helper()
|
|
active, _, err := s.ActiveAppNotifications(context.Background(), "", time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var notices []protocol.AppNotification
|
|
for _, n := range active {
|
|
if n.Kind == protocol.NotificationSchedule {
|
|
notices = append(notices, n)
|
|
}
|
|
}
|
|
return notices
|
|
}
|
|
|
|
func waitOccurrence(t *testing.T, s *pipeline.Store, id int64, due time.Time, want string) (string, int64) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
status, diagnostic, run, err := s.SavedScheduleOccurrence(context.Background(), id, due)
|
|
if err == nil && status == want {
|
|
return diagnostic, run
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("occurrence status %q (%q, %v), want %q", status, diagnostic, err, want)
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// A scheduled run whose workspace another run holds is delayed, not skipped:
|
|
// it is recorded as delayed, the operator is told at low priority, and it
|
|
// starts once the workspace is free.
|
|
func TestScheduledRunWaitsForABusyWorkspace(t *testing.T) {
|
|
seamScheduleBusyRetry(t, 20*time.Millisecond, time.Hour)
|
|
ctx, s, r, pid := scheduleFixture(t)
|
|
r.notes = newAppNotifier(s)
|
|
workspace := t.TempDir()
|
|
busy := saveTestSchedule(t, ctx, s, pid, "busy", workspace, "1h", time.Now())
|
|
brief := saveTestSchedule(t, ctx, s, pid, "Daily brief", workspace, "1h", time.Now())
|
|
release := make(chan struct{})
|
|
entered := make(chan struct{}, 2)
|
|
r.execute = func(ctx context.Context, _ stage.Definition, input stage.Values) (stage.Result, error) {
|
|
entered <- struct{}{}
|
|
if prompt, _ := input["prompt"].(string); prompt == "busy" {
|
|
<-release
|
|
}
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Done"}}, nil
|
|
}
|
|
if err := r.scheduleTick(dueOf(t, busy), false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
<-entered
|
|
due := dueOf(t, brief)
|
|
if err := r.scheduleTick(due, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
diagnostic, _ := waitOccurrence(t, s, brief.ID, due, "delayed")
|
|
if !strings.Contains(diagnostic, "busy workspace") || !strings.Contains(diagnostic, "already active") {
|
|
t.Fatalf("delay diagnostic %q", diagnostic)
|
|
}
|
|
notices := scheduleNotices(t, s)
|
|
if len(notices) != 1 || notices[0].Priority != 2 || !strings.Contains(notices[0].Title, "delayed") || !strings.Contains(notices[0].Title, "Daily brief") {
|
|
t.Fatalf("delay notices %+v", notices)
|
|
}
|
|
close(release)
|
|
diagnostic, run := waitOccurrence(t, s, brief.ID, due, "started")
|
|
r.scheduleAdmissions.Wait()
|
|
r.wg.Wait()
|
|
if run == 0 || !strings.Contains(diagnostic, "started after waiting") {
|
|
t.Fatalf("delayed run not admitted: run=%d %q", run, diagnostic)
|
|
}
|
|
runs, err := s.ListRuns(ctx)
|
|
if err != nil || len(runs) != 2 {
|
|
t.Fatalf("runs %+v %v", runs, err)
|
|
}
|
|
}
|
|
|
|
// Waiting is bounded: past the limit the occurrence is skipped, saying why,
|
|
// and the operator is told.
|
|
func TestScheduledRunGivesUpOnABusyWorkspace(t *testing.T) {
|
|
seamScheduleBusyRetry(t, 20*time.Millisecond, 150*time.Millisecond)
|
|
ctx, s, r, pid := scheduleFixture(t)
|
|
r.notes = newAppNotifier(s)
|
|
workspace := t.TempDir()
|
|
busy := saveTestSchedule(t, ctx, s, pid, "busy", workspace, "1h", time.Now())
|
|
brief := saveTestSchedule(t, ctx, s, pid, "Daily brief", workspace, "1h", time.Now())
|
|
release := make(chan struct{})
|
|
defer close(release)
|
|
entered := make(chan struct{}, 2)
|
|
r.execute = func(ctx context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) {
|
|
entered <- struct{}{}
|
|
select {
|
|
case <-release:
|
|
case <-ctx.Done():
|
|
}
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Done"}}, nil
|
|
}
|
|
if err := r.scheduleTick(dueOf(t, busy), false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
<-entered
|
|
due := dueOf(t, brief)
|
|
if claimed, err := s.ClaimSavedSchedule(ctx, *brief, due, due.Add(time.Hour), "claimed"); err != nil || !claimed {
|
|
t.Fatal(claimed, err)
|
|
}
|
|
r.admitScheduled(*brief, due)
|
|
diagnostic, run := waitOccurrence(t, s, brief.ID, due, "skipped")
|
|
if run != 0 || !strings.Contains(diagnostic, "gave up after waiting") {
|
|
t.Fatalf("skip diagnostic %q run=%d", diagnostic, run)
|
|
}
|
|
notices := scheduleNotices(t, s)
|
|
var titles []string
|
|
for _, n := range notices {
|
|
titles = append(titles, n.Title)
|
|
if n.Priority != 2 {
|
|
t.Fatalf("schedule notice is not low priority: %+v", n)
|
|
}
|
|
}
|
|
if len(notices) != 2 || !strings.Contains(strings.Join(titles, "|"), "skipped") {
|
|
t.Fatalf("notices %v", titles)
|
|
}
|
|
}
|