workspacecmd

package
v1.5.45 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 20 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 workspacecmd implements `golder workspace`: the command line that manages global named terminal sessions, each with an independent daemon.

golder workspace [run] [flags] [-- <agent args>...]   start it, and attach
golder workspace status [flags]                       one JSON status document
golder workspace stop [flags]                         shut it down

The package is the argv-and-identity half of the command and nothing more. It parses the flags, resolves a global name and its config/state/log/socket paths, and decides whether starting one here would nest it inside another multiplexer; then it hands one resolved Options value to a Backend, which owns the daemon. That split is why this file has no os.Exit, no process-wide chdir, no environment mutation, no signal subscription and no dependency outside the standard library plus internal/hostcompat and internal/golderhome: a policy that can only be tested against a real terminal is a policy that is not tested.

Three decisions are worth stating, because they are what the flags mean:

  • Visible by default. `golder workspace` and `golder workspace run` start a workspace a person can see and use. --headless is the explicit "daemon only" mode: it starts the same workspace, attaches nothing, and prints one readiness JSON document so a script can reach it.
  • Arguments after -- are an argv. They are handed to the golder agent this workspace hosts as tokens, never joined into a string and never re-parsed by a shell. `golder workspace -- --model opus` passes two arguments, and `golder workspace -- 'a; rm -rf b'` passes one literal one.
  • Names, not invocation directories, select a session. Config, state, log and ownership proof are per name under GOLDER_HOME; cwd is only the saved directory where its initial process starts.

Index

Constants

View Source
const (
	// ExitOK is success, including a help request.
	ExitOK = 0
	// ExitFailure is a workspace that could not be started, reached or
	// stopped: a backend error, an unusable state root, a refused nesting, a
	// backend that is not wired into this build.
	ExitFailure = 1
	// ExitUsage is argv this command cannot act on: an unknown flag, a flag
	// without its value, an extra argument, a session name the daemon would
	// refuse, a --socket path that cannot be a Unix socket.
	ExitUsage = 2
	// ExitInterrupted is a cancellation: the context was already done before
	// the backend was asked, or the backend returned because it was cancelled.
	// It is 128+SIGINT, the code a shell reports for a Ctrl+C'd command.
	ExitInterrupted = 130
)

Exit codes Run returns. They are exported because the dispatcher in cmd/golder passes the code straight to os.Exit and a script may want to name them; nothing else in this package exits a process.

Variables

This section is empty.

Functions

func CheckIdentity added in v1.5.5

func CheckIdentity(opts Options) error

CheckIdentity prevents a caller or a corrupt record from retargeting a named session's removable directory. Config and state can be explicit overrides; identity, manifest and log paths cannot.

func EnsureIdentityDirs added in v1.5.5

func EnsureIdentityDirs(opts Options) error

EnsureIdentityDirs walks each component owned by golder separately, refusing symlinks before creating anything underneath them. Runtime locks live beside sessions, not inside a directory `rm` can remove.

func Run

func Run(ctx context.Context, args []string, stdout, stderr io.Writer, backend Backend) int

Run executes one `golder workspace` invocation with args — the words after `workspace` in argv — writing its output to stdout and its errors to stderr. It returns the process exit code (see the Exit constants) and never exits, changes directory or touches the environment itself.

func RunWith

func RunWith(ctx context.Context, args []string, stdout, stderr io.Writer, backend Backend, deps Deps) int

RunWith is Run with the environment seams named, which is what a test needs to state a GOLDER_HOME and a multiplexer instead of inheriting one.

func RuntimeDir

func RuntimeDir(getenv func(string) string) string

RuntimeDir is the short, private directory default sockets live in: $XDG_RUNTIME_DIR/golder, else $TMPDIR/golder-<uid>, else /tmp/golder-<uid>. It is exported because a backend writing a pid file or a lock beside the socket should use the same directory rather than guess at one.

A socket path is limited by the kernel to about a hundred bytes, so this is deliberately not under the state root: $GOLDER_HOME can be arbitrarily deep, and a project directory is usually deeper still. Neither is it in the runtime directory bare: a socket in a world-writable /tmp is a socket anyone can connect to, so it sits one level down, in a directory this command creates 0700 and keeps that way (see ensurePrivateDir). Under a shared temp directory the name carries the uid, so two accounts on one machine cannot contend for the same socket directory, pid file or lock; see privatepath.RuntimeRoot, which owns the rule.

func SaveSession added in v1.5.5

func SaveSession(opts Options) error

SaveSession writes one named session's record atomically. It never writes a legacy session; those have no record and are addressed only by --legacy.

