Documentation
¶
Index ¶
- Constants
- func ActiveFile(baseDir string) string
- func ComputeEffectiveImportance(importance int, accessCount int, daysSinceAccess float64, edgeCount int) float64
- func DefaultDataDir() string
- func IsImmune(importance int, accessCount int) bool
- func ListStores(baseDir string) ([]string, error)
- func MaxInsightsFromEnv(fallback int) int
- func MigrateIfNeeded(baseDir string) error
- func ReadActive(baseDir string) string
- func StoreDir(baseDir, name string) string
- func StoreExists(baseDir, name string) bool
- func ValidStoreName(name string) bool
- func WriteActive(baseDir, name string) error
- type DB
- func (db *DB) AutoPrune(maxInsights int, excludeIDs []string) (int, error)
- func (db *DB) AutoPruneOwned(owner string, maxInsights int, excludeIDs []string) (int, error)
- func (db *DB) BoostRetention(id string) error
- func (db *DB) Close() error
- func (db *DB) Conn() *sql.DB
- func (db *DB) DeleteEdge(sourceID string, targetID string, edgeType model.EdgeType) error
- func (db *DB) DeleteEdgesByNode(nodeID string) error
- func (db *DB) Dialect() Dialect
- func (db *DB) DisplayPath() string
- func (db *DB) EmbeddingStats() (total int, embedded int, err error)
- func (db *DB) FindInsightsWithEntity(entity string, excludeID string, limit int) ([]string, error)
- func (db *DB) GetActiveInsightsBySourceOrdered(source string) ([]*model.Insight, error)
- func (db *DB) GetActiveInsightsForDiff(owner, layer string) ([]*model.Insight, error)
- func (db *DB) GetAllActiveInsights() ([]*model.Insight, error)
- func (db *DB) GetAllEdges() ([]*model.Edge, error)
- func (db *DB) GetAllEmbeddings() ([]EmbeddedInsight, error)
- func (db *DB) GetEdgesByNode(nodeID string) ([]*model.Edge, error)
- func (db *DB) GetEdgesByNodeAndType(nodeID string, edgeType model.EdgeType) ([]*model.Edge, error)
- func (db *DB) GetEdgesBySourceAndType(sourceID string, edgeType model.EdgeType) ([]*model.Edge, error)
- func (db *DB) GetEmbedding(id string) ([]byte, error)
- func (db *DB) GetInsightByID(id string) (*model.Insight, error)
- func (db *DB) GetInsightByIDIncludeDeleted(id string) (*model.Insight, error)
- func (db *DB) GetInsightsWithoutEmbedding(limit int) ([]*model.Insight, error)
- func (db *DB) GetLatestInsightBySource(source string, excludeID string) (*model.Insight, error)
- func (db *DB) GetOplog(limit int) ([]OplogEntry, error)
- func (db *DB) GetPrincipal(principal string) (*Principal, error)
- func (db *DB) GetRecentInsightsBySource(source string, excludeID string, limit int) ([]*model.Insight, error)
- func (db *DB) GetRecentInsightsInWindow(excludeID string, windowHours float64, limit int) ([]*model.Insight, error)
- func (db *DB) GetRetentionCandidates(threshold float64, limit int) ([]RetentionCandidate, int, error)
- func (db *DB) GetRetentionCandidatesOwned(threshold float64, limit int, owner string) ([]RetentionCandidate, int, error)
- func (db *DB) GetStats() (*InsightStats, error)
- func (db *DB) InTransaction(fn func(*DB) error) error
- func (db *DB) IncrementAccessCount(id string) error
- func (db *DB) InsertEdge(e *model.Edge) error
- func (db *DB) InsertInsight(i *model.Insight) error
- func (db *DB) InsertIssuedToken(jti, principal string, expiresAt time.Time) error
- func (db *DB) IsReadOnly() bool
- func (db *DB) LoadKnownEntities() (map[string]bool, error)
- func (db *DB) LogOp(operation, insightID, detail string)
- func (db *DB) LookupIssuedToken(jti string) (*IssuedToken, error)
- func (db *DB) Path() string
- func (db *DB) Ping() error
- func (db *DB) QueryInsights(f QueryFilter) ([]*model.Insight, error)
- func (db *DB) RefreshEffectiveImportance(id string) (float64, error)
- func (db *DB) RevokeIssuedToken(jti string) error
- func (db *DB) RevokePrincipalTokens(principal string) error
- func (db *DB) ScanEmbeddings(fn func(id string, blob []byte) bool) error
- func (db *DB) SetPrincipalDisabled(principal string, disabled bool) error
- func (db *DB) SoftDeleteInsight(id string) error
- func (db *DB) UpdateEmbedding(id string, blob []byte) error
- func (db *DB) UpdateEntities(id string, entities []string) error
- func (db *DB) UpsertPrincipal(principal, role string) error
- type Dialect
- type EmbeddedInsight
- type EntityStat
- type InsightStats
- type IssuedToken
- type OplogEntry
- type Options
- type OrgDocument
- type OrgSource
- type Principal
- type QueryFilter
- type RetentionCandidate
Constants ¶
const ( // HalfLifeDays controls how fast effective_importance decays. // After this many days without access, importance halves. HalfLifeDays = 30.0 // MaxInsights is the default cap before auto-pruning kicks in for local SQLite. MaxInsights = 1000 // ServerDefaultMaxInsights is the Helm/server default personal cap per principal. ServerDefaultMaxInsights = 25000 // PruneBatchSize is how many excess insights to prune at once. PruneBatchSize = 10 )
Lifecycle constants
const DefaultStoreName = "default"
DefaultStoreName is the fallback store when none is specified.
const MaxOplogEntries = 5000
MaxOplogEntries is the maximum number of oplog entries to retain.
Variables ¶
This section is empty.
Functions ¶
func ActiveFile ¶
ActiveFile returns the path to <baseDir>/active.
func ComputeEffectiveImportance ¶
func ComputeEffectiveImportance(importance int, accessCount int, daysSinceAccess float64, edgeCount int) float64
ComputeEffectiveImportance calculates the current effective importance. Formula: base_weight(imp) * log(1 + access_count) * 0.5^(days_since_access / half_life) * (1 + 0.1*min(edges,5)) For newly created insights (0 days, 0 access), the access factor is log(1+0)=0, so we use max(1.0, log(1+access)) to ensure a non-zero baseline.
func IsImmune ¶
IsImmune returns true if an insight should never be auto-pruned. Immune if: importance >= 4 OR access_count >= 3.
func ListStores ¶
ListStores returns sorted names of all stores under <baseDir>/data/.
func MaxInsightsFromEnv ¶
MaxInsightsFromEnv returns MNEMON_MAX_INSIGHTS or fallback when unset/invalid.
func MigrateIfNeeded ¶
MigrateIfNeeded moves a legacy ~/.mnemon/mnemon.db into the new data/default/ layout. It is safe to call multiple times.
func ReadActive ¶
ReadActive reads the active store name from <baseDir>/active. Returns DefaultStoreName if the file doesn't exist or is empty.
func StoreExists ¶
StoreExists checks whether the named store directory exists.
func ValidStoreName ¶
ValidStoreName returns true if name matches [a-zA-Z0-9][a-zA-Z0-9_-]*.
func WriteActive ¶
WriteActive writes the active store name to <baseDir>/active.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB wraps a SQLite or Postgres database connection.
func OpenReadOnly ¶
OpenReadOnly opens the SQLite database in read-only mode. Safe for read-only filesystem mounts: uses journal_mode=OFF to avoid writing WAL/SHM sidecar files.
func OpenWithOptions ¶
OpenWithOptions opens SQLite (default) or Postgres from a connection URL.
func (*DB) AutoPrune ¶
AutoPrune soft-deletes the lowest effective_importance non-immune insights when total active count exceeds maxInsights. excludeIDs are protected from pruning (typically the just-created insights). Returns number pruned. If already inside a transaction (db.tx != nil), executes inline; otherwise wraps in its own transaction.
func (*DB) AutoPruneOwned ¶
AutoPruneOwned soft-deletes personal-layer insights for owner when owner is set.
func (*DB) BoostRetention ¶
BoostRetention boosts an insight's retention: access_count +3, refreshes last_accessed_at.
func (*DB) DeleteEdge ¶
DeleteEdge removes one typed edge between two nodes.
func (*DB) DeleteEdgesByNode ¶
DeleteEdgesByNode removes all edges referencing a node.
func (*DB) DisplayPath ¶
DisplayPath is safe to show to clients. Postgres DSNs have the password redacted.
func (*DB) EmbeddingStats ¶
EmbeddingStats returns total insights and how many have embeddings.
func (*DB) FindInsightsWithEntity ¶
FindInsightsWithEntity returns insight IDs that have the given entity in their entities JSON array.
func (*DB) GetActiveInsightsBySourceOrdered ¶
GetActiveInsightsBySourceOrdered returns active insights for a source in chronological order.
func (*DB) GetActiveInsightsForDiff ¶
GetActiveInsightsForDiff returns insights considered for duplicate/conflict detection. When owner is set, only that principal's personal-layer memories are returned.
func (*DB) GetAllActiveInsights ¶
GetAllActiveInsights returns all non-deleted insights.
func (*DB) GetAllEdges ¶
GetAllEdges returns all edges in the graph.
func (*DB) GetAllEmbeddings ¶
func (db *DB) GetAllEmbeddings() ([]EmbeddedInsight, error)
GetAllEmbeddings returns all active insights that have embeddings.
func (*DB) GetEdgesByNode ¶
GetEdgesByNode returns all edges where the given node is source or target. Uses UNION ALL to allow SQLite to use separate indexes on source_id and target_id.
func (*DB) GetEdgesByNodeAndType ¶
GetEdgesByNodeAndType returns edges for a node filtered by edge type. Uses UNION ALL to allow SQLite to use composite indexes.
func (*DB) GetEdgesBySourceAndType ¶
func (db *DB) GetEdgesBySourceAndType(sourceID string, edgeType model.EdgeType) ([]*model.Edge, error)
GetEdgesBySourceAndType returns edges where the given node is source, filtered by type.
func (*DB) GetEmbedding ¶
GetEmbedding returns the raw embedding blob for an insight.
func (*DB) GetInsightByID ¶
GetInsightByID returns a single insight by ID (excludes soft-deleted).
func (*DB) GetInsightByIDIncludeDeleted ¶
GetInsightByIDIncludeDeleted returns a single insight by ID, including soft-deleted.
func (*DB) GetInsightsWithoutEmbedding ¶
GetInsightsWithoutEmbedding returns active insights that lack embeddings.
func (*DB) GetLatestInsightBySource ¶
GetLatestInsightBySource returns the most recent non-deleted insight for a given source, excluding the given ID.
func (*DB) GetOplog ¶
func (db *DB) GetOplog(limit int) ([]OplogEntry, error)
GetOplog returns the most recent N oplog entries.
func (*DB) GetPrincipal ¶
GetPrincipal loads a principal row.
func (*DB) GetRecentInsightsBySource ¶
func (db *DB) GetRecentInsightsBySource(source string, excludeID string, limit int) ([]*model.Insight, error)
GetRecentInsightsBySource returns the N most recent non-deleted insights for a source, excluding the given ID.
func (*DB) GetRecentInsightsInWindow ¶
func (db *DB) GetRecentInsightsInWindow(excludeID string, windowHours float64, limit int) ([]*model.Insight, error)
GetRecentInsightsInWindow returns non-deleted insights created within the given time window (hours), excluding the given ID. Ordered by created_at DESC.
func (*DB) GetRetentionCandidates ¶
func (db *DB) GetRetentionCandidates(threshold float64, limit int) ([]RetentionCandidate, int, error)
GetRetentionCandidates returns non-immune insights sorted by effective_importance ascending. Uses bulk queries for last_accessed_at and edge counts instead of per-insight queries.
func (*DB) GetRetentionCandidatesOwned ¶
func (db *DB) GetRetentionCandidatesOwned(threshold float64, limit int, owner string) ([]RetentionCandidate, int, error)
GetRetentionCandidatesOwned lists GC candidates. When owner is set, only that principal's personal-layer memories are considered.
func (*DB) GetStats ¶
func (db *DB) GetStats() (*InsightStats, error)
GetStats returns aggregate statistics.
func (*DB) InTransaction ¶
InTransaction runs fn inside a single SQL transaction. fn receives a scoped *DB whose tx pointer is private to this call, so concurrent InTransaction calls on the same *DB do not share db.tx. Store methods used inside fn must go through the scoped *DB, not the receiver.
func (*DB) IncrementAccessCount ¶
IncrementAccessCount bumps the access count and refreshes last_accessed_at. No-op when the database is read-only.
func (*DB) InsertEdge ¶
InsertEdge inserts or replaces an edge.
func (*DB) InsertInsight ¶
InsertInsight inserts a new insight into the database.
func (*DB) InsertIssuedToken ¶
InsertIssuedToken records a newly issued JWT id.
func (*DB) IsReadOnly ¶
IsReadOnly returns true if the database was opened in read-only mode.
func (*DB) LoadKnownEntities ¶
LoadKnownEntities returns the set of distinct entities that appear on at least one active (non-deleted) insight. It returns an empty (non-nil) map when no insights carry entities. The result is intended for use as a read-only filter — for example, by an index-aware entity extractor that admits wide-cast candidates only when they match a previously stored entity.
func (*DB) LogOp ¶
LogOp records an operation to the oplog and trims old entries beyond MaxOplogEntries. Best-effort: failures are logged to stderr but do not propagate. No-op when the database is read-only.
func (*DB) LookupIssuedToken ¶
func (db *DB) LookupIssuedToken(jti string) (*IssuedToken, error)
LookupIssuedToken returns a token row by jti.
func (*DB) QueryInsights ¶
func (db *DB) QueryInsights(f QueryFilter) ([]*model.Insight, error)
QueryInsights returns insights matching the filter, ordered by importance DESC, created_at DESC.
func (*DB) RefreshEffectiveImportance ¶
RefreshEffectiveImportance recomputes and stores effective_importance for one insight.
func (*DB) RevokeIssuedToken ¶
RevokeIssuedToken flags a jti as revoked.
func (*DB) RevokePrincipalTokens ¶
RevokePrincipalTokens revokes every token for a principal.
func (*DB) ScanEmbeddings ¶
ScanEmbeddings streams embeddings one at a time via callback, avoiding full-slice allocation. The callback returns true to continue scanning, false to stop early.
func (*DB) SetPrincipalDisabled ¶
SetPrincipalDisabled toggles a principal without deleting history.
func (*DB) SoftDeleteInsight ¶
SoftDeleteInsight sets deleted_at on an insight and removes all associated edges.
func (*DB) UpdateEmbedding ¶
UpdateEmbedding stores an embedding vector for an insight.
func (*DB) UpdateEntities ¶
UpdateEntities updates the entities field for an insight.
func (*DB) UpsertPrincipal ¶
UpsertPrincipal creates or updates a principal. Existing role is replaced.
type EmbeddedInsight ¶
EmbeddedInsight pairs an insight ID, content, and its embedding blob.
type EntityStat ¶
EntityStat holds entity link count info.
type InsightStats ¶
type InsightStats struct {
Total int `json:"total"`
ByCategory map[string]int `json:"by_category"`
EdgeCount int `json:"edge_count"`
DeletedCount int `json:"deleted_count"`
TopEntities []EntityStat `json:"top_entities"`
OplogCount int `json:"oplog_count"`
}
InsightStats holds aggregate statistics.
type IssuedToken ¶
type IssuedToken struct {
JTI string
Principal string
ExpiresAt time.Time
Revoked bool
CreatedAt time.Time
}
IssuedToken is a revocable JWT identifier.
type OplogEntry ¶
type OplogEntry struct {
ID int `json:"id"`
Operation string `json:"operation"`
InsightID string `json:"insight_id,omitempty"`
Detail string `json:"detail,omitempty"`
CreatedAt string `json:"created_at"`
}
OplogEntry represents a single operation log entry.
type OrgDocument ¶
type OrgDocument struct {
ExternalRef string
SourceURI string
Content string
Category string
Importance int
Tags []string
Entities []string
}
OrgDocument is one organizational ground-truth record from an external source. Future wiki/plugin adapters implement OrgSource and upsert by ExternalRef.
type OrgSource ¶
type OrgSource interface {
List(ctx context.Context) ([]OrgDocument, error)
}
OrgSource is a future plugin hook for organizational memories stored outside Postgres (for example a company wiki). Mnemon would list documents, upsert them as layer=org insights keyed by ExternalRef, then recall and adjust them like any other memory. Not implemented in this cut.
type QueryFilter ¶
type QueryFilter struct {
Keyword string
Category string
MinImportance int
Source string
Limit int
}
QueryFilter holds optional filters for querying insights.
type RetentionCandidate ¶
type RetentionCandidate struct {
Insight *model.Insight `json:"insight"`
EffectiveImportance float64 `json:"effective_importance"`
DaysSinceAccess float64 `json:"days_since_access"`
EdgeCount int `json:"edge_count"`
Immune bool `json:"immune"`
}
RetentionCandidate holds an insight and its effective importance breakdown.