sqlite

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package sqlite provides SQLite-based persistence using modernc.org/sqlite (pure Go, no CGO).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CausalRepo

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

CausalRepo implements causal.CausalStore using SQLite. Compatible with causal_chains.db schema.

func NewCausalRepo

func NewCausalRepo(db *DB) (*CausalRepo, error)

NewCausalRepo creates a CausalRepo and ensures the schema exists.

func (*CausalRepo) AddEdge

func (r *CausalRepo) AddEdge(ctx context.Context, edge *causal.Edge) error

AddEdge inserts a causal edge.

func (*CausalRepo) AddNode

func (r *CausalRepo) AddNode(ctx context.Context, node *causal.Node) error

AddNode inserts a causal node.

func (*CausalRepo) GetChain

func (r *CausalRepo) GetChain(ctx context.Context, query string, maxDepth int) (*causal.Chain, error)

GetChain builds a causal chain around a decision node matching the query.

func (*CausalRepo) Stats

func (r *CausalRepo) Stats(ctx context.Context) (*causal.CausalStats, error)

Stats returns aggregate statistics about the causal store.

type CrystalRepo

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

CrystalRepo implements crystal.CrystalStore using SQLite. Compatible with crystals.db schema.

func NewCrystalRepo

func NewCrystalRepo(db *DB) (*CrystalRepo, error)

NewCrystalRepo creates a CrystalRepo and ensures the schema exists.

func (*CrystalRepo) Delete

func (r *CrystalRepo) Delete(ctx context.Context, path string) error

Delete removes a crystal by path.

func (*CrystalRepo) Get

func (r *CrystalRepo) Get(ctx context.Context, path string) (*crystal.Crystal, error)

Get retrieves a crystal by path.

func (*CrystalRepo) List

func (r *CrystalRepo) List(ctx context.Context, pattern string, limit int) ([]*crystal.Crystal, error)

List returns crystals matching a path pattern. Empty pattern returns all.

func (*CrystalRepo) Search

func (r *CrystalRepo) Search(ctx context.Context, query string, limit int) ([]*crystal.Crystal, error)

Search searches crystal primitives by name/value containing query.

func (*CrystalRepo) Stats

Stats returns aggregate statistics.

func (*CrystalRepo) Upsert

func (r *CrystalRepo) Upsert(ctx context.Context, c *crystal.Crystal) error

Upsert inserts or replaces a crystal.

type DB

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

DB wraps a *sql.DB with SQLite-specific configuration.

func Open

func Open(path string) (*DB, error)

Open opens or creates an SQLite database at the given path. It applies WAL mode and recommended pragmas for performance.

func OpenMemory

func OpenMemory() (*DB, error)

OpenMemory opens an in-memory SQLite database (for testing).

func (*DB) Close

func (d *DB) Close() error

Close closes the database connection.

func (*DB) Exec

func (d *DB) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query that doesn't return rows.

func (*DB) Path

func (d *DB) Path() string

Path returns the database file path.

func (*DB) Query

func (d *DB) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows.

func (*DB) QueryRow

func (d *DB) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query that returns at most one row.

func (*DB) SqlDB

func (d *DB) SqlDB() *sql.DB

SqlDB returns the underlying *sql.DB.

type FactRepo

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

FactRepo implements memory.FactStore using SQLite. Compatible with memory_bridge_v2.db schema v2.0.0.

func NewFactRepo

func NewFactRepo(db *DB) (*FactRepo, error)

NewFactRepo creates a FactRepo and ensures the schema exists.

func (*FactRepo) Add

func (r *FactRepo) Add(ctx context.Context, fact *memory.Fact) error

Add inserts a new fact.

func (*FactRepo) CompressFacts

func (r *FactRepo) CompressFacts(ctx context.Context, ids []string, summary string) (string, error)

CompressFacts archives originals and creates a summary fact. Genes (source='genome') are silently skipped.

func (*FactRepo) Delete

func (r *FactRepo) Delete(ctx context.Context, id string) error

Delete removes a fact by ID.

func (*FactRepo) Get

