Class: ClaudeAgentSDK::SubprocessCLITransport
- 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- ACTIVE_PROCESSES =
Track live CLI subprocesses so we can terminate them when the parent Ruby process exits. Mirrors the Python (PR #916, a ‘set`) and TypeScript SDKs’ parent-exit cleanup, preventing orphaned ‘claude` processes from leaking when callers crash or exit before reaching #close. A Set keyed by object identity (like Python’s set) keeps the hot path off ‘#pid` — only #kill_active_processes touches `#pid`/`#alive?`, at exit. Guarded by a mutex because #close can run on a FiberBoundary worker thread while #connect runs on the reactor fiber. Stored in CONSTANTS (not class instance variables) so the registry is a single shared instance across this class and any subclass: constants resolve through the ancestor chain, whereas class ivars are NOT inherited — a `SubprocessCLITransport` subclass instance calling `self.class.register_active_process` would otherwise reach a nil mutex and raise mid-#connect, orphaning the just-spawned child. The base-class at_exit handler must be able to see every subprocess, a subclass’s too.
Set.new
- ACTIVE_PROCESSES_MUTEX =
Mutex.new
Class Method Summary collapse
-
.active_processes ⇒ Object
Public readers (the test suite uses ‘described_class.active_processes`); they return the shared constants so subclasses observe the same objects.
- .active_processes_mutex ⇒ Object
- .deregister_active_process(wait_thr) ⇒ Object
-
.kill_active_processes ⇒ Object
Best-effort SIGTERM to every still-running child.
-
.register_active_process(wait_thr) ⇒ Object
wait_thris the Process::Waiter returned by Open3.popen3.
Instance Method Summary collapse
- #build_command ⇒ Object
- #check_claude_version ⇒ Object
- #close ⇒ Object
- #connect ⇒ Object
- #drain_stderr_with_accumulation ⇒ Object
- #end_input ⇒ Object
- #find_cli ⇒ Object
- #handle_stderr ⇒ Object
-
#initialize(options_or_prompt = nil, options = nil) ⇒ SubprocessCLITransport
constructor
A new instance of SubprocessCLITransport.
- #read_messages(&block) ⇒ Object
- #ready? ⇒ Boolean
-
#wait_process_with_timeout(timeout_seconds) ⇒ Object
Wait for the spawned process to exit, up to
timeout_seconds. - #write(data) ⇒ Object
Constructor Details
#initialize(options_or_prompt = nil, options = nil) ⇒ SubprocessCLITransport
Returns a new instance of SubprocessCLITransport.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 88 def initialize( = nil, = nil) # Support both new single-arg form and legacy two-arg form @options = .nil? ? : @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 # Serializes stdin access across the reactor fiber (transport writes # from inside Async) and user-callback threads spawned via FiberBoundary # (tool handlers / hooks calling Client#query). Without this lock, # close can nil @stdin between write's readiness check and the actual # @stdin.write call, producing NoMethodError on nil. @stdin_mutex = Mutex.new end |
Class Method Details
.active_processes ⇒ Object
Public readers (the test suite uses ‘described_class.active_processes`); they return the shared constants so subclasses observe the same objects.
40 41 42 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 40 def active_processes ACTIVE_PROCESSES end |
.active_processes_mutex ⇒ Object
44 45 46 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 44 def active_processes_mutex ACTIVE_PROCESSES_MUTEX end |
.deregister_active_process(wait_thr) ⇒ Object
55 56 57 58 59 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 55 def deregister_active_process(wait_thr) return unless wait_thr active_processes_mutex.synchronize { active_processes.delete(wait_thr) } end |
.kill_active_processes ⇒ Object
Best-effort SIGTERM to every still-running child. Registered with at_exit at the bottom of this file. Never reaps (a blocking wait could hang interpreter shutdown) — the OS reparents and reaps orphans.
Deliberately does NOT take active_processes_mutex: at interpreter shutdown Ruby runs at_exit handlers before terminating other threads, and Mutex is unfair, so blocking here while a still-live worker churns register/deregister can starve this handler and hang the process. A lock-free read is safe — a torn snapshot at worst misses or repeats a SIGTERM, both harmless. The outer rescue guarantees the handler never raises (e.g. ThreadError if reached from a trap context, or a concurrent-modification error from the unlocked read), honoring the “never interrupt interpreter shutdown” contract.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 74 def kill_active_processes active_processes.to_a.each do |wait_thr| next unless wait_thr.alive? Process.kill('TERM', wait_thr.pid) rescue StandardError # Process already gone (Errno::ESRCH), not permitted, or invalid pid. end active_processes.clear rescue StandardError # Never let cleanup interfere with interpreter shutdown. end |
.register_active_process(wait_thr) ⇒ Object
wait_thr is the Process::Waiter returned by Open3.popen3.
49 50 51 52 53 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 49 def register_active_process(wait_thr) return unless wait_thr active_processes_mutex.synchronize { active_processes.add(wait_thr) } end |
Instance Method Details
#build_command ⇒ Object
146 147 148 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 146 def build_command CommandBuilder.new(@cli_path, @options).build end |
#check_claude_version ⇒ Object
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 501 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 |
#close ⇒ Object
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 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 268 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.}" end end # Close stdin under the same lock that guards write — otherwise a # concurrent writer (callbacks running on FiberBoundary threads) can # see @stdin nilled mid-write and hit NoMethodError on nil. @stdin_mutex.synchronize do begin @stdin&.close rescue IOError # Already closed, ignore rescue StandardError => e cleanup_errors << "stdin: #{e.}" end @stdin = nil end begin @stdout&.close rescue IOError # Already closed, ignore rescue StandardError => e cleanup_errors << "stdout: #{e.}" end begin @stderr&.close rescue IOError # Already closed, ignore rescue StandardError => e cleanup_errors << "stderr: #{e.}" 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.}" end rescue Errno::ESRCH # Process already dead end rescue Errno::ESRCH # Process already dead, ignore rescue StandardError => e cleanup_errors << "process termination: #{e.}" end # Log any cleanup errors (non-fatal) if cleanup_errors.any? warn "Claude SDK: Cleanup warnings: #{cleanup_errors.join(', ')}" end self.class.deregister_active_process(@process) @process = nil @stdout = nil # @stdin already nilled under the mutex above. @stderr = nil @stderr_task = nil @exit_error = nil end |
#connect ⇒ Object
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 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 150 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) self.class.register_active_process(@process) # 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_accumulation ⇒ Object
257 258 259 260 261 262 263 264 265 266 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 257 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_input ⇒ Object
402 403 404 405 406 407 408 409 410 411 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 402 def end_input return unless @stdin begin @stdin.close rescue StandardError # Ignore end @stdin = nil end |
#find_cli ⇒ Object
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 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 111 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_stderr ⇒ Object
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 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 219 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) # Per-line isolation: a callback that raises (e.g. user's logger # transiently failing) must not poison the rest of the stderr stream. # Without this, the first exception terminates the each_line loop and # the SDK silently stops capturing stderr for the lifetime of the # process. Matches Python SDK v0.2.82 (PR #932). begin @options.stderr&.call(line_str) rescue StandardError # Drop the callback error; the line is already in the recent-stderr # ring buffer, which is what ProcessError surfaces on non-zero exit. end # Write to debug_stderr file/IO if provided, also isolated. begin 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 rescue StandardError # Drop debug_stderr write errors so they never interrupt the loop. end end rescue StandardError # Stream-level error (pipe closed mid-read); the loop naturally ends here. end |
#read_messages(&block) ⇒ Object
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 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 413 def (&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? # When no partial JSON is buffered, the next line must start with # `{` to be a valid stream-json message. Stray stderr-like text # (e.g., debug warnings the CLI occasionally writes to stdout) # would otherwise be appended into json_buffer, poisoning every # subsequent parse until the buffer overflows. Matches the Python # SDK's `if not json_buffer and not json_line.startswith("{")` guard. next if json_buffer.empty? && !json_line.start_with?('{') 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. @process may already be nil (close() ran # concurrently and reset it) or already waited on (Errno::ECHILD on # double-wait). Both are non-fatal — the message loop just exits. returncode = nil begin status = @process&.value returncode = status&.exitstatus rescue Errno::ECHILD # Process was already reaped (e.g., by close()); no exit status to surface. returncode = nil end # The child has exited and been reaped; drop it from the parent-exit # registry now rather than waiting for #close, which a caller may never # reach (e.g. a Client abandoned without #disconnect, or direct transport # use). Idempotent — #close's own deregister becomes a harmless no-op, and # #close still sees @process (left set here) for its termination logic. self.class.deregister_active_process(@process) 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
522 523 524 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 522 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.
361 362 363 364 365 366 367 368 369 370 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 361 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
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 |
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 372 def write(data) 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 # Snapshot @stdin under the lock so close() nilling it concurrently is # safe, but do the actual blocking IO *outside* the lock. Holding the # mutex across @stdin.write would let a full pipe buffer block the # writer indefinitely and block close() (which also needs the lock) # from killing the subprocess — a hang on disconnect. # # If close() runs while we are inside the IO call, it will close the # underlying stream and Ruby raises IOError("stream closed in another # thread") inside @stdin.write — the rescue below converts that into a # standard CLIConnectionError so callers see a clean shutdown error. stdin = @stdin_mutex.synchronize do raise CLIConnectionError, 'ProcessTransport is not ready for writing' unless @ready && @stdin @stdin end 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 |