Skip to content

fix(mecatui): mirror the window title to the icon name so iTerm2 tabs show the conversation - #1585

Closed
amirejaz wants to merge 1 commit into
mainfrom
fix/1460-iterm-tab-title
Closed

amirejaz wants to merge 1 commit into
mainfrom
fix/1460-iterm-tab-title

Conversation

@amirejaz

Copy link
Copy Markdown

Problem

In iTerm2, every mecatui tab is labelled mecatui regardless of the conversation, so multiple sessions are indistinguishable (#1460).

mecatui already composes a per-conversation title (cmd/mecatui/ui/wintitle.go), but Bubble Tea emits tea.View.WindowTitle as OSC 2 only — the window title (title bar). iTerm2 honours xterm's title/icon split and labels tabs from the icon name (OSC 1), falling back to the job name when none is set. Terminals that don't distinguish the two (Ghostty, Alacritty, Kitty, WezTerm) show OSC 2 in the tab, which is why this went unnoticed.

Fix

ui.NewTitleMirror — a stateful pass-through term.File wrapping the program output at both composition sites (teaProgramOptions in cmd/mecatui/main.go). Every complete OSC 2 sequence flowing to the terminal is immediately twinned with an OSC 1 carrying the byte-identical payload.

Why at the output byte boundary rather than in the Update loop:

  • Inherits the renderer's discipline for free — Bubble Tea writes OSC 2 only when the title changed (never per frame) and clears it with an empty payload on exit; the mirror follows both, so tabs update on transitions and don't go stale after quit.
  • Provably zero UI-layer perturbation — no reducer changes, no Cmd-stream changes. (An earlier draft hooked the central Update wrapper; it broke 5 existing tests that treat the returned Cmd as the complete side-effect set. This design was chosen specifically to avoid that class.)
  • The labels cannot drift — the OSC 1 payload is copied from the OSC 2 bytes, never recomposed.
  • Detection preserved — the wrapper implements term.File (forwards Fd/Read/Close), because both Bubble Tea's TTY detection (p.output.(term.File) in initInput) and colorprofile.Detect type-assert it; a plain io.Writer would silently degrade colour rendering.

--terminal-title=off / MECATUI_NO_TERMINAL_TITLE=1 simply does not install the mirror, so "off" leaves the icon name untouched (whatever the shell set survives).

Safety notes: the scanner is purely observational (output = input + injections at sequence boundaries — a malformed/truncated sequence degrades to "no mirror", never a corrupted frame); state survives split Writes; payloads are capped (titleMirrorPayloadCap); the mirrored text was already escape-sanitised upstream by clampWindowTitle (C0/ESC/DEL stripped), so it cannot smuggle a terminator.

Tests

cmd/mecatui/ui/titlemirror_test.go:

  • injection with byte-exact passthrough of surrounding output
  • sequence split at every byte position across two Writes
  • both OSC terminators (BEL and ST)
  • empty-title mirror (exit-time icon-name clear)
  • byte-identical passthrough for non-title output (CSI, OSC 0/1/8/52, truncated/unterminated sequences)
  • malformed-payload abort + re-synchronisation (including an aborting ESC that itself starts the next introducer)
  • payload-overflow bound
  • term.File forwarding (Fd/Close)

Gates

  • task test green (root + engine + standalone); full cmd/mecatui/... suite green with zero pre-existing test perturbation
  • task lint clean for this diff (the one remaining finding, internal/adapter/privatefile/update_unix.go G115, is pre-existing on main and untouched here)
  • task docs:check green (386 documents, 0 broken links). Full task docs fails locally in sdk:install (pnpm under Node v20, ERR_UNKNOWN_BUILTIN_MODULE) — environmental, fails identically on clean main
  • go run ./cmd/mecademo prints a full offline session

Docs

docs/tui.md (terminal-title section + flag table) and the --terminal-title flag help now document the dual-sequence behaviour, the iTerm2 profile-title fallback, and the sharpened "off" semantics.

Scope

Classification: Routine — an extra escape sequence through the already-gated title path plus docs; no durable contract changed. The system-context-in-title request from #1460 is deliberately out of scope (it changes the composed-title contract/rune budget — Bounded, needs an acceptance plan), so this PR relates to, and does not close, #1460.

… show the conversation

Bubble Tea emits tea.View.WindowTitle as OSC 2 only (the title bar).
iTerm2 labels tabs from the ICON NAME (OSC 1) and falls back to the job
name when none is set, so every mecatui tab read a uniform 'mecatui'
and concurrent conversations were indistinguishable.

Wrap the program output (both composition sites) in ui.NewTitleMirror,
a stateful pass-through term.File that twins every complete OSC 2
sequence with a byte-identical OSC 1. Mirroring at the byte boundary
inherits the renderer's change-only emission and exit-time clear for
free, keeps the two labels provably identical, and touches no reducer:
the Update command stream is byte-identical with and without it. The
wrapper forwards Fd/Read/Close so Bubble Tea's TTY detection and
colorprofile.Detect (both assert term.File) keep seeing the real
descriptor. --terminal-title=off simply does not wrap, leaving the
icon name untouched.

Relates to #1460 (the system-context-in-title request from that issue
is deliberately out of scope here).

