Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/memory"

Package memory is zelk’s cross-conversation memory: short, typed notes the user or the model saves on purpose, which later sessions read.

A Note lives in a Scope, one project’s or the user’s, and has a Type that says how it is used: who the user is, how they want work done, project context the code doesn’t show, or where to look for something. A Store keeps notes with writes conditional on the Revision the writer read, so two sessions correcting one note can’t lose an update silently. View renders what a session starts with, and Search finds notes on demand.

Memory is attributed data, never policy: nothing in it reaches a permission decision, and View frames it so the model treats it that way. The design, and why it is shaped like this, is docs/design/memory.md.

The stores are memory/file, notes as markdown files a person can read and edit, and memory/mem, for tests; memory/memorytest checks both.

const (
MaxName = 64
MaxDescription = 150
MaxBody = 2000
MaxSource = 512
)

Limits on a note’s fields, in bytes.

const DefaultViewBytes = 8 << 10

DefaultViewBytes bounds the memory view a session starts with.

const MaxFile = 8 << 10

MaxFile bounds the size of a stored note a store reads; anything larger was not written by a store and is reported as a Problem.

var ErrConflict = errors.New("memory: the note changed since it was read")

ErrConflict is returned by a write whose expected revision isn’t the stored one: the note was created, changed or removed by someone else.

var ErrInvalid = errors.New("memory: invalid note")

ErrInvalid is returned for a note the store refuses to keep.

var ErrNotFound = errors.New("memory: no such note")

ErrNotFound is returned for a note the store doesn’t hold.

func Encode(n Note) []byte

Encode renders n as a stored note: front matter of one “key: value” line per field between “—” lines, then the body. It is how both stores keep notes, preserving timestamp precision in a format people can edit.

func FormatExpiry(t time.Time) string

FormatExpiry says when an expiry falls, in UTC as notes store it: the date alone for an expiry at midnight, which is what a date-only expires field means, otherwise the date and exact time. Trailing fractional zeroes are omitted without changing the deadline.

func Line(n Note) string

Line is a note’s one-line index entry: what it is and where to find it.

func Render(n Note) string

Render is a note in full, as the model reads it: its identity, so it can be corrected or forgotten, its provenance and age, then its body.

func ValidName(name string) bool

ValidName reports whether name can name a note: kebab-case of lowercase letters and digits, at most MaxName bytes.

func View(notes []Note, now time.Time, maxBytes int) string

View renders the memory view of notes at now, within maxBytes: the rules (user and feedback notes) in full, then the other notes in full, and an index line for each note that doesn’t fit in full (docs/design/memory.md). A note is shown whole while it fits because a live model answered from an indexed note’s name rather than read its corrected body. Notes in a project scope come before the user scope’s, the newest first, ties broken by name, so equal stores render alike. It is empty when no note is active.

type Author string

Author is who saved a note.

const (
// AuthorUser is the user, through a host command or by editing the file.
AuthorUser Author = "user"
// AuthorModel is the model, through its memory tools.
AuthorModel Author = "model"
// AuthorHost is the host program on its own account.
AuthorHost Author = "host"
)

The authors of notes.

func (a Author) Valid() bool

Valid reports whether a is one of the authors.

type ConflictError struct {
Current *Note
}

ConflictError is the ErrConflict of a write, with the note as it is stored now, if there is one, so the writer can read it and retry.

func (e *ConflictError) Error() string
func (e *ConflictError) Unwrap() error

Unwrap makes errors.Is(err, ErrConflict) hold.

