store

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a requested record does not exist.

Functions

This section is empty.

Types

type MetricSnapshot added in v0.1.13

type MetricSnapshot struct {
	// Pool metrics
	CollectedAt       time.Time
	TotalWorkers      int
	ActiveWorkers     int
	IdleWorkers       int
	QueueDepth        int
	TasksCompleted    int64
	TasksFailed       int64
	AvgTaskDurationMs float64
	P95TaskDurationMs float64

	// Dispatcher metrics
	DispCollectedAt      time.Time
	ClaimedCount         int
	TotalRouted          int64
	TotalRequeued        int64
	TotalEventsProcessed int64
	AvgPollLatencyMs     float64
	LastPollAt           time.Time
}

MetricSnapshot holds the latest pool and dispatcher metrics persisted by the dispatch process for cross-process consumption by the serve process.

type SQLiteStore

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

SQLiteStore implements Store using an SQLite database.

func New

func New(dbPath string) (*SQLiteStore, error)

New opens (or creates) an SQLite database at dbPath, runs migrations, and enables WAL mode and foreign keys.

func (*SQLiteStore) ClearIssueFailureCount added in v0.1.14

func (s *SQLiteStore) ClearIssueFailureCount(ctx context.Context, issueNumber int) error

ClearIssueFailureCount removes the failure count record for an issue.

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close closes the underlying database connection.

func (*SQLiteStore) ComputeCostForIssue added in v0.1.13

func (s *SQLiteStore) ComputeCostForIssue(ctx context.Context, issueNumber int) (summary *models.CostSummary, err error)

ComputeCostForIssue aggregates all cost records across every session for the given issue number. This enables per-issue budget tracking and outlier detection.

func (*SQLiteStore) ComputeCostSince

func (s *SQLiteStore) ComputeCostSince(ctx context.Context, since time.Time) (summary *models.CostSummary, err error)

ComputeCostSince aggregates all cost records created at or after the given time.

func (*SQLiteStore) CountFailuresByIssue added in v0.1.14

func (s *SQLiteStore) CountFailuresByIssue(ctx context.Context, issueNumber int) (int, error)

CountFailuresByIssue returns the total number of failure events for a given issue.

func (*SQLiteStore) CreateSession

func (s *SQLiteStore) CreateSession(ctx context.Context, sess *models.Session) error

CreateSession inserts a new session record. If s.ID is empty, a random ID is generated.

func (*SQLiteStore) GetBudgetStatus

func (s *SQLiteStore) GetBudgetStatus(ctx context.Context, dailyBudgetUSD float64) (spent, remaining float64, err error)

GetBudgetStatus computes today's spending and remaining budget. spent is the total cost_usd since midnight UTC today. remaining is dailyBudgetUSD - spent (floored at 0).

func (*SQLiteStore) GetFailureSummary added in v0.1.14

func (s *SQLiteStore) GetFailureSummary(ctx context.Context, since time.Time) (*models.FailureSummary, error)

GetFailureSummary builds an aggregated failure summary for the given period.

func (*SQLiteStore) GetIssueFailureCount added in v0.1.14

func (s *SQLiteStore) GetIssueFailureCount(ctx context.Context, issueNumber int) (int, error)

GetIssueFailureCount returns the persisted failure count for an issue. Returns 0 if no record exists.

func (*SQLiteStore) GetLastAudit added in v0.1.18

func (s *SQLiteStore) GetLastAudit(ctx context.Context) ([]audit.AuditResult, error)

GetLastAudit returns the most recent audit results (all rows sharing the latest audited_at timestamp).

func (*SQLiteStore) GetLastCheckIn added in v0.1.21

func (s *SQLiteStore) GetLastCheckIn(ctx context.Context) (time.Time, error)

GetLastCheckIn returns the time of the most recent get_digest call. Returns ErrNotFound if no check-in has been recorded yet.

func (*SQLiteStore) GetScalingControl added in v0.1.11

func (s *SQLiteStore) GetScalingControl(ctx context.Context) (*models.ScalingControl, error)

GetScalingControl returns the current scaling control record. Returns a zeroed ScalingControl (no override, not paused) if no record exists.

func (*SQLiteStore) GetSession

