datastore

package
v0.1.5 Latest Latest
Warning

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

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

Documentation

Overview

Package datastore provides a unified data layer for session-scoped persistence. It composes existing data packages and serves as the authoritative source for conversation histories, prompt history, session state, rewind stack, checkpoint, and cost tracking within a session.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	PromptHistPath string // path to global prompt_history.jsonl
	SessionPaths   session.Paths
	SessionID      string
	Meta           session.SessionMetadata
	RecipeStore    *recipestore.Store
	Recipe         *recipe.Recipe // base recipe loaded at startup
}

Options configures a new Store.

type RewindEntry

type RewindEntry struct {
	FileSnapshots  []tools.FileSnapshot // nil if no writes were applied
	HistoryLengths map[string]int       // per-adapter history len BEFORE AppendHistory
	FeedIndex      int                  // index into the UI feed for annotation
	Prompt         string               // original prompt (for rewind marker)
}

RewindEntry captures enough state to undo one run.

type RewindResult

type RewindResult struct {
	FileMsg   string // warning from file restore, or ""
	FeedIndex int    // index into the UI feed for annotation
	Note      string // "[rewound]" or "[rewound] Applied: ..."
}

RewindResult is returned by Rewind with information the UI needs to update display.

type SelectionParams

type SelectionParams struct {
	Prompt          string
	SelectedModelID string
	Responses       []models.ModelResponse
	AppliedFiles    []string // nil for text-only votes / ratings
	Rating          string   // "" for selection, "good" or "bad" for rating
}

SelectionParams captures the information needed to record a selection or rating.

type Store

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

Store is the single source of truth for persisted and session-scoped data. It owns the in-memory state and persists on mutation.

func New

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

New creates a Store, loading existing data from disk. Missing files are not errors — the store starts empty.

func (*Store) AccumulateCost

func (s *Store) AccumulateCost(modelID string, cost float64)

AccumulateCost adds cost for a model run.

func (*Store) ActiveRecipe

func (s *Store) ActiveRecipe() *recipe.Recipe

ActiveRecipe returns the session recipe if set, otherwise the base recipe.

func (*Store) AppendHistories

func (s *Store) AppendHistories(
	panelIDs []string,
	responses []models.ModelResponse,
	userPrompt string,
) map[string]int

AppendHistories appends conversation turns for a completed run and persists to disk. Returns the pre-append lengths (for rewind support).

func (*Store) BaseRecipe

func (s *Store) BaseRecipe() *recipe.Recipe

BaseRecipe returns the immutable base recipe from startup.

func (*Store) BuildRecipeSnapshot

func (s *Store) BuildRecipeSnapshot() *recipestore.RecipeSnapshot

BuildRecipeSnapshot creates a RecipeSnapshot from the active recipe and the last active tools.

func (*Store) CanRewind

func (s *Store) CanRewind() bool

CanRewind returns true if the rewind stack is non-empty.

func (*Store) CheckpointPath

func (s *Store) CheckpointPath() string

CheckpointPath returns the path used for checkpoint files.

func (*Store) ClearCheckpoint

func (s *Store) ClearCheckpoint()

ClearCheckpoint removes the checkpoint file.

func (*Store) ClearHistories

func (s *Store) ClearHistories()

ClearHistories wipes all conversation history and persists.

func (*Store) ClearRewindStack

func (s *Store) ClearRewindStack()

ClearRewindStack empties the rewind stack (called on /clear, /wipe, /compact).

func (*Store) Content

func (s *Store) Content() session.SessionContent

Content returns the current session content.

func (*Store) ContentPath

func (s *Store) ContentPath() string

ContentPath returns the path to session_content.json.

func (*Store) CostPerModel

func (s *Store) CostPerModel() map[string]float64

CostPerModel returns the per-model cumulative cost this session.

func (*Store) Histories

func (s *Store) Histories() map[string][]models.ConversationTurn

Histories returns the current conversation histories map. The caller must not mutate the returned map.

func (*Store) LastActiveTools

func (s *Store) LastActiveTools() []string

LastActiveTools returns the tool names from the last run.

func (*Store) LoadCheckpoint

func (s *Store) LoadCheckpoint() (*checkpoint.Checkpoint, error)

LoadCheckpoint loads the checkpoint from disk. Returns (nil, nil) if not found.

