state

package
v1.6.5 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package state is where sitrep keeps its files: the folder, the session ids that name them, and the way they are written.

Index

Constants

View Source
const (
	MaxWindow     = 100_000_000
	MaxTokens     = 10_000_000_000
	ContextMaxAge = 300 // seconds after which a record is written again though its numbers are the same, so its time means last seen
)

Limits of a context record. The input comes from Claude Code, and the file can be edited, so every number is range-checked.

View Source
const (
	FileMode os.FileMode = 0o600
	DirMode  os.FileMode = 0o700
)

Modes for everything sitrep writes (invariant 2): state holds prompt text, so it is owner-only. A Umask call does not reach in-process tests, so the modes are always explicit.

View Source
const EnvDir = "SITREP_STATE_DIR"

EnvDir is the variable that moves the state folder.

View Source
const MaxAlias = 32

Longest alias, in characters.

View Source
const MaxTopic = 80

MaxTopic is the longest topic of a Branch, in characters.

Variables

View Source
var ErrBusy = errors.New("busy: another sitrep is changing the same files")

ErrBusy says a lock could not be taken in time.

View Source
var ErrHasAlias = errors.New("the session has a name already")

ErrHasAlias says a session has a name already, and a caller that only names the unnamed would not change it.

View Source
var ErrNotRegular = errors.New("not a regular file")

ErrNotRegular says a state file is not a regular file: a link, a pipe, a device or a folder.

View Source
var SessionFiles = []SessionFile{
	{"alias", true}, {"cwd", true}, {"notes", true}, {"pinned", true}, {"queue", true},
	{"state", false}, {"count", false}, {"stamp", false}, {"pending", false}, {"lock", false},
	{"nudge", false}, {"context", false}, {"blocked", false}, {"snooze", false}, {"parent", false}, {"report", false}, {"continued", false},
}

SessionFiles are the files of a session, the names that Store.Path is given for one. A new file is a row here: a test fails for a name that code asks for and that is not here, and for a row that docs/STATE-LAYOUT.md or docs/BACKUP.md does not list.

Functions

func ConfigDir

func ConfigDir(getenv func(string) string, home string) string

ConfigDir is Claude Code's own folder. Only the transcripts and the live-session registry honor CLAUDE_CONFIG_DIR; sitrep's own state never moves (see Open), and unifying the two would orphan existing state.

func FirstLine

func FirstLine(path string) string

FirstLine is the first line of a small regular file, without leading or trailing spaces and tabs, as the shell's read -r gives it. It is empty when the file cannot be read.

func IsRegular

func IsRegular(path string) bool

IsRegular says whether path is a regular file, not a link to one.

func Lock

func Lock(path string) (unlock func(), err error)

Lock takes an exclusive lock on a file (made if needed), waiting a moment for another holder, and gives up rather than hang a prompt. The returned function releases it. Locks are advisory: every writer of what the lock guards takes it.

func OpenRegular added in v1.0.1

func OpenRegular(path string, flag int, perm os.FileMode) (*os.File, error)

OpenRegular opens a state file for writing (or reading and writing). It never follows a link in the last place of the path and never blocks on a pipe, and it refuses what is not a regular file, as ReadFile does for reading: a planted link or pipe cannot make sitrep write elsewhere or hang while it holds a lock.

func ReadFile

func ReadFile(path string, max int64) ([]byte, error)

ReadFile reads at most max bytes of a regular file, and nothing else: a link, a pipe, a device or a directory is an error, and opening one never blocks (invariant 4: a hook must not hang on a hostile file). It is for the files in the state folder, which sitrep writes itself and never through a link, so a link there is not ours.

func ReadFileFollow added in v1.0.0

func ReadFileFollow(path string, max int64) ([]byte, error)

ReadFileFollow is ReadFile for a file that is not ours and may be a link on purpose: sitrep.conf and settings.json, which a dotfiles manager links into place, and the registry files of Claude Code.

func SavedFiles added in v1.2.1

func SavedFiles() []string

SavedFiles are the names of the files of a session that a backup saves, in the order of SessionFiles.

func SessionID

func SessionID(s string) bool

SessionID is ValidID for a real session: the inbox is not one.

func TryLock added in v1.0.0

func TryLock(path string) (unlock func(), ok bool)

TryLock is Lock without the wait: one attempt, and ok is false when another holder has the lock. A hook uses it for work it can skip.

func ValidAlias added in v0.6.0

func ValidAlias(s string) bool

ValidAlias says s may be an alias: lowercase letters, digits and hyphens, starting with a letter, at most MaxAlias characters. The inbox is reserved. The grammar is what makes an alias safe to print and impossible to mistake for a flag or an item number.

func ValidID

func ValidID(s string) bool

ValidID reports whether s can be part of a file name: letters, digits, underscore and hyphen, and nothing else (invariant 3).

Types

type AliasEntry added in v0.6.0

type AliasEntry struct {
	Name string
	ID   ID
}

AliasEntry is one alias and the session that has it.

