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
- Variables
- func ConfigDir(getenv func(string) string, home string) string
- func FirstLine(path string) string
- func IsRegular(path string) bool
- func Lock(path string) (unlock func(), err error)
- func ReadFile(path string, max int64) ([]byte, error)
- func SessionID(s string) bool
- func ValidAlias(s string) bool
- func ValidID(s string) bool
- type AliasEntry
- type AliasPrefixError
- type AliasTakenError
- type ID
- type Legacy
- type Store
- func (s Store) Alias(id ID) string
- func (s Store) AliasList() []AliasEntry
- func (s Store) Aliases() map[ID]string
- func (s Store) ClearAlias(id ID) (string, error)
- func (s Store) Ensure() error
- func (s Store) EnsureSession(id ID) error
- func (s Store) Lock() (func(), error)
- func (s Store) Path(id ID, name string) string
- func (s Store) Resolve(prefix string) (ID, error)
- func (s Store) SessionDir(id ID) string
- func (s Store) Sessions() []ID
- func (s Store) SessionsDir() string
- func (s Store) SetAlias(id ID, name string) (bool, error)
- func (s Store) WriteFile(path string, data []byte) error
Constants ¶
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.
const MaxAlias = 32
Longest alias, in characters.
Variables ¶
var ErrBusy = errors.New("busy: another sitrep is changing the same files")
ErrBusy says a lock could not be taken in time.
Functions ¶
func ConfigDir ¶
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 ¶
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 Lock ¶
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 ReadFile ¶
ReadFile reads at most max bytes of a regular file, and nothing else: 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). A link to a regular file is followed, as the bash tests did.
func ValidAlias ¶ added in v0.6.0
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.
Types ¶
type AliasEntry ¶ added in v0.6.0
AliasEntry is one alias and the session that has it.
type AliasPrefixError ¶ added in v0.6.0
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
AliasTakenError says another session has the alias.
func (*AliasTakenError) Error ¶ added in v0.6.0
func (e *AliasTakenError) Error() string
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.
type Legacy ¶ added in v0.5.0
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
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 Store ¶
type Store struct{ Dir string }
Store is the state folder.
func Open ¶
Open names the state folder, always ~/.claude/sitrep, whatever CLAUDE_CONFIG_DIR says. It creates nothing.
func (Store) Alias ¶ added in v0.6.0
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) ClearAlias ¶ added in v0.6.0
ClearAlias removes a session's alias and returns the name it had ("" when there was none).
func (Store) EnsureSession ¶ added in v0.5.0
EnsureSession makes the folder of a session, owner-only, and the folder of sessions above it. Whatever writes first makes it.
func (Store) Lock ¶ added in v0.6.0
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) Path ¶
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) Resolve ¶
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
SessionDir is the folder of one session. The inbox is no session, and lives in the root.
func (Store) Sessions ¶ added in v0.5.0
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
SessionsDir is the folder that holds one folder per session.
func (Store) SetAlias ¶ added in v0.6.0
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) WriteFile ¶
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.