Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
66cef18
ControlMode(core): Add engine stack and protocol bridge
tony Nov 24, 2025
ca08fb8
Server(refactor[sessions]): Use engine internal filters
tony Nov 24, 2025
6d0b770
ControlMode(test): Add control-mode regression coverage and helpers
tony Nov 24, 2025
441a588
docs(control-mode): Document engine usage, errors, env reqs, sandbox
tony Nov 24, 2025
05165ed
docs(control-mode): Note env requirements and capture normalization i…
tony Nov 24, 2025
eec2fd2
docs(control-mode): Restructure release notes with examples (#605)
tony Nov 24, 2025
9340a37
ControlMode(test): Cover protocol errors, attach_to, sandbox isolation
tony Nov 24, 2025
26f0ff9
test(control-mode): Adjust attach_to expectation to ignore control-on…
tony Nov 24, 2025
a10c8e0
ControlMode(test): Add restart, overflow, attach failure, capture ran…
tony Nov 24, 2025
c609431
ControlMode(core,test): Preflight attach_to and harden restart/captur…
tony Nov 24, 2025
55119cd
ControlMode(test): Add attach failure, flags, and retry placeholder
tony Nov 24, 2025
110b6d3
test(control-mode): Add attach notifications and capture flag coverage
tony Nov 24, 2025
35cdaea
test(control-mode): Expand attach notifications, capture flags, retry…
tony Nov 24, 2025
15b2da3
ControlMode(core,test): Preflight attach_to and stabilize control fla…
tony Nov 24, 2025
c27df0d
ControlMode(core,test): Attach preflight and stabilize control flag/c…
tony Nov 24, 2025
3b8081c
test(control-mode): Xfail capture range/joined races
tony Nov 24, 2025
c15e097
test(control-mode): Xfail restart retry and capture races
tony Nov 24, 2025
787ec8d
test(control-mode): Xfail notification, collision, switch-client gaps
tony Nov 24, 2025
726a071
test(control-mode): Add xfail coverage for backlog, notifications, co…
tony Nov 24, 2025
c50a08b
ControlMode(test[xfail]): Add scrollback/timeout placeholders
tony Nov 24, 2025
597f266
ControlModeEngine(feat[testability]): Allow injectable process factory
tony Nov 24, 2025
8a4b87e
ControlModeEngine(types): Introduce process protocol for test hooks
tony Nov 24, 2025
1cd9fe6
ControlModeEngine(types): Extend process protocol (pid/poll)
tony Nov 24, 2025
f1840b5
ControlModeEngine(types): Cast fakes to protocol in tests
tony Nov 24, 2025
d9674d3
ControlModeEngine(test): Make fakes satisfy _ControlProcess without c…
tony Nov 24, 2025
d908230
ControlModeEngine(feat[testability]): Configurable retries and thread…
tony Nov 24, 2025
94eaaa0
ControlModeEngine(test): Tighten scripted process typing for mypy
tony Nov 24, 2025
05cacd3
ControlModeEngine(test): Backpressure integration via scripted process
tony Nov 24, 2025
57a63eb
ControlModeEngine(core): UUID session names, control-mode flag, drain…
tony Nov 25, 2025
556ec47
ControlModeEngine(threading): Non-daemon threads with explicit join
tony Nov 25, 2025
0ba36a3
ControlModeEngine(abstraction): Engine hooks for probe/switch
tony Nov 25, 2025
a5ed016
neo: Route fetch_objs through engine for unified command execution
tony Nov 25, 2025
a9f1fae
ControlModeEngine(test): ScriptedStdout/Stdin with queue-based I/O
tony Nov 25, 2025
4cc78c8
pane(fix): Unify capture_pane trimming with engine behavior
tony Nov 25, 2025
620a239
conftest(fix): Skip doctests for control mode engine
tony Nov 25, 2025
6c97b94
conftest(fix): Filter doctests at collection for control engine
tony Nov 25, 2025
66f3005
test(cleanup): Remove obsolete timeout restart xfail
tony Nov 25, 2025
c825054
engine(refactor): Add ServerContext and Engine.bind()
tony Nov 25, 2025
682ae47
engine(api): Standardize hook signatures using stored context
tony Nov 25, 2025
4eb34ac
ControlModeEngine(api): Rename attach_to to control_session
tony Nov 25, 2025
c1f2eae
ControlModeEngine(feat): Add set_client_flags() for runtime flag control
tony Nov 25, 2025
0938fb5
ControlProtocol(fix): Add SKIPPING state for unexpected %begin blocks
tony Dec 1, 2025
5e8b75b
test(hooks): Mark raw_cmd and dataclass tests as subprocess-only
tony Dec 1, 2025
157c6b2
test(options): Avoid % format expansion in control mode test
tony Dec 1, 2025
a78f708
test(env): Use wait_for_line() for environment propagation tests
tony Dec 1, 2025
2781b8f
ControlProtocol(feat): Add MESSAGE and CONFIG_ERROR notification types
tony Dec 2, 2025
5f1d209
ControlProtocol(fix): Correct notification parsing per tmux source
tony Dec 2, 2025
0b1d58d
ControlProtocol(test[lint-timing]): fix control-mode lint and timing
tony Dec 6, 2025
7f91d55
Hooks(chore[compat]): drop tmux<3.2 guards
tony Dec 6, 2025
d557a67
Pane(fix[capture]): trim trailing whitespace consistently
tony Dec 6, 2025
ebfa38f
Server(test[connect]): cover connect() under control engine
tony Dec 6, 2025
ab3bf89
ControlModeEngine(test[flags]): verify set_client_flags invocation
tony Dec 6, 2025
ce170a2
CI(chore[control]): add control-engine pytest run
tony Dec 6, 2025
30ebcb3
docs(control): add control-engine testing guidance
tony Dec 6, 2025
9ebace0
ControlModeEngine(test[typing]): fix lint typing for set_client_flags…
tony Dec 6, 2025
8c7a2f0
ControlModeEngine(feat[flags]): expand control client helpers
tony Dec 6, 2025
1864056
ControlModeEngine(test[flags]): cover client flags, flow, subscriptions
tony Dec 6, 2025
6cee660
Docs(control): document client flags, flow, subscriptions
tony Dec 6, 2025
91b0607
ControlModeEngine(test[live]): exercise flags/flow/subscriptions
tony Dec 6, 2025
25e6f38
Pane(fix[capture_pane]): Retry echo-only captures in control mode
tony Jan 1, 2026
8bdf699
Pane(fix[capture_pane]): Simplify retry predicate
tony Jan 1, 2026
dc8926e
Server(fix[shutdown]): Close engine on kill-server
tony Jan 1, 2026
f159981
ControlModeEngine(fix[filter_sessions]): Parse list-clients with deli…
tony Jan 1, 2026
e153ad0
Server(fix[connect]): Restore TMUX env on failure
tony Jan 1, 2026
bc0c9f6
tests(fix[control_client_logs]): Log stdout to file
tony Jan 1, 2026
e79f022
tests(fix[connect]): Stabilize env restore test
tony Jan 1, 2026
116bc84
tests(fix[stability]): Use monotonic timing and stub cleanup
tony Jan 1, 2026
395ee91
ControlModeEngine(fix[shutdown]): Close control-mode pipes on teardown
tony Jan 1, 2026
cd57187
ControlModeEngine(fix[can_switch_client]): Handle empty client flags
tony Jan 1, 2026
cdfc1e3
ControlModeEngine(fix[shutdown]): Guard teardown timeouts
tony Jan 1, 2026
64437d7
docs(fix[control-mode]): Clarify internal session naming
tony Jan 1, 2026
2fd82aa
pytest(fix[engine-option]): Register engine CLI in plugin
tony Jan 1, 2026
d7bf5f7
pytest(fix[doctest-filter]): Apply control doctest filter in plugin
tony Jan 1, 2026
6daf365
pytest(fix[engine-option]): Default engine when option missing
tony Jan 1, 2026
1fac20e
pytest(fix[doctest-filter]): Report deselected doctests
tony Jan 1, 2026
acfe564
tests(fix[pytest-plugin]): Assert doctest filtering
tony Jan 1, 2026
6b133c6
Pane(fix[capture_pane]): simplify retry predicate
tony Jan 1, 2026
4110e15
ControlModeEngine(fix[switch_client]): bootstrap before client check
tony Jan 1, 2026
77ba49e
ControlModeEngine(fix[probe]): defer to Server on missing tmux
tony Jan 1, 2026
1f51c7a
ControlModeEngine(fix[shutdown]): avoid hang on shutdown
tony Jan 1, 2026
e99a6cc
pytest(fix[fixtures]): move control fixtures into plugin
tony Jan 1, 2026
7631809
chore(rebase): Fix conflict-resolution regressions
tony Jul 18, 2026
27a67c3
chore(rebase): Fit trunk's Ruff rules
tony Nov 22, 2026
93c697b
Engines(fix[socket]): Measure engine socket flags
tony Nov 22, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ jobs:
COV_CORE_SOURCE: .
COV_CORE_CONFIG: .coveragerc
COV_CORE_DATAFILE: .coverage.eager
- name: Test with pytest (control engine)
continue-on-error: ${{ matrix.tmux-version == 'master' }}
run: |
tmux -V
uv run py.test --engine=control -n auto --verbose
- uses: codecov/codecov-action@v7
with:
token: ${{ secrets.CODECOV_TOKEN }}
Expand Down
55 changes: 55 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,61 @@ $ uvx --from 'libtmux' --prerelease allow python
_Notes on the upcoming release will go here._
<!-- END PLACEHOLDER - ADD NEW CHANGELOG ENTRIES BELOW THIS LINE -->

### What's new

#### Control mode engine (experimental) (#605)
- Control-mode–first engine stack with structured `CommandResult`, notification parsing
(%layout-change, unlinked window add/close/rename, client session change/detach, paste buffer),
and stats, while keeping the public `cmd` API returning `tmux_cmd`.
- Internal control session is automatically filtered from `Server.sessions` /
`Server.has_session()`; advanced users can inspect all sessions via
`Server._sessions_all()`.
- `ControlModeEngine` accepts `internal_session_name` (default `libtmux_ctrl_XXXXXXXX`)
and `control_session` for advanced connection strategies.

Example:

```python
from libtmux._internal.engines.control_mode import ControlModeEngine
from libtmux.server import Server

engine = ControlModeEngine(command_timeout=5)
server = Server(engine=engine)
session = server.new_session(session_name="ctrl")
for notif in engine.iter_notifications(timeout=0.1):
print(notif.kind, notif.data)
```

#### Convenience APIs (#605)
- `Server.connect()` returns an existing session or creates a new detached one,
working with both subprocess and control-mode engines.

#### Diagnostics and resilience (#605)
- Bounded notification queue with drop counting; exposed via engine stats.
- Expanded exceptions: `ControlModeTimeout`, `ControlModeProtocolError`,
`ControlModeConnectionError`, `SubprocessTimeout`; documented retry/timeout behaviour.
- Control-mode capture-pane trims trailing whitespace-only lines to mirror subprocess
semantics; first capture after send-keys briefly retries to avoid prompt races.

#### Testing utilities (#605)
- Control sandbox fixture provides a hermetic control-mode tmux server (isolated HOME,
TMUX_TMPDIR, unique socket); handy for integration-style tests.
Example:

```python
@pytest.mark.engines(["control"])
def test_control_sandbox(control_sandbox):
with control_sandbox as server:
out = server.cmd("display-message", "-p", "hi")
assert out.stdout == ["hi"]
```

### Compatibility

- Control mode internal session filtering is engine-driven; callers no longer need
to manually exclude `libtmux_ctrl_*`. APIs stay unchanged; additional metadata
is attached for advanced users. (#605)

## libtmux 0.67.0 (2026-11-22)

libtmux 0.67.0 keeps a `#` in session names, window names, pane titles,
Expand Down
7 changes: 7 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ Active deprecations and migration guidance.

::::


:::{note}
Looking for the new control-mode engine? See {ref}`control-mode` for an
experimental, protocol-focused entry point that still preserves the public
``tmux_cmd`` return type.
:::

```{toctree}
:hidden:
:maxdepth: 1
Expand Down
49 changes: 49 additions & 0 deletions docs/api/testing/pytest-plugin/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,55 @@ short `/tmp/<random>` — when you need a socket path of your own, or point
... monkeypatch.setenv("HOME", str(user_path))
```

## Selecting tmux engines (experimental)

Fixtures can run against different execution engines. By default the
`subprocess` engine is used. You can choose control mode globally:

```console
$ pytest --engine=control
```

Or per-test via the `engines` marker (uses parametrization) and the `engine_name`
fixture:

```python
import pytest


@pytest.mark.engines(["subprocess", "control"])
def test_my_flow(server, engine_name):
# server uses the selected engine, engine_name reflects the current one
assert engine_name in {"subprocess", "control"}
assert server.is_alive()
```

`TestServer` also respects the selected engine. Control mode is experimental and
its APIs may change between releases.

### Control sandbox fixture (experimental)

Use ``control_sandbox`` when you need a hermetic control-mode server for a test:

```python
import typing as t
import pytest
from libtmux.server import Server


@pytest.mark.engines(["control"])
def test_control_sandbox(control_sandbox: t.ContextManager[Server]):
with control_sandbox as server:
session = server.new_session(session_name="sandbox", attach=False)
out = server.cmd("display-message", "-p", "hi")
assert out.stdout == ["hi"]
```

The fixture:
- Spins up a unique socket name and isolates ``HOME`` / ``TMUX_TMPDIR``
- Clears inherited ``TMUX`` so it never attaches to the user's server
- Uses ``ControlModeEngine`` and cleans up the server on exit

[pytest]: https://docs.pytest.org/en/stable/
[pytest-tmux]: https://pytest-tmux.readthedocs.io/
[tmuxp]: https://tmuxp.git-pull.com/
65 changes: 65 additions & 0 deletions docs/pytest-plugin/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
myst:
html_meta:
description: "libtmux pytest plugin for testing tmux applications with pytest"
keywords: "tmux, pytest, plugin, testing, libtmux"
---

(pytest_plugin)=

# tmux `pytest` plugin

```{toctree}
:hidden:
:maxdepth: 2

getting-started
fixtures
usage-examples
advanced-techniques
```

libtmux provides pytest fixtures for tmux, making it easy to test tmux-related functionality with complete isolation. The plugin automatically manages setup and teardown of independent tmux servers, sessions, windows, and panes.

```{admonition} Connect with us!
:class: seealso

Do you want more flexibility? Correctness? Power? Defaults changed? [Connect with us](https://github.com/tmux-python/libtmux/discussions) on the tracker. We want to know your use case and won't stabilize APIs until we're sure everything is by the book.
```

## Benefits at a glance

- **Isolated Testing Environment**: Each test gets a fresh tmux server that won't interfere with other tests
- **Automatic Cleanup**: Servers, sessions, and other resources are automatically cleaned up after tests
- **Simplified Setup**: Common fixtures for server, session, window, and pane management
- **Reliable Testing**: Consistent environment for reproducible test results
- **Custom Configuration**: Easily test with different tmux configurations and settings

## Quick installation

Install `libtmux` via the Python package manager of your choice:

```{code-block} console
$ pip install libtmux
```

The pytest plugin will be automatically detected by pytest, and the fixtures will be available in your test environment.

## See real-world examples

View libtmux's own [tests/](https://github.com/tmux-python/libtmux/tree/master/tests) as well as tmuxp's [tests/](https://github.com/tmux-python/tmuxp/tree/master/tests) for real-world examples.

For detailed code examples and usage patterns, refer to the {ref}`usage-examples` page.

## Module reference

```{module} libtmux.pytest_plugin
```

```{eval-rst}
.. automodule:: libtmux.pytest_plugin
:members:
:inherited-members:
:private-members:
:show-inheritance:
:member-order: bysource
55 changes: 55 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ equivalent to `$ tmux -L mysocket`.
{class}`~libtmux.Session`, {class}`~libtmux.Window`, and
{class}`~libtmux.Pane` objects.

### Optional: Control mode (experimental)

Control mode keeps a persistent tmux client open and streams
notifications. Enable it by injecting a control-mode engine:

```python
from libtmux._internal.engines.control_mode import ControlModeEngine
from libtmux.server import Server

engine = ControlModeEngine()
server = Server(engine=engine)
session = server.new_session(session_name="ctrl")
print(session.name)
```

See {ref}`control-mode` for details, caveats, and notification handling.

`server` is now a living object bound to the tmux server's Sessions,
Windows and Panes.

## Raw, contextual commands

New session:
Expand Down Expand Up @@ -283,6 +303,41 @@ Session($1 foo)

to give us a `session` object to play with.

## Connect to or create a session

A simpler approach is to use {meth}`Server.connect()`, which returns an existing
session or creates it if it doesn't exist:

```python
>>> session = server.connect('my_project')
>>> session.name
'my_project'

>>> # Calling again returns the same session
>>> session2 = server.connect('my_project')
>>> session2.session_id == session.session_id
True
```

This is particularly useful for:

- Development workflows where you want to reuse existing sessions
- Scripts that should create a session on first run, then reattach
- Working with both subprocess and control-mode engines transparently

Compare with the traditional approach:

```python
>>> # Traditional: check then create
>>> if server.has_session('my_project'):
... session = server.sessions.get(session_name='my_project')
... else:
... session = server.new_session('my_project')

>>> # Simpler with connect()
>>> session = server.connect('my_project')
```

## Playing with our tmux session

We now have access to `session` from above with all of the methods
Expand Down
Loading
Loading