MCPcopy
hub / github.com/claude-code-best/claude-code / TmuxEngine

Class TmuxEngine

src/cli/bg/engines/tmux.ts:11–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9} from '../engine.js'
10
11export class TmuxEngine implements BgEngine {
12 readonly name = 'tmux' as const
13 readonly supportsInteractiveInput = true
14
15 async available(): Promise<boolean> {
16 const { code } = await execFileNoThrow('tmux', ['-V'], { useCwd: false })
17 return code === 0
18 }
19
20 async start(opts: BgStartOptions): Promise<BgStartResult> {
21 const launch = buildCliLaunch(opts.args, {
22 env: {
23 ...opts.env,
24 CLAUDE_CODE_SESSION_KIND: 'bg',
25 CLAUDE_CODE_SESSION_NAME: opts.sessionName,
26 CLAUDE_CODE_SESSION_LOG: opts.logPath,
27 CLAUDE_CODE_TMUX_SESSION: opts.sessionName,
28 } as NodeJS.ProcessEnv,
29 })
30
31 const cmd = quoteCliLaunch(launch)
32
33 const result = spawnSync(
34 'tmux',
35 ['new-session', '-d', '-s', opts.sessionName, cmd],
36 { stdio: 'inherit', env: launch.env },
37 )
38
39 if (result.status !== 0) {
40 throw new Error('Failed to create tmux session.')
41 }
42
43 // tmux doesn't directly report the child PID; we return 0.
44 // The actual session process writes its own PID file.
45 return {
46 pid: 0,
47 sessionName: opts.sessionName,
48 logPath: opts.logPath,
49 engineUsed: 'tmux',
50 }
51 }
52
53 async attach(session: SessionEntry): Promise<void> {
54 if (!session.tmuxSessionName) {
55 throw new Error(`Session ${session.sessionId} has no tmux session name.`)
56 }
57
58 const result = spawnSync(
59 'tmux',
60 ['attach-session', '-t', session.tmuxSessionName],
61 { stdio: 'inherit' },
62 )
63
64 if (result.status !== 0) {
65 throw new Error(
66 `Failed to attach to tmux session '${session.tmuxSessionName}'.`,
67 )
68 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected