From 2e1960dfe004344feb6e70da6f2c78b3e8716bca Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 23 Sep 2026 01:37:12 +1000 Subject: [PATCH] refactor(scheduling): delete the legacy file-backed cronstore The schedules feature no longer runs on it: jobs live in the state store as the schedules control-plane document, read and written through the control-plane client (scheduleResourceStore), and the legacy jobs.json migration into that document has run its course. What remained of internal/infrastructure/cronstore was 1,600 lines of file store, OS-level cross-process locking and schema-versioned JSON envelope -- plus the schedule vocabulary held hostage inside it. The vocabulary moves to internal/domain/scheduling, which is where migration-decisions already said a schedules vocabulary belongs: a domain vocabulary package must not import infrastructure. JobSpec, Target, Payload, the kind constants, Schedule and Duration move unchanged -- same wire shape, same JSON tags -- with the two sentinel errors callers matched outside the package (ErrJobNotFound, ErrScheduleUnsupported) and ErrInvalidSpec, which the schedule arithmetic itself raises. The store's own sentinels die with the store. The legacy migration path (migrateLegacySchedules, readLegacyJobs) goes with it: its only dependency was cronstore.Open, and its work is done. crondelivery keeps its consumer-owned contracts; the compile-time assertions that pinned *cronstore.Store move to the production implementation, scheduleResourceStore, in internal/app/archied. The package's fixtures swap the real file store for an in-memory one with the same Get/MarkRun/Due semantics, so the run-recording acceptance test still asserts against real schedule arithmetic. A future storage backend (PocketBase) implements the same vocabulary. --- docs/architecture/migration-decisions.md | 7 +- .../archied/migrate_legacy_schedules_test.go | 206 ------ internal/app/archied/scheduling.go | 23 +- internal/app/archied/state_store.go | 82 +-- internal/app/controlplane/client.go | 10 +- internal/app/controlplane/schedules.go | 11 +- internal/app/controlplane/schedules_test.go | 6 +- internal/app/controlplane/schema.go | 4 +- internal/domain/scheduling/jobspec.go | 112 +++ .../scheduling}/schedule.go | 2 +- .../infrastructure/crondelivery/contract.go | 31 +- .../crondelivery/helpers_test.go | 129 +++- .../infrastructure/crondelivery/router.go | 9 +- .../crondelivery/router_test.go | 69 +- .../crondelivery/runner_test.go | 5 +- internal/infrastructure/cronstore/doc.go | 41 -- internal/infrastructure/cronstore/errors.go | 39 - internal/infrastructure/cronstore/kind.go | 24 - .../infrastructure/cronstore/kind_test.go | 148 ---- .../infrastructure/cronstore/lock_unix.go | 68 -- .../infrastructure/cronstore/lock_windows.go | 53 -- .../infrastructure/cronstore/process_lock.go | 33 - .../cronstore/schedule_duration_test.go | 91 --- internal/infrastructure/cronstore/store.go | 665 ------------------ .../cronstore/store_review_test.go | 485 ------------- .../infrastructure/cronstore/store_test.go | 512 -------------- 26 files changed, 297 insertions(+), 2568 deletions(-) delete mode 100644 internal/app/archied/migrate_legacy_schedules_test.go create mode 100644 internal/domain/scheduling/jobspec.go rename internal/{infrastructure/cronstore => domain/scheduling}/schedule.go (99%) delete mode 100644 internal/infrastructure/cronstore/doc.go delete mode 100644 internal/infrastructure/cronstore/errors.go delete mode 100644 internal/infrastructure/cronstore/kind.go delete mode 100644 internal/infrastructure/cronstore/kind_test.go delete mode 100644 internal/infrastructure/cronstore/lock_unix.go delete mode 100644 internal/infrastructure/cronstore/lock_windows.go delete mode 100644 internal/infrastructure/cronstore/process_lock.go delete mode 100644 internal/infrastructure/cronstore/schedule_duration_test.go delete mode 100644 internal/infrastructure/cronstore/store.go delete mode 100644 internal/infrastructure/cronstore/store_review_test.go delete mode 100644 internal/infrastructure/cronstore/store_test.go diff --git a/docs/architecture/migration-decisions.md b/docs/architecture/migration-decisions.md index 0e4a4a660..326c1f348 100644 --- a/docs/architecture/migration-decisions.md +++ b/docs/architecture/migration-decisions.md @@ -264,9 +264,10 @@ around is no longer a boundary. returns and validates `workflow.ExecutionSettings`, so any package holding it links the workflow domain and therefore `internal/agentexec`. This is why the client cannot move as one package -- see open question 2. -- The schedules document, whose type is `cronstore.JobSpec` in - `internal/infrastructure`. A domain vocabulary package may not import - infrastructure. +- The schedules document, whose type is `scheduling.JobSpec` in + `internal/domain/scheduling` -- the vocabulary moved there when the legacy + file-backed cronstore was deleted, which is also what satisfies the rule + that a domain vocabulary package may not import infrastructure. - The schema derivation, which describes the documents the server advertises. **Placement as landed (2026-09-22, `archie-core-1ng1`).** One new package, diff --git a/internal/app/archied/migrate_legacy_schedules_test.go b/internal/app/archied/migrate_legacy_schedules_test.go deleted file mode 100644 index dcf35ddeb..000000000 --- a/internal/app/archied/migrate_legacy_schedules_test.go +++ /dev/null @@ -1,206 +0,0 @@ -package archied - -import ( - "encoding/json" - "log/slog" - "os" - "path/filepath" - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/app/controlplane" - controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" - "github.com/samcharles93/archie-core/internal/store" -) - -// migrationServer builds the validating side exactly as RunStateStore composes -// it: a store over a throwaway file and openStateStoreControlPlane, the root -// helper that registers the shared step vocabulary. -func migrationServer(t *testing.T) (*controlplane.Server, *store.Store) { - t.Helper() - st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "archie.db-tasks.sqlite")) - if err != nil { - t.Fatalf("open store: %v", err) - } - t.Cleanup(func() { _ = st.Close() }) - server, err := openStateStoreControlPlane(st) - if err != nil { - t.Fatalf("open control plane: %v", err) - } - return server, st -} - -// seedSchedules mirrors RunStateStore's ImportConfig: the schedules resource -// starts as the empty seed at version 1, which is the state the legacy -// migration runs against. -func seedSchedules(t *testing.T, control *controlplane.Server) int64 { - t.Helper() - seed, err := json.Marshal([]cronstore.JobSpec{}) - if err != nil { - t.Fatalf("encode empty schedules seed: %v", err) - } - response, err := control.Command(t.Context(), &controlpb.CommandRequest{ - Kind: controlplane.SchedulesKind, Command: "replace", ValueJson: seed, - ExpectedVersion: 0, Actor: "system:migration", Source: "legacy-config", RequestId: "import:schedules", - }) - if err != nil { - t.Fatalf("seed schedules: %v", err) - } - return response.GetResource().GetVersion() -} - -// writeLegacyJobs writes a legacy cronstore file at the layout -// migrateLegacySchedules reads: /cron/jobs.json, in the -// store's own on-disk envelope shape (schema v2). -func writeLegacyJobs(t *testing.T, dir string, jobs []cronstore.JobSpec) string { - t.Helper() - data, err := json.Marshal(struct { - SchemaVersion int `json:"schema_version"` - Jobs []cronstore.JobSpec `json:"jobs"` - }{SchemaVersion: 2, Jobs: jobs}) - if err != nil { - t.Fatalf("encode fixture: %v", err) - } - return writeLegacyFile(t, dir, string(data)) -} - -func writeLegacyFile(t *testing.T, dir, content string) string { - t.Helper() - path := filepath.Join(dir, "cron", "jobs.json") - if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { - t.Fatalf("create legacy cron directory: %v", err) - } - if err := os.WriteFile(path, []byte(content), 0o644); err != nil { - t.Fatalf("write legacy jobs file: %v", err) - } - return path -} - -// legacyJob is a schema-v1-shaped job the old daemon's cron engine wrote. -// An empty Kind means chat, the pre-field default (cronstore.KindChat). -func legacyJob(id, kind string, interval time.Duration) cronstore.JobSpec { - stamped := time.Date(2026, 9, 1, 9, 0, 0, 0, time.UTC) - return cronstore.JobSpec{ - ID: id, Kind: kind, Detail: "legacy " + id, - Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(interval)}, - NextRun: stamped, Created: stamped, Updated: stamped, - } -} - -// storedScheduleIDs decodes the schedules resource into the job IDs it holds. -func storedScheduleIDs(t *testing.T, st *store.Store) []string { - t.Helper() - resource, err := st.Resource(t.Context(), controlplane.SchedulesKind) - if err != nil { - t.Fatalf("read schedules resource: %v", err) - } - var jobs []cronstore.JobSpec - if err := json.Unmarshal(resource.Value, &jobs); err != nil { - t.Fatalf("decode schedules resource: %v", err) - } - ids := make([]string, 0, len(jobs)) - for _, job := range jobs { - ids = append(ids, job.ID) - } - return ids -} - -// TestMigrateLegacySchedulesMovesOnlyWorkflowJobs: a legacy file holding a -// chat job (the pre-field default, kind omitted) and a workflow job migrates -// the workflow job and skips -- never refuses -- the chat one. The current -// behaviour replaces the whole list and lets the schedules validator fail the -// State Store on the chat job, taking every process that dials it down with -// it (archie-core-q6vw). -func TestMigrateLegacySchedulesMovesOnlyWorkflowJobs(t *testing.T) { - dir := t.TempDir() - dbPath := filepath.Join(dir, "archie.db-tasks.sqlite") - writeLegacyJobs(t, dir, []cronstore.JobSpec{ - legacyJob("chat-daily", "", 24*time.Hour), - legacyJob("tdd-hourly", cronstore.KindWorkflow, time.Hour), - }) - server, st := migrationServer(t) - version := seedSchedules(t, server) - - migrateLegacySchedules(t.Context(), server, dbPath, version, discardLog()) - got := storedScheduleIDs(t, st) - if len(got) != 1 || got[0] != "tdd-hourly" { - t.Fatalf("schedules resource holds %v, want only the migratable workflow job", got) - } - if _, err := os.Stat(path_migrated(dir)); err != nil { - t.Fatalf("migrated legacy file not marked (jobs.json.migrated): %v", err) - } - if _, err := os.Stat(filepath.Join(dir, "cron", "jobs.json")); !os.IsNotExist(err) { - t.Fatalf("legacy file still at its original path after a successful migration") - } -} - -// path_migrated is where a completed migration leaves the legacy file. -func path_migrated(dir string) string { - return filepath.Join(dir, "cron", "jobs.json.migrated") -} - -// TestMigrateLegacySchedulesKeepsStoreRunningWhenNothingIsMigratable: a -// legacy file holding only chat jobs leaves the schedules resource untouched -// and reports -- never fails -- the skip. The store's whole job is to come up. -func TestMigrateLegacySchedulesKeepsStoreRunningWhenNothingIsMigratable(t *testing.T) { - dir := t.TempDir() - dbPath := filepath.Join(dir, "archie.db-tasks.sqlite") - path := writeLegacyJobs(t, dir, []cronstore.JobSpec{ - legacyJob("chat-daily", "", 24*time.Hour), - legacyJob("chat-weekly", "chat", 7*24*time.Hour), - }) - server, st := migrationServer(t) - version := seedSchedules(t, server) - - migrateLegacySchedules(t.Context(), server, dbPath, version, discardLog()) - if _, err := os.Stat(path); err != nil { - t.Fatalf("legacy file removed though nothing was migrated: %v", err) - } - if got := storedScheduleIDs(t, st); len(got) != 0 { - t.Fatalf("schedules resource holds %v, want the untouched empty seed", got) - } -} - -// TestMigrateLegacySchedulesKeepsStoreRunningOnUnreadableLegacyStore: a -// corrupt legacy file is a diagnosis, not a boot blocker. The store must come -// up on the empty seed and the file must stay in place for the operator. -func TestMigrateLegacySchedulesKeepsStoreRunningOnUnreadableLegacyStore(t *testing.T) { - dir := t.TempDir() - dbPath := filepath.Join(dir, "archie.db-tasks.sqlite") - path := writeLegacyFile(t, dir, "{not json") - server, _ := migrationServer(t) - version := seedSchedules(t, server) - - migrateLegacySchedules(t.Context(), server, dbPath, version, discardLog()) - if _, err := os.Stat(path); err != nil { - t.Fatalf("legacy file removed though it was never read: %v", err) - } -} - -// TestMigrateLegacySchedulesLeavesTheFileWhenTheReplaceIsRefused: even a -// workflow job the schedules validator refuses is skipped and retried on the -// next start, the way ImportConfig skips a refused seed -- never fatal. -func TestMigrateLegacySchedulesLeavesTheFileWhenTheReplaceIsRefused(t *testing.T) { - dir := t.TempDir() - dbPath := filepath.Join(dir, "archie.db-tasks.sqlite") - path := writeLegacyJobs(t, dir, []cronstore.JobSpec{ - legacyJob("broken-interval", cronstore.KindWorkflow, 0), // interval schedule with no interval - }) - server, st := migrationServer(t) - version := seedSchedules(t, server) - - migrateLegacySchedules(t.Context(), server, dbPath, version, discardLog()) - if _, err := os.Stat(path); err != nil { - t.Fatalf("legacy file removed though the replace never landed: %v", err) - } - if got := storedScheduleIDs(t, st); len(got) != 0 { - t.Fatalf("schedules resource holds %v, want the untouched empty seed", got) - } -} - -// discardLog returns a logger that drops every record: these tests pin the -// migration's effects on the resource and the file, not its reporting. -func discardLog() *slog.Logger { - return slog.New(slog.DiscardHandler) -} diff --git a/internal/app/archied/scheduling.go b/internal/app/archied/scheduling.go index 0e10eacd3..a4fa4df0e 100644 --- a/internal/app/archied/scheduling.go +++ b/internal/app/archied/scheduling.go @@ -14,7 +14,6 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/crondelivery" "github.com/samcharles93/archie-core/internal/infrastructure/cronevents" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" ) // schedulingConfig translates external input into the domain-owned engine @@ -75,7 +74,7 @@ func (b *boot) setupScheduling() error { if err != nil { return fmt.Errorf("scheduling: build workflow runner: %w", err) } - router, err := crondelivery.NewRouter(store, map[string]scheduling.Runner{cronstore.KindWorkflow: workflowRunner}, cfg.Events) + router, err := crondelivery.NewRouter(store, map[string]scheduling.Runner{scheduling.KindWorkflow: workflowRunner}, cfg.Events) if err != nil { return fmt.Errorf("scheduling: build router: %w", err) } @@ -93,17 +92,27 @@ type scheduleResourceStore struct { client *controlplane.Client } -func (s scheduleResourceStore) Get(ctx context.Context, id string) (cronstore.JobSpec, bool, error) { +// The schedules document behind the control plane is the production +// RouterStore: it resolves a job id to its spec and records a completed run. +// The assertions this replaces used to pin the retired file-backed store; +// crondelivery's contract comments name this type as the implementation. +var ( + _ crondelivery.SpecLookup = scheduleResourceStore{} + _ crondelivery.RunRecorder = scheduleResourceStore{} + _ crondelivery.RouterStore = scheduleResourceStore{} +) + +func (s scheduleResourceStore) Get(ctx context.Context, id string) (scheduling.JobSpec, bool, error) { jobs, _, err := s.client.Schedules(ctx) if err != nil { - return cronstore.JobSpec{}, false, err + return scheduling.JobSpec{}, false, err } for _, job := range jobs { if job.ID == id { return job, true, nil } } - return cronstore.JobSpec{}, false, nil + return scheduling.JobSpec{}, false, nil } func (s scheduleResourceStore) Due(ctx context.Context, now time.Time) ([]scheduling.Job, error) { @@ -132,7 +141,7 @@ func (s scheduleResourceStore) MarkRun(ctx context.Context, id string, runAt tim continue } found = true - if jobs[index].Schedule.Resolved().Kind == cronstore.ScheduleOnce { + if jobs[index].Schedule.Resolved().Kind == scheduling.ScheduleOnce { jobs = append(jobs[:index], jobs[index+1:]...) break } @@ -146,7 +155,7 @@ func (s scheduleResourceStore) MarkRun(ctx context.Context, id string, runAt tim break } if !found { - return fmt.Errorf("%w: id %q", cronstore.ErrJobNotFound, id) + return fmt.Errorf("%w: id %q", scheduling.ErrJobNotFound, id) } _, err = s.client.ReplaceSchedules(ctx, jobs, version, "system:scheduler", "scheduler", fmt.Sprintf("schedule-run:%s:%d", id, runAt.UnixNano())) if err == nil { diff --git a/internal/app/archied/state_store.go b/internal/app/archied/state_store.go index 1fe82d3ef..7b6ca1498 100644 --- a/internal/app/archied/state_store.go +++ b/internal/app/archied/state_store.go @@ -13,10 +13,8 @@ import ( "context" "encoding/json" "fmt" - "log/slog" "net" "net/http" - "os" "path/filepath" "time" @@ -24,11 +22,9 @@ import ( "github.com/samcharles93/archie-core/internal/app/controlplane" "github.com/samcharles93/archie-core/internal/config" - controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/health" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" "github.com/samcharles93/archie-core/internal/infrastructure/readiness" "github.com/samcharles93/archie-core/internal/infrastructure/stateadmin" "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" @@ -112,12 +108,11 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error { if err != nil { return err } - versions, skipped, err := control.ImportConfig(ctx, b.cfg) + _, skipped, err := control.ImportConfig(ctx, b.cfg) if err != nil { return fmt.Errorf("import control-plane resources: %w", err) } b.reportUnseededResources(skipped) - migrateLegacySchedules(ctx, control, b.cfg.DBPath, versions[controlplane.SchedulesKind], b.log) token := options.Token if token == "" { @@ -191,81 +186,6 @@ func openStateStoreControlPlane(resources controlplane.ResourceStore) (*controlp return server, nil } -// migrateLegacySchedules carries a legacy cronstore file -- the old daemon's -// /cron/jobs.json -- into the schedules resource, once, on a fresh -// seed. It moves only the jobs the schedules resource can hold: workflow -// jobs, the one delivery the router maps. Chat and unrecognised kinds were -// already dead deliveries in the old engine (the delivery router maps -// KindWorkflow alone), so they are reported and left in the legacy file -// rather than being allowed to refuse the replace the whole store depends on. -// -// Nothing here is fatal. The migration is a legacy-data courtesy: an -// unreadable or refused sidecar is logged and left in place for the next -// start, the way ImportConfig skips a seed the resource validator refuses -- -// a value this process cannot use must not stop the one process every other -// process dials (docs/prds/runtime-control-plane.md, "Bootstrap, migration, -// and recovery"). A successful replace is marked by renaming the file to -// jobs.json.migrated, so the whole legacy list -- the skipped jobs included -// -- stays on disk for the operator; until the rename lands, the replaying -// request ID keeps the replace idempotent across restarts. -func migrateLegacySchedules(ctx context.Context, control *controlplane.Server, dbPath string, version int64, log *slog.Logger) { - if version != 1 { - return - } - path := filepath.Join(filepath.Dir(dbPath), "cron", "jobs.json") - if _, err := os.Stat(path); err != nil { - if os.IsNotExist(err) { - return - } - log.Warn("legacy cron store not readable; schedules migration skipped and retried on the next start", "path", path, "err", err) - return - } - jobs, err := readLegacyJobs(ctx, path) - if err != nil { - log.Error("legacy cron store unreadable; schedules migration skipped and retried on the next start", "path", path, "err", err) - return - } - migratable := make([]cronstore.JobSpec, 0, len(jobs)) - for _, job := range jobs { - if job.Kind == cronstore.KindWorkflow { - migratable = append(migratable, job) - continue - } - log.Warn("legacy cron job is not a workflow job; the schedules resource does not carry it and it stays in the legacy file", "id", job.ID, "kind", job.Kind) - } - if len(migratable) == 0 { - log.Info("legacy cron store holds no workflow jobs; nothing to migrate", "path", path, "jobs", len(jobs)) - return - } - value, err := json.Marshal(migratable) - if err != nil { - log.Error("encode legacy cron jobs; schedules migration skipped and retried on the next start", "path", path, "err", err) - return - } - if _, err := control.Command(ctx, &controlpb.CommandRequest{Kind: controlplane.SchedulesKind, Command: "replace", ValueJson: value, ExpectedVersion: 1, Actor: "system:migration", Source: "legacy-cronstore", RequestId: "import:legacy-schedules"}); err != nil { - log.Error("legacy cron jobs do not satisfy the schedules resource; migration skipped and retried on the next start", "path", path, "err", err) - return - } - if err := os.Rename(path, path+".migrated"); err != nil { - log.Error("mark the migrated legacy cron file; migration retried on the next start", "path", path, "err", err) - } -} - -// readLegacyJobs reads the legacy cronstore file and closes its handle, -// reporting whichever failure came first. -func readLegacyJobs(ctx context.Context, path string) ([]cronstore.JobSpec, error) { - legacy, err := cronstore.Open(path) - if err != nil { - return nil, err - } - jobs, listErr := legacy.List(ctx) - closeErr := legacy.Close() - if listErr != nil { - return nil, listErr - } - return jobs, closeErr -} - // openStateStore opens the single task-store SQLite file exactly once for // service ownership. It resolves secrets (for the binding cipher) but does // NOT open gateway chat sessions -- those live in the separate archie-gateway diff --git a/internal/app/controlplane/client.go b/internal/app/controlplane/client.go index 58b1564c1..aa411513e 100644 --- a/internal/app/controlplane/client.go +++ b/internal/app/controlplane/client.go @@ -11,9 +11,9 @@ import ( pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/agent" + "github.com/samcharles93/archie-core/internal/domain/scheduling" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" ) // Client is the control-plane client for every resource that does not resolve @@ -25,7 +25,7 @@ import ( // Client is the control-plane client a process that serves the store uses. It // embeds the Messaging Service's client -- the generic resource read path and // the channel-settings projection -- and adds the resource-specific reads and -// watches, which need the workflow and cronstore engines that such a process +// watches, which need the workflow and scheduling engines that such a process // already links (archie-core-1ng1). type Client struct { *controlplanerpc.Client @@ -213,19 +213,19 @@ func sendUpdate[T any](ctx context.Context, out chan<- T, update T) bool { } } -func (c *Client) Schedules(ctx context.Context) ([]cronstore.JobSpec, int64, error) { +func (c *Client) Schedules(ctx context.Context) ([]scheduling.JobSpec, int64, error) { response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: SchedulesKind}) if err != nil { return nil, 0, controlplanerpc.ClientError(err) } - var jobs []cronstore.JobSpec + var jobs []scheduling.JobSpec if err := json.Unmarshal(response.Resource.ValueJson, &jobs); err != nil { return nil, 0, err } return jobs, response.Resource.Version, nil } -func (c *Client) ReplaceSchedules(ctx context.Context, jobs []cronstore.JobSpec, expectedVersion int64, actor, source, requestID string) (int64, error) { +func (c *Client) ReplaceSchedules(ctx context.Context, jobs []scheduling.JobSpec, expectedVersion int64, actor, source, requestID string) (int64, error) { value, err := json.Marshal(jobs) if err != nil { return 0, err diff --git a/internal/app/controlplane/schedules.go b/internal/app/controlplane/schedules.go index bbc0fe2ff..4e8e15e3b 100644 --- a/internal/app/controlplane/schedules.go +++ b/internal/app/controlplane/schedules.go @@ -8,7 +8,6 @@ import ( "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/scheduling" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" ) const SchedulesKind = "schedules" @@ -17,9 +16,9 @@ func scheduleDefinition() Definition { return Definition{ Kind: SchedulesKind, Title: "Schedules", - Document: []cronstore.JobSpec{}, + Document: []scheduling.JobSpec{}, ApplyMode: "live", - Seed: func(config.Config) any { return []cronstore.JobSpec{} }, + Seed: func(config.Config) any { return []scheduling.JobSpec{} }, Validate: func(input []byte) error { return validateAs(input, validateSchedules) }, @@ -27,7 +26,7 @@ func scheduleDefinition() Definition { } } -func validateSchedules(jobs []cronstore.JobSpec) error { +func validateSchedules(jobs []scheduling.JobSpec) error { seen := make(map[string]struct{}, len(jobs)) for _, job := range jobs { if strings.TrimSpace(job.ID) == "" { @@ -37,7 +36,7 @@ func validateSchedules(jobs []cronstore.JobSpec) error { return fmt.Errorf("duplicate schedule %q", job.ID) } seen[job.ID] = struct{}{} - if job.Kind != cronstore.KindWorkflow { + if job.Kind != scheduling.KindWorkflow { return fmt.Errorf("schedule %q has unsupported kind %q", job.ID, job.Kind) } if err := job.Schedule.Validate(); err != nil { @@ -51,7 +50,7 @@ func validateSchedules(jobs []cronstore.JobSpec) error { } func normalizeSchedules(input []byte) ([]byte, error) { - var jobs []cronstore.JobSpec + var jobs []scheduling.JobSpec if err := json.Unmarshal(input, &jobs); err != nil { return nil, err } diff --git a/internal/app/controlplane/schedules_test.go b/internal/app/controlplane/schedules_test.go index 3a7d20f42..8a7ae612a 100644 --- a/internal/app/controlplane/schedules_test.go +++ b/internal/app/controlplane/schedules_test.go @@ -5,14 +5,14 @@ import ( "testing" "time" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" + "github.com/samcharles93/archie-core/internal/domain/scheduling" ) func TestScheduleDefinitionComputesFirstRunAndRejectsDuplicateIDs(t *testing.T) { t.Parallel() definition := scheduleDefinition() - jobs := []cronstore.JobSpec{{ID: "daily", Kind: cronstore.KindWorkflow, Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)}}} + jobs := []scheduling.JobSpec{{ID: "daily", Kind: scheduling.KindWorkflow, Schedule: scheduling.Schedule{Kind: scheduling.ScheduleInterval, Interval: scheduling.Duration(time.Hour)}}} input, err := json.Marshal(jobs) if err != nil { t.Fatal(err) @@ -21,7 +21,7 @@ func TestScheduleDefinitionComputesFirstRunAndRejectsDuplicateIDs(t *testing.T) if err != nil { t.Fatal(err) } - var normalized []cronstore.JobSpec + var normalized []scheduling.JobSpec if err := json.Unmarshal(got, &normalized); err != nil { t.Fatal(err) } diff --git a/internal/app/controlplane/schema.go b/internal/app/controlplane/schema.go index da181275f..ca28599d2 100644 --- a/internal/app/controlplane/schema.go +++ b/internal/app/controlplane/schema.go @@ -17,7 +17,7 @@ const bareObjectSchema = `{"type":"object"}` var timeType = reflect.TypeFor[time.Time]() // stdDuration is the marker this tree's string-form duration types satisfy: -// config.Duration, channelDuration and cronstore.Duration each expose the +// config.Duration, channelDuration and scheduling.Duration each expose the // standard duration for arithmetic. var stdDuration = reflect.TypeOf((*interface{ Std() time.Duration })(nil)).Elem() @@ -28,7 +28,7 @@ var stdDuration = reflect.TypeOf((*interface{ Std() time.Duration })(nil)).Elem( // stale the moment a fourth duration type appears and the failure is silent in // the worst direction: the field derives as an integer and a client renders a // number box for a duration. That is exactly what the enumerated version of this -// function did to cronstore.Duration when the schedules interval became one. +// function did to scheduling.Duration when the schedules interval became one. func durationLike(t reflect.Type) bool { if !t.Implements(stdDuration) { return false diff --git a/internal/domain/scheduling/jobspec.go b/internal/domain/scheduling/jobspec.go new file mode 100644 index 000000000..1a5da4d52 --- /dev/null +++ b/internal/domain/scheduling/jobspec.go @@ -0,0 +1,112 @@ +package scheduling + +import ( + "errors" + "time" +) + +// Kind values for JobSpec.Kind: the discriminator that decides which delivery +// runner handles a job. +// +// They live beside the field that carries them because they are part of the +// schedules vocabulary an operator writes into the schedules document, a strict +// decoder reads back, and a build that never heard of a kind must reject loudly +// rather than deliver the wrong way. Which runner serves a kind is not this +// package's business: the router in internal/infrastructure/crondelivery owns +// that mapping, which is why an unrecognised kind is stored verbatim rather +// than refused at write time. +const ( + // KindChat delivers the job's Payload.Text as a chat message through the + // deployment's channel. It is the default: an empty Kind means KindChat, + // so every job written before this field existed keeps delivering the + // way it did when it was created. + KindChat = "chat" + + // KindWorkflow submits the job as a unit of work through the work-intake + // path, so the existing task lifecycle owns scheduling, retries, gates + // and PR opening. + KindWorkflow = "workflow" +) + +// JobSpec is one scheduled job as the schedules document persists it, plus +// the bookkeeping fields the scheduler computes (NextRun, LastRun, Created, +// Updated). The wire shape is the public API; the scheduling.Job returned +// to the engine is a stripped subset. +type JobSpec struct { + // ID is the unique identifier. Required, non-empty, and the merge + // key for run recording. + ID string `json:"id"` + + // Pool is "parallel" or "sequential". Empty means sequential — the + // safer default, matching Pool.resolve(). + Pool string `json:"pool,omitempty"` + + // Detail is a human-readable label rendered on events. + Detail string `json:"detail,omitempty"` + + // Kind selects which delivery runner handles this job. Empty means + // KindChat, so a job written before this field existed keeps + // delivering as a chat message. This package does not interpret the + // value: an unrecognised kind is stored verbatim and refused by the + // delivery router, which is the one place the mapping from kind to + // runner lives. + Kind string `json:"kind,omitempty"` + + // Schedule decides when this job is due. See schedule.go for the + // supported kinds and their arithmetic. + Schedule Schedule `json:"schedule"` + + // Target and Payload describe where the job's output goes and what it + // carries. They live on the wire so jobs created ahead of the delivery + // runners that read them do not need a shape migration. + Target Target `json:"target"` + Payload Payload `json:"payload"` + + // NextRun is the absolute moment the job will next fire. Computed + // at write time so the engine's hot path is a single comparison. + NextRun time.Time `json:"next_run"` + + // LastRun is the last run time reported by the run-recording step, + // nil if the job has never run. The schedule does not know whether + // the run succeeded — that judgement belongs to the runner. + LastRun *time.Time `json:"last_run,omitempty"` + + // Created and Updated are audit metadata written by the document's + // owner; the engine does not read them. + Created time.Time `json:"created"` + Updated time.Time `json:"updated"` +} + +// Target identifies where the job's output goes. Empty Target is valid — +// the delivery runner chooses a default. +type Target struct { + // ChatID is the chat channel id (Telegram chat id, email address, + // webhook URL — depends on the channel). Empty defers to a default + // chosen by the delivery runner. + ChatID string `json:"chat_id,omitempty"` +} + +// Payload is the content sent at run time. +type Payload struct { + // Text is the literal message body. Templating is a future concern + // and is deliberately absent here. + Text string `json:"text,omitempty"` +} + +// Sentinels the schedule vocabulary and its callers match with errors.Is. +var ( + // ErrJobNotFound is returned by a job source when the requested id + // is absent from its document. + ErrJobNotFound = errors.New("scheduling: job not found") + + // ErrInvalidSpec is returned when a JobSpec or Schedule fails + // structural validation (empty id, unknown schedule kind, + // non-positive interval). + ErrInvalidSpec = errors.New("scheduling: invalid job spec") + + // ErrScheduleUnsupported is returned when a job's schedule kind + // cannot compute its next run time. Interval arithmetic is fully + // implemented; cron and once return this so callers can surface a + // clear error rather than silently advancing. + ErrScheduleUnsupported = errors.New("scheduling: schedule kind not implemented") +) diff --git a/internal/infrastructure/cronstore/schedule.go b/internal/domain/scheduling/schedule.go similarity index 99% rename from internal/infrastructure/cronstore/schedule.go rename to internal/domain/scheduling/schedule.go index f08ecfadb..25d044645 100644 --- a/internal/infrastructure/cronstore/schedule.go +++ b/internal/domain/scheduling/schedule.go @@ -1,4 +1,4 @@ -package cronstore +package scheduling import ( "encoding/json" diff --git a/internal/infrastructure/crondelivery/contract.go b/internal/infrastructure/crondelivery/contract.go index 0496c772b..a65b2dc31 100644 --- a/internal/infrastructure/crondelivery/contract.go +++ b/internal/infrastructure/crondelivery/contract.go @@ -29,24 +29,23 @@ import ( "time" "github.com/samcharles93/archie-core/internal/domain/scheduling" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" ) // SpecLookup resolves a job id to its persisted spec. // -// It is declared here (consumer-owned) rather than imported from cronstore as -// a concrete type so this package depends on the one method it uses. -// *cronstore.Store satisfies it as written, so a deployment passes the store -// straight in. +// It is declared here (consumer-owned) rather than imported from the document's +// owner as a concrete type so this package depends on the one method it uses. +// scheduleResourceStore in internal/app/archied satisfies it as written, so a +// deployment passes that in. type SpecLookup interface { // Get returns the job and whether it was found; the error is reserved - // for I/O failures, matching cronstore.Store.Get. - Get(ctx context.Context, id string) (cronstore.JobSpec, bool, error) + // for I/O failures, matching scheduleResourceStore.Get. + Get(ctx context.Context, id string) (scheduling.JobSpec, bool, error) } // RunRecorder advances a job's schedule once a run has completed. It is // declared here (consumer-owned) for the same reason SpecLookup is, and -// *cronstore.Store satisfies it as written. +// scheduleResourceStore satisfies it as written. // // MarkRun is the only step that moves a job's NextRun, so a store whose Due // keeps reporting an already-completed job keeps handing it back on every @@ -98,20 +97,18 @@ var errSpecMissing = errors.New("crondelivery: job spec not found") // hydrate loads the job's persisted spec, wrapping both a store error and the // not-found case so a caller can match on one sentinel. -func hydrate(ctx context.Context, specs SpecLookup, job scheduling.Job) (cronstore.JobSpec, error) { +func hydrate(ctx context.Context, specs SpecLookup, job scheduling.Job) (scheduling.JobSpec, error) { spec, ok, err := specs.Get(ctx, job.ID) if err != nil { - return cronstore.JobSpec{}, fmt.Errorf("crondelivery: load job %q: %w", job.ID, err) + return scheduling.JobSpec{}, fmt.Errorf("crondelivery: load job %q: %w", job.ID, err) } if !ok { - return cronstore.JobSpec{}, fmt.Errorf("%w: id %q", errSpecMissing, job.ID) + return scheduling.JobSpec{}, fmt.Errorf("%w: id %q", errSpecMissing, job.ID) } return spec, nil } -// compile-time check: the store is the lookup a deployment hands in unchanged. -var ( - _ SpecLookup = (*cronstore.Store)(nil) - _ RunRecorder = (*cronstore.Store)(nil) - _ RouterStore = (*cronstore.Store)(nil) -) +// Compile-time check that the production store satisfies the contract. It +// lives in internal/app/archied, which imports this package, so the assertion +// is made there (scheduling.go) rather than here, where it would be an import +// cycle. diff --git a/internal/infrastructure/crondelivery/helpers_test.go b/internal/infrastructure/crondelivery/helpers_test.go index b162db762..d12791229 100644 --- a/internal/infrastructure/crondelivery/helpers_test.go +++ b/internal/infrastructure/crondelivery/helpers_test.go @@ -6,13 +6,12 @@ package crondelivery import ( "context" - "path/filepath" + "fmt" "sync" "testing" "time" "github.com/samcharles93/archie-core/internal/domain/scheduling" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" ) // --- fakes -------------------------------------------------------------------- @@ -88,24 +87,24 @@ func (n *naiveRunner) count() int { // fakeLookup serves specs from a map and ignores ctx entirely, like // naiveCourier. // -// The cancellation tests need it because the real store is itself -// cancellation-aware: *cronstore.Store.Get returns ctx.Err() before it reads, -// so with a real store the runner's own guard is masked — hydrate fails and the -// run returns the context error whether or not the runner checked. A lookup -// that ignores ctx removes that mask, leaving the runner's guard as the only -// thing that can prevent the side effect. +// The cancellation tests need it because a production store is expected to be +// cancellation-aware: a source whose Get returns ctx.Err() before it reads +// masks the runner's own guard — hydrate fails and the run returns the context +// error whether or not the runner checked. A lookup that ignores ctx removes +// that mask, leaving the runner's guard as the only thing that can prevent the +// side effect. type fakeLookup struct { - specs map[string]cronstore.JobSpec + specs map[string]scheduling.JobSpec } -func (f *fakeLookup) Get(_ context.Context, id string) (cronstore.JobSpec, bool, error) { +func (f *fakeLookup) Get(_ context.Context, id string) (scheduling.JobSpec, bool, error) { spec, ok := f.specs[id] return spec, ok, nil } // MarkRun satisfies RouterStore. It deliberately does nothing: these tests // exercise the runner's and the router's own dispatch guards, and the record -// step is covered against the real store in +// step is covered against a store that really advances in // TestRouterAdvancesTheScheduleAfterASuccessfulRun. func (f *fakeLookup) MarkRun(_ context.Context, _ string, _ time.Time) error { return nil } @@ -247,20 +246,80 @@ func (c *countingRunner) count() int { // --- helpers ------------------------------------------------------------------ -// newStore opens a real cronstore on a temp path and closes it on cleanup. -func newStore(t *testing.T) *cronstore.Store { - t.Helper() - s, err := cronstore.Open(filepath.Join(t.TempDir(), "jobs.json")) - if err != nil { - t.Fatalf("cronstore.Open: %v", err) - } - t.Cleanup(func() { _ = s.Close() }) - return s +// memStore is the test's schedules document: a map with the three faces the +// production store shows the package — SpecLookup (Get), RunRecorder (MarkRun +// with real schedule arithmetic), and JobSource (Due). It replaces the file- +// backed store this package's fixtures used to open, which died with the +// legacy jobs.json store; the document now lives behind the control plane in +// production, and what these tests need from persistence is semantics, not a +// file. +type memStore struct { + mu sync.Mutex + jobs map[string]scheduling.JobSpec } -// createJob persists a spec through the real store and returns the scheduling -// job the engine would hand a runner for it. -func createJob(t *testing.T, s *cronstore.Store, spec cronstore.JobSpec) scheduling.Job { +func newStore(t *testing.T) *memStore { + t.Helper() + return &memStore{jobs: make(map[string]scheduling.JobSpec)} +} + +// Create persists a spec verbatim. Fixtures that want a due job set NextRun +// themselves (createJob does it when unset). +func (m *memStore) Create(_ context.Context, spec scheduling.JobSpec) error { + m.mu.Lock() + defer m.mu.Unlock() + if _, exists := m.jobs[spec.ID]; exists { + return fmt.Errorf("memStore: id %q already exists", spec.ID) + } + m.jobs[spec.ID] = spec + return nil +} + +func (m *memStore) Get(_ context.Context, id string) (scheduling.JobSpec, bool, error) { + m.mu.Lock() + defer m.mu.Unlock() + spec, ok := m.jobs[id] + return spec, ok, nil +} + +// Due returns the jobs whose NextRun is at or before now, the same contract +// the production source keeps. +func (m *memStore) Due(_ context.Context, now time.Time) ([]scheduling.Job, error) { + m.mu.Lock() + defer m.mu.Unlock() + due := make([]scheduling.Job, 0, len(m.jobs)) + for _, spec := range m.jobs { + if !spec.NextRun.After(now) { + due = append(due, scheduling.Job{ID: spec.ID, Pool: scheduling.Pool(spec.Pool), Detail: spec.Detail}) + } + } + return due, nil +} + +// MarkRun advances an interval job's NextRun the way the production +// recording step does, and reports a once schedule as unsupported — the +// sentinel the router swallows. +func (m *memStore) MarkRun(_ context.Context, id string, runAt time.Time) error { + m.mu.Lock() + defer m.mu.Unlock() + spec, ok := m.jobs[id] + if !ok { + return fmt.Errorf("%w: id %q", scheduling.ErrJobNotFound, id) + } + next, err := spec.Schedule.NextRun(runAt) + if err != nil { + return err + } + at := runAt.UTC() + spec.LastRun = &at + spec.NextRun = next + m.jobs[id] = spec + return nil +} + +// createJob persists a spec and returns the scheduling job the engine would +// hand a runner for it. +func createJob(t *testing.T, s *memStore, spec scheduling.JobSpec) scheduling.Job { t.Helper() if spec.NextRun.IsZero() { // Due now: the engine only ever hands a runner a job its source @@ -268,30 +327,30 @@ func createJob(t *testing.T, s *cronstore.Store, spec cronstore.JobSpec) schedul spec.NextRun = time.Now().UTC().Add(-time.Minute) } if err := s.Create(t.Context(), spec); err != nil { - t.Fatalf("cronstore.Create(%q): %v", spec.ID, err) + t.Fatalf("Create(%q): %v", spec.ID, err) } return scheduling.Job{ID: spec.ID, Pool: scheduling.Pool(spec.Pool), Detail: spec.Detail} } // chatSpec is a minimal chat job. -func chatSpec(id, chatID, text string) cronstore.JobSpec { - return cronstore.JobSpec{ +func chatSpec(id, chatID, text string) scheduling.JobSpec { + return scheduling.JobSpec{ ID: id, Detail: "test job " + id, - Kind: cronstore.KindChat, - Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)}, - Target: cronstore.Target{ChatID: chatID}, - Payload: cronstore.Payload{Text: text}, + Kind: scheduling.KindChat, + Schedule: scheduling.Schedule{Kind: scheduling.ScheduleInterval, Interval: scheduling.Duration(time.Hour)}, + Target: scheduling.Target{ChatID: chatID}, + Payload: scheduling.Payload{Text: text}, } } // workflowSpec is a minimal workflow job. -func workflowSpec(id, detail, body string) cronstore.JobSpec { - return cronstore.JobSpec{ +func workflowSpec(id, detail, body string) scheduling.JobSpec { + return scheduling.JobSpec{ ID: id, Detail: detail, - Kind: cronstore.KindWorkflow, - Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)}, - Payload: cronstore.Payload{Text: body}, + Kind: scheduling.KindWorkflow, + Schedule: scheduling.Schedule{Kind: scheduling.ScheduleInterval, Interval: scheduling.Duration(time.Hour)}, + Payload: scheduling.Payload{Text: body}, } } diff --git a/internal/infrastructure/crondelivery/router.go b/internal/infrastructure/crondelivery/router.go index d2421e8a3..fc73da673 100644 --- a/internal/infrastructure/crondelivery/router.go +++ b/internal/infrastructure/crondelivery/router.go @@ -9,7 +9,6 @@ import ( "github.com/samcharles93/archie-core/internal/domain/scheduling" "github.com/samcharles93/archie-core/internal/events" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" ) // phaseDispatch names the step that failed on a KindJobError the Router emits. @@ -43,7 +42,7 @@ type Router struct { // emission — the router still refuses, it is just unobservable, the same // tolerance the engine has for its own nil sink. // -// An empty kind resolves to cronstore.KindChat, so a deployment only needs a +// An empty kind resolves to scheduling.KindChat, so a deployment only needs a // mapping for the kinds it actually uses. func NewRouter(specs RouterStore, runners map[string]scheduling.Runner, sink scheduling.Sink) (*Router, error) { if specs == nil { @@ -67,7 +66,7 @@ func (r *Router) Run(ctx context.Context, job scheduling.Job) error { kind := spec.Kind if kind == "" { - kind = cronstore.KindChat + kind = scheduling.KindChat } runner, ok := r.runners[kind] if !ok { @@ -86,12 +85,12 @@ func (r *Router) Run(ctx context.Context, job scheduling.Job) error { // per tick instead. // // A schedule kind with no recurring next run -- a one-shot -- reports -// cronstore.ErrScheduleUnsupported, which is the job's definition rather than a +// scheduling.ErrScheduleUnsupported, which is the job's definition rather than a // run failure, so it is swallowed. Anything else is returned: the run happened // but the bookkeeping did not, and hiding that would leave the job silently // re-firing. func (r *Router) recordRun(ctx context.Context, job scheduling.Job) error { - if err := r.specs.MarkRun(ctx, job.ID, time.Now()); err != nil && !errors.Is(err, cronstore.ErrScheduleUnsupported) { + if err := r.specs.MarkRun(ctx, job.ID, time.Now()); err != nil && !errors.Is(err, scheduling.ErrScheduleUnsupported) { return fmt.Errorf("crondelivery: record run of job %q: %w", job.ID, err) } return nil diff --git a/internal/infrastructure/crondelivery/router_test.go b/internal/infrastructure/crondelivery/router_test.go index 8f85cf4d4..63d557c6d 100644 --- a/internal/infrastructure/crondelivery/router_test.go +++ b/internal/infrastructure/crondelivery/router_test.go @@ -8,7 +8,6 @@ import ( "github.com/samcharles93/archie-core/internal/domain/scheduling" "github.com/samcharles93/archie-core/internal/events" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" ) // --- Router ------------------------------------------------------------------- @@ -19,17 +18,17 @@ import ( func routerFixture(t *testing.T, kind string) (*Router, *countingRunner, *countingRunner, *recordingSink) { t.Helper() s := newStore(t) - createJob(t, s, cronstore.JobSpec{ + createJob(t, s, scheduling.JobSpec{ ID: "routed", Detail: "routed job", Kind: kind, - Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)}, + Schedule: scheduling.Schedule{Kind: scheduling.ScheduleInterval, Interval: scheduling.Duration(time.Hour)}, }) chat, workflow := &countingRunner{}, &countingRunner{} sink := &recordingSink{} r, err := NewRouter(s, map[string]scheduling.Runner{ - cronstore.KindChat: chat, - cronstore.KindWorkflow: workflow, + scheduling.KindChat: chat, + scheduling.KindWorkflow: workflow, }, sink) if err != nil { t.Fatalf("NewRouter: %v", err) @@ -44,8 +43,8 @@ func TestRouterDispatchesByKind(t *testing.T) { wantChat int wantWorkflow int }{ - {"chat kind reaches the chat runner", cronstore.KindChat, 1, 0}, - {"workflow kind reaches the workflow runner", cronstore.KindWorkflow, 0, 1}, + {"chat kind reaches the chat runner", scheduling.KindChat, 1, 0}, + {"workflow kind reaches the workflow runner", scheduling.KindWorkflow, 0, 1}, {"empty kind defaults to the chat runner", "", 1, 0}, } for _, tc := range tests { @@ -117,15 +116,15 @@ func TestRouterUnknownKindIsRefusedAtTheGate(t *testing.T) { // tolerance: a nil sink disables emission without disabling the refusal. func TestRouterNilSinkDoesNotPanicOnUnknownKind(t *testing.T) { s := newStore(t) - createJob(t, s, cronstore.JobSpec{ + createJob(t, s, scheduling.JobSpec{ ID: "routed", Kind: "teleport", - Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)}, + Schedule: scheduling.Schedule{Kind: scheduling.ScheduleInterval, Interval: scheduling.Duration(time.Hour)}, }) chat, workflow := &countingRunner{}, &countingRunner{} r, err := NewRouter(s, map[string]scheduling.Runner{ - cronstore.KindChat: chat, - cronstore.KindWorkflow: workflow, + scheduling.KindChat: chat, + scheduling.KindWorkflow: workflow, }, nil) if err != nil { t.Fatalf("NewRouter: %v", err) @@ -143,7 +142,7 @@ func TestRouterNilSinkDoesNotPanicOnUnknownKind(t *testing.T) { func TestRouterMissingSpecReturnsError(t *testing.T) { s := newStore(t) r, err := NewRouter(s, map[string]scheduling.Runner{ - cronstore.KindChat: &countingRunner{}, + scheduling.KindChat: &countingRunner{}, }, &recordingSink{}) if err != nil { t.Fatalf("NewRouter: %v", err) @@ -161,7 +160,7 @@ func TestRouterMissingSpecReturnsError(t *testing.T) { // the job the engine built, not a re-derived one: identity and pool are the // engine's, and the runner's event attribution depends on them surviving. func TestRouterPassesTheEngineJobThrough(t *testing.T) { - r, chat, _, _ := routerFixture(t, cronstore.KindChat) + r, chat, _, _ := routerFixture(t, scheduling.KindChat) job := scheduling.Job{ID: "routed", Pool: scheduling.PoolSequential, Detail: "detail from engine"} if err := r.Run(t.Context(), job); err != nil { @@ -185,7 +184,7 @@ func TestRouterPassesTheEngineJobThrough(t *testing.T) { // honours cancellation would make this pass whether or not the runner checks, // which is the failure mode the criterion exists to prevent. func TestEveryRunnerHonoursCancellation(t *testing.T) { - lookup := &fakeLookup{specs: map[string]cronstore.JobSpec{ + lookup := &fakeLookup{specs: map[string]scheduling.JobSpec{ "chat-job": chatSpec("chat-job", "ops-room", "hello"), "wf-job": workflowSpec("wf-job", "workflow job", "body"), }} @@ -204,8 +203,8 @@ func TestEveryRunnerHonoursCancellation(t *testing.T) { // router's own guard: a cancelled run must not reach the dispatch. forwarded := &naiveRunner{} router, err := NewRouter(lookup, map[string]scheduling.Runner{ - cronstore.KindChat: forwarded, - cronstore.KindWorkflow: workflow, + scheduling.KindChat: forwarded, + scheduling.KindWorkflow: workflow, }, &recordingSink{}) if err != nil { t.Fatalf("NewRouter: %v", err) @@ -246,27 +245,27 @@ func TestEveryRunnerHonoursCancellation(t *testing.T) { const dailyStatusSummary = "Daily status summary: all workers healthy, no tasks parked." // TestDailyStatusSummaryEndToEnd is the epic's headline scenario exercised -// through the whole chain a deployment uses: a real job persisted in a real -// cronstore is reported due by the store's own JobSource, handed to the router +// through the whole chain a deployment uses: a scheduled job in the schedules +// document is reported due by its own JobSource, handed to the router // the engine is given, dispatched to the chat runner, and delivered as the // composed message. func TestDailyStatusSummaryEndToEnd(t *testing.T) { s := newStore(t) const chatID = "ops-room" - spec := cronstore.JobSpec{ + spec := scheduling.JobSpec{ ID: "daily-status", Detail: "daily status summary", - Kind: cronstore.KindChat, - Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(24 * time.Hour)}, - Target: cronstore.Target{ChatID: chatID}, - Payload: cronstore.Payload{Text: dailyStatusSummary}, + Kind: scheduling.KindChat, + Schedule: scheduling.Schedule{Kind: scheduling.ScheduleInterval, Interval: scheduling.Duration(24 * time.Hour)}, + Target: scheduling.Target{ChatID: chatID}, + Payload: scheduling.Payload{Text: dailyStatusSummary}, // Due now: create it with a next_run in the past, the state a // long-idle daemon wakes up to. NextRun: time.Now().UTC().Add(-time.Minute), } if err := s.Create(t.Context(), spec); err != nil { - t.Fatalf("cronstore.Create: %v", err) + t.Fatalf("Create: %v", err) } // The engine's source: the store itself, answering what is due. @@ -284,7 +283,7 @@ func TestDailyStatusSummaryEndToEnd(t *testing.T) { t.Fatalf("NewChatCourier: %v", err) } router, err := NewRouter(s, map[string]scheduling.Runner{ - cronstore.KindChat: courierRunner, + scheduling.KindChat: courierRunner, }, &recordingSink{}) if err != nil { t.Fatalf("NewRouter: %v", err) @@ -334,7 +333,7 @@ func TestConstructorsRejectNilDependencies(t *testing.T) { }}, {"Router without a spec lookup", func() error { _, err := NewRouter(nil, map[string]scheduling.Runner{ - cronstore.KindChat: &countingRunner{}, + scheduling.KindChat: &countingRunner{}, }, nil) return err }}, @@ -363,7 +362,7 @@ func TestImplementsSchedulingRunner(t *testing.T) { } // TestRouterAdvancesTheScheduleAfterASuccessfulRun is the acceptance criterion -// for a recurring job's timing: cronstore.Store.MarkRun is the only step that +// for a recurring job's timing: the recording step is the only thing that // moves a job's NextRun, and the Router is the single Runner the engine hands // every job. With no caller the store keeps reporting the job due on every // tick, so a job scheduled every hour fires once per tick instead. @@ -380,7 +379,7 @@ func TestRouterAdvancesTheScheduleAfterASuccessfulRun(t *testing.T) { } r, err := NewRouter(s, map[string]scheduling.Runner{ - cronstore.KindChat: &countingRunner{}, + scheduling.KindChat: &countingRunner{}, }, &recordingSink{}) if err != nil { t.Fatalf("NewRouter: %v", err) @@ -404,23 +403,23 @@ func TestRouterAdvancesTheScheduleAfterASuccessfulRun(t *testing.T) { // TestRouterToleratesAOnceScheduleWithoutANextRun pins the other half: a // one-shot job has no recurring next run by definition, so MarkRun reports -// cronstore.ErrScheduleUnsupported. That is not a run failure -- the job ran -- +// scheduling.ErrScheduleUnsupported. That is not a run failure -- the job ran -- // and returning it would make the engine emit KindJobError for a job that // succeeded. func TestRouterToleratesAOnceScheduleWithoutANextRun(t *testing.T) { s := newStore(t) at := time.Now().Add(-time.Hour) - job := createJob(t, s, cronstore.JobSpec{ + job := createJob(t, s, scheduling.JobSpec{ ID: "one-shot", Detail: "one-shot job", - Kind: cronstore.KindChat, - Schedule: cronstore.Schedule{Kind: cronstore.ScheduleOnce, At: &at}, - Target: cronstore.Target{ChatID: "chat-1"}, - Payload: cronstore.Payload{Text: "once"}, + Kind: scheduling.KindChat, + Schedule: scheduling.Schedule{Kind: scheduling.ScheduleOnce, At: &at}, + Target: scheduling.Target{ChatID: "chat-1"}, + Payload: scheduling.Payload{Text: "once"}, }) r, err := NewRouter(s, map[string]scheduling.Runner{ - cronstore.KindChat: &countingRunner{}, + scheduling.KindChat: &countingRunner{}, }, &recordingSink{}) if err != nil { t.Fatalf("NewRouter: %v", err) diff --git a/internal/infrastructure/crondelivery/runner_test.go b/internal/infrastructure/crondelivery/runner_test.go index 5701aac02..d7f559754 100644 --- a/internal/infrastructure/crondelivery/runner_test.go +++ b/internal/infrastructure/crondelivery/runner_test.go @@ -7,7 +7,6 @@ import ( "time" "github.com/samcharles93/archie-core/internal/domain/scheduling" - "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" ) // --- ChatCourier -------------------------------------------------------------- @@ -83,7 +82,7 @@ func TestChatCourierHonoursCancellation(t *testing.T) { t.Run("cancelled before the run sends nothing", func(t *testing.T) { // Both the lookup and the courier ignore ctx, so only the // runner's own guard can stop the send. - lookup := &fakeLookup{specs: map[string]cronstore.JobSpec{ + lookup := &fakeLookup{specs: map[string]scheduling.JobSpec{ "cancel-me": chatSpec("cancel-me", "ops-room", "morning"), }} courier := &naiveCourier{} @@ -224,7 +223,7 @@ func TestWorkflowTaskHonoursCancellation(t *testing.T) { t.Run("cancelled before the run submits nothing", func(t *testing.T) { // Both the lookup and the submitter ignore ctx, so only the // runner's own guard can stop the submit. - lookup := &fakeLookup{specs: map[string]cronstore.JobSpec{ + lookup := &fakeLookup{specs: map[string]scheduling.JobSpec{ "cancel-me": workflowSpec("cancel-me", "cancel me", "body"), }} sub := &naiveSubmitter{} diff --git a/internal/infrastructure/cronstore/doc.go b/internal/infrastructure/cronstore/doc.go deleted file mode 100644 index d886acc7b..000000000 --- a/internal/infrastructure/cronstore/doc.go +++ /dev/null @@ -1,41 +0,0 @@ -// Package cronstore is the file-backed job store that backs the scheduling -// ticker engine. It is infrastructure: it owns the on-disk layout, the -// cross-process lock, and the version-stamped JSON schema. It does not own -// timing semantics — those live in internal/domain/scheduling. -// -// Two surfaces: -// -// - scheduling.JobSource: Due(ctx, now) — what the engine consumes. -// - CRUD on JobSpec — what the CLI and dashboard consume. -// -// Both share the same Store value, the same lock, and the same on-disk -// file. The engine applies no schedule arithmetic of its own; whatever -// "due" means is the store's decision, including future kinds the store -// grows into. -// -// Concurrency model: every read and write acquires an OS-level flock on a -// sibling lock file, so a CLI edit and a daemon writer cannot tear the file. -// Conflict resolution is last-writer-wins: the second writer overwrites the -// first's whole document. The lock makes that serial rather than racy. -// In-process, two Store values for the same path are serialised by a -// package-level mutex keyed by lock-file path — flock is per file -// description on Linux and would not block them otherwise. -// -// File format (versioned; bump schemaVersion on ANY shape change -// including additive ones, since DisallowUnknownFields rejects them): -// -// { -// "schema_version": 2, -// "jobs": [ JobSpec, ... ] -// } -// -// Version 2 added JobSpec.Kind, the delivery discriminator. A version-1 -// file still loads — Kind reads back empty, which the delivery router -// resolves to the chat default — so jobs created before the field existed -// keep firing the way they always did. -// -// JobSpec carries the engine-facing fields it always needed (ID, Pool, -// Detail) plus everything the store computes for it (NextRun, LastRun, -// Created, Updated). Merge semantics on Update mean a nil field in a Patch -// leaves the corresponding persisted field untouched. -package cronstore diff --git a/internal/infrastructure/cronstore/errors.go b/internal/infrastructure/cronstore/errors.go deleted file mode 100644 index 2bd7268db..000000000 --- a/internal/infrastructure/cronstore/errors.go +++ /dev/null @@ -1,39 +0,0 @@ -package cronstore - -import "errors" - -// Sentinel errors callers can match with errors.Is. -var ( - // ErrUnsupportedSchema is returned by Open when the persisted file - // declares a schema version this build does not understand. The - // store treats that as an explicit refusal to read: a forward - // version may have shape we cannot safely render back out, and - // silently downgrading would lose data. - ErrUnsupportedSchema = errors.New("cronstore: unsupported schema version") - - // ErrCorruptFile is returned by Open when the file exists but does - // not decode as a valid envelope: truncated, garbage, or fields of - // the wrong shape. The store never panics on bad input, so the - // engine can surface this as phase="source" KindJobError. - ErrCorruptFile = errors.New("cronstore: corrupt file") - - // ErrJobExists is returned by Create when the requested id is - // already present. Callers that want upsert semantics should - // follow up with Update. - ErrJobExists = errors.New("cronstore: job already exists") - - // ErrJobNotFound is returned by Get, Update and MarkRun when the - // id is absent. Delete is the exception: it is idempotent. - ErrJobNotFound = errors.New("cronstore: job not found") - - // ErrInvalidSpec is returned by Create and Update when the supplied - // JobSpec / Patch fails the store's structural validation (empty id, - // unknown schedule kind, non-positive interval). - ErrInvalidSpec = errors.New("cronstore: invalid job spec") - - // ErrScheduleUnsupported is returned by MarkRun when the job's - // schedule kind cannot compute its next run time. Slice 1 only - // knows interval arithmetic; cron / once return this so callers - // can surface a clear error rather than silently advancing. - ErrScheduleUnsupported = errors.New("cronstore: schedule kind not implemented") -) diff --git a/internal/infrastructure/cronstore/kind.go b/internal/infrastructure/cronstore/kind.go deleted file mode 100644 index 128a2e7cd..000000000 --- a/internal/infrastructure/cronstore/kind.go +++ /dev/null @@ -1,24 +0,0 @@ -package cronstore - -// Kind values for JobSpec.Kind: the discriminator that decides which delivery -// runner handles a job. -// -// They live here, beside the field that carries them, because they are part of -// the on-disk vocabulary — an operator writes one in jobs.json, the strict -// decoder reads it back, and a file written by a build that never heard of it -// must fail loudly rather than deliver the wrong way. Which runner serves a -// kind is not the store's business: the router in -// internal/infrastructure/crondelivery owns that mapping, which is why an -// unrecognised kind is left on disk rather than refused at Create. -const ( - // KindChat delivers the job's Payload.Text as a chat message through the - // deployment's channel. It is the default: an empty Kind means KindChat, - // so every job written before this field existed (schema version 1) - // keeps delivering the way it did when it was created. - KindChat = "chat" - - // KindWorkflow submits the job as a unit of work through the work-intake - // path, so the existing task lifecycle owns scheduling, retries, gates - // and PR opening. - KindWorkflow = "workflow" -) diff --git a/internal/infrastructure/cronstore/kind_test.go b/internal/infrastructure/cronstore/kind_test.go deleted file mode 100644 index e4e4d8d06..000000000 --- a/internal/infrastructure/cronstore/kind_test.go +++ /dev/null @@ -1,148 +0,0 @@ -// Tests for the JobSpec.Kind discriminator added in the cron-delivery slice. -// -// The store owns the persisted vocabulary; the runner mapping belongs to -// internal/infrastructure/crondelivery. These tests pin the store-side -// properties that mapping depends on: the field round-trips, an empty value -// reads back as the chat default, and a file written before the field existed -// still loads. -package cronstore - -import ( - "bytes" - "context" - "os" - "path/filepath" - "testing" - "time" -) - -// TestPersistedFileStampsCurrentSchemaVersion guards the additive-change rule -// documented on schemaVersion: adding Kind to JobSpec forces a version bump, -// because DisallowUnknownFields means an older build must reject a file -// carrying a field it does not know rather than silently dropping it. -func TestPersistedFileStampsCurrentSchemaVersion(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - s, err := Open(path) - if err != nil { - t.Fatalf("Open: %v", err) - } - defer s.Close() - mustCreate(t, s, fixedIntervalSpec("x", time.Minute)) - if err := s.Close(); err != nil { - t.Fatalf("Close: %v", err) - } - - data, err := os.ReadFile(path) - if err != nil { - t.Fatalf("ReadFile: %v", err) - } - if !bytes.Contains(data, []byte(`"schema_version":2`)) { - t.Errorf("file does not carry the bumped schema_version:2 stamp: %s", data) - } -} - -// TestKindRoundTrips pins that the discriminator survives a write and read on -// the real file, not just in memory: Create serialises it, a fresh Open with -// the strict decoder must read it back. -func TestKindRoundTrips(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - - s, err := Open(path) - if err != nil { - t.Fatalf("Open: %v", err) - } - spec := fixedIntervalSpec("nightly-build", time.Hour) - spec.Kind = KindWorkflow - mustCreate(t, s, spec) - if err := s.Close(); err != nil { - t.Fatalf("Close: %v", err) - } - - // A second handle proves the value is on disk and survives the strict - // decoder, rather than only living in the writer's in-memory mirror. - reopened, err := Open(path) - if err != nil { - t.Fatalf("reopen: %v", err) - } - defer reopened.Close() - - got, ok, err := reopened.Get(context.Background(), "nightly-build") - if err != nil { - t.Fatalf("Get: %v", err) - } - if !ok { - t.Fatal("Get returned ok=false after reopen") - } - if got.Kind != KindWorkflow { - t.Errorf("Kind = %q, want %q", got.Kind, KindWorkflow) - } -} - -// TestEmptyKindLoadsAsChatDefault pins the compatibility rule the router -// relies on: a job that names no kind delivers as a chat message. Asserting -// the empty string (rather than KindChat) is deliberate — the store must not -// rewrite what the operator wrote; resolving empty to "chat" is the router's -// job, and this test pins that the store hands it through untouched. -func TestEmptyKindLoadsAsChatDefault(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("no-kind", time.Minute)) - - got, ok, err := s.Get(context.Background(), "no-kind") - if err != nil { - t.Fatalf("Get: %v", err) - } - if !ok { - t.Fatal("Get returned ok=false") - } - if got.Kind != "" { - t.Errorf("Kind = %q, want empty (the chat default)", got.Kind) - } -} - -// TestVersion1FileWithoutKindStillLoads is the migration guard for the schema -// bump. A file written by Slice 1 has schema_version 1 and no kind field; the -// bumped build must load it rather than rejecting it, or every existing cron -// job stops firing the moment this ships. -func TestVersion1FileWithoutKindStillLoads(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - past := time.Now().Add(-time.Minute).UTC().Format(time.RFC3339Nano) - contents := `{"schema_version":1,"jobs":[{"id":"legacy","pool":"sequential",` + - `"detail":"written before kind existed",` + - `"schedule":{"kind":"interval","interval":60000000000},` + - `"target":{"chat_id":"ops"},"payload":{"text":"legacy payload"},` + - `"next_run":"` + past + `","created":"` + past + `","updated":"` + past + `"}]}` - if err := os.WriteFile(path, []byte(contents), 0o600); err != nil { - t.Fatalf("WriteFile: %v", err) - } - - s, err := Open(path) - if err != nil { - t.Fatalf("Open on a version-1 file after the bump = %v, want nil", err) - } - defer s.Close() - - got, ok, err := s.Get(context.Background(), "legacy") - if err != nil { - t.Fatalf("Get: %v", err) - } - if !ok { - t.Fatal("Get returned ok=false for a version-1 job") - } - if got.Kind != "" { - t.Errorf("Kind = %q, want empty for a version-1 job", got.Kind) - } - if got.Payload.Text != "legacy payload" { - t.Errorf("Payload.Text = %q, want %q", got.Payload.Text, "legacy payload") - } - // It must still be dispatchable: the engine reads due jobs through Due. - due, err := s.Due(context.Background(), time.Now()) - if err != nil { - t.Fatalf("Due: %v", err) - } - if len(due) != 1 || due[0].ID != "legacy" { - t.Errorf("Due on a version-1 file = %+v, want the legacy job", due) - } -} diff --git a/internal/infrastructure/cronstore/lock_unix.go b/internal/infrastructure/cronstore/lock_unix.go deleted file mode 100644 index d2734da9d..000000000 --- a/internal/infrastructure/cronstore/lock_unix.go +++ /dev/null @@ -1,68 +0,0 @@ -//go:build unix - -package cronstore - -import ( - "fmt" - "os" - "syscall" -) - -// lockedFile is an OS-level flock wrapper around a sidecar file. The -// store acquires it before reading or writing the JSON, so a CLI edit -// and a daemon writer cannot tear the same jobs.json. -// -// flock is per-open-file-description, which means a single Store holds -// the lock for as long as it lives; concurrent processes opening the -// same sidecar queue behind it. -type lockedFile struct { - f *os.File -} - -// openLock creates (or opens) the lock file and returns it unlocked. -// The lock is acquired separately via Lock, so Open can hydrate without -// holding it. -func openLock(path string) (*lockedFile, error) { - f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o644) - if err != nil { - return nil, fmt.Errorf("open lock file %s: %w", path, err) - } - return &lockedFile{f: f}, nil -} - -// Lock blocks until the OS-level exclusive flock is acquired. flock is -// reentrant on Linux per-fd, so calling Lock twice on the same lockedFile -// from the same goroutine is fine; calling it from two goroutines still -// serialises. -func (l *lockedFile) Lock() error { - if l == nil || l.f == nil { - return fmt.Errorf("cronstore: lock not initialised") - } - if err := syscall.Flock(int(l.f.Fd()), syscall.LOCK_EX); err != nil { - return fmt.Errorf("flock LOCK_EX: %w", err) - } - return nil -} - -// Unlock releases the exclusive flock. Idempotent in the sense that a -// second call after Close simply returns nil because the fd is closed. -func (l *lockedFile) Unlock() error { - if l == nil || l.f == nil { - return nil - } - if err := syscall.Flock(int(l.f.Fd()), syscall.LOCK_UN); err != nil { - return fmt.Errorf("flock LOCK_UN: %w", err) - } - return nil -} - -// Close closes the sidecar fd. The flock is released automatically by -// the kernel. -func (l *lockedFile) Close() error { - if l == nil || l.f == nil { - return nil - } - err := l.f.Close() - l.f = nil - return err -} diff --git a/internal/infrastructure/cronstore/lock_windows.go b/internal/infrastructure/cronstore/lock_windows.go deleted file mode 100644 index 823db691e..000000000 --- a/internal/infrastructure/cronstore/lock_windows.go +++ /dev/null @@ -1,53 +0,0 @@ -//go:build windows - -// flock is a POSIX primitive; Windows does not implement syscall.Flock -// the same way. Windows deployments of archied are not the supported -// surface (the deployment model in CLAUDE.md and deployments/*.toml is -// Linux + macOS), so this file deliberately fails closed: every Lock -// returns an error rather than silently degrading to "no lock". -// -// If a Windows deployment ever becomes supported, swap this for a real -// LockFileEx / UnlockFileEx implementation; the rest of the store does -// not need to change. - -package cronstore - -import ( - "errors" - "fmt" - "os" -) - -// lockedFile is a stub on Windows. The store still opens the sidecar -// file so the on-disk layout matches across platforms, but every Lock -// returns an explicit error. -type lockedFile struct { - f *os.File -} - -func openLock(path string) (*lockedFile, error) { - f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o644) - if err != nil { - return nil, fmt.Errorf("open lock file %s: %w", path, err) - } - return &lockedFile{f: f}, nil -} - -// errLockUnsupported is wrapped in every Lock call so callers can match -// on it without parsing strings. -var errLockUnsupported = errors.New("cronstore: cross-process locking is unsupported on Windows") - -func (l *lockedFile) Lock() error { return errLockUnsupported } -func (l *lockedFile) Unlock() error { return nil } - -func (l *lockedFile) Close() error { - if l == nil || l.f == nil { - return nil - } - err := l.f.Close() - l.f = nil - return err -} - -// Keep imports alive when this file is built in isolation. -var _ = fmt.Sprintf diff --git a/internal/infrastructure/cronstore/process_lock.go b/internal/infrastructure/cronstore/process_lock.go deleted file mode 100644 index 49558d281..000000000 --- a/internal/infrastructure/cronstore/process_lock.go +++ /dev/null @@ -1,33 +0,0 @@ -package cronstore - -import "sync" - -// processLocks serialises Stores within one process. It exists because -// Linux flock is per file description, not per inode: two Store -// instances opening the same jobs.json.lock each get their own fd and -// their own independent flock state, so they would not block each -// other inside a single process. The cross-process lock handles the -// CLI-vs-daemon case; this map handles the in-process case. -// -// Keyed by absolute lock-file path so two unrelated cronstores in the -// same process do not contend. Entries are created lazily and live for -// the life of the process; the map is append-only and small in -// practice (one entry per distinct config dir). -var ( - processLocksMu sync.Mutex - processLocks = map[string]*sync.Mutex{} -) - -// processLockFor returns the per-lock-path mutex, creating it on first -// use. Callers must hold it for the duration of an operation that has -// already acquired the cross-process flock. -func processLockFor(path string) *sync.Mutex { - processLocksMu.Lock() - defer processLocksMu.Unlock() - if m, ok := processLocks[path]; ok { - return m - } - m := &sync.Mutex{} - processLocks[path] = m - return m -} diff --git a/internal/infrastructure/cronstore/schedule_duration_test.go b/internal/infrastructure/cronstore/schedule_duration_test.go deleted file mode 100644 index c1457c685..000000000 --- a/internal/infrastructure/cronstore/schedule_duration_test.go +++ /dev/null @@ -1,91 +0,0 @@ -package cronstore - -import ( - "encoding/json" - "strings" - "testing" - "time" -) - -// TestScheduleIntervalWritesADurationString pins the document form: the interval -// an operator edits must read as a duration, not as the nanosecond count that -// asked them to type 1800000000000. -func TestScheduleIntervalWritesADurationString(t *testing.T) { - encoded, err := json.Marshal(Schedule{Kind: ScheduleInterval, Interval: Duration(30 * time.Minute)}) - if err != nil { - t.Fatalf("marshal: %v", err) - } - if got := string(encoded); !strings.Contains(got, `"interval":"30m0s"`) { - t.Errorf("marshalled schedule = %s, want interval as the string \"30m0s\"", got) - } - if strings.Contains(string(encoded), "1800000000000") { - t.Errorf("marshalled schedule = %s, want no nanosecond count", encoded) - } -} - -// TestScheduleIntervalReadsEveryStoredForm covers what the store already holds. -// Every schedule written before this shape existed carries a nanosecond count, in -// both the control plane's schedules resource and this package's own jobs table, -// and both have to keep working without being re-saved first. -func TestScheduleIntervalReadsEveryStoredForm(t *testing.T) { - for _, tt := range []struct { - name string - document string - expected time.Duration - }{ - {"legacy nanosecond count", `{"kind":"interval","interval":1800000000000}`, 30 * time.Minute}, - {"duration string", `{"kind":"interval","interval":"30m"}`, 30 * time.Minute}, - {"duration string, canonical", `{"kind":"interval","interval":"30m0s"}`, 30 * time.Minute}, - } { - t.Run(tt.name, func(t *testing.T) { - var schedule Schedule - if err := json.Unmarshal([]byte(tt.document), &schedule); err != nil { - t.Fatalf("unmarshal %s: %v", tt.document, err) - } - if schedule.Interval.Std() != tt.expected { - t.Errorf("Interval = %v, want %v", schedule.Interval, tt.expected) - } - if err := schedule.Validate(); err != nil { - t.Errorf("Validate() = %v, want the decoded schedule to be usable", err) - } - }) - } -} - -// TestScheduleIntervalRefusesWhatItCannotUse keeps the tolerance above from -// becoming indifference: a duration that does not parse must fail loudly rather -// than decode as zero, because a zero interval is a job that never fires. -func TestScheduleIntervalRefusesWhatItCannotUse(t *testing.T) { - for _, tt := range []struct{ name, document string }{ - {"an unparseable duration", `{"kind":"interval","interval":"soon"}`}, - {"a fractional count", `{"kind":"interval","interval":1.5}`}, - {"a null interval", `{"kind":"interval","interval":null}`}, - } { - t.Run(tt.name, func(t *testing.T) { - var schedule Schedule - if err := json.Unmarshal([]byte(tt.document), &schedule); err == nil { - t.Fatalf("unmarshal(%s) = nil, want a refusal: a silent zero interval never fires", tt.document) - } - }) - } -} - -// TestScheduleIntervalRoundTripsThroughTheStore pins the shape the jobs table -// keeps, since it persists the spec as JSON rather than as columns. -func TestScheduleIntervalRoundTripsThroughTheStore(t *testing.T) { - spec := JobSpec{ID: "nightly", Schedule: Schedule{Kind: ScheduleInterval, Interval: Duration(90 * time.Minute)}} - encoded, err := json.Marshal(spec) - if err != nil { - t.Fatalf("marshal: %v", err) - } - if !strings.Contains(string(encoded), `"interval":"1h30m0s"`) { - t.Fatalf("marshalled spec = %s, want the interval as \"1h30m0s\"", encoded) - } - var decoded JobSpec - if err := json.Unmarshal(encoded, &decoded); err != nil { - t.Fatalf("unmarshal: %v", err) - } - if decoded.Schedule.Interval.Std() != 90*time.Minute { - t.Errorf("round-tripped Interval = %v, want 1h30m0s", decoded.Schedule.Interval) - } -} diff --git a/internal/infrastructure/cronstore/store.go b/internal/infrastructure/cronstore/store.go deleted file mode 100644 index 89df661ed..000000000 --- a/internal/infrastructure/cronstore/store.go +++ /dev/null @@ -1,665 +0,0 @@ -package cronstore - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "fmt" - "io/fs" - "os" - "path/filepath" - "strings" - "sync" - "sync/atomic" - "time" - - "github.com/samcharles93/archie-core/internal/domain/scheduling" -) - -// schemaVersion is stamped on every persisted file. Bump it on any -// breaking change to JobSpec, Schedule, Payload or envelope shape, AND -// on any additive change: DisallowUnknownFields means a future build -// that adds a new field to JobSpec must also bump schemaVersion so -// older builds keep rejecting the file rather than silently dropping -// the new field. -const schemaVersion = 2 - -// filePerm is the mode applied to the jobs.json file on write. Payloads -// and chat ids can carry operator-identifying information, so 0o600 is -// the default; operators who want it group-readable can chmod after. -const filePerm = 0o600 - -// JobSpec is one persisted cron job as the engine's view sees it, plus -// the bookkeeping fields the store computes (NextRun, LastRun, Created, -// Updated). The wire shape is the public API; the scheduling.Job returned -// to the engine is a stripped subset. -type JobSpec struct { - // ID is the unique identifier. Required, non-empty, and the merge - // key for Update and MarkRun. - ID string `json:"id"` - - // Pool is "parallel" or "sequential". Empty means sequential — the - // safer default, matching scheduling.Pool.resolve(). - Pool string `json:"pool,omitempty"` - - // Detail is a human-readable label rendered on events. - Detail string `json:"detail,omitempty"` - - // Kind selects which delivery runner handles this job. Empty means - // KindChat, so a job written before this field existed (schema - // version 1) keeps delivering as a chat message. The store does not - // interpret the value: an unrecognised kind is persisted verbatim and - // refused by the delivery router, which is the one place the mapping - // from kind to runner lives. - Kind string `json:"kind,omitempty"` - - // Schedule decides when this job is due. See schedule.go for the - // supported kinds and their arithmetic. - Schedule Schedule `json:"schedule"` - - // Target and Payload are placeholders for Slice 2 (delivery runner). - // They live on the wire now so jobs created ahead of Slice 2 do not - // need a schema migration when delivery lands. - Target Target `json:"target"` - Payload Payload `json:"payload"` - - // NextRun is the absolute moment the job will next fire. Computed - // at write time so the engine's hot path is a single comparison. - NextRun time.Time `json:"next_run"` - - // LastRun is the last run time reported by MarkRun, nil if the job - // has never run. Mutated only by MarkRun. The store does not know - // whether the run succeeded — that judgement belongs to the runner. - LastRun *time.Time `json:"last_run,omitempty"` - - // Created and Updated are stamped by Create / Update respectively. - // They are audit metadata; the engine does not read them. - Created time.Time `json:"created"` - Updated time.Time `json:"updated"` -} - -// Target identifies where the job's output goes (Slice 2 fills this in). -// Empty Target is valid — Slice 1 stores the field but no consumer reads -// it yet. -type Target struct { - // ChatID is the chat channel id (Telegram chat id, email address, - // webhook URL — depends on the channel). Empty defers to a default - // chosen by the delivery runner. - ChatID string `json:"chat_id,omitempty"` -} - -// Payload is the content sent at run time. Slice 2 fills this in; Slice 1 -// stores the text verbatim so the engine does not need to know about it. -type Payload struct { - // Text is the literal message body. Templating (cron blueprints) is - // a Slice 3 concern and is deliberately absent here. - Text string `json:"text,omitempty"` -} - -// Patch is the partial JobSpec Update merges over the existing record. A -// nil pointer field leaves the corresponding persisted field untouched; -// the non-nil pointer or non-zero value overwrites it. -// -// Schedule is replaced wholesale when the Patch's Schedule pointer is -// non-nil — schedules are not deep-merged, because each field of -// Schedule carries semantics (Kind, Interval, Cron, At) that have no -// meaningful "preserve" answer on their own. -type Patch struct { - Pool *string `json:"pool,omitempty"` - Detail *string `json:"detail,omitempty"` - Schedule *Schedule `json:"schedule,omitempty"` - Target *Target `json:"target,omitempty"` - Payload *Payload `json:"payload,omitempty"` -} - -// envelope is the on-disk shape. Kept private so the only public surface -// is JobSpec / Patch. -type envelope struct { - SchemaVersion int `json:"schema_version"` - Jobs []JobSpec `json:"jobs"` -} - -// Store is one open handle to the job file. It is safe for concurrent -// callers in the same process, and safe against concurrent CLI / daemon -// processes through the cross-process lock. -// -// All exported methods take a context for cancellation; reads and writes -// are short, so the ctx is plumbed through but rarely observed. -// -// After Close, every other method returns ErrClosed. Close itself is -// safe to call multiple times. -type Store struct { - path string // jobs.json - lock *lockedFile - - // closed is set by Close so concurrent callers fail fast rather than - // dereferencing a closed lock fd. - closed atomic.Bool - - // mu guards the in-memory copy that mirrors the file. Acquire it - // before reading or writing jobs; the cross-process lock guards - // the file itself. They must always be taken in the same order: - // cross-process lock first, then mu. - mu sync.Mutex - buf []JobSpec // in-memory mirror of the on-disk jobs array -} - -// ErrClosed is returned by every method after Close. It lets callers -// distinguish "I gave up" from a real error. -var ErrClosed = errors.New("cronstore: store is closed") - -// Open constructs a Store for the given jobs.json path. The file is -// created lazily on first write, but the parent directory is created -// immediately so a subsequent Create does not race against mkdir. -// -// Open validates the persisted file's schema version: a future version -// the build does not know returns ErrUnsupportedSchema rather than a -// silent best-effort decode. A corrupt or truncated file returns -// ErrCorruptFile. -func Open(path string) (*Store, error) { - if path == "" { - return nil, fmt.Errorf("%w: path must be non-empty", ErrInvalidSpec) - } - if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { - return nil, fmt.Errorf("cronstore: create parent directory: %w", err) - } - lock, err := openLock(path + ".lock") - if err != nil { - return nil, fmt.Errorf("cronstore: open lock: %w", err) - } - - s := &Store{ - path: path, - lock: lock, - } - - // Best-effort hydrate. An absent file is fine: the store will - // materialise it on first Create. - if err := s.hydrate(); err != nil { - _ = lock.Close() - return nil, err - } - return s, nil -} - -// Path returns the file the store reads and writes. Exposed for -// diagnostics and for the CLI to print the location it is editing. -func (s *Store) Path() string { return s.path } - -// Close releases the cross-process lock. After Close, every other -// method returns ErrClosed. Close is safe to call more than once. -func (s *Store) Close() error { - if !s.closed.CompareAndSwap(false, true) { - return nil - } - if s.lock == nil { - return nil - } - err := s.lock.Close() - s.lock = nil - return err -} - -// checkOpen returns ErrClosed when the store has been closed. Every -// public method calls this first so a Close mid-operation surfaces as a -// real error rather than a panic on a nil lock fd. -func (s *Store) checkOpen() error { - if s.closed.Load() { - return ErrClosed - } - return nil -} - -// Create adds a new job. Fails with ErrJobExists on a duplicate id, with -// ErrInvalidSpec on a structurally invalid spec. The store stamps -// Created / Updated to now and computes NextRun from the schedule. -func (s *Store) Create(ctx context.Context, spec JobSpec) error { - if err := ctx.Err(); err != nil { - return err - } - if err := s.checkOpen(); err != nil { - return err - } - if strings.TrimSpace(spec.ID) == "" { - return fmt.Errorf("%w: id must be non-empty", ErrInvalidSpec) - } - if err := spec.Schedule.Validate(); err != nil { - return err - } - if err := scheduling.Pool(spec.Pool).Validate(); err != nil { - return err - } - - if err := s.withLock(func() error { - for _, existing := range s.buf { - if existing.ID == spec.ID { - return fmt.Errorf("%w: id %q", ErrJobExists, spec.ID) - } - } - - now := time.Now().UTC() - // Honor a caller-supplied NextRun when present: the test suite - // needs to set up past/future due states deterministically, and - // the CLI may want to schedule a one-off for a specific time. - // Zero NextRun falls back to the schedule's first-run arithmetic. - if spec.NextRun.IsZero() { - next, err := spec.Schedule.firstRun(now) - if err != nil { - return err - } - spec.NextRun = next.UTC() - } - spec.Created = now - spec.Updated = now - spec.Schedule = spec.Schedule.resolve() - s.buf = append(s.buf, spec) - return s.writeLocked() - }); err != nil { - return err - } - return nil -} - -// Get returns the job by id. The second return value is false when the -// id is absent; the error is reserved for I/O failures. The returned -// JobSpec is a deep copy: callers can mutate LastRun and other fields -// without disturbing the store's in-memory mirror. -func (s *Store) Get(ctx context.Context, id string) (JobSpec, bool, error) { - if err := ctx.Err(); err != nil { - return JobSpec{}, false, err - } - if err := s.checkOpen(); err != nil { - return JobSpec{}, false, err - } - var ( - got JobSpec - ok bool - ) - err := s.withLock(func() error { - for _, j := range s.buf { - if j.ID == id { - got = deepCopyJob(j) - ok = true - return nil - } - } - return nil - }) - return got, ok, err -} - -// List returns a copy of every persisted job. Order is the on-disk -// order, which is also the creation order. Callers may mutate the -// returned slice and its elements without disturbing the store's -// in-memory mirror. -func (s *Store) List(ctx context.Context) ([]JobSpec, error) { - if err := ctx.Err(); err != nil { - return nil, err - } - if err := s.checkOpen(); err != nil { - return nil, err - } - var out []JobSpec - err := s.withLock(func() error { - out = make([]JobSpec, len(s.buf)) - for i := range s.buf { - out[i] = deepCopyJob(s.buf[i]) - } - return nil - }) - return out, err -} - -// Update applies a Patch over the existing record. Fields the Patch -// leaves nil are preserved verbatim; non-nil pointers and the Schedule -// are wholesale replacements. Schedule changes trigger a NextRun -// recomputation, since NextRun is derived from Schedule and LastRun. -// -// Returns ErrJobNotFound when the id is absent. -func (s *Store) Update(ctx context.Context, id string, patch Patch) error { - if err := ctx.Err(); err != nil { - return err - } - if err := s.checkOpen(); err != nil { - return err - } - if id == "" { - return fmt.Errorf("%w: id must be non-empty", ErrInvalidSpec) - } - if patch.Pool != nil { - if err := scheduling.Pool(*patch.Pool).Validate(); err != nil { - return err - } - } - if patch.Schedule != nil { - if err := patch.Schedule.Validate(); err != nil { - return err - } - } - return s.withLock(func() error { - for i := range s.buf { - if s.buf[i].ID != id { - continue - } - if err := applyPatch(&s.buf[i], patch); err != nil { - return err - } - s.buf[i].Updated = time.Now().UTC() - return s.writeLocked() - } - return fmt.Errorf("%w: id %q", ErrJobNotFound, id) - }) -} - -// applyPatch overwrites record's non-nil Patch fields in place, recomputing -// NextRun when Schedule changes. -func applyPatch(record *JobSpec, patch Patch) error { - if patch.Pool != nil { - record.Pool = *patch.Pool - } - if patch.Detail != nil { - record.Detail = *patch.Detail - } - if patch.Schedule != nil { - if err := applyScheduleChange(record, patch.Schedule); err != nil { - return err - } - } - if patch.Target != nil { - record.Target = *patch.Target - } - if patch.Payload != nil { - record.Payload = *patch.Payload - } - return nil -} - -// applyScheduleChange sets record's Schedule and recomputes NextRun, which -// is stale once Schedule changes. Recomputed from the last run if there is -// one, else as a first run from now — keeping it strictly future-of-now -// would re-fire a job the operator just shortened. -func applyScheduleChange(record *JobSpec, schedule *Schedule) error { - record.Schedule = schedule.resolve() - var next time.Time - var err error - if record.LastRun != nil { - next, err = record.Schedule.nextRun(*record.LastRun) - } else { - next, err = record.Schedule.firstRun(time.Now().UTC()) - } - if err != nil { - return err - } - record.NextRun = next.UTC() - return nil -} - -// Delete removes the job by id. Idempotent: deleting a missing id -// returns nil rather than ErrJobNotFound, so a CLI retry after a -// partial failure cannot stall the operator. -func (s *Store) Delete(ctx context.Context, id string) error { - if err := ctx.Err(); err != nil { - return err - } - if err := s.checkOpen(); err != nil { - return err - } - if id == "" { - return fmt.Errorf("%w: id must be non-empty", ErrInvalidSpec) - } - return s.withLock(func() error { - for i := range s.buf { - if s.buf[i].ID != id { - continue - } - s.buf = append(s.buf[:i], s.buf[i+1:]...) - return s.writeLocked() - } - return nil - }) -} - -// MarkRun records that id ran at runAt and advances NextRun to the -// schedule's next firing. It is the engine-side counterpart to Create: -// once the ticker has dispatched a job, the runner (or the engine -// itself, in tests) calls MarkRun to keep next_run fresh. -// -// Returns ErrJobNotFound when the id is absent and -// ErrScheduleUnsupported when the schedule kind has no defined -// "next after this run" semantics. -func (s *Store) MarkRun(ctx context.Context, id string, runAt time.Time) error { - if err := ctx.Err(); err != nil { - return err - } - if err := s.checkOpen(); err != nil { - return err - } - if id == "" { - return fmt.Errorf("%w: id must be non-empty", ErrInvalidSpec) - } - return s.withLock(func() error { - for i := range s.buf { - if s.buf[i].ID != id { - continue - } - next, err := s.buf[i].Schedule.nextRun(runAt) - if err != nil { - return err - } - t := runAt.UTC() - s.buf[i].LastRun = &t - s.buf[i].NextRun = next.UTC() - s.buf[i].Updated = time.Now().UTC() - return s.writeLocked() - } - return fmt.Errorf("%w: id %q", ErrJobNotFound, id) - }) -} - -// Due implements scheduling.JobSource. It returns the jobs whose -// NextRun is at or before now, in creation order. The returned -// scheduling.Job is the engine's projection of the persisted JobSpec: -// only the fields the engine relies on (ID, Pool, Detail) cross the -// boundary. Schedule / Payload / Target are deliberately not exposed: -// the engine does no arithmetic on them and has no business knowing -// they exist. -// -// A job with an unrecognised pool is skipped: Create / Update enforce -// the set, but a file edited by hand could carry an invalid value, and -// the engine cannot reason about a pool it has never heard of. The -// store does not crash on bad input — the same way it does not panic -// on a truncated file. -func (s *Store) Due(ctx context.Context, now time.Time) ([]scheduling.Job, error) { - if err := ctx.Err(); err != nil { - return nil, err - } - if err := s.checkOpen(); err != nil { - return nil, err - } - var due []scheduling.Job - err := s.withLock(func() error { - for _, j := range s.buf { - if j.NextRun.After(now) { - continue - } - pool := scheduling.Pool(j.Pool) - if err := pool.Validate(); err != nil { - continue - } - due = append(due, scheduling.Job{ - ID: j.ID, - Pool: pool, - Detail: j.Detail, - }) - } - return nil - }) - return due, err -} - -// withLock serialises access to the file. Callers receive a closure -// that runs with the cross-process lock held and the in-memory buffer -// freshly hydrated from disk; the closure's return value becomes -// withLock's return value. -// -// Three locks stack, always in this order: -// -// 1. processMu: keyed by lock-file path. Serialises every Store that -// points at the same jobs.json within one process. Necessary -// because Linux flock is per file description: two Store instances -// in the same process have independent fds and would otherwise -// proceed in parallel. fcntl / POSIX locks would not have this -// property — they would block — but flock is the documented -// primitive on Linux + macOS, so we compensate at this layer. -// 2. cross-process flock: serialises Store instances across -// processes (CLI vs daemon). flock alone is insufficient for -// in-process coordination (see #1). -// 3. s.mu: guards s.buf against same-Store concurrent goroutines. -// Redundant under #1 + #2 but cheap, and it documents the -// invariant for any reader trying to follow the locking story. -// -// After all three are held, the in-memory buffer is re-read from disk -// so a writer that just acquired the locks sees every change the -// previous writer committed. Without this, a Store that opened before -// any writes happened would carry an empty view forever and silently -// overwrite everything. -// -// Re-reading on every operation is acceptable for cron workloads: the -// file holds O(dozens) of jobs, not O(thousands), and operations are -// human-paced, not high-throughput. -func (s *Store) withLock(fn func() error) error { - processMu := processLockFor(s.path + ".lock") - processMu.Lock() - defer processMu.Unlock() - - if s.closed.Load() { - return ErrClosed - } - if err := s.lock.Lock(); err != nil { - return fmt.Errorf("cronstore: acquire lock: %w", err) - } - defer func() { _ = s.lock.Unlock() }() - - s.mu.Lock() - defer s.mu.Unlock() - - if err := s.hydrate(); err != nil { - return err - } - return fn() -} - -// hydrate reads the file into the in-memory buffer. Called from Open -// (no lock needed; nothing else can see the Store yet) and from -// withLock (with both locks held). -func (s *Store) hydrate() error { - data, err := os.ReadFile(s.path) - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return nil - } - return fmt.Errorf("%w: read %s: %w", ErrCorruptFile, s.path, err) - } - if len(bytes.TrimSpace(data)) == 0 { - return nil - } - var env envelope - dec := json.NewDecoder(bytes.NewReader(data)) - // Reject unknown fields so a typo on the operator's side does not - // silently land in a future field and shadow a real value. The - // cost is that any additive change to JobSpec MUST bump - // schemaVersion — see the comment on that constant. - dec.DisallowUnknownFields() - if err := dec.Decode(&env); err != nil { - return fmt.Errorf("%w: decode %s: %w", ErrCorruptFile, s.path, err) - } - if env.SchemaVersion == 0 { - return fmt.Errorf("%w: missing schema_version in %s", ErrCorruptFile, s.path) - } - if env.SchemaVersion > schemaVersion { - return fmt.Errorf("%w: file is version %d, this build understands up to %d", - ErrUnsupportedSchema, env.SchemaVersion, schemaVersion) - } - s.buf = env.Jobs - return nil -} - -// writeLocked persists the in-memory buffer to the file. Must be called -// with s.mu held and the cross-process lock held — both guarantees -// follow from withLock. Atomic write via temp-file + rename, so a -// crash mid-write leaves the previous file intact rather than a -// truncated mess the next Open would have to clean up. -func (s *Store) writeLocked() error { - env := envelope{ - SchemaVersion: schemaVersion, - Jobs: s.buf, - } - var buf bytes.Buffer - enc := json.NewEncoder(&buf) - // operator-readable file: do not escape <, >, &. The default - // encoding/json behaviour writes these as < etc, which is - // round-trip-safe but unreadable when a human opens jobs.json to - // understand why a job failed to fire. - enc.SetEscapeHTML(false) - if err := enc.Encode(env); err != nil { - return fmt.Errorf("cronstore: encode: %w", err) - } - if err := atomicWriteFile(s.path, buf.Bytes(), filePerm); err != nil { - return fmt.Errorf("cronstore: write %s: %w", s.path, err) - } - return nil -} - -// atomicWriteFile writes data to path via a sibling temp file and -// rename. Rename within a single filesystem is atomic on POSIX, so a -// concurrent reader either sees the previous file in full or the new -// one in full — never a half-written body. Sync before rename gives a -// durability guarantee: the inode's data is on disk before the -// directory entry switches over. -func atomicWriteFile(path string, data []byte, perm os.FileMode) error { - dir := filepath.Dir(path) - tmp, err := os.CreateTemp(dir, filepath.Base(path)+".tmp-*") - if err != nil { - return err - } - tmpName := tmp.Name() - defer func() { - // Best-effort cleanup on any failure path. The rename has - // either happened (and tmpName is gone) or it has not (and - // the temp file still needs to go). - _ = os.Remove(tmpName) - }() - if _, err := tmp.Write(data); err != nil { - _ = tmp.Close() - return err - } - if err := tmp.Sync(); err != nil { - _ = tmp.Close() - return err - } - if err := tmp.Close(); err != nil { - return err - } - if err := os.Chmod(tmpName, perm); err != nil { - return err - } - return os.Rename(tmpName, path) -} - -// deepCopyJob returns a JobSpec value independent of the source's -// pointer fields. JobSpec carries one pointer (LastRun); a shallow -// copy aliases it and lets callers mutate the store's in-memory state -// by writing through the returned value. -func deepCopyJob(j JobSpec) JobSpec { - out := j - if j.LastRun != nil { - t := *j.LastRun - out.LastRun = &t - } - if j.Schedule.At != nil { - t := *j.Schedule.At - out.Schedule.At = &t - } - return out -} diff --git a/internal/infrastructure/cronstore/store_review_test.go b/internal/infrastructure/cronstore/store_review_test.go deleted file mode 100644 index 19b87fea2..000000000 --- a/internal/infrastructure/cronstore/store_review_test.go +++ /dev/null @@ -1,485 +0,0 @@ -// Additional tests added in response to the Slice-1 adversarial review. -// They live in a separate file so the original spec file stays focused -// on the contract-level behaviour the engine depends on, and these -// cases stay grouped where the reviewer can re-read them as a unit. -package cronstore - -import ( - "bytes" - "context" - "errors" - "os" - "os/exec" - "path/filepath" - "sync" - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/domain/scheduling" -) - -// --- C2 + P8: ErrCorruptFile is the right sentinel for bad files ------- - -func TestOpenTruncatedFileReturnsErrCorruptFile(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - if err := os.WriteFile(path, []byte(`{"schema_version":1,"jobs":[{"id":"x","`), 0o600); err != nil { - t.Fatalf("WriteFile: %v", err) - } - _, err := Open(path) - if !errors.Is(err, ErrCorruptFile) { - t.Fatalf("Open on truncated file = %v, want ErrCorruptFile", err) - } -} - -func TestOpenGarbageFileReturnsErrCorruptFile(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - if err := os.WriteFile(path, []byte("not json at all"), 0o600); err != nil { - t.Fatalf("WriteFile: %v", err) - } - _, err := Open(path) - if !errors.Is(err, ErrCorruptFile) { - t.Fatalf("Open on garbage file = %v, want ErrCorruptFile", err) - } -} - -func TestOpenEmptyFileIsTreatedAsNoJobs(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - if err := os.WriteFile(path, []byte(" \n\t "), 0o600); err != nil { - t.Fatalf("WriteFile: %v", err) - } - s, err := Open(path) - if err != nil { - t.Fatalf("Open on whitespace-only file = %v, want nil", err) - } - defer s.Close() - jobs, _ := s.List(context.Background()) - if len(jobs) != 0 { - t.Errorf("List on whitespace-only file = %d jobs, want 0", len(jobs)) - } -} - -// --- C6 / P8: DisallowUnknownFields rejects hand-edited extras --------- - -func TestOpenRejectsUnknownFieldAtSameSchemaVersion(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - // A future build might add a "tags" field to JobSpec. An older - // build with DisallowUnknownFields set must reject the file rather - // than silently dropping the field — that's the contract behind - // bumping schemaVersion on additive changes. - contents := `{"schema_version":1,"jobs":[{"id":"x","pool":"parallel","schedule":{"kind":"interval","interval":60000000000},"target":{},"payload":{},"next_run":"2026-08-28T12:00:00Z","created":"2026-08-28T12:00:00Z","updated":"2026-08-28T12:00:00Z","tags":["ops"]}]}` - if err := os.WriteFile(path, []byte(contents), 0o600); err != nil { - t.Fatalf("WriteFile: %v", err) - } - _, err := Open(path) - if !errors.Is(err, ErrCorruptFile) { - t.Fatalf("Open on file with unknown field = %v, want ErrCorruptFile", err) - } -} - -// --- P2: Get / List return deep copies ------------------------------- - -func TestGetReturnsDeepCopyOfLastRun(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("x", time.Hour)) - first, _, _ := s.Get(context.Background(), "x") - if first.LastRun != nil { - t.Fatalf("LastRun on fresh create = %v, want nil", first.LastRun) - } - runAt := time.Now().UTC().Add(-time.Minute) - if err := s.MarkRun(context.Background(), "x", runAt); err != nil { - t.Fatalf("MarkRun: %v", err) - } - got, _, _ := s.Get(context.Background(), "x") - if got.LastRun == nil { - t.Fatal("LastRun is nil after MarkRun") - } - // Caller mutates the returned LastRun. The store's in-memory - // value must NOT change — a future Get must still report the - // original runAt. - *got.LastRun = time.Date(2099, 1, 1, 0, 0, 0, 0, time.UTC) - got2, _, _ := s.Get(context.Background(), "x") - if !got2.LastRun.Equal(runAt) { - t.Errorf("after caller mutation, Get returned LastRun=%v, want %v", got2.LastRun, runAt) - } -} - -func TestListReturnsDeepCopies(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("x", time.Hour)) - mustCreate(t, s, fixedIntervalSpec("y", time.Hour)) - - list1, _ := s.List(context.Background()) - if len(list1) != 2 { - t.Fatalf("List returned %d jobs, want 2", len(list1)) - } - // Caller mutates the returned slice (deletes an entry) and a job's - // string fields. Neither must touch the store's in-memory mirror. - list1[0].Detail = "tampered" - - list2, _ := s.List(context.Background()) - if len(list2) != 2 { - t.Errorf("after caller truncated list, second List returned %d jobs, want 2", len(list2)) - } - for _, j := range list2 { - if j.Detail == "tampered" { - t.Errorf("caller's Detail mutation leaked into the store: %+v", j) - } - } -} - -// --- P3: pool validation in Create / Update --------------------------- - -func TestCreateRejectsInvalidPool(t *testing.T) { - s := newTestStore(t) - spec := fixedIntervalSpec("x", time.Minute) - spec.Pool = "paralle" // typo - if err := s.Create(context.Background(), spec); err == nil { - t.Fatal("Create with invalid pool succeeded; want error") - } -} - -func TestUpdateRejectsInvalidPool(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("x", time.Minute)) - if err := s.Update(context.Background(), "x", Patch{Pool: new("paralle")}); err == nil { - t.Fatal("Update with invalid pool succeeded; want error") - } -} - -// --- P5 / C1 / P6: Once schedule behaviour + MarkRun sentinel --------- - -func TestCreateAcceptsOnceSchedule(t *testing.T) { - s := newTestStore(t) - at := time.Now().Add(time.Hour) - spec := JobSpec{ - ID: "one-off", - Schedule: Schedule{ - Kind: ScheduleOnce, - At: &at, - }, - } - if err := s.Create(context.Background(), spec); err != nil { - t.Fatalf("Create with Once schedule: %v", err) - } -} - -func TestDueFiresOnceAtConfiguredTime(t *testing.T) { - s := newTestStore(t) - at := time.Date(2026, 8, 28, 12, 0, 0, 0, time.UTC) - spec := JobSpec{ - ID: "one-off", - Schedule: Schedule{ - Kind: ScheduleOnce, - At: &at, - }, - } - if err := s.Create(context.Background(), spec); err != nil { - t.Fatalf("Create: %v", err) - } - - due, err := s.Due(context.Background(), at) - if err != nil { - t.Fatalf("Due: %v", err) - } - if len(due) != 1 || due[0].ID != "one-off" { - t.Errorf("Due(at) = %+v, want one-off", due) - } - due, _ = s.Due(context.Background(), at.Add(-time.Second)) - if len(due) != 0 { - t.Errorf("Due(at - 1s) = %+v, want empty", due) - } -} - -func TestMarkRunOnOnceReturnsErrScheduleUnsupported(t *testing.T) { - s := newTestStore(t) - at := time.Now().Add(time.Hour) - spec := JobSpec{ - ID: "one-off", - Schedule: Schedule{ - Kind: ScheduleOnce, - At: &at, - }, - } - if err := s.Create(context.Background(), spec); err != nil { - t.Fatalf("Create: %v", err) - } - err := s.MarkRun(context.Background(), "one-off", time.Now()) - if !errors.Is(err, ErrScheduleUnsupported) { - t.Fatalf("MarkRun on once = %v, want ErrScheduleUnsupported", err) - } -} - -// --- P8: Due drops unknown pool values silently ----------------------- - -func TestDueSilentlySkipsUnknownPool(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - s, err := Open(path) - if err != nil { - t.Fatalf("Open: %v", err) - } - defer s.Close() - // Hand-edit the file: bypass Create's validation by writing a - // job with an invalid pool directly to disk. - past := time.Now().Add(-time.Minute).UTC().Format(time.RFC3339Nano) - contents := `{"schema_version":1,"jobs":[{"id":"badpool","pool":"paralle","schedule":{"kind":"interval","interval":60000000000},"target":{},"payload":{},"next_run":"` + past + `","created":"` + past + `","updated":"` + past + `"}]}` - if err := os.WriteFile(path, []byte(contents), 0o600); err != nil { - t.Fatalf("WriteFile: %v", err) - } - // Drop the in-memory mirror so the next read picks up the file. - if err := s.hydrate(); err != nil { - t.Fatalf("hydrate: %v", err) - } - due, err := s.Due(context.Background(), time.Now()) - if err != nil { - t.Fatalf("Due: %v", err) - } - if len(due) != 0 { - t.Errorf("Due returned %d jobs for an invalid pool, want 0", len(due)) - } - // But Get still sees the job — Create/Update validation is the - // gate that prevents this in normal operation; a hand-edited - // file remains visible through CRUD. - if _, ok, _ := s.Get(context.Background(), "badpool"); !ok { - t.Error("Get returned ok=false for a hand-edited job with bad pool") - } -} - -// --- P10: UTF-8 + quote + newline round-trip -------------------------- - -func TestCreateAndGetRoundTripWithUnicodeAndQuotes(t *testing.T) { - s := newTestStore(t) - spec := JobSpec{ - ID: "ops-standup", - Pool: string(scheduling.PoolSequential), - Detail: "Hello\n\"世界\"\\back", - Schedule: Schedule{ - Kind: ScheduleInterval, - Interval: Duration(30 * time.Minute), - }, - Payload: Payload{Text: " & 中文"}, - } - mustCreate(t, s, spec) - got, ok, err := s.Get(context.Background(), spec.ID) - if err != nil { - t.Fatalf("Get: %v", err) - } - if !ok { - t.Fatal("Get returned ok=false") - } - if got.Detail != spec.Detail { - t.Errorf("Detail = %q, want %q", got.Detail, spec.Detail) - } - if got.Payload.Text != spec.Payload.Text { - t.Errorf("Payload.Text = %q, want %q", got.Payload.Text, spec.Payload.Text) - } -} - -// --- C3: operator-readable file does not HTML-escape ------------------- - -func TestFileIsOperatorReadableNoHTMLEscape(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - s, _ := Open(path) - defer s.Close() - spec := fixedIntervalSpec("x", time.Minute) - spec.Payload.Text = "" - mustCreate(t, s, spec) - _ = s.Close() - - data, err := os.ReadFile(path) - if err != nil { - t.Fatalf("ReadFile: %v", err) - } - // With SetEscapeHTML(false), < > & appear as the literal characters. - // The escaped form would be <, >, & — none of which - // is the printable payload. Assert the literal is there. - if !bytes.Contains(data, []byte(`""`)) { - t.Errorf("payload was HTML-escaped; want operator-readable literal: %s", data) - } -} - -// --- C4: file is 0o600, not 0o644 -------------------------------------- - -func TestFileIsCreatedWithOwnerOnlyPermissions(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - s, _ := Open(path) - defer s.Close() - mustCreate(t, s, fixedIntervalSpec("x", time.Minute)) - _ = s.Close() - - info, err := os.Stat(path) - if err != nil { - t.Fatalf("Stat: %v", err) - } - perm := info.Mode().Perm() - if perm != 0o600 { - t.Errorf("jobs.json perm = %#o, want 0o600", perm) - } -} - -// --- C5: cross-process flock via os/exec ------------------------------ - -// TestCrossProcessFlockBlocksParentWhileChildHoldsLock runs a child -// process that grabs the cross-process flock and sleeps; the parent -// then tries to take the same lock and must block for at least the -// child's sleep duration. Without the flock, the parent's Create -// would return immediately and the test would fail. -func TestCrossProcessFlockBlocksParentWhileChildHoldsLock(t *testing.T) { - if testing.Short() { - t.Skip("subprocess test") - } - if _, err := exec.LookPath("go"); err != nil { - t.Skip("no go toolchain available for subprocess test") - } - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - ctx := t.Context() - - // Build a tiny helper that opens the same file and holds the - // flock. The helper is built once per run; the cost is paid only - // when this test runs. - helperSrc := filepath.Join(dir, "helper.go") - if err := os.WriteFile(helperSrc, []byte(` -package main - -import ( - "fmt" - "os" - "syscall" - "time" -) - -func main() { - f, err := os.OpenFile(os.Args[1]+".lock", os.O_RDWR|os.O_CREATE, 0o644) - if err != nil { fmt.Println("open:", err); os.Exit(2) } - if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil { - fmt.Println("flock:", err); os.Exit(3) - } - fmt.Println("locked") - // Signal readiness, then hold the lock long enough for the - // parent to observe the block. - time.Sleep(2 * time.Second) - _ = f.Close() -} -`), 0o644); err != nil { - t.Fatalf("WriteFile helper: %v", err) - } - helperBin := filepath.Join(dir, "helper") - if out, err := exec.CommandContext(ctx, "go", "build", "-o", helperBin, helperSrc).CombinedOutput(); err != nil { - t.Fatalf("go build helper: %v\n%s", err, out) - } - - // Launch the child in the background. - child := exec.CommandContext(ctx, helperBin, path) - child.Stdout = os.Stdout - child.Stderr = os.Stderr - if err := child.Start(); err != nil { - t.Fatalf("child Start: %v", err) - } - defer func() { - _ = child.Process.Kill() - _ = child.Wait() - }() - - // Give the child a moment to acquire the lock. - time.Sleep(300 * time.Millisecond) - - // From the parent, open the same Store and call Create. Without - // flock, Create would return instantly; with flock, it must - // block until the child releases. - parent, err := Open(path) - if err != nil { - t.Fatalf("parent Open: %v", err) - } - defer parent.Close() - - start := time.Now() - if err := parent.Create(context.Background(), fixedIntervalSpec("from-parent", time.Minute)); err != nil { - t.Fatalf("parent Create: %v", err) - } - elapsed := time.Since(start) - - // The child held the lock for ~2s. The parent's Create must have - // blocked for at least the overlap between its start and the - // child's release — conservatively, >1s. - if elapsed < time.Second { - t.Errorf("parent Create returned in %v; expected to block on the child's flock", elapsed) - } -} - -// --- P1: Close mid-operation returns ErrClosed ------------------------ - -func TestMethodsReturnErrClosedAfterClose(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("x", time.Minute)) - if err := s.Close(); err != nil { - t.Fatalf("Close: %v", err) - } - // Every public method must surface ErrClosed, not panic on the - // nil lock fd. - tests := []struct { - name string - fn func() error - }{ - {"Create", func() error { return s.Create(context.Background(), fixedIntervalSpec("y", time.Minute)) }}, - {"Get", func() error { _, _, err := s.Get(context.Background(), "x"); return err }}, - {"List", func() error { _, err := s.List(context.Background()); return err }}, - {"Update", func() error { return s.Update(context.Background(), "x", Patch{Detail: new("z")}) }}, - {"Delete", func() error { return s.Delete(context.Background(), "x") }}, - {"MarkRun", func() error { return s.MarkRun(context.Background(), "x", time.Now()) }}, - {"Due", func() error { _, err := s.Due(context.Background(), time.Now()); return err }}, - } - // Close itself is documented as idempotent (returns nil on the - // second call), so it does NOT appear in the ErrClosed list above. - if err := s.Close(); err != nil { - t.Errorf("second Close = %v, want nil", err) - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - err := tc.fn() - if !errors.Is(err, ErrClosed) { - t.Errorf("%s after Close = %v, want ErrClosed", tc.name, err) - } - }) - } -} - -// --- P8: same-path stores share the in-process mutex ------------------ - -func TestTwoStoresForSamePathSerialiseInProcess(t *testing.T) { - // This is the test that fails without process_lock.go. With it, - // 50/50 writes from each side land; without it, half the data - // disappears. Mirrors TestConcurrentWritersAreSerialised but in - // its own right so the regression is named. - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - a, _ := Open(path) - defer a.Close() - b, _ := Open(path) - defer b.Close() - - var wg sync.WaitGroup - for i := range 25 { - wg.Add(2) - go func(i int) { - defer wg.Done() - _ = a.Create(context.Background(), fixedIntervalSpec("a"+itoa(i), time.Minute)) - }(i) - go func(i int) { - defer wg.Done() - _ = b.Create(context.Background(), fixedIntervalSpec("b"+itoa(i), time.Minute)) - }(i) - } - wg.Wait() - - jobs, _ := a.List(context.Background()) - if len(jobs) != 50 { - t.Errorf("after 50 concurrent writes from two stores, %d persisted, want 50", len(jobs)) - } -} diff --git a/internal/infrastructure/cronstore/store_test.go b/internal/infrastructure/cronstore/store_test.go deleted file mode 100644 index ec3694e34..000000000 --- a/internal/infrastructure/cronstore/store_test.go +++ /dev/null @@ -1,512 +0,0 @@ -// Package cronstore is the persistent job store backing the scheduling -// ticker engine. Tests in this file are the spec: an implementing agent -// must make every test in this file pass WITHOUT modifying the tests -// themselves. -// -// The store's only contract with the engine is scheduling.JobSource: one -// file-backed JSON document, cross-process locked, with version-stamped -// schema so future slices can extend it without breaking old files. -package cronstore - -import ( - "context" - "errors" - "os" - "path/filepath" - "strconv" - "strings" - "sync" - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/domain/scheduling" -) - -// helpers - -func newTestStore(t *testing.T) *Store { - t.Helper() - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - s, err := Open(path) - if err != nil { - t.Fatalf("Open(%s): %v", path, err) - } - t.Cleanup(func() { _ = s.Close() }) - return s -} - -func mustCreate(t *testing.T, s *Store, spec JobSpec) { - t.Helper() - if err := s.Create(context.Background(), spec); err != nil { - t.Fatalf("Create(%q): %v", spec.ID, err) - } -} - -func fixedIntervalSpec(id string, every time.Duration) JobSpec { - return JobSpec{ - ID: id, - Pool: string(scheduling.PoolParallel), - Detail: "test job " + id, - Schedule: Schedule{ - Kind: ScheduleInterval, - Interval: Duration(every), - }, - Payload: Payload{Text: "hello " + id}, - } -} - -// --- open / close ------------------------------------------------------------ - -func TestOpenMissingFileReturnsEmpty(t *testing.T) { - dir := t.TempDir() - s, err := Open(filepath.Join(dir, "jobs.json")) - if err != nil { - t.Fatalf("Open: %v", err) - } - defer s.Close() - - jobs, err := s.List(context.Background()) - if err != nil { - t.Fatalf("List: %v", err) - } - if len(jobs) != 0 { - t.Errorf("List on missing file = %d jobs, want 0", len(jobs)) - } -} - -func TestOpenCreatesParentDirectory(t *testing.T) { - dir := t.TempDir() - deep := filepath.Join(dir, "a", "b", "c", "jobs.json") - s, err := Open(deep) - if err != nil { - t.Fatalf("Open with missing parent: %v", err) - } - defer s.Close() - mustCreate(t, s, fixedIntervalSpec("x", time.Minute)) - if _, err := os.Stat(deep); err != nil { - t.Fatalf("expected file to exist at %s: %v", deep, err) - } -} - -// --- schema versioning ------------------------------------------------------- - -func TestPersistedFileHasVersionStamp(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - s, err := Open(path) - if err != nil { - t.Fatalf("Open: %v", err) - } - mustCreate(t, s, fixedIntervalSpec("x", time.Minute)) - _ = s.Close() - - data, err := os.ReadFile(path) - if err != nil { - t.Fatalf("ReadFile: %v", err) - } - // Asserted against the constant, not a literal: the stamp must track - // schemaVersion through every bump (Kind was the first), and a - // hardcoded value here would fail on the bump as a false alarm - // instead of guarding the property it names. - want := `"schema_version":` + strconv.Itoa(schemaVersion) - if !strings.Contains(string(data), want) { - t.Errorf("file does not contain %s stamp: %s", want, data) - } -} - -func TestOpenRejectsUnknownSchemaVersion(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - if err := os.WriteFile(path, []byte(`{"schema_version":999,"jobs":[]}`), 0o644); err != nil { - t.Fatalf("WriteFile: %v", err) - } - if _, err := Open(path); err == nil { - t.Fatal("Open with future schema version succeeded; want error") - } else if !errors.Is(err, ErrUnsupportedSchema) { - t.Errorf("error = %v, want ErrUnsupportedSchema", err) - } -} - -// --- create / read ----------------------------------------------------------- - -func TestCreateAndGetRoundTrip(t *testing.T) { - s := newTestStore(t) - want := JobSpec{ - ID: "morning-status", - Pool: string(scheduling.PoolSequential), - Detail: "morning status", - Schedule: Schedule{ - Kind: ScheduleInterval, - Interval: Duration(30 * time.Minute), - }, - Payload: Payload{Text: "status please"}, - } - mustCreate(t, s, want) - - got, ok, err := s.Get(context.Background(), want.ID) - if err != nil { - t.Fatalf("Get: %v", err) - } - if !ok { - t.Fatal("Get returned ok=false") - } - if got.ID != want.ID { - t.Errorf("ID = %q, want %q", got.ID, want.ID) - } - if got.Pool != want.Pool { - t.Errorf("Pool = %q, want %q", got.Pool, want.Pool) - } - if got.Detail != want.Detail { - t.Errorf("Detail = %q, want %q", got.Detail, want.Detail) - } - if got.Schedule.Kind != want.Schedule.Kind { - t.Errorf("Schedule.Kind = %q, want %q", got.Schedule.Kind, want.Schedule.Kind) - } - if got.Schedule.Interval != want.Schedule.Interval { - t.Errorf("Schedule.Interval = %v, want %v", got.Schedule.Interval, want.Schedule.Interval) - } - if got.Payload.Text != want.Payload.Text { - t.Errorf("Payload.Text = %q, want %q", got.Payload.Text, want.Payload.Text) - } - if got.NextRun.IsZero() { - t.Error("NextRun is zero; Create must compute it for an interval job") - } - if got.Created.IsZero() { - t.Error("Created is zero; Create must stamp it") - } - if got.Updated.IsZero() { - t.Error("Updated is zero; Create must stamp it") - } - if !got.Created.Equal(got.Updated) { - t.Errorf("Created %v != Updated %v on a fresh create", got.Created, got.Updated) - } -} - -func TestGetMissingReturnsNotFound(t *testing.T) { - s := newTestStore(t) - _, ok, err := s.Get(context.Background(), "nope") - if err != nil { - t.Fatalf("Get: %v", err) - } - if ok { - t.Error("Get of missing id returned ok=true") - } -} - -func TestCreateRejectsEmptyID(t *testing.T) { - s := newTestStore(t) - if err := s.Create(context.Background(), fixedIntervalSpec("", time.Minute)); err == nil { - t.Fatal("Create with empty id succeeded; want error") - } -} - -func TestCreateRejectsUnknownScheduleKind(t *testing.T) { - s := newTestStore(t) - spec := fixedIntervalSpec("x", time.Minute) - spec.Schedule.Kind = "magic" - if err := s.Create(context.Background(), spec); err == nil { - t.Fatal("Create with unknown schedule kind succeeded; want error") - } -} - -func TestCreateRejectsDuplicateID(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("dup", time.Minute)) - if err := s.Create(context.Background(), fixedIntervalSpec("dup", time.Hour)); err == nil { - t.Fatal("Create with duplicate id succeeded; want error") - } -} - -// --- update with merge semantics -------------------------------------------- - -func TestUpdatePreservesFieldsNotInPatch(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, JobSpec{ - ID: "patch-me", - Pool: string(scheduling.PoolParallel), - Detail: "original detail", - Schedule: Schedule{ - Kind: ScheduleInterval, - Interval: Duration(10 * time.Minute), - }, - Payload: Payload{Text: "original payload"}, - }) - - if err := s.Update(context.Background(), "patch-me", Patch{ - Detail: new("updated detail"), - }); err != nil { - t.Fatalf("Update: %v", err) - } - - got, _, _ := s.Get(context.Background(), "patch-me") - if got.Detail != "updated detail" { - t.Errorf("Detail = %q, want %q", got.Detail, "updated detail") - } - // Untouched fields must survive. - if got.Pool != string(scheduling.PoolParallel) { - t.Errorf("Pool = %q, want %q (merge must preserve)", got.Pool, scheduling.PoolParallel) - } - if got.Schedule.Interval.Std() != 10*time.Minute { - t.Errorf("Schedule.Interval = %v, want %v (merge must preserve)", got.Schedule.Interval, 10*time.Minute) - } - if got.Payload.Text != "original payload" { - t.Errorf("Payload.Text = %q, want %q (merge must preserve)", got.Payload.Text, "original payload") - } -} - -func TestUpdateAdvancesUpdatedTimestamp(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("ts", time.Minute)) - first, _, _ := s.Get(context.Background(), "ts") - - // Sleep long enough that Updated must move forward even on coarse - // clocks. mtime resolution on some filesystems is the second. - time.Sleep(1100 * time.Millisecond) - - if err := s.Update(context.Background(), "ts", Patch{Detail: new("new")}); err != nil { - t.Fatalf("Update: %v", err) - } - got, _, _ := s.Get(context.Background(), "ts") - if !got.Updated.After(first.Updated) { - t.Errorf("Updated did not advance: first=%v got=%v", first.Updated, got.Updated) - } -} - -func TestUpdateRecomputesNextRunWhenScheduleChanges(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("rs", time.Hour)) - first, _, _ := s.Get(context.Background(), "rs") - - // Change the interval; NextRun must move. - if err := s.Update(context.Background(), "rs", Patch{ - Schedule: &Schedule{Kind: ScheduleInterval, Interval: Duration(2 * time.Minute)}, - }); err != nil { - t.Fatalf("Update: %v", err) - } - got, _, _ := s.Get(context.Background(), "rs") - if got.Schedule.Interval.Std() != 2*time.Minute { - t.Errorf("Schedule.Interval = %v, want %v", got.Schedule.Interval, 2*time.Minute) - } - if !got.NextRun.Before(first.NextRun) { - t.Errorf("NextRun did not move earlier after shortening interval: first=%v got=%v", first.NextRun, got.NextRun) - } -} - -func TestUpdateMissingReturnsNotFound(t *testing.T) { - s := newTestStore(t) - if err := s.Update(context.Background(), "ghost", Patch{Detail: new("x")}); err == nil { - t.Fatal("Update of missing id succeeded; want error") - } -} - -// --- delete ------------------------------------------------------------------ - -func TestDeleteRemovesJob(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("bye", time.Minute)) - if err := s.Delete(context.Background(), "bye"); err != nil { - t.Fatalf("Delete: %v", err) - } - if _, ok, _ := s.Get(context.Background(), "bye"); ok { - t.Error("Get after Delete returned ok=true") - } -} - -func TestDeleteMissingIsIdempotent(t *testing.T) { - s := newTestStore(t) - // First delete is a no-op on a missing id; second delete must not panic - // and must not return an error. CLI retries should be safe. - if err := s.Delete(context.Background(), "never-existed"); err != nil { - t.Errorf("Delete(missing) = %v, want nil", err) - } - if err := s.Delete(context.Background(), "never-existed"); err != nil { - t.Errorf("Delete(missing) again = %v, want nil", err) - } -} - -// --- list -------------------------------------------------------------------- - -func TestListReturnsAllJobs(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("a", time.Minute)) - mustCreate(t, s, fixedIntervalSpec("b", time.Minute)) - mustCreate(t, s, fixedIntervalSpec("c", time.Minute)) - - jobs, err := s.List(context.Background()) - if err != nil { - t.Fatalf("List: %v", err) - } - if len(jobs) != 3 { - t.Errorf("List returned %d jobs, want 3", len(jobs)) - } -} - -// --- JobSource.Due ----------------------------------------------------------- - -func TestDueReturnsOnlyJobsWhoseNextRunIsInThePast(t *testing.T) { - s := newTestStore(t) - now := time.Date(2026, 8, 28, 12, 0, 0, 0, time.UTC) - - // past: due - past := fixedIntervalSpec("past", 30*time.Minute) - past.NextRun = now.Add(-time.Minute) - // future: not due - future := fixedIntervalSpec("future", 30*time.Minute) - future.NextRun = now.Add(time.Hour) - mustCreate(t, s, past) - mustCreate(t, s, future) - - got, err := s.Due(context.Background(), now) - if err != nil { - t.Fatalf("Due: %v", err) - } - if len(got) != 1 { - t.Fatalf("Due returned %d jobs, want 1: %+v", len(got), got) - } - if got[0].ID != "past" { - t.Errorf("due job = %q, want %q", got[0].ID, "past") - } - if got[0].Pool != scheduling.PoolParallel { - t.Errorf("due job pool = %q, want %q", got[0].Pool, scheduling.PoolParallel) - } - if got[0].Detail != "test job past" { - t.Errorf("due job detail = %q, want %q", got[0].Detail, "test job past") - } -} - -func TestDueOnEmptyStoreReturnsEmptySlice(t *testing.T) { - s := newTestStore(t) - got, err := s.Due(context.Background(), time.Now()) - if err != nil { - t.Fatalf("Due: %v", err) - } - if len(got) != 0 { - t.Errorf("Due on empty = %d jobs, want 0", len(got)) - } -} - -func TestImplementsSchedulingJobSource(t *testing.T) { - // Static, compile-time check: *Store must satisfy scheduling.JobSource - // so the ticker engine can poll it without knowing the file layout. - var _ scheduling.JobSource = (*Store)(nil) -} - -func TestMarkRunAdvancesNextRun(t *testing.T) { - s := newTestStore(t) - mustCreate(t, s, fixedIntervalSpec("recurring", time.Hour)) - - first, _, _ := s.Get(context.Background(), "recurring") - runAt := first.NextRun.Add(time.Minute) // pretend engine ran a bit late - if err := s.MarkRun(context.Background(), "recurring", runAt); err != nil { - t.Fatalf("MarkRun: %v", err) - } - got, _, _ := s.Get(context.Background(), "recurring") - if got.LastRun == nil { - t.Fatal("LastRun is nil after MarkRun") - } - if !got.LastRun.Equal(runAt) { - t.Errorf("LastRun = %v, want %v", got.LastRun, runAt) - } - wantNext := runAt.Add(time.Hour) - if !got.NextRun.Equal(wantNext) { - t.Errorf("NextRun = %v, want %v (last_run + interval)", got.NextRun, wantNext) - } -} - -func TestMarkRunMissingReturnsNotFound(t *testing.T) { - s := newTestStore(t) - if err := s.MarkRun(context.Background(), "ghost", time.Now()); err == nil { - t.Fatal("MarkRun of missing id succeeded; want error") - } -} - -// --- partial-write handling -------------------------------------------------- - -func TestOpenTruncatedFileReturnsExplicitError(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - // Header present, body cut mid-array. Must not panic on read; the - // engine surfaces this as phase="source" KindJobError, not a crash. - if err := os.WriteFile(path, []byte(`{"schema_version":1,"jobs":[{"id":"x","`), 0o644); err != nil { - t.Fatalf("WriteFile: %v", err) - } - _, err := Open(path) - if err == nil { - t.Fatal("Open on truncated file succeeded; want explicit error") - } - if errors.Is(err, ErrUnsupportedSchema) { - t.Errorf("error = ErrUnsupportedSchema; want a corrupt-file error") - } -} - -func TestOpenGarbageFileReturnsExplicitError(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - if err := os.WriteFile(path, []byte("not json at all"), 0o644); err != nil { - t.Fatalf("WriteFile: %v", err) - } - if _, err := Open(path); err == nil { - t.Fatal("Open on garbage file succeeded; want explicit error") - } -} - -// --- cross-process locking --------------------------------------------------- - -func TestConcurrentWritersAreSerialised(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "jobs.json") - a, err := Open(path) - if err != nil { - t.Fatalf("Open a: %v", err) - } - defer a.Close() - b, err := Open(path) - if err != nil { - t.Fatalf("Open b: %v", err) - } - defer b.Close() - - // 50 writes from each writer, on the same file, interleaved. Without - // the lock, two writers would race and the file would end up either - // truncated or holding a non-deterministic subset. The lock makes - // each writer wait, so all 100 distinct ids end up persisted. - var wg sync.WaitGroup - for i := range 50 { - wg.Add(2) - go func(i int) { - defer wg.Done() - id := "a" + itoa(i) - _ = a.Create(context.Background(), fixedIntervalSpec(id, time.Minute)) - }(i) - go func(i int) { - defer wg.Done() - id := "b" + itoa(i) - _ = b.Create(context.Background(), fixedIntervalSpec(id, time.Minute)) - }(i) - } - wg.Wait() - - jobs, err := a.List(context.Background()) - if err != nil { - t.Fatalf("List: %v", err) - } - if len(jobs) != 100 { - t.Errorf("after 100 concurrent writes, %d jobs persisted, want 100", len(jobs)) - } -} - -// itoa avoids strconv import in tests. -func itoa(n int) string { - if n == 0 { - return "0" - } - var buf [20]byte - i := len(buf) - for n > 0 { - i-- - buf[i] = byte('0' + n%10) - n /= 10 - } - return string(buf[i:]) -}