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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import { createHash } from 'node:crypto'
import { createReadStream } from 'node:fs'
import {
  chmod,
  copyFile,
  mkdir,
  readFile,
  rename,
  stat,
  unlink,
  writeFile,
} from 'node:fs/promises'
import { homedir } from 'node:os'
import { delimiter, dirname, join, resolve } from 'node:path'

import { resolveClaudeCliLauncher } from '../../utils/desktopBundledCli.js'
import { execFileNoThrow } from '../../utils/execFileNoThrow.js'
import { getShellConfigPaths } from '../../utils/shellConfig.js'
import { getUserBinDir } from '../../utils/xdg.js'

const DESKTOP_CLI_NAME = 'claude-haha'
const PATH_BLOCK_START = '# >>> Versper AI Claw PATH >>>'
const PATH_BLOCK_END = '# <<< Versper AI Claw PATH <<<'
const WINDOWS_PATH_TARGET = 'Windows User PATH'
const WINDOWS_USER_BIN_EXPR = '%USERPROFILE%\\.local\\bin'

export type DesktopCliLauncherStatus = {
  supported: boolean
  command: string
  installed: boolean
  launcherPath: string
  binDir: string
  pathConfigured: boolean
  pathInCurrentShell: boolean
  availableInNewTerminals: boolean
  needsTerminalRestart: boolean
  configTarget: string | null
  lastError: string | null
}

let inFlightEnsure: Promise<DesktopCliLauncherStatus> | null = null

export function getDesktopCliCommandName(
  platform: NodeJS.Platform = process.platform,
) {
  return platform === 'win32' ? `${DESKTOP_CLI_NAME}.exe` : DESKTOP_CLI_NAME
}

export function resolveHomeDir(env: NodeJS.ProcessEnv = process.env) {
  return env.HOME || env.USERPROFILE || homedir()
}

export function isPathEntryPresent(
  pathValue: string | undefined,
  targetDir: string,
  platform: NodeJS.Platform = process.platform,
  homeDir: string = resolveHomeDir(),
) {
  if (!pathValue) return false

  if (platform === 'win32') {
    const normalizedTarget = normalizeWindowsPathEntry(targetDir, homeDir)
    return pathValue
      .split(';')
      .map((entry) => normalizeWindowsPathEntry(entry, homeDir))
      .some((entry) => entry === normalizedTarget)
  }

  const normalizedTarget = resolve(targetDir)
  return pathValue
    .split(delimiter)
    .map((entry) => entry.trim())
    .filter(Boolean)
    .some((entry) => {
      try {
        return resolve(entry) === normalizedTarget
      } catch {
        return false
      }
    })
}

export function upsertManagedPathBlock(
  existingContent: string,
  block: string,
): string {
  const escapedStart = PATH_BLOCK_START.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  const escapedEnd = PATH_BLOCK_END.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  const pattern = new RegExp(`${escapedStart}[\\s\\S]*?${escapedEnd}\\n?`, 'm')
  const nextBlock = `${block.trimEnd()}\n`

  if (pattern.test(existingContent)) {
    return existingContent.replace(pattern, nextBlock)
  }

  const trimmed = existingContent.trimEnd()
  if (!trimmed) {
    return nextBlock
  }

  return `${trimmed}\n\n${nextBlock}`
}

export function buildManagedPathBlock(
  shellType: 'zsh' | 'bash' | 'fish',
  binDir: string,
  homeDir: string = resolveHomeDir(),
) {
  const defaultBinDir = join(homeDir, '.local', 'bin')
  const pathExpr = resolve(binDir) === resolve(defaultBinDir) ? '$HOME/.local/bin' : binDir

  if (shellType === 'fish') {
    return [
      PATH_BLOCK_START,
      `if not contains "${pathExpr}" $PATH`,
      `  set -gx PATH "${pathExpr}" $PATH`,
      'end',
      PATH_BLOCK_END,
    ].join('\n')
  }

  return [
    PATH_BLOCK_START,
    `export PATH="${pathExpr}:$PATH"`,
    PATH_BLOCK_END,
  ].join('\n')
}