func (*Store) Metadata

func (s *Store) Metadata() session.SessionMetadata

Metadata returns the current session metadata.

func (*Store) MetadataPath

func (s *Store) MetadataPath() string

MetadataPath returns the path to session_metadata.json.

func (*Store) PersistRunState

func (s *Store) PersistRunState(
	lastPrompt string,
	responses []models.ModelResponse,
	collector *output.Collector,
	toolNames []string,
)

PersistRunState updates session metadata with a RunSummary, appends a RunContent to content, and persists both files plus the session recipe.

func (*Store) PromptHistory

func (s *Store) PromptHistory() []string

PromptHistory returns the prompt history (newest-first). The caller must not mutate the returned slice.

func (*Store) PushFileSnapshots

func (s *Store) PushFileSnapshots(snaps []tools.FileSnapshot)

PushFileSnapshots stores file snapshots on the top rewind entry (called after applySelection writes files).

func (*Store) PushRewindEntry

func (s *Store) PushRewindEntry(entry RewindEntry)

PushRewindEntry pushes a new rewind entry onto the stack.

func (*Store) RecipeHash

func (s *Store) RecipeHash() string

RecipeHash computes a content-addressed hash for the active recipe snapshot. Returns "" if no recipe store is configured.

func (*Store) RecipeNameLookup

func (s *Store) RecipeNameLookup() func(string) string

RecipeNameLookup returns a function that resolves a config hash to a recipe name.

func (*Store) RecipeStore

func (s *Store) RecipeStore() *recipestore.Store

RecipeStore returns the recipe store (for /stats filter).

func (*Store) RecipesDir

func (s *Store) RecipesDir() string

RecipesDir returns the recipes directory (data/recipes/).

func (*Store) RecordPrompt

func (s *Store) RecordPrompt(prompt string) bool

RecordPrompt adds a prompt to history if it differs from the most recent entry, persists to disk, and returns whether the prompt was actually added.

func (*Store) RecordSelection

func (s *Store) RecordSelection(p SelectionParams)

RecordSelection updates the last RunSummary in metadata with the selection outcome.

func (*Store) Rewind

func (s *Store) Rewind() (RewindResult, error)

Rewind pops the top rewind entry, restores files, truncates histories, records a rewind marker in metadata, and annotates the run note. Returns a RewindResult for the UI to update display.

func (*Store) RewindStackLen

func (s *Store) RewindStackLen() int

RewindStackLen returns the number of entries in the rewind stack (used by tests).

func (*Store) SaveInitialMeta

func (s *Store) SaveInitialMeta()

SaveInitialMeta persists the initial session metadata to disk.

func (*Store) SessionID

func (s *Store) SessionID() string

SessionID returns the session ID.

func (*Store) SessionRecipe

func (s *Store) SessionRecipe() *recipe.Recipe

SessionRecipe returns the working copy of the recipe (nil if unmodified).

func (*Store) SessionRecipePath

func (s *Store) SessionRecipePath() string

SessionRecipePath returns the path to session recipe.md (used by tests).

func (*Store) SessionsDir

func (s *Store) SessionsDir() string

SessionsDir returns the base sessions directory (data/sessions/).

func (*Store) SetHistories

func (s *Store) SetHistories(h map[string][]models.ConversationTurn)

SetHistories replaces the histories wholesale (used by compaction) and persists.

func (*Store) SetLastActiveTools

func (s *Store) SetLastActiveTools(names []string)

SetLastActiveTools sets the last active tool names (used after run complete).

func (*Store) SetSessionRecipe

func (s *Store) SetSessionRecipe(r *recipe.Recipe)

SetSessionRecipe sets the working copy of the recipe.

func (*Store) TotalCost

func (s *Store) TotalCost() float64

TotalCost returns the cumulative cost across all runs this session.

func (*Store) TruncateHistories

func (s *Store) TruncateHistories(lengths map[string]int)

TruncateHistories restores per-adapter history to the given pre-run lengths (used by rewind) and persists.

func (*Store) UpdateLastRunNote

func (s *Store) UpdateLastRunNote(note string)

UpdateLastRunNote updates the note on the most recent RunSummary in metadata and re-saves session_metadata.json.

Jump to

Keyboard shortcuts

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