Documentation
¶
Overview ¶
Package session manages terminal and agent work sessions scoped by git branch and agent identity.
Index ¶
- func CleanupStaleSessions(database *db.DB, maxAge time.Duration) (int, error)
- func FormatSessionID(database *db.DB, sessionID string) string
- func GetCurrentBranch() string
- func ParseDuration(s string) (time.Duration, error)
- type AgentFingerprint
- type AgentType
- type Session
- func ForceNewSession(database *db.DB) (*Session, error)
- func Get(database *db.DB) (*Session, error)
- func GetOrCreate(database *db.DB) (*Session, error)
- func GetWithContextCheck(database *db.DB) (*Session, error)
- func ListSessions(database *db.DB) ([]Session, error)
- func SetName(database *db.DB, name string) (*Session, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanupStaleSessions ¶ added in v0.4.26
CleanupStaleSessions removes sessions older than maxAge
func FormatSessionID ¶
FormatSessionID formats a session ID with optional name lookup.
func GetCurrentBranch ¶ added in v0.4.26
func GetCurrentBranch() string
GetCurrentBranch returns the current git branch (exported for display)
func ParseDuration ¶
ParseDuration parses human-readable duration strings.
The `d` (days) suffix is td's own extension — time.ParseDuration stops at hours. Days are multiplied into nanoseconds, so the multiplication MUST be range-checked: an unchecked `days * 24 * time.Hour` wraps silently, and a wrapped value that lands back in positive territory is indistinguishable from a deliberate short duration. That is not a cosmetic bug where the result gates a destructive sweep — an operator writing an intended-as-never `--stale 213504d` would get a 25-minute reaper.
Types ¶
type AgentFingerprint ¶ added in v0.4.26
type AgentFingerprint struct {
Type AgentType
PID int
ExplicitID string // set when TD_SESSION_ID is used
}
AgentFingerprint identifies a specific agent session
func GetAgentFingerprint ¶ added in v0.4.26
func GetAgentFingerprint() AgentFingerprint
GetAgentFingerprint detects the agent running this process by walking the process tree. The expensive process tree walk is cached; env var checks are always re-evaluated.
func (AgentFingerprint) String ¶ added in v0.4.26
func (af AgentFingerprint) String() string
String returns a filesystem-safe string representation
type AgentType ¶ added in v0.4.26
type AgentType string
AgentType identifies the type of AI agent
const ( AgentClaudeCode AgentType = "claude-code" AgentCursor AgentType = "cursor" AgentCodex AgentType = "codex" AgentWindsurf AgentType = "windsurf" AgentZed AgentType = "zed" AgentAider AgentType = "aider" AgentCopilot AgentType = "copilot" AgentGemini AgentType = "gemini" AgentUnknown AgentType = "unknown" AgentTerminal AgentType = "terminal" )
type Session ¶
type Session struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Branch string `json:"branch,omitempty"` // git branch for session scoping
AgentType string `json:"agent_type,omitempty"` // agent type (claude-code, cursor, terminal, etc.)
AgentPID int `json:"agent_pid,omitempty"` // stable parent agent process ID
ContextID string `json:"context_id,omitempty"` // audit-only execution-context fingerprint
MatchContextID string `json:"match_context_id,omitempty"` // identity-key dimension fed by TD_CONTEXT_ID
WorktreeID string `json:"worktree_id,omitempty"`
WorktreeRoot string `json:"worktree_root,omitempty"`
RepoRoot string `json:"repo_root,omitempty"`
PreviousSessionID string `json:"previous_session_id,omitempty"`
StartedAt time.Time `json:"started_at"`
LastActivity time.Time `json:"last_activity,omitempty"` // heartbeat for session liveness
IsNew bool `json:"-"` // True if session was just created (not persisted)
}
Session represents the current terminal session
func ForceNewSession ¶
ForceNewSession creates a new session on the current branch/agent, regardless of existing session
func GetOrCreate ¶
GetOrCreate returns the current session for the current git branch and agent. Sessions are scoped by branch + agent fingerprint - same agent on same branch = same session. Creates a new session if none exists for this branch/agent combination.
func GetWithContextCheck ¶
GetWithContextCheck returns the current session and checks if context changed.
func ListSessions ¶ added in v0.4.26
ListSessions returns all sessions
func (*Session) Display ¶
Display returns the session ID with name if set: "ses_abc123 (my-name)" or just "ses_abc123"
func (*Session) DisplayWithAgent ¶ added in v0.4.26
DisplayWithAgent returns session info including agent: "ses_abc123 [claude-code]" or with name
func (*Session) LastActive ¶ added in v0.55.0
LastActive returns the session's liveness timestamp: its last recorded activity, falling back to its start time for sessions that predate activity tracking (or were never touched again). This is the single definition of "when was this session last seen" shared by `session list`, `session cleanup`, and stale-claim reclamation.