Documentation
¶
Overview ¶
Package memory provides optional, bank-scoped long-term agent memory. Run checkpoints remain the runtime store's responsibility.
Index ¶
- Constants
- Variables
- func BankName(scope Scope) string
- func FactEmbeddingText(fact ExtractedFact, mentionedAt time.Time) string
- func KeywordText(fact Fact) string
- func PruneCheckpoints(ctx context.Context, store CheckpointStore, keep int) error
- func RestoreCheckpoint(ctx context.Context, store CheckpointStore, checkpointID, destination string, ...) error
- func RestoreEncryptedCheckpoint(ctx context.Context, input io.Reader, destination string, maxBytes int64, ...) (err error)
- type Backend
- type CausalLink
- type Checkpoint
- type CheckpointAuthority
- type CheckpointStore
- type CheckpointURL
- type ConsolidationAction
- type ConsolidationDelete
- type ConsolidationPlan
- type ConsolidationResult
- type Consolidator
- type Embedder
- type ExtractedChunk
- type ExtractedFact
- type Extractor
- type Fact
- type FactEmbedder
- type Hindsight
- type MentalModel
- type Observation
- type ObservationReconciler
- type ObservationReconciliation
- type PresignedCheckpointStore
- func (s *PresignedCheckpointStore) Delete(ctx context.Context, id string) error
- func (s *PresignedCheckpointStore) List(ctx context.Context) ([]Checkpoint, error)
- func (s *PresignedCheckpointStore) Open(ctx context.Context, id string) (io.ReadCloser, error)
- func (s *PresignedCheckpointStore) Put(ctx context.Context, checkpoint Checkpoint, body io.Reader) error
- type Provider
- func (p *Provider) Consolidate(ctx context.Context, facts []Fact, observations []Observation) (ConsolidationPlan, error)
- func (p *Provider) Embed(ctx context.Context, texts []string) ([][]float32, error)
- func (p *Provider) EmbedFacts(ctx context.Context, facts []ExtractedFact, mentionedAt time.Time) ([][]float32, error)
- func (p *Provider) Extract(ctx context.Context, req RetainRequest) ([]ExtractedFact, error)
- func (p *Provider) ModelID() string
- func (p *Provider) ReconcileObservations(ctx context.Context, newText, existing string) (ObservationReconciliation, error)
- func (p *Provider) ReflectionStep(ctx context.Context, messages []ReflectionMessage, forcedTool string) (ReflectionMessage, error)
- func (p *Provider) Rerank(ctx context.Context, query string, documents []string) ([]float64, error)
- type ProviderConfig
- type RecallRequest
- type RecallResult
- type ReflectBackend
- type ReflectRequest
- type ReflectResult
- type ReflectionMessage
- type ReflectionModel
- type ReflectionToolCall
- type Reranker
- type Result
- type RetainRequest
- type RetainResult
- type SQLite
- func (s *SQLite) Close() error
- func (s *SQLite) Consolidate(ctx context.Context, scope Scope, model Consolidator, limit int) (out ConsolidationResult, err error)
- func (s *SQLite) ConsolidateMemory(ctx context.Context, scope Scope, limit int) (ConsolidationResult, error)
- func (s *SQLite) CreateMentalModel(ctx context.Context, scope Scope, id, name, query string) error
- func (s *SQLite) EncryptedSnapshot(ctx context.Context, destination string, recipients ...age.Recipient) (err error)
- func (s *SQLite) Forget(ctx context.Context, scope Scope, documentID string) error
- func (s *SQLite) InvalidateDocument(ctx context.Context, scope Scope, documentID string) error
- func (s *SQLite) ReadMentalModel(ctx context.Context, scope Scope, id string) (MentalModel, error)
- func (s *SQLite) Recall(ctx context.Context, scope Scope, req RecallRequest) (RecallResult, error)
- func (s *SQLite) Reflect(ctx context.Context, scope Scope, req ReflectRequest, model ReflectionModel) (ReflectResult, error)
- func (s *SQLite) ReflectMemory(ctx context.Context, scope Scope, req ReflectRequest) (ReflectResult, error)
- func (s *SQLite) RefreshMentalModel(ctx context.Context, scope Scope, id string, model ReflectionModel) (MentalModel, error)
- func (s *SQLite) Retain(ctx context.Context, scope Scope, req RetainRequest) (RetainResult, error)
- func (s *SQLite) SaveCheckpoint(ctx context.Context, store CheckpointStore, recipients ...age.Recipient) (checkpoint Checkpoint, err error)
- func (s *SQLite) SearchMentalModels(ctx context.Context, scope Scope, query string, limit int) ([]MentalModel, error)
- func (s *SQLite) SearchObservations(ctx context.Context, scope Scope, query string, limit int) ([]Observation, error)
- func (s *SQLite) Snapshot(ctx context.Context, destination string) error
- type SQLiteConfig
- type Scope
- type SourceChunk
- type TemporalWindow
- type TokenCounter
Constants ¶
const UpstreamCommit = "017b3f5d888d67341e70f43c102cb8155bb9f51e"
Variables ¶
var ( ErrConflict = errors.New("memory document changed during retain") ErrForgotten = errors.New("memory document was forgotten; use a new document ID to reimport") )
Functions ¶
func BankName ¶
BankName maps the complete host scope to an upstream bank, preventing two apps with the same human-readable bank ID sharing memory accidentally.
func FactEmbeddingText ¶
func FactEmbeddingText(fact ExtractedFact, mentionedAt time.Time) string
FactEmbeddingText renders the pinned upstream fact embedding input.
func KeywordText ¶
KeywordText mirrors upstream's text/context/entity/date full-text inputs. SQLite's tokenizer and BM25 score remain different from PostgreSQL ranking.
func PruneCheckpoints ¶
func PruneCheckpoints(ctx context.Context, store CheckpointStore, keep int) error
PruneCheckpoints retains the newest keep checkpoints. Call with keep=1 after a successful post-forget upload to remove older snapshots containing forgotten facts. Provider implementations must actually purge object versions.
func RestoreCheckpoint ¶
func RestoreEncryptedCheckpoint ¶
func RestoreEncryptedCheckpoint(ctx context.Context, input io.Reader, destination string, maxBytes int64, identities ...age.Identity) (err error)
RestoreEncryptedCheckpoint authenticates and validates a checkpoint before creating a standalone SQLite file. It never overwrites an existing database. maxBytes bounds plaintext output; zero defaults to 1 GiB.
Types ¶
type Backend ¶
type Backend interface {
Retain(context.Context, Scope, RetainRequest) (RetainResult, error)
Recall(context.Context, Scope, RecallRequest) (RecallResult, error)
Forget(context.Context, Scope, string) error
}
type CausalLink ¶
type Checkpoint ¶
type CheckpointAuthority ¶
type CheckpointAuthority interface {
UploadURL(context.Context, Checkpoint) (CheckpointURL, error)
DownloadURL(context.Context, string) (CheckpointURL, error)
List(context.Context) ([]Checkpoint, error)
Delete(context.Context, string) error
}
CheckpointAuthority authorizes one profile's storage operations. Implement it using the desktop host's authenticated backup API or cloud SDK. Signed URLs work with S3-compatible storage without embedding cloud credentials here. List/Delete include the host's checkpoint index and object-version policy.
type CheckpointStore ¶
type CheckpointStore interface {
Put(context.Context, Checkpoint, io.Reader) error
Open(context.Context, string) (io.ReadCloser, error)
List(context.Context) ([]Checkpoint, error)
Delete(context.Context, string) error
}
CheckpointStore is selected and authorized by the desktop host for one profile. Implementations upload opaque encrypted bytes. Delete must remove the named checkpoint's historical object versions too when used for forgetting history.
type CheckpointURL ¶
CheckpointURL carries an opaque host-issued signed PUT/GET URL. Never persist or log these URLs: they may contain temporary cloud access credentials.
type ConsolidationAction ¶
type ConsolidationDelete ¶
type ConsolidationPlan ¶
type ConsolidationPlan struct {
Creates []ConsolidationAction `json:"creates"`
Updates []ConsolidationAction `json:"updates"`
Deletes []ConsolidationDelete `json:"deletes"`
}
type ConsolidationResult ¶
type Consolidator ¶
type Consolidator interface {
Consolidate(context.Context, []Fact, []Observation) (ConsolidationPlan, error)
}
type ExtractedChunk ¶
type ExtractedFact ¶
type ExtractedFact struct {
// Metadata is supplied by the extractor; document metadata takes precedence.
Metadata map[string]string `json:"metadata,omitempty"`
SourceChunk *ExtractedChunk `json:"source_chunk,omitempty"`
Text string `json:"text"`
Where string `json:"where,omitempty"`
Type string `json:"type"`
Entities []string `json:"entities"`
OccurredStart *time.Time `json:"occurred_start,omitempty"`
OccurredEnd *time.Time `json:"occurred_end,omitempty"`
CausalRelations []CausalLink `json:"causal_relations,omitempty"`
}
type Extractor ¶
type Extractor interface {
Extract(context.Context, RetainRequest) ([]ExtractedFact, error)
}
type Fact ¶
type Fact struct {
ID string `json:"id"`
ChunkID string `json:"chunk_id,omitempty"`
DocumentID string `json:"document_id"`
Text string `json:"text"`
Type string `json:"type"`
Entities []string `json:"entities"`
Context string `json:"context,omitempty"`
MentionedAt time.Time `json:"mentioned_at"`
Where string `json:"where,omitempty"`
OccurredStart *time.Time `json:"occurred_start,omitempty"`
OccurredEnd *time.Time `json:"occurred_end,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
type FactEmbedder ¶
type FactEmbedder interface {
EmbedFacts(context.Context, []ExtractedFact, time.Time) ([][]float32, error)
}
FactEmbedder optionally supplies upstream fact-specific date/entity augmentation.
type Hindsight ¶
type Hindsight struct {
// contains filtered or unexported fields
}
Hindsight is the reference backend, using the upstream HTTP API without requiring the Python service in the local implementation's dependency graph.
func NewHindsight ¶
func (*Hindsight) Recall ¶
func (h *Hindsight) Recall(ctx context.Context, scope Scope, req RecallRequest) (RecallResult, error)
func (*Hindsight) Retain ¶
func (h *Hindsight) Retain(ctx context.Context, scope Scope, req RetainRequest) (RetainResult, error)
type MentalModel ¶
type Observation ¶
type Observation struct {
ID string `json:"id"`
Text string `json:"text"`
SourceFactIDs []string `json:"source_fact_ids"`
ProofCount int `json:"proof_count"`
UpdatedAt time.Time `json:"updated_at"`
IsStale bool `json:"is_stale"`
MentionedAt time.Time `json:"mentioned_at"`
OccurredStart *time.Time `json:"occurred_start,omitempty"`
OccurredEnd *time.Time `json:"occurred_end,omitempty"`
SourceMemories []Fact `json:"source_memories,omitempty"`
}
Observation is derived evidence. SourceFactIDs are mandatory provenance, not model-selected permissions. Any changed/forgotten proof invalidates the text.
type ObservationReconciler ¶
type PresignedCheckpointStore ¶
type PresignedCheckpointStore struct {
// contains filtered or unexported fields
}
func NewPresignedCheckpointStore ¶
func NewPresignedCheckpointStore(authority CheckpointAuthority, client *http.Client) (*PresignedCheckpointStore, error)
func (*PresignedCheckpointStore) Delete ¶
func (s *PresignedCheckpointStore) Delete(ctx context.Context, id string) error
func (*PresignedCheckpointStore) List ¶
func (s *PresignedCheckpointStore) List(ctx context.Context) ([]Checkpoint, error)
func (*PresignedCheckpointStore) Open ¶
func (s *PresignedCheckpointStore) Open(ctx context.Context, id string) (io.ReadCloser, error)
func (*PresignedCheckpointStore) Put ¶
func (s *PresignedCheckpointStore) Put(ctx context.Context, checkpoint Checkpoint, body io.Reader) error
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider implements extraction/embeddings using OpenAI-compatible endpoints, and optional cross-encoder reranking using the /rerank endpoint used by vLLM and other local model servers. No inference process is started automatically.
func NewProvider ¶
func NewProvider(cfg ProviderConfig) (*Provider, error)
func (*Provider) Consolidate ¶
func (p *Provider) Consolidate(ctx context.Context, facts []Fact, observations []Observation) (ConsolidationPlan, error)
Consolidate uses the pinned default mission/language and the configured local extraction model. Host-controlled nondefault missions are not supported yet.
func (*Provider) EmbedFacts ¶
func (p *Provider) EmbedFacts(ctx context.Context, facts []ExtractedFact, mentionedAt time.Time) ([][]float32, error)
EmbedFacts mirrors upstream augment_texts_with_dates. Queries use Embed directly.
func (*Provider) Extract ¶
func (p *Provider) Extract(ctx context.Context, req RetainRequest) ([]ExtractedFact, error)
func (*Provider) ReconcileObservations ¶
func (*Provider) ReflectionStep ¶
func (p *Provider) ReflectionStep(ctx context.Context, messages []ReflectionMessage, forcedTool string) (ReflectionMessage, error)
type ProviderConfig ¶
type ProviderConfig struct {
BaseURL string
APIKey string
ExtractionModel string
EmbeddingModel string
EmbeddingModelID string
EmbeddingBaseURL string
EmbeddingAPIKey string
EmbeddingDimensions int
RerankModel string
Client *http.Client
// ExtractionOptions supplies provider-specific sampling/reasoning controls.
// Core model, messages and response schema cannot be overridden.
ExtractionOptions map[string]any
}
type RecallRequest ¶
type RecallRequest struct {
Query string `json:"query"`
IncludeChunks bool `json:"include_chunks,omitempty"`
MaxChunkTokens int `json:"max_chunk_tokens,omitempty"`
Limit int `json:"limit,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Types []string `json:"types,omitempty"`
QueryTimestamp time.Time `json:"query_timestamp,omitempty"`
TemporalWindow *TemporalWindow `json:"temporal_window,omitempty"`
}
type RecallResult ¶
type RecallResult struct {
Results []Result `json:"results"`
Chunks map[string]SourceChunk `json:"chunks,omitempty"`
EstimatedChunkTokens int `json:"estimated_chunk_tokens,omitempty"`
// EstimatedTokens includes JSON result framing. A caller may supply an exact
// tokenizer; the default uses Hindsight's o200k_base encoding.
EstimatedTokens int `json:"estimated_tokens"`
// Nil means the reference server did not expose its reranking configuration.
Reranked *bool `json:"reranked,omitempty"`
}
type ReflectBackend ¶
type ReflectBackend interface {
ReflectMemory(context.Context, Scope, ReflectRequest) (ReflectResult, error)
}
type ReflectRequest ¶
type ReflectResult ¶
type ReflectionMessage ¶
type ReflectionMessage struct {
Role string `json:"role"`
Content string `json:"content,omitempty"`
ToolCalls []ReflectionToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
type ReflectionModel ¶
type ReflectionModel interface {
ReflectionStep(context.Context, []ReflectionMessage, string) (ReflectionMessage, error)
}
type ReflectionToolCall ¶
type RetainRequest ¶
type RetainRequest struct {
DocumentID string `json:"document_id"`
Content string `json:"content"`
Context string `json:"context,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
RetainRequest replaces one source document atomically. DocumentID is a stable host/source identifier, not a random ID generated for each retry.
type RetainResult ¶
type SQLite ¶
type SQLite struct {
// contains filtered or unexported fields
}
func OpenSQLite ¶
func OpenSQLite(ctx context.Context, cfg SQLiteConfig) (*SQLite, error)
OpenSQLite uses the runtime CLI's existing pure-Go SQLite driver. Vector similarity is exact and computed in Go; no extension or embedding daemon is required by storage itself. Inference is supplied through the model interfaces.
func (*SQLite) Consolidate ¶
func (s *SQLite) Consolidate(ctx context.Context, scope Scope, model Consolidator, limit int) (out ConsolidationResult, err error)
Consolidate processes a bounded batch of previously unprocessed facts. The host schedules this operation; retain never starts background model calls. All inference finishes outside transactions. A bank-state CAS prevents stale inference from restoring information after source replacement or forgetting.
func (*SQLite) ConsolidateMemory ¶
func (s *SQLite) ConsolidateMemory(ctx context.Context, scope Scope, limit int) (ConsolidationResult, error)
ConsolidateMemory uses the configured provider, letting a host schedule work without extracting model credentials from the initialized backend.
func (*SQLite) CreateMentalModel ¶
CreateMentalModel registers a host-curated question, then requires an explicit RefreshMentalModel call to derive its content. IDs and scope are host-selected.
func (*SQLite) EncryptedSnapshot ¶
func (s *SQLite) EncryptedSnapshot(ctx context.Context, destination string, recipients ...age.Recipient) (err error)
EncryptedSnapshot creates a consistent age-encrypted checkpoint for upload. Recipients are selected by the host; private identities never leave the device. The destination must not exist. Plaintext temporary files are removed on return.
func (*SQLite) Forget ¶
Forget removes the document and all its facts, indexes, and graph links. A tombstone prevents an in-flight extraction or a retried import resurrecting it. Older snapshots must also be expired by the application's backup policy.
func (*SQLite) InvalidateDocument ¶ added in v0.13.13
InvalidateDocument removes derived facts and observations after a source revision changes. It preserves the recorded source and any forgetting tombstone.
func (*SQLite) ReadMentalModel ¶
func (*SQLite) Recall ¶
func (s *SQLite) Recall(ctx context.Context, scope Scope, req RecallRequest) (RecallResult, error)
func (*SQLite) Reflect ¶
func (s *SQLite) Reflect(ctx context.Context, scope Scope, req ReflectRequest, model ReflectionModel) (ReflectResult, error)
Reflect runs a bounded native tool-calling loop over derived observations and raw facts. It performs no writes. Directives are supplied by the host.
func (*SQLite) ReflectMemory ¶
func (s *SQLite) ReflectMemory(ctx context.Context, scope Scope, req ReflectRequest) (ReflectResult, error)
func (*SQLite) RefreshMentalModel ¶
func (s *SQLite) RefreshMentalModel(ctx context.Context, scope Scope, id string, model ReflectionModel) (MentalModel, error)
func (*SQLite) Retain ¶
func (s *SQLite) Retain(ctx context.Context, scope Scope, req RetainRequest) (RetainResult, error)
func (*SQLite) SaveCheckpoint ¶
func (s *SQLite) SaveCheckpoint(ctx context.Context, store CheckpointStore, recipients ...age.Recipient) (checkpoint Checkpoint, err error)
SaveCheckpoint encrypts locally before the store receives any bytes. It returns the checkpoint identity even when upload fails, for reconciliation.
func (*SQLite) SearchMentalModels ¶
func (*SQLite) SearchObservations ¶
func (s *SQLite) SearchObservations(ctx context.Context, scope Scope, query string, limit int) ([]Observation, error)
SearchObservations returns derived evidence with its complete proof IDs. This search fuses semantic, keyword, proof-link and temporal evidence, then applies the same combined scoring as raw recall plus the upstream proof-count boost.
type SQLiteConfig ¶
type SQLiteConfig struct {
Path string
Extractor Extractor
Embedder Embedder
Reranker Reranker
CountTokens TokenCounter
// MaxObservations bounds each bank; zero leaves it unlimited.
MaxObservations int
// CandidateLimit bounds each retrieval arm before RRF (default 100).
CandidateLimit int
// RerankerMaxCandidates caps the fused pool before reranking and combined
// scoring, matching Hindsight's default of 300. It is independent of the
// per-arm retrieval limit: changing it also changes rank normalization.
RerankerMaxCandidates int
}
type SourceChunk ¶
type TemporalWindow ¶
TemporalWindow boosts the temporal retrieval arm. It is not a hard filter on semantic, keyword, or graph results, matching Hindsight's API semantics.
type TokenCounter ¶
Source Files
¶
- checkpoint.go
- checkpoint_http.go
- checkpoint_store.go
- chunking.go
- chunks.go
- consolidation.go
- consolidation_dedup.go
- consolidation_provider.go
- extraction_dates.go
- hindsight.go
- keyword_text.go
- links.go
- memory.go
- mental_models.go
- observations.go
- provider.go
- recall.go
- reflect.go
- retry_split.go
- scoring.go
- sqlite.go
- temporal.go
- tokens.go