From 4250a31ff95d3078ae42420b6f5c4d528e17fb14 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 21:53:19 +1100 Subject: [PATCH] fix(state): retain product-read events and normalise audit retention --- internal/app/controlplane/audit_retention.go | 43 ++++++++++++++++--- internal/app/statestore/audit_retention.go | 22 ++++++---- .../app/statestore/audit_retention_test.go | 6 ++- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/internal/app/controlplane/audit_retention.go b/internal/app/controlplane/audit_retention.go index 8ae95aaef..2c9390694 100644 --- a/internal/app/controlplane/audit_retention.go +++ b/internal/app/controlplane/audit_retention.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "strconv" "time" "github.com/samcharles93/archie-core/internal/config" @@ -28,9 +29,10 @@ type auditRetention struct { func auditRetentionDefinition() Definition { return Definition{ Kind: AuditRetentionKind, Title: "Audit retention", ApplyMode: "live", - Document: auditRetention{}, - Seed: func(config.Config) any { return auditRetention{RuntimeEventRetention: auditRetentionSeed} }, - Validate: validateAuditRetention, + Document: auditRetention{}, + Seed: func(config.Config) any { return auditRetention{RuntimeEventRetention: auditRetentionSeed} }, + Validate: validateAuditRetention, + Normalize: normalizeAuditRetention, } } @@ -46,13 +48,44 @@ func validateAuditRetention(input []byte) error { // uses this same parser, so a value the control plane accepted is one the // sweep can run. func RuntimeEventRetention(document []byte) (time.Duration, error) { + decoded, err := auditRetentionDocument(document) + if err != nil { + return 0, err + } + return parseRuntimeEventRetention(decoded.RuntimeEventRetention) +} + +func auditRetentionDocument(document []byte) (auditRetention, error) { var decoded auditRetention reader := json.NewDecoder(bytes.NewReader(document)) reader.DisallowUnknownFields() if err := reader.Decode(&decoded); err != nil { - return 0, fmt.Errorf("decode %s: %w", AuditRetentionKind, err) + return auditRetention{}, fmt.Errorf("decode %s: %w", AuditRetentionKind, err) } - return parseRuntimeEventRetention(decoded.RuntimeEventRetention) + return decoded, nil +} + +// normalizeAuditRetention stores the retention in whole hours: the audit page +// parses "h" only, so a value such as 604800s would read as zero there and +// the next save would disable pruning. Zero stays "0". +func normalizeAuditRetention(input []byte) ([]byte, error) { + decoded, err := auditRetentionDocument(input) + if err != nil { + return nil, err + } + retention, err := parseRuntimeEventRetention(decoded.RuntimeEventRetention) + if err != nil { + return nil, err + } + decoded.RuntimeEventRetention = formatRuntimeEventRetention(retention) + return json.Marshal(decoded) +} + +func formatRuntimeEventRetention(retention time.Duration) string { + if retention == 0 { + return "0" + } + return strconv.FormatInt(int64(retention/time.Hour), 10) + "h" } func parseRuntimeEventRetention(value string) (time.Duration, error) { diff --git a/internal/app/statestore/audit_retention.go b/internal/app/statestore/audit_retention.go index fb4227a43..10b60f0a5 100644 --- a/internal/app/statestore/audit_retention.go +++ b/internal/app/statestore/audit_retention.go @@ -27,17 +27,16 @@ type auditRetentionResources interface { // observabilityEventKinds is the hard-coded allowlist of high-volume event // kinds the sweep may delete. It is code, not a setting: no stored value can -// widen it, and adding a kind here is the only way to make it prunable. +// widen it, and adding a kind here is the only way to make it prunable. Kinds +// a product view reads are deliberately absent. func observabilityEventKinds() []string { return []string{ events.KindStageStart, - events.KindStageFinish, events.KindAgentFinish, events.KindToolCall, events.KindLog, events.KindTurnCompleted, events.KindConfigCaptured, - events.KindChangesCaptured, events.KindCuratorRun, events.KindCuratorAction, events.KindCuratorError, @@ -46,13 +45,20 @@ func observabilityEventKinds() []string { } } -// protectedEventKinds are security-relevant kinds the sweep must never -// delete: approvals and rejections, retries, cancellation and abandonment, -// pull-request actions, delivery records, binding and mapping changes, alerts -// and update reports. Keeping the list here, rather than in a comment, is what -// lets the prunable predicate refuse a kind that appears in both lists. +// protectedEventKinds are the kinds the sweep must never delete: security +// relevant records (approvals and rejections, retries, cancellation and +// abandonment, pull-request actions, delivery records, binding and mapping +// changes, alerts, update reports) and the two observability kinds a product +// read depends on. stage_finish is the only source for StageStats' slowest +// stages and most failures; changes_captured is what api_tasks_changes rebuilds +// a task's captured-changes view from. Neither has a fallback, so pruning them +// would silently change what the product shows. Keeping the list here, rather +// than in a comment, is what lets the prunable predicate refuse a kind that +// appears in both lists. func protectedEventKinds() []string { return []string{ + events.KindStageFinish, + events.KindChangesCaptured, events.KindTaskQueued, events.KindDeliveryRecorded, events.KindParked, diff --git a/internal/app/statestore/audit_retention_test.go b/internal/app/statestore/audit_retention_test.go index 75fd6e717..2207006ca 100644 --- a/internal/app/statestore/audit_retention_test.go +++ b/internal/app/statestore/audit_retention_test.go @@ -14,13 +14,11 @@ func TestPrunableEventKindAllowsOnlyObservability(t *testing.T) { }{ // The allowlist: high-volume observability only. {events.KindStageStart, true}, - {events.KindStageFinish, true}, {events.KindAgentFinish, true}, {events.KindToolCall, true}, {events.KindLog, true}, {events.KindTurnCompleted, true}, {events.KindConfigCaptured, true}, - {events.KindChangesCaptured, true}, {events.KindCuratorRun, true}, {events.KindCuratorAction, true}, {events.KindCuratorError, true}, @@ -58,6 +56,10 @@ func TestPrunableEventKindAllowsOnlyObservability(t *testing.T) { // Alerts and update reports. {events.KindAlert, false}, {events.KindUpdateReport, false}, + // Observability with a product read and no fallback: StageStats and + // the task's captured-changes view. + {events.KindStageFinish, false}, + {events.KindChangesCaptured, false}, // Other lifecycle and operator actions stay too. {events.KindTaskQueued, false}, {events.KindParked, false},