sessions

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 20 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 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

Constants

This section is empty.

Variables

View Source
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

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

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

func LocalAlive(pids []int) map[int]bool

LocalAlive reports which pids are running on this machine.

func LocalConfigDir

func LocalConfigDir() (string, error)

LocalConfigDir is ConfigDir for this machine.

func ProjectDirName

func ProjectDirName(path string, getenv func(string) string) string

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 ReadSmall

func ReadSmall(fs fsys.FS, name string, max int) ([]byte, error)

ReadSmall reads a small file (at most max bytes) with a single read request.

func ResolvedSlug

func ResolvedSlug(path string) string

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

func Slug(path string) string

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

func StopLocal(configDir, sessionID string, pid int, wait time.Duration) error

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

func TurnsAfterMoveMark(fs fsys.FS, file string) (int, error)

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.

func TurnsSince added in v0.2.0

func TurnsSince(fs fsys.FS, file string, t time.Time) (int, error)

TurnsSince counts the messages in a copy timestamped after t.

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

func (l Locator) Find(id string) ([]string, error)

Find returns every transcript file for a session id (normally one; duplicates break `claude --resume <id>`, which is why transports remove them).

func (Locator) List

func (l Locator) List(opt ListOptions) ([]*Summary, error)

List summarises every session transcript under <configDir>/projects.

func (Locator) LiveRegistry

func (l Locator) LiveRegistry(alive func(pids []int) map[int]bool) ([]LiveEntry, error)

LiveRegistry reads the per-process registry. alive reports which pids are running on that machine; entries whose pid is not running are dropped (crash leftovers).

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

func Summarize(fs fsys.FS, file string) (*Summary, error)

Summarize reads the head and tail of a transcript and derives its summary using the same priorities as Claude Code's picker.

func SummarizeHint

func SummarizeHint(fs fsys.FS, file string, info os.FileInfo, hasSidecar *bool) (*Summary, error)

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.

Jump to

Keyboard shortcuts

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