storage

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package storage provides database storage interfaces and implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DatabaseInfo

type DatabaseInfo struct {
	Path         string
	SizeBytes    int64
	EventCount   int
	SessionCount int
	OldestEvent  time.Time
	NewestEvent  time.Time
}

DatabaseInfo contains information about the database.

type EventStore

type EventStore interface {
	// SaveEvent persists a new audit event.
	SaveEvent(ctx context.Context, event *events.Event) error

	// GetEvent retrieves an event by ID.
	GetEvent(ctx context.Context, id uuid.UUID) (*events.Event, error)

	// GetEventByPrefix retrieves an event by ID prefix.
	GetEventByPrefix(ctx context.Context, prefix string) (*events.Event, error)

	// QueryEvents retrieves events matching the given filter.
	QueryEvents(ctx context.Context, filter *events.EventFilter) ([]*events.Event, error)

	// CountEvents returns the count of events matching the given filter.
	CountEvents(ctx context.Context, filter *events.EventFilter) (int, error)

	// GetEventsBySession retrieves all events for a session.
	GetEventsBySession(ctx context.Context, sessionID uuid.UUID) ([]*events.Event, error)

	// DeleteEventsBefore deletes events older than the given time.
	DeleteEventsBefore(ctx context.Context, before time.Time) (int, error)

	// CountEventsBefore returns the count of events older than the given time.
	CountEventsBefore(ctx context.Context, before time.Time) (int, error)

	// QueryEventsAfter retrieves events after the given time, ordered ascending.
	// When afterID is non-nil, a compound cursor (timestamp, id) is used so that
	// events sharing the same timestamp as after are only included when their ID
	// is greater than afterID. This prevents skipping records at batch boundaries.
	QueryEventsAfter(ctx context.Context, after time.Time, afterID uuid.UUID, limit int) ([]*events.Event, error)
}

EventStore defines the interface for storing and querying audit events.

type SQLiteStore

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

SQLiteStore implements Store using SQLite via ent.

func NewSQLiteStore

func NewSQLiteStore(path string) (*SQLiteStore, error)

NewSQLiteStore creates a new SQLite store at the given path.

func (*SQLiteStore) BackfillFTS added in v0.5.0

func (s *SQLiteStore) BackfillFTS(ctx context.Context, store EventStore) (int, error)

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close closes the database connection.

func (*SQLiteStore) CountEvents

func (s *SQLiteStore) CountEvents(ctx context.Context, filter *events.EventFilter) (int, error)

CountEvents returns the count of events matching the given filter.

func (*SQLiteStore) CountEventsBefore

func (s *SQLiteStore) CountEventsBefore(ctx context.Context, before time.Time) (int, error)

CountEventsBefore returns the count of events older than the given time.

func (*SQLiteStore) DB added in v0.5.0

func (s *SQLiteStore) DB() *sql.DB

DB returns the underlying database connection.

func (*SQLiteStore) DeleteEventsBefore

func (s *SQLiteStore) DeleteEventsBefore(ctx context.Context, before time.Time) (int, error)

DeleteEventsBefore deletes events older than the given time.

func (*SQLiteStore) DistinctAgents added in v0.5.0

func (s *SQLiteStore) DistinctAgents(ctx context.Context) ([]string, error)

func (*SQLiteStore) GetActiveSession

func (s *SQLiteStore) GetActiveSession(ctx context.Context, agentName string) (*session.Session, error)

GetActiveSession retrieves the active session for an agent, if any.

func (*SQLiteStore) GetAuditCursor added in v0.3.2

func (s *SQLiteStore) GetAuditCursor(ctx context.Context, targetName string) (*StreamCursor, error)

GetAuditCursor retrieves the audit stream cursor for a target.

func (*SQLiteStore) GetDatabaseInfo

func (s *SQLiteStore) GetDatabaseInfo(ctx context.Context) (*DatabaseInfo, error)

GetDatabaseInfo returns information about the database.

func (*SQLiteStore) GetEvent

func (s *SQLiteStore) GetEvent(ctx context.Context, id uuid.UUID) (*events.Event, error)

GetEvent retrieves an event by ID.

func (*SQLiteStore) GetEventByPrefix

func (s *SQLiteStore) GetEventByPrefix(ctx context.Context, prefix string) (*events.Event, error)

