diff --git a/internal/infrastructure/cronstore/doc.go b/internal/infrastructure/cronstore/doc.go index 580453d72..d886acc7b 100644 --- a/internal/infrastructure/cronstore/doc.go +++ b/internal/infrastructure/cronstore/doc.go @@ -25,10 +25,15 @@ // including additive ones, since DisallowUnknownFields rejects them): // // { -// "schema_version": 1, +// "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 diff --git a/internal/infrastructure/cronstore/kind.go b/internal/infrastructure/cronstore/kind.go new file mode 100644 index 000000000..128a2e7cd --- /dev/null +++ b/internal/infrastructure/cronstore/kind.go @@ -0,0 +1,24 @@ +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 new file mode 100644 index 000000000..e4e4d8d06 --- /dev/null +++ b/internal/infrastructure/cronstore/kind_test.go @@ -0,0 +1,148 @@ +// 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/store.go b/internal/infrastructure/cronstore/store.go index ff08fbbb5..89df661ed 100644 --- a/internal/infrastructure/cronstore/store.go +++ b/internal/infrastructure/cronstore/store.go @@ -23,7 +23,7 @@ import ( // 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 = 1 +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 @@ -46,6 +46,14 @@ type JobSpec struct { // 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"` diff --git a/internal/infrastructure/cronstore/store_test.go b/internal/infrastructure/cronstore/store_test.go index 496ef5fbb..f3bdbf06a 100644 --- a/internal/infrastructure/cronstore/store_test.go +++ b/internal/infrastructure/cronstore/store_test.go @@ -13,6 +13,7 @@ import ( "errors" "os" "path/filepath" + "strconv" "strings" "sync" "testing" @@ -104,8 +105,13 @@ func TestPersistedFileHasVersionStamp(t *testing.T) { if err != nil { t.Fatalf("ReadFile: %v", err) } - if !strings.Contains(string(data), `"schema_version":1`) { - t.Errorf("file does not contain schema_version:1 stamp: %s", data) + // 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) } }