type AliasPrefixError added in v0.6.0

type AliasPrefixError struct {
	Name string
	ID   ID
}

AliasPrefixError says the name is the start of a session id, which `Resolve` reads as that id.

func (*AliasPrefixError) Error added in v0.6.0

func (e *AliasPrefixError) Error() string

type AliasTakenError added in v0.6.0

type AliasTakenError struct {
	Name   string
	Holder ID
}

AliasTakenError says another session has the alias.

func (*AliasTakenError) Error added in v0.6.0

func (e *AliasTakenError) Error() string

type Context added in v0.8.0

type Context struct {
	UsedPercent float64 // 0 to 100
	Window      int64   // the size of the window, in tokens
	Tokens      int64   // what was in it
	At          int64   // when the status line last showed these numbers, in epoch seconds
}

Context is how full a session's context window was when its status line was last drawn.

func (Context) Valid added in v0.8.0

func (c Context) Valid() bool

Valid says every number is in range.

type ID

type ID string

ID names a queue: a session id, or the inbox. It can only be made by ParseID or Resolve, so a path built from an ID cannot leave the state folder (invariant 3).

const Inbox ID = "inbox"

Inbox is the queue of no session. It is reserved: no session id may be this.

func ParseID

func ParseID(s string) (ID, error)

ParseID checks that s is a valid id (the inbox included).

type Legacy added in v0.5.0

type Legacy struct {
	ID       ID
	File     string
	Obsolete bool
}

Legacy says what a name in the root of the state folder is in the old flat layout: <id>.<ext> for txt, queue and cwd, and .<id>.<ext> for the others, for a valid session id. file is the name it has in the session's folder; obsolete says nothing reads the file any more.

func ParseLegacy added in v0.5.0

func ParseLegacy(name string) (Legacy, bool)

ParseLegacy reads a name of the old layout. The id has no dot, so the last dot ends it; the inbox is no session, and a name that is anything else is not ours (ok is false).

type SessionFile added in v1.2.1

type SessionFile struct {
	Name  string
	Saved bool // a backup saves it; the others describe a moment that has passed
}

SessionFile is a file in a session's folder.

type Store

type Store struct{ Dir string }

Store is the state folder.

func Locate added in v1.0.0

func Locate(home string, getenv func(string) string) (Store, error)

Locate names the state folder: the path in SITREP_STATE_DIR when it is set, or else the default. Only the state moves. The program, which the hooks run from ~/.claude/sitrep/bin, stays where setup put it. A value that is not an absolute path is refused, and never read as relative to wherever the program happens to run, so that a hook cannot write state somewhere by accident. So is the home folder, Claude's own folder, the registry and the transcripts in it, and any folder above them: the state is made owner-only and fills the folder with its own files, so it needs one of its own.

func Open

func Open(home string) Store

Open names the default state folder, ~/.claude/sitrep, whatever CLAUDE_CONFIG_DIR says. It creates nothing.

func (Store) Alias added in v0.6.0

func (s Store) Alias(id ID) string

Alias is the name the user gave a session, or "" when there is none. The file is the user's to edit, so only a regular file that is not a link, holds at most 64 bytes and has a first line that passes ValidAlias counts; anything else is no alias.

func (Store) AliasList added in v0.6.0

func (s Store) AliasList() []AliasEntry

AliasList is the aliases, sorted by name.

func (Store) Aliases added in v0.6.0

func (s Store) Aliases() map[ID]string

Aliases maps each session that has an alias to it.

func (Store) BranchesDir added in v1.6.2

func (s Store) BranchesDir() string

BranchesDir holds the pending records, one file per Branch that has not started yet, named by its id.

func (Store) Check added in v1.0.0

func (s Store) Check() error

Check says whether the folder can be read, for a command that only shows state: it would otherwise show a folder that cannot be read, such as a stale mount, as an empty one. A folder that does not exist is fine, since nothing has been written yet. A mount that hangs is not caught, as nothing in the standard library can stop the call.

func (Store) ClearAlias added in v0.6.0

func (s Store) ClearAlias(id ID) (string, error)

ClearAlias removes a session's alias and returns the name it had ("" when there was none).

func (Store) Context added in v0.8.0

func (s Store) Context(id ID) (Context, bool)

Context is the record of a session, and whether there is a valid one. The file is the user's to edit, so only a regular file that is not a link, holds at most 128 bytes and has four numbers in range counts.

func (Store) Continued added in v1.6.2

func (s Store) Continued(id ID) ID

Continued is the session that a session's conversation went on as after a /clear, which `sitrep adopt` recorded (docs/ADOPT-LINK.md), or "" when there is no such record. It is read as Parent is.

func (Store) Ensure

func (s Store) Ensure() error

Ensure creates the folder if needed and makes it owner-only. A folder that exists, such as one chosen with SITREP_STATE_DIR, only loses its group and other bits: its mode is never widened (invariant 5).

func (Store) EnsureSession added in v0.5.0

