store

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 11 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) 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) 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) 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) 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) 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) 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) 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)

	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