tmux

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package tmux provides tmux session management for agent orchestration.

Each bc agent runs in an isolated tmux session, allowing for:

  • Concurrent agent execution
  • Session persistence across restarts
  • Direct terminal access for debugging

Basic Usage

Create a session manager:

mgr := tmux.NewWorkspaceManager("bc-", "/path/to/workspace")

Create a session:

err := mgr.CreateSession("eng-01", "/path/to/worktree")

Send commands to a session:

err := mgr.SendKeys("eng-01", "echo hello")

Capture output:

output, err := mgr.CapturePane("eng-01", 100) // last 100 lines

Session Naming

Sessions are prefixed and optionally include a workspace hash for isolation:

// With workspace hash: bc-a1b2c3-eng-01
mgr := tmux.NewWorkspaceManager("bc-", "/path/to/workspace")

// Without hash: bc-eng-01
mgr := tmux.NewManager("bc-")

Caching

The manager caches session existence checks to reduce subprocess calls. Cache is automatically invalidated when sessions are created or destroyed.

Index

Constants

View Source
const DefaultCacheTTL = 2 * time.Second

DefaultCacheTTL is the default time-to-live for cached session data.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	SessionPrefix string // Prepended to all session names (e.g., "bc-")
	// contains filtered or unexported fields
}

Manager handles tmux session operations.

func NewDefaultManager

func NewDefaultManager() *Manager

NewDefaultManager creates a new tmux manager with default prefix "bc-".

func NewManager

func NewManager(prefix string) *Manager

NewManager creates a new tmux manager with the given prefix.

func NewWorkspaceManager

func NewWorkspaceManager(prefix, workspacePath string) *Manager

NewWorkspaceManager creates a tmux manager scoped to a workspace. Session names include a short hash of the workspace path for isolation.

func (*Manager) AttachCmd

func (m *Manager) AttachCmd(ctx context.Context, name string) *exec.Cmd

AttachCmd returns an exec.Cmd to attach to a session. The caller should set Stdin/Stdout/Stderr and run it.

func (*Manager) Capture

func (m *Manager) Capture(ctx context.Context, name string, lines int) (string, error)

Capture captures the current pane content.

func (*Manager) CreateSession

func (m *Manager) CreateSession(ctx context.Context, name, dir string) error

CreateSession creates a new tmux session.

func (*Manager) CreateSessionWithCommand

func (m *Manager) CreateSessionWithCommand(ctx context.Context, name, dir, command string) error

CreateSessionWithCommand creates a session and runs a command.

func (*Manager) CreateSessionWithEnv

func (m *Manager) CreateSessionWithEnv(ctx context.Context, name, dir, command string, env map[string]string) error

CreateSessionWithEnv creates a session with env vars baked into the shell command. Environment variable keys are validated to prevent shell injection attacks. Keys must match POSIX standards: start with letter/underscore, contain only alphanumerics/underscores.

func (*Manager) HasSession

func (m *Manager) HasSession(ctx context.Context, name string) bool

HasSession checks if a session exists. Results are cached with a short TTL to reduce subprocess calls.

func (*Manager) IsRunning

func (m *Manager) IsRunning(ctx context.Context) bool

IsRunning checks if tmux server is running.

func (*Manager) KillServer

func (m *Manager) KillServer(ctx context.Context) error

KillServer kills the tmux server (all sessions).

func (*Manager) KillSession

func (m *Manager) KillSession(ctx context.Context, name string) error

KillSession kills a tmux session.

func (*Manager) ListSessions

func (m *Manager) ListSessions(ctx context.Context) ([]Session, error)

ListSessions lists all sessions with our prefix. Results are cached with a short TTL to reduce subprocess calls.

func (*Manager) PipePane

func (m *Manager) PipePane(ctx context.Context, name, logPath string) error

PipePane starts or stops streaming a session's output to a log file. If logPath is non-empty, starts piping output via "cat >> logPath". If logPath is empty, stops any existing pipe.

func (*Manager) RenameSession

func (m *Manager) RenameSession(ctx context.Context, oldName, newName string) error

RenameSession renames a tmux session.

func (*Manager) SendKeys

func (m *Manager) SendKeys(ctx context.Context, name, keys string) error

SendKeys sends keys to a session with Enter as submit key. This is a convenience wrapper around SendKeysWithSubmit.

func (*Manager) SendKeysWithSubmit

func (m *Manager) SendKeysWithSubmit(ctx context.Context, name, keys, submitKey string) error

SendKeysWithSubmit sends keys to a session with a specified submit key. For messages longer than 500 chars, uses tmux load-buffer/paste-buffer to avoid truncation. Trailing newlines are trimmed. submitKey specifies what to send after the message: - "Enter" sends the Enter key as a tmux key-name event - "" sends nothing (message is left in input buffer) - Other values are sent as tmux key names (e.g., "C-m" for Ctrl+M)

func (*Manager) SessionName

func (m *Manager) SessionName(name string) string

SessionName returns the full session name with prefix (and workspace hash if set).

func (*Manager) SetEnvironment

func (m *Manager) SetEnvironment(ctx context.Context, name, key, value string) error

SetEnvironment sets an environment variable in a session. Environment variable key is validated to prevent shell injection attacks. Key must match POSIX standards: start with letter/underscore, contain only alphanumerics/underscores.

func (*Manager) WithExecCommand

func (m *Manager) WithExecCommand(fn func(string, ...string) *exec.Cmd) *Manager

WithExecCommand returns a copy of the Manager with a custom command executor. This is primarily used for testing to mock subprocess calls.

type Session

type Session struct {
	Name      string
	Created   string
	Directory string
	Windows   int
	Attached  bool
}

Session represents a tmux session.

Jump to

Keyboard shortcuts

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