session

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const MaxSessionNameLen = 100

MaxSessionNameLen is the maximum allowed length for a session name. The TUI input and the validation regex both enforce this limit.

Variables

This section is empty.

Functions

func AllocatePortsLegacy

func AllocatePortsLegacy() (fePort, apiPort int, err error)

AllocatePortsLegacy allocates two unique ports for frontend and API (backwards compatibility)

func AttachEditorToSession

func AttachEditorToSession(sessionName, path string) error

AttachEditorToSession handles editor attachment logic for session attach

func AttachTmuxSession

func AttachTmuxSession(sessionName string) error

AttachTmuxSession attaches to an existing tmux session

func BranchExists

func BranchExists(repoPath, branchName string) (bool, error)

BranchExists checks if a branch exists in the repository

func ClearAttentionFlag

func ClearAttentionFlag(sessionName string) error

ClearAttentionFlag clears the attention flag for a session

func ClearRegistry

func ClearRegistry() error

ClearRegistry removes all sessions and clears the sessions file

func CopyBootstrapFiles

func CopyBootstrapFiles(projectRoot, worktreePath string) error

CopyBootstrapFiles copies configured bootstrap files from project root to the worktree

func CreateWorktree

func CreateWorktree(repoPath, name string, detach bool) error

CreateWorktree creates a new git worktree

func FetchOrigin added in v0.3.0

func FetchOrigin(repoPath string) error

FetchOrigin fetches remote refs from origin, pruning deleted branches. Failure is non-fatal (caller warns and continues).

func GenerateEnvrc

func GenerateEnvrc(worktreePath string, data EnvrcData) error

GenerateEnvrc creates an .envrc file in the worktree directory

func GenerateTmuxpConfig

func GenerateTmuxpConfig(worktreePath string, data TmuxpData, projectPath string) error

GenerateTmuxpConfig creates a .tmuxp.yaml file in the worktree directory. projectPath is the root of the project (containing .devx/) and is used to find the project-level template. It must be passed explicitly so that config lookup is project-path-based rather than relying on os.Getwd(), which is unreliable when creating sessions via --project flag or the web UI.

func GetCurrentSessionName

func GetCurrentSessionName() string

GetCurrentSessionName attempts to determine the current session based on working directory

func GetEditorCommand

func GetEditorCommand() string

GetEditorCommand returns the editor command to use, checking in this order: 1. devx config "editor" setting 2. VISUAL environment variable 3. EDITOR environment variable 4. Empty string if none are set

func GetProjectRoot

func GetProjectRoot() (string, error)

GetProjectRoot finds the git repository root directory

func IsEditorAvailable

func IsEditorAvailable() bool

IsEditorAvailable checks if the configured editor command is available

func IsProcessRunning

func IsProcessRunning(pid int) bool

IsProcessRunning checks if a process with the given PID is still running

func IsTmuxRunning

func IsTmuxRunning() bool

IsTmuxRunning checks if we're already inside a tmux session

func IsValidSessionName added in v0.3.0

func IsValidSessionName(name string) bool

IsValidSessionName returns true if name passes both the character-class regex and path-traversal guards. Names are used as git branch names, tmux targets, and path components under .worktrees/, so "." and ".." segments must be rejected.

Validation is two-stage: (1) the regex enforces the allowed character set and maximum length; (2) the segment loop enforces structural constraints (path traversal, git-rejected patterns) that regex alone cannot cleanly express.

func IsWorktreeCheckedOut

func IsWorktreeCheckedOut(repoPath, branchName string) (bool, string, error)

IsWorktreeCheckedOut checks if a branch is already checked out in a worktree

func LaunchEditor

func LaunchEditor(path string) (int, error)

LaunchEditor launches the configured editor with the given path It runs asynchronously and returns the process PID

func LaunchEditorForSession

func LaunchEditorForSession(sessionName, path string) error

LaunchEditorForSession launches the editor for a session and updates the session metadata

func LaunchTmuxSession

func LaunchTmuxSession(worktreePath, sessionName string) error

LaunchTmuxSession launches a tmux session using tmuxp

func PruneWorktrees

func PruneWorktrees(repoPath string) error

PruneWorktrees removes stale worktree references. --expire now bypasses git's default 3-month grace period so recently-deleted worktrees are pruned immediately rather than showing as "missing but registered".

func PullFromOrigin

func PullFromOrigin(repoPath, branch string) error

PullFromOrigin pulls the latest changes from origin for the specified branch

func RemoteBranchExists added in v0.3.0

func RemoteBranchExists(repoPath, branchName string) (bool, error)

RemoteBranchExists checks if a branch exists on origin.

func RemoveSession

func RemoveSession(name string, sess *Session) error

RemoveSession removes a single session completely (helper for commands)

func RunCleanupCommand

func RunCleanupCommand(sess *Session) error

RunCleanupCommand executes the configured cleanup command with session environment variables

func RunCleanupCommandForShell

func RunCleanupCommandForShell(sess *Session) error

RunCleanupCommandForShell executes cleanup command through shell for complex commands

func SetAttentionFlag

