Skip to content

bug: --background doesn't suppress macOS app-foregrounding when creating tabs #401

@voidfreud

Description

@voidfreud

Root cause (found via investigation — see comment)

--background was designed to prevent iTerm2 from re-focusing a different session within itself. It never handled macOS raising iTerm2.app to the foreground when a new tab materialises. capture_focus / restore_focus in src/ita/_core.py:303-349 only snapshot and restore the iTerm2-internal focused session — they never capture or restore the macOS frontmost application.

Observation: During agent runs, Alex's focus was stolen by iTerm2 coming to the front each time a tab was created. PR #392's env var / --background flag all worked as designed. The env-var plumbing was never the bug.

Scope

Affects every use of ita new / ita tab new / ita layout ... — including real agent workflows, not just tests. When an agent creates a tab while user is in another app, macOS raises iTerm2 to foreground.

Proposed fix (proper)

Extend capture_focus / restore_focus to also preserve the macOS frontmost app:

  • Option A (PyObjC): NSWorkspace.sharedWorkspace().frontmostApplication() → store in FocusSnapshot → NSRunningApplication.activateWithOptions_ to restore. Requires PyObjC as a dep.
  • Option B (osascript shell-out): osascript -e 'tell application "System Events" to get name of first process whose frontmost is true' → store → osascript -e 'tell application "X" to activate' to restore. No new dep, small subprocess overhead per creation.

Option B is probably the cleaner near-term fit (no new dep, ita already uses osascript in a few places). Option A is cleaner long-term if PyObjC is already transitively pulled.

Priority

Medium-high. Real UX impact beyond tests.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions