audit

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package audit records, reads, and exports immutable AgentSQL audit events. Database access remains behind the store repository interfaces.

Index

Constants

View Source
const (
	ActionDiscover      = "discover"
	ActionDiscoverApply = "discover.apply"
	ActionAuditExport   = "audit.export"
	ActionB5Reconcile   = "b5.reconcile"
	ActionB5Discard     = "b5.quarantine.confirm_discard"
)

Variables

View Source
var (
	// ErrInvalidDecision indicates an audit decision outside the frozen set.
	ErrInvalidDecision = errors.New("invalid audit decision")
	// ErrExportLimit indicates an export containing more than 100000 rows.
	ErrExportLimit = errors.New("audit export exceeds 100000 rows")
)

Functions

func NewBatchError

func NewBatchError(kind BatchFailureKind, retryable bool, cause error) error

NewBatchError constructs an explicit backend outcome for the coordinator.

Types

type BatchBackend

type BatchBackend interface {
	InsertBatch(ctx context.Context, batch []model.AuditLog) ([]model.AuditLog, error)
}

BatchBackend durably commits one batch atomically. Returned rows may be in any order, but each row must carry the EventUUID from its input row.

type BatchError

type BatchError struct {
	Kind      BatchFailureKind
	Retryable bool
	Cause     error
}

BatchError is the explicit error contract between an S5b adapter and this DB-independent coordinator. Retryable is honored only for transient and global failures; deterministic failures are isolated by stable bisection.

func (*BatchError) Error

func (err *BatchError) Error() string

func (*BatchError) Unwrap

func (err *BatchError) Unwrap() error

type BatchFailureKind

type BatchFailureKind string

BatchFailureKind tells the coordinator what is safe after a failed backend call. Backends must only report rolled-back when commit is known not to have happened.

const (
	BatchFailureTransient     BatchFailureKind = "transient_rolled_back"
	BatchFailureDeterministic BatchFailureKind = "deterministic_rolled_back"
	BatchFailureGlobal        BatchFailureKind = "global"
	BatchFailureCommitUnknown BatchFailureKind = "commit_unknown"
)

type GroupCommitSink

type GroupCommitSink struct {
	// contains filtered or unexported fields
}

GroupCommitSink multiplexes synchronous Insert calls onto one flusher per physical audit chain.

func NewGroupCommitSink

func NewGroupCommitSink(
	backend BatchBackend,
	resolveChainID func(model.AuditLog) string,
	optionValues ...Option,
) *GroupCommitSink

NewGroupCommitSink constructs an already-started, lazily sharded sink.

func (*GroupCommitSink) Close

func (sink *GroupCommitSink) Close() error

Close stops admission, immediately flushes queued work, and waits for every chain flusher to leave. It never closes the backend itself.

func (*GroupCommitSink) Insert

func (sink *GroupCommitSink) Insert(ctx context.Context, log model.AuditLog) (model.AuditLog, error)

func (*GroupCommitSink) Start

func (sink *GroupCommitSink) Start() error

Start exists for lifecycle symmetry. Construction starts the sink, while this method reports configuration errors before the first Insert.

func (*GroupCommitSink) Stats

func (sink *GroupCommitSink) Stats() GroupCommitStats

Stats returns an aggregate point-in-time snapshot across all physical chains owned by this sink.

type GroupCommitStats

type GroupCommitStats struct {
	Batches                 uint64
	BatchEvents             uint64
	LastBatchSize           int64
	QueueDepth              int
	QueueBytes              int64
	OldestQueueAge          time.Duration
	TransactionTime         time.Duration
	Retries                 uint64
	Acknowledgements        uint64
	AcknowledgementTime     time.Duration
	Quarantined             uint64
	ClosingNotStarted       uint64
	ClosingRolledBack       uint64
	ClosingCommitUnknown    uint64
	AdmissionTokens         int
	Waiters                 int
	CapacityCount           int
	CapacityBytes           int64
	ActiveFlusherGoroutines int64
}

GroupCommitStats is an in-process snapshot. S5b can translate it to Prometheus without coupling this coordinator to a metrics implementation.

type Option

type Option func(*groupCommitOptions)

Option configures GroupCommitSink. Options are intentionally small and independently injectable so concurrency tests need not wait on production limits.

func SQLiteGroupCommitOptions

func SQLiteGroupCommitOptions() []Option

SQLiteGroupCommitOptions returns the smaller S5 SQLite queue limits.

func WithAttemptTimeout

func WithAttemptTimeout(value time.Duration) Option

func WithCanonicalDigest

func WithCanonicalDigest(value func(model.AuditLog) (string, error)) Option

func WithDrainTimeout

func WithDrainTimeout(value time.Duration) Option

func WithEnqueueWaitTimeout

func WithEnqueueWaitTimeout(value time.Duration) Option

func WithFlusherExitedHook

func WithFlusherExitedHook(value func(chainID string)) Option

WithFlusherExitedHook installs the ordering point after a chain flusher has completely stopped. An owner may close its backend only after Close returns (and therefore after this hook has run for every chain).

func WithLinger

func WithLinger(value time.Duration) Option

func WithMaxBatch

func WithMaxBatch(value int) Option

func WithMaxEventBytes

func WithMaxEventBytes(value int) Option

func WithMaxWaiters

func WithMaxWaiters(value int) Option

func WithQueueCapacity

func WithQueueCapacity(value int) Option

func WithQueueMaxBytes

func WithQueueMaxBytes(value int64) Option

func WithRootTimeout

func WithRootTimeout(value time.Duration) Option

type Reader

type Reader interface {
	FilteredPage(
		ctx context.Context,
		filter model.AuditFilter,
		page int,
		size int,
	) (model.AuditPage, error)
}

Reader returns immutable filtered audit pages.

type Recorder

type Recorder interface {
	Record(ctx context.Context, log model.AuditLog) (model.AuditLog, error)
}

Recorder synchronously records one validated audit event.

func NewRecorder

func NewRecorder(sink Sink) Recorder

NewRecorder returns a synchronous recorder backed by sink.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service combines synchronous recording, filtered reads, and export.

func NewService

func NewService(reader Reader, sink Sink) *Service

NewService returns an audit service backed by repository-shaped interfaces.

func (*Service) ExportJSONL

func (service *Service) ExportJSONL(
	ctx context.Context,
	filter model.AuditFilter,
	writer io.Writer,
) (rows int, err error)

ExportJSONL streams filtered audit events as one JSON object per line.

func (*Service) Page

func (service *Service) Page(
	ctx context.Context,
	filter model.AuditFilter,
	page int,
	size int,
) (model.AuditPage, error)

Page returns one filtered immutable audit page.

func (*Service) Record

func (service *Service) Record(
	ctx context.Context,
	log model.AuditLog,
) (model.AuditLog, error)

Record synchronously persists one validated audit event.

type Sink

type Sink interface {
	Insert(ctx context.Context, log model.AuditLog) (model.AuditLog, error)
}

Sink appends immutable audit events.

type SinkError

type SinkError struct {
	Code         model.DBErrorCode `json:"code"`
	Reason       string            `json:"reason"`
	RetryAfterMS int               `json:"retry_after_ms,omitempty"`
	Retryable    bool              `json:"retryable"`
	Decision     string            `json:"decision"`
}

SinkError is the stable, transport-independent failure returned by the group-commit admission and flushing paths. HTTP mapping belongs to S5b.

func (*SinkError) Error

func (err *SinkError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL