()
| 134 | * 5. Otherwise, throw error with instructions |
| 135 | */ |
| 136 | export async function detectAndGetBackend(): Promise<BackendDetectionResult> { |
| 137 | // Ensure backends are registered before detection |
| 138 | await ensureBackendsRegistered() |
| 139 | |
| 140 | // Return cached result if available |
| 141 | if (cachedDetectionResult) { |
| 142 | logForDebugging( |
| 143 | `[BackendRegistry] Using cached backend: ${cachedDetectionResult.backend.type}`, |
| 144 | ) |
| 145 | return cachedDetectionResult |
| 146 | } |
| 147 | |
| 148 | logForDebugging('[BackendRegistry] Starting backend detection...') |
| 149 | |
| 150 | // Check all environment conditions upfront for logging |
| 151 | const insideTmux = await isInsideTmux() |
| 152 | const inITerm2 = isInITerm2() |
| 153 | |
| 154 | logForDebugging( |
| 155 | `[BackendRegistry] Environment: insideTmux=${insideTmux}, inITerm2=${inITerm2}`, |
| 156 | ) |
| 157 | |
| 158 | // Priority 1: If inside tmux, always use tmux |
| 159 | if (insideTmux) { |
| 160 | logForDebugging( |
| 161 | '[BackendRegistry] Selected: tmux (running inside tmux session)', |
| 162 | ) |
| 163 | const backend = createTmuxBackend() |
| 164 | cachedBackend = backend |
| 165 | cachedDetectionResult = { |
| 166 | backend, |
| 167 | isNative: true, |
| 168 | needsIt2Setup: false, |
| 169 | } |
| 170 | return cachedDetectionResult |
| 171 | } |
| 172 | |
| 173 | // Priority 2: If in iTerm2, try to use native panes |
| 174 | if (inITerm2) { |
| 175 | // Check if user previously chose to prefer tmux over iTerm2 |
| 176 | const preferTmux = getPreferTmuxOverIterm2() |
| 177 | if (preferTmux) { |
| 178 | logForDebugging( |
| 179 | '[BackendRegistry] User prefers tmux over iTerm2, skipping iTerm2 detection', |
| 180 | ) |
| 181 | } else { |
| 182 | const it2Available = await isIt2CliAvailable() |
| 183 | logForDebugging( |
| 184 | `[BackendRegistry] iTerm2 detected, it2 CLI available: ${it2Available}`, |
| 185 | ) |
| 186 | |
| 187 | if (it2Available) { |
| 188 | logForDebugging( |
| 189 | '[BackendRegistry] Selected: iterm2 (native iTerm2 with it2 CLI)', |
| 190 | ) |
| 191 | const backend = createITermBackend() |
| 192 | cachedBackend = backend |
| 193 | cachedDetectionResult = { |
no test coverage detected