diff --git a/internal/app/controlplane/operational_settings.go b/internal/app/controlplane/operational_settings.go index e439943f8..c67a0d046 100644 --- a/internal/app/controlplane/operational_settings.go +++ b/internal/app/controlplane/operational_settings.go @@ -6,6 +6,7 @@ import ( "time" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/workintake" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" ) @@ -19,9 +20,17 @@ const ( ) type schedulingPolicy struct { - PollInterval string `json:"poll_interval"` - MaxRetries int `json:"max_retries"` - Dispatch config.Dispatch `json:"dispatch"` + PollInterval string `json:"poll_interval"` + MaxRetries int `json:"max_retries"` + // Label is the trigger-matching label the stored dispatch pairs with. + // It is a pointer, not a string, so a policy stored before the field + // existed decodes nil and leaves the file document's label in force when + // it is layered (runtime_config.go) -- the same absence-means-inherited + // shape a kind with no stored value has. Writes are stricter: the pairing + // rule below refuses a label-requiring trigger with no label, so the + // store can never bless the half of a pairing boot then refuses. + Label *string `json:"label,omitempty"` + Dispatch config.Dispatch `json:"dispatch"` } type pluginSettings struct { @@ -36,7 +45,7 @@ func operationalDefinitions() []Definition { {Kind: RepositoryPoliciesKind, Title: "Repository policies", ApplyMode: "restart-required", Schema: arraySchema, Seed: func(cfg config.Config) any { return cfg.Repos }, Validate: validateRepositories}, {Kind: ChannelSettingsKind, Title: "Channel settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: seedChannels, Validate: validateChannels}, {Kind: SchedulingPolicyKind, Title: "Scheduling policy", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any { - return schedulingPolicy{cfg.PollInterval.Std().String(), cfg.MaxRetries, cfg.Dispatch} + return schedulingPolicy{PollInterval: cfg.PollInterval.Std().String(), MaxRetries: cfg.MaxRetries, Label: &cfg.Label, Dispatch: cfg.Dispatch} }, Validate: validateScheduling}, {Kind: ToolSettingsKind, Title: "Tool and MCP settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: seedTools, Validate: validateTools}, {Kind: PluginSettingsKind, Title: "Plugin settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any { @@ -82,6 +91,16 @@ func validateScheduling(input []byte) error { if !configuration.DispatchTriggerValid(policy.Dispatch.Trigger) { return fmt.Errorf("dispatch.trigger %q is not a discovery rule the daemon can poll with", policy.Dispatch.Trigger) } + // The pairing rule the file layer's validateDispatch enforces on the + // effective document, judged here on the data the resource carries: + // RuntimeConfig layers this label over the file's (an absent field + // leaves the file's label in force, which is what legacy policies + // decode with), so a stored label-requiring trigger with no label was + // a value the store blessed and boot refused -- the two-layer + // disagreement the parity test refuses to allow (archie-core-7pyj). + if workintake.RequiresLabel(policy.Dispatch.Trigger) && (policy.Label == nil || *policy.Label == "") { + return fmt.Errorf("label is required when dispatch.trigger is %q (an empty label matches every open issue)", policy.Dispatch.Trigger) + } return nil }) } diff --git a/internal/app/controlplane/runtime_config.go b/internal/app/controlplane/runtime_config.go index e09694cf7..4c31dd116 100644 --- a/internal/app/controlplane/runtime_config.go +++ b/internal/app/controlplane/runtime_config.go @@ -137,6 +137,15 @@ func runtimeConfigFrom(ctx context.Context, reader resourceReader, base config.C return err } out.PollInterval, out.MaxRetries, out.Dispatch = config.Duration(interval), policy.MaxRetries, policy.Dispatch + // The label pairs with the trigger and layers with it: once the store + // carries one, it owns it, and the file can no longer drop the label a + // stored label-requiring trigger depends on. A policy stored before + // the field existed -- the nil the seed never produces -- leaves the + // file document's label in force, and boot's gate judges the pairing + // either way. + if policy.Label != nil { + out.Label = *policy.Label + } return nil }); err != nil { return config.Config{}, nil, err diff --git a/internal/app/controlplane/runtime_config_test.go b/internal/app/controlplane/runtime_config_test.go index ec4c4ab4f..cf5ce3f3d 100644 --- a/internal/app/controlplane/runtime_config_test.go +++ b/internal/app/controlplane/runtime_config_test.go @@ -79,3 +79,69 @@ func TestRuntimeConfigUsesDatabaseResourcesAndPreservesBootstrapOnlySecrets(t *t t.Fatalf("bootstrap-only secret-backed settings were dropped: chat=%+v tools=%+v", got.Chat, got.Tools) } } + +// TestRuntimeConfigLayersTheStoredSchedulingLabel: the label pairs with the +// dispatch trigger and layers with it. A policy that carries one replaces the +// file document's label, so the store owns the half of the pairing it judged +// at write time (archie-core-7pyj). +func TestRuntimeConfigLayersTheStoredSchedulingLabel(t *testing.T) { + base := config.Config{ + Label: "file-label", + PollInterval: config.Duration(60 * 1e9), + Dispatch: config.Dispatch{Trigger: "assignee"}, + } + client := NewRPCClient(&runtimeConfigClient{values: map[string]any{ + SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "max_retries": 7, "label": "stored-label", "dispatch": map[string]any{"trigger": "assignee"}}, + }}) + got, _, err := client.RuntimeConfig(t.Context(), base) + if err != nil { + t.Fatalf("RuntimeConfig: %v", err) + } + if got.Label != "stored-label" { + t.Fatalf("label after layering = %q, want the stored policy's label", got.Label) + } +} + +// TestRuntimeConfigLeavesTheFileLabelInForceWhenThePolicyCarriesNone: a +// policy stored before the label field existed (or one that omits it) is not +// an instruction to clear the file document's label -- the same +// absence-means-inherited shape a kind with no stored value has. +func TestRuntimeConfigLeavesTheFileLabelInForceWhenThePolicyCarriesNone(t *testing.T) { + base := config.Config{ + Label: "file-label", + PollInterval: config.Duration(60 * 1e9), + Dispatch: config.Dispatch{Trigger: "assignee"}, + } + client := NewRPCClient(&runtimeConfigClient{values: map[string]any{ + SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "max_retries": 7, "dispatch": map[string]any{"trigger": "assignee"}}, + }}) + got, _, err := client.RuntimeConfig(t.Context(), base) + if err != nil { + t.Fatalf("RuntimeConfig: %v", err) + } + if got.Label != "file-label" { + t.Fatalf("label after layering = %q, want the file document's label left in force", got.Label) + } +} + +// TestSchedulingPolicySeedCarriesTheLabel: the seed a fresh store writes must +// carry the file document's label, so the pairing a stored trigger requires +// is judgeable at write time from the moment the resource exists. +func TestSchedulingPolicySeedCarriesTheLabel(t *testing.T) { + server := testServer(t, nil) + seed, err := server.definitions[SchedulingPolicyKind].seededValue(config.Config{ + Label: "archie:labelled", + PollInterval: config.Duration(60 * 1e9), + Dispatch: config.Dispatch{Trigger: "label"}, + }) + if err != nil { + t.Fatalf("seededValue: %v", err) + } + var document map[string]any + if err := json.Unmarshal(seed, &document); err != nil { + t.Fatalf("decode seed: %v", err) + } + if label, _ := document["label"].(string); label != "archie:labelled" { + t.Fatalf("seed label = %q, want the file document's label", label) + } +} diff --git a/internal/app/controlplane/validators_agree_test.go b/internal/app/controlplane/validators_agree_test.go index f3e6f890d..4ee5f1260 100644 --- a/internal/app/controlplane/validators_agree_test.go +++ b/internal/app/controlplane/validators_agree_test.go @@ -59,6 +59,25 @@ func TestResourceValidatorsRejectWhatEffectiveValidationRejects(t *testing.T) { mutate: func(cfg *config.Config) { cfg.PollInterval = 0 }, wantErr: true, }, + { + // The pairing rule the file layer's validateDispatch enforces on the + // effective document (the GH#445 guard): a label-requiring trigger + // with no label matches every open issue. The resource carries the + // label precisely so the write path can judge the pairing, so a + // stored policy without one is refused here and by boot alike. + name: "label trigger without a label", + kind: SchedulingPolicyKind, + value: map[string]any{"poll_interval": "1m0s", "max_retries": 0, "dispatch": map[string]any{"trigger": "label"}}, + mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "label" }, + wantErr: true, + }, + { + name: "label trigger with a label", + kind: SchedulingPolicyKind, + value: map[string]any{"poll_interval": "1m0s", "max_retries": 0, "label": "archie:labelled", "dispatch": map[string]any{"trigger": "label"}}, + mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "label"; cfg.Label = "archie:labelled" }, + wantErr: false, + }, { name: "valid repository policies", kind: RepositoryPoliciesKind,