diff --git a/CHANGELOG.md b/CHANGELOG.md index b54cade..16070ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Keep scheduler due checks independent when job names and repository values produce the same colon-separated history key. + - Keep scheduler status scoped to the exact job name when another job or repository history key shares its prefix. - Allow native independent review in either Codex or OpenClaw while preserving pinned, isolated, read-only review and existing validation gates. diff --git a/scheduler/run.go b/scheduler/run.go index 652ac46..27c7a9a 100644 --- a/scheduler/run.go +++ b/scheduler/run.go @@ -52,7 +52,11 @@ func Run(ctx context.Context, opts RunOptions) ([]RunRecord, error) { if err != nil { return nil, err } - last := LastRecords(history) + type runKey struct{ job, repo string } + last := make(map[runKey]RunRecord, len(history)) + for _, record := range history { + last[runKey{record.Job, record.Repo}] = record + } names := selectedNames(opts.Config, opts.Names) records := []RunRecord{} @@ -72,7 +76,7 @@ func Run(ctx context.Context, opts RunOptions) ([]RunRecord, error) { continue } for _, item := range expanded { - lastRun, ok := lastRecord(last, name, item.repo) + lastRun, ok := last[runKey{name, item.repo}] if len(opts.Names) == 0 && !jobDue(now(), job, lastRun, ok) { continue } @@ -91,15 +95,6 @@ func Run(ctx context.Context, opts RunOptions) ([]RunRecord, error) { return records, historyErr } -func lastRecord(records map[string]RunRecord, name, repo string) (RunRecord, bool) { - if repo != "" { - record, ok := records[name+":"+repo] - return record, ok - } - record, ok := records[name] - return record, ok -} - func jobDue(now time.Time, job Job, last RunRecord, ok bool) bool { if !ok || last.Status != "success" { return true diff --git a/scheduler/scheduler_test.go b/scheduler/scheduler_test.go index 9e1ba1e..5c72a6f 100644 --- a/scheduler/scheduler_test.go +++ b/scheduler/scheduler_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "os" "path/filepath" @@ -263,6 +264,63 @@ func TestRunSkipsJobsThatAreNotDue(t *testing.T) { } } +func TestRunKeepsCollidingHistoryIdentitiesSeparate(t *testing.T) { + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + now := time.Date(2026, 5, 22, 12, 0, 0, 0, time.UTC) + for _, tc := range []struct { + name, job, repo, otherJob, otherRepo string + }{ + {"plain job", "sync:repo", "", "sync", "repo"}, + {"repository job", "sync", "repo", "sync:repo", ""}, + {"colon in repository", "sync", "group:repo", "sync:group", "repo"}, + } { + for _, due := range []bool{true, false} { + t.Run(fmt.Sprintf("%s/due=%t", tc.name, due), func(t *testing.T) { + dir := t.TempDir() + paths := Paths{LogDir: filepath.Join(dir, "logs"), StateDir: filepath.Join(dir, "state"), LockPath: filepath.Join(dir, "state", "lock"), History: filepath.Join(dir, "state", "runs.jsonl")} + finished := now.Add(-time.Minute) + otherStatus := "failed" + if due { + finished = now.Add(-2 * time.Hour) + otherStatus = "success" + } + for _, record := range []RunRecord{ + {Job: tc.job, Repo: tc.repo, Status: "success", FinishedAt: finished.Format(time.RFC3339)}, + {Job: tc.otherJob, Repo: tc.otherRepo, Status: otherStatus, FinishedAt: now.Format(time.RFC3339)}, + } { + if err := appendHistory(paths.History, record); err != nil { + t.Fatal(err) + } + } + job := Job{Enabled: true, Every: "1h", Command: []string{exe, "-test.run=^$"}} + if tc.repo != "" { + job.Command = append(job.Command, "--", "{repo}") + job.Repos = []string{tc.repo} + } + cfg := DefaultConfig() + cfg.Jobs[tc.job] = job + records, err := Run(t.Context(), RunOptions{Config: cfg, Paths: paths, Now: func() time.Time { return now }}) + if err != nil { + t.Fatal(err) + } + want := 0 + if due { + want = 1 + } + if len(records) != want { + t.Fatalf("runs = %d, want %d: %#v", len(records), want, records) + } + if due && (records[0].Job != tc.job || records[0].Repo != tc.repo || records[0].Status != "success") { + t.Fatalf("unexpected run: %#v", records[0]) + } + }) + } + } +} + func TestParseEveryRejectsSubMinute(t *testing.T) { if _, err := ParseEvery("30s"); err == nil { t.Fatal("expected reject error")