fix(tmux): stop nested prefix keys and TUI Alt+digits from moving other clients

Docker and owned-remote tmux servers kept the stock prefix table, so Ctrl+Q
then )/(/s/w/L switch-client'd the inner client onto a sibling. Those create
chains now leave only C-q send-prefix.

codeman tui bound M-1..M-9 in the server-wide root table. The keys stay, but
switch-client runs only for the attaching client (tty plus a one-shot pid).
A browser tab's Esc-then-digit no longer jumps sessions, and a killed TUI
leaves a binding that matches no live client.

Co-authored-by: DDT <1786035110@qq.com>
This commit is contained in:
DDTandDDT committed 2026-10-23 07:41:46 +00:00
1 parent 002de32a08
commit daeea54495
14 files changed
+728 -62

No files matched your search

+34 -8
View File
@@ -56,6 +56,8 @@ import { composerScroll, composerStep, composerText, createComposer, type TuiCom
import { formatAwayDigest } from './tui-digest.js';
import {
ATTACH_BANNER_MARKER,
isTuiClientTty,
readControllingTty,
TuiClient,
type TuiSessionOptions,
type TuiWindowSizing,
@@ -550,7 +552,14 @@ export async function beginAttachHandoff(
label: string,
tabs: readonly TuiAttachTab[] = [],
cols?: number,
rows?: number
rows?: number,
/**
* Tty the attach client will inherit. Omit to read this process's terminal.
* `null` means there is no usable tty: switch keys are not bound (an unscoped
* `switch-client` would move every client on the socket, including a browser
* tab). Tests pass a tty so they do not depend on stdin.
*/
clientTty?: string | null
): Promise<TuiAttachHandoff> {
const prefix = (await client.readPrefixKey(muxName)) ?? undefined;
// Read, not assumed: see detachChord() for the `d` vs `D` mix-up this closes.
@@ -584,13 +593,29 @@ export async function beginAttachHandoff(
// usable rather than decorative. Only the first nine, only sessions that
// really have a pane, and only keys tmux reports as free — the same rule the
// way-out key follows, so a binding of the user's own is never shadowed.
//
// The key table is server-global. The command is guarded to this terminal's
// client (see bindSwitchKey): without a tty we bind nothing, and a leaked
// unscoped `switch-client` from an older TUI is removed so it cannot keep
// moving browser tabs. When we do claim the keys, drop whatever previous
// attach left on M-1..M-9 first — including keys this strip will not reuse.
const switchKeys: string[] = [];
for (const tab of tabs.slice(0, 9)) {
if (!tab.muxName) continue;
const key = `M-${tab.index}`;
const bound = await client.readPrefixBinding(key, 'root');
if (bound !== null && !bound.startsWith('switch-client')) continue;
if (await client.bindSwitchKey(key, tab.muxName)) switchKeys.push(key);
const tty = clientTty === undefined ? readControllingTty() : clientTty || null;
const switchTabs = tabs.slice(0, 9).filter((tab) => tab.index >= 1 && tab.index <= 9 && tab.muxName);
const guarded = Boolean(tty && isTuiClientTty(tty));
if (!guarded) {
for (let index = 1; index <= 9; index++) await client.unbindSwitchKey(`M-${index}`, '');
} else if (switchTabs.length > 0 && (await client.armTuiSwitchGuard(tty as string))) {
for (let index = 1; index <= 9; index++) await client.unbindSwitchKey(`M-${index}`);
for (const tab of switchTabs) {
const key = `M-${tab.index}`;
const bound = await client.readPrefixBinding(key, 'root');
// Takeover just cleared ours. Anything still here belongs to the user.
if (bound !== null) continue;
if (await client.bindSwitchKey(key, tab.muxName as string, tty as string)) switchKeys.push(key);
}
} else {
for (let index = 1; index <= 9; index++) await client.unbindSwitchKey(`M-${index}`, '');
}
const bannerFor = (activeMux: string, ownLabel: string): Record<string, string> =>
buildAttachBanner({
@@ -638,7 +663,8 @@ export async function beginAttachHandoff(
return {
chord: oneKey ? ONE_KEY_DETACH : fallbackDetachChord(prefix, detachKey, claimed && alias ? alias : undefined),
async restore(): Promise<void> {
for (const key of switchKeys) await client.unbindSwitchKey(key);
for (const key of switchKeys) await client.unbindSwitchKey(key, tty ?? '');
if (tty && isTuiClientTty(tty)) await client.disarmTuiSwitchGuard(tty);
if (oneKey) await client.unbindDetachKey(ONE_KEY_DETACH, 'root');
if (claimed && alias) await client.unbindDetachKey(alias);
// Options first, then the size: dropping the status bar gives its row