Class: ClaudeAgentSDK::SubprocessCLITransport

Inherits:
Transport
  • Object
show all
Defined in:
lib/claude_agent_sdk/subprocess_cli_transport.rb

Overview

Subprocess transport using Claude Code CLI

Constant Summary collapse

DEFAULT_MAX_BUFFER_SIZE =

1MB buffer limit

1024 * 1024
MINIMUM_CLAUDE_CODE_VERSION =
'2.0.0'
RECENT_STDERR_LINES_LIMIT =
20

Instance Method Summary collapse

Constructor Details

#initialize(options_or_prompt = nil, options = nil) ⇒ SubprocessCLITransport

Returns a new instance of SubprocessCLITransport.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 18

def initialize(options_or_prompt = nil, options = nil)
  # Support both new single-arg form and legacy two-arg form
  @options = options.nil? ? options_or_prompt : options
  @cli_path = @options.cli_path || find_cli
  @cwd = @options.cwd
  @process = nil
  @stdin = nil
  @stdout = nil
  @stderr = nil
  @ready = false
  @exit_error = nil
  @max_buffer_size = @options.max_buffer_size || DEFAULT_MAX_BUFFER_SIZE
  @stderr_task = nil
  @recent_stderr = []
  @recent_stderr_mutex = Mutex.new
end

Instance Method Details

#build_commandObject



70
71
72
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 70

def build_command
  CommandBuilder.new(@cli_path, @options).build
end

#check_claude_versionObject



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 367

def check_claude_version
  begin
    stdout, stderr, = Open3.capture3(@cli_path.to_s, '-v')
    output = (stdout.to_s + stderr.to_s).strip
    if match = output.match(/([0-9]+\.[0-9]+\.[0-9]+)/)
      version = match[1]
      version_parts = version.split('.').map(&:to_i)
      min_parts = MINIMUM_CLAUDE_CODE_VERSION.split('.').map(&:to_i)

      if version_parts < min_parts
        warning = "Warning: Claude Code version #{version} is unsupported in the Agent SDK. " \
                  "Minimum required version is #{MINIMUM_CLAUDE_CODE_VERSION}. " \
                  "Some features may not work correctly."
        warn warning
      end
    end
  rescue StandardError
    # Ignore version check errors
  end
end

#closeObject



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
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 178

def close
  @ready = false
  return unless @process

  cleanup_errors = []

  # Kill stderr thread
  if @stderr_task&.alive?
    begin
      @stderr_task.kill
      @stderr_task.join(1)
    rescue StandardError => e
      cleanup_errors << "stderr thread: #{e.message}"
    end
  end

  # Close streams
  begin
    @stdin&.close
  rescue IOError
    # Already closed, ignore
  rescue StandardError => e
    cleanup_errors << "stdin: #{e.message}"
  end

  begin
    @stdout&.close
  rescue IOError
    # Already closed, ignore
  rescue StandardError => e
    cleanup_errors << "stdout: #{e.message}"
  end

  begin
    @stderr&.close
  rescue IOError
    # Already closed, ignore
  rescue StandardError => e
    cleanup_errors << "stderr: #{e.message}"
  end

  # Wait for graceful shutdown after stdin EOF, then terminate if needed.
  # The subprocess needs time to flush its session file after receiving
  # EOF on stdin. Without this grace period, SIGTERM can interrupt the
  # write and cause the last assistant message to be lost.
  begin
    wait_process_with_timeout(5) if @process.alive?
  rescue Timeout::Error
    # Graceful shutdown timed out — send SIGTERM
    begin
      Process.kill('TERM', @process.pid)
      wait_process_with_timeout(2)
    rescue Timeout::Error
      # SIGTERM didn't work — force kill
      begin
        Process.kill('KILL', @process.pid)
        @process.value
      rescue StandardError => e
        cleanup_errors << "force kill: #{e.message}"
      end
    rescue Errno::ESRCH
      # Process already dead
    end
  rescue Errno::ESRCH
    # Process already dead, ignore
  rescue StandardError => e
    cleanup_errors << "process termination: #{e.message}"
  end

  # Log any cleanup errors (non-fatal)
  if cleanup_errors.any?
    warn "Claude SDK: Cleanup warnings: #{cleanup_errors.join(', ')}"
  end

  @process = nil
  @stdout = nil
  @stdin = nil
  @stderr = nil
  @stderr_task = nil
  @exit_error = nil