func SetAttentionFlag(sessionName, reason string) error

SetAttentionFlag sets the attention flag for a session

func TerminateEditor

func TerminateEditor(sessionName string) error

TerminateEditor terminates the editor process for a session

func ValidatePort

func ValidatePort(port int) error

ValidatePort checks if a port number is valid

func WorktreeExists

func WorktreeExists(repoPath, worktreePath string) (bool, error)

WorktreeExists checks if a worktree exists at the specified path

Types

type EnvrcData

type EnvrcData struct {
	Ports          map[string]int    // service name -> port number
	Routes         map[string]string // service name -> local hostname (*.localhost)
	ExternalRoutes map[string]string // service name -> external hostname (*.domain.com), if CF configured
	Name           string
}

type PortAllocation

type PortAllocation struct {
	Ports map[string]int // service name -> port number
}

PortAllocation represents allocated ports with their service names

func AllocatePorts

func AllocatePorts(serviceNames []string) (*PortAllocation, error)

AllocatePorts allocates unique ports for the given service names

type Session

type Session struct {
	Name            string            `json:"name"`
	ProjectAlias    string            `json:"project_alias,omitempty"` // Reference to project in registry
	ProjectPath     string            `json:"project_path,omitempty"`  // Resolved project path
	Branch          string            `json:"branch"`
	Path            string            `json:"path"`
	Ports           map[string]int    `json:"ports"`
	Routes          map[string]string `json:"routes,omitempty"`     // service -> hostname mapping
	EditorPID       int               `json:"editor_pid,omitempty"` // PID of the editor process
	AttentionFlag   bool              `json:"attention_flag,omitempty"`
	AttentionReason string            `json:"attention_reason,omitempty"` // "claude_done", "claude_stuck", "manual", etc.
	AttentionTime   time.Time         `json:"attention_time,omitempty"`
	LastAttached    time.Time         `json:"last_attached,omitempty"`
	CreatedAt       time.Time         `json:"created_at"`
	UpdatedAt       time.Time         `json:"updated_at"`
}

type SessionStore

type SessionStore struct {
	Sessions      map[string]*Session `json:"sessions"`
	NumberedSlots map[int]string      `json:"numbered_slots,omitempty"`
}

func LoadRegistry

func LoadRegistry() (*SessionStore, error)

LoadRegistry is an alias for LoadSessions for compatibility

func LoadSessions

func LoadSessions() (*SessionStore, error)

LoadSessions loads the sessions from the metadata file

func (*SessionStore) AddSession

func (s *SessionStore) AddSession(name, branch, path string, ports map[string]int) error

AddSession adds a new session to the store

func (*SessionStore) AddSessionWithProject

func (s *SessionStore) AddSessionWithProject(name, branch, path string, ports map[string]int, projectAlias, projectPath string) error

AddSessionWithProject adds a new session to the store with project information

func (*SessionStore) AssignSlot added in v0.2.0

func (s *SessionStore) AssignSlot(name string) (int, error)

AssignSlot assigns a numbered slot (1-9) to a session. If the session already has a slot, returns the existing slot (stable). If a free slot exists, assigns the lowest available. If all 9 are full, evicts the session with the oldest LastAttached.

func (*SessionStore) GetSession

func (s *SessionStore) GetSession(name string) (*Session, bool)

GetSession retrieves a session by name

func (*SessionStore) GetSessionForSlot added in v0.2.0

func (s *SessionStore) GetSessionForSlot(slot int) string

GetSessionForSlot returns the session name assigned to a slot, or "" if empty.

func (*SessionStore) GetSlotForSession added in v0.2.0

func (s *SessionStore) GetSlotForSession(name string) int

GetSlotForSession returns the slot number for a session, or 0 if unassigned.

func (*SessionStore) ReconcileSlots added in v0.2.0

func (s *SessionStore) ReconcileSlots()

ReconcileSlots removes slot assignments for sessions that no longer exist.

func (*SessionStore) RecordAttach added in v0.2.0

func (s *SessionStore) RecordAttach(name string) error

RecordAttach updates the LastAttached timestamp for a session

func (*SessionStore) RemoveSession

func (s *SessionStore) RemoveSession(name string) error

RemoveSession removes a session from the store

func (*SessionStore) Save

func (s *SessionStore) Save() error

SaveSessions saves the sessions to the metadata file

func (*SessionStore) UpdateSession

func (s *SessionStore) UpdateSession(name string, updateFn func(*Session)) error

UpdateSession updates an existing session

type TmuxpData

type TmuxpData struct {
	Name           string
	Path           string
	Ports          map[string]int    // service name -> port number
	Routes         map[string]string // service name -> local hostname (*.localhost)
	ExternalRoutes map[string]string // service name -> external hostname (*.domain.com), if CF configured
}

type WorktreeInfo

type WorktreeInfo struct {
	Path   string
	Branch string
	Head   string
}

func ListWorktrees

func ListWorktrees(repoPath string) ([]WorktreeInfo, error)

ListWorktrees returns all git worktrees in the repository

Jump to

Keyboard shortcuts

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