File size: 21,019 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | /* eslint-disable custom-rules/no-process-exit */
import { feature } from 'bun:bundle'
import chalk from 'chalk'
import {
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
logEvent,
} from 'src/services/analytics/index.js'
import { getCwd } from 'src/utils/cwd.js'
import { checkForReleaseNotes } from 'src/utils/releaseNotes.js'
import { setCwd } from 'src/utils/Shell.js'
import { initSinks } from 'src/utils/sinks.js'
import {
getIsNonInteractiveSession,
getProjectRoot,
getSessionId,
setOriginalCwd,
setProjectRoot,
switchSession,
} from './bootstrap/state.js'
import { getCommands } from './commands.js'
import { initSessionMemory } from './services/SessionMemory/sessionMemory.js'
import { asSessionId } from './types/ids.js'
import { isAgentSwarmsEnabled } from './utils/agentSwarmsEnabled.js'
import { checkAndRestoreTerminalBackup } from './utils/appleTerminalBackup.js'
import { prefetchApiKeyFromApiKeyHelperIfSafe } from './utils/auth.js'
import { clearMemoryFileCaches } from './utils/claudemd.js'
import { getCurrentProjectConfig, getGlobalConfig } from './utils/config.js'
import { logForDiagnosticsNoPII } from './utils/diagLogs.js'
import { env } from './utils/env.js'
import { envDynamic } from './utils/envDynamic.js'
import { isBareMode, isEnvTruthy } from './utils/envUtils.js'
import { errorMessage } from './utils/errors.js'
import { findCanonicalGitRoot, findGitRoot, getIsGit } from './utils/git.js'
import { initializeFileChangedWatcher } from './utils/hooks/fileChangedWatcher.js'
import {
captureHooksConfigSnapshot,
updateHooksConfigSnapshot,
} from './utils/hooks/hooksConfigSnapshot.js'
import { hasWorktreeCreateHook } from './utils/hooks.js'
import { checkAndRestoreITerm2Backup } from './utils/iTermBackup.js'
import { logError } from './utils/log.js'
import { getRecentActivity } from './utils/logoV2Utils.js'
import { lockCurrentVersion } from './utils/nativeInstaller/index.js'
import type { PermissionMode } from './utils/permissions/PermissionMode.js'
import { getPlanSlug } from './utils/plans.js'
import { saveWorktreeState } from './utils/sessionStorage.js'
import { profileCheckpoint } from './utils/startupProfiler.js'
import {
createTmuxSessionForWorktree,
createWorktreeForSession,
generateTmuxSessionName,
worktreeBranchName,
} from './utils/worktree.js'
export async function setup(
cwd: string,
permissionMode: PermissionMode,
allowDangerouslySkipPermissions: boolean,
worktreeEnabled: boolean,
worktreeName: string | undefined,
tmuxEnabled: boolean,
customSessionId?: string | null,
worktreePRNumber?: number,
messagingSocketPath?: string,
): Promise<void> {
logForDiagnosticsNoPII('info', 'setup_started')
// Check for Node.js version < 18
const nodeVersion = process.version.match(/^v(\d+)\./)?.[1]
if (!nodeVersion || parseInt(nodeVersion) < 18) {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.error(
chalk.bold.red(
'Error: Claude Code requires Node.js version 18 or higher.',
),
)
process.exit(1)
}
// Set custom session ID if provided
if (customSessionId) {
switchSession(asSessionId(customSessionId))
}
// --bare / SIMPLE: skip UDS messaging server and teammate snapshot.
// Scripted calls don't receive injected messages and don't use swarm teammates.
// Explicit --messaging-socket-path is the escape hatch (per #23222 gate pattern).
if (!isBareMode() || messagingSocketPath !== undefined) {
// Start UDS messaging server (Mac/Linux only).
// Enabled by default for ants β creates a socket in tmpdir if no
// --messaging-socket-path is passed. Awaited so the server is bound
// and $CLAUDE_CODE_MESSAGING_SOCKET is exported before any hook
// (SessionStart in particular) can spawn and snapshot process.env.
if (feature('UDS_INBOX')) {
const m = await import('./utils/udsMessaging.js')
await m.startUdsMessaging(
messagingSocketPath ?? m.getDefaultUdsSocketPath(),
{ isExplicit: messagingSocketPath !== undefined },
)
}
}
// Teammate snapshot β SIMPLE-only gate (no escape hatch, swarm not used in bare)
if (!isBareMode() && isAgentSwarmsEnabled()) {
const { captureTeammateModeSnapshot } = await import(
'./utils/swarm/backends/teammateModeSnapshot.js'
)
captureTeammateModeSnapshot()
}
// Terminal backup restoration β interactive only. Print mode doesn't
// interact with terminal settings; the next interactive session will
// detect and restore any interrupted setup.
if (!getIsNonInteractiveSession()) {
// iTerm2 backup check only when swarms enabled
if (isAgentSwarmsEnabled()) {
const restoredIterm2Backup = await checkAndRestoreITerm2Backup()
if (restoredIterm2Backup.status === 'restored') {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.log(
chalk.yellow(
'Detected an interrupted iTerm2 setup. Your original settings have been restored. You may need to restart iTerm2 for the changes to take effect.',
),
)
} else if (restoredIterm2Backup.status === 'failed') {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.error(
chalk.red(
`Failed to restore iTerm2 settings. Please manually restore your original settings with: defaults import com.googlecode.iterm2 ${restoredIterm2Backup.backupPath}.`,
),
)
}
}
// Check and restore Terminal.app backup if setup was interrupted
try {
const restoredTerminalBackup = await checkAndRestoreTerminalBackup()
if (restoredTerminalBackup.status === 'restored') {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.log(
chalk.yellow(
'Detected an interrupted Terminal.app setup. Your original settings have been restored. You may need to restart Terminal.app for the changes to take effect.',
),
)
} else if (restoredTerminalBackup.status === 'failed') {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.error(
chalk.red(
`Failed to restore Terminal.app settings. Please manually restore your original settings with: defaults import com.apple.Terminal ${restoredTerminalBackup.backupPath}.`,
),
)
}
} catch (error) {
// Log but don't crash if Terminal.app backup restoration fails
logError(error)
}
}
// IMPORTANT: setCwd() must be called before any other code that depends on the cwd
setCwd(cwd)
// Capture hooks configuration snapshot to avoid hidden hook modifications.
// IMPORTANT: Must be called AFTER setCwd() so hooks are loaded from the correct directory
const hooksStart = Date.now()
captureHooksConfigSnapshot()
logForDiagnosticsNoPII('info', 'setup_hooks_captured', {
duration_ms: Date.now() - hooksStart,
})
// Initialize FileChanged hook watcher β sync, reads hook config snapshot
initializeFileChangedWatcher(cwd)
// Handle worktree creation if requested
// IMPORTANT: this must be called befiore getCommands(), otherwise /eject won't be available.
if (worktreeEnabled) {
// Mirrors bridgeMain.ts: hook-configured sessions can proceed without git
// so createWorktreeForSession() can delegate to the hook (non-git VCS).
const hasHook = hasWorktreeCreateHook()
const inGit = await getIsGit()
if (!hasHook && !inGit) {
process.stderr.write(
chalk.red(
`Error: Can only use --worktree in a git repository, but ${chalk.bold(cwd)} is not a git repository. ` +
`Configure a WorktreeCreate hook in settings.json to use --worktree with other VCS systems.\n`,
),
)
process.exit(1)
}
const slug = worktreePRNumber
? `pr-${worktreePRNumber}`
: (worktreeName ?? getPlanSlug())
// Git preamble runs whenever we're in a git repo β even if a hook is
// configured β so --tmux keeps working for git users who also have a
// WorktreeCreate hook. Only hook-only (non-git) mode skips it.
let tmuxSessionName: string | undefined
if (inGit) {
// Resolve to main repo root (handles being invoked from within a worktree).
// findCanonicalGitRoot is sync/filesystem-only/memoized; the underlying
// findGitRoot cache was already warmed by getIsGit() above, so this is ~free.
const mainRepoRoot = findCanonicalGitRoot(getCwd())
if (!mainRepoRoot) {
process.stderr.write(
chalk.red(
`Error: Could not determine the main git repository root.\n`,
),
)
process.exit(1)
}
// If we're inside a worktree, switch to the main repo for worktree creation
if (mainRepoRoot !== (findGitRoot(getCwd()) ?? getCwd())) {
logForDiagnosticsNoPII('info', 'worktree_resolved_to_main_repo')
process.chdir(mainRepoRoot)
setCwd(mainRepoRoot)
}
tmuxSessionName = tmuxEnabled
? generateTmuxSessionName(mainRepoRoot, worktreeBranchName(slug))
: undefined
} else {
// Non-git hook mode: no canonical root to resolve, so name the tmux
// session from cwd β generateTmuxSessionName only basenames the path.
tmuxSessionName = tmuxEnabled
? generateTmuxSessionName(getCwd(), worktreeBranchName(slug))
: undefined
}
let worktreeSession: Awaited<ReturnType<typeof createWorktreeForSession>>
try {
worktreeSession = await createWorktreeForSession(
getSessionId(),
slug,
tmuxSessionName,
worktreePRNumber ? { prNumber: worktreePRNumber } : undefined,
)
} catch (error) {
process.stderr.write(
chalk.red(`Error creating worktree: ${errorMessage(error)}\n`),
)
process.exit(1)
}
logEvent('tengu_worktree_created', { tmux_enabled: tmuxEnabled })
// Create tmux session for the worktree if enabled
if (tmuxEnabled && tmuxSessionName) {
const tmuxResult = await createTmuxSessionForWorktree(
tmuxSessionName,
worktreeSession.worktreePath,
)
if (tmuxResult.created) {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.log(
chalk.green(
`Created tmux session: ${chalk.bold(tmuxSessionName)}\nTo attach: ${chalk.bold(`tmux attach -t ${tmuxSessionName}`)}`,
),
)
} else {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.error(
chalk.yellow(
`Warning: Failed to create tmux session: ${tmuxResult.error}`,
),
)
}
}
process.chdir(worktreeSession.worktreePath)
setCwd(worktreeSession.worktreePath)
setOriginalCwd(getCwd())
// --worktree means the worktree IS the session's project, so skills/hooks/
// cron/etc. should resolve here. (EnterWorktreeTool mid-session does NOT
// touch projectRoot β that's a throwaway worktree, project stays stable.)
setProjectRoot(getCwd())
saveWorktreeState(worktreeSession)
// Clear memory files cache since originalCwd has changed
clearMemoryFileCaches()
// Settings cache was populated in init() (via applySafeConfigEnvironmentVariables)
// and again at captureHooksConfigSnapshot() above, both from the original dir's
// .claude/settings.json. Re-read from the worktree and re-capture hooks.
updateHooksConfigSnapshot()
}
// Background jobs - only critical registrations that must happen before first query
logForDiagnosticsNoPII('info', 'setup_background_jobs_starting')
// Bundled skills/plugins are registered in main.tsx before the parallel
// getCommands() kick β see comment there. Moved out of setup() because
// the await points above (startUdsMessaging, ~20ms) meant getCommands()
// raced ahead and memoized an empty bundledSkills list.
if (!isBareMode()) {
initSessionMemory() // Synchronous - registers hook, gate check happens lazily
if (feature('CONTEXT_COLLAPSE')) {
/* eslint-disable @typescript-eslint/no-require-imports */
;(
require('./services/contextCollapse/index.js') as typeof import('./services/contextCollapse/index.js')
).initContextCollapse()
/* eslint-enable @typescript-eslint/no-require-imports */
}
}
void lockCurrentVersion() // Lock current version to prevent deletion by other processes
logForDiagnosticsNoPII('info', 'setup_background_jobs_launched')
profileCheckpoint('setup_before_prefetch')
// Pre-fetch promises - only items needed before render
logForDiagnosticsNoPII('info', 'setup_prefetch_starting')
// When CLAUDE_CODE_SYNC_PLUGIN_INSTALL is set, skip all plugin prefetch.
// The sync install path in print.ts calls refreshPluginState() after
// installing, which reloads commands, hooks, and agents. Prefetching here
// races with the install (concurrent copyPluginToVersionedCache / cachePlugin
// on the same directories), and the hot-reload handler fires clearPluginCache()
// mid-install when policySettings arrives.
const skipPluginPrefetch =
(getIsNonInteractiveSession() &&
isEnvTruthy(process.env.CLAUDE_CODE_SYNC_PLUGIN_INSTALL)) ||
// --bare: loadPluginHooks β loadAllPlugins is filesystem work that's
// wasted when executeHooks early-returns under --bare anyway.
isBareMode()
if (!skipPluginPrefetch) {
void getCommands(getProjectRoot())
}
void import('./utils/plugins/loadPluginHooks.js').then(m => {
if (!skipPluginPrefetch) {
void m.loadPluginHooks() // Pre-load plugin hooks (consumed by processSessionStartHooks before render)
m.setupPluginHookHotReload() // Set up hot reload for plugin hooks when settings change
}
})
// --bare: skip attribution hook install + repo classification +
// session-file-access analytics + team memory watcher. These are background
// bookkeeping for commit attribution + usage metrics β scripted calls don't
// commit code, and the 49ms attribution hook stat check (measured) is pure
// overhead. NOT an early-return: the --dangerously-skip-permissions safety
// gate, tengu_started beacon, and apiKeyHelper prefetch below must still run.
if (!isBareMode()) {
if (process.env.USER_TYPE === 'ant') {
// Prime repo classification cache for auto-undercover mode. Default is
// undercover ON until proven internal; if this resolves to internal, clear
// the prompt cache so the next turn picks up the OFF state.
void import('./utils/commitAttribution.js').then(async m => {
if (await m.isInternalModelRepo()) {
const { clearSystemPromptSections } = await import(
'./constants/systemPromptSections.js'
)
clearSystemPromptSections()
}
})
}
if (feature('COMMIT_ATTRIBUTION')) {
// Dynamic import to enable dead code elimination (module contains excluded strings).
// Defer to next tick so the git subprocess spawn runs after first render
// rather than during the setup() microtask window.
setImmediate(() => {
void import('./utils/attributionHooks.js').then(
({ registerAttributionHooks }) => {
registerAttributionHooks() // Register attribution tracking hooks (ant-only feature)
},
)
})
}
void import('./utils/sessionFileAccessHooks.js').then(m =>
m.registerSessionFileAccessHooks(),
) // Register session file access analytics hooks
if (feature('TEAMMEM')) {
void import('./services/teamMemorySync/watcher.js').then(m =>
m.startTeamMemoryWatcher(),
) // Start team memory sync watcher
}
}
initSinks() // Attach error log + analytics sinks and drain queued events
// Session-success-rate denominator. Emit immediately after the analytics
// sink is attached β before any parsing, fetching, or I/O that could throw.
// inc-3694 (P0 CHANGELOG crash) threw at checkForReleaseNotes below; every
// event after this point was dead. This beacon is the earliest reliable
// "process started" signal for release health monitoring.
logEvent('tengu_started', {})
void prefetchApiKeyFromApiKeyHelperIfSafe(getIsNonInteractiveSession()) // Prefetch safely - only executes if trust already confirmed
profileCheckpoint('setup_after_prefetch')
// Pre-fetch data for Logo v2 - await to ensure it's ready before logo renders.
// --bare / SIMPLE: skip β release notes are interactive-UI display data,
// and getRecentActivity() reads up to 10 session JSONL files.
if (!isBareMode()) {
const { hasReleaseNotes } = await checkForReleaseNotes(
getGlobalConfig().lastReleaseNotesSeen,
)
if (hasReleaseNotes) {
await getRecentActivity()
}
}
// Start background fetch of OpenRouter models if configured
// This is non-blocking and runs in the background
const { getConfiguredAuthProvider } = await import('./utils/auth.js')
if (getConfiguredAuthProvider() === 'openrouter') {
const { startOpenRouterModelsFetch } = await import('./utils/model/openRouterModels.js')
startOpenRouterModelsFetch()
}
// If permission mode is set to bypass, verify we're in a safe environment
if (
permissionMode === 'bypassPermissions' ||
allowDangerouslySkipPermissions
) {
// Check if running as root/sudo on Unix-like systems
// Allow root if in a sandbox (e.g., TPU devspaces that require root)
if (
process.platform !== 'win32' &&
typeof process.getuid === 'function' &&
process.getuid() === 0 &&
process.env.IS_SANDBOX !== '1' &&
!isEnvTruthy(process.env.CLAUDE_CODE_BUBBLEWRAP)
) {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.error(
`--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons`,
)
process.exit(1)
}
if (
process.env.USER_TYPE === 'ant' &&
// Skip for Desktop's local agent mode β same trust model as CCR/BYOC
// (trusted Anthropic-managed launcher intentionally pre-approving everything).
// Precedent: permissionSetup.ts:861, applySettingsChange.ts:55 (PR #19116)
process.env.CLAUDE_CODE_ENTRYPOINT !== 'local-agent' &&
// Same for CCD (Claude Code in Desktop) β apps#29127 passes the flag
// unconditionally to unlock mid-session bypass switching
process.env.CLAUDE_CODE_ENTRYPOINT !== 'claude-desktop'
) {
// Only await if permission mode is set to bypass
const [isDocker, hasInternet] = await Promise.all([
envDynamic.getIsDocker(),
env.hasInternetAccess(),
])
const isBubblewrap = envDynamic.getIsBubblewrapSandbox()
const isSandbox = process.env.IS_SANDBOX === '1'
const isSandboxed = isDocker || isBubblewrap || isSandbox
if (!isSandboxed || hasInternet) {
// biome-ignore lint/suspicious/noConsole:: intentional console output
console.error(
`--dangerously-skip-permissions can only be used in Docker/sandbox containers with no internet access but got Docker: ${isDocker}, Bubblewrap: ${isBubblewrap}, IS_SANDBOX: ${isSandbox}, hasInternet: ${hasInternet}`,
)
process.exit(1)
}
}
}
if (process.env.NODE_ENV === 'test') {
return
}
// Log tengu_exit event from the last session?
const projectConfig = getCurrentProjectConfig()
if (
projectConfig.lastCost !== undefined &&
projectConfig.lastDuration !== undefined
) {
logEvent('tengu_exit', {
last_session_cost: projectConfig.lastCost,
last_session_api_duration: projectConfig.lastAPIDuration,
last_session_tool_duration: projectConfig.lastToolDuration,
last_session_duration: projectConfig.lastDuration,
last_session_lines_added: projectConfig.lastLinesAdded,
last_session_lines_removed: projectConfig.lastLinesRemoved,
last_session_total_input_tokens: projectConfig.lastTotalInputTokens,
last_session_total_output_tokens: projectConfig.lastTotalOutputTokens,
last_session_total_cache_creation_input_tokens:
projectConfig.lastTotalCacheCreationInputTokens,
last_session_total_cache_read_input_tokens:
projectConfig.lastTotalCacheReadInputTokens,
last_session_fps_average: projectConfig.lastFpsAverage,
last_session_fps_low_1_pct: projectConfig.lastFpsLow1Pct,
last_session_id:
projectConfig.lastSessionId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
...projectConfig.lastSessionMetrics,
})
// Note: We intentionally don't clear these values after logging.
// They're needed for cost restoration when resuming sessions.
// The values will be overwritten when the next session exits.
}
}
|