feat(audit): set per-kind event retention from the instance (#1856)
deploy / plan (push) Successful in 35s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 25s
ci/woodpecker/push/deploy Pipeline was canceled

The retention sweep applied one window to a code-fixed allowlist, and the
largest event kind (agent transcripts) was outside it, so the events table
grew whatever the instance set. The audit-retention document now takes
per-kind overrides, the dashboard offers each prunable kind its own window
from the server's list, and a kind without an override follows the default.
A security kind is refused where the document is written, and the sweep cuts
each kind at its own cutoff in one bounded, locked transaction.
This commit is contained in:
sam committed 2026-11-11 02:31:46 +11:00
1 parent 4728c8c253
commit dcc12582ce
11 files changed
+471 -167

No files matched your search

+2
View File
@@ -18,6 +18,8 @@ release's per-component sections are labelled beneath its heading.
- Revoking an API token on the account page works.
- An organisation's model usage is reported on a new Usage page: calls, tokens and cost for the last 7, 30 or 90 days, broken down by day, model, source, and the user or agent the work was requested as. Local models with no catalogue price report tokens only.
- Audit retention can give each runtime event kind its own window, and agent transcripts are now among the kinds it prunes. A transcript window keeps old agent conversations out of the events table, and a kind with no window of its own follows the default retention.
### archie-gateway
- Archie's chat replies lead with the answer, keep long evidence last, and treat a pasted error as a request to diagnose it. Archie checks its own earlier actions before describing them, and treats the managed repositories as the source of truth over any local checkout or mirror.
+8 -100
View File
@@ -3,11 +3,10 @@ package statestore
import (
"context"
"errors"
"slices"
"time"
"github.com/samcharles93/archie-core/internal/domain/audit"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
)
@@ -17,7 +16,7 @@ const auditRetentionSweepInterval = time.Hour
// auditEventPruner is the store's bounded, lock-guarded event delete.
type auditEventPruner interface {
PruneAuditEvents(ctx context.Context, kinds []string, cutoff time.Time) (int64, error)
PruneAuditEvents(ctx context.Context, cutoffs map[string]time.Time) (int64, error)
}
// auditRetentionResources reads the instance's retention document.
@@ -25,97 +24,6 @@ type auditRetentionResources interface {
Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error)
}
// 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. Kinds
// a product view reads are deliberately absent.
func observabilityEventKinds() []string {
return []string{
events.KindStageStart,
events.KindAgentFinish,
events.KindToolCall,
events.KindLog,
events.KindTurnCompleted,
events.KindConfigCaptured,
events.KindCuratorRun,
events.KindCuratorAction,
events.KindCuratorError,
events.KindJobRun,
events.KindJobError,
}
}
// 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,
events.KindOutcome,
events.KindChatOutcome,
events.KindPRMerged,
events.KindPRRejected,
events.KindPRClosed,
events.KindPRMergeRequested,
events.KindTaskDead,
events.KindOperatorApproved,
events.KindOperatorRejected,
events.KindAgentApproved,
events.KindAgentRejected,
events.KindTaskApproved,
events.KindTaskRejected,
events.KindOperatorRereviewed,
events.KindAgentRereviewed,
events.KindTaskRereviewed,
events.KindTaskRetried,
events.KindTaskCancelled,
events.KindTaskStopped,
events.KindTaskAbandoned,
events.KindRemediationRound,
events.KindReviewApproved,
events.KindStepRetried,
events.KindTaskWithdrawn,
events.KindTaskArchiveRequested,
events.KindWorkRequestSubmitted,
events.KindAlert,
events.KindBindingChanged,
events.KindMappingChanged,
events.KindBindingStarted,
events.KindWorkflowCallStarted,
events.KindIdentityStarted,
events.KindWorkflowCallFinished,
events.KindUpdateReport,
// Written by the daemon's binding dispatch as a bare literal.
"binding_dispatch_failure",
}
}
// prunableEventKinds is the allowlist with the protected set subtracted. The
// two are disjoint; the subtraction is the fail-safe, so a kind accidentally
// added to both is kept rather than deleted.
func prunableEventKinds() []string {
protected := protectedEventKinds()
return slices.DeleteFunc(slices.Clone(observabilityEventKinds()), func(kind string) bool {
return slices.Contains(protected, kind)
})
}
// prunableEventKind reports whether kind is on the sweep's allowlist.
func prunableEventKind(kind string) bool {
return slices.Contains(prunableEventKinds(), kind)
}
// sweepAuditRetention applies the audit-retention setting until ctx ends. It
// reads the resource on every tick, so an edit reaches the next sweep without
// a restart, and an unreadable or invalid document skips the tick rather than
@@ -145,23 +53,23 @@ func (b *server) pruneAuditEvents(ctx context.Context) {
}
return
}
retention, err := controlplanestore.RuntimeEventRetention(resource.Value)
policy, err := controlplanestore.DecodeRetentionPolicy(resource.Value)
if err != nil {
if ctx.Err() == nil {
b.log.WarnContext(ctx, "audit retention not applied: setting invalid, sweep skipped", "err", err)
}
return
}
if retention == 0 {
// Keep forever: there is no cutoff to apply.
cutoffs := policy.Cutoffs(time.Now(), audit.PrunableEventKinds())
if len(cutoffs) == 0 {
// Every prunable kind is kept forever: there is no cutoff to apply.
return
}
pruner, ok := b.st.(auditEventPruner)
if !ok {
return
}
cutoff := time.Now().Add(-retention)
rows, err := pruner.PruneAuditEvents(ctx, prunableEventKinds(), cutoff)
rows, err := pruner.PruneAuditEvents(ctx, cutoffs)
switch {
case errors.Is(err, postgres.ErrAuditPruneBusy):
b.log.DebugContext(ctx, "audit retention sweep skipped: another sweep holds the lock")
@@ -170,6 +78,6 @@ func (b *server) pruneAuditEvents(ctx context.Context) {
b.log.WarnContext(ctx, "prune audit events", "err", err)
}
default:
b.log.InfoContext(ctx, "pruned audit observability events", "rows", rows, "cutoff", cutoff.UTC())
b.log.InfoContext(ctx, "pruned audit observability events", "rows", rows, "kinds", len(cutoffs))
}
}
+106
View File
@@ -0,0 +1,106 @@
package audit
import (
"slices"
"github.com/samcharles93/archie-core/internal/events"
)
// The audit retention policy: which runtime event kinds the sweep may delete,
// and which it must keep no matter what an instance setting says. The lists are
// code, not settings — a stored document chooses a window for a prunable kind
// but can never widen this boundary, and a security record is never deleted.
// observabilityEventKinds is the high-volume observability trail the sweep may
// delete. agent_transcript leads because it is the largest payload by far; the
// task-attempt view shows transcripts while they are kept and still works when
// they are gone.
func observabilityEventKinds() []string {
return []string{
events.KindAgentTranscript,
events.KindStageStart,
events.KindAgentFinish,
events.KindToolCall,
events.KindLog,
events.KindTurnCompleted,
events.KindConfigCaptured,
events.KindCuratorRun,
events.KindCuratorAction,
events.KindCuratorError,
events.KindJobRun,
events.KindJobError,
}
}
// 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,
events.KindOutcome,
events.KindChatOutcome,
events.KindPRMerged,
events.KindPRRejected,
events.KindPRClosed,
events.KindPRMergeRequested,
events.KindTaskDead,
events.KindOperatorApproved,
events.KindOperatorRejected,
events.KindAgentApproved,
events.KindAgentRejected,
events.KindTaskApproved,
events.KindTaskRejected,
events.KindOperatorRereviewed,
events.KindAgentRereviewed,
events.KindTaskRereviewed,
events.KindTaskRetried,
events.KindTaskCancelled,
events.KindTaskStopped,
events.KindTaskAbandoned,
events.KindRemediationRound,
events.KindReviewApproved,
events.KindStepRetried,
events.KindTaskWithdrawn,
events.KindTaskArchiveRequested,
events.KindWorkRequestSubmitted,
events.KindAlert,
events.KindBindingChanged,
events.KindMappingChanged,
events.KindBindingStarted,
events.KindWorkflowCallStarted,
events.KindIdentityStarted,
events.KindWorkflowCallFinished,
events.KindUpdateReport,
// Written by the daemon's binding dispatch as a bare literal.
"binding_dispatch_failure",
}
}
// PrunableEventKinds returns the event kinds an instance's audit retention
// setting may give a window to, in display order. The protected set is
// subtracted, so a kind accidentally listed in both is kept rather than
// deleted.
func PrunableEventKinds() []string {
protected := protectedEventKinds()
return slices.DeleteFunc(slices.Clone(observabilityEventKinds()), func(kind string) bool {
return slices.Contains(protected, kind)
})
}
// PrunableEventKind reports whether kind is one the sweep may delete. Unknown
// kinds and security records are not prunable.
func PrunableEventKind(kind string) bool {
return slices.Contains(PrunableEventKinds(), kind)
}
@@ -1,4 +1,4 @@
package statestore
package audit
import (
"slices"
@@ -13,6 +13,7 @@ func TestPrunableEventKindAllowsOnlyObservability(t *testing.T) {
want bool
}{
// The allowlist: high-volume observability only.
{events.KindAgentTranscript, true},
{events.KindStageStart, true},
{events.KindAgentFinish, true},
{events.KindToolCall, true},
@@ -81,15 +82,18 @@ func TestPrunableEventKindAllowsOnlyObservability(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.kind, func(t *testing.T) {
if got := prunableEventKind(tt.kind); got != tt.want {
t.Fatalf("prunableEventKind(%q) = %v, want %v", tt.kind, got, tt.want)
if got := PrunableEventKind(tt.kind); got != tt.want {
t.Fatalf("PrunableEventKind(%q) = %v, want %v", tt.kind, got, tt.want)
}
})
}
for _, kind := range protectedEventKinds() {
if slices.Contains(prunableEventKinds(), kind) {
if slices.Contains(PrunableEventKinds(), kind) {
t.Fatalf("protected kind %q is in the prunable allowlist", kind)
}
}
if want := events.KindAgentTranscript; !slices.Contains(PrunableEventKinds(), want) {
t.Fatalf("PrunableEventKinds() does not include %q", want)
}
}
@@ -8,6 +8,7 @@ import (
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/audit"
)
const (
@@ -22,8 +23,13 @@ const (
// auditRetention is the audit-retention document. Zero keeps the observability
// trail forever; any other value is a Go duration of at least a week.
// EventRetention overrides the default for named prunable kinds (for example
// agent_transcript); an omitted kind inherits the default. The sweep may only
// delete kinds the audit domain marks prunable, so a security record cannot be
// given a window here.
type auditRetention struct {
RuntimeEventRetention string `json:"runtime_event_retention"`
RuntimeEventRetention string `json:"runtime_event_retention"`
EventRetention map[string]string `json:"event_retention,omitempty"`
}
func auditRetentionDefinition() Definition {
@@ -38,21 +44,67 @@ func auditRetentionDefinition() Definition {
func validateAuditRetention(input []byte) error {
return validateAs(input, func(document auditRetention) error {
_, err := parseRuntimeEventRetention(document.RuntimeEventRetention)
return err
if _, err := parseRetention("runtime_event_retention", document.RuntimeEventRetention); err != nil {
return err
}
for kind, value := range document.EventRetention {
if !audit.PrunableEventKind(kind) {
return fmt.Errorf("event_retention: %q is not an event kind whose retention can be set", kind)
}
if _, err := parseRetention("event_retention."+kind, value); err != nil {
return err
}
}
return nil
})
}
// RuntimeEventRetention decodes a stored audit-retention document and returns
// how long observability events are kept. Zero means keep forever. The sweep
// uses this same parser, so a value the control plane accepted is one the
// sweep can run.
func RuntimeEventRetention(document []byte) (time.Duration, error) {
// RetentionPolicy is a decoded audit-retention document: how long observability
// events are kept by default, and the per-kind windows that replace it.
type RetentionPolicy struct {
Default time.Duration
Overrides map[string]time.Duration
}
// Cutoffs returns the delete-before instant for every kind in kinds that has a
// window; a kind kept forever is absent, and an empty result means the sweep
// has nothing to do. The sweep and the control plane share this decoder, so a
// value the control plane accepted is one the sweep can run.
func (p RetentionPolicy) Cutoffs(now time.Time, kinds []string) map[string]time.Time {
cutoffs := make(map[string]time.Time, len(kinds))
for _, kind := range kinds {
keep := p.Default
if override, ok := p.Overrides[kind]; ok {
keep = override
}
if keep > 0 {
cutoffs[kind] = now.Add(-keep)
}
}
return cutoffs
}
// DecodeRetentionPolicy decodes a stored audit-retention document.
func DecodeRetentionPolicy(document []byte) (RetentionPolicy, error) {
decoded, err := auditRetentionDocument(document)
if err != nil {
return 0, err
return RetentionPolicy{}, err
}
return parseRuntimeEventRetention(decoded.RuntimeEventRetention)
policy := RetentionPolicy{Overrides: make(map[string]time.Duration, len(decoded.EventRetention))}
if policy.Default, err = parseRetention("runtime_event_retention", decoded.RuntimeEventRetention); err != nil {
return RetentionPolicy{}, err
}
for kind, value := range decoded.EventRetention {
if !audit.PrunableEventKind(kind) {
return RetentionPolicy{}, fmt.Errorf("event_retention: %q is not an event kind whose retention can be set", kind)
}
window, err := parseRetention("event_retention."+kind, value)
if err != nil {
return RetentionPolicy{}, err
}
policy.Overrides[kind] = window
}
return policy, nil
}
func auditRetentionDocument(document []byte) (auditRetention, error) {
@@ -65,7 +117,7 @@ func auditRetentionDocument(document []byte) (auditRetention, error) {
return decoded, nil
}
// normalizeAuditRetention stores the retention in whole hours: the audit page
// normalizeAuditRetention stores every window 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) {
@@ -73,31 +125,42 @@ func normalizeAuditRetention(input []byte) ([]byte, error) {
if err != nil {
return nil, err
}
retention, err := parseRuntimeEventRetention(decoded.RuntimeEventRetention)
retention, err := parseRetention("runtime_event_retention", decoded.RuntimeEventRetention)
if err != nil {
return nil, err
}
decoded.RuntimeEventRetention = formatRuntimeEventRetention(retention)
decoded.RuntimeEventRetention = formatRetention(retention)
if len(decoded.EventRetention) > 0 {
normalized := make(map[string]string, len(decoded.EventRetention))
for kind, value := range decoded.EventRetention {
window, err := parseRetention("event_retention."+kind, value)
if err != nil {
return nil, err
}
normalized[kind] = formatRetention(window)
}
decoded.EventRetention = normalized
}
return json.Marshal(decoded)
}
func formatRuntimeEventRetention(retention time.Duration) string {
func formatRetention(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) {
func parseRetention(field, value string) (time.Duration, error) {
retention, err := time.ParseDuration(value)
if err != nil {
return 0, fmt.Errorf("runtime_event_retention must be a duration such as 2160h: %w", err)
return 0, fmt.Errorf("%s must be a duration such as 2160h: %w", field, err)
}
if retention < 0 {
return 0, fmt.Errorf("runtime_event_retention must not be negative")
return 0, fmt.Errorf("%s must not be negative", field)
}
if retention > 0 && retention < MinimumAuditRetention {
return 0, fmt.Errorf("runtime_event_retention must be at least 168h, or 0 to keep forever")
return 0, fmt.Errorf("%s must be at least 168h, or 0 to keep forever", field)
}
return retention, nil
}
@@ -0,0 +1,131 @@
package controlplanestore
import (
"encoding/json"
"testing"
"time"
)
// TestAuditRetentionPerKindWindows pins the retention document: a per-kind
// window replaces the default, zero keeps a kind forever, and a kind the audit
// policy does not prune is refused where it is written rather than silently
// ignored by the sweep.
func TestAuditRetentionPerKindWindows(t *testing.T) {
tests := []struct {
name string
document string
wantErr bool
check func(*testing.T, RetentionPolicy)
}{
{
name: "the default window applies to every prunable kind",
document: `{"runtime_event_retention":"720h"}`,
check: func(t *testing.T, policy RetentionPolicy) {
if policy.Default != 720*time.Hour {
t.Fatalf("default = %v, want 720h", policy.Default)
}
if len(policy.Overrides) != 0 {
t.Fatalf("overrides = %v, want none", policy.Overrides)
}
},
},
{
name: "a per-kind window replaces the default",
document: `{"runtime_event_retention":"720h","event_retention":{"agent_transcript":"168h"}}`,
check: func(t *testing.T, policy RetentionPolicy) {
if policy.Default != 720*time.Hour || policy.Overrides["agent_transcript"] != 168*time.Hour {
t.Fatalf("policy = %+v, want 720h default and 168h transcripts", policy)
}
},
},
{
name: "zero keeps one kind forever",
document: `{"runtime_event_retention":"720h","event_retention":{"agent_transcript":"0"}}`,
check: func(t *testing.T, policy RetentionPolicy) {
if window, ok := policy.Overrides["agent_transcript"]; !ok || window != 0 {
t.Fatalf("transcript override = %v (present %v), want 0", window, ok)
}
},
},
{
name: "a security kind is refused",
document: `{"runtime_event_retention":"720h","event_retention":{"task_queued":"168h"}}`,
wantErr: true,
},
{
name: "an unknown kind is refused",
document: `{"runtime_event_retention":"720h","event_retention":{"not_a_kind":"168h"}}`,
wantErr: true,
},
{
name: "a window under a week is refused",
document: `{"runtime_event_retention":"720h","event_retention":{"agent_transcript":"24h"}}`,
wantErr: true,
},
{
name: "a per-kind value that is not a duration is refused",
document: `{"runtime_event_retention":"720h","event_retention":{"agent_transcript":"week"}}`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
document := []byte(tt.document)
if err := validateAuditRetention(document); (err != nil) != tt.wantErr {
t.Fatalf("validateAuditRetention() error = %v, wantErr %v", err, tt.wantErr)
}
policy, err := DecodeRetentionPolicy(document)
if tt.wantErr {
if err == nil {
t.Fatalf("DecodeRetentionPolicy() error = nil, want a refusal")
}
return
}
if err != nil {
t.Fatalf("DecodeRetentionPolicy() error = %v", err)
}
tt.check(t, policy)
})
}
}
// TestAuditRetentionNormalizeKeepsWholeHours pins that every stored window is
// rewritten in whole hours, so a value written in seconds is not read as zero
// by the dashboard and silently disable pruning.
func TestAuditRetentionNormalizeKeepsWholeHours(t *testing.T) {
document := []byte(`{"runtime_event_retention":"604800s","event_retention":{"agent_transcript":"168h"}}`)
normalized, err := normalizeAuditRetention(document)
if err != nil {
t.Fatalf("normalizeAuditRetention() error = %v", err)
}
var decoded auditRetention
if err := json.Unmarshal(normalized, &decoded); err != nil {
t.Fatalf("normalized document is not JSON: %v", err)
}
if decoded.RuntimeEventRetention != "168h" {
t.Fatalf("runtime_event_retention = %q, want 168h", decoded.RuntimeEventRetention)
}
if decoded.EventRetention["agent_transcript"] != "168h" {
t.Fatalf("agent_transcript = %q, want 168h", decoded.EventRetention["agent_transcript"])
}
}
// TestRetentionPolicyCutoffs pins the sweep's rule: each kind is cut at its own
// window, a kind kept forever is absent, and no window means no delete.
func TestRetentionPolicyCutoffs(t *testing.T) {
now := time.Date(2026, 11, 11, 2, 0, 0, 0, time.UTC)
policy := RetentionPolicy{
Default: 720 * time.Hour,
Overrides: map[string]time.Duration{"agent_transcript": 168 * time.Hour},
}
cutoffs := policy.Cutoffs(now, []string{"agent_transcript", "stage_start"})
if got := cutoffs["agent_transcript"]; !got.Equal(now.Add(-168 * time.Hour)) {
t.Fatalf("agent_transcript cutoff = %v, want %v", got, now.Add(-168*time.Hour))
}
if got := cutoffs["stage_start"]; !got.Equal(now.Add(-720 * time.Hour)) {
t.Fatalf("stage_start cutoff = %v, want %v", got, now.Add(-720*time.Hour))
}
if got := (RetentionPolicy{Overrides: map[string]time.Duration{"agent_transcript": 0}}).Cutoffs(now, []string{"agent_transcript"}); len(got) != 0 {
t.Fatalf("cutoffs for a kind kept forever = %v, want none", got)
}
}
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"maps"
"slices"
"time"
"github.com/samcharles93/archie-core/internal/domain/access"
@@ -13,7 +15,8 @@ import (
const (
// auditPruneBatch is one delete's bound; auditPruneMaxBatches keeps a
// single sweep from owning the table for an unbounded time.
// single sweep from owning the table for an unbounded time. The budget is
// shared across every kind the sweep prunes.
auditPruneBatch = 10_000
auditPruneMaxBatches = 100
@@ -27,18 +30,20 @@ var ErrAuditPruneBusy = errors.New("postgres: audit prune already running")
// auditPruneRecord is the value stored on the sweep's audit row.
type auditPruneRecord struct {
Cutoff time.Time `json:"cutoff"`
Rows int64 `json:"rows"`
// Rules is the delete-before instant each pruned kind was cut at, so the
// audit row says what the setting asked for, not just how much went.
Rules map[string]time.Time `json:"rules"`
Rows int64 `json:"rows"`
}
// PruneAuditEvents deletes events of the given kinds older than cutoff in
// bounded batches and appends one sys_audit row recording the cutoff and the
// total removed, only when it is non-zero. The whole sweep is one
// transaction under the audit prune lock, so the lock covers every batch and a
// concurrent sweep gets ErrAuditPruneBusy without touching a row. The kind
// list is the caller's allowlist: this method deletes nothing else.
func (s *Store) PruneAuditEvents(ctx context.Context, kinds []string, cutoff time.Time) (int64, error) {
if len(kinds) == 0 {
// PruneAuditEvents deletes events of the given kinds older than each kind's
// cutoff in bounded batches and appends one sys_audit row recording the rules
// applied and the total removed, only when it is non-zero. The whole sweep is
// one transaction under the audit prune lock, so the lock covers every batch
// and a concurrent sweep gets ErrAuditPruneBusy without touching a row. The
// kinds are the caller's policy: this method deletes nothing else.
func (s *Store) PruneAuditEvents(ctx context.Context, cutoffs map[string]time.Time) (int64, error) {
if len(cutoffs) == 0 {
return 0, nil
}
tx, err := s.pool.Begin(ctx)
@@ -56,24 +61,33 @@ func (s *Store) PruneAuditEvents(ctx context.Context, kinds []string, cutoff tim
return 0, ErrAuditPruneBusy
}
var total int64
for range auditPruneMaxBatches {
removed, err := q.PruneAuditEventBatch(ctx, postgresdb.PruneAuditEventBatchParams{
Kinds: kinds, Cutoff: cutoff.UTC(), Batch: auditPruneBatch,
})
if err != nil {
return 0, fmt.Errorf("prune audit events: %w", err)
}
total += removed
if removed < auditPruneBatch {
break
budget := auditPruneMaxBatches
for _, kind := range slices.Sorted(maps.Keys(cutoffs)) {
cutoff := cutoffs[kind].UTC()
for budget > 0 {
removed, err := q.PruneAuditEventBatch(ctx, postgresdb.PruneAuditEventBatchParams{
Kinds: []string{kind}, Cutoff: cutoff, Batch: auditPruneBatch,
})
if err != nil {
return 0, fmt.Errorf("prune audit events: %w", err)
}
total += removed
budget--
if removed < auditPruneBatch {
break
}
}
}
if total > 0 {
rules := make(map[string]time.Time, len(cutoffs))
for kind, cutoff := range cutoffs {
rules[kind] = cutoff.UTC()
}
// The sweep is not an RPC, so it attributes its own row: the State
// Store pruned the events and the retention setting decided how many.
ctx = access.WithActor(access.WithSource(ctx, auditRetentionSource), auditRetentionActor)
if err := insertAuditValues(ctx, q, "audit_retention", "runtime_events", "pruned", nil,
auditPruneRecord{Cutoff: cutoff.UTC(), Rows: total}, 0, storecontract.DefaultOrgID); err != nil {
auditPruneRecord{Rules: rules, Rows: total}, 0, storecontract.DefaultOrgID); err != nil {
return 0, err
}
}
+10 -5
View File
@@ -108,6 +108,10 @@ type auditFacetsView struct {
Outcomes []string `json:"outcomes"`
TargetKinds []string `json:"target_kinds"`
SettingsKinds []auditSettingsKind `json:"settings_kinds"`
// RetentionKinds is the runtime event kinds the instance's audit
// retention setting can give a window to; the dashboard offers exactly
// these in its retention editor.
RetentionKinds []string `json:"retention_kinds"`
}
type auditResponse struct {
@@ -184,11 +188,12 @@ func (a *auditAPI) handleAudit(w http.ResponseWriter, r *http.Request) {
response := auditResponse{Records: records, NextCursor: page.NextCursor, HasMore: page.HasMore}
if cursor.IsZero() {
response.Facets = &auditFacetsView{
Categories: audit.Categories(),
Severities: audit.Severities(),
Outcomes: audit.Outcomes(),
TargetKinds: audit.TargetKinds(),
SettingsKinds: settings,
Categories: audit.Categories(),
Severities: audit.Severities(),
Outcomes: audit.Outcomes(),
TargetKinds: audit.TargetKinds(),
SettingsKinds: settings,
RetentionKinds: audit.PrunableEventKinds(),
}
}
writeJSON(w, response)
+85 -18
View File
@@ -321,6 +321,7 @@ async function confirmRevert(): Promise<void> {
const RETENTION_KIND = "audit-retention";
const retentionOpen = ref(false);
const retentionHours = ref("");
const retentionOverrides = ref<Record<string, string>>({});
const retentionError = ref("");
const retentionSaving = ref(false);
const retentionState = settings.stateFor(RETENTION_KIND);
@@ -329,6 +330,28 @@ const retentionState = settings.stateFor(RETENTION_KIND);
const retentionAvailable = computed(() =>
settings.catalog.some((resource) => resource.kind === RETENTION_KIND),
);
// The kinds come from the server so this list cannot drift from the policy
// the sweep enforces.
const retentionKinds = computed(() => facets.value.retention_kinds ?? []);
const RETENTION_LABELS: Record<string, string> = {
agent_transcript: "Agent transcripts",
stage_start: "Stage starts",
agent_finish: "Agent finishes",
config_captured: "Configuration captures",
tool_call: "Tool calls",
log: "Log lines",
turn_completed: "Chat turns",
curator_run: "Curator runs",
curator_action: "Curator actions",
curator_error: "Curator errors",
job_run: "Job runs",
job_error: "Job errors",
};
function retentionLabel(kind: string): string {
return RETENTION_LABELS[kind] ?? kind;
}
function hoursFrom(value: unknown): number {
if (typeof value === "number")
@@ -343,23 +366,52 @@ async function openRetention(): Promise<void> {
retentionError.value = "";
await settings.loadResource(RETENTION_KIND);
const resource = retentionState.resource;
if (resource) {
const value = resource.value as { runtime_event_retention?: unknown } | undefined;
retentionHours.value = String(hoursFrom(value?.runtime_event_retention));
if (!resource) return;
const value = resource.value as
| { runtime_event_retention?: unknown; event_retention?: Record<string, unknown> | null }
| undefined;
retentionHours.value = String(hoursFrom(value?.runtime_event_retention));
const overrides: Record<string, string> = {};
for (const [kind, hours] of Object.entries(value?.event_retention ?? {})) {
overrides[kind] = String(hoursFrom(hours));
}
retentionOverrides.value = overrides;
}
// retentionWindow validates one input and returns the stored "<n>h" form, or
// null when the operator left it blank (overrides only). It writes the error
// it finds so the caller only has to stop.
function retentionWindow(raw: string, label: string): string | null {
const text = String(raw ?? "").trim();
if (!text) return null;
const hours = Number(text);
if (!Number.isInteger(hours) || hours < 0) {
retentionError.value = `${label}: enter a whole number of hours.`;
return null;
}
if (hours > 0 && hours < 168) {
retentionError.value = `${label}: minimum 168 hours, or 0 to keep forever.`;
return null;
}
return `${hours}h`;
}
async function saveRetention(): Promise<void> {
if (retentionSaving.value) return;
const raw = String(retentionHours.value).trim();
const hours = Number(raw);
if (!raw || !Number.isInteger(hours) || hours < 0) {
retentionError.value = "Enter a whole number of hours.";
retentionError.value = "";
if (!String(retentionHours.value).trim()) {
retentionError.value = "Enter the default retention in hours.";
return;
}
if (hours > 0 && hours < 168) {
retentionError.value = "Minimum 168 hours, or 0 to keep forever.";
return;
const fallback = retentionWindow(retentionHours.value, "Default retention");
if (!fallback) return;
const overrides: Record<string, string> = {};
for (const kind of retentionKinds.value) {
const raw = retentionOverrides.value[kind];
if (raw === undefined || !String(raw).trim()) continue;
const window = retentionWindow(raw, retentionLabel(kind));
if (!window) return;
overrides[kind] = window;
}
const state = retentionState;
if (!state.resource) {
@@ -367,12 +419,11 @@ async function saveRetention(): Promise<void> {
return;
}
retentionSaving.value = true;
retentionError.value = "";
const saved = await settings.replace(
RETENTION_KIND,
{ runtime_event_retention: `${hours}h` },
state.resource.version,
);
const value: { runtime_event_retention: string; event_retention?: Record<string, string> } = {
runtime_event_retention: fallback,
};
if (Object.keys(overrides).length) value.event_retention = overrides;
const saved = await settings.replace(RETENTION_KIND, value, state.resource.version);
retentionSaving.value = false;
if (saved) retentionOpen.value = false;
else retentionError.value = state.error || "Retention could not be saved.";
@@ -499,13 +550,29 @@ async function saveRetention(): Promise<void> {
<DialogContent>
<DialogHeader>
<DialogTitle>Audit retention</DialogTitle>
<DialogDescription>Older runtime events are deleted hourly. Settings, access and identity records are kept.</DialogDescription>
<DialogDescription>Older runtime events are deleted hourly. Settings, access and identity records are kept, and each kind can keep its own window.</DialogDescription>
</DialogHeader>
<div class="grid gap-1.5">
<Label for="audit-retention-hours">Runtime event retention (hours)</Label>
<Label for="audit-retention-hours">Default runtime event retention (hours)</Label>
<Input id="audit-retention-hours" v-model="retentionHours" type="number" min="0" step="1" class="w-32" />
<p class="text-caption text-fg-subtle">0 keeps them forever. Minimum 168 hours.</p>
</div>
<div v-if="retentionKinds.length" class="grid gap-2">
<p class="text-label">Per-kind overrides</p>
<div v-for="kind in retentionKinds" :key="kind" class="flex items-center justify-between gap-2">
<Label :for="`audit-retention-${kind}`">{{ retentionLabel(kind) }}</Label>
<Input
:id="`audit-retention-${kind}`"
v-model="retentionOverrides[kind]"
type="number"
min="0"
step="1"
placeholder="inherit"
class="w-28"
/>
</div>
<p class="text-caption text-fg-subtle">Blank uses the default. Minimum 168 hours, or 0 to keep forever.</p>
</div>
<Alert v-if="retentionError || retentionState.error" variant="destructive"><AlertDescription>{{ retentionError || retentionState.error }}</AlertDescription></Alert>
<DialogFooter>
<Button variant="ghost" @click="retentionOpen = false">Cancel</Button>
+3
View File
@@ -70,6 +70,8 @@ export interface AuditFacets {
outcomes: string[];
target_kinds: string[];
settings_kinds: SettingsKind[];
/** The runtime event kinds the instance's audit retention can window. */
retention_kinds: string[];
}
export interface AuditResponse {
@@ -86,6 +88,7 @@ export function emptyFacets(): AuditFacets {
outcomes: [],
target_kinds: [],
settings_kinds: [],
retention_kinds: [],
};
}
+1
View File
@@ -33,6 +33,7 @@ function facetsFrom(next: AuditFacets | undefined, current: AuditFacets): AuditF
outcomes: pick(next.outcomes, current.outcomes),
target_kinds: pick(next.target_kinds, current.target_kinds),
settings_kinds: pick(next.settings_kinds, current.settings_kinds),
retention_kinds: pick(next.retention_kinds, current.retention_kinds),
};
}