This file provides guidance to AI agents (e.g., Claude Code, Cursor, and other LLM-powered tools) when working with code in this repository.
tmuxp is a session manager for tmux that allows users to save and load tmux sessions through YAML/JSON configuration files. It's powered by libtmux and provides a declarative way to manage tmux sessions.
just testoruv run py.test- Run all testsuv run py.test tests/path/to/test.py::TestClass::test_method- Run a single testuv run ptw .- Continuous test runner with pytest-watcheruv run ptw . --now --doctest-modules- Watch tests including doctestsjust startorjust watch-test- Watch and run tests on file changes
just rufforuv run ruff check .- Run linteruv run ruff check . --fix --show-fixes- Fix linting issues automaticallyjust ruff-formatoruv run ruff format .- Format codejust mypyoruv run mypy- Run type checking (strict mode enabled)just watch-ruff- Watch and lint on changesjust watch-mypy- Watch and type check on changes
just build-docs- Build documentationjust serve-docs- Serve docs locally at http://localhost:8013just dev-docs- Watch and serve docs with auto-reloadjust start-docs- Alternative to dev_docs
tmuxp load <config>- Load a tmux session from configtmuxp load -d <config>- Load session in detached statetmuxp freeze <session-name>- Export running session to configtmuxp convert <file>- Convert between YAML and JSONtmuxp shell- Interactive Python shell with tmux contexttmuxp debug-info- Collect system info for debugging
-
CLI Module (
src/tmuxp/cli/): Entry points for all tmuxp commandsload.py: Load tmux sessions from config filesfreeze.py: Export live sessions to config filesconvert.py: Convert between YAML/JSON formatsshell.py: Interactive Python shell with tmux context
-
Workspace Module (
src/tmuxp/workspace/): Core session managementbuilder.py: Builds tmux sessions from configurationloader.py: Loads and validates config filesfinders.py: Locates workspace config filesfreezer.py: Exports running sessions to config
-
Plugin System (
src/tmuxp/plugin.py): Extensibility framework- Plugins extend
TmuxpPluginbase class - Hooks:
before_workspace_builder,on_window_create,after_window_finished,before_script,reattach - Version constraint checking for compatibility
- Plugins extend
- Load YAML/JSON config via
ConfigReader(handles includes, environment variables) - Expand inline shorthand syntax
- Trickle down default values (session β window β pane)
- Validate configuration structure
- Build tmux session via
WorkspaceBuilder
- Type Safety: All code uses type hints with mypy strict mode
- Error Handling: Custom exception hierarchy based on
TmuxpException - Testing: Pytest with fixtures for tmux server/session/window/pane isolation
- Future Imports: All files use
from __future__ import annotations
session_name: my-session
start_directory: ~/project
windows:
- window_name: editor
layout: main-vertical
panes:
- shell_command:
- vim
- shell_command:
- git statusTMUXP_CONFIGDIR: Custom directory for workspace configsTMUX_CONF: Path to tmux configuration fileTMUXP_DEFAULT_COLUMNS/ROWS: Default session dimensions
- Use functional tests only: Write tests as standalone functions, not classes. Avoid
class TestFoo:groupings - use descriptive function names and file organization instead. - Use pytest fixtures from
tests/fixtures/for tmux objects - Test plugins using mock packages in
tests/fixtures/pluginsystem/ - Use
retry_untilutilities for async tmux operations - Run single tests with:
uv run py.test tests/file.py::test_function_name - Use libtmux fixtures: Prefer
server,session,window,panefixtures over manual setup - Avoid mocks when fixtures exist: Use real tmux fixtures instead of
MagicMock - Use
tmp_pathfixture instead of Python'stempfile - Use
monkeypatchfixture instead ofunittest.mock
- Follow NumPy-style docstrings (pydocstyle convention)
- Use ruff for formatting and linting
- Maintain strict mypy type checking
- Keep imports organized with future annotations at top
- Prefer namespace imports for stdlib: Use
import enumandenum.Enuminstead offrom enum import Enum; third-party packages may usefrom X import Y - Type imports: Use
import typing as tand access via namespace (e.g.,t.Optional) - Development workflow: Format β Test β Commit β Lint/Type Check β Test β Final Commit
These rules guide future logging changes; existing code may not yet conform.
- Use
logging.getLogger(__name__)in every module - Add
NullHandlerin library__init__.pyfiles - Never configure handlers, levels, or formatters in library code β that's the application's job
Pass structured data on every log call where useful for filtering, searching, or test assertions.
Core keys (stable, scalar, safe at any log level):
| Key | Type | Context |
|---|---|---|
tmux_cmd |
str |
tmux command line |
tmux_subcommand |
str |
tmux subcommand (e.g. new-session) |
tmux_target |
str |
tmux target specifier (e.g. mysession:1.2) |
tmux_exit_code |
int |
tmux process exit code |
tmux_session |
str |
session name |
tmux_window |
str |
window name or index |
tmux_pane |
str |
pane identifier |
tmux_config_path |
str |
workspace config file path |
tmux_layout |
str |
window layout string |
Heavy/optional keys (DEBUG only, potentially large):
| Key | Type | Context |
|---|---|---|
tmux_stdout |
list[str] |
tmux stdout lines (truncate or cap; %(tmux_stdout)s produces repr) |
tmux_stderr |
list[str] |
tmux stderr lines (same caveats) |
Treat established keys as compatibility-sensitive β downstream users may build dashboards and alerts on them. Change deliberately.
snake_case, not dotted;tmux_prefix- Prefer stable scalars; avoid ad-hoc objects
- Heavy keys (
tmux_stdout,tmux_stderr) are DEBUG-only; consider companiontmux_stdout_lenfields or hard truncation (e.g.stdout[:100])
logger.debug("msg %s", val) not f-strings. Two rationales:
- Deferred string interpolation: skipped entirely when level is filtered
- Aggregator message template grouping:
"Running %s"is one signature grouped Γ10,000; f-strings make each line unique
When computing val itself is expensive, guard with if logger.isEnabledFor(logging.DEBUG).
Increment for each wrapper layer so %(filename)s:%(lineno)d and OTel code.filepath point to the real caller. Verify whenever call depth changes.
For objects with stable identity (Session, Window, Pane), use LoggerAdapter to avoid repeating the same extra on every call. Lead with the portable pattern (override process() to merge); merge_extra=True simplifies this on Python 3.13+.
| Level | Use for | Examples |
|---|---|---|
DEBUG |
Internal mechanics, tmux I/O, config expansion | tmux command + stdout, trickle-down steps |
INFO |
Session lifecycle, user-visible operations | Session created, window added, workspace loaded |
WARNING |
Recoverable issues, deprecation, user-actionable config | Deprecated key, missing optional program |
ERROR |
Failures that stop an operation | tmux command failed, config validation error |
Config discovery noise belongs in DEBUG; only surprising/user-actionable config issues β WARNING.
- Lowercase, past tense for events:
"session created","tmux command failed" - No trailing punctuation
- Keep messages short; put details in
extra, not the message string
- Use
logger.exception()only insideexceptblocks when you are not re-raising - Use
logger.error(..., exc_info=True)when you need the traceback outside anexceptblock - Avoid
logger.exception()followed byraiseβ this duplicates the traceback. Either add context viaextrathat would otherwise be lost, or let the exception propagate
Assert on caplog.records attributes, not string matching on caplog.text:
- Scope capture:
caplog.at_level(logging.DEBUG, logger="libtmux.common") - Filter records rather than index by position:
[r for r in caplog.records if hasattr(r, "tmux_cmd")] - Assert on schema:
record.tmux_exit_code == 0not"exit code 0" in caplog.text caplog.record_tuplescannot access extra fields β always usecaplog.records
Two output channels serve different audiences:
- Diagnostics (
logger.*()withextra): System events for log files,caplog, and aggregators. Never styled. - User-facing output: What the human sees. Styled via
Colorsclass.- Commands with output modes (
--json/--ndjson): preferOutputFormatter.emit_text()fromtmuxp.cli._outputβ silenced in non-human modes. - Human-only commands: use
tmuxp_echo()fromtmuxp.log(re-exported viatmuxp.cli.utils) for user-facing messages. - Undefined contracts: Machine-output behavior for error and empty-result paths (e.g.,
searchwith no matches) is not yet defined. These paths currently emit styled text throughformatter.emit_text(), which is a no-op in machine modes.
- Commands with output modes (
Raw print() is forbidden in command/business logic. The print() call lives only inside the presenter layer (_output.py) or tmuxp_echo.
- f-strings/
.format()in log calls - Unguarded logging in hot loops (guard with
isEnabledFor()) - Catch-log-reraise without adding new context
print()for debugging or internal diagnostics β uselogger.debug()with structuredextrainstead- Logging secret env var values (log key names only)
- Non-scalar ad-hoc objects in
extra - Requiring custom
extrafields in format strings without safe defaults (missing keys raiseKeyError)
All functions and methods MUST have working doctests. Doctests serve as both documentation and tests.
CRITICAL RULES:
- Doctests MUST actually execute - never comment out function calls or similar
- Doctests MUST NOT be converted to
.. code-block::as a workaround (code-blocks don't run) - If you cannot create a working doctest, STOP and ask for help
Available tools for doctests:
doctest_namespacefixtures:server,session,window,pane,tmp_path,test_utils- Ellipsis for variable output:
# doctest: +ELLIPSIS - Update
conftest.pyto add new fixtures todoctest_namespace
# doctest: +SKIP is NOT permitted - it's just another workaround that doesn't test anything. Use the fixtures properly - tmux is required to run tests anyway.
Using fixtures in doctests:
>>> from tmuxp.workspace.builder import WorkspaceBuilder
>>> config = {'session_name': 'test', 'windows': [{'window_name': 'main'}]}
>>> builder = WorkspaceBuilder(session_config=config, server=server) # doctest: +ELLIPSIS
>>> builder.build()
>>> builder.session.name
'test'When output varies, use ellipsis:
>>> session.session_id # doctest: +ELLIPSIS
'$...'
>>> window.window_id # doctest: +ELLIPSIS
'@...'Additional guidelines:
- Use narrative descriptions for test sections rather than inline comments
- Move complex examples to dedicated test files at
tests/examples/<path>/test_<example>.py - Keep doctests simple and focused on demonstrating usage
- Add blank lines between test sections for improved readability
Doctest exceptions (patterns where doctests are not required):
- Sphinx/docutils
visit_*/depart_*methods - tested via integration tests; 0 examples across docutils (851 methods), Sphinx (800+), and CPython'sast.NodeVisitor - Sphinx
setup()functions - entry points not testable in isolation - Complex recursive traversal functions - extract helper predicates instead
Best practice for node processing: Extract testable helper functions (like _is_usage_block()) and doctest those. Keep complex visitor logic in integration tests.
When writing documentation (README, CHANGES, docs/), follow these rules for code blocks:
One command per code block. This makes commands individually copyable.
Put explanations outside the code block, not as comments inside.
Good:
Run the tests:
$ uv run pytestRun with coverage:
$ uv run pytest --covBad:
# Run the tests
$ uv run pytest
# Run with coverage
$ uv run pytest --cov- QA every edit: Run formatting and tests before committing
- Minimum Python: 3.10+ (per pyproject.toml)
- Minimum tmux: 3.2+ (as per README)
The CLI uses semantic colors via the Colors class in src/tmuxp/_internal/colors.py. Colors are chosen based on hierarchy level and semantic meaning, not just data type.
- Structural hierarchy: Headers > Items > Details
- Semantic meaning: What IS this element?
- Visual weight: What should draw the eye first?
- Depth separation: Parent elements should visually contain children
Inspired by patterns from jq (object keys vs values), ripgrep (path/line/match distinction), and mise/just (semantic method names).
| Level | Element Type | Method | Color | Examples |
|---|---|---|---|---|
| L0 | Section headers | heading() |
Bright cyan + bold | "Local workspaces:", "Global workspaces:" |
| L1 | Primary content | highlight() |
Magenta + bold | Workspace names (braintree, .tmuxp) |
| L2 | Supplementary info | info() |
Cyan | Paths (~/.tmuxp, ~/project/.tmuxp.yaml) |
| L3 | Metadata/labels | muted() |
Blue | Source labels (Legacy:, XDG default:) |
| Status | Method | Color | Examples |
|---|---|---|---|
| Success/Active | success() |
Green | "active", "18 workspaces" |
| Warning | warning() |
Yellow | Deprecation notices |
| Error | error() |
Red | Error messages |
Local workspaces: β heading() bright_cyan+bold
.tmuxp ~/work/python/tmuxp/.tmuxp.yaml β highlight() + info()
Global workspaces (~/.tmuxp): β heading() + info()
braintree β highlight()
cihai β highlight()
Global workspace directories: β heading()
Legacy: ~/.tmuxp (18 workspaces, active) β muted() + info() + success()
XDG default: ~/.config/tmuxp (not found) β muted() + info() + muted()
colors = Colors()
colors.heading("Section:") # Cyan + bold (section headers)
colors.highlight("item") # Magenta + bold (primary content)
colors.info("/path/to/file") # Cyan (paths, supplementary info)
colors.muted("label:") # Blue (metadata, labels)
colors.success("ok") # Green (success states)
colors.warning("caution") # Yellow (warnings)
colors.error("failed") # Red (errors)Never use the same color for adjacent hierarchy levels. If headers and items are both blue, they blend together. Each level must be visually distinct.
Avoid dim/faint styling. The ANSI dim attribute (\x1b[2m) is too dark to read on black terminal backgrounds. This includes both standard and bright color variants with dim.
Bold may not render distinctly. Some terminal/font combinations don't differentiate bold from normal weight. Don't rely on bold alone for visual distinction - pair it with color differences.