func (s *SQLiteStore) GetSession(ctx context.Context, id string) (*models.Session, error)

GetSession retrieves a session by ID. Returns ErrNotFound if no row matches.

func (*SQLiteStore) GetTaskProfile added in v0.1.11

func (s *SQLiteStore) GetTaskProfile(ctx context.Context, agentType, provider string) (*models.TaskProfile, error)

GetTaskProfile returns the profile for a specific (agentType, provider) pair. Returns (nil, nil) when no profile exists yet.

func (*SQLiteStore) IncrementIssueFailureCount added in v0.1.14

func (s *SQLiteStore) IncrementIssueFailureCount(ctx context.Context, issueNumber int) (int, error)

IncrementIssueFailureCount atomically increments the failure count for an issue and returns the new value. Creates the record if it doesn't exist.

func (*SQLiteStore) LatestMetricSnapshot added in v0.1.13

func (s *SQLiteStore) LatestMetricSnapshot(ctx context.Context) (*MetricSnapshot, error)

LatestMetricSnapshot returns the most recent metric snapshot, or nil if none exists.

func (*SQLiteStore) ListCorrectionEvents added in v0.1.16

func (s *SQLiteStore) ListCorrectionEvents(ctx context.Context, issueNumber int) ([]*models.CorrectionEvent, error)

ListCorrectionEvents returns correction events for a given issue.

func (*SQLiteStore) ListFailureEvents added in v0.1.14

func (s *SQLiteStore) ListFailureEvents(ctx context.Context, since time.Time, limit int) ([]*models.FailureEvent, error)

ListFailureEvents returns failure events since the given time, ordered by timestamp descending. Use limit=0 for no limit.

func (*SQLiteStore) ListScalingEvents added in v0.1.11

func (s *SQLiteStore) ListScalingEvents(ctx context.Context, limit int) ([]*models.ScalingEvent, error)

ListScalingEvents returns up to limit scaling events ordered newest-first. If limit <= 0 it defaults to 100.

func (*SQLiteStore) ListSessions

func (s *SQLiteStore) ListSessions(ctx context.Context, status models.SessionStatus) ([]*models.Session, error)

ListSessions returns all sessions matching the given status. Pass an empty status to list all sessions.

func (*SQLiteStore) ListTaskProfiles added in v0.1.11

func (s *SQLiteStore) ListTaskProfiles(ctx context.Context) ([]*models.TaskProfile, error)

ListTaskProfiles returns all task profiles ordered by agent_type, provider.

func (*SQLiteStore) RecordCost

func (s *SQLiteStore) RecordCost(ctx context.Context, r *models.CostRecord) error

RecordCost inserts a cost record. If r.ID is empty, a random ID is generated.

func (*SQLiteStore) SaveAuditResults added in v0.1.18

func (s *SQLiteStore) SaveAuditResults(ctx context.Context, results []audit.AuditResult) error

SaveAuditResults persists a batch of provider audit results.

func (*SQLiteStore) SaveCorrectionEvent added in v0.1.16

func (s *SQLiteStore) SaveCorrectionEvent(ctx context.Context, e *models.CorrectionEvent) error

SaveCorrectionEvent inserts a correction event into the corrections table.

func (*SQLiteStore) SaveFailureEvent added in v0.1.14

func (s *SQLiteStore) SaveFailureEvent(ctx context.Context, e *models.FailureEvent) error

SaveFailureEvent inserts a failure event into the failure_events table.

func (*SQLiteStore) SaveMetricSnapshot added in v0.1.13

func (s *SQLiteStore) SaveMetricSnapshot(ctx context.Context, m MetricSnapshot) error

SaveMetricSnapshot upserts the latest metric snapshot (single-row table).

func (*SQLiteStore) SaveScalingEvent added in v0.1.11

func (s *SQLiteStore) SaveScalingEvent(ctx context.Context, e models.ScalingEvent) error

SaveScalingEvent inserts a scaling event into the database. It is safe to call concurrently; each call uses a fresh ID.

func (*SQLiteStore) SetLastCheckIn added in v0.1.21

func (s *SQLiteStore) SetLastCheckIn(ctx context.Context, t time.Time) error

SetLastCheckIn records the current check-in time, replacing any previous value.

