fix(state): retain product-read events and normalise audit retention

This commit is contained in:
sam committed 2026-10-25 21:53:19 +11:00
1 parent 59eecd956d
commit 4250a31ff9
3 files changed
+56 -15

No files matched your search

+38 -5
View File
@@ -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 "<n>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) {
+14 -8
View File
@@ -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,
@@ -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},