Help us shape the future of Go! Take the 2026 Go Developer Survey

memory

package
v2.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package memory is X-Ray's memory store, run inside your own process.

It is the same store the X-Ray server uses: typed items with a scope and a subject, a keyword index, an HNSW index over embeddings when you give it an embedding endpoint, near-duplicate merging on write, recall shaping, and provenance, where an item a person wrote is never replaced by one a model wrote. There is no server, no Kubernetes and no network except the embedding endpoint you choose.

store, err := memory.New(ctx,
	memory.WithEmbeddings("http://localhost:8090", "nomic", 256),
	memory.WithFile("agent-memory.json"),
)
if err != nil { ... }
defer store.Close()

store.Upsert(ctx, xray.Write{
	Kind: xray.KindStableFact, Scope: xray.ScopePublic, Confidence: "high",
	Title: "Checkout retries on 503", Content: "Three times, with jitter.",
})
items, _ := store.Search(ctx, "payment retries", xray.WithLimit(5))

Store implements xray.Memory, the interface the remote xray.Client also implements. An agent written against it runs in process during development and against a shared X-Ray in production without a change.

With no embedding endpoint search is by keyword and trigram only. With WithFile the store loads the file on start and writes it after every change, so memory survives a restart; without it memory lives until the process ends.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*config)

Option configures New.

func WithEmbeddings

func WithEmbeddings(endpoint, model string, dims int) Option

WithEmbeddings turns on semantic search. endpoint is an OpenAI-compatible /v1/embeddings server (llama.cpp, Ollama, vLLM, LM Studio or a hosted one), model its model name and dims the vector size to truncate to (256 suits nomic). Items are embedded when written, so a write waits for the endpoint.

func WithFile

func WithFile(path string) Option

WithFile keeps the store in a JSON file: loaded on New, rewritten after every change. The file holds every item, private ones included, so give it the permissions you would give the data (it is written 0600).

func WithMaxInputChars

func WithMaxInputChars(n int) Option

WithMaxInputChars caps the text sent to the embedder per item. The default is 8192, which fits a 2048-token model.

func WithOrigin

func WithOrigin(origin string) Option

WithOrigin sets who the program's writes are attributed to. The default is xray.OriginDerived: written by a model. Use xray.OriginHuman for a store that holds what a person told the agent, or xray.OriginObserved for facts the code measured. A derived write never replaces a human item.

func WithRecallShaping

func WithRecallShaping(on bool) Option

WithRecallShaping turns collapsing of near-duplicates in search results on or off (default on).

func WithWriteDedup

func WithWriteDedup(on bool) Option

WithWriteDedup turns merging of restated facts on write on or off (default on).

type Store

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

Store is an in-process X-Ray memory store. It is safe for concurrent use.

func New

func New(ctx context.Context, opts ...Option) (*Store, error)

New creates a store. With WithFile it loads the file if it exists.

func (*Store) Close

func (s *Store) Close() error

Close writes the file, if there is one. The store stays usable.

func (*Store) Consolidate

func (s *Store) Consolidate(_ context.Context, apply bool, opts ...xray.QueryOption) (*xray.ConsolidationReport, error)

Consolidate reports near-duplicate facts. With apply it retires the restatements. WithSubject restricts the sweep to one subject.

func (*Store) Context

func (s *Store) Context(_ context.Context, anchor string, maxDepth int) ([]xray.ContextItem, error)

Context follows links from an anchor (an id, a subject or part of a title) with decaying activation, and returns the anchor items and the neighbourhood they reach. maxDepth 0 uses 3, and at most 8 is followed.

func (*Store) Delete

func (s *Store) Delete(_ context.Context, id string, opts ...xray.QueryOption) error

Delete removes an item. A private item needs its subject.

func (*Store) Expire

func (s *Store) Expire(_ context.Context, id string, opts ...xray.QueryOption) error

Expire retires an item that is out of date. Use Refute when it is wrong.

func (*Store) Fetch

func (s *Store) Fetch(_ context.Context, ids []string, opts ...xray.QueryOption) ([]xray.Item, error)

Fetch returns the items with the given ids, in that order. A missing id, or a private item the subject does not own, is left out.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string, opts ...xray.QueryOption) (*xray.Item, error)

Get returns one item, or an error satisfying xray.IsNotFound.

func (*Store) List

func (s *Store) List(_ context.Context, opts ...xray.QueryOption) ([]xray.Item, error)

List browses items by kind and subject, unranked.

func (*Store) Recent

func (s *Store) Recent(_ context.Context, opts ...xray.QueryOption) ([]xray.Episode, error)

Recent returns the newest episodic memories of a subject, newest first.

func (*Store) Refute

func (s *Store) Refute(ctx context.Context, id, reason string, evidence []string, opts ...xray.QueryOption) (*xray.RefuteResult, error)

Refute retires an item that is wrong, records the reason and evidence on it, and retires every active derived item that cites it.

func (*Store) Search

func (s *Store) Search(ctx context.Context, query string, opts ...xray.QueryOption) ([]xray.Item, error)

Search ranks items by relevance to query. Without a subject only shared items are searched; a subject also reaches that subject's private items.

func (*Store) Summary

func (s *Store) Summary(context.Context) (*xray.Summary, error)

Summary counts the shared items by kind.

func (*Store) Supersede

func (s *Store) Supersede(ctx context.Context, oldID string, w xray.Write) (*xray.SupersedeResult, error)

Supersede writes w as the replacement of oldID and retires the old item. A derived replacement cannot supersede a human item.

func (*Store) Upsert

func (s *Store) Upsert(ctx context.Context, w xray.Write) (*xray.WriteResult, error)

Upsert creates an item, or updates the one named by w.ID. A write that restates a fact already held comes back with Status "merged".

Jump to

Keyboard shortcuts

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