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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Notable changes to `libtmux`.
and return types.
- Docs: the Lua API export uses original source lines and records SHA-256
digests for its Lua inputs. (#113)
- Runtime: `Server:capabilities()` reports feature support and workarounds
for the daemon release checked when connecting. (#104)

## 0.1.1-1 (2026-11-01)

Expand Down
20 changes: 20 additions & 0 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ also run the check with a 3.3a client against the old daemon, which covers the

## Optional features

Use `Server:capabilities():await()` to check a feature before attempting it.
The [complete program](../examples/api/capabilities.lua) starts and stops its
own daemon and checks named buffer deletion against it.

The request uses the daemon release checked when the handle connected.
It sends no tmux command. A pinned path is rechecked and a stale path
returns `stale_generation`. With a named or default socket, the report
describes the bound release until the runtime detects a change. Use a new
connection for a fresh measurement.

- `version` records the daemon's release at connection.
- `features[id]` gives the requirement, affected APIs and commands, and
boolean `supported` decision.
- `workarounds[id]` gives release-specific defect details and the boolean
`active` decision.

Unknown IDs are absent. The report is an observation; later daemon changes do
not update it. Editing it cannot change the library's command guards or a
later report. Those guards still check every operation before dispatch.

The 3.3a minimum covers subprocess commands, typed operations, snapshots and
polling captures. Each feature below names its own release requirement. A
refusal has a test that fails when the guard is removed.
Expand Down
1 change: 1 addition & 0 deletions examples/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ to select a different build.
| [start_server.lua](start_server.lua) | Start a private server, use it and stop it | `sessions: 0, then 1` |
| [with_server.lua](with_server.lua) | Run a body on a server that stops when the body ends | `sessions: 1` |
| [connect.lua](connect.lua) | Find the `work` session on a tmux server | `work` |
| [capabilities.lua](capabilities.lua) | Check a feature on the connected daemon | `named deletion requires: 3.4`, then `daemon decision checked` |
| [snapshot.lua](snapshot.lua) | List sessions, windows, and panes | Two of each |
| [new_session.lua](new_session.lua) | Create a session | `session: demo` |
| [new_window.lua](new_window.lua) | Create a window in that session | `window: logs` |
Expand Down
27 changes: 27 additions & 0 deletions examples/api/capabilities.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local adapter = require("libtmux.runtime.luv")

local function must(value, err)
if err ~= nil then
error(tostring(err), 0)
end
return value
end

must(adapter.run(function(runtime)
local owned = must(runtime:start_server():await())
local report = must(owned.server:capabilities():await())
local deletion = assert(report.features["buffer-delete-named"])
print("named deletion requires: " .. deletion.since)

must(owned.server:set_buffer("demo", "kept until deletion is safe"):await())
if deletion.supported then
must(owned.server:delete_buffer("demo"):await())
else
local deleted, err = owned.server:delete_buffer("demo"):await()
assert(deleted == nil and err.code == "unsupported" and err.effect == "not_sent")
end
print("daemon decision checked")

must(owned:stop():await())
return true
end))
11 changes: 11 additions & 0 deletions examples/api/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
"description": "Find the session named work on a supplied tmux socket or try it on a private server.",
"stdout": "work\n"
},
{
"id": "capabilities",
"symbols": [
"libtmux.Server:capabilities",
"libtmux.CapabilityReport.features",
"libtmux.CapabilityStatus.supported"
],
"file": "examples/api/capabilities.lua",
"description": "Check named feature requirements against the connected daemon and use its decision.",
"stdout": "named deletion requires: 3.4\ndaemon decision checked\n"
},
{
"id": "snapshot",
"symbols": [
Expand Down
6 changes: 6 additions & 0 deletions lua/libtmux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ $ nvim --clean \
-c 'luafile examples/nvim_snapshot.lua'
```

`Server:capabilities` reports feature support for the daemon release checked
when connecting. See the [compatibility guide][guide-capabilities] and the
[complete example][example-capabilities].

### [`libtmux.query`][ref-query]: tables without tmux

[`select`][ref-query-select] wraps Lua tables in a [`Selection`][ref-selection],
Expand Down Expand Up @@ -251,3 +255,5 @@ of a table. The [query guide][guide-query] lists the criteria both accept.
[src-luv]: https://github.com/libtmux/libtmux-lua/blob/master/lua/libtmux/runtime/luv.lua
[src-nvim]: https://github.com/libtmux/libtmux-lua/blob/master/lua/libtmux/runtime/nvim.lua
[src-query]: https://github.com/libtmux/libtmux-lua/blob/master/lua/libtmux/query.lua
[guide-capabilities]: https://github.com/libtmux/libtmux-lua/blob/master/docs/compatibility.md#optional-features
[example-capabilities]: https://github.com/libtmux/libtmux-lua/blob/master/examples/api/capabilities.lua
9 changes: 9 additions & 0 deletions lua/libtmux/_internal/endpoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,15 @@ function Bound:generation()
return bindings[self].generation
end

function Bound:_checked_evidence()
local state = assert(bindings[self], "invalid endpoint")
local valid, err = verify(state)
if not valid then
return nil, err
end
return self:evidence()
end

function Bound:close()
local state = assert(bindings[self], "invalid endpoint")
invalidate(state, "endpoint closed", "endpoint_closed")
Expand Down
33 changes: 33 additions & 0 deletions lua/libtmux/_internal/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local domain = require("libtmux._internal.domain")
local live_query = require("libtmux._internal.live_query")
local observation = require("libtmux._internal.observation")
local program = require("libtmux._internal.program")
local capability_registry = require("libtmux._internal.support.capabilities")
local M, Server = {}, {}
-- Live objects own state; the registry must not root returned-request cycles on Lua 5.1.
local servers = setmetatable({}, { __mode = "kv" })
Expand Down Expand Up @@ -383,6 +384,29 @@ local function capture(state, options)
end, { operation = "snapshot", effect = "not_sent" })
end

--- Report feature support for the daemon release checked when connecting.
--- Rechecks a pinned socket's identity. Named and default sockets use the
--- original binding until the runtime detects a change. The returned report
--- owns its metadata, and editing it leaves command guards intact.
---@return libtmux.Request<libtmux.CapabilityReport>
function Server:capabilities()
local state = servers[self]
return state.runtime:_operation(function()
if state.closed then
return nil,
errors.new("closed", "server handle is closed", {
operation = "capabilities",
effect = "not_sent",
})
end
local evidence, err = state.bound:_checked_evidence()
if not evidence then
return nil, err
end
return capability_registry.report(evidence.version)
end, { operation = "capabilities", effect = "not_sent" })
end

function Server:get_option(name, options)
return settings.run(servers[self], nil, "option", "get", name, nil, options)
end
Expand Down Expand Up @@ -1380,6 +1404,14 @@ end
---
--- `libtmux.Runtime:connect`, `Server:close` and `Server:observe`
---
--- **Checking feature support.** `Server:capabilities` resolves to a
--- detached `libtmux.CapabilityReport` for the release checked at connection.
--- It reports named feature requirements and decisions, plus release-specific
--- workarounds. Editing the report leaves command guards intact.
---
--- `Server:capabilities`, `libtmux.CapabilityReport.features` and
--- `libtmux.CapabilityReport.workarounds`
---
--- **Finding one thing.** Take one record from a `libtmux.Snapshot`, or from
--- the rows of a query, with `libtmux.Selection:one`, which reports no match or
--- several as errors, or `libtmux.Selection:one_or_nil`, which also accepts no
Expand Down Expand Up @@ -1477,6 +1509,7 @@ end
---
--- `Server:command`, `Server:group` and `Server:batch`
---@class libtmux.Server
---@field capabilities fun(self:libtmux.Server):libtmux.Request<libtmux.CapabilityReport>
---@field get_option fun(self:libtmux.Server,name:libtmux.OptionName|string,
--- options?:libtmux.SettingGetOptions):
--- libtmux.Request<libtmux.OptionRecord>
Expand Down
59 changes: 59 additions & 0 deletions lua/libtmux/_internal/support/capabilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-- features.lua refuses with these thresholds, and scripts/emit_capabilities.lua
-- serializes this same table to capabilities.json, so the two cannot disagree.
-- Release names are inferred from the daemon's version; tmux advertises none.
local fields = require("libtmux._internal.fields")
local M = {}

local RAW = { "libtmux.Server:command", "libtmux.Server:batch", "libtmux.Server:group" }
Expand Down Expand Up @@ -232,4 +233,62 @@ function M.workaround(id)
return find(M.workarounds, id)
end

---@class libtmux.CapabilityStatus: libtmux.Capability
---@field supported boolean Whether the observed release provides this feature.

---@class libtmux.WorkaroundStatus
---@field id string
---@field name string
---@field summary string
---@field releases string[] Exact releases affected by this workaround.
---@field required string? First release that fixes this defect, when known.
---@field handling "refuse"|"repair"
---@field apis string[]
---@field commands libtmux.CapabilityCommand[]
---@field active boolean Whether the observed release needs this workaround.

---@class libtmux.CapabilityReport
---@field version string Daemon release checked at connection.
---@field features table<string, libtmux.CapabilityStatus> Named feature decisions and requirements.
---@field workarounds table<string, libtmux.WorkaroundStatus> Release-specific defects and handling.

local function copy(value)
if type(value) ~= "table" then
return value
end
local result = {}
for key, item in pairs(value) do
result[key] = copy(item)
end
return result
end

local function contains(values, version)
for _, value in ipairs(values) do
if value == version then
return true
end
end
return false
end

-- Each report owns its nested metadata; caller edits cannot change admission.
---@param version string
---@return libtmux.CapabilityReport
function M.report(version)
local report = { version = version, features = {}, workarounds = {} }
for _, entry in ipairs(M.capabilities) do
local status = copy(entry)
status.supported = fields.at_least(version, entry.since) == true
and not contains(entry.except, version)
report.features[entry.id] = status
end
for _, entry in ipairs(M.workarounds) do
local status = copy(entry)
status.active = contains(entry.releases, version)
report.workarounds[entry.id] = status
end
return report
end

return M
9 changes: 9 additions & 0 deletions scripts/check_editor/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ local function connect(doc, adapter, entry)
doc:complete(" pending:", { "await", "cancel", "result", "on_complete" })
doc:add(" local server = assert(pending:await())")
doc:complete(" server:", {
"capabilities",
"snapshot",
"close",
"handle",
Expand All @@ -124,6 +125,14 @@ local function connect(doc, adapter, entry)
"show_buffer",
"delete_buffer",
})
doc:add(" local discovery = server:capabilities()")
doc:complete(" discovery:", { "await", "cancel", "result", "on_complete" })
doc:add(" local capabilities = assert(discovery:await())")
doc:complete(" capabilities.", { "version", "features", "workarounds" })
doc:add(' local feature = capabilities.features["layout-json"]')
doc:complete(" feature.", { "id", "since", "except", "supported", "apis", "commands" })
doc:add(' local workaround = capabilities.workarounds["name-separators-rejected"]')
doc:complete(" workaround.", { "id", "releases", "required", "active", "handling" })
end

--- A buffer, a created session and the objects it holds: session, window and window link.
Expand Down
6 changes: 4 additions & 2 deletions scripts/docs/tests/test_api_examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ for symbol in
libtmux.WindowLink:move libtmux.Window:layout
libtmux.Server:start_program libtmux.Server:run_program
libtmux.Program:send_text libtmux.Program:send_keys libtmux.Program:wait
libtmux.Program:reference libtmux.Program:stop libtmux.Program:screen]]):gmatch("%S+")
libtmux.Program:reference libtmux.Program:stop libtmux.Program:screen
libtmux.Server:capabilities libtmux.CapabilityReport.features
libtmux.CapabilityStatus.supported]]):gmatch("%S+")
do
TARGETS[symbol] = true
end
Expand Down Expand Up @@ -73,7 +75,7 @@ end

function M.test_complete_programs_cover_their_existing_targets()
local loaded = api.load_examples(repo.root, TARGETS)
test.assertEquals(#loaded.examples, 22)
test.assertEquals(#loaded.examples, 23)
local seen = {}
for _, example in ipairs(loaded.examples) do
for _, symbol in ipairs(example.symbols) do
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/endpoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ local function work(runtime)
assert(server:close():await())
return mode .. " passed"
end
if mode == "capabilities_replacement" then
local server = assert(
runtime:connect({ binary = options.binary, socket_path = options.socket }):await()
)
local report = assert(server:capabilities():await())
ready(runtime, {
pid = assert(os.getenv("TMUX_DAEMON_PID")),
started = assert(os.getenv("TMUX_DAEMON_START")),
})
local replaced, err = server:capabilities():await()
assert(replaced == nil, "capability discovery accepted the replacement daemon")
assert(err.code == "stale_generation" and err.effect == "not_sent", tostring(err))
assert(report.version == os.getenv("TMUX_DAEMON_VERSION"))
assert(server:close():await())
return mode .. " passed"
end
if mode == "cancel_bind" then
local uv = runtime._driver.uv
local original = uv.fs_mkdtemp
Expand Down
30 changes: 28 additions & 2 deletions tests/integration/features.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,38 @@ local mode = assert(os.getenv("LIBTMUX_FEATURE_CASE"))
local function work(runtime)
local server = assert(runtime
:connect({
binary = assert(os.getenv("TMUX_BIN")),
binary = os.getenv("TMUX_CLIENT_BIN") or assert(os.getenv("TMUX_BIN")),
socket_path = assert(os.getenv("TMUX_SOCKET")),
})
:await())
local report
if mode == "observe" then
if mode == "capabilities" then
local discovered = assert(server:capabilities():await())
assert(discovered.version == os.getenv("TMUX_DAEMON_VERSION"), discovered.version)
local deletion = assert(discovered.features["buffer-delete-named"])
assert(deletion.since == "3.4" and deletion.handling == "refuse")
assert(discovered.features["unknown-feature"] == nil)
local expected = assert(os.getenv("LIBTMUX_EXPECT_DELETE")) == "yes"
assert(deletion.supported == expected, "feature decision disagrees with daemon")
deletion.since, deletion.supported = "1.0", true
deletion.commands[1].flags[1] = "-not-a-flag"
assert(server:set_buffer("capability-check", "owned"):await())
local deleted, err = server:delete_buffer("capability-check"):await()
if expected then
assert(deleted, tostring(err))
else
assert(deleted == nil and err.code == "unsupported" and err.effect == "not_sent")
end
local again = assert(server:capabilities():await())
assert(again.features["buffer-delete-named"].since == "3.4")
assert(again.features["buffer-delete-named"].supported == expected)
assert(again.features["buffer-delete-named"].commands[1].flags[1] == "-b")
assert(server:close():await())
local closed, closed_error = server:capabilities():await()
assert(closed == nil and closed_error.code == "closed")
assert(discovered.version == os.getenv("TMUX_DAEMON_VERSION"))
return "DISCOVERED " .. discovered.version
elseif mode == "observe" then
local snapshot =
assert(server:snapshot({ fields = { sessions = { "id", "name" } } }):await())
local session = assert(server:handle(snapshot, assert(snapshot.sessions:first())))
Expand Down
Loading