Documentation
¶
Overview ¶
Package memory provides cross-session note persistence so agents can learn between sessions without retraining.
Three pieces:
- Note: one distilled fact, preference, or correction.
- Store: persistence contract scoped by an opaque ScopeKey (typically a user id, tenant id, or sessionKey when no higher scope exists).
- FormatNotes: rendering helper that produces the text block prepended to a session's system prompt by the agent loader.
Bounded growth is a hard requirement of every Store implementation: either via a per-scope cap enforced inside Put (the InMemStore path) or via a periodic Compactor that calls ReplaceAll with a curated subset (the agent.Consolidator path). Without one of these in place, uncurated note streams would grow the loader's prompt linearly and eventually exhaust the model context.
Consolidation (turning a closed session's transcript into Notes) lives in pkg/agent so it can reuse the loop's LLMProvider; this package stays a pure data layer with no LLM dependency.
Concurrency: every Store implementation must be safe for concurrent Put/List/Delete/ReplaceAll. Reads dominate writes — the loader pulls notes on every Run start — so backends should favor read paths.
Index ¶
- Constants
- func FormatNotes(notes []Note) string
- type InMemStore
- func (s *InMemStore) Delete(_ context.Context, scope, key string) error
- func (s *InMemStore) List(_ context.Context, scope string, opts ListOpts) ([]Note, error)
- func (s *InMemStore) Put(_ context.Context, scope string, note Note) error
- func (s *InMemStore) ReplaceAll(_ context.Context, scope string, notes []Note) error
- type InMemStoreOpt
- type ListOpts
- type Note
- type Store
Constants ¶
const DefaultMaxNotesPerScope = 100
DefaultMaxNotesPerScope is the cap applied by NewInMemStore when the caller doesn't pass WithMaxNotesPerScope. Picked to be permissive but not unbounded: 100 notes × ~30 tokens ≈ 3K tokens — well above what the loader's default TokenBudget will surface, low enough that an adopter that forgets to wire a Consolidator still can't blow out memory by hand-Putting forever.
Override (including disabling the cap with 0) via WithMaxNotesPerScope.
Variables ¶
This section is empty.
Functions ¶
func FormatNotes ¶
FormatNotes renders notes for system-prompt injection. The output is stable: same notes produce same text, so prompt-cache prefixes stay warm across calls within a Run. Returns "" when notes is empty so callers can concatenate unconditionally without checking length.
The format is intentionally plain text (not JSON) so the LLM treats it as natural-language context. Adopters can swap this for a custom renderer by formatting notes themselves before injection.
Types ¶
type InMemStore ¶
type InMemStore struct {
// contains filtered or unexported fields
}
InMemStore is a process-local Store. Safe for concurrent use; reads take an RLock so the loader hot-path scales across goroutines.
Bounded: when maxPerScope > 0, Put evicts the least-recently-updated note in the scope before inserting a key that would overflow. The eviction is silent — callers see no error, just a smaller store than the naive insert count.
func NewInMemStore ¶
func NewInMemStore(opts ...InMemStoreOpt) *InMemStore
NewInMemStore returns an in-process Store backed by a map. Use for tests and single-node deployments where rebuilding memory on restart is acceptable (the agent.Consolidator rebuilds within a few sessions).
Bounded by default: callers who don't pass WithMaxNotesPerScope get DefaultMaxNotesPerScope as the cap. To disable the cap, pass WithMaxNotesPerScope(0) explicitly — opting in to unbounded growth is a deliberate choice the caller must make on the line.
func (*InMemStore) Delete ¶
func (s *InMemStore) Delete(_ context.Context, scope, key string) error
Delete removes one note. No-op when missing.
func (*InMemStore) List ¶
List returns notes under scope filtered by opts and ordered by UpdatedAt descending. An unseen scope returns (nil, nil).
func (*InMemStore) Put ¶
Put inserts or updates a note. CreatedAt is preserved across updates; UpdatedAt is stamped now. When the resulting scope size would exceed maxPerScope, the least-recently-updated note (other than the one being inserted) is evicted first.
func (*InMemStore) ReplaceAll ¶
ReplaceAll atomically swaps every note under scope. CreatedAt is preserved from the prior state when a key matches; new keys get a fresh stamp. UpdatedAt is stamped on every entry. An empty notes slice clears the scope.
Used by the Consolidator's merge path: one LLM call produces the curated full set, ReplaceAll publishes it without exposing a window where deletions and inserts are observable separately.
type InMemStoreOpt ¶
type InMemStoreOpt func(*InMemStore)
InMemStoreOpt configures an InMemStore at construction.
func WithMaxNotesPerScope ¶
func WithMaxNotesPerScope(n int) InMemStoreOpt
WithMaxNotesPerScope caps each scope's note count. When Put would exceed n, the least-recently-updated note in the scope is evicted before the new note lands — keeping the store bounded even when an upstream Consolidator drifts toward variable keys, or when an adopter calls Put directly without running a Consolidator. Pass 0 to disable the cap entirely (unbounded — use only when you know growth is bounded elsewhere). Negative values are treated as 0.
Pick n based on the loader's expected token budget. The default FormatNotes block is ~36 tokens of overhead plus ~21 tokens per note (one short sentence), so 100 notes ≈ 2100 tokens of system prompt before the loader's per-Run TokenBudget further trims it.
type ListOpts ¶
type ListOpts struct {
// Limit caps the number of notes returned. 0 means unlimited.
// Stores apply the limit after sorting (newest first), so callers
// always get the most recent N. Negative values are clamped to 0.
Limit int
// Tags, when non-empty, returns only notes that carry every listed
// tag (AND filter). Empty (the default) returns all notes
// regardless of tags.
Tags []string
// UpdatedAfter, when non-zero, returns only notes whose UpdatedAt
// is strictly after this cutoff. Pair with a "last seen 30 days
// ago" cutoff to prune stale knowledge from the loader prompt
// without deleting it from storage.
UpdatedAfter time.Time
}
ListOpts narrows a Store.List read. Zero values disable each filter, so the empty struct is equivalent to "everything for this scope".
Limit + UpdatedAfter together let the loader pull only the most recent N notes — bounded both by count and by recency so a long-dead scope can't dominate a fresh one.
type Note ¶
type Note struct {
// Key is the stable identifier within (scope, key). Re-Putting an
// existing key updates the note in place; pick keys that describe the
// fact (e.g. "jira.default_workspace") rather than session-derived
// IDs (e.g. "fact_2026_05_20") so updates collapse instead of
// piling new rows on top of stale ones.
Key string `json:"key"`
// Content is the note body. Plain text, kept short.
Content string `json:"content"`
// Tags is an optional taxonomy (e.g. "preference", "fact", "mistake").
// ListOpts.Tags filters on this; the loader treats tags as
// observational only.
Tags []string `json:"tags,omitempty"`
// CreatedAt is stamped on first Put and preserved across updates.
CreatedAt time.Time `json:"created_at"`
// UpdatedAt is stamped on every Put. Stores return List ordered by
// UpdatedAt descending so the most recent knowledge appears first
// and per-scope eviction targets the oldest entries.
UpdatedAt time.Time `json:"updated_at"`
}
Note is a single piece of distilled knowledge persisted across sessions.
Keep Content short — a one-line fact, preference, or correction. The agent loader concatenates notes into the system prompt, so token cost scales linearly with the per-scope note count up to the loader's budget cap. Store implementations enforce a hard per-scope cap on top of the loader cap to prevent runaway growth from undisciplined callers.
type Store ¶
type Store interface {
// Put inserts or updates a note for (scope, note.Key). The
// implementation stamps UpdatedAt; CreatedAt is preserved on
// update. Empty Key returns an error. Implementations may enforce
// a per-scope cap by evicting the least-recently-updated note when
// the cap is exceeded; that eviction is a normal Put outcome, not
// an error.
Put(ctx context.Context, scope string, note Note) error
// List returns notes under scope, ordered by UpdatedAt descending,
// filtered and bounded by opts. An unseen scope returns (nil, nil).
List(ctx context.Context, scope string, opts ListOpts) ([]Note, error)
// Delete removes one note. No-op when missing.
Delete(ctx context.Context, scope, key string) error
// ReplaceAll atomically replaces every note under scope with the
// supplied set. Used by the Consolidator's merge path so a curated
// new state lands without a window where deletions and inserts are
// observable separately. CreatedAt timestamps are preserved when an
// incoming note's Key matches an existing one; new keys get a
// fresh CreatedAt. UpdatedAt is stamped on every entry. An empty
// slice clears the scope.
ReplaceAll(ctx context.Context, scope string, notes []Note) error
}
Store is the persistence contract for cross-session memory.
Implementations must be safe for concurrent use. Backends fall into the same three buckets as pkg/history: InMem (process-local, fast, lost on restart), File (single-node persistent), and remote (DB-backed). Today only InMem ships in-tree; the others can be added without changing this interface.