file

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package file provides a filesystem-backed statestore implementation.

One directory per conversation under Options.Root holds:

state.json       — atomically-rewritten snapshot of meta + metadata
messages.jsonl   — append-only message log (one types.Message per line)
summaries.jsonl  — append-only summary log
lists/<name>.jsonl — opaque-byte append-only lists for ListAccessor

The store is single-process: pointing two stores at the same Root is undefined behavior. A future revision may add an OS-level lock file.

Index

Constants

This section is empty.

Variables

View Source
var ErrStoreClosed = errors.New("filestore: store closed")

ErrStoreClosed is returned from any method called after Close.

Functions

This section is empty.

Types

type FSyncPolicy

type FSyncPolicy int

FSyncPolicy controls when on-disk writes are fsync'd.

const (
	// FSyncOff matches MemoryStore durability semantics: writes are buffered
	// by the OS and may be lost on power-loss / kernel panic.
	FSyncOff FSyncPolicy = iota
	// FSyncOnSave fsyncs only on Save (the atomic rename of state.json).
	// Mid-loop message-log appends are not fsync'd. Default.
	FSyncOnSave
	// FSyncOnAppend fsyncs on every JSONL append (slowest, most durable).
	FSyncOnAppend
)

type Options

type Options struct {
	// Root is the directory under which per-conversation directories live.
	// Created if absent. Required.
	Root string

	// FSync controls fsync behavior. Defaults to FSyncOnSave.
	FSync FSyncPolicy

	// TTL, if non-zero, removes conversation directories whose state.json
	// mtime is older than now-TTL at NewStore time.
	TTL time.Duration
}

Options configures the file-backed store.

type Store

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

Store is a filesystem-backed implementation of statestore.Store and the optional MessageLog / MessageReader / MessageAppender / MetadataAccessor / SummaryAccessor / ListAccessor interfaces.

func NewStore

func NewStore(opts Options) (*Store, error)

NewStore opens (or creates) a file-backed store at opts.Root. Returns an error if Root is empty or the directory cannot be created.

func (*Store) AppendList

func (s *Store) AppendList(
	_ context.Context, id, listName string, items [][]byte,
) error

AppendList appends opaque-byte items to the named list. Each item becomes one line in lists/<listName>.jsonl. Auto-creates the conversation and list.

func (*Store) AppendMessages

func (s *Store) AppendMessages(
	_ context.Context, id string, messages []types.Message,
) error

AppendMessages appends without dedup. Auto-creates the conversation. Differs from LogAppend in that it takes no startSeq — used by pipeline paths that already track their own offset.

func (*Store) Close

func (s *Store) Close() error

Close releases in-memory resources. Idempotent. Outstanding handles to the store after Close return ErrStoreClosed.

func (*Store) Delete

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

Delete removes the entire conv-<id> directory. No-op when absent.

func (*Store) Fork

func (s *Store) Fork(_ context.Context, sourceID, newID string) error

Fork copies all files in <root>/conv-<sourceID>/ to <root>/conv-<newID>/. Returns ErrNotFound when source is missing.

func (*Store) List

func (s *Store) List(_ context.Context, opts statestore.ListOptions) ([]string, error)

List returns conversation IDs filtered by ListOptions. The UserID filter reads each candidate's state.json (no separate user index in v1; ample for typical scales).

func (*Store) ListLen

func (s *Store) ListLen(_ context.Context, id, listName string) (int, error)

ListLen returns the number of items in the named list. 0 for missing.

func (*Store) Load

Load reads state.json + messages.jsonl + summaries.jsonl into a ConversationState. Returns ErrNotFound when state.json is absent.

func (*Store) LoadList

func (s *Store) LoadList(
	_ context.Context, id, listName string,
) ([][]byte, error)

LoadList returns all items of the named list in append order. Returns (nil, nil) when the list is empty or missing.

func (*Store) LoadMetadata

func (s *Store) LoadMetadata(_ context.Context, id string) (map[string]any, error)

LoadMetadata returns the metadata map for the conversation. Returns ErrNotFound if no state.json exists for the conv. The returned map is a deep copy safe for caller mutation.

func (*Store) LoadRecentMessages

func (s *Store) LoadRecentMessages(
	ctx context.Context, id string, n int,
) ([]types.Message, error)

LoadRecentMessages returns the last n messages. Returns ErrNotFound if the conversation has no state.json (the caller asked about a conv that doesn't exist).

func (*Store) LoadSummaries

func (s *Store) LoadSummaries(_ context.Context, id string) ([]statestore.Summary, error)

LoadSummaries returns all summaries for the conversation, or nil if none.

func (*Store) LogAppend

func (s *Store) LogAppend(
	_ context.Context, id string, startSeq int, messages []types.Message,
) (int, error)

LogAppend appends messages with clamp-and-skip idempotent dedup. Mirrors MemoryStore.LogAppend / RedisStore.LogAppend semantics exactly. Also writes a minimal state.json stub on first append so Load(id) works during crash recovery before the end-of-turn Save runs.

func (*Store) LogLen

func (s *Store) LogLen(_ context.Context, id string) (int, error)

LogLen returns the message count, or 0 if absent.

func (*Store) LogLoad

func (s *Store) LogLoad(_ context.Context, id string, recent int) ([]types.Message, error)

LogLoad returns messages for the conversation. If recent > 0, returns only the last N. Returns nil (not an error) when the file doesn't exist.

func (*Store) MergeMetadata

func (s *Store) MergeMetadata(
	_ context.Context, id string, updates map[string]any,
) error

MergeMetadata atomically merges updates into the conversation's metadata. Auto-creates the conversation. Read-modify-write under the per-conv lock.

func (*Store) MessageCount

func (s *Store) MessageCount(_ context.Context, id string) (int, error)

MessageCount returns the total number of messages. Returns ErrNotFound when the conversation has no state.json.

func (*Store) Save

Save atomically writes state.json and reconciles messages.jsonl / summaries.jsonl with the provided state. When the on-disk message log is shorter than state.Messages, only the delta is appended (matches the Redis delta-append branch). When it is longer, the file is rewritten in full.

func (*Store) SaveSummary

func (s *Store) SaveSummary(_ context.Context, id string, summary statestore.Summary) error

SaveSummary appends a summary to summaries.jsonl. Auto-creates the conv.

Jump to

Keyboard shortcuts

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