config

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package config owns Loom's on-disk configuration, app state, profile catalog, and workspace registry.

The root config directory defaults to ~/.loom (overridable via $LOOM_HOME). WorkspaceContext carries the resolved directory for a given invocation and is threaded through the app rather than relying on globals, so per-workspace configs at <repo>/.loom/ can coexist with the global config. LoadConfigFrom/LoadStateFrom accept an empty path to mean "use the default directory."

MigrateLegacyHome performs the one-shot rename from ~/.claude-squad to ~/.loom that covers users upgrading across the rebrand; it is idempotent and safe to call on every startup.

Index

Constants

View Source
const (
	StateFileName     = "state.json"
	InstancesFileName = "instances.json"
)

StateFileName is the on-disk filename for ephemeral app state (help-screen-seen flags, etc.). InstancesFileName stores the serialized session list. Both are resolved relative to the config directory.

View Source
const (
	ConfigFileName = "config.json"
)

ConfigFileName is the on-disk filename of the Loom user config, resolved relative to the config directory (LOOM_HOME or ~/.loom).

View Source
const (
	EnvHome = "LOOM_HOME"
)

EnvHome names the override env var for the application's config directory. Legacy CLAUDE_SQUAD_HOME is honored as a deprecated fallback with a one-time warning so shell configs continue working across the rename from claude-squad to loom.

Variables

This section is empty.

Functions

func AtomicWriteFile

func AtomicWriteFile(path string, data []byte, perm os.FileMode) error

AtomicWriteFile writes data to path via a temp file in the same directory, fsyncs it, and renames it into place. This guarantees readers see either the old contents or the new contents, never a truncated or empty file.

The temp file is created with O_EXCL so concurrent writers don't share it. If the write or rename fails, the temp file is removed and the original file is left untouched.

After rename we also fsync the parent directory so the directory entry update is durable; without it a power loss between rename and the next filesystem commit can leave the old contents (or, on data=writeback mode, an empty file) visible after reboot. Best-effort — silently a no-op on filesystems/platforms (e.g. Windows) that don't support directory fsync.

func EnsureGitignore

func EnsureGitignore(repoPath string) error

EnsureGitignore ensures .loom/ is listed in the repo's .gitignore. Writes atomically via AtomicWriteFile so a crash or a concurrent call can't leave a half-written entry behind. An O_APPEND-based write would issue two syscalls (newline fixup, then entry), and racing callers each saw an absent entry and each appended their own copy.

func GetClaudeCommand

func GetClaudeCommand() (string, error)

GetClaudeCommand attempts to find the "claude" command in the user's shell It checks in the following order: 1. Shell alias resolution: using "which" command 2. PATH lookup

If both fail, it returns an error.

func GetConfigDir

func GetConfigDir() (string, error)

GetConfigDir returns the path to the application's configuration directory. If LOOM_HOME is set, that value is used directly as the config directory. CLAUDE_SQUAD_HOME is honored as a deprecated fallback with a one-time warning. The value must be an absolute path (after ~ expansion). Falls back to ~/.loom otherwise.

func GetGlobalConfigDir

func GetGlobalConfigDir() (string, error)

GetGlobalConfigDir returns ~/.loom/ regardless of LOOM_HOME.

func MigrateLegacyHome

func MigrateLegacyHome() error

MigrateLegacyHome performs a one-time rename of the legacy config directory (~/.claude-squad) to its new location (~/.loom) so that in-flight instances, worktrees, and scripts survive the rename.

Behavior:

  • If ~/.loom already exists, return (idempotent; nothing to do).
  • If ~/.claude-squad is absent, return (fresh install).
  • Otherwise os.Rename the directory and print a one-line notice to stderr so the operator knows what happened.

When LOOM_HOME or CLAUDE_SQUAD_HOME is set (i.e. the user has overridden the default path), migration is skipped — the user has explicitly chosen a location and is assumed to have already placed their state there.

Errors from os.Rename (e.g. cross-device) are returned; the caller should fall through to normal startup on failure since the legacy dir is still readable and GetConfigDir will honor CLAUDE_SQUAD_HOME as a deprecated fallback if the user points at it explicitly.

func SaveConfigTo

func SaveConfigTo(config *Config, dir string) error

SaveConfigTo saves the configuration to an explicit directory.

func SaveState

func SaveState(state *State) error

SaveState saves the state to disk

func SaveStateTo

func SaveStateTo(state *State, dir string) error

SaveStateTo saves state to an explicit directory.

func SaveWorkspaceRegistry

func SaveWorkspaceRegistry(reg *WorkspaceRegistry) error

SaveWorkspaceRegistry writes the workspace registry to the global config dir.

func WorkspaceConfigDir