end

#connectObject



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
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 74

def connect
  return if @process

  check_claude_version

  cmd = build_command

  # Build environment
  # Convert symbol keys to strings for spawn compatibility
  custom_env = @options.env.transform_keys { |k| k.to_s }
  # Explicitly unset CLAUDECODE to prevent "nested session" detection when the SDK
  # launches Claude Code from within an existing Claude Code terminal.
  # NOTE: Must set to nil (not just omit the key) — Ruby's spawn only overlays
  # the env hash on top of the parent environment; a nil value actively unsets.
  process_env = ENV.to_h.merge('CLAUDECODE' => nil, 'CLAUDE_AGENT_SDK_VERSION' => VERSION).merge(custom_env)
  process_env['CLAUDE_CODE_ENTRYPOINT'] ||= 'sdk-rb'
  process_env['CLAUDE_CODE_ENABLE_SDK_FILE_CHECKPOINTING'] = 'true' if @options.enable_file_checkpointing
  process_env['PWD'] = @cwd.to_s if @cwd

  # Determine stderr handling
  should_pipe_stderr = @options.stderr || @options.debug_stderr || @options.extra_args.key?('debug-to-stderr')

  begin
    # Start process using Open3
    opts = { chdir: @cwd&.to_s }.compact

    @stdin, @stdout, @stderr, @process = Open3.popen3(process_env, *cmd, opts)

    # Always drain stderr to prevent pipe buffer deadlock.
    # Without this, --verbose output fills the OS pipe buffer (~64KB),
    # the subprocess blocks on write, and all pipes stall → EPIPE.
    if @stderr
      if should_pipe_stderr # rubocop:disable Style/ConditionalAssignment
        @stderr_task = Thread.new do
          handle_stderr
        rescue StandardError
          # Ignore errors during stderr reading
        end
      else
        # Silently drain stderr so the subprocess never blocks,
        # but still accumulate recent lines for error reporting.
        @stderr_task = Thread.new do
          drain_stderr_with_accumulation
        rescue StandardError
          # Ignore — process may have already exited
        end
      end
    end

    # Always keep stdin open — streaming mode uses it for the control protocol
    @ready = true
  rescue Errno::ENOENT => e
    # Check if error is from cwd or CLI
    if @cwd && !File.directory?(@cwd.to_s)
      error = CLIConnectionError.new("Working directory does not exist: #{@cwd}")
      @exit_error = error
      raise error
    end
    error = CLINotFoundError.new("Claude Code not found at: #{@cli_path}")
    @exit_error = error
    raise error
  rescue StandardError => e
    error = CLIConnectionError.new("Failed to start Claude Code: #{e}")
    @exit_error = error
    raise error
  end
end

#drain_stderr_with_accumulationObject



167
168
169
170
171
172
173
174
175
176
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 167

def drain_stderr_with_accumulation
  return unless @stderr

  @stderr.each_line do |line|
    line_str = line.chomp
    next if line_str.empty?

    record_bounded_stderr(line_str)
  end
end

#end_inputObject



291
292
293
294
295
296
297
298
299
300
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 291

def end_input
  return unless @stdin

  begin
    @stdin.close
  rescue StandardError
    # Ignore
  end
  @stdin = nil
end

#find_cliObject

Raises:



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
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 35

def find_cli
  # Try which command first (using Open3 for thread safety)
  cli = nil
  begin
    stdout, _status = Open3.capture2('which', 'claude')
    cli = stdout.strip
  rescue StandardError
    # which command failed, try common locations
  end
  return cli if cli && !cli.empty? && File.executable?(cli)

  # Try common locations
  locations = [
    File.join(Dir.home, '.claude/local/claude'),  # Claude Code default install location
    File.join(Dir.home, '.npm-global/bin/claude'),
    '/usr/local/bin/claude',
    File.join(Dir.home, '.local/bin/claude'),
    File.join(Dir.home, 'node_modules/.bin/claude'),
    File.join(Dir.home, '.yarn/bin/claude')
  ]

  locations.each do |path|
    return path if File.exist?(path) && File.file?(path)
  end

  raise CLINotFoundError.new(
    "Claude Code not found. Install with:\n" \
    "  npm install -g @anthropic-ai/claude-code\n" \
    "\nIf already installed locally, try:\n" \
    '  export PATH="$HOME/node_modules/.bin:$PATH"' \
    "\n\nOr provide the path via ClaudeAgentOptions:\n" \
    "  ClaudeAgentOptions.new(cli_path: '/path/to/claude')"
  )