export async function ensureDesktopCliLauncherInstalled(): Promise<DesktopCliLauncherStatus> {
  if (inFlightEnsure) {
    return inFlightEnsure
  }

  const promise = ensureDesktopCliLauncherInstalledImpl()
  inFlightEnsure = promise

  try {
    return await promise
  } finally {
    if (inFlightEnsure === promise) {
      inFlightEnsure = null
    }
  }
}

async function ensureDesktopCliLauncherInstalledImpl(): Promise<DesktopCliLauncherStatus> {
  const homeDir = resolveHomeDir()
  const binDir = getUserBinDir({ homedir: homeDir })
  const launcherPath = join(binDir, getDesktopCliCommandName())
  const sourcePath = resolveBundledSidecarSourcePath()

  if (!sourcePath) {
    return buildStatus({
      supported: false,
      launcherPath,
      binDir,
      command: DESKTOP_CLI_NAME,
      installed: false,
      pathConfigured: false,
      pathInCurrentShell: isPathEntryPresent(process.env.PATH, binDir),
      configTarget: null,
      lastError: null,
    })
  }

  let lastError: string | null = null

  try {
    await syncLauncher(sourcePath, launcherPath)
  } catch (error) {
    lastError = error instanceof Error ? error.message : String(error)
  }

  const installed = await isUsableLauncher(launcherPath)
  const currentPathReady = isPathEntryPresent(process.env.PATH, binDir)

  let pathConfigured = currentPathReady
  let configTarget: string | null = null

  try {
    if (process.platform === 'win32') {
      const windowsResult = await ensureWindowsUserPathConfigured(binDir, homeDir)
      pathConfigured = currentPathReady || windowsResult.pathConfigured
      configTarget = windowsResult.configTarget
      lastError ||= windowsResult.lastError
    } else {
      const unixResult = await ensureUnixShellPathConfigured(binDir, homeDir)
      pathConfigured = currentPathReady || unixResult.pathConfigured
      configTarget = unixResult.configTarget
      lastError ||= unixResult.lastError
    }
  } catch (error) {
    lastError ||= error instanceof Error ? error.message : String(error)
  }

  return buildStatus({
    supported: true,
    command: DESKTOP_CLI_NAME,
    launcherPath,
    binDir,
    installed,
    pathConfigured,
    pathInCurrentShell: currentPathReady,
    configTarget,
    lastError,
  })
}

function buildStatus(
  input: Omit<
    DesktopCliLauncherStatus,
    'availableInNewTerminals' | 'needsTerminalRestart'
  >,
): DesktopCliLauncherStatus {
  const availableInNewTerminals =
    input.installed && (input.pathInCurrentShell || input.pathConfigured)

  return {
    ...input,
    availableInNewTerminals,
    needsTerminalRestart:
      availableInNewTerminals && !input.pathInCurrentShell,
  }
}

function resolveBundledSidecarSourcePath(): string | null {
  const launcher = resolveClaudeCliLauncher({
    cliPath: process.env.CLAUDE_CLI_PATH,
    execPath: process.execPath,
  })

  if (!launcher || launcher.kind !== 'sidecar') {
    return null
  }

  return launcher.command
}

async function syncLauncher(sourcePath: string, targetPath: string) {
  await mkdir(dirname(targetPath), { recursive: true })

  if (process.platform !== 'win32') {
    await syncUnixLauncherWrapper(sourcePath, targetPath)
    return
  }

  if (await filesMatch(sourcePath, targetPath)) {
    return
  }

  const tempPath = `${targetPath}.tmp.${process.pid}.${Date.now()}`
  await copyFile(sourcePath, tempPath)

  if (process.platform !== 'win32') {
    await chmod(tempPath, 0o755)
  }

  try {
    if (process.platform === 'win32') {
      await replaceWindowsBinary(tempPath, targetPath)
    } else {
      await rename(tempPath, targetPath)
      await chmod(targetPath, 0o755)
    }
  } finally {
    await unlink(tempPath).catch(() => undefined)
  }
}