func (r *FactRepo) Get(ctx context.Context, id string) (*memory.Fact, error)

Get retrieves a fact by ID.

func (*FactRepo) GetColdFacts

func (r *FactRepo) GetColdFacts(ctx context.Context, limit int) ([]*memory.Fact, error)

GetColdFacts returns facts with hit_count=0, created >30 days ago. Genes (source='genome') and archived facts are excluded.

func (*FactRepo) GetExpired

func (r *FactRepo) GetExpired(ctx context.Context) ([]*memory.Fact, error)

GetExpired returns facts whose TTL has expired.

func (*FactRepo) GetStale

func (r *FactRepo) GetStale(ctx context.Context, includeArchived bool) ([]*memory.Fact, error)

GetStale returns stale facts, optionally including archived ones.

func (*FactRepo) ListByDomain

func (r *FactRepo) ListByDomain(ctx context.Context, domain string, includeStale bool) ([]*memory.Fact, error)

ListByDomain returns facts in a domain, optionally including stale ones.

func (*FactRepo) ListByLevel

func (r *FactRepo) ListByLevel(ctx context.Context, level memory.HierLevel) ([]*memory.Fact, error)

ListByLevel returns all facts at a given hierarchy level.

func (*FactRepo) ListDomains

func (r *FactRepo) ListDomains(ctx context.Context) ([]string, error)

ListDomains returns distinct domain names.

func (*FactRepo) ListGenes

func (r *FactRepo) ListGenes(ctx context.Context) ([]*memory.Fact, error)

ListGenes returns all genome facts (immutable survival invariants).

func (*FactRepo) RefreshTTL

func (r *FactRepo) RefreshTTL(ctx context.Context, id string) error

RefreshTTL resets the created_at timestamp for a fact (effectively refreshing its TTL).

func (*FactRepo) Search

func (r *FactRepo) Search(ctx context.Context, query string, limit int) ([]*memory.Fact, error)

Search performs a LIKE-based text search on fact content.

func (*FactRepo) Stats

func (r *FactRepo) Stats(ctx context.Context) (*memory.FactStoreStats, error)

Stats returns aggregate statistics about the fact store.

func (*FactRepo) TouchFact

func (r *FactRepo) TouchFact(ctx context.Context, id string) error

TouchFact increments hit_count and updates last_accessed_at.

func (*FactRepo) Update

func (r *FactRepo) Update(ctx context.Context, fact *memory.Fact) error

Update updates an existing fact.

type InteractionEntry

type InteractionEntry struct {
	ID        int64     `json:"id"`
	ToolName  string    `json:"tool_name"`
	ArgsJSON  string    `json:"args_json,omitempty"`
	Timestamp time.Time `json:"timestamp"`
	Processed bool      `json:"processed"`
}

InteractionEntry represents a single tool call record in the interaction log.

type InteractionLogRepo

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

InteractionLogRepo provides crash-safe tool call recording in SQLite. Every tool call is INSERT-ed immediately; WAL mode ensures durability even on kill -9 / terminal close.

func NewInteractionLogRepo

func NewInteractionLogRepo(db *DB) (*InteractionLogRepo, error)

NewInteractionLogRepo creates the interaction_log table if needed and returns the repo.

func (*InteractionLogRepo) Count

func (r *InteractionLogRepo) Count(ctx context.Context) (total int, unprocessed int, err error)

Count returns the total number of entries and unprocessed count.

func (*InteractionLogRepo) GetUnprocessed

func (r *InteractionLogRepo) GetUnprocessed(ctx context.Context) ([]InteractionEntry, error)

GetUnprocessed returns all entries not yet processed, ordered oldest first.

func (*InteractionLogRepo) MarkProcessed

func (r *InteractionLogRepo) MarkProcessed(ctx context.Context, ids []int64) error

MarkProcessed marks entries as processed by their IDs.

func (*InteractionLogRepo) Prune

func (r *InteractionLogRepo) Prune(ctx context.Context, olderThan time.Duration) (int64, error)

Prune deletes processed entries older than the given duration.

