Files
Codeman/test/session-resize-arbitration.test.ts
T
Kamil KaliszandClaude Opus 5.5 70ee1312d4 fix(tui): Redraw wins, leaks are told by process, detach unsets the whole hook array
Review follow-up on the follow-typing attach.

- Redraw (and every forced resize at an unchanged size) handed the window
  back to the terminal: an unchanged pty sends tmux no SIGWINCH, so the
  terminal stayed the latest client and the after-resize-window hook undid
  the resize-window. Session.resize() now nudges the pty one row first
  (FORCED_RESIZE_NUDGE_MS), then applies the size, so tmux counts the tab
  as the client used last; the second move bumps the guarded geometry
  revision too. Under manual with no attach the agent sees no change.
- With the nudge, client-active[91] alone covers every case, so the
  after-resize-window hook is gone. It kept latest through every browser
  resize and cost the agent three height changes per resize; now the first
  resize after the terminal was used costs two and later ones one. The
  cost is stated next to the hook.
- The leak sweep matched owners by tty, and Linux hands the next terminal
  the pty that just closed, so the common leak was never swept. The arm
  now records the TUI's pid (@codeman-tui-follow-pid), and the sweep keeps
  only sessions this process armed (a Set filled before the first write)
  or whose pid is another live process. One list-sessions call reads every
  owner. It also runs before each attach dresses its sessions, so
  `codeman tui <n>` gets it and a leaked hook cannot undo presizeWindow().
- `set-hook -u` on our index left an empty session-level array, which
  hides the user's global hook on that event. Disarm unsets the whole
  event when ours is its only entry, and arm refuses while the user has a
  global client-active hook.
- Arm and disarm are one tmux call to read and one to write (`;` lists).
- Docs: the "forced resize when a browser opens a session" wording was
  wrong (selectSession sends no force; only Redraw, a closing pop-out and
  the split pane do), fixed in tui-client.ts, docs/tui.md and the
  invariants, whose paragraph now stands on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 10:46:57 +01:00

