MCPcopy Index your code
hub / github.com/codeaashu/claude-code / detectAndGetBackend

Function detectAndGetBackend

src/utils/swarm/backends/registry.ts:136–254  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

134 * 5. Otherwise, throw error with instructions
135 */
136export 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 = {

Callers 4

handleSpawnSplitPaneFunction · 0.85
handleSpawnFunction · 0.85
getBackendFunction · 0.85
getPaneBackendExecutorFunction · 0.85

Calls 10

ensureBackendsRegisteredFunction · 0.85
logForDebuggingFunction · 0.85
isInITerm2Function · 0.85
createTmuxBackendFunction · 0.85
getPreferTmuxOverIterm2Function · 0.85
createITermBackendFunction · 0.85
isInsideTmuxFunction · 0.70
isIt2CliAvailableFunction · 0.70
isTmuxAvailableFunction · 0.70

Tested by

no test coverage detected