diff --git a/CHANGELOG.md b/CHANGELOG.md index 65b2cc1..a69c154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- Save discovered executables as absolute paths so relative app selections work from another runner directory. + +- Preserve each job's latest text status when history keys collide or runs finish in the same second. + - Keep scheduler due checks scoped to the exact job and repository when colon-containing names share a legacy history key. - Keep scheduler status scoped to the exact job name when another job or repository history key shares its prefix. diff --git a/README.md b/README.md index 785e997..391a2d7 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,8 @@ schedule source-specific commands such as Photos' `import_apple`. Discovery reports malformed metadata and missing manifest IDs in the app's `error` field. Supported legacy binaries retain their fallback manifests. Legacy fallback also recognizes supported binaries selected by absolute or -relative executable path. +relative executable path. Discovery records an absolute executable path so saved +jobs keep working when the runner starts from another directory. Scheduler due checks use the exact job and repository identity, so colon-containing names do not share scheduling history. diff --git a/cmd/crawlctl/main.go b/cmd/crawlctl/main.go index ba3a273..c34c699 100644 --- a/cmd/crawlctl/main.go +++ b/cmd/crawlctl/main.go @@ -294,7 +294,7 @@ func (a *app) runStatus(args []string) error { if job.Enabled { state = "never" } - if record, ok := latestRecordForJob(last, name); ok { + if record, ok := latestRecordForJob(history, name); ok { state = record.Status detail = " " + record.FinishedAt } @@ -303,17 +303,14 @@ func (a *app) runStatus(args []string) error { return nil } -func latestRecordForJob(last map[string]scheduler.RunRecord, name string) (scheduler.RunRecord, bool) { - if record, ok := last[name]; ok && record.Job == name { - return record, true - } +func latestRecordForJob(history []scheduler.RunRecord, name string) (scheduler.RunRecord, bool) { var latest scheduler.RunRecord found := false - for _, record := range last { + for _, record := range history { if record.Job != name { continue } - if !found || record.FinishedAt > latest.FinishedAt { + if !found || record.FinishedAt >= latest.FinishedAt { latest = record found = true } diff --git a/cmd/crawlctl/main_test.go b/cmd/crawlctl/main_test.go index dd3136c..b6a08d1 100644 --- a/cmd/crawlctl/main_test.go +++ b/cmd/crawlctl/main_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "os" "path/filepath" "runtime" @@ -38,9 +39,9 @@ func TestResolveVersion(t *testing.T) { } func TestLatestRecordForJobAggregatesRepoRuns(t *testing.T) { - records := map[string]scheduler.RunRecord{ - "gitcrawl:openclaw/old": {Job: "gitcrawl", Repo: "openclaw/old", Status: "success", FinishedAt: "2026-05-22T10:00:00Z"}, - "gitcrawl:openclaw/new": {Job: "gitcrawl", Repo: "openclaw/new", Status: "failed", FinishedAt: "2026-05-22T11:00:00Z"}, + records := []scheduler.RunRecord{ + {Job: "gitcrawl", Repo: "openclaw/old", Status: "success", FinishedAt: "2026-05-22T10:00:00Z"}, + {Job: "gitcrawl", Repo: "openclaw/new", Status: "failed", FinishedAt: "2026-05-22T11:00:00Z"}, } record, ok := latestRecordForJob(records, "gitcrawl") if !ok { @@ -51,6 +52,17 @@ func TestLatestRecordForJobAggregatesRepoRuns(t *testing.T) { } } +func TestLatestRecordForJobUsesLastEntryAtSameTime(t *testing.T) { + history := []scheduler.RunRecord{ + {Job: "sync", Status: "failed", FinishedAt: "2026-05-22T10:00:00Z"}, + {Job: "sync", Status: "success", FinishedAt: "2026-05-22T10:00:00Z"}, + } + record, ok := latestRecordForJob(history, "sync") + if !ok || record.Status != "success" { + t.Fatalf("latest record = %+v, found = %v; want the later successful run", record, ok) + } +} + func TestStatusDoesNotUseAnotherJobsHistory(t *testing.T) { for _, tc := range []struct { name, job, recordedJob, repo string @@ -91,6 +103,50 @@ func TestStatusDoesNotUseAnotherJobsHistory(t *testing.T) { } } +func TestStatusKeepsJobsWithCollidingHistoryKeys(t *testing.T) { + for _, reversed := range []bool{false, true} { + t.Run(fmt.Sprint(reversed), func(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "crawlctl.toml") + cfg := scheduler.DefaultConfig() + for _, name := range []string{"sync", "sync:owner/repo"} { + cfg.Jobs[name] = scheduler.Job{Enabled: true, Command: []string{"unused"}} + } + paths, err := scheduler.Save(configPath, cfg, false) + if err != nil { + t.Fatal(err) + } + records := []scheduler.RunRecord{ + {Job: "sync:owner/repo", Status: "failed", FinishedAt: "2026-05-22T10:00:00Z"}, + {Job: "sync", Repo: "owner/repo", Status: "success", FinishedAt: "2026-05-22T11:00:00Z"}, + } + if reversed { + records[0], records[1] = records[1], records[0] + } + var history bytes.Buffer + for _, record := range records { + if err := json.NewEncoder(&history).Encode(record); err != nil { + t.Fatal(err) + } + } + if err := os.MkdirAll(filepath.Dir(paths.History), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(paths.History, history.Bytes(), 0o600); err != nil { + t.Fatal(err) + } + var stdout bytes.Buffer + a := app{stdout: &stdout, stderr: ioDiscard{}, config: configPath} + if err := a.runStatus(nil); err != nil { + t.Fatal(err) + } + want := "sync: success 2026-05-22T11:00:00Z\nsync:owner/repo: failed 2026-05-22T10:00:00Z\n" + if stdout.String() != want { + t.Fatalf("status = %q, want %q", stdout.String(), want) + } + }) + } +} + func TestRunJSONDoesNotIncludeProgress(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("uses POSIX echo") diff --git a/scheduler/discover.go b/scheduler/discover.go index 12f1ce1..3acf8b4 100644 --- a/scheduler/discover.go +++ b/scheduler/discover.go @@ -51,6 +51,11 @@ func discoverOne(ctx context.Context, binary string) App { app.Error = err.Error() return app } + path, err = filepath.Abs(path) + if err != nil { + app.Error = err.Error() + return app + } app.Found = true app.Path = path cmd := exec.CommandContext(ctx, path, "metadata", "--json") diff --git a/scheduler/discover_test.go b/scheduler/discover_test.go index 578d999..f3a80a2 100644 --- a/scheduler/discover_test.go +++ b/scheduler/discover_test.go @@ -149,3 +149,38 @@ func TestDiscoverLegacyBinaryPaths(t *testing.T) { } } } + +func TestDiscoveredRelativeExecutableRunsFromAnotherDirectory(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("synthetic POSIX executable fixture") + } + binDir := t.TempDir() + executable := filepath.Join(binDir, "wacrawl") + if err := os.WriteFile(executable, []byte("#!/bin/sh\nif [ \"$1\" = metadata ]; then exit 2; fi\nprintf 'synthetic crawler ran\\n'\n"), 0o700); err != nil { + t.Fatal(err) + } + t.Chdir(binDir) + app := Discover(context.Background(), []string{"./wacrawl"})[0] + job, ok := DefaultJobForApp(app, nil) + if !app.Found || !app.Legacy || !ok { + t.Fatalf("relative discovery = %+v, job = %+v", app, job) + } + cfg := DefaultConfig() + cfg.Jobs = map[string]Job{"wacrawl": job} + paths, err := Save(filepath.Join(t.TempDir(), "crawlctl.toml"), cfg, false) + if err != nil { + t.Fatal(err) + } + t.Chdir(t.TempDir()) + loaded, _, err := Load(paths.ConfigPath) + if err != nil { + t.Fatal(err) + } + records, err := Run(context.Background(), RunOptions{Config: loaded, Paths: paths, Names: []string{"wacrawl"}}) + if err != nil || len(records) != 1 || records[0].Status != "success" { + t.Fatalf("relative selection from another directory: records = %+v, error = %v", records, err) + } + if app.Path != executable || loaded.Jobs["wacrawl"].Command[0] != executable { + t.Fatalf("selected executable not retained absolutely: app = %+v, command = %v", app, loaded.Jobs["wacrawl"].Command) + } +}