recall

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Recall

Typed, durable agent memory. An append-only log of what an agent was told, and a fold that turns it into what the agent believes, now or at any past instant.

Recall is the memory layer for Polign, but it depends on no particular database: it needs a VectorDB you provide, so it runs against polign_db, a test fake, or anything else with the same four operations.

go get github.com/Polign/recall

What it does differently

Most "AI memory" puts text in a vector store and hopes similarity search finds it again. Recall treats memory as what it actually is: state, with rules.

Every memory is a typed record. Predicates come from a closed registry that declares each one's cardinality and value type. An agent cannot invent editor_preference alongside prefers_editor and split one fact across two names that never meet.

kind:        preference
subject:     user
predicate:   prefers_editor
value:       neovim
confidence:  1.0
source:      user_stated
observed_at: 2026-09-12T04:31:07.000000000Z

Supersession is deterministic. A single-valued predicate holds one value: a newer statement replaces an older one. A multi-valued predicate accumulates. Which happens is decided by the registry, never by model judgment.

Beliefs are derived, never mutated. The log is the truth, and Fold is the only thing that reads it. Nothing is rewritten when a belief changes, so no crash between two writes can leave an agent believing two contradictory things at once.

History is queryable. Forgetting is a retraction event, not a deletion, so asking what the agent believed last Tuesday still works after Wednesday's correction.

Using it

registry, err := recall.LoadRegistry(predicatesJSON)
store := recall.NewStore(db, "memories", registry, embed)

store.Remember("preference", "user", "prefers_editor", "neovim", 1.0, "user_stated")

// What is believed now.
beliefs, err := store.Recall(recall.Query{Subject: "user", Predicate: "prefers_editor"})

// What was believed on Tuesday. Same call, one more field.
beliefs, err = store.Recall(recall.Query{
    Subject:   "user",
    Predicate: "prefers_editor",
    AsOf:      tuesday,
})

// Semantic recall, folded like every other read, so a superseded statement
// ranking top of the search still cannot come back as a live belief.
beliefs, err = store.Recall(recall.Query{Text: "which editor do I use"})

// Every event behind a belief, oldest first: the audit trail.
events, err := store.History("user", "prefers_editor")

Remember writes at most one record, and only after folding what is already there. Restating a belief that already holds writes nothing at all.

The registry

{
  "prefers_editor": {
    "cardinality": "single",
    "value_type": "string",
    "description": "the editor the user works in"
  },
  "likes": {
    "cardinality": "multi",
    "value_type": "string",
    "description": "things the user likes"
  }
}

cardinality is single or multi. value_type is string, number, or boolean, and values are stored with that type, so a number compares numerically in a filter instead of lexically.

Registry.PromptTable() renders the registry for an agent's system prompt.

Embeddings

Recall does not embed anything. You pass an embed func(string) []float32, and whatever produced the stored vectors has to produce the query vectors too.

License

Apache 2.0. See LICENSE.

Documentation

Overview

Package recall is the typed agent-memory layer: a closed registry of predicates, an append-only event log per subject, and the fold that turns that log into what the agent believes, now or at any past instant.

The central decision is that a belief is derived, never mutated. An earlier design flipped a record's status to "superseded" in a second write after storing its replacement, which left two live beliefs for a single-valued predicate whenever a process died between the two. Here the log is the truth and Fold is the only reader of it, so no interleaving of writes can produce a state Fold reads wrongly. "What does the agent believe" and "what did it believe on Tuesday" become the same call with a different ceiling.

Retraction is likewise an event, not a deletion. Forgetting a fact on Wednesday must not change what the agent believed on Tuesday, and a log that erases its own history cannot answer that.

Index

Constants

View Source
const MaxExport = 10000

MaxExport bounds an unbounded export, so that asking for everything cannot turn into an unbounded scan by accident.

Variables

This section is empty.

Functions

func Digest added in v0.2.0

func Digest(events []Event) string

Digest is a stable checksum over a log.

It lets a reviewer confirm that an export they were handed is the one the store produced, and lets two parties compare logs without exchanging them. The input is each event's canonical fields in a fixed order, so the digest depends on the content of the log and not on how it was serialised, paged, or which node answered.

func SortEvents added in v0.2.0

func SortEvents(events []Event)

SortEvents orders a log oldest first, breaking ties on id so that two exports of the same events are byte-identical.

