Class: Harnex::Adapters::CodexAppServer

Inherits:
Base
  • Object
show all
Defined in:
lib/harnex/adapters/codex_appserver.rb

Overview

Codex ‘app-server` adapter — JSON-RPC over stdio.

Talks to a spawned ‘codex app-server` subprocess by writing newline-delimited JSON-RPC messages on stdin and reading responses + notifications from stdout. Replaces the pane-scraping heuristics in `Adapters::Codex` (legacy, kept behind –legacy-pty).

Defined Under Namespace

Classes: JsonRpcClient

Constant Summary collapse

CLIENT_TITLE =
"harnex"
CLIENT_NAME =
"harnex"
OPT_OUT_NOTIFICATIONS =
%w[
  item/agentMessage/delta
  item/reasoning/summaryTextDelta
  item/reasoning/summaryPartAdded
  item/reasoning/textDelta
].freeze
REQUEST_METHODS =
%w[
  initialize thread/start turn/start turn/interrupt thread/resume
].freeze
NOTIFICATION_METHODS =
%w[
  thread/started turn/started turn/completed
  item/started item/completed
  thread/status/changed thread/tokenUsage/updated
  thread/compacted account/rateLimits/updated
  error
].freeze
EVENTS =
%w[task_complete turn_started item_completed disconnected].freeze

Constants inherited from Base

Base::PROMPT_PREFIXES

Instance Attribute Summary collapse

Attributes inherited from Base

#key

Instance Method Summary collapse

Methods inherited from Base

#build_command, #infer_repo_path, #parse_session_summary, #send_wait_seconds, #wait_for_sendable, #wait_for_sendable_state?

Constructor Details

#initialize(extra_args = []) ⇒ CodexAppServer

Returns a new instance of CodexAppServer.



39
40
41
42
43
44
45
46
47
48
# File 'lib/harnex/adapters/codex_appserver.rb', line 39

def initialize(extra_args = [])
  super("codex", extra_args)
  @client = nil
  @thread_id = nil
  @current_turn_id = nil
  @state = :disconnected
  @last_completed_at = nil
  @notification_handler = nil
  @disconnect_handler = nil
end

Instance Attribute Details

#current_turn_idObject (readonly)

Returns the value of attribute current_turn_id.



37
38
39
# File 'lib/harnex/adapters/codex_appserver.rb', line 37

def current_turn_id
  @current_turn_id
end

#last_completed_atObject (readonly)

Returns the value of attribute last_completed_at.



37
38
39
# File 'lib/harnex/adapters/codex_appserver.rb', line 37

def last_completed_at
  @last_completed_at
end

#thread_idObject (readonly)

Returns the value of attribute thread_id.



37
38
39
# File 'lib/harnex/adapters/codex_appserver.rb', line 37

def thread_id
  @thread_id
end

Instance Method Details

#base_commandObject



54
55
56
# File 'lib/harnex/adapters/codex_appserver.rb', line 54

def base_command
  ["codex", "app-server"]
end

#build_send_payloadObject

The screen-based send path is not used for stdio_jsonrpc. Session#run_jsonrpc routes through #dispatch instead.

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/harnex/adapters/codex_appserver.rb', line 81

def build_send_payload(*)
  raise NotImplementedError, "codex_appserver uses #dispatch, not screen-based send"
end

#closeObject



148
149
150
151
152
153
154
# File 'lib/harnex/adapters/codex_appserver.rb', line 148

def close
  return unless @client

  @client.close
  @client = nil
  @state = :disconnected
end

#describeObject



58
59
60
61
62
63
64
65
# File 'lib/harnex/adapters/codex_appserver.rb', line 58

def describe
  {
    transport: transport,
    request_methods: REQUEST_METHODS,
    notification_methods: NOTIFICATION_METHODS,
    events: EVENTS
  }
end

#dispatch(prompt:, model: nil, effort: nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/harnex/adapters/codex_appserver.rb', line 116

def dispatch(prompt:, model: nil, effort: nil)
  ensure_open!
  ensure_thread!
  params = {
    threadId: @thread_id,
    input: { content: [{ type: "text", text: prompt.to_s }] }
  }
  params[:model] = model if model
  params[:effort] = effort if effort

  result = @client.request("turn/start", params)
  @current_turn_id = result["turnId"] || result["turn_id"] || result["id"]
  @state = :busy
  @current_turn_id
end

#inject_exit(_writer, **_kwargs) ⇒ Object

No-op: closing the subprocess is handled via #close.



86
87
88
# File 'lib/harnex/adapters/codex_appserver.rb', line 86

def inject_exit(_writer, **_kwargs)
  nil
end

#input_state(_screen_text = nil) ⇒ Object

Override: state is RPC-driven, screen text is ignored.



72
73
74
75
76
77
# File 'lib/harnex/adapters/codex_appserver.rb', line 72

def input_state(_screen_text = nil)
  {
    state: @state.to_s,
    input_ready: @state == :prompt
  }
end

#interrupt(turn_id: nil) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/harnex/adapters/codex_appserver.rb', line 132

def interrupt(turn_id: nil)
  ensure_open!
  target = turn_id || @current_turn_id
  return nil if target.nil?

  @client.request("turn/interrupt", { threadId: @thread_id, turnId: target })
end

#on_disconnect(&block) ⇒ Object



94
95
96
# File 'lib/harnex/adapters/codex_appserver.rb', line 94

def on_disconnect(&block)
  @disconnect_handler = block
end

#on_notification(&block) ⇒ Object



90
91
92
# File 'lib/harnex/adapters/codex_appserver.rb', line 90

def on_notification(&block)
  @notification_handler = block
end

#pidObject



156
157
158
# File 'lib/harnex/adapters/codex_appserver.rb', line 156

def pid
  @client&.pid
end

#resume(thread_id:) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/harnex/adapters/codex_appserver.rb', line 140

def resume(thread_id:)
  ensure_open!
  result = @client.request("thread/resume", { threadId: thread_id })
  @thread_id = thread_id
  @state = :prompt
  result
end

#start_rpc(env: nil, cwd: nil, read_io: nil, write_io: nil, pid: nil) ⇒ Object

Start the JSON-RPC client. In production, spawns the codex subprocess. In tests, callers may pass pre-built IO objects.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/harnex/adapters/codex_appserver.rb', line 100

def start_rpc(env: nil, cwd: nil, read_io: nil, write_io: nil, pid: nil)
  if read_io && write_io
    @client = JsonRpcClient.new(read_io: read_io, write_io: write_io, pid: pid)
  else
    spawn_pid, child_stdin, child_stdout = spawn_subprocess(env, cwd)
    @client = JsonRpcClient.new(read_io: child_stdout, write_io: child_stdin, pid: spawn_pid)
  end

  @client.on_notification { |msg| handle_notification(msg) }
  @client.on_disconnect { |err| handle_disconnect(err) }
  @client.start
  perform_handshake
  @state = :prompt
  self
end

#stateObject



67
68
69
# File 'lib/harnex/adapters/codex_appserver.rb', line 67

def state
  @state
end

#transportObject



50
51
52
# File 'lib/harnex/adapters/codex_appserver.rb', line 50

def transport
  :stdio_jsonrpc
end