Documentation
¶
Overview ¶
Package session is the session service: create, recycle, rename, delete and prune sessions, and spawn their terminals.
Index ¶
- func RenderUserCommandWindows(renderer *tmpl.Renderer, windows []config.WindowConfig, data map[string]any) ([]multiplexer.WindowSpec, error)
- func RenderWindows(renderer *tmpl.Renderer, windows []config.WindowConfig, data SpawnData) ([]multiplexer.WindowSpec, error)
- func Target(sess session.Session) multiplexer.Target
- type CreateOptions
- type CreateSessionError
- type Detector
- type FileCopier
- type HookRunner
- type LaunchError
- type LaunchOptions
- type LaunchRepository
- type LaunchRequest
- type Lifecycle
- type Multiplexer
- type NewSessionRequest
- type OutputStyler
- type PlainStyler
- type RecycleData
- type Recycler
- type Risk
- type Service
- func (s *Service) AddWindowsToTmuxSession(ctx context.Context, tmuxName, workDir string, ...) error
- func (s *Service) AttachOrSwitch(ctx context.Context, target multiplexer.Target, ...) error
- func (s *Service) CheckRisk(ctx context.Context, sess session.Session) Risk
- func (s *Service) CheckSessionRisk(ctx context.Context, id string) (Risk, error)
- func (s *Service) CreateFromRequest(ctx context.Context, req LaunchRequest) (session.Session, error)
- func (s *Service) CreateSession(ctx context.Context, opts CreateOptions) (*session.Session, error)
- func (s *Service) CreateSessionWithWindows(ctx context.Context, req NewSessionRequest, windows []multiplexer.WindowSpec, ...) error
- func (s *Service) CurrentSession(ctx context.Context) (multiplexer.Target, error)
- func (s *Service) DeleteSession(ctx context.Context, id string) error
- func (s *Service) DetectRemote(ctx context.Context, dir string) (string, error)
- func (s *Service) DetectSession(ctx context.Context) (string, error)
- func (s *Service) GetSession(ctx context.Context, id string) (session.Session, error)
- func (s *Service) Git() git.Git
- func (s *Service) KillTmuxWindow(ctx context.Context, target multiplexer.Target) error
- func (s *Service) ListSessions(ctx context.Context) ([]session.Session, error)
- func (s *Service) OpenTmuxSession(ctx context.Context, name, path, remote, targetWindow string, background bool) error
- func (s *Service) Prune(ctx context.Context, all bool) (int, error)
- func (s *Service) RecycleSession(ctx context.Context, id string, w io.Writer) error
- func (s *Service) RenameSession(ctx context.Context, id, newName string) error
- func (s *Service) ResolveSessionLaunchRepository(ctx context.Context, remote string) (LaunchRepository, error)
- func (s *Service) SessionAtPath(ctx context.Context, path string) (session.Session, bool, error)
- func (s *Service) SessionLaunchOptions(ctx context.Context) (LaunchOptions, error)
- func (s *Service) SetSessionGroup(ctx context.Context, id, group string) error
- func (s *Service) SilenceOutput() (restore func())
- func (s *Service) SwitchTmuxSession(ctx context.Context, name string) error
- type SessionCreator
- type SpawnData
- type Spawner
- func (s *Spawner) AddWindowsToTmuxSession(ctx context.Context, tmuxName, workDir string, ...) error
- func (s *Spawner) OpenWindows(ctx context.Context, windows []config.WindowConfig, data SpawnData, ...) error
- func (s *Spawner) OpenWindowsWith(ctx context.Context, windows []config.WindowConfig, data SpawnData, ...) error
- func (s *Spawner) Spawn(ctx context.Context, commands []string, data SpawnData) error
- func (s *Spawner) SpawnWindows(ctx context.Context, windows []config.WindowConfig, data SpawnData, ...) error
- func (s *Spawner) SpawnWindowsWith(ctx context.Context, windows []config.WindowConfig, data SpawnData, ...) error
- func (s *Spawner) SpawnWith(ctx context.Context, commands []string, data SpawnData, ...) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RenderUserCommandWindows ¶
func RenderUserCommandWindows(renderer *tmpl.Renderer, windows []config.WindowConfig, data map[string]any) ([]multiplexer.WindowSpec, error)
RenderUserCommandWindows renders windows from a UserCommand using the provided template data map. Unlike RenderWindows, it accepts map[string]any to include .Form and session variables.
func RenderWindows ¶
func RenderWindows(renderer *tmpl.Renderer, windows []config.WindowConfig, data SpawnData) ([]multiplexer.WindowSpec, error)
RenderWindows renders a slice of WindowConfig templates against SpawnData, producing fully resolved window specifications for the multiplexer client.
Types ¶
type CreateOptions ¶
type CreateOptions struct {
Name string // Session name (used in path)
SessionID string // Session ID (auto-generated if empty)
Prompt string // Prompt to pass to spawned terminal (batch only)
Remote string // Git remote URL to clone (auto-detected if empty)
Source string // Source directory for file copying
UseBatchSpawn bool // Use batch_spawn commands instead of spawn
Background bool // Create session without attaching to tmux
// CloneStrategy selects the clone method: "full" (default) or "worktree".
// Empty resolves via config rule matching, then global config, then "full".
CloneStrategy string
// SkipSpawn skips the configured spawn strategy (spawn: / batch_spawn: / windows:).
// The caller is responsible for launching any terminal or tmux session. Use this
// when the session directory is needed but terminal management happens elsewhere
// (e.g. CreateSessionWithWindows, which creates the tmux session itself).
SkipSpawn bool
// AgentKey selects a named agent profile for this session's spawn command.
// When non-empty, the spawn templates use that profile's command/flags instead
// of the process-wide default. Must match a key in config.Agents.Profiles.
AgentKey string
// Tags are user-defined labels attached to the session for external provider tracking.
Tags []string
// CollisionSuffix is appended to a Name whose slug is taken. Use an id that
// is stable across retries, so a retry collides with its own earlier session.
CollisionSuffix string
// Progress receives human-readable progress lines during session creation.
// When non-nil, service output (hooks, file copies) is also redirected here.
Progress io.Writer
}
CreateOptions configures session creation.
type CreateSessionError ¶
type CreateSessionError struct {
Operation string
Destination string
CloneStrategy string
Err error
}
CreateSessionError describes a session creation step that failed after its destination was resolved.
func (*CreateSessionError) Error ¶
func (e *CreateSessionError) Error() string
func (*CreateSessionError) Unwrap ¶
func (e *CreateSessionError) Unwrap() error
type Detector ¶
type Detector struct {
// contains filtered or unexported fields
}
Detector finds the current session from the working directory.
func NewDetector ¶
NewDetector creates a new session detector.
func (*Detector) DetectSession ¶
DetectSession returns the session ID for the current working directory. Returns empty string if not in a hive session, or an error if detection fails.
func (*Detector) DetectSessionFromPath ¶
DetectSessionFromPath returns the session ID for the given path. Returns empty string if the path is not within a hive session, or an error if detection fails.
type FileCopier ¶
type FileCopier struct {
// contains filtered or unexported fields
}
FileCopier copies files from a source directory to a destination.
func NewFileCopier ¶
func NewFileCopier(log zerolog.Logger, styler OutputStyler, stdout io.Writer) *FileCopier
NewFileCopier creates a new FileCopier.
type HookRunner ¶
type HookRunner struct {
// contains filtered or unexported fields
}
HookRunner executes repository-specific setup hooks.
func NewHookRunner ¶
func NewHookRunner(log zerolog.Logger, executor executil.Executor, renderer *tmpl.Renderer, styler OutputStyler, stdout, stderr io.Writer) *HookRunner
NewHookRunner creates a new HookRunner.
type LaunchError ¶
type LaunchError struct {
Name string
Remote string
// Destination and CloneStrategy come off CreateSessionError. They are
// empty for a failure raised before a checkout was resolved, or after the
// clone (a spawn that failed).
Destination string
CloneStrategy string
// LeftoverCheckout reports that Destination is still on disk: git removes
// its own directory when it refuses a clone, but keeps a complete one when
// a post-checkout hook fails.
LeftoverCheckout bool
// Step is the failed operation ("clone repository", "worktree add") when
// CreateSessionError carries one, and otherwise the last progress line.
Step string
Output string
Err error
}
LaunchError is a failed CreateFromRequest, carrying what a caller needs to hand the request back as a retryable draft. Nothing should match on its text (ADR a-failed-session-creation-is-a-retryable-draft).
func (*LaunchError) Error ¶
func (e *LaunchError) Error() string
func (*LaunchError) Unwrap ¶
func (e *LaunchError) Unwrap() error
type LaunchOptions ¶
type LaunchOptions struct {
Repositories []LaunchRepository
DefaultRepository string
Agents []string
DefaultAgent string
}
LaunchOptions supplies the configured repositories and agents for an interactive session launcher.
type LaunchRepository ¶
LaunchRepository is a repository available to an interactive session launch. Source is an existing local checkout used for Hive's normal file-copy behavior; desktop maps this to a narrower presentation DTO.
type LaunchRequest ¶
type LaunchRequest struct {
Name string
Prompt string
Agent string
Repo string
CollisionSuffix string
// Tags label the session for a reader inside hive. Empty and repeated
// tags are dropped.
Tags []string
}
LaunchRequest asks for a detached session the way a launcher does: by name and by a repository that may be any spelling of a configured checkout's remote.
type Lifecycle ¶
type Lifecycle interface {
CurrentSession(ctx context.Context) (multiplexer.Target, error)
AttachOrSwitch(ctx context.Context, target multiplexer.Target, streams multiplexer.AttachStreams) error
RenameSession(ctx context.Context, target multiplexer.Target, newName string) error
KillSession(ctx context.Context, target multiplexer.Target) error
KillWindow(ctx context.Context, target multiplexer.Target) error
}
Lifecycle manages existing multiplexer sessions and windows.
type Multiplexer ¶
type Multiplexer interface {
SessionCreator
Lifecycle
}
Multiplexer combines the creation and lifecycle operations used by Hive.
type NewSessionRequest ¶
type NewSessionRequest struct {
Name string
Remote string
// ShCmd runs in the new session's directory after the clone and before
// windows open. A non-zero exit aborts window creation.
ShCmd string
}
NewSessionRequest describes a Hive session to create before windows are opened in it.
type OutputStyler ¶
type OutputStyler interface {
Header(s string) string
Muted(s string) string
Text(s string) string
}
OutputStyler styles the headers that hooks and file copies print. The caller supplies it, so this package has no rendering dependency.
type PlainStyler ¶
type PlainStyler struct{}
PlainStyler returns every string unchanged.
func (PlainStyler) Header ¶
func (PlainStyler) Header(s string) string
func (PlainStyler) Muted ¶
func (PlainStyler) Muted(s string) string
func (PlainStyler) Text ¶
func (PlainStyler) Text(s string) string
type RecycleData ¶
type RecycleData struct {
DefaultBranch string
}
RecycleData contains template data for recycle commands.
type Recycler ¶
type Recycler struct {
// contains filtered or unexported fields
}
Recycler handles resetting a session environment for reuse.
func NewRecycler ¶
NewRecycler creates a new Recycler.
func (*Recycler) Recycle ¶
func (r *Recycler) Recycle(ctx context.Context, path string, commands []string, data RecycleData, w io.Writer) error
Recycle executes recycle commands sequentially in the session directory. Commands are rendered as Go templates with the provided data. Output is written to the provided writer. If w is nil, output is discarded.
type Risk ¶
Risk describes uncommitted or unpushed work that would be lost if a session is deleted or recycled. Only meaningful for active sessions.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service orchestrates hive session operations.
func NewService ¶
func NewService( log zerolog.Logger, sessions session.Store, gitClient git.Git, cfg *config.Config, bus *events.EventBus, exec executil.Executor, renderer *tmpl.Renderer, styler OutputStyler, stdout, stderr io.Writer, client Multiplexer, ) *Service
NewService creates a new Service.
func (*Service) AddWindowsToTmuxSession ¶
func (s *Service) AddWindowsToTmuxSession(ctx context.Context, tmuxName, workDir string, windows []multiplexer.WindowSpec, background bool) error
AddWindowsToTmuxSession adds windows to an existing tmux session.
func (*Service) AttachOrSwitch ¶
func (s *Service) AttachOrSwitch(ctx context.Context, target multiplexer.Target, streams multiplexer.AttachStreams) error
AttachOrSwitch connects to a qualified multiplexer target.
func (*Service) CheckRisk ¶
CheckRisk is CheckSessionRisk for a session the caller has already read.
func (*Service) CheckSessionRisk ¶
CheckSessionRisk checks whether an active session has uncommitted or unpushed changes. Non-active sessions (recycled, corrupted) always return an empty risk. Git errors are treated conservatively: IsClean failures assume dirty; HasUnpushedCommits failures assume no unpushed commits.
func (*Service) CreateFromRequest ¶
func (s *Service) CreateFromRequest(ctx context.Context, req LaunchRequest) (session.Session, error)
CreateFromRequest creates a detached session for req. It prefers a configured local checkout whose remote is equivalent to req.Repo, so the session copies files from it.
A duplicate name returns session.ErrDuplicateName unwrapped, because the caller asks for another name rather than showing a failure. Any other creation failure is a *LaunchError.
func (*Service) CreateSession ¶
CreateSession creates a new session or recycles an existing one.
func (*Service) CreateSessionWithWindows ¶
func (s *Service) CreateSessionWithWindows(ctx context.Context, req NewSessionRequest, windows []multiplexer.WindowSpec, background bool) error
CreateSessionWithWindows creates a new Hive session, optionally runs shCmd in its directory, then opens tmux windows in it. Non-zero shCmd exit aborts window creation.
func (*Service) CurrentSession ¶
CurrentSession returns the current multiplexer session identity.
func (*Service) DeleteSession ¶
DeleteSession removes a session and its directory.
func (*Service) DetectRemote ¶
DetectRemote gets the git remote URL from the specified directory.
func (*Service) DetectSession ¶
DetectSession returns the session ID for the current working directory. Returns empty string if not in a hive session.
func (*Service) GetSession ¶
GetSession returns a session by ID.
func (*Service) KillTmuxWindow ¶
KillTmuxWindow kills one qualified tmux window.
func (*Service) ListSessions ¶
ListSessions returns all sessions.
func (*Service) OpenTmuxSession ¶
func (s *Service) OpenTmuxSession(ctx context.Context, name, path, remote, targetWindow string, background bool) error
OpenTmuxSession opens (or creates) a tmux session for the given session parameters. It resolves the spawn strategy, renders window templates, and delegates to the spawner.
func (*Service) Prune ¶
Prune removes recycled and corrupted sessions and their directories. If all is true, deletes ALL recycled sessions. If all is false, respects max_recycled limit per repository (keeps newest N).
func (*Service) RecycleSession ¶
RecycleSession marks a session for recycling and runs recycle commands. The session directory is not moved; only the DB record state changes. Output is written to w. If w is nil, output is discarded.
func (*Service) RenameSession ¶
RenameSession changes the name (and slug) of an existing session.
func (*Service) ResolveSessionLaunchRepository ¶
func (s *Service) ResolveSessionLaunchRepository(ctx context.Context, remote string) (LaunchRepository, error)
ResolveSessionLaunchRepository selects a known local checkout when the requested remote is equivalent. Non-GitHub and local paths match only by their exact spelling, preserving explicit choices made by the caller.
func (*Service) SessionAtPath ¶
SessionAtPath returns the active session whose checkout holds path, and false when path is inside none.
func (*Service) SessionLaunchOptions ¶
func (s *Service) SessionLaunchOptions(ctx context.Context) (LaunchOptions, error)
SessionLaunchOptions lists repositories from configured workspaces followed by existing on-disk Hive sessions. Equivalent GitHub remote spellings are coalesced so a local configured checkout wins over a Hive clone without rewriting either configured URL.
func (*Service) SetSessionGroup ¶
SetSessionGroup sets or clears the user-assigned group for a session. An empty group clears the assignment.
func (*Service) SilenceOutput ¶
func (s *Service) SilenceOutput() (restore func())
SilenceOutput redirects all output to io.Discard and returns a restore function that reverts to the previous writers. Call before starting the TUI to prevent hook and spawn output from corrupting the terminal display.
type SessionCreator ¶
type SessionCreator interface {
CreateSession(ctx context.Context, spec multiplexer.SessionSpec) error
OpenSession(ctx context.Context, spec multiplexer.SessionSpec, selection multiplexer.Target) error
AddWindows(ctx context.Context, target multiplexer.Target, windows []multiplexer.WindowSpec) error
AttachOrSwitch(ctx context.Context, target multiplexer.Target, streams multiplexer.AttachStreams) error
}
SessionCreator is the interface used by consumers that create and open sessions.
type SpawnData ¶
type SpawnData struct {
Path string // Absolute path to session directory
Name string // Session name (display name)
Prompt string // User-provided prompt (batch only)
Slug string // Session slug (URL-safe version of name)
ContextDir string // Path to context directory
Owner string // Repository owner
Repo string // Repository name
}
SpawnData is the template context for spawn commands.
type Spawner ¶
type Spawner struct {
// contains filtered or unexported fields
}
Spawner handles terminal spawning with template rendering.
func NewSpawner ¶
func NewSpawner(log zerolog.Logger, executor executil.Executor, renderer *tmpl.Renderer, tmuxClient SessionCreator, stdout, stderr io.Writer) *Spawner
NewSpawner creates a new Spawner.
func (*Spawner) AddWindowsToTmuxSession ¶
func (s *Spawner) AddWindowsToTmuxSession(ctx context.Context, tmuxName, workDir string, windows []multiplexer.WindowSpec, background bool) error
AddWindowsToTmuxSession adds pre-rendered windows to an existing tmux session. Windows without a working directory inherit workDir. If background is false, it switches to the session after adding windows.
func (*Spawner) OpenWindows ¶
func (s *Spawner) OpenWindows(ctx context.Context, windows []config.WindowConfig, data SpawnData, background bool, targetWindow string) error
OpenWindows renders window templates and opens (or creates) a tmux session. If the session already exists, it attaches to it (optionally selecting targetWindow).
func (*Spawner) OpenWindowsWith ¶
func (s *Spawner) OpenWindowsWith(ctx context.Context, windows []config.WindowConfig, data SpawnData, background bool, targetWindow string, renderer *tmpl.Renderer) error
OpenWindowsWith renders window templates using the given renderer and opens (or creates) a tmux session.
func (*Spawner) SpawnWindows ¶
func (s *Spawner) SpawnWindows(ctx context.Context, windows []config.WindowConfig, data SpawnData, background bool) error
SpawnWindows renders window templates and creates a tmux session.