func SessionLockPath added in v1.5.5

func SessionLockPath(opts Options) string

SessionLockPath is stable across removal/recreation of the session record.

Types

type Appearance

type Appearance struct {
	// ASCIIOnly draws panes without Nerd Font glyphs.
	ASCIIOnly bool
	// NoAnimations disables the workspace's UI animations.
	NoAnimations bool
	// Theme is a colour theme name, "" for the configured one.
	Theme string
}

Appearance is the small subset of the workspace's display settings the command line accepts. A zero Appearance asks for the configured defaults, which is what every field means when it is unset: there is a flag to turn icons and animations off and none to turn them back on.

type Backend

type Backend interface {
	// Start starts the workspace Options describes and returns once it is
	// ready to use. In visible mode it also attaches or opens a window on it,
	// which normally means Start returns when the person leaves.
	Start(ctx context.Context, opts Options) (StartResult, error)
	// Status returns the workspace's status as one JSON document. The command
	// re-indents and validates it; it does not interpret it.
	Status(ctx context.Context, opts Options) ([]byte, error)
	// Stop stops the workspace and its daemon. Stopping a workspace that is
	// not running is a backend's decision; the usual answer is success.
	Stop(ctx context.Context, opts Options) error
}

Backend is the workspace implementation behind the command: the daemon, its panes and its client. It is an interface here, and only here, so that the command line can be read and tested without a terminal, a daemon or a binary on PATH.

The command owns the argv surface, the project identity, the paths and the exit codes; a backend owns everything that touches the machine. Every method receives the same fully resolved Options, and must respect Headless (start the daemon and return, attach nothing) and Shell (no initial agent).

type Command

type Command string

Command is the operation one `golder workspace` invocation asks for.

const (
	// CommandRun starts a workspace: visible and attached unless
	// Options.Headless, which starts the daemon alone.
	CommandRun Command = "run"
	// CommandNew starts a workspace only if the named session does not exist.
	CommandNew Command = "new"
	// CommandAttach connects to a running session and never starts one.
	CommandAttach Command = "attach"
	// CommandList reports every named session on this machine.
	CommandList Command = "list"
	// CommandStatus reports a running workspace as one JSON document.
	CommandStatus Command = "status"
	// CommandStop stops a running workspace.
	CommandStop Command = "stop"
	// CommandRemove deletes a stopped session's record and directories.
	CommandRemove Command = "rm"
)

type Deps

type Deps struct {
	// Getenv reads the environment: GOLDER_HOME, the runtime directory and the
	// multiplexer markers hostcompat parses.
	Getenv func(string) string
	// Getwd is the project directory when --cwd was not given.
	Getwd func() (string, error)
}

Deps are the seams a test (or a driver that knows better) can replace. Every field is optional; a zero Deps reads the real environment and the real working directory.

type Options

type Options struct {
	// Command is the operation being asked for.
	Command Command
	// Paths is where this workspace keeps its files. It is embedded, so
	// opts.StateDir reads as one field and opts.Paths reads as the whole set.
	Paths
	// Cwd is the session's saved initial pane directory, or an explicit update
	// for a stopped session. It need not exist to query or attach a live owner.
	Cwd string
	// Session is the name (-s/--session, default "default").
	Session string
	// ManifestPath is where this session's record lives. Empty for a legacy
	// workspace, which predates the record.
	ManifestPath string
	// Exists reports that a session record (or a legacy session directory) is
	// already on disk. It is what distinguishes "run this stopped session" from
	// "create it".
	Exists bool
	// Legacy marks a session in the pre-global layout. It can be attached,
	// inspected and stopped, but never created or removed through the new
	// commands.
	Legacy bool
	// CwdExplicit records that -C/--cwd was given. On an existing session it
	// is an update to the saved start directory, not a lookup key.
	CwdExplicit bool
	// SocketExplicit records that --socket was given for a session that is
	// being created. An existing session keeps its recorded endpoint.
	SocketExplicit bool
	// ConfigExplicit records that --config was given for a new session.
	ConfigExplicit bool
	// StateDirExplicit records that --state-dir named StateDir.
	StateDirExplicit bool
	// CreatedAt is the first time this session was created, carried through
	// saves so a rewritten record does not claim to be new.
	CreatedAt string
	// JSON asks list for its machine-readable document.
	JSON bool
	// DiscoveryError keeps a broken record visible in list without probing an
	// endpoint taken from untrusted/incomplete metadata.
	DiscoveryError string
	// Socket is the daemon's control socket, absolute.
	Socket string
	// Headless asks for the daemon alone: no client is attached, and Start
	// returns as soon as the workspace is ready.
	Headless bool
	// Shell asks for a workspace whose first pane is a shell rather than an
	// agent. AgentArgs is then empty, by construction (see parsed.validate).
	Shell bool
	// Nested records that --nested was given: this workspace was asked for
	// inside another multiplexer, and the person knows.
	Nested bool
	// Host is the multiplexer this process runs in, as hostcompat names it
	// ("golder-workspace", "tuios", "herdr", "tmux", "zellij", "screen"), or
	// "" in a plain terminal. A backend may use it to say where the workspace
	// was started from; it has already been used for the nesting decision.
	Host string
	// AgentArgs is the argv after "--": the golder agent's own arguments,
	// exactly as typed. It is never joined into a command line.
	AgentArgs []string
	// Appearance is the display settings asked for on the command line.
	Appearance Appearance
}