type Note struct {
Scope Scope `json:"scope"`
// Name is the note's identity within its scope, in kebab-case.
Name string `json:"name"`
// Description is one line that says what the note is about, used to
// decide whether it is relevant.
Description string `json:"description"`
Type Type `json:"type"`
Author Author `json:"author"`
// Source says where the note came from: a session and tool call, or
// a host command.
Source string `json:"source,omitempty"`
Created time.Time `json:"created,omitzero"`
Updated time.Time `json:"updated,omitzero"`
// Expires, when set, is when the note stops being retrieved. It stays
// listed until it is renewed or forgotten.
Expires time.Time `json:"expires,omitzero"`
Body string `json:"body"`
// Revision is the stored content's identity, set by the store.
Revision Revision `json:"revision,omitempty"`
// Problem, set by the store, says why a stored note can't be read,
// such as a file edited into a shape that doesn't parse. Such a note
// is listed, so it can be fixed or forgotten, but never retrieved.
Problem string `json:"problem,omitempty"`
}

Note is one remembered note.

func Parse(scope Scope, name string, data []byte) Note

Parse reads the note scope keeps as name from data. Unknown front matter keys are ignored, so a person can annotate a file; a note without an author was written by hand and is the user’s. A note that doesn’t parse is returned with its Problem set and its revision, never as an error, so it can still be listed and forgotten.

func Search(notes []Note, query string, now time.Time, maxBytes int) []Note

Search returns the active notes that share words with query, the most shared first, ties broken as View orders notes. Words in the name, description and type count twice, as they say what the note is about. It keeps the leading notes whose renderings fit in maxBytes; maxBytes ≤ 0 keeps all.

func (n Note) Active(now time.Time) bool

Active reports whether n is retrieved at now: readable and not expired.

func (n Note) Check() error

Check reports why n can’t be saved, wrapping ErrInvalid, or nil. It checks what the writer supplies, not what the store assigns.

func (n Note) Expired(now time.Time) bool

Expired reports whether n has an expiry that now has reached. An expired note is no longer retrieved but stays listed, so it can be renewed or forgotten. A note that can’t be read has no expiry to speak of.

type Revision string

Revision identifies the stored content of a note: a digest of its file’s bytes, so any change, including an edit by hand, is a new revision.

func RevisionOf(data []byte) Revision

RevisionOf is the revision of a note stored as data.

type Scope string

Scope is where a note applies: one project, or everywhere the user works.

const User Scope = "user"

User is the scope of notes that hold in all of the user’s projects.

func Project(id string) Scope

Project returns the scope of the project the host names id. Core never discovers a project’s identity; the host chooses one that is stable for the project and distinct from every other.

func (s Scope) Label() string

Label is how the scope is named to the model and the user: “project” or “user”.

func (s Scope) Valid() bool

Valid reports whether s is User or a project scope with an ID.

type Store interface {
// List returns scope's notes, ordered by name, including those with a
// Problem.
List(ctx context.Context, scope Scope) ([]Note, error)
// Put saves n. With expect empty it creates the note, failing with a
// ConflictError if the name is taken. Otherwise it replaces the note
// whose revision is expect, keeping its creation time. Put sets the
// times and the revision, and returns the note as stored, only once it
// is durable.
Put(ctx context.Context, n Note, expect Revision) (Note, error)
// Delete removes scope's note name whose revision is expect.
Delete(ctx context.Context, scope Scope, name string, expect Revision) error
}

Store keeps notes. Writes are conditional on the revision the caller read, so concurrent corrections can’t lose one silently. A Store is safe for concurrent use, and one shared by processes serializes them too.

type Type string

Type says how a note is used.

const (
// TypeUser is who the user is: role, expertise, how they like answers.
TypeUser Type = "user"
// TypeFeedback is how the user wants work done: corrections and
// confirmed approaches, with why and how to apply.
TypeFeedback Type = "feedback"
// TypeProject is project context the code and history don't show.
TypeProject Type = "project"
// TypeReference is where to look for something outside the project.
TypeReference Type = "reference"
)

The types of note (docs/design/memory.md, Types).

func (t Type) Rule() bool

Rule reports whether notes of type t change how the agent works on every turn, and so are shown in full at the start of a session.

func (t Type) Valid() bool

Valid reports whether t is one of the types.