func ValueKey

func ValueKey(v any) string

ValueKey renders a value in the canonical form used to tell two values apart. Strings fold case, so "Neovim" stated twice with different capitals is one belief rather than two; the event keeps whatever form was written, and only identity is case-insensitive. Numbers render without exponent drift so that 8000 and 8000.0 are one value rather than two.

Types

type Belief

type Belief struct {
	Subject    string  `json:"subject"`
	Predicate  string  `json:"predicate"`
	Value      any     `json:"value"`
	Confidence float64 `json:"confidence"`
	Source     string  `json:"source"`
	Kind       string  `json:"kind"`
	// ObservedAt is when the surviving event was observed, which is what
	// "since when has this been true" means.
	ObservedAt time.Time `json:"observed_at"`
	// EventID identifies the event this belief came from, so a caller can
	// fetch its full history or cite it.
	EventID string `json:"event_id"`
}

Belief is a statement that holds at some instant: the result of folding a log, not a stored row.

func Fold

func Fold(events []Event, card Cardinality, asOf time.Time) []Belief

Fold reduces one subject-and-predicate's events to the beliefs that hold at asOf. A zero asOf means now.

Events may arrive in any order and may repeat; the fold is defined by the log's content alone, so a partially applied write, a retried write, or a clock that stepped backwards changes the answer only in so far as it changed the log.

type Cardinality

type Cardinality string

Cardinality decides what a second value for the same subject and predicate means.

const (
	// Single means a newer value replaces the older one: an agent has one
	// preferred editor.
	Single Cardinality = "single"
	// Multi means values accumulate: an agent may like many languages.
	Multi Cardinality = "multi"
)

type Event

type Event struct {
	// ID is the event's stable identity, derived from its content and
	// instant so that replaying the same write is a no-op.
	ID string `json:"id"`
	// Kind is "fact" or "preference".
	Kind string `json:"kind"`
	// Subject is who or what the statement is about, lowercased.
	Subject string `json:"subject"`
	// Predicate names the relation, and selects the cardinality that decides
	// how this event interacts with its neighbours.
	Predicate string `json:"predicate"`
	// Value holds the predicate's declared type: string, float64, or bool.
	// It is nil on a retraction that clears every value for the pair.
	Value any `json:"value"`
	// Confidence is in [0, 1].
	Confidence float64 `json:"confidence"`
	// Source is how the statement was come by: user_stated, agent_inferred,
	// or tool_result.
	Source string `json:"source"`
	// Retraction marks this event as withdrawing a belief rather than
	// asserting one. With Value set it withdraws that one value; with Value
	// nil it withdraws every value for the pair.
	Retraction bool `json:"retraction,omitempty"`
	// ObservedAt is when the statement was made. It orders the log, so it is
	// the one field Fold cannot do without.
	ObservedAt time.Time `json:"observed_at"`
}

Event is one durable statement about a subject, or the retraction of one. Events are immutable once written: a correction is a later event, never an edit of an earlier one.

func EventFromMetadata

func EventFromMetadata(id string, m map[string]any) Event

EventFromMetadata reads an event back. A record whose metadata is missing or malformed comes back with a zero ObservedAt, which the fold discards rather than letting it sort ahead of real events.

func (Event) Metadata

func (e Event) Metadata() map[string]any

Metadata flattens an event to typed metadata. Confidence stays a number so range filters compare numerically rather than lexically, and the instant is stored twice: once human readable and lexically sortable, once as milliseconds for numeric range filters.

func (Event) Text

func (e Event) Text() string

Text is the natural-language rendering an event is embedded from, so that semantic recall searches the same statements exact recall filters.

type Export added in v0.2.0

type Export struct {
	Subject   string
	Predicate string
	// Limit bounds how many events are read. Zero uses MaxExport.
	Limit int
}

Export selects part of the log for audit. An empty field means "every one of these": zero Export exports everything the store holds.

type Hit

type Hit struct {
	ID       string
	Distance float32
	Score    float32
	Metadata map[string]any
}

Hit is one search result.

type Predicate

type Predicate struct {
	Cardinality string `json:"cardinality"`
	ValueType   string `json:"value_type"`
	Description string `json:"description"`
}

Predicate is one registry entry. Cardinality decides what a second value for the same subject means: "single" makes a newer value supersede the old one, "multi" makes it an additional fact. ValueType decides what a value IS: a string, a number, or a boolean. Values are stored with that type, so a number compares numerically in recall filters instead of lexically.

