session

package
v0.69.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 12 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 session manages terminal and agent work sessions scoped by git branch and agent identity.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanupStaleSessions added in v0.4.26

func CleanupStaleSessions(database *db.DB, maxAge time.Duration) (int, error)

CleanupStaleSessions removes sessions older than maxAge

func FormatSessionID

func FormatSessionID(database *db.DB, sessionID string) string

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

func ParseDuration(s string) (time.Duration, error)

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

func ForceNewSession(database *db.DB) (*Session, error)

ForceNewSession creates a new session on the current branch/agent, regardless of existing session

func Get

func Get(database *db.DB) (*Session, error)

Get returns the current session without creating one

func GetOrCreate

func GetOrCreate(database *db.DB) (*Session, error)

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

func GetWithContextCheck(database *db.DB) (*Session, error)

GetWithContextCheck returns the current session and checks if context changed.

func ListSessions added in v0.4.26

func ListSessions(database *db.DB) ([]Session, error)

ListSessions returns all sessions

func SetName

func SetName(database *db.DB, name string) (*Session, error)

SetName sets the session name

func (*Session) Display

func (s *Session) Display() string

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

func (s *Session) DisplayWithAgent() string

DisplayWithAgent returns session info including agent: "ses_abc123 [claude-code]" or with name

func (*Session) LastActive added in v0.55.0

func (s *Session) LastActive() time.Time

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.

Jump to

Keyboard shortcuts

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