szn

Bug 442 — Automatic window renaming latency from fixed 1000ms polling rate-limit

442. Automatic window renaming latency from fixed 1000ms polling rate-limit

Severity: LOW
Status: Fixed

Automatic window renaming updated the window title to match the active foreground process (e.g. vim, git, cargo, zsh), but users experienced a perceptible delay (up to 1,000 ms) whenever a program started or exited.


Root Cause Analysis

In src/server/server.zig:renderToDisplayClient(), automatic renaming evaluated foreground process inspection with a hardcoded time-based throttle:

if (win.last_foreground_name.len == 0 or
    now - win.last_foreground_check_ms >= 1000)
  1. When a user executed a command at the shell prompt, processInput dispatched <Enter>. At that exact millisecond, the shell had not yet fork()ed and exec()ed the child program.
  2. The initial render checked getForegroundProcessName(), observed the shell (zsh), and stamped last_foreground_check_ms = now.
  3. When the child process (vim) spawned ~10–50 ms later and began rendering, now - win.last_foreground_check_ms was only ~50 ms (< 1000 ms). The server skipped checking the foreground process.
  4. The window title and status bar remained displaying zsh for up to a full second after Vim had already taken over the screen.
  5. Similarly, when Vim terminated and returned to the shell prompt, zsh was not reflected in the title until the 1,000 ms interval elapsed.

Solution

Implemented instantaneous PGID-based state change detection:

  1. Added last_foreground_pgid: i32 = -1 to Window.
  2. Added getForegroundPgid() and getProcessNameByPgid() to Pty using the lightweight tcgetpgrp(self.master) syscall (~0.5 µs).
  3. In renderToDisplayClient(), whenever tcgetpgrp indicates that the foreground process group ID has changed (cur_pgid != win.last_foreground_pgid), the foreground process name is queried and applied instantly on that very frame (0 ms latency).
  4. When cur_pgid == win.last_foreground_pgid, process name resolution is skipped until 500 ms expires, maintaining near-zero CPU usage.