Module: AgentClientProtocol::Stdio

Defined in:
lib/agent_client_protocol/stdio.rb

Constant Summary collapse

SHUTDOWN_TIMEOUT =

seconds

5

Class Method Summary collapse

Class Method Details

.clean_environment(extra = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/agent_client_protocol/stdio.rb', line 119

def clean_environment(extra = nil)
  env = {}
  # Minimal safe environment
  %w[PATH HOME USER LANG TERM].each do |key|
    env[key] = ENV[key] if ENV[key]
  end
  env.merge!(extra) if extra
  env
end

.run_agent(agent) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/agent_client_protocol/stdio.rb', line 11

def run_agent(agent)
  Async do
    reader = Transport::NdjsonReader.new($stdin)
    writer = Transport::NdjsonWriter.new($stdout)

    # Redirect any puts/print to stderr so stdout is reserved for protocol
    $stdout = $stderr

    conn = Agent::Connection.new(agent, reader, writer)
    conn.listen
  end
end

.shutdown_process(conn, pid, stdin_w, listen_task) ⇒ Object



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
# File 'lib/agent_client_protocol/stdio.rb', line 84

def shutdown_process(conn, pid, stdin_w, listen_task)
  listen_task&.stop
  conn.close

  # Close stdin to signal the subprocess
  stdin_w.close unless stdin_w.closed?

  # Use a thread for waitpid to avoid blocking the async reactor
  reap_thread = Thread.new do
    begin
      Process.waitpid(pid)
    rescue Errno::ECHILD, Errno::ESRCH
      # Already reaped
    end
  end

  unless reap_thread.join(SHUTDOWN_TIMEOUT)
    begin
      Process.kill("TERM", pid)
    rescue Errno::ESRCH
      # Already gone
    end
    unless reap_thread.join(SHUTDOWN_TIMEOUT)
      begin
        Process.kill("KILL", pid)
      rescue Errno::ESRCH
        # Already gone
      end
      reap_thread.join(1)
    end
  end
rescue Errno::ECHILD, Errno::ESRCH
  # Process already gone
end

.spawn_agent_process(client, command, *args, env: nil, cwd: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/agent_client_protocol/stdio.rb', line 24

def spawn_agent_process(client, command, *args, env: nil, cwd: nil)
  Async do |task|
    pid, stdin_w, stdout_r = spawn_process(command, *args, env: env, cwd: cwd)

    reader = Transport::NdjsonReader.new(stdout_r)
    writer = Transport::NdjsonWriter.new(stdin_w)
    conn = Client::Connection.new(client, reader, writer)

    listen_task = task.async { conn.listen }

    if block_given?
      begin
        yield conn, pid
      ensure
        shutdown_process(conn, pid, stdin_w, listen_task)
      end
    else
      [conn, pid, listen_task]
    end
  end
end

.spawn_client_process(agent, command, *args, env: nil, cwd: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/agent_client_protocol/stdio.rb', line 46

def spawn_client_process(agent, command, *args, env: nil, cwd: nil)
  Async do |task|
    pid, stdin_w, stdout_r = spawn_process(command, *args, env: env, cwd: cwd)

    reader = Transport::NdjsonReader.new(stdout_r)
    writer = Transport::NdjsonWriter.new(stdin_w)
    conn = Agent::Connection.new(agent, reader, writer)

    listen_task = task.async { conn.listen }

    if block_given?
      begin
        yield conn, pid
      ensure
        shutdown_process(conn, pid, stdin_w, listen_task)
      end
    else
      [conn, pid, listen_task]
    end
  end
end

.spawn_process(command, *args, env: nil, cwd: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/agent_client_protocol/stdio.rb', line 68

def spawn_process(command, *args, env: nil, cwd: nil)
  spawn_env = clean_environment(env)
  opts = {in: :pipe, out: :pipe, err: $stderr}
  opts[:chdir] = cwd if cwd

  stdin_r, stdin_w = IO.pipe
  stdout_r, stdout_w = IO.pipe

  pid = Process.spawn(spawn_env, command, *args, in: stdin_r, out: stdout_w, err: $stderr, **(cwd ? {chdir: cwd} : {}))

  stdin_r.close
  stdout_w.close

  [pid, stdin_w, stdout_r]
end