GetEventByPrefix retrieves an event by ID prefix.

func (*SQLiteStore) GetEventCursor added in v0.3.2

func (s *SQLiteStore) GetEventCursor(ctx context.Context, targetName string) (*StreamCursor, error)

GetEventCursor retrieves the event stream cursor for a target.

func (*SQLiteStore) GetEventsBySession

func (s *SQLiteStore) GetEventsBySession(ctx context.Context, sessionID uuid.UUID) ([]*events.Event, error)

GetEventsBySession retrieves all events for a session.

func (*SQLiteStore) GetSession

func (s *SQLiteStore) GetSession(ctx context.Context, id uuid.UUID) (*session.Session, error)

GetSession retrieves a session by ID.

func (*SQLiteStore) GetSessionByPrefix

func (s *SQLiteStore) GetSessionByPrefix(ctx context.Context, prefix string) (*session.Session, error)

GetSessionByPrefix retrieves a session by ID prefix.

func (*SQLiteStore) GetSessionStats

func (s *SQLiteStore) GetSessionStats(ctx context.Context) (*session.SessionStats, error)

GetSessionStats retrieves aggregated session statistics.

func (*SQLiteStore) HasSearch added in v0.5.0

func (s *SQLiteStore) HasSearch() bool

func (*SQLiteStore) Init

func (s *SQLiteStore) Init(ctx context.Context) error

Init initializes the database schema.

func (*SQLiteStore) InitFTS added in v0.5.0

func (s *SQLiteStore) InitFTS(ctx context.Context) error

InitFTS creates the FTS5 virtual table if it doesn't exist.

func (*SQLiteStore) QueryEvents

func (s *SQLiteStore) QueryEvents(ctx context.Context, filter *events.EventFilter) ([]*events.Event, error)

QueryEvents retrieves events matching the given filter.

func (*SQLiteStore) QueryEventsAfter

func (s *SQLiteStore) QueryEventsAfter(ctx context.Context, after time.Time, afterID uuid.UUID, limit int) ([]*events.Event, error)

QueryEventsAfter retrieves events after the given time, ordered ascending.

func (*SQLiteStore) QuerySelfAudits

func (s *SQLiteStore) QuerySelfAudits(ctx context.Context, filter *SelfAuditFilter) ([]*SelfAuditEntry, error)

QuerySelfAudits retrieves self-audit entries matching the filter.

func (*SQLiteStore) QuerySelfAuditsAfter

func (s *SQLiteStore) QuerySelfAuditsAfter(ctx context.Context, after time.Time, afterID uuid.UUID, limit int) ([]*SelfAuditEntry, error)

QuerySelfAuditsAfter retrieves self-audit entries after the given time, ordered ascending.

func (*SQLiteStore) QuerySessions

func (s *SQLiteStore) QuerySessions(ctx context.Context, filter *session.SessionFilter) ([]*session.Session, error)

QuerySessions retrieves sessions matching the given filter.

func (*SQLiteStore) SaveAuditCursor added in v0.3.2

func (s *SQLiteStore) SaveAuditCursor(ctx context.Context, cursor *StreamCursor) error

SaveAuditCursor persists an audit stream cursor using create-or-update inside a transaction to prevent TOCTOU races.

func (*SQLiteStore) SaveEvent

func (s *SQLiteStore) SaveEvent(ctx context.Context, event *events.Event) error

SaveEvent persists a new audit event.

func (*SQLiteStore) SaveEventCursor added in v0.3.2

func (s *SQLiteStore) SaveEventCursor(ctx context.Context, cursor *StreamCursor) error

SaveEventCursor persists an event stream cursor using create-or-update inside a transaction to prevent TOCTOU races.

func (*SQLiteStore) SaveSelfAudit

func (s *SQLiteStore) SaveSelfAudit(ctx context.Context, entry *SelfAuditEntry) error

SaveSelfAudit persists a self-audit entry.

func (*SQLiteStore) SaveSession

func (s *SQLiteStore) SaveSession(ctx context.Context, sess *session.Session) error

SaveSession persists a new session.

func (*SQLiteStore) SearchEvents added in v0.5.0

func (s *SQLiteStore) SearchEvents(ctx context.Context, query string, limit int) ([]SearchResult, error)

func (*SQLiteStore) UpdateSession