func (*SQLiteStore) UpdateSession

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

UpdateSession updates an existing session record. The ID must already exist.

func (*SQLiteStore) UpdateTaskProfile added in v0.1.11

func (s *SQLiteStore) UpdateTaskProfile(ctx context.Context, agentType, provider string) error

UpdateTaskProfile recomputes the profile for (agentType, provider) from completed sessions, then upserts the result. It uses all available completed sessions for accurate percentile calculation. A minimum of 1 sample is required; with fewer than 5 samples the P90 is estimated from the same distribution as P50.

func (*SQLiteStore) UpsertScalingControl added in v0.1.11

func (s *SQLiteStore) UpsertScalingControl(ctx context.Context, c models.ScalingControl) error

UpsertScalingControl inserts or replaces the scaling control record (single row, id=1).

type Store

type Store interface {
	// Sessions
	CreateSession(ctx context.Context, s *models.Session) error
	GetSession(ctx context.Context, id string) (*models.Session, error)
	UpdateSession(ctx context.Context, s *models.Session) error
	ListSessions(ctx context.Context, status models.SessionStatus) ([]*models.Session, error)

	// Cost tracking
	RecordCost(ctx context.Context, r *models.CostRecord) error
	ComputeCostSince(ctx context.Context, since time.Time) (*models.CostSummary, error)
	ComputeCostForIssue(ctx context.Context, issueNumber int) (*models.CostSummary, error)
	GetBudgetStatus(ctx context.Context, dailyBudgetUSD float64) (spent float64, remaining float64, err error)

	// Scaling events (written by dispatch, read by serve and MCP)
	SaveScalingEvent(ctx context.Context, e models.ScalingEvent) error
	ListScalingEvents(ctx context.Context, limit int) ([]*models.ScalingEvent, error)

	// Scaling control (written by serve/CLI, read by dispatch autoscaler)
	GetScalingControl(ctx context.Context) (*models.ScalingControl, error)
	UpsertScalingControl(ctx context.Context, c models.ScalingControl) error

	// Task profiles (updated after each completed session, read by policy engine and metrics API)
	UpdateTaskProfile(ctx context.Context, agentType, provider string) error
	ListTaskProfiles(ctx context.Context) ([]*models.TaskProfile, error)
	GetTaskProfile(ctx context.Context, agentType, provider string) (*models.TaskProfile, error)

	// Metric snapshots (written by dispatch, read by serve for cross-process metrics)
	SaveMetricSnapshot(ctx context.Context, m MetricSnapshot) error
	LatestMetricSnapshot(ctx context.Context) (*MetricSnapshot, error)

	// Failure events (written by dispatcher and runner, read by API, MCP, and CLI)
	SaveFailureEvent(ctx context.Context, e *models.FailureEvent) error
	ListFailureEvents(ctx context.Context, since time.Time, limit int) ([]*models.FailureEvent, error)
	CountFailuresByIssue(ctx context.Context, issueNumber int) (int, error)
	GetFailureSummary(ctx context.Context, since time.Time) (*models.FailureSummary, error)

	// Persisted issue failure counter (survives restarts, unlike in-memory map)
	GetIssueFailureCount(ctx context.Context, issueNumber int) (int, error)
	IncrementIssueFailureCount(ctx context.Context, issueNumber int) (int, error)
	ClearIssueFailureCount(ctx context.Context, issueNumber int) error

	// Correction events (written by correction engine, read by API and diagnostics)
	SaveCorrectionEvent(ctx context.Context, e *models.CorrectionEvent) error
	ListCorrectionEvents(ctx context.Context, issueNumber int) ([]*models.CorrectionEvent, error)

	// Provider audits (written by audit CLI, read by API and diagnostics)
	SaveAuditResults(ctx context.Context, results []audit.AuditResult) error
	GetLastAudit(ctx context.Context) ([]audit.AuditResult, error)

	// Check-in state (persists the last get_digest call time for accurate "away" duration)
	GetLastCheckIn(ctx context.Context) (time.Time, error)
	SetLastCheckIn(ctx context.Context, t time.Time) error

	Close() error
}

Store defines the persistence interface for Samverk.

Jump to

Keyboard shortcuts

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