func WorkspaceConfigDir(ws *Workspace) string

WorkspaceConfigDir returns the .loom directory inside the workspace repo.

Types

type AppState

type AppState interface {
	// GetHelpScreensSeen returns the bitmask of seen help screens
	GetHelpScreensSeen() uint32
	// SetHelpScreensSeen updates the bitmask of seen help screens
	SetHelpScreensSeen(seen uint32) error
}

AppState handles application-level state

type Config

type Config struct {
	// DefaultProgram is the default program to run in new instances
	DefaultProgram string `json:"default_program"`
	// AutoYes is a flag to automatically accept all prompts.
	AutoYes bool `json:"auto_yes"`
	// DaemonPollInterval is the interval (ms) at which the daemon polls sessions for autoyes mode.
	DaemonPollInterval int `json:"daemon_poll_interval"`
	// BranchPrefix is the prefix used for git branches created by the application.
	BranchPrefix string `json:"branch_prefix"`
	// Profiles is a list of named program profiles.
	Profiles []Profile `json:"profiles,omitempty"`
}

Config represents the application configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default configuration

func LoadConfig

func LoadConfig() *Config

LoadConfig loads configuration from the default config directory. It is a convenience wrapper around LoadConfigFrom(""). If the config file does not yet exist, a default config is written to disk.

func LoadConfigFrom

func LoadConfigFrom(dir string) *Config

LoadConfigFrom loads configuration from an explicit directory. Empty `dir` is a soft shim: a warning is logged and the global config directory is used. Per docs/specs/workspaces.md §3 internal callers must pass a resolved WorkspaceContext.ConfigDir.

func LoadConfigFromGlobal

func LoadConfigFromGlobal() *Config

LoadConfigFromGlobal loads configuration from the global (LOOM_HOME or ~/.loom) directory. Prefer this over LoadConfigFrom("") at call sites that truly want the global config — the explicit name prevents workspace-context leaks.

func (*Config) GetProfiles

func (c *Config) GetProfiles() []Profile

GetProfiles returns a unified list of profiles. If Profiles is defined, those are returned with the default profile first. Otherwise, a single profile is synthesized from DefaultProgram.

func (*Config) GetProgram

func (c *Config) GetProgram() string

GetProgram returns the program to run. If Profiles is non-empty and DefaultProgram matches a profile name, that profile's Program is returned. Otherwise DefaultProgram is returned as-is.

type InstanceStorage

type InstanceStorage interface {
	// SaveInstances saves the raw instance data
	SaveInstances(instancesJSON json.RawMessage) error
	// GetInstances returns the raw instance data
	GetInstances() json.RawMessage
	// DeleteAllInstances removes all stored instances
	DeleteAllInstances() error
}

InstanceStorage handles instance-related operations

type Profile

type Profile struct {
	// Name is the short identifier shown in the profile picker. Must be
	// unique within a Config's Profiles list.
	Name string `json:"name"`
	// Program is the literal command string run when this profile is
	// selected (e.g. "aider --model gpt-4"). No templating is applied.
	Program string `json:"program"`
}

Profile is a named shortcut for a program invocation. Profiles let users store multiple agent configurations (e.g. "aider-gpt4", "claude-fast") and pick between them at session creation or by setting DefaultProgram to a profile's Name.

type State

type State struct {
	// HelpScreensSeen is a bitmask tracking which help screens have been shown
	HelpScreensSeen uint32 `json:"help_screens_seen"`
	// Instances stores the serialized instance data as raw JSON
	InstancesData json.RawMessage `json:"instances"`
	// contains filtered or unexported fields
}

State represents the application state that persists between sessions

func DefaultState

func DefaultState() *State

DefaultState returns the default state

func LoadState

func LoadState() *State

LoadState loads the state from disk. If it cannot be done, we return the default state.

func LoadStateFrom

func LoadStateFrom(dir string) *State

LoadStateFrom loads state from an explicit directory. Empty `dir` is a soft shim: a warning is logged and the global config directory is used. Internal callers must pass a resolved WorkspaceContext.ConfigDir per docs/specs/workspaces.md §3. The returned State remembers dir so that SaveState writes back to it.

func LoadStateFromGlobal

func LoadStateFromGlobal() *State

LoadStateFromGlobal loads state from the global config directory. Prefer this over LoadStateFrom("") for call sites that truly want the global state file.

func (*State) DeleteAllInstances

func (s *State) DeleteAllInstances() error

DeleteAllInstances removes all stored instances

func (*State) GetHelpScreensSeen

func (s *State) GetHelpScreensSeen() uint32

GetHelpScreensSeen returns the bitmask of seen help screens

func (*State) GetInstances