func (s *SQLiteStore) UpdateSession(ctx context.Context, sess *session.Session) error

UpdateSession updates an existing session.

type SearchResult added in v0.5.0

type SearchResult struct {
	EventID   uuid.UUID
	SessionID uuid.UUID
	Snippet   string
	Rank      float64
}

SearchResult holds a single FTS match.

type Searcher added in v0.5.0

type Searcher interface {
	SearchEvents(ctx context.Context, query string, limit int) ([]SearchResult, error)
	HasSearch() bool
	BackfillFTS(ctx context.Context, store EventStore) (int, error)
	DistinctAgents(ctx context.Context) ([]string, error)
}

Searcher provides full-text search and discovery capabilities. SQLite-specific; the TUI accepts this as optional via Options.

type SelfAuditEntry

type SelfAuditEntry struct {
	ID           uuid.UUID
	Timestamp    time.Time
	Action       string
	AgentName    string
	Details      map[string]interface{}
	Result       string
	ErrorMessage string
	ToolVersion  string
}

SelfAuditEntry represents a self-audit log entry for storage.

type SelfAuditFilter

type SelfAuditFilter struct {
	Since  *time.Time
	Action string
	Limit  int
}

SelfAuditFilter provides filtering for self-audit queries.

type SelfAuditStore

type SelfAuditStore interface {
	// SaveSelfAudit persists a self-audit entry.
	SaveSelfAudit(ctx context.Context, entry *SelfAuditEntry) error

	// QuerySelfAudits retrieves self-audit entries matching the filter.
	QuerySelfAudits(ctx context.Context, filter *SelfAuditFilter) ([]*SelfAuditEntry, error)

	// QuerySelfAuditsAfter retrieves self-audit entries after the given time, ordered ascending.
	// When afterID is non-nil, a compound cursor (timestamp, id) is used to avoid
	// skipping records that share the same timestamp at batch boundaries.
	QuerySelfAuditsAfter(ctx context.Context, after time.Time, afterID uuid.UUID, limit int) ([]*SelfAuditEntry, error)
}

SelfAuditStore defines the interface for storing self-audit entries.

type SessionStore

type SessionStore interface {
	// SaveSession persists a new session.
	SaveSession(ctx context.Context, sess *session.Session) error

	// UpdateSession updates an existing session.
	UpdateSession(ctx context.Context, sess *session.Session) error

	// GetSession retrieves a session by ID.
	GetSession(ctx context.Context, id uuid.UUID) (*session.Session, error)

	// GetSessionByPrefix retrieves a session by ID prefix.
	GetSessionByPrefix(ctx context.Context, prefix string) (*session.Session, error)

	// QuerySessions retrieves sessions matching the given filter.
	QuerySessions(ctx context.Context, filter *session.SessionFilter) ([]*session.Session, error)

	// GetActiveSession retrieves the active session for an agent, if any.
	GetActiveSession(ctx context.Context, agentName string) (*session.Session, error)

	// GetSessionStats retrieves aggregated session statistics.
	GetSessionStats(ctx context.Context) (*session.SessionStats, error)
}

SessionStore defines the interface for storing and querying sessions.

type Store

type Store interface {
	EventStore
	SessionStore
	SelfAuditStore
	StreamCursorStore

	// Init initializes the database schema.
	Init(ctx context.Context) error

	// Close closes the database connection.
	Close() error
}

Store combines all storage interfaces.

type StreamCursor added in v0.3.2

type StreamCursor struct {
	TargetName   string
	LastSyncedAt time.Time
	LastID       string
}

StreamCursor represents the sync cursor for a single collection (events or audits).

type StreamCursorStore added in v0.3.2

type StreamCursorStore interface {
	GetEventCursor(ctx context.Context, targetName string) (*StreamCursor, error)
	SaveEventCursor(ctx context.Context, cursor *StreamCursor) error
	GetAuditCursor(ctx context.Context, targetName string) (*StreamCursor, error)
	SaveAuditCursor(ctx context.Context, cursor *StreamCursor) error
}

StreamCursorStore defines the interface for stream sync cursors.

Directories

Path Synopsis
ent
Package ent provides generated database code using entgo.io/ent.
Package ent provides generated database code using entgo.io/ent.

Jump to

Keyboard shortcuts

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