func (s Store) EnsureSession(id ID) error

EnsureSession makes the folder of a session, owner-only, and the folder of sessions above it. Whatever writes first makes it.

func (Store) GiveAlias added in v1.0.7

func (s Store) GiveAlias(id ID, name string) error

GiveAlias names a session that has no name, and changes nothing otherwise: the session's folder must be there, it must have no alias, and the name must be free, all looked at under the folder's lock, so that what a plan saw before the lock cannot have changed unnoticed. It never makes a folder.

func (Store) Lock added in v0.6.0

func (s Store) Lock() (func(), error)

Lock takes the lock every change to a queue or an alias takes, so that one lock covers the whole folder. Nothing to protect when the folder does not exist yet.

func (Store) MoveAlias added in v1.1.0

func (s Store) MoveAlias(from, to ID) error

MoveAlias gives the name that from holds to to, which has none. The name is written to to before from loses it, so that a failure leaves it on both and never on neither. The caller holds Lock and has checked both sessions, so this does neither.

func (Store) Parent added in v1.6.2

func (s Store) Parent(id ID) ID

Parent is the session a Branch was forked from, or "" when the session is no Branch. Only a regular file that is not a link, holds at most 128 bytes and has a session id on its first line counts.

func (Store) Path

func (s Store) Path(id ID, name string) string

Path is the file of a session: notes, queue, cwd, state, count, stamp, pending, lock, pinned or snooze. The inbox has only its queue, in the root as inbox.queue.

func (Store) PruneBranches added in v1.6.2

func (s Store) PruneBranches(now time.Time, age time.Duration)

PruneBranches removes the pending records older than age: a Branch whose Claude never started leaves one. Each is removed by its own path, and only a regular file with an id for a name.

func (Store) RecordContext added in v0.8.0

func (s Store) RecordContext(id ID, c Context) error

RecordContext writes the record of a session, if the session exists (its notes file does: an unknown id makes no folder) and the numbers are valid. It writes only when the numbers changed or the record is more than ContextMaxAge old.

func (Store) Resolve

func (s Store) Resolve(prefix string) (ID, error)

Resolve turns a unique prefix of a session id, or an alias, into the full id. "inbox" matches exactly. The error text is safe to print.

It is stricter than the bash it replaces: only session folders with a valid id count.

func (Store) SessionDir added in v0.5.0

func (s Store) SessionDir(id ID) string

SessionDir is the folder of one session. The inbox is no session, and lives in the root.

func (Store) SessionFolders added in v1.0.7

func (s Store) SessionFolders() []ID

SessionFolders lists the folders of the sessions folder that have a valid id, with or without notes: a queue can be added to a session that no hook has seen yet, and the folder then holds items that a backup must not miss. A link is not a folder here.

func (Store) Sessions added in v0.5.0

func (s Store) Sessions() []ID

Sessions lists the sessions: the folders of the sessions folder that have a valid id and hold a regular file notes. A link is not followed, and a folder without notes is not a session.

func (Store) SessionsDir added in v0.5.0

func (s Store) SessionsDir() string

SessionsDir is the folder that holds one folder per session.

func (Store) SetAlias added in v0.6.0

func (s Store) SetAlias(id ID, name string) (bool, error)

SetAlias gives a session an alias and says whether anything changed. A name that another session holds is refused, whatever that session's state, as an ended session may be resumed. The check and the write happen under the folder's lock.

func (Store) SetAliasFrom added in v1.0.6

func (s Store) SetAliasFrom(id ID, name string, mayTake func(holder ID) bool) (changed bool, from ID, err error)

SetAliasFrom is SetAlias for a caller that may take the name from the session that holds it: mayTake says, for that holder, whether it may (the caller asked for it, and the holder has ended). The holder loses the name in the same locked step as the new session gets it, and `from` is that holder, or "" when the name was free.

func (Store) SetContinued added in v1.6.2

func (s Store) SetContinued(from, to ID) error

SetContinued records that the conversation of from went on as to.

func (Store) SetParent added in v1.6.2

func (s Store) SetParent(id, parent ID) error

SetParent records the parent of a Branch.

func (Store) TakeBranch added in v1.6.2

func (s Store) TakeBranch(branch ID) (parent ID, topic string, ok bool)

TakeBranch reads and removes the pending record of a Branch, and says whether there was one that names a session. A record that is no regular file is neither read nor removed.

func (Store) WriteBranch added in v1.6.2

func (s Store) WriteBranch(branch, parent ID, topic string) error

WriteBranch writes the pending record of a Branch that is about to start: the parent's id, and the topic on the second line. The topic is cleaned by the caller and cleaned again by the hook.

func (Store) WriteFile

func (s Store) WriteFile(path string, data []byte) error

WriteFile replaces path with data in one step: a reader sees the old file or the new one, never a half. The path must be in the state folder or in the folder of a session (made if need be). The temporary file has a unique name in the same folder and is removed if anything fails.

Jump to

Keyboard shortcuts

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