func (s *State) GetInstances() json.RawMessage

GetInstances returns the raw instance data

func (*State) SaveInstances

func (s *State) SaveInstances(instancesJSON json.RawMessage) error

SaveInstances saves the raw instance data

func (*State) SetHelpScreensSeen

func (s *State) SetHelpScreensSeen(seen uint32) error

SetHelpScreensSeen updates the bitmask of seen help screens

type StateManager

type StateManager interface {
	InstanceStorage
	AppState
}

StateManager combines instance storage and app state management

type Workspace

type Workspace struct {
	Name    string    `json:"name"`
	Path    string    `json:"path"`
	AddedAt time.Time `json:"added_at"`
}

Workspace represents a registered workspace tied to a git repository.

type WorkspaceContext

type WorkspaceContext struct {
	// Name is the workspace name. Empty string means global.
	Name string
	// ConfigDir is the absolute path to the config directory
	// (e.g., /repo/.loom or ~/.loom).
	ConfigDir string
	// RepoPath is the absolute path to the repo root. Empty for global.
	RepoPath string
}

WorkspaceContext carries the resolved identity of the active workspace. A nil *WorkspaceContext means "global" (no workspace).

func GlobalWorkspaceContext

func GlobalWorkspaceContext() (*WorkspaceContext, error)

GlobalWorkspaceContext returns a WorkspaceContext pointing at ~/.loom.

func ResolveWorkspace

func ResolveWorkspace(cwd string, registry *WorkspaceRegistry) (*WorkspaceContext, error)

ResolveWorkspace determines the workspace context for the given working directory. If a registered workspace matches, returns its context. Otherwise returns the global context.

func WorkspaceContextFor

func WorkspaceContextFor(ws *Workspace) *WorkspaceContext

WorkspaceContextFor returns a WorkspaceContext for the given workspace.

type WorkspaceRegistry

type WorkspaceRegistry struct {
	Workspaces     []Workspace `json:"workspaces"`
	LastUsed       string      `json:"last_used"`
	OpenWorkspaces []string    `json:"open_workspaces,omitempty"`
}

WorkspaceRegistry holds all registered workspaces, tracks the last-focused one, and records the ordered list of open workspace tabs for restore.

func LoadWorkspaceRegistry

func LoadWorkspaceRegistry() (*WorkspaceRegistry, error)

LoadWorkspaceRegistry reads the workspace registry from the global config dir. Returns an empty registry if the file doesn't exist.

func (*WorkspaceRegistry) Add

func (r *WorkspaceRegistry) Add(name, repoPath string) error

Add registers a new workspace. Validates uniqueness of name and that the path is a git repository. Automatically calls EnsureGitignore.

func (*WorkspaceRegistry) FindByPath

func (r *WorkspaceRegistry) FindByPath(path string) *Workspace

FindByPath finds a workspace whose Path matches or is a parent of the given path.

func (*WorkspaceRegistry) Get

func (r *WorkspaceRegistry) Get(name string) *Workspace

Get finds a workspace by name.

func (*WorkspaceRegistry) GetOpenWorkspaces

func (r *WorkspaceRegistry) GetOpenWorkspaces() []Workspace

GetOpenWorkspaces returns the workspaces named in OpenWorkspaces, in order. Names that no longer exist in the registry are skipped silently.

func (*WorkspaceRegistry) Remove

func (r *WorkspaceRegistry) Remove(name string) error

Remove removes a workspace by name. Does NOT delete the .loom/ directory.

func (*WorkspaceRegistry) Rename

func (r *WorkspaceRegistry) Rename(oldName, newName string) error

Rename changes a workspace's name. Updates LastUsed if it pointed to the old name.

func (*WorkspaceRegistry) SetOpenWorkspaces

func (r *WorkspaceRegistry) SetOpenWorkspaces(names []string) error

SetOpenWorkspaces replaces the ordered list of open workspace names and persists the registry. Unknown names are silently dropped so the stored list always references workspaces that still exist.

The on-disk registry is reloaded before save so concurrent writers (a `loom workspace add` invocation in another shell, or a second running TUI) are not clobbered by this process's stale snapshot of Workspaces. The receiver's in-memory state is refreshed to match the merged result.

func (*WorkspaceRegistry) UpdateLastUsed

func (r *WorkspaceRegistry) UpdateLastUsed(name string) error

UpdateLastUsed sets the last used workspace and saves the registry. Returns an error if no workspace with the given name exists.

The on-disk registry is reloaded before save so concurrent writers (a `loom workspace add` invocation in another shell, or a second running TUI) are not clobbered by this process's stale snapshot of Workspaces. The receiver's in-memory state is refreshed to match the merged result.

Jump to

Keyboard shortcuts

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