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
Root cause (found via investigation — see comment)
--backgroundwas 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_focusinsrc/ita/_core.py:303-349only 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 /
--backgroundflag 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_focusto also preserve the macOS frontmost app:NSWorkspace.sharedWorkspace().frontmostApplication()→ store in FocusSnapshot →NSRunningApplication.activateWithOptions_to restore. Requires PyObjC as a dep.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
ITA_DEFAULT_BACKGROUNDenv var (working as designed)--backgroundflag