diff --git a/internal/domain/applystatus/applystatus.go b/internal/domain/applystatus/applystatus.go index c46d77068..8e627f530 100644 --- a/internal/domain/applystatus/applystatus.go +++ b/internal/domain/applystatus/applystatus.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/storecontract" ) @@ -91,10 +92,18 @@ func New(process string, store storecontract.ApplyStatusStore, log *slog.Logger) // Report records that this process applied version of kind, or failed to. A // failure keeps the previous version. Errors are logged. +// +// Records describe the instance's own copy of a kind: they are keyed by +// process and kind and compared with the system org's stored version. A read +// made for another org (a workflow or playbook collection) would overwrite +// them with a version they are not comparable to, so it reports nothing. func (r *Reporter) Report(ctx context.Context, kind string, version int64, applyErr error) { if r == nil || !slices.Contains(Applicability()[kind], r.process) { return } + if org.OrgFromContext(ctx) != org.DefaultOrgID { + return + } status := storecontract.ApplyStatus{ Process: r.process, Kind: kind, AppliedVersion: version, ReportedAt: r.now().UTC(), } diff --git a/internal/domain/applystatus/applystatus_test.go b/internal/domain/applystatus/applystatus_test.go new file mode 100644 index 000000000..1b6f38729 --- /dev/null +++ b/internal/domain/applystatus/applystatus_test.go @@ -0,0 +1,42 @@ +package applystatus + +import ( + "context" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/storecontract" +) + +type recordingStore struct{ put []storecontract.ApplyStatus } + +func (s *recordingStore) PutApplyStatus(_ context.Context, status storecontract.ApplyStatus) error { + s.put = append(s.put, status) + return nil +} + +func (s *recordingStore) ListApplyStatus(context.Context) ([]storecontract.ApplyStatus, error) { + return s.put, nil +} + +// A read made for another org must not overwrite the instance's own record. +func TestReportKeepsToTheInstanceCopy(t *testing.T) { + tests := []struct { + name string + org org.OrgID + want int + }{ + {"no org", "", 1}, + {"system org", org.DefaultOrgID, 1}, + {"other org", "org-acme", 0}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + store := &recordingStore{} + New(Daemon, store, nil).Report(org.WithOrg(context.Background(), tt.org), "workflow-definitions", 3, nil) + if len(store.put) != tt.want { + t.Fatalf("records written = %d, want %d", len(store.put), tt.want) + } + }) + } +} diff --git a/internal/infrastructure/controlplanestore/org_models.go b/internal/infrastructure/controlplanestore/org_models.go index 585d48f6f..66500a2df 100644 --- a/internal/infrastructure/controlplanestore/org_models.go +++ b/internal/infrastructure/controlplanestore/org_models.go @@ -177,6 +177,14 @@ func (s *Server) readResource(ctx context.Context, orgID, kind string, into any) // instanceOnlyKinds are the kinds the runtime layers from the system org's // copy alone. Another org's write would be stored and never applied. +// +// Instance-wide by design (execution infrastructure, installed extensions, +// limits): ToolSettings, PluginSettings, ContainerRuntimePolicies, +// 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). var instanceOnlyKinds = map[string]bool{ ChannelSettingsKind: true, SchedulingPolicyKind: true, TaskPolicyKind: true, ToolSettingsKind: true,