szn

Bug 563 — Auto-rename clobbers OSC 0/2 and ESC k window titles

563. Automatic window rename clobbers application window titles within 500 ms

Severity: HIGH
Status: ✅ RESOLVED
Files:

Symptom

When launching programs such as vith from a shell inside szn, the window title briefly shows vith (or the document title), then reverts back to Python after ~500 ms:

  1. vith sends OSC 0 (\x1b]0;vith\x07) and ESC k (\x1bkvith\x1b\).
  2. Szn updates the window name in the status line to vith.
  3. Shortly afterwards, the status line changes back to Python.

Cause

  1. Auto-rename overwrite: In Server.renderToDisplayClient, the automatic window rename loop ran every 500 ms:
    if (proc_res) |proc_name_val| {
        if (proc_name_val.len > 0 and !std.mem.eql(u8, win.name, proc_name_val)) {
            win.setName(proc_name_val);
            win.setLastForegroundName(proc_name_val);
    

    On macOS, pty.getProcessNameByPgid calls Darwin’s proc_name(), returning "Python" (which reflects the kernel’s immutable p_comm field). Even though vith had set win.name to "vith", !std.mem.eql(u8, "vith", "Python") evaluated to true. Auto-rename treated this as a process name change and clobbered win.name back to "Python".

  2. Unhandled ESC k screen rename sequence: Because szn exports TERM=tmux-256color, applications also emit \x1bk<title>\x1b\. In advanceEsc, 'k' was not handled, resetting the parser to .ground and writing raw title bytes to the terminal grid.

Fix

  1. Custom title tracking on Window: Added has_custom_title: bool = false to Window. When windowTitleCallback receives a non-empty title (from OSC 0/2 or ESC k), it sets win.has_custom_title = true. If given an empty title (\x1b]0;\x07 or \x1bk\x1b\), it clears has_custom_title and restores last_foreground_name.
  2. Gated auto-rename: In renderToDisplayClient, auto-rename tracks whether the foreground process actually changed (!std.mem.eql(u8, win.last_foreground_name, proc_name_val)). When a foreground process change occurs (e.g. vith exits back to zsh), has_custom_title is cleared and win.name reverts to the shell. While the same process runs, has_custom_title protects the application’s title.
  3. ESC k support: In src/input.zig advanceEsc, 'k' transitions into osc_string pre-filled with "0;", routing ESC k <title> ST to the existing title callback and preventing escape bytes from leaking into the screen.

Verification