Options is one resolved `golder workspace` invocation: everything a backend needs, and nothing it has to derive for itself.

func Discover added in v1.5.5

func Discover(home string) ([]Options, error)

Discover lists every named session and every legacy session directory under home. It reads records only: no daemon is started, no socket is created and no saved start directory has to exist.

func ReloadSession added in v1.5.5

func ReloadSession(opts Options) (Options, error)

ReloadSession re-reads a record after a caller has acquired the session lock. The first resolve happened without that lock, so a concurrent start could have created or rewritten the record in between; the backend must not decide "this is new" from the earlier answer.

func (Options) Selector added in v1.5.5

func (o Options) Selector() string

Selector is the explicit, unambiguous CLI target shown by list. Legacy names are qualified by the old project id, since two projects may share a name.

type Paths

type Paths struct {
	// Home is golder's state root: $GOLDER_HOME, else ~/.golder.
	Home string
	// Project is the legacy reporting alias of Cwd, not an ownership boundary.
	Project string
	// ProjectID is populated only for a pre-global legacy session.
	ProjectID string
	// ProjectDir is the old project directory for legacy sessions and the
	// identity directory (SessionDir) for new named sessions.
	ProjectDir string
	// ConfigPath is per named session; an explicit override is recorded.
	ConfigPath string
	// SessionDir holds this session's record, log, owner proofs and default state.
	SessionDir string
	// StateDir is the workspace's state directory: --state-dir when it was
	// given, else state/ in SessionDir. The default is per session, so two
	// sessions of one project never share a session store; --state-dir is the
	// person's own decision, and two sessions given the same directory do share
	// it, which is what StateDirExplicit records for a backend that has to
	// arbitrate (its lock decides who owns it, not this package).
	StateDir string
	// LogFile is the workspace daemon's log, in SessionDir: one session's
	// history is one file to read, and two sessions never append to one file.
	LogFile string
	// SocketDir is the private directory the default socket is created in, and
	// "" when --socket named a path (a custom socket is used verbatim, and its
	// directory is the person's to manage).
	SocketDir string
}

Paths are the durable and runtime locations one workspace uses.

type Remover added in v1.5.5

type Remover interface {
	Remove(context.Context, Options) error
}

Remover is the optional backend operation `workspace rm` needs. Keeping it outside Backend lets a test or another backend omit removal without growing a no-op method.

type SessionRecord added in v1.5.5

type SessionRecord struct {
	Version   int    `json:"version"`
	Name      string `json:"name"`
	Cwd       string `json:"cwd"`
	Socket    string `json:"socket"`
	SocketDir string `json:"socket_dir,omitempty"`
	Config    string `json:"config"`
	StateDir  string `json:"state_dir"`
	Log       string `json:"log"`
	CreatedAt string `json:"created_at"`
}

SessionRecord is one named session's durable identity. It deliberately holds no secret: the socket path is a filesystem name, and the ownership proof stays in the daemon's private owner record.

type StartResult

type StartResult struct {
	// Session is the session the workspace is running under. Empty keeps the
	// name the options asked for.
	Session string `json:"session"`
	// Socket is the daemon socket the workspace listens on. Empty keeps the
	// path the options named.
	Socket   string `json:"socket"`
	Cwd      string `json:"cwd"`
	Config   string `json:"config"`
	StateDir string `json:"state_dir"`
	Log      string `json:"log"`
	// Created and Instance distinguish the daemon this call started from an
	// existing owner. A failed client may clean up only its own new instance.
	Created  bool   `json:"created"`
	Instance string `json:"instance"`
}

StartResult is what a backend reports once a workspace is up. It exists so a backend can name what actually happened — a daemon that generated its own session name, a socket the runtime chose — without the caller having to re-derive it.

Jump to

Keyboard shortcuts

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