func (*InteractionLogRepo) Record

func (r *InteractionLogRepo) Record(ctx context.Context, toolName string, args map[string]interface{}) error

Record inserts a tool call entry. This is designed to be fire-and-forget from the middleware — errors are logged but don't break the tool call.

type PeerRepo

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

PeerRepo implements peer.PeerStore using SQLite.

func NewPeerRepo

func NewPeerRepo(db *DB) (*PeerRepo, error)

NewPeerRepo creates a PeerRepo and ensures the peers table exists.

func (*PeerRepo) DeleteExpired

func (r *PeerRepo) DeleteExpired(_ context.Context, olderThan time.Duration) (int, error)

DeleteExpired removes peers not seen within the given duration.

func (*PeerRepo) LoadPeers

func (r *PeerRepo) LoadPeers(_ context.Context) ([]*peer.PeerInfo, error)

LoadPeers returns all stored peers.

func (*PeerRepo) SavePeer

func (r *PeerRepo) SavePeer(_ context.Context, p *peer.PeerInfo) error

SavePeer upserts a peer record.

type SOCRepo

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

SOCRepo provides SQLite persistence for SOC events, incidents, and sensors.

func NewSOCRepo

func NewSOCRepo(db *DB) (*SOCRepo, error)

NewSOCRepo creates and initializes SOC tables.

func (*SOCRepo) CountEvents

func (r *SOCRepo) CountEvents(tenantID string) (int, error)

CountEvents returns total event count.

func (*SOCRepo) CountEventsSince

func (r *SOCRepo) CountEventsSince(tenantID string, since time.Time) (int, error)

CountEventsSince returns events in the given time window.

func (*SOCRepo) CountOpenIncidents

func (r *SOCRepo) CountOpenIncidents(tenantID string) (int, error)

CountOpenIncidents returns count of non-resolved incidents.

func (*SOCRepo) CountSensorsByStatus

func (r *SOCRepo) CountSensorsByStatus(tenantID string) (map[soc.SensorStatus]int, error)

CountSensorsByStatus returns sensor count grouped by status.

func (*SOCRepo) EventExistsByHash

func (r *SOCRepo) EventExistsByHash(contentHash string) (bool, error)

EventExistsByHash checks if an event with the given content hash already exists (§5.2 dedup).

func (*SOCRepo) GetEvent

func (r *SOCRepo) GetEvent(id string) (*soc.SOCEvent, error)

GetEvent retrieves a single event by ID.

func (*SOCRepo) GetIncident

func (r *SOCRepo) GetIncident(id string) (*soc.Incident, error)

GetIncident retrieves an incident by ID with full case management data.

func (*SOCRepo) GetSensor

func (r *SOCRepo) GetSensor(id string) (*soc.Sensor, error)

GetSensor retrieves a sensor by ID.

func (*SOCRepo) InsertEvent

func (r *SOCRepo) InsertEvent(e soc.SOCEvent) error

InsertEvent persists a SOC event.

func (*SOCRepo) InsertIncident

func (r *SOCRepo) InsertIncident(inc soc.Incident) error

InsertIncident persists a new incident.

func (*SOCRepo) ListEvents

func (r *SOCRepo) ListEvents(tenantID string, limit int) ([]soc.SOCEvent, error)

ListEvents returns events ordered by timestamp (newest first), with limit.

func (*SOCRepo) ListEventsByCategory

func (r *SOCRepo) ListEventsByCategory(tenantID string, category string, limit int) ([]soc.SOCEvent, error)

ListEventsByCategory returns events filtered by category.

func (*SOCRepo) ListIncidents

func (r *SOCRepo) ListIncidents(tenantID string, status string, limit int) ([]soc.Incident, error)

ListIncidents returns incidents, optionally filtered by status.

func (*SOCRepo) ListSensors

func (r *SOCRepo) ListSensors(tenantID string) ([]soc.Sensor, error)

ListSensors returns all registered sensors.

func (*SOCRepo) PurgeExpiredEvents

func (r *SOCRepo) PurgeExpiredEvents(retentionDays int) (int64, error)