async function syncUnixLauncherWrapper(sourcePath: string, targetPath: string) {
  const wrapper = buildUnixLauncherWrapper(sourcePath)

  const existing = await readFile(targetPath, 'utf8').catch(() => null)
  if (existing === wrapper) {
    await chmod(targetPath, 0o755)
    return
  }

  const tempPath = `${targetPath}.tmp.${process.pid}.${Date.now()}`
  await writeFile(tempPath, wrapper, { encoding: 'utf8', mode: 0o755 })

  try {
    await rename(tempPath, targetPath)
    await chmod(targetPath, 0o755)
  } finally {
    await unlink(tempPath).catch(() => undefined)
  }
}

function buildUnixLauncherWrapper(sourcePath: string) {
  const quotedSource = shellSingleQuote(sourcePath)
  const quotedAppRoot = shellSingleQuote(dirname(sourcePath))

  return `#!/usr/bin/env bash
set -euo pipefail

SIDECAR=${quotedSource}
APP_ROOT=${quotedAppRoot}

if [[ ! -x "$SIDECAR" ]]; then
  echo "claude-haha launcher could not find bundled sidecar: $SIDECAR" >&2
  exit 127
fi

# Bun-compiled macOS sidecars can be suspended by job control while reading the
# controlling TTY directly. A nested PTY keeps the terminal foreground handoff
# stable for the interactive TUI; non-interactive commands keep direct exec
# semantics and exit codes.
if [[ "$(uname -s)" == "Darwin" && -t 0 && -t 1 && -x /usr/bin/script ]]; then
  exec /usr/bin/script -q /dev/null "$SIDECAR" cli --app-root "$APP_ROOT" "$@"
fi

exec "$SIDECAR" cli --app-root "$APP_ROOT" "$@"
`
}

