Class: Harnex::Codex::AppServer::Client
- Inherits:
-
Object
- Object
- Harnex::Codex::AppServer::Client
- Defined in:
- lib/harnex/codex/app_server/client.rb
Overview
Extracted from Adapters::CodexAppServer per issue #41 Slice A. Pure move; behavior unchanged.
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Class Method Summary collapse
-
.spawn(deployment_config:) ⇒ Object
Plan 30 Phase 2 — deployment-fallback subprocess restart.
-
.spawn_with_fallback(prior_thread_id:, deployment_config:, handshake_params:, notification_handler: nil, request_handler: nil, disconnect_handler: nil) ⇒ Object
Plan 30 Phase 2 — full subprocess restart for deployment fallback.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(read_io:, write_io:, pid: nil) ⇒ Client
constructor
A new instance of Client.
- #notify(method, params = {}) ⇒ Object
- #on_disconnect(&block) ⇒ Object
- #on_notification(&block) ⇒ Object
-
#on_request(&block) ⇒ Object
Handler for server-initiated requests (id + method).
- #request(method, params = {}) ⇒ Object
- #start ⇒ Object
-
#stop_for_fallback(in_flight_turn: nil, term_grace_seconds: 0.5, kill_grace_seconds: 1.0, interrupt_grace_seconds: 0.5) ⇒ Object
Plan 30 Phase 2 — graceful stop ahead of a deployment switch.
- #terminate_process(term_grace_seconds:, kill_grace_seconds:) ⇒ Object
Constructor Details
#initialize(read_io:, write_io:, pid: nil) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/harnex/codex/app_server/client.rb', line 12 def initialize(read_io:, write_io:, pid: nil) @read_io = read_io @write_io = write_io @pid = pid @next_id = 1 @pending = {} @id_mutex = Mutex.new @write_mutex = Mutex.new @notification_handler = nil @request_handler = nil @disconnect_handler = nil @disconnect_signaled = false @closed = false @reader_thread = nil end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
10 11 12 |
# File 'lib/harnex/codex/app_server/client.rb', line 10 def pid @pid end |
Class Method Details
.spawn(deployment_config:) ⇒ Object
Plan 30 Phase 2 — deployment-fallback subprocess restart.
Spawns a ‘codex app-server` subprocess against a deployment_config and wraps it in a fresh Client. Caller is responsible for wiring handlers and running the JSON-RPC handshake — or use `spawn_with_fallback` for the full restart-with-resume flow.
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/harnex/codex/app_server/client.rb', line 125 def self.spawn(deployment_config:) command = deployment_config[:command] || deployment_config["command"] raise ArgumentError, "deployment_config requires :command" if command.nil? || Array(command).empty? env = deployment_config[:env] || deployment_config["env"] || {} cwd = deployment_config[:cwd] || deployment_config["cwd"] opts = {} opts[:chdir] = cwd if cwd stdin_io, stdout_io, _stderr_io, wait_thr = Open3.popen3(env, *Array(command), **opts) new(read_io: stdout_io, write_io: stdin_io, pid: wait_thr.pid) end |
.spawn_with_fallback(prior_thread_id:, deployment_config:, handshake_params:, notification_handler: nil, request_handler: nil, disconnect_handler: nil) ⇒ Object
Plan 30 Phase 2 — full subprocess restart for deployment fallback.
Spawns a new subprocess against the alternate deployment, wires the supplied handlers, runs the initialize handshake, and resumes the prior threadId. Returns the started Client.
Handlers are installed before the reader thread starts so no post-handshake notification is dropped.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/harnex/codex/app_server/client.rb', line 147 def self.spawn_with_fallback(prior_thread_id:, deployment_config:, handshake_params:, notification_handler: nil, request_handler: nil, disconnect_handler: nil) raise ArgumentError, "prior_thread_id required" if prior_thread_id.nil? || prior_thread_id.to_s.empty? client = spawn(deployment_config: deployment_config) client.on_notification(¬ification_handler) if notification_handler client.on_request(&request_handler) if request_handler client.on_disconnect(&disconnect_handler) if disconnect_handler client.start begin client.request("initialize", handshake_params) client.notify("initialized", {}) client.request("thread/resume", { threadId: prior_thread_id }) rescue StandardError client.close raise end client end |
Instance Method Details
#close ⇒ Object
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 |
# File 'lib/harnex/codex/app_server/client.rb', line 71 def close return if @closed @closed = true @id_mutex.synchronize do @pending.each_value { |q| q.push(StandardError.new("codex_appserver client closed")) } @pending.clear end begin @write_io.close unless @write_io.closed? rescue IOError nil end if @pid && process_alive?(@pid) sleep 0.05 begin Process.kill("TERM", @pid) rescue Errno::ESRCH nil end end @reader_thread&.join(2) end |
#notify(method, params = {}) ⇒ Object
65 66 67 68 69 |
# File 'lib/harnex/codex/app_server/client.rb', line 65 def notify(method, params = {}) return if @closed write_line({ jsonrpc: "2.0", method: method, params: params }) end |
#on_disconnect(&block) ⇒ Object
39 40 41 |
# File 'lib/harnex/codex/app_server/client.rb', line 39 def on_disconnect(&block) @disconnect_handler = block end |
#on_notification(&block) ⇒ Object
28 29 30 |
# File 'lib/harnex/codex/app_server/client.rb', line 28 def on_notification(&block) @notification_handler = block end |
#on_request(&block) ⇒ Object
Handler for server-initiated requests (id + method). The block receives (method, params) and returns the response body for the JSON-RPC ‘result` field, or nil to reject with -32601.
35 36 37 |
# File 'lib/harnex/codex/app_server/client.rb', line 35 def on_request(&block) @request_handler = block end |
#request(method, params = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/harnex/codex/app_server/client.rb', line 47 def request(method, params = {}) raise "codex_appserver client is closed" if @closed queue = Queue.new id = @id_mutex.synchronize do assigned = @next_id @next_id += 1 @pending[assigned] = queue assigned end write_line({ jsonrpc: "2.0", id: id, method: method, params: params }) result = queue.pop raise result if result.is_a?(Exception) result end |
#start ⇒ Object
43 44 45 |
# File 'lib/harnex/codex/app_server/client.rb', line 43 def start @reader_thread = Thread.new { read_loop } end |
#stop_for_fallback(in_flight_turn: nil, term_grace_seconds: 0.5, kill_grace_seconds: 1.0, interrupt_grace_seconds: 0.5) ⇒ Object
Plan 30 Phase 2 — graceful stop ahead of a deployment switch.
Best-effort: issues ‘turn/interrupt` for any in-flight turn (bounded by interrupt_grace_seconds), drains pending RPC, and tears the subprocess down with the same TERM/KILL escalation used by `terminate_process`.
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 |
# File 'lib/harnex/codex/app_server/client.rb', line 176 def stop_for_fallback(in_flight_turn: nil, term_grace_seconds: 0.5, kill_grace_seconds: 1.0, interrupt_grace_seconds: 0.5) if in_flight_turn && !@closed interrupt_thread = Thread.new do begin request("turn/interrupt", in_flight_turn) rescue StandardError nil end end interrupt_thread.kill unless interrupt_thread.join(interrupt_grace_seconds) end return true if @closed @closed = true fail_pending_requests(StandardError.new("codex_appserver client closed for fallback")) begin @write_io.close unless @write_io.closed? rescue IOError nil end terminated = if @pid terminate_process( term_grace_seconds: term_grace_seconds, kill_grace_seconds: kill_grace_seconds ) else true end @reader_thread&.join(2) terminated end |
#terminate_process(term_grace_seconds:, kill_grace_seconds:) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/harnex/codex/app_server/client.rb', line 99 def terminate_process(term_grace_seconds:, kill_grace_seconds:) return false unless @pid begin Process.kill("TERM", @pid) rescue Errno::ESRCH return true end return true if wait_for_process_exit(@pid, term_grace_seconds) begin Process.kill("KILL", @pid) rescue Errno::ESRCH return true end wait_for_process_exit(@pid, kill_grace_seconds) end |