Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ sessions after client close: 1

More: [ownership and errors][doc-ownership].

## Run a program and read its exit status

The core and Async preview APIs use tmux 3.7 or newer to run a program on a
dedicated server and return its exit status and bounded final screen. See how
to [report a failed terminal command][owned-terminal-programs].

## Packages

Start with `libtmux`. Add the companion for your caller:
Expand Down Expand Up @@ -582,6 +588,7 @@ Part of the [libtmux.org](https://libtmux.org/) family alongside Python
[mcp-application]: https://libtmux.org/en/ruby/latest/mcp/reference/libtmux-mcp-application/
[multiplematcheserror]: https://libtmux.org/en/ruby/latest/reference/libtmux-multiplematcheserror/
[nomatcherror]: https://libtmux.org/en/ruby/latest/reference/libtmux-nomatcherror/
[owned-terminal-programs]: docs/reference/behavior.md#owned-terminal-programs
[pane]: https://libtmux.org/en/ruby/latest/reference/libtmux-pane-14z6/
[pane-capture]: https://libtmux.org/en/ruby/latest/reference/libtmux-pane-capture/
[pane-resize]: https://libtmux.org/en/ruby/latest/reference/libtmux-pane-resize/
Expand Down
15 changes: 15 additions & 0 deletions capabilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
"minimum": "3.3a"
},
"capabilities": [
{
"id": "owned-terminal-program",
"name": "Owned terminal program",
"summary": "Wait for process exit and retain the drained terminal screen on a private daemon.",
"since": "3.7",
"handling": "refuse",
"error": "LibTmux::UnsupportedFeatureError",
"apis": [
"LibTmux.start_program",
"LibTmux.run_program",
"LibTmux::Async.start_program",
"LibTmux::Async.run_program"
],
"commands": []
},
{
"id": "control-mode",
"name": "Control mode",
Expand Down
32 changes: 31 additions & 1 deletion docs/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ This inventory checks declaration presence and visibility. The installed
signature consumers check exercised calls; neither is whole-program static
type checking. See [signature proof](behavior.md#signature-proof).

## LibTmux

| Method | Declared return | Contract |
| --- | --- | --- |
| [.run_program](../../gems/libtmux/lib/libtmux/program.rb#L10) | `::LibTmux::Program::Result` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [.start_program](../../gems/libtmux/lib/libtmux/program.rb#L5) | `::LibTmux::Program` / `T` | [Owned terminal programs](behavior.md#owned-terminal-programs) |

## LibTmux::Async

| Method | Declared return | Contract |
| --- | --- | --- |
| [.open](../../gems/libtmux-async/lib/libtmux/async.rb#L31) | `T` | [Async scope](behavior.md#async-scope) |
| [.open](../../gems/libtmux-async/lib/libtmux/async.rb#L41) | `T` | [Async scope](behavior.md#async-scope) |
| [.run_program](../../gems/libtmux-async/lib/libtmux/async.rb#L37) | `::LibTmux::Program::Result` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [.start_program](../../gems/libtmux-async/lib/libtmux/async.rb#L32) | `::LibTmux::Program` / `T` | [Owned terminal programs](behavior.md#owned-terminal-programs) |

## LibTmux::Async::ControlConnection

Expand Down Expand Up @@ -637,6 +646,27 @@ type checking. See [signature proof](behavior.md#signature-proof).
| --- | --- | --- |
| [.build](../../gems/libtmux/lib/libtmux/criteria.rb#L405) | `::LibTmux::FilterExpr` | [Criteria](behavior.md#criteria) |

## LibTmux::Program

| Method | Declared return | Contract |
| --- | --- | --- |
| [#close](../../gems/libtmux/lib/libtmux/program.rb#L171) | `nil` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#inspect](../../gems/libtmux/lib/libtmux/program.rb#L181) | `::String` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#resize](../../gems/libtmux/lib/libtmux/program.rb#L164) | `::LibTmux::CommandResult` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#screen](../../gems/libtmux/lib/libtmux/program.rb#L144) | `::String` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#send_keys](../../gems/libtmux/lib/libtmux/program.rb#L158) | `::LibTmux::CommandResult` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#send_text](../../gems/libtmux/lib/libtmux/program.rb#L152) | `::LibTmux::CommandResult` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#wait](../../gems/libtmux/lib/libtmux/program.rb#L120) | `::LibTmux::Program::Result` | [Owned terminal programs](behavior.md#owned-terminal-programs) |

## LibTmux::Program::Result

| Method | Declared return | Contract |
| --- | --- | --- |
| [#exit_status](../../gems/libtmux/lib/libtmux/program.rb#L19) | `::Integer?` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#inspect](../../gems/libtmux/lib/libtmux/program.rb#L27) | `::String` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#screen](../../gems/libtmux/lib/libtmux/program.rb#L19) | `::String` | [Owned terminal programs](behavior.md#owned-terminal-programs) |
| [#signal](../../gems/libtmux/lib/libtmux/program.rb#L19) | `::Integer?` | [Owned terminal programs](behavior.md#owned-terminal-programs) |

## LibTmux::ProtocolError

| Method | Declared return | Contract |
Expand Down
64 changes: 64 additions & 0 deletions docs/reference/behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -1021,3 +1021,67 @@ Checker-only dependencies are installed after each artifact's minimal
runtime import and executable recipes, so they cannot hide runtime dependency
omissions. Behavioral tests, real-tmux fixtures and the platform matrix remain
separate evidence.

## Owned terminal programs

`LibTmux.start_program` and `LibTmux::Async.start_program` run literal argv
in a terminal on a dedicated server. The preview checks the daemon's tmux 3.7
floor before launching the application. Each launch accepts a working
directory, environment overrides, and dimensions. Geometry defaults to
80×24 and is limited to 256×100 cells.
The methods return a program owner or yield it to a block and close it
when the block ends. `run_program` launches, waits, and closes, then
returns the completion result. The `timeout:` value applies separately to
daemon startup, terminal setup, and the wait in `run_program`. Each phase can
be cancelled through the supplied `cancel:` token.

`Program#wait` observes the terminal process and returns a frozen result
with an exit code or a signal number once tmux's process-exit hook runs.
Successful waits share the result. A cancelled or expired waiter leaves the
program and other waiters running. The Async owner uses the current Async task
and bounded scope dispatch. Dedicated daemon startup and retirement use
the core lifecycle.

The screen joins stdout and stderr as rendered terminal text. It includes
the visible rows and up to 100 history rows. Captures retain the newest
64 KiB of UTF-8 text and omit incomplete or invalid code units. tmux
may wrap lines, overwrite terminal cells, and add an exit banner.
Use `send_text` and `send_keys` to enter input and `resize` to change
dimensions.

`Program#close` retires the Async scope and dedicated daemon in the
owner process. Block scopes also retire on exceptions. If an explicit close
fails, the same owner can retry it. A forcibly terminated Ruby owner may leave
a live daemon. An application that ignores SIGHUP or detaches can survive
terminal closure. Keep the daemon exclusive to this owner. External respawn
can replace the process observed by the program handle.

Run a sample migration command and report the failure from its retained
screen.

<!-- example: owned_program/main -->
```ruby
require "libtmux"
require "rbconfig"

command = [RbConfig.ruby, "-e", <<~'RUBY']
warn "Migration stopped: café table missing"
exit 7
RUBY

result = LibTmux.run_program(command)
diagnostic = result.screen.lines(chomp: true).find do |line|
line.start_with?("Migration stopped:")
end
raise "Expected a migration failure with a diagnostic" unless
result.exit_status == 7 && diagnostic

puts "Exit #{result.exit_status}. #{diagnostic}"
```
<!-- /example -->

It prints:

```text
Exit 7. Migration stopped: café table missing
```
21 changes: 21 additions & 0 deletions docs/reference/contracts.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
{
"version": 1,
"contracts": [
{
"id": "owned-terminal-programs",
"title": "Owned terminal programs",
"methods": [
"LibTmux.start_program",
"LibTmux.run_program",
"LibTmux::Async.start_program",
"LibTmux::Async.run_program",
"LibTmux::Program#close",
"LibTmux::Program#inspect",
"LibTmux::Program#resize",
"LibTmux::Program#screen",
"LibTmux::Program#send_keys",
"LibTmux::Program#send_text",
"LibTmux::Program#wait",
"LibTmux::Program::Result#exit_status",
"LibTmux::Program::Result#inspect",
"LibTmux::Program::Result#screen",
"LibTmux::Program::Result#signal"
]
},
{
"id": "async-map",
"title": "Async map",
Expand Down
25 changes: 25 additions & 0 deletions examples/api/owned_program.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

begin
# docs:begin main
require "libtmux"
require "rbconfig"

command = [RbConfig.ruby, "-e", <<~'RUBY']
warn "Migration stopped: café table missing"
exit 7
RUBY

result = LibTmux.run_program(command)
diagnostic = result.screen.lines(chomp: true).find do |line|
line.start_with?("Migration stopped:")
end
raise "Expected a migration failure with a diagnostic" unless
result.exit_status == 7 && diagnostic

puts "Exit #{result.exit_status}. #{diagnostic}"
# docs:end main
rescue StandardError => error
warn "#{error.class}: #{error.message}"
exit 1
end
17 changes: 17 additions & 0 deletions examples/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{
"version": 2,
"programs": [
{
"id": "owned_program",
"path": "examples/api/owned_program.rb",
"gem": "libtmux",
"api": {
"symbols": ["LibTmux.run_program"],
"capabilities": ["owned-terminal-program"],
"description": "Report a terminal program failure with the exit status and the final diagnostic.",
"output": "Exit 7. Migration stopped: café table missing\n"
}
},
{
"id": "quickstart",
"path": "examples/quickstart.rb",
Expand Down Expand Up @@ -365,6 +376,12 @@
"gems/libtmux/README.md"
],
"snippets": [
{
"id": "owned_program/main",
"source": "examples/api/owned_program.rb",
"region": "main",
"language": "ruby"
},
{
"id": "quickstart/main",
"source": "examples/quickstart.rb",
Expand Down
3 changes: 3 additions & 0 deletions gems/libtmux-async/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ in the [main changelog](../../CHANGELOG.md).

## 0.1.3 (unreleased)

- Preview `LibTmux::Async.start_program` and `run_program` with
scheduler-aware terminal input, capture, and cancellable waits.

## 0.1.2 (2026-11-04)

No entries for `libtmux-async`.
Expand Down
10 changes: 10 additions & 0 deletions gems/libtmux-async/lib/libtmux/async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ def self.attach_cleanup(error, details)
end
private_class_method :attach_cleanup

# Own a private terminal program with scheduler-aware tmux requests.
def self.start_program(argv, parent: ::Async::Task.current, **options, &block)
adapter = ->(server) { Scope.new(parent: parent, server: server) }
Program.__send__(:start_with, adapter, argv, **options, &block)
end

def self.run_program(argv, **options)
start_program(argv, **options) { |program| program.wait(**options.slice(:timeout, :cancel)) }
end

def self.open(server:, parent: ::Async::Task.current, **options)
raise ArgumentError, "Async scope requires a block" unless block_given?

Expand Down
4 changes: 4 additions & 0 deletions gems/libtmux-async/sig/libtmux-async.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module LibTmux

def self.open: [T] (server: LibTmux::Server, ?parent: untyped, **untyped) { (Scope) -> T } -> T

def self.start_program: (Array[String], ?parent: untyped, ?executable: String, ?cwd: String?, ?environment: Hash[String, String], ?width: Integer, ?height: Integer, ?timeout: Numeric, ?cancel: untyped) -> LibTmux::Program
| [T] (Array[String], ?parent: untyped, ?executable: String, ?cwd: String?, ?environment: Hash[String, String], ?width: Integer, ?height: Integer, ?timeout: Numeric, ?cancel: untyped) { (LibTmux::Program) -> T } -> T
def self.run_program: (Array[String], ?parent: untyped, ?executable: String, ?cwd: String?, ?environment: Hash[String, String], ?width: Integer, ?height: Integer, ?timeout: Numeric, ?cancel: untyped) -> LibTmux::Program::Result

type scope_diagnostics = {
transport: :async_process, closed: bool, admitted_requests: Integer,
reserved_process_slots: Integer,
Expand Down
3 changes: 3 additions & 0 deletions gems/libtmux/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ in the [main changelog](../../CHANGELOG.md).

## 0.1.3 (unreleased)

- Preview `LibTmux.start_program` and `LibTmux.run_program` for literal argv
in a private tmux terminal with a typed exit and bounded final screen.

## 0.1.2 (2026-11-04)

No entries for `libtmux`.
Expand Down
1 change: 1 addition & 0 deletions gems/libtmux/lib/libtmux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
require "libtmux/group"
require "libtmux/owned"
require "libtmux/terminal"
require "libtmux/program"
5 changes: 5 additions & 0 deletions gems/libtmux/lib/libtmux/capabilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def to_h
CONTROL_CHANNELS = %w[LibTmux::ControlConnection#exchange LibTmux::Async::ControlConnection#exchange].freeze

CAPABILITIES = [
Capability.new(id: "owned-terminal-program", name: "Owned terminal program", since: "3.7",
handling: :refuse, error: ERROR,
summary: "Wait for process exit and retain the drained terminal screen on a private daemon.",
apis: ["LibTmux.start_program", "LibTmux.run_program", "LibTmux::Async.start_program", "LibTmux::Async.run_program"],
commands: []),
Capability.new(id: "control-mode", name: "Control mode", since: "3.7", handling: :refuse, error: ERROR,
summary: "Attach a library-managed control client and stream events.",
apis: ["LibTmux::Server#open_control", "LibTmux::Async::Server#open_control",
Expand Down
Loading
Loading