Skip to main content

Module session_state

Module session_state 

Source
Expand description

Per-session workspace-mode state: which repo a roaming/detached Claude Code session is currently bound to. Persisted outside any repo so it survives a session that changes directories. Set by tracevault repo switch (sub-plan B); read by stream/commands as a resolution fallback.

Structs§

InferredRepo
One SessionState::inferred_repos entry: the repo a checkout resolved to within project_id, or none. A None repo is remembered only until retry_after (RFC3339), so a checkout registered or added to the project later is picked up without asking on every event. A found repo has no expiry: it is a fact about the server that changes only by a re-register.
ProjectBinding
A resolved binding to a registered project.
RepoBinding
A resolved binding to a registered repo.
SessionState
Session-level active binding plus per-worktree subagent overrides (keyed by the subagent’s git worktree toplevel path — design §7).

Enums§

ForceStatus
What a ProjectBinding::forced_until currently says about its force.

Constants§

WORKTREES_FILE
Inside an anchored session dir: every worktree toplevel the session ran an event in, one per line. The origin marker names only the latest, and check / verify-start in a checkout must still find a session that worked there and then moved on — as they did when the session kept a second dir in that checkout.

Functions§

anchored_session_dirs
anchored_session_dirs_in the user’s state dir.
anchored_session_dirs_in
The dirs of sessions anchored OUTSIDE local_sessions_dir (see SessionState::storage_root) that ran an event in worktree_top, read from the per-session state files in state_dir. A session started in an umbrella dir keeps its dir there, so the checkout it worked in has to look it up this way. Sorted; unreadable entries are skipped.
env_session_id
The session of the agent that runs this command, from its environment: $TRACEVAULT_SESSION_ID (exported by the Claude Code / Codex SessionStart hook), else $AMP_CURRENT_THREAD_ID.
force_status
Classify a stored forced_until — see ForceStatus.
load
Load a session’s state, returning the default (empty) state if the file is absent or malformed (workspace state is best-effort, never fatal).
load_from
Load a session’s state from an explicit sessions dir (test seam / callers that resolve the dir themselves). Absent or malformed → default state.
ran_in
Whether the session in session_dir ran an event in worktree_top: its origin marker names it, or its WORKTREES_FILE lists it.
record_storage_root
Record root as the session’s SessionState::storage_root unless one is already recorded: SessionStart fires again on resume, /clear and compaction, from wherever the shell has moved, and the session dir must not move with it. Returns whether it wrote.
record_worktree
Add worktree_top to session_dir’s WORKTREES_FILE unless listed.
save
Persist a session’s state, creating the state dir if needed.
session_dir
<root>/.tracevault/sessions/<session_id> for session_id, where root is the session’s recorded storage_root, else fallback_root (the root resolved from the caller’s working directory). Every reader and writer of a session dir goes through this, so a session is found where its hook keeps it.
session_id_from_env
env_session_id on given values: tracevault wins, then amp_thread.
sessions_dir
$XDG_STATE_HOME/tracevault/sessions or ~/.local/state/tracevault/sessions.
storage_root
The session’s recorded SessionState::storage_root, if that directory still exists. Its .tracevault/ may not exist yet: a git root is recorded whether or not it has one, and the stream hook creates it on first use, exactly as it would for an event whose cwd resolved there.
update_in
Load, change and save a session’s state while holding an exclusive lock on <session_id>.lock beside it, so the session’s parallel hooks — each recording what it looked up — do not overwrite each other’s changes. The lock is advisory: writers that do not take it (the interactive switch commands) are as before. The OS releases it when the holder exits.