259 lines
9.8 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { Session } from '../src/session.js';
/** Must exceed Session.DESKTOP_CLAIM_IDLE_MS (90s) */
const PAST_IDLE_MS = 91_000;
type ResizeableSessionInternals = {
ptyProcess: { resize: (cols: number, rows: number) => void };
_ptyCols: number;
_ptyRows: number;
};
function attachFakePty(session: Session, cols = 160, rows = 48) {
const resize = vi.fn();
const internals = session as unknown as ResizeableSessionInternals;
internals.ptyProcess = { resize };
internals._ptyCols = cols;
internals._ptyRows = rows;
return resize;
}
describe('Session.ptyGeometry', () => {
// ⚠️ `resize()` writes `_ptyCols`/`_ptyRows` only when `ptyProcess` is set,
// and nothing seeds them from the spawn geometry — so the fields hold the
// constructor defaults of 120x40 for any session whose pane is not running.
// Reporting those to a client made it adopt a width no process had ever been
// told, and on anything narrower than 120 columns claim another device owned
// the pane when none existed (issue #464).
it('reports nothing for a session that has no pane', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
expect(session.ptyGeometry).toBeNull();
});
it('still reports nothing after a resize it could not apply', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
session.resize(45, 20, { viewportType: 'mobile' });
// The resize was swallowed (no pty to resize), so there is no geometry to
// report — NOT the 120x40 the fields still hold.
expect(session.ptyGeometry).toBeNull();
});
it('reports the pane geometry once a pane exists, and follows a resize', () => {
// The contrast, so "always null" would fail this.
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
attachFakePty(session, 160, 48);
expect(session.ptyGeometry).toEqual({ cols: 160, rows: 48 });
session.resize(62, 40, { viewportType: 'mobile' });
expect(session.ptyGeometry).toEqual({ cols: 62, rows: 40 });
});
});
describe('Session resize arbitration', () => {
it('lets a mobile-only session shrink below the spawn default (no desktop connected)', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.resize(48, 28, { viewportType: 'mobile' });
expect(resize).toHaveBeenCalledWith(48, 28);
});
it('lets a mobile-only session shrink rows only', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.resize(160, 28, { viewportType: 'mobile' });
expect(resize).toHaveBeenCalledWith(160, 28);
});
it('lets a mobile-only session re-grow after shrinking', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.resize(48, 28, { viewportType: 'mobile' });
session.resize(80, 36, { viewportType: 'tablet' });
expect(resize).toHaveBeenNthCalledWith(1, 48, 28);
expect(resize).toHaveBeenNthCalledWith(2, 80, 36);
});
it('ignores mobile resizes while a desktop connection holds a sizing claim', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
const desktop = Symbol('desktop-conn');
session.claimDesktopSizing(desktop);
session.resize(48, 28, { viewportType: 'mobile' });
// Grow is ignored too — it would reflow the desktop view just the same.
session.resize(200, 60, { viewportType: 'tablet' });
expect(resize).not.toHaveBeenCalled();
});
it('always applies desktop resizes, claim or not', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.claimDesktopSizing(Symbol('desktop-conn'));
session.resize(120, 40, { viewportType: 'desktop' });
expect(resize).toHaveBeenCalledWith(120, 40);
});
it('restores mobile control once the desktop claim is released', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
const desktop = Symbol('desktop-conn');
session.claimDesktopSizing(desktop);
session.resize(48, 28, { viewportType: 'mobile' });
expect(resize).not.toHaveBeenCalled();
session.releaseDesktopSizing(desktop);
session.resize(48, 28, { viewportType: 'mobile' });
expect(resize).toHaveBeenCalledWith(48, 28);
});
it('keeps ignoring mobile resizes until every desktop claim is released', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
const desktopA = Symbol('desktop-a');
const desktopB = Symbol('desktop-b');
session.claimDesktopSizing(desktopA);
session.claimDesktopSizing(desktopB);
session.releaseDesktopSizing(desktopA);
session.resize(48, 28, { viewportType: 'mobile' });
expect(resize).not.toHaveBeenCalled();
session.releaseDesktopSizing(desktopB);
session.resize(48, 28, { viewportType: 'mobile' });
expect(resize).toHaveBeenCalledWith(48, 28);
});
it('applies untyped (legacy/API) resizes regardless of claims', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.claimDesktopSizing(Symbol('desktop-conn'));
session.resize(100, 30);
expect(resize).toHaveBeenCalledWith(100, 30);
});
// A forced resize at an unchanged size is a device asking for the pane back
// (Redraw). An unchanged pty gives tmux no SIGWINCH, so a `codeman tui`
// attach would keep the window; a one-row nudge first makes this client
// the one tmux counts as used last.
it('applies forced resizes even when the dimensions did not change, through a one-row nudge', () => {
vi.useFakeTimers();
try {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 120, 40);
session.resize(120, 40, { force: true });
expect(resize.mock.calls).toEqual([[120, 39]]);
const between = session.guardedInputGeneration;
vi.advanceTimersByTime(Session.FORCED_RESIZE_NUDGE_MS);
// The second move invalidates guarded input read between the two.
expect(session.guardedInputGeneration).not.toBe(between);
expect(resize.mock.calls).toEqual([
[120, 39],
[120, 40],
]);
expect(session.ptyGeometry).toEqual({ cols: 120, rows: 40 });
} finally {
vi.useRealTimers();
}
});
it('drops the end of a nudge that a newer resize overtook', () => {
vi.useFakeTimers();
try {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 120, 40);
session.resize(120, 40, { force: true });
session.resize(100, 30);
vi.advanceTimersByTime(Session.FORCED_RESIZE_NUDGE_MS);
expect(resize.mock.calls).toEqual([
[120, 39],
[100, 30],
]);
} finally {
vi.useRealTimers();
}
});
it('does not nudge a forced resize that changes the size', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 120, 40);
session.resize(100, 30, { force: true });
expect(resize.mock.calls).toEqual([[100, 30]]);
});
describe('idle-desktop override (whoever is active wins)', () => {
afterEach(() => {
vi.useRealTimers();
});
it('lets a mobile client take the pane once the desktop claim goes idle', () => {
vi.useFakeTimers();
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.claimDesktopSizing(Symbol('desktop-conn'));
session.resize(48, 28, { viewportType: 'mobile' });
expect(resize).not.toHaveBeenCalled(); // fresh claim → ignored
vi.advanceTimersByTime(PAST_IDLE_MS);
session.resize(48, 28, { viewportType: 'mobile' });
expect(resize).toHaveBeenCalledWith(48, 28); // idle desktop → applied
});
it('keeps blocking mobile while the desktop stays active via typed input', () => {
vi.useFakeTimers();
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.claimDesktopSizing(Symbol('desktop-conn'));
vi.advanceTimersByTime(PAST_IDLE_MS - 10_000);
session.noteDesktopActivity(); // user typed on desktop
vi.advanceTimersByTime(20_000); // idle since claim, but not since input
session.resize(48, 28, { viewportType: 'mobile' });
expect(resize).not.toHaveBeenCalled();
});
it('re-asserts the desktop layout on desktop input after a mobile override', () => {
vi.useFakeTimers();
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.resize(208, 45, { viewportType: 'desktop' }); // desktop sizes the pane
session.claimDesktopSizing(Symbol('desktop-conn'));
vi.advanceTimersByTime(PAST_IDLE_MS);
session.resize(48, 28, { viewportType: 'mobile' }); // phone takes over
expect(resize).toHaveBeenLastCalledWith(48, 28);
session.noteDesktopActivity(); // desktop user types again
expect(resize).toHaveBeenLastCalledWith(208, 45); // layout restored
});
it('does not re-assert when no mobile override happened', () => {
const session = new Session({ workingDir: '/tmp', mode: 'shell' });
const resize = attachFakePty(session, 160, 48);
session.resize(208, 45, { viewportType: 'desktop' });
resize.mockClear();
session.noteDesktopActivity();
expect(resize).not.toHaveBeenCalled();
});
});
});