end

#handle_stderrObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 142

def handle_stderr
  return unless @stderr

  @stderr.each_line do |line|
    line_str = line.chomp
    next if line_str.empty?

    record_bounded_stderr(line_str)

    # Call stderr callback if provided
    @options.stderr&.call(line_str)

    # Write to debug_stderr file/IO if provided
    if @options.debug_stderr
      if @options.debug_stderr.respond_to?(:puts)
        @options.debug_stderr.puts(line_str)
      elsif @options.debug_stderr.is_a?(String)
        File.open(@options.debug_stderr, 'a') { |f| f.puts(line_str) }
      end
    end
  end
rescue StandardError
  # Ignore errors during stderr reading
end

#read_messages(&block) ⇒ Object

Raises:



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
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 302

def read_messages(&block)
  return enum_for(:read_messages) unless block_given?

  raise CLIConnectionError, 'Not connected' unless @process && @stdout

  json_buffer = ''

  begin
    @stdout.each_line do |line|
      line_str = line.strip
      next if line_str.empty?

      json_lines = line_str.split("\n")

      json_lines.each do |json_line|
        json_line = json_line.strip
        next if json_line.empty?

        json_buffer += json_line

        if json_buffer.bytesize > @max_buffer_size
          buffer_length = json_buffer.bytesize
          json_buffer = ''
          raise CLIJSONDecodeError.new(
            "JSON message exceeded maximum buffer size",
            StandardError.new("Buffer size #{buffer_length} exceeds limit #{@max_buffer_size}")
          )
        end

        begin
          data = JSON.parse(json_buffer, symbolize_names: true)
          json_buffer = ''
          yield data
        rescue JSON::ParserError
          # Continue accumulating
          next
        end
      end
    end
  rescue IOError
    # Stream closed
  rescue StopIteration
    # Client disconnected
  end

  # Check process completion
  status = @process.value
  returncode = status.exitstatus

  if returncode && returncode != 0
    # Wait briefly for stderr thread to finish draining
    @stderr_task&.join(1)

    stderr_text = @recent_stderr_mutex.synchronize { @recent_stderr.last(10).join("\n") }
    stderr_text = 'No stderr output captured' if stderr_text.empty?

    @exit_error = ProcessError.new(
      "Command failed with exit code #{returncode}",
      exit_code: returncode,
      stderr: stderr_text
    )
    raise @exit_error
  end
end

#ready?Boolean

Returns:

  • (Boolean)


388
389
390
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 388

def ready?
  @ready
end

#wait_process_with_timeout(timeout_seconds) ⇒ Object

Wait for the spawned process to exit, up to timeout_seconds. Polls @process.alive? rather than using stdlib Timeout.timeout, which raises across threads via Thread#raise and corrupts Async fiber-scheduler state (close is always called inside an Async task). Yields to the current Async task when one is active so the reactor keeps running.



265
266
267
268
269
270
271
272
273
274
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 265

def wait_process_with_timeout(timeout_seconds)
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout_seconds
  task = defined?(Async::Task) ? Async::Task.current? : nil
  while @process.alive?
    raise Timeout::Error if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline

    task ? task.sleep(0.05) : sleep(0.05)
  end
  @process.value
end

#write(data) ⇒ Object

Raises:



276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 276

def write(data)
  raise CLIConnectionError, 'ProcessTransport is not ready for writing' unless @ready && @stdin
  raise CLIConnectionError, "Cannot write to terminated process" if @process && !@process.alive?
  raise CLIConnectionError, "Cannot write to process that exited with error: #{@exit_error}" if @exit_error

  begin
    @stdin.write(data)
    @stdin.flush
  rescue StandardError => e
    @ready = false
    @exit_error = CLIConnectionError.new("Failed to write to process stdin: #{e}")
    raise @exit_error
  end
end