PurgeExpiredEvents deletes events older than the retention period. Returns the number of deleted events.

func (*SOCRepo) PurgeExpiredIncidents

func (r *SOCRepo) PurgeExpiredIncidents(retentionDays int) (int64, error)

PurgeExpiredIncidents deletes resolved incidents older than the retention period. Only resolved incidents are purged; open/investigating incidents are preserved. Returns the number of deleted incidents.

func (*SOCRepo) UpdateIncident

func (r *SOCRepo) UpdateIncident(inc *soc.Incident) error

UpdateIncident persists the full incident state including case management data.

func (*SOCRepo) UpdateIncidentStatus

func (r *SOCRepo) UpdateIncidentStatus(id string, status soc.IncidentStatus) error

UpdateIncidentStatus updates status (and optionally resolved_at).

func (*SOCRepo) UpsertSensor

func (r *SOCRepo) UpsertSensor(s soc.Sensor) error

UpsertSensor creates or updates a sensor entry.

type StateRepo

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

StateRepo implements session.StateStore using SQLite. Compatible with memory_bridge.db schema (states + audit_log). NOTE: The Python version uses AES-256-GCM encryption on the data blob. This Go implementation stores plaintext JSON for now — encryption can be layered on top via a decorator if needed.

func NewStateRepo

func NewStateRepo(db *DB) (*StateRepo, error)

NewStateRepo creates a StateRepo and ensures the schema exists.

func (*StateRepo) DeleteSession

func (r *StateRepo) DeleteSession(ctx context.Context, sessionID string) (int, error)

DeleteSession removes all versions of a session. Returns the number of deleted rows.

func (*StateRepo) GetAuditLog

func (r *StateRepo) GetAuditLog(ctx context.Context, sessionID string, limit int) ([]session.AuditEntry, error)

GetAuditLog returns the audit log for a session.

func (*StateRepo) ListSessions

func (r *StateRepo) ListSessions(ctx context.Context) ([]session.SessionInfo, error)

ListSessions returns metadata about all persisted sessions.

func (*StateRepo) Load

func (r *StateRepo) Load(ctx context.Context, sessionID string, version *int) (*session.CognitiveStateVector, string, error)

Load retrieves a cognitive state vector. If version is nil, loads the latest.

func (*StateRepo) Save

func (r *StateRepo) Save(ctx context.Context, state *session.CognitiveStateVector, checksum string) error

Save persists a cognitive state vector snapshot.

type SynapseRepo

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

SynapseRepo implements synapse.SynapseStore using SQLite.

func NewSynapseRepo

func NewSynapseRepo(db *DB) *SynapseRepo

NewSynapseRepo creates a SynapseRepo (table created by FactRepo migration v3.3).

func (*SynapseRepo) Accept

func (r *SynapseRepo) Accept(ctx context.Context, id int64) error

Accept transitions a synapse to VERIFIED.

func (*SynapseRepo) Count

func (r *SynapseRepo) Count(ctx context.Context) (pending, verified, rejected int, err error)

Count returns synapse counts by status.

func (*SynapseRepo) Create

func (r *SynapseRepo) Create(ctx context.Context, factIDA, factIDB string, confidence float64) (int64, error)

Create inserts a new PENDING synapse.

func (*SynapseRepo) Exists

func (r *SynapseRepo) Exists(ctx context.Context, factIDA, factIDB string) (bool, error)

Exists checks if a synapse exists between two facts (either direction).

func (*SynapseRepo) ListPending

func (r *SynapseRepo) ListPending(ctx context.Context, limit int) ([]*synapse.Synapse, error)

ListPending returns synapses with status PENDING.

func (*SynapseRepo) ListVerified

func (r *SynapseRepo) ListVerified(ctx context.Context) ([]*synapse.Synapse, error)

ListVerified returns all VERIFIED synapses.

func (*SynapseRepo) Reject

func (r *SynapseRepo) Reject(ctx context.Context, id int64) error

Reject transitions a synapse to REJECTED.

Jump to

Keyboard shortcuts

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