func (Predicate) Cardinal

func (p Predicate) Cardinal() Cardinality

Cardinal returns the predicate's cardinality as the fold understands it.

type Query

type Query struct {
	// Text runs a semantic search. Empty means an exact, filtered read.
	Text      string
	Subject   string
	Predicate string
	Kind      string
	// AsOf answers as of a past instant. Zero means now, so "what is believed"
	// and "what was believed on Tuesday" are the same call.
	AsOf          time.Time
	MinConfidence float64
	Limit         int
	// ValueMin and ValueMax bound number-typed values.
	ValueMin *float64
	ValueMax *float64
}

Query is one read. With Text set the read is semantic (the text is embedded and searched); without it it is an exact filtered read. Both modes answer with beliefs folded from the same log, so semantic recall can never return something exact recall would say is no longer believed.

type Registry

type Registry map[string]Predicate

Registry is the closed set of predicates the store accepts. A write with an unregistered predicate is rejected, so an agent cannot invent near-duplicate predicates ("editor_preference" beside "prefers_editor") and split one fact across two names that never meet.

func LoadRegistry

func LoadRegistry(raw []byte) (Registry, error)

LoadRegistry parses and validates a registry document.

func (Registry) Names

func (r Registry) Names() []string

Names returns the registered predicates sorted, for error messages and the system prompt.

func (Registry) PromptTable

func (r Registry) PromptTable() string

PromptTable renders the registry for an agent's system prompt.

type RememberResult

type RememberResult struct {
	Stored     Belief   `json:"stored"`
	Existing   bool     `json:"already_known,omitempty"`
	Superseded []Belief `json:"superseded,omitempty"`
}

RememberResult is what a write reports back: the belief that now holds, whether it already held, and anything it displaced.

type Store

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

Store turns writes into durable, typed events and reads into beliefs. It holds no state: every answer is folded from the log at read time, so two processes sharing a collection cannot disagree about what is believed.

func NewStore

func NewStore(db VectorDB, collection string, registry Registry, embed func(string) []float32) *Store

NewStore returns a store over one collection.

func (*Store) ExportEvents added in v0.2.0

func (s *Store) ExportEvents(q Export) ([]Event, error)

ExportEvents returns the raw event log, oldest first.

This is the audit primitive. A belief is a pure function of these events, so a reviewer given this log can re-derive every answer the store ever gave, including the ones it no longer gives: superseded statements and retracted ones are here too, because nothing is deleted.

func (*Store) Forget

func (s *Store) Forget(subject, predicate, value string) (int, error)

Forget withdraws a belief by appending a retraction.

It is a write, never a delete. Forgetting on Wednesday must not change what the agent believed on Tuesday, and a log that erased its own history could not answer that. With value set only that value is withdrawn; without it every value for the pair is.

func (*Store) History

func (s *Store) History(subject, predicate string) ([]Event, error)

History returns every event recorded for one subject and predicate, oldest first. It is the audit primitive: the fold is a pure function of exactly this, so anything the store answered can be re-derived from it.

func (*Store) Recall

func (s *Store) Recall(q Query) ([]Belief, error)

Recall returns the beliefs that hold at the query's instant.

func (*Store) Registry

func (s *Store) Registry() Registry

Registry returns the predicates this store accepts.

func (*Store) Remember

func (s *Store) Remember(kind, subject, predicate string, value any, confidence float64, source string) (RememberResult, error)

Remember records one statement.

It writes at most one event, and only after folding the existing log: if the statement already holds, nothing is written at all. Nothing is ever rewritten, so there is no window in which a crash can leave two live beliefs for a single-valued predicate.

type StoredVector

type StoredVector struct {
	ID       string
	Values   []float32
	Metadata map[string]any
}

StoredVector is one record as the database returns it.

type VectorDB

type VectorDB interface {
	Put(collection, id string, values []float32, metadata map[string]any) error
	List(collection string, filter map[string]any, limit int) ([]StoredVector, int, error)
	Search(collection string, values []float32, k int, filter map[string]any) ([]Hit, error)
}

VectorDB is the database surface the store needs. Keeping it an interface is what lets the memory layer live outside the database it usually runs against, and lets tests exercise the semantics without one.

Jump to

Keyboard shortcuts

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