feat(scheduling): resolve the issue discovery rule per organisation

This commit is contained in:
sam committed 2026-10-27 10:38:00 +11:00
1 parent b8e080301c
commit 8150df8d81
12 files changed
+168 -35

No files matched your search

@@ -197,10 +197,21 @@ var orgSeededKinds = map[string]bool{
ForgeConnectionsKind: true, RepositoryPoliciesKind: true,
}
// orgEmptyDocuments are the kinds an org other than the system org reads as
// an unset document until it writes one: its unset fields fall back to the
// instance's, so nothing is stored for it and the instance's values are not
// copied in.
var orgEmptyDocuments = map[string]string{
SchedulingPolicyKind: `{"poll_interval":"","max_retries":0,"dispatch":{"trigger":"","ack_reaction":""}}`,
}
// resource seeds an org's workflow documents on first use, including orgs
// created after this process started. File-derived settings are never copied.
func (s *Server) resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) {
resource, err := s.store.Resource(ctx, orgID, kind)
if empty, ok := orgEmptyDocuments[kind]; ok && orgID != storecontract.DefaultOrgID && errors.Is(err, storecontract.ErrResourceNotFound) {
return storecontract.Resource{OrgID: orgID, Kind: kind, Value: []byte(empty)}, nil
}
if !errors.Is(err, storecontract.ErrResourceNotFound) || !orgSeededKinds[kind] {
// Serve the current channel schema so stored settings remain editable.
if err == nil && kind == ChannelSettingsKind {
@@ -1,11 +1,13 @@
package controlplanestore
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workintake"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
@@ -104,22 +106,21 @@ func validateRepositories(input []byte) error {
return validateAs(input, configuration.ValidateRepositories)
}
// validateScheduling applies the rules every org's document shares. The
// instance-wide fields are required of the system org and refused of every
// other (checkSchedulingWrite).
func validateScheduling(input []byte) error {
return validateAs(input, func(policy schedulingPolicy) error {
if policy.MaxRetries < 0 {
return fmt.Errorf("max_retries must not be negative")
}
// A positive interval, not a non-negative one: the file layer's rule is
// the same, and there is no defaulting behind a stored value -- a stored
// "0s" reaches cfg.PollInterval as zero and stops archied starting.
interval, err := time.ParseDuration(policy.PollInterval)
if err != nil || interval <= 0 {
return fmt.Errorf("poll_interval must be a positive duration")
if policy.PollInterval != "" {
interval, err := time.ParseDuration(policy.PollInterval)
if err != nil || interval <= 0 {
return fmt.Errorf("poll_interval must be a positive duration")
}
}
// Same reason as the repository rules above: RuntimeConfig replaces
// cfg.Dispatch from this resource, and the daemon refuses to start with a
// trigger it does not know.
if !configuration.DispatchTriggerValid(policy.Dispatch.Trigger) {
if policy.Dispatch.Trigger != "" && !configuration.DispatchTriggerValid(policy.Dispatch.Trigger) {
return fmt.Errorf("dispatch.trigger %q is not a discovery rule the daemon can poll with", policy.Dispatch.Trigger)
}
// A label-requiring trigger needs a label.
@@ -130,6 +131,48 @@ func validateScheduling(input []byte) error {
})
}
// checkSchedulingWrite separates the instance's polling from an org's
// discovery rule. The poll interval and retry cap belong to the one daemon
// (RuntimeConfig layers them from the system org), so another org's copy
// would parse and never apply; the system org's must still be complete,
// because the daemon refuses to start without them.
func checkSchedulingWrite(orgID string, value []byte) error {
var policy schedulingPolicy
if err := json.Unmarshal(value, &policy); err != nil {
return err
}
if orgID == storecontract.DefaultOrgID {
if policy.PollInterval == "" || policy.Dispatch.Trigger == "" {
return fmt.Errorf("%w: poll_interval and dispatch.trigger are required", ErrValidation)
}
return nil
}
if policy.PollInterval != "" || policy.MaxRetries != 0 {
return fmt.Errorf("%w: poll_interval and max_retries are instance settings; an org sets the dispatch rule and label only", ErrValidation)
}
return nil
}
// ApplyOrgScheduling returns cfg with an org's discovery rule over the
// instance's: a field the org leaves unset keeps the instance value, so an
// org that has written nothing polls as it always has.
func ApplyOrgScheduling(cfg config.Config, stored []byte) (config.Config, error) {
var policy schedulingPolicy
if err := json.Unmarshal(stored, &policy); err != nil {
return config.Config{}, fmt.Errorf("decode %s: %w", SchedulingPolicyKind, err)
}
if policy.Dispatch.Trigger != "" {
cfg.Dispatch.Trigger = policy.Dispatch.Trigger
}
if policy.Dispatch.AckReaction != "" {
cfg.Dispatch.AckReaction = policy.Dispatch.AckReaction
}
if policy.Label != nil {
cfg.Label = *policy.Label
}
return cfg, nil
}
// validateUpdateSettings refuses a channel the update scripts cannot select.
func validateUpdateSettings(input []byte) error {
return validateAs(input, func(doc controlplanerpc.UpdateSettings) error {
@@ -183,11 +183,10 @@ func (s *Server) readResource(ctx context.Context, orgID, kind string, into any)
// AgentProfile, WorkflowExecutionSettings, ExtensionSettings, UpdateSettings,
// AuditRetention, ScheduleSettings (the zone every cron schedule reads in).
// Pending an org owner of the channels they configure:
// ChannelSettings, SchedulingPolicy, TaskPolicy (outbound notification
// webhook).
// ChannelSettings, TaskPolicy (outbound notification webhook).
var instanceOnlyKinds = map[string]bool{
ChannelSettingsKind: true, SchedulingPolicyKind: true,
TaskPolicyKind: true, ToolSettingsKind: true,
ChannelSettingsKind: true,
TaskPolicyKind: true, ToolSettingsKind: true,
PluginSettingsKind: true, ContainerRuntimePoliciesKind: true, AgentProfileKind: true,
ExtensionSettingsKind: true, UpdateSettingsKind: true, WorkflowExecutionSettingsKind: true,
AuditRetentionKind: true, ScheduleSettingsKind: true,
@@ -210,6 +209,8 @@ func (s *Server) checkOrgWrite(ctx context.Context, orgID, kind string, value []
if orgID != storecontract.DefaultOrgID {
return s.checkOrgProviders(ctx, orgID, value)
}
case SchedulingPolicyKind:
return checkSchedulingWrite(orgID, value)
case ModelAliasesKind:
return s.checkAliasWrite(ctx, orgID, value)
case WorkflowDefinitionsKind:
@@ -0,0 +1,52 @@
package controlplanestore
import (
"errors"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
func TestSchedulingPolicyWritesStayWithinTheirOrg(t *testing.T) {
tests := []struct {
name, org, value string
refused bool
}{
{"system org writes the full policy", storecontract.DefaultOrgID, `{"poll_interval":"1m","dispatch":{"trigger":"assignee"}}`, false},
{"system org omits the poll interval", storecontract.DefaultOrgID, `{"dispatch":{"trigger":"assignee"}}`, true},
{"another org sets its dispatch rule", "acme", `{"label":"go","dispatch":{"trigger":"label"}}`, false},
{"another org sets the instance poll interval", "acme", `{"poll_interval":"1s","dispatch":{"trigger":"label"},"label":"go"}`, true},
{"another org sets the instance retry cap", "acme", `{"max_retries":99}`, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := (&Server{}).checkOrgWrite(t.Context(), tt.org, SchedulingPolicyKind, []byte(tt.value))
if refused := errors.Is(err, ErrValidation); refused != tt.refused {
t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err)
}
})
}
}
func TestApplyOrgSchedulingKeepsTheInstanceRuleForUnsetFields(t *testing.T) {
instance := config.Config{Label: "archie", Dispatch: config.Dispatch{Trigger: "assignee", AckReaction: "eyes"}}
tests := []struct {
name, stored string
want config.Config
}{
{"org overrides trigger and label", `{"label":"go","dispatch":{"trigger":"label"}}`, config.Config{Label: "go", Dispatch: config.Dispatch{Trigger: "label", AckReaction: "eyes"}}},
{"org leaves everything unset", `{}`, instance},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ApplyOrgScheduling(instance, []byte(tt.stored))
if err != nil {
t.Fatal(err)
}
if got.Label != tt.want.Label || got.Dispatch != tt.want.Dispatch {
t.Fatalf("got %+v/%q, want %+v/%q", got.Dispatch, got.Label, tt.want.Dispatch, tt.want.Label)
}
})
}
}