Co-Authored-By: mecatl <[email protected]>
@amirejaz amirejaz added bug Something isn't working tui mecatui terminal UI (rendering, keybindings, footer, panes, scrollback) labels Sep 15, 2026
@jbeda

jbeda commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

I'm going to take a closer look and review, but one thing I'd like to do is make sure that this has a setting in .config/mecatui/settings.yaml to disable it too. I set the title via a custom status bar program and don't want it to fight with this.

@jbeda

jbeda commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

I don't know how deep down this rabbit hole you want to go :)

This works but installing the filter and parsing things looking for where we set the title feels a little hacky to me.

Options:

  1. Continue to use bubbletea to set the window title but also set the "icon" name there. That isn't supported by bubbletea so we'd have to wire that in separately.
  2. Stop using bubbletea for window title and handle both window and icon through a dedicated component.

In addition, right now the window title is kind of long:
<session title> <short session handle> — <status> mecatui

We could allow people to customize that (and set different things for window vs icon) through extending/reusing the templating system for the status line. https://mecatl.dev/docs/mecatui/status-line. We could give them a super rich set of information to use and maybe have a custom title returned via the optional command executable.

Regardless:

  1. We need to make sure that this is documented in user-docs
  2. Test feedback from an agent:

The parser-level coverage is good, but the composition contract is still untested. Please add coverage that both normal and disconnected-recovery program construction install the mirror when titles are enabled, and that --terminal-title=off leaves an existing OSC 1 icon name untouched. Also extend split-write coverage to ST termination, including the ESC / \ boundary, and add a normal multi-title stream case to prove one OSC 1 twin is emitted for every completed OSC 2.

Let me know what your thoughts are.

@jbeda

jbeda commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Also, I'm happy to take this over and do the more comprehensive solution. Let me know

@amirejaz

Copy link
Copy Markdown
Author

@jbeda I’m not strongly confident in the current solution, so feel free to take over and reshape it however you think makes sense.

Quick context on the options: option 1 (emitting via the Update loop) broke existing tests that treat Update’s returned Cmd as the complete set of side effects, which is how I ended up with the filter. Option 2, with a dedicated component owning both sequences (set via a plain method call rather than a Cmd), seems like the cleanest path to me, but I’ll leave it to your judgment.

@jbeda

jbeda commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Sounds good! Putting together a plan now. Thanks!

@amirejaz

Copy link
Copy Markdown
Author

Closing in favor of the plan in #1616 — the controller covers everything this PR was after.

@amirejaz amirejaz closed this Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working tui mecatui terminal UI (rendering, keybindings, footer, panes, scrollback)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants