From d1d6301fc2afeab01ec11001ffb625f3df8ebbe3 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 26 Oct 2026 00:38:51 +1100 Subject: [PATCH] feat(eda): capture schedule firings on a reserved source for bindings --- WORKFLOWS.md | 5 ++ internal/domain/source/path.go | 3 ++ internal/domain/source/schedule.go | 40 ++++++++++++++++ internal/domain/source/source.go | 2 + .../infrastructure/captureintake/receiver.go | 6 +++ .../captureintake/receiver_test.go | 42 +++++++++++++++++ .../migrations/0064_schedule_fired.sql | 24 ++++++++++ .../infrastructure/schedulecapture/firer.go | 47 +++++++++++++++++++ internal/webui/api_source.go | 2 +- 9 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 internal/domain/source/schedule.go create mode 100644 internal/infrastructure/postgres/migrations/0064_schedule_fired.sql create mode 100644 internal/infrastructure/schedulecapture/firer.go diff --git a/WORKFLOWS.md b/WORKFLOWS.md index c38bc0de9..91dc85ec7 100644 --- a/WORKFLOWS.md +++ b/WORKFLOWS.md @@ -293,6 +293,11 @@ expressions that limit which files the rule applies to. else runs `triage`, which classifies it and hands off. - **Event bindings**: a webhook source mapped onto a workflow and its inputs (Events page). +- **Schedules**: each firing is captured on the reserved `schedule` source as + a `Schedule fired` event (`schedule_id`, `name`, `scheduled_for`, + `fired_at`). A schedule trigger is a binding on that source using the + `schedule-fired` mapping, filtered to one `schedule_id`. A firing starts at + most one task per binding. - **Playbooks**: a trigger (issue kind and labels) and actions that dispatch workflows. - **Other workflows**: `workflow.call` (a child run), `workflow.handoff` (the diff --git a/internal/domain/source/path.go b/internal/domain/source/path.go index e58d97652..fd7e59029 100644 --- a/internal/domain/source/path.go +++ b/internal/domain/source/path.go @@ -36,6 +36,9 @@ func ValidatePath(path string) error { if path == "" || len(path) > MaxPathLen || path == "." || path == ".." { return ErrInvalidPath } + if path == SchedulePath { + return ErrReservedPath + } for i := range len(path) { if !unreserved(path[i]) { return ErrInvalidPath diff --git a/internal/domain/source/schedule.go b/internal/domain/source/schedule.go new file mode 100644 index 000000000..539265df6 --- /dev/null +++ b/internal/domain/source/schedule.go @@ -0,0 +1,40 @@ +package source + +import ( + "encoding/json" + "time" +) + +// SchedulePath is the source a schedule's firing is captured on. It is +// reserved: no endpoint receives on it and no operator source may take it, so +// only the scheduler writes its captures. +const SchedulePath = "schedule" + +// ScheduleFiredType and ScheduleFiredMapping name the built-in event type and +// mapping that identify and map every capture on SchedulePath. A schedule +// trigger is a binding on SchedulePath using ScheduleFiredMapping, filtered to +// one schedule_id. +const ( + ScheduleFiredType = "schedule-fired" + ScheduleFiredMapping = "schedule-fired" +) + +// ScheduleFired is one firing of a schedule: the payload of its capture. +type ScheduleFired struct { + ScheduleID string `json:"schedule_id"` + Name string `json:"name"` + ScheduledFor time.Time `json:"scheduled_for"` + FiredAt time.Time `json:"fired_at"` +} + +// Delivery names the firing of one slot. A retried firing of the slot shares +// it, so a binding starts at most one task per slot. +func (f ScheduleFired) Delivery() string { + return f.ScheduleID + "@" + f.ScheduledFor.UTC().Format(time.RFC3339Nano) +} + +// Body is the capture payload. +func (f ScheduleFired) Body() ([]byte, error) { + f.ScheduledFor, f.FiredAt = f.ScheduledFor.UTC(), f.FiredAt.UTC() + return json.Marshal(f) +} diff --git a/internal/domain/source/source.go b/internal/domain/source/source.go index 1c93e02c2..ea94f8bed 100644 --- a/internal/domain/source/source.go +++ b/internal/domain/source/source.go @@ -31,6 +31,8 @@ var ( ErrInvalidPath = errors.New("source: path must be 1-128 URL-safe characters (A-Z a-z 0-9 - . _ ~), not a dot segment") // ErrInvalidHeader refuses a delivery header that is not an HTTP field name. ErrInvalidHeader = errors.New("source: delivery header must be an HTTP header name") + // ErrReservedPath refuses a path the scheduler captures on. + ErrReservedPath = errors.New("source: path is reserved") // ErrSigningTransition refuses an approval with no pending request. ErrSigningTransition = errors.New("source: signing transition rejected") ) diff --git a/internal/infrastructure/captureintake/receiver.go b/internal/infrastructure/captureintake/receiver.go index b9458c14a..1e72db0b6 100644 --- a/internal/infrastructure/captureintake/receiver.go +++ b/internal/infrastructure/captureintake/receiver.go @@ -80,6 +80,12 @@ func (rc *Receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } path := r.PathValue("source") + // Only the scheduler captures on the schedule source; an outside post + // there would pose as a schedule firing. + if path == source.SchedulePath { + http.NotFound(w, r) + return + } // Rate limit by remote address; the source segment is attacker-chosen. if rc.Limiter != nil && !rc.Limiter.Allow(remoteAddrHost(r)) { diff --git a/internal/infrastructure/captureintake/receiver_test.go b/internal/infrastructure/captureintake/receiver_test.go index 10bb9a179..0c68b7b22 100644 --- a/internal/infrastructure/captureintake/receiver_test.go +++ b/internal/infrastructure/captureintake/receiver_test.go @@ -1,8 +1,16 @@ package captureintake import ( + "context" + "net/http" + "net/http/httptest" "net/url" + "strings" "testing" + "time" + + "github.com/samcharles93/archie-core/internal/domain/source" + "github.com/samcharles93/archie-core/internal/domain/storecontract" ) func TestFormPayload(t *testing.T) { @@ -25,3 +33,37 @@ func TestFormPayload(t *testing.T) { } } } + +type recordingCaptures struct{ sources []string } + +func (c *recordingCaptures) InsertCapture(_ context.Context, e storecontract.CapturedEvent, _ time.Duration, _ int) (string, error) { + c.sources = append(c.sources, e.Source) + return "id", nil +} + +func (c *recordingCaptures) ListCaptures(context.Context, int) ([]storecontract.CapturedEvent, error) { + return nil, nil +} + +// An outside post must never land on the schedule source, where it would +// pose as a schedule firing. +func TestReceiverReservesScheduleSource(t *testing.T) { + tests := []struct { + path string + status int + stored bool + }{ + {source.SchedulePath, http.StatusNotFound, false}, + {"hooks", http.StatusAccepted, true}, + } + for _, tt := range tests { + captures := &recordingCaptures{} + mux := http.NewServeMux() + (&Receiver{Captures: captures}).Register(mux) + rec := httptest.NewRecorder() + mux.ServeHTTP(rec, httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/"+tt.path, strings.NewReader(`{"schedule_id":"x"}`))) + if rec.Code != tt.status || (len(captures.sources) == 1) != tt.stored { + t.Errorf("%s: status %d stored %v, want %d %v", tt.path, rec.Code, captures.sources, tt.status, tt.stored) + } + } +} diff --git a/internal/infrastructure/postgres/migrations/0064_schedule_fired.sql b/internal/infrastructure/postgres/migrations/0064_schedule_fired.sql new file mode 100644 index 000000000..36acd96a4 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0064_schedule_fired.sql @@ -0,0 +1,24 @@ +-- A schedule's firing is captured on the reserved "schedule" source. This +-- built-in event type identifies every capture there, and the built-in mapping +-- hands a binding the firing's fields, so a schedule trigger is an ordinary +-- binding on that source filtered to one schedule_id. + +-- +goose Up +INSERT INTO event_types (id, source, name, rule, schema, org_id, workspace_id) +VALUES ('schedule-fired', 'schedule', 'Schedule fired', '{"headers":[],"payload":[]}', + '{"schedule_id":"string","name":"string","scheduled_for":"string","fired_at":"string"}', + 'org-sys', 'default') +ON CONFLICT (id) DO NOTHING; + +INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id, workspace_id) +VALUES ('schedule-fired', 'Schedule fired', 'schedule', 'schedule-fired', + '[{"name":"schedule_id","path":"schedule_id","type":"string","required":true},' + '{"name":"name","path":"name","type":"string","required":false},' + '{"name":"scheduled_for","path":"scheduled_for","type":"string","required":true},' + '{"name":"fired_at","path":"fired_at","type":"string","required":true}]', + 'org-sys', 'default') +ON CONFLICT (id) DO NOTHING; + +-- +goose Down +DELETE FROM mappings WHERE id = 'schedule-fired'; +DELETE FROM event_types WHERE id = 'schedule-fired'; diff --git a/internal/infrastructure/schedulecapture/firer.go b/internal/infrastructure/schedulecapture/firer.go new file mode 100644 index 000000000..374f18891 --- /dev/null +++ b/internal/infrastructure/schedulecapture/firer.go @@ -0,0 +1,47 @@ +// Package schedulecapture records a schedule's firing as a capture on the +// reserved schedule source, where armed bindings pick it up like any other +// event. +package schedulecapture + +import ( + "context" + "errors" + "fmt" + "time" + + "github.com/samcharles93/archie-core/internal/domain/source" + "github.com/samcharles93/archie-core/internal/domain/storecontract" +) + +// Firer captures schedule firings. Retention and MaxEvents are the capture +// table's prune-on-write bounds, as config.CaptureConfig sets for webhooks. +type Firer struct { + Captures storecontract.CaptureStore + Retention time.Duration + MaxEvents int +} + +// Fire records one firing and returns its capture id. The tasks it starts are +// the binding dispatches recorded against that id. A retried firing of the +// same slot shares its delivery, so no binding starts a second task for it. +func (f Firer) Fire(ctx context.Context, fired source.ScheduleFired) (string, error) { + if f.Captures == nil { + return "", errors.New("schedulecapture: no capture store") + } + body, err := fired.Body() + if err != nil { + return "", fmt.Errorf("schedulecapture: encode firing: %w", err) + } + id, err := f.Captures.InsertCapture(ctx, storecontract.CapturedEvent{ + ReceivedAt: fired.FiredAt.UTC(), + Source: source.SchedulePath, + ContentType: "application/json", + Body: string(body), + Authenticated: true, + Delivery: fired.Delivery(), + }, f.Retention, f.MaxEvents) + if err != nil { + return "", fmt.Errorf("schedulecapture: capture firing of %q: %w", fired.ScheduleID, err) + } + return id, nil +} diff --git a/internal/webui/api_source.go b/internal/webui/api_source.go index 1d3510bc2..5b71c4d4f 100644 --- a/internal/webui/api_source.go +++ b/internal/webui/api_source.go @@ -60,7 +60,7 @@ func (s *Server) handleSourceCreate(w http.ResponseWriter, r *http.Request) { return } src, err := source.New(req.Path) - if errors.Is(err, source.ErrInvalidPath) { + if errors.Is(err, source.ErrInvalidPath) || errors.Is(err, source.ErrReservedPath) { http.Error(w, err.Error(), http.StatusBadRequest) return }