function shellSingleQuote(value: string) {
  return `'${value.replace(/'/g, `'\\''`)}'`
}

async function replaceWindowsBinary(tempPath: string, targetPath: string) {
  try {
    await unlink(targetPath)
  } catch {
    // noop
  }

  try {
    await rename(tempPath, targetPath)
    return
  } catch {
    // The existing executable may still be in use. Rename it away and retry.
  }

  const backupPath = `${targetPath}.old.${Date.now()}`
  try {
    await rename(targetPath, backupPath)
  } catch {
    // noop
  }

  await rename(tempPath, targetPath)
  await unlink(backupPath).catch(() => undefined)
}

async function filesMatch(sourcePath: string, targetPath: string) {
  try {
    const [sourceStats, targetStats] = await Promise.all([
      stat(sourcePath),
      stat(targetPath),
    ])

    if (!sourceStats.isFile() || !targetStats.isFile()) {
      return false
    }

    if (sourceStats.size !== targetStats.size) {
      return false
    }

    const [sourceHash, targetHash] = await Promise.all([
      hashFile(sourcePath),
      hashFile(targetPath),
    ])
    return sourceHash === targetHash
  } catch {
    return false
  }
}

async function hashFile(filePath: string) {
  return await new Promise<string>((resolvePromise, reject) => {
    const hash = createHash('sha256')
    const stream = createReadStream(filePath)
    stream.on('data', (chunk) => hash.update(chunk))
    stream.on('error', reject)
    stream.on('end', () => resolvePromise(hash.digest('hex')))
  })
}

async function isUsableLauncher(filePath: string) {
  try {
    const fileStats = await stat(filePath)
    return fileStats.isFile() && fileStats.size > 0
  } catch {
    return false
  }
}

async function ensureUnixShellPathConfigured(
  binDir: string,
  homeDir: string,
): Promise<{
  pathConfigured: boolean
  configTarget: string | null
  lastError: string | null
}> {
  const shellType = resolveShellType()
  const configPaths = getShellConfigPaths({ env: process.env, homedir: homeDir })
  const configTarget =
    configPaths[shellType] ??
    (process.platform === 'darwin' ? configPaths.zsh : configPaths.bash)

  if (!configTarget) {
    return {
      pathConfigured: false,
      configTarget: null,
      lastError: 'Could not resolve a shell config file for PATH setup',
    }
  }

  const block = buildManagedPathBlock(shellType, binDir, homeDir)
  const existingContent = await readFile(configTarget, 'utf8').catch(() => '')
  const nextContent = upsertManagedPathBlock(existingContent, block)

  if (nextContent !== existingContent) {
    await mkdir(dirname(configTarget), { recursive: true })
    await writeFile(configTarget, nextContent, 'utf8')
  }

  return {
    pathConfigured: true,
    configTarget,
    lastError: null,
  }
}

async function ensureWindowsUserPathConfigured(
  binDir: string,
  homeDir: string,
): Promise<{
  pathConfigured: boolean
  configTarget: string
  lastError: string | null
}> {
  const userPath = await readWindowsUserPath()
  if (isPathEntryPresent(userPath, binDir, 'win32', homeDir)) {
    return {
      pathConfigured: true,
      configTarget: WINDOWS_PATH_TARGET,
      lastError: null,
    }
  }

  const script = [
    `$bin = [Environment]::ExpandEnvironmentVariables('${WINDOWS_USER_BIN_EXPR}')`,
    `$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')`,
    `$segments = @()`,
    `if ($userPath) { $segments = $userPath.Split(';') | Where-Object { $_ -and $_.Trim() -ne '' } }`,
    `$normalized = $segments | ForEach-Object { [Environment]::ExpandEnvironmentVariables($_).TrimEnd('\\').ToLowerInvariant() }`,
    `if (-not ($normalized -contains $bin.TrimEnd('\\').ToLowerInvariant())) {`,
    `  $newPath = if ([string]::IsNullOrWhiteSpace($userPath)) { '${WINDOWS_USER_BIN_EXPR}' } else { '${WINDOWS_USER_BIN_EXPR};' + $userPath }`,
    `  [Environment]::SetEnvironmentVariable('Path', $newPath, 'User')`,
    `  $signature = @'`,
    `using System;`,
    `using System.Runtime.InteropServices;`,
    `public static class NativeMethods {`,
    `  [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]`,
    `  public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, out IntPtr lpdwResult);`,
    `}`,
    `'@`,
    `  Add-Type -TypeDefinition $signature -ErrorAction SilentlyContinue | Out-Null`,
    `  $HWND_BROADCAST = [IntPtr]0xffff`,
    `  $WM_SETTINGCHANGE = 0x1A`,
    `  $SMTO_ABORTIFHUNG = 0x2`,
    `  $result = [IntPtr]::Zero`,
    `  [void][NativeMethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [IntPtr]::Zero, 'Environment', $SMTO_ABORTIFHUNG, 5000, [ref]$result)`,
    `}`,
  ].join('\n')

  const result = await execFileNoThrow(
    'powershell.exe',
    ['-NoProfile', '-NonInteractive', '-Command', script],
    { useCwd: false },
  )

  if (result.code !== 0) {
    return {
      pathConfigured: false,
      configTarget: WINDOWS_PATH_TARGET,
      lastError:
        result.stderr.trim() ||
        result.stdout.trim() ||
        result.error ||
        'Failed to update Windows user PATH',
    }
  }

  return {
    pathConfigured: true,
    configTarget: WINDOWS_PATH_TARGET,
    lastError: null,
  }
}

function resolveShellType(): 'zsh' | 'bash' | 'fish' {
  const shellPath = process.env.SHELL || ''
  if (shellPath.includes('fish')) return 'fish'
  if (shellPath.includes('bash')) return 'bash'
  if (shellPath.includes('zsh')) return 'zsh'
  return process.platform === 'darwin' ? 'zsh' : 'bash'
}

async function readWindowsUserPath() {
  const result = await execFileNoThrow(
    'powershell.exe',
    [
      '-NoProfile',
      '-NonInteractive',
      '-Command',
      `[Environment]::GetEnvironmentVariable('Path', 'User')`,
    ],
    { useCwd: false },
  )

  if (result.code !== 0) {
    return ''
  }

  return result.stdout.trim()
}

function normalizeWindowsPathEntry(entry: string, homeDir: string) {
  return entry
    .trim()
    .replace(/^"+|"+$/g, '')
    .replace(/%USERPROFILE%/gi, homeDir)
    .replace(/%HOMEDRIVE%%HOMEPATH%/gi, homeDir)
    .replace(/\//g, '\\')
    .replace(/\\+$/, '')
    .toLowerCase()
}