memory
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.
Constants
Section titled “Constants”const ( MaxName = 64 MaxDescription = 150 MaxBody = 2000 MaxSource = 512)Limits on a note’s fields, in bytes.
const DefaultViewBytes = 8 << 10DefaultViewBytes bounds the memory view a session starts with.
const MaxFile = 8 << 10MaxFile bounds the size of a stored note a store reads; anything larger was not written by a store and is reported as a Problem.
Variables
Section titled “Variables”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.
Encode
Section titled “Encode”func Encode(n Note) []byteEncode 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.
FormatExpiry
Section titled “FormatExpiry”func FormatExpiry(t time.Time) stringFormatExpiry 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) stringLine is a note’s one-line index entry: what it is and where to find it.
Render
Section titled “Render”func Render(n Note) stringRender 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.
ValidName
Section titled “ValidName”func ValidName(name string) boolValidName 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) stringView 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.
Author
Section titled “Author”type Author stringAuthor 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.
Author.Valid
Section titled “Author.Valid”func (a Author) Valid() boolValid reports whether a is one of the authors.
ConflictError
Section titled “ConflictError”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.
ConflictError.Error
Section titled “ConflictError.Error”func (e *ConflictError) Error() stringConflictError.Unwrap
Section titled “ConflictError.Unwrap”func (e *ConflictError) Unwrap() errorUnwrap 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) NoteParse 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.
Search
Section titled “Search”func Search(notes []Note, query string, now time.Time, maxBytes int) []NoteSearch 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.
Note.Active
Section titled “Note.Active”func (n Note) Active(now time.Time) boolActive reports whether n is retrieved at now: readable and not expired.
Note.Check
Section titled “Note.Check”func (n Note) Check() errorCheck reports why n can’t be saved, wrapping ErrInvalid, or nil. It checks what the writer supplies, not what the store assigns.
Note.Expired
Section titled “Note.Expired”func (n Note) Expired(now time.Time) boolExpired 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.
Revision
Section titled “Revision”type Revision stringRevision 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.
RevisionOf
Section titled “RevisionOf”func RevisionOf(data []byte) RevisionRevisionOf is the revision of a note stored as data.
type Scope stringScope 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.
Project
Section titled “Project”func Project(id string) ScopeProject 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.
Scope.Label
Section titled “Scope.Label”func (s Scope) Label() stringLabel is how the scope is named to the model and the user: “project” or “user”.
Scope.Valid
Section titled “Scope.Valid”func (s Scope) Valid() boolValid 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 stringType 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).
Type.Rule
Section titled “Type.Rule”func (t Type) Rule() boolRule 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.
Type.Valid
Section titled “Type.Valid”func (t Type) Valid() boolValid reports whether t is one of the types.