Batch tmux configuration into single client invocations

configure() ran one tmux client per option and per key binding, and
refresh_labels() one or two per window, on every start/refresh/open: a
no-op refresh with three windows spawned 45 tmux processes. On a phone
each spawn is noticeable.

Add run_batch(), which joins commands with ';' separators (escaping
arguments that end in ';', which tmux would otherwise split) and replays
them one by one if the batch fails so errors stay attributable. Use it
for configure(), window creation, endpoint replacement and label
refreshes. list-windows now also reports the stored short label so
unchanged labels are skipped, and managed_windows() no longer probes
has-session first. The same no-op refresh now spawns 9 processes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-10-03 03:02:31 +00:00
co-authored by Claude Fable 5.1
parent 83dc7b5ef9
commit 907813867a
2 changed files with 171 additions and 60 deletions
+53
View File
@@ -351,6 +351,59 @@ class TmuxIntegrationTests(unittest.TestCase):
self.assertIn(str(self.fake_ssh), command)
self.assertIn("-tt", command)
def test_reconfiguration_batches_tmux_calls_and_escapes_semicolons(self) -> None:
calls: list[list[str]] = []
real_run = self.hub._run # type: ignore[attr-defined]
def counting_run(command, **keywords): # type: ignore[no-untyped-def]
calls.append(list(command))
return real_run(command, **keywords)
self.hub._run = counting_run # type: ignore[attr-defined]
self.hub.ensure_server()
for name in ("a", "b", "c"):
self.hub.create_window(Endpoint.local(name), str(self.fake_herdr))
self.hub.remove_bootstrap()
# A no-op refresh (nothing new discovered, labels unchanged) used to
# spawn one tmux client per option and per window label.
calls.clear()
self.hub.ensure_server()
self.hub.add_missing([], str(self.fake_herdr))
self.hub.managed_windows()
self.assertLessEqual(len(calls), 10, calls)
self.assertEqual(self.hub.run(["show-options", "-gv", "prefix"]).stdout.strip(), "C-a")
self.assertIn("display-popup", self.hub.run(["list-keys", "-T", "prefix", "g"]).stdout)
# tmux treats any argument ending in ';' as a command separator.
self.hub.run_batch(
[
["set-option", "-g", "@herdr_hub_test_one", "value;"],
["set-option", "-g", "@herdr_hub_test_two", "other"],
]
)
self.assertEqual(
self.hub.run(["show-options", "-gv", "@herdr_hub_test_one"]).stdout.strip(),
"value;",
)
self.assertEqual(
self.hub.run(["show-options", "-gv", "@herdr_hub_test_two"]).stdout.strip(),
"other",
)
# A failing command is attributed precisely instead of silently
# skipping the rest of the batch.
with self.assertRaisesRegex(HubError, "no-such-option"):
self.hub.run_batch(
[
["set-option", "-g", "@herdr_hub_test_three", "set"],
["set-option", "-g", "no-such-option", "1"],
]
)
self.assertEqual(
self.hub.run(["show-options", "-gv", "@herdr_hub_test_three"]).stdout.strip(),
"set",
)
def test_concurrent_open_of_the_same_endpoint_creates_one_window(self) -> None:
self.hub.ensure_server()
second_hub = TmuxHub(