memory

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package memory defines memini's core domain types.

Index

Constants

View Source
const (
	// ConfidenceSeedFresh is the starting confidence of a durable fact written
	// or promoted from observed activity (some basis, not yet corroborated).
	ConfidenceSeedFresh = 0.4
	// ConfidenceSeedImported is the starting confidence of an uncorroborated
	// bulk import: lower, so imports must earn trust before outranking facts the
	// agent actually established.
	ConfidenceSeedImported = 0.25
	// ConfidenceDemoteFloor is the corroboration below which an old, never-
	// recalled durable memory is treated as uncorroborated debris (demotion
	// eligibility, and the diagnostic's low-confidence count).
	ConfidenceDemoteFloor = 0.35
)

Confidence lifecycle constants.

Variables

This section is empty.

Functions

func GrowConfidence added in v0.0.11

func GrowConfidence(c float64) float64

GrowConfidence raises a confidence toward 1 logistically on corroboration: each re-observation closes 10% of the remaining gap, so confidence asymptotes to 1 and never overshoots.

func NormalizeContent

func NormalizeContent(s string) string

NormalizeContent collapses whitespace and case so trivially-duplicated memories compare equal. Used by recall dedup and the fsck duplicate audit.

Types

type Memory

type Memory struct {
	ID        string `json:"id"`
	Namespace string `json:"namespace"`
	Tier      Tier   `json:"tier"`
	Content   string `json:"content"`
	Summary   string `json:"summary,omitempty"`

	// Metadata is arbitrary structured data, persisted as JSON.
	Metadata map[string]any `json:"metadata,omitempty"`
	// Tags are free-form labels used for keyword retrieval and filtering.
	Tags []string `json:"tags,omitempty"`

	// Importance biases decay and ranking; higher survives longer. Range [0,1].
	Importance float64 `json:"importance"`

	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
	LastAccessedAt time.Time  `json:"last_accessed_at"`
	AccessCount    int        `json:"access_count"`
	ExpiresAt      *time.Time `json:"expires_at,omitempty"`

	// SupersededBy points at the memory that replaced this one during
	// contradiction resolution; non-nil means this record is tombstoned.
	SupersededBy *string `json:"superseded_by,omitempty"`

	// ValidFrom / ValidTo bound the wall-clock interval a fact was true. Both nil
	// means "always" (the common case). ValidTo is stamped when a fact is
	// superseded, so a time-filtered recall (Filter.AsOf) can answer "what was
	// true in March" by surfacing facts valid then even if later replaced.
	ValidFrom *time.Time `json:"valid_from,omitempty"`
	ValidTo   *time.Time `json:"valid_to,omitempty"`

	// Confidence is how corroborated a durable (semantic/procedural) fact is,
	// in [0,1]. It starts low for a fresh or imported fact, grows logistically
	// each time the fact is re-observed, and decays without reinforcement, so a
	// corroborated fact outranks one-off noise. nil means "not tracked" (every
	// short-term memory, and durable memories written before the field existed),
	// treated as fully trusted so existing data is never retroactively penalized.
	Confidence *float64 `json:"confidence,omitempty"`

	// Embedding is the dense vector for similarity search. It is required when
	// writing to the store and is omitted from API responses.
	Embedding []float32 `json:"-"`
}

Memory is a single stored memory, scoped to a namespace (tenant/agent).

func (*Memory) DurableScore added in v0.0.13

func (m *Memory) DurableScore(now time.Time) float64

DurableScore ranks a memory as durable knowledge (e.g. a session briefing): salience × corroboration × reinforcement, without Quality's recency decay, so a core fact unrecalled for weeks is not buried under fresher trivia.

func (*Memory) EffectiveConfidence added in v0.0.11

func (m *Memory) EffectiveConfidence(now time.Time) float64

EffectiveConfidence is a durable memory's corroboration at now: the stored confidence lazily decayed for the weeks elapsed since it was last corroborated (UpdatedAt) or recalled (LastAccessedAt). Decay is applied at read time so the maintenance sweep needs no extra write. Returns 1 (neutral) for short-term memories and for any memory that never had confidence recorded — so existing data written before the field is treated as fully trusted, not penalized.

func (*Memory) Expired

func (m *Memory) Expired(now time.Time) bool

Expired reports whether the memory has passed its TTL as of now.

func (*Memory) Quality added in v0.0.11

func (m *Memory) Quality(now time.Time) float64

Quality scores a memory for both recall ranking and lifecycle decisions (higher = more worth keeping and surfacing). It multiplies the base salience by corroboration (confidence), reinforcement (access frequency), and recency (exponential decay since last access). A corroborated, frequently-recalled durable fact scores far above a stale one-off observation, so low-value bulk memories sink by construction rather than by a tuning nudge.

func (*Memory) Recency

func (m *Memory) Recency(now time.Time) float64

Recency returns an exponentially-decaying [0,1] factor for how recently the memory was accessed, halving every retentionHalfLife. Used by recall ranking.

func (*Memory) RetentionScore

func (m *Memory) RetentionScore(now time.Time) float64

RetentionScore is the legacy short-term-eviction score, retained as an alias of Quality so existing callers keep working; new code should call Quality.

func (*Memory) Salience added in v0.0.11

func (m *Memory) Salience() float64

Salience is the base, time-independent quality of a memory in [0,1]: the tier's weight modulated by importance. It does not depend on access or age.

type Term

type Term string

Term is the coarse memory horizon: short-term memories are transient and TTL'd; long-term memories are durable and curated.

const (
	ShortTerm Term = "short" // working, episodic — transient, decays
	LongTerm  Term = "long"  // semantic, procedural — durable, curated
)

type Tier

type Tier string

Tier classifies a memory by how consolidated it is: working → episodic → semantic, with procedural held separately for how-to knowledge.

const (
	// TierWorking holds raw, short-lived observations (typically session-scoped).
	TierWorking Tier = "working"
	// TierEpisodic holds summaries of what happened in a session.
	TierEpisodic Tier = "episodic"
	// TierSemantic holds durable extracted facts ("what I know").
	TierSemantic Tier = "semantic"
	// TierProcedural holds workflows and how-to knowledge.
	TierProcedural Tier = "procedural"
)

func (Tier) DefaultTTL

func (t Tier) DefaultTTL() time.Duration

DefaultTTL is the TTL for a tier; zero means never expires.

func (Tier) Term

func (t Tier) Term() Term

Term maps a tier to its memory horizon. Working/episodic are short-term; semantic/procedural are long-term.

func (Tier) Valid

func (t Tier) Valid() bool

Valid reports whether t is a known tier.

Jump to

Keyboard shortcuts

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