Documentation
¶
Overview ¶
Package sessions locates and summarizes Claude Code sessions.
It knows the config-dir rules (CLAUDE_CONFIG_DIR, ~/.claude, %USERPROFILE%\\.claude), the realpath-derived project folder slug, the per-PID live registry, sidecar folders, and the title / last-prompt rules used by Claude's own session picker. It reads only the head and tail of transcripts. Everything Claude-specific lives here and in package rewrite, so format drift has one place to land.
Index ¶
- Variables
- func ConfigDir(home string, getenv func(string) string, join func(...string) string) string
- func LocalAlive(pids []int) map[int]bool
- func LocalConfigDir() (string, error)
- func ProjectDirName(path string, getenv func(string) string) string
- func ReadSmall(fs fsys.FS, name string, max int) ([]byte, error)
- func ResolvedSlug(path string) string
- func Slug(path string) string
- func StopLocal(configDir, sessionID string, pid int, wait time.Duration) error
- func TurnsAfterMoveMark(fs fsys.FS, file string) (int, error)
- func TurnsSince(fs fsys.FS, file string, t time.Time) (int, error)
- type ListOptions
- type LiveEntry
- type Locator
- type Summary
Constants ¶
This section is empty.
Variables ¶
var ErrStillRunning = errors.New("the session did not exit in time; quit it yourself (type /exit in it), then try again")
ErrStillRunning means a stopped session did not exit in time.
Functions ¶
func ConfigDir ¶
ConfigDir returns Claude Code's config directory for a machine: $CLAUDE_CONFIG_DIR, else <home>/.claude. For the local machine pass os.UserHomeDir() and os.Getenv.
func LocalAlive ¶
LocalAlive reports which pids are running on this machine.
func LocalConfigDir ¶
LocalConfigDir is ConfigDir for this machine.
func ProjectDirName ¶
ProjectDirName applies the CLAUDE_CODE_PROJECT_DIR_NAME override (honoured only when CLAUDE_CONFIG_DIR is also set and the name is valid), otherwise returns Slug(path).
func ResolvedSlug ¶
ResolvedSlug resolves symlinks (falling back to the path as given, like Claude Code) and returns its Slug. For a directory on another machine, resolve it there and call Slug with the result.
func Slug ¶
Slug returns the project folder name Claude Code uses for an (already resolved) working directory: every UTF-16 code unit that is not [A-Za-z0-9] becomes '-', and names longer than 200 units are cut to 200 and suffixed with "-" plus the base-36 absolute value of the Java String.hashCode of the full path.
Pass the realpath'd directory; use ResolvedSlug to resolve symlinks first. The path is NFC-normalised here, as Claude Code does.
func StopLocal ¶ added in v0.2.0
StopLocal asks a Claude Code process on this machine to exit (SIGTERM, which lets it finish writing its transcript) and waits for it. It first re-reads the live registry to confirm the pid still belongs to sessionID, so a recycled pid is never signalled.
func TurnsAfterMoveMark ¶ added in v0.2.0
TurnsAfterMoveMark counts the messages added to a copy after hopsesh marked it "moved" (someone kept working on the copy left behind). It returns -1 when the copy carries no mark in the part of the file it reads.
Types ¶
type ListOptions ¶
type ListOptions struct {
IncludeSidechains bool // subagent-only transcripts (normally hidden, like the picker)
IncludeEmpty bool // bookkeeping-only transcripts with no messages
}
ListOptions filters List.
type LiveEntry ¶
type LiveEntry struct {
PID int `json:"pid"`
SessionID string `json:"sessionId"`
CWD string `json:"cwd"`
Status string `json:"status"`
WaitingFor string `json:"waitingFor"`
Name string `json:"name"`
Version string `json:"version"`
Entrypoint string `json:"entrypoint"`
Kind string `json:"kind"`
ProcStart string `json:"procStart"`
StartedAt int64 `json:"startedAt"`
BridgeSessionID string `json:"bridgeSessionId"`
Alive bool `json:"alive"`
}
LiveEntry is one running Claude Code process from <configDir>/sessions/<pid>.json. The format is internal to Claude Code; unknown fields are ignored.
type Locator ¶
type Locator struct {
FS fsys.FS
ConfigDir string
// Workers bounds parallel summarisation (remote filesystems benefit from more).
Workers int
}
Locator finds sessions under one config directory on one filesystem.
func (Locator) Find ¶
Find returns every transcript file for a session id (normally one; duplicates break `claude --resume <id>`, which is why transports remove them).
type Summary ¶
type Summary struct {
ID string `json:"id"`
File string `json:"file"`
ProjectDir string `json:"projectDir"`
Title string `json:"title"`
TitleSource string `json:"titleSource"` // custom | ai | summary | prompt | none
// MovedTo is set when this copy was left behind by a handoff: hopsesh marked it
// "↪ moved to <host> · <title>" (Title holds the original title).
MovedTo string `json:"movedTo,omitempty"`
CWD string `json:"cwd"` // project directory: relocated cwd, else the launch cwd
LastCWD string `json:"lastCwd,omitempty"` // shell cwd of the newest record (may be a subdirectory)
// WorktreeRoot is set when CWD is a Claude-managed worktree (<repo>/.claude/worktrees/<name>):
// such transcripts stay in the repo root's project folder.
WorktreeRoot string `json:"worktreeRoot,omitempty"`
GitBranch string `json:"gitBranch,omitempty"`
LastPrompt string `json:"lastPrompt,omitempty"`
Version string `json:"claudeVersion,omitempty"`
Entrypoint string `json:"entrypoint,omitempty"`
LastActivity time.Time `json:"lastActive"`
Size int64 `json:"sizeBytes"`
IsSidechain bool `json:"isSidechain,omitempty"`
HasMessages bool `json:"hasMessages"`
Subagents int `json:"subagents"`
}
Summary describes one session transcript.
func Summarize ¶
Summarize reads the head and tail of a transcript and derives its summary using the same priorities as Claude Code's picker.
func SummarizeHint ¶
SummarizeHint is Summarize with what a directory listing already knows: the file's info (saving a Stat) and whether its sidecar folder exists (saving lookups). On a remote filesystem every avoided request is a network round trip.