Class: Harnex::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/harnex/runtime/session.rb

Constant Summary collapse

OUTPUT_BUFFER_LIMIT =
64 * 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:, command:, repo_root:, host:, port: nil, id: DEFAULT_ID, watch: nil, description: nil, inbox_ttl: Inbox::DEFAULT_TTL) ⇒ Session

Returns a new instance of Session.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/harnex/runtime/session.rb', line 11

def initialize(adapter:, command:, repo_root:, host:, port: nil, id: DEFAULT_ID, watch: nil, description: nil, inbox_ttl: Inbox::DEFAULT_TTL)
  @adapter = adapter
  @command = command
  @repo_root = repo_root
  @host = host
  @id = Harnex.normalize_id(id)
  @watch = watch
  @description = description.to_s.strip
  @description = nil if @description.empty?
  @registry_path = Harnex.registry_path(repo_root, @id)
  @output_log_path = Harnex.output_log_path(repo_root, @id)
  @session_id = SecureRandom.hex(8)
  @token = SecureRandom.hex(16)
  @port = Harnex.allocate_port(repo_root, @id, port, host: host)
  @mutex = Mutex.new
  @inject_mutex = Mutex.new
  @injected_count = 0
  @last_injected_at = nil
  @started_at = Time.now
  @server = nil
  @reader = nil
  @output_log = nil
  @writer = nil
  @pid = nil
  @term_signal = nil
  @output_buffer = +""
  @output_buffer.force_encoding(Encoding::BINARY)
  @state_machine = SessionState.new(adapter)
  @inbox = Inbox.new(self, @state_machine, ttl: inbox_ttl)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def adapter
  @adapter
end

#commandObject (readonly)

Returns the value of attribute command.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def command
  @command
end

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def description
  @description
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def host
  @host
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def id
  @id
end

#inboxObject (readonly)

Returns the value of attribute inbox.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def inbox
  @inbox
end

#output_log_pathObject (readonly)

Returns the value of attribute output_log_path.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def output_log_path
  @output_log_path
end

#pidObject (readonly)

Returns the value of attribute pid.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def pid
  @pid
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def port
  @port
end

#repo_rootObject (readonly)

Returns the value of attribute repo_root.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def repo_root
  @repo_root
end

#session_idObject (readonly)

Returns the value of attribute session_id.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def session_id
  @session_id
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def token
  @token
end

#watchObject (readonly)

Returns the value of attribute watch.



9
10
11
# File 'lib/harnex/runtime/session.rb', line 9

def watch
  @watch
end

Class Method Details

.validate_binary!(command) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/harnex/runtime/session.rb', line 42

def self.validate_binary!(command)
  binary = Array(command).first.to_s
  raise BinaryNotFound, "\"\" not found — is it installed and on your PATH?" if binary.empty?

  if binary.include?("/")
    return binary if File.executable?(binary) && !File.directory?(binary)

    raise BinaryNotFound, "\"#{binary}\" not found — is it installed and on your PATH?"
  end

  ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).each do |dir|
    path = File.join(dir, binary)
    return path if File.executable?(path) && !File.directory?(path)
  end

  raise BinaryNotFound, "\"#{binary}\" not found — is it installed and on your PATH?"
end

Instance Method Details

#auth_ok?(header) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/harnex/runtime/session.rb', line 128

def auth_ok?(header)
  header == "Bearer #{token}"
end

#inject(text, newline: true) ⇒ Object



132
133
134
135
136
# File 'lib/harnex/runtime/session.rb', line 132

def inject(text, newline: true)
  raise "session is not running" unless pid && Harnex.alive_pid?(pid)

  inject_sequence([{ text: text, newline: newline }])
end

#inject_stopObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/harnex/runtime/session.rb', line 138

def inject_stop
  raise "session is not running" unless pid && Harnex.alive_pid?(pid)

  @inject_mutex.synchronize do
    adapter.inject_exit(@writer)
    @state_machine.force_busy!
  end

  { ok: true, signal: "exit_sequence_sent" }
end

#inject_via_adapter(text:, submit:, enter_only:, force: false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/harnex/runtime/session.rb', line 149

def inject_via_adapter(text:, submit:, enter_only:, force: false)
  snapshot = adapter.wait_for_sendable(method(:screen_snapshot), submit: submit, enter_only: enter_only, force: force)
  payload = adapter.build_send_payload(
    text: text,
    submit: submit,
    enter_only: enter_only,
    screen_text: snapshot,
    force: force
  )

  result =
    if payload[:steps]
      inject_sequence(payload.fetch(:steps))
    else
      inject(payload.fetch(:text), newline: payload.fetch(:newline, false))
    end

  result.merge(
    cli: adapter.key,
    input_state: payload[:input_state],
    force: payload[:force]
  )
end

#run(validate_binary: true) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
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
# File 'lib/harnex/runtime/session.rb', line 60

def run(validate_binary: true)
  validate_binary! if validate_binary
  prepare_output_log
  @reader, @writer, @pid = PTY.spawn(child_env, *command)
  @writer.sync = true

  install_signal_handlers
  sync_window_size
  @server = ApiServer.new(self)
  @server.start
  persist_registry

  stdin_state = STDIN.tty? ? STDIN.raw! : nil
  watch_thread = start_watch_thread
  @inbox.start
  input_thread = start_input_thread
  output_thread = start_output_thread

  _, status = Process.wait2(pid)
  @term_signal = status.signaled? ? status.termsig : nil
  @exit_code = status.exited? ? status.exitstatus : 128 + status.termsig

  output_thread.join(1)
  input_thread&.kill
  watch_thread&.kill
  @exit_code
ensure
  @inbox.stop
  STDIN.cooked! if STDIN.tty? && stdin_state
  @server&.stop
  persist_exit_status
  cleanup_registry
  @reader&.close unless @reader&.closed?
  @output_log&.close unless @output_log&.closed?
  @writer&.close unless @writer&.closed?
end

#status_payload(include_input_state: true) ⇒ Object



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
# File 'lib/harnex/runtime/session.rb', line 97

def status_payload(include_input_state: true)
  payload = {
    ok: true,
    session_id: session_id,
    repo_root: repo_root,
    repo_key: Harnex.repo_key(repo_root),
    cli: adapter.key,
    id: id,
    pid: pid,
    host: host,
    port: port,
    command: command,
    started_at: @started_at.iso8601,
    last_injected_at: @last_injected_at&.iso8601,
    injected_count: @injected_count,
    output_log_path: output_log_path
  }
  payload[:description] = description if description

  if watch
    payload[:watch_path] = watch.display_path
    payload[:watch_absolute_path] = watch.absolute_path
    payload[:watch_debounce_seconds] = watch.debounce_seconds
  end

  payload[:input_state] = adapter.input_state(screen_snapshot) if include_input_state
  payload[:agent_state] = @state_machine.to_s
  payload[:inbox] = @inbox.stats
  payload
end

#sync_window_sizeObject



173
174
175
176
177
178
179
# File 'lib/harnex/runtime/session.rb', line 173

def sync_window_size
  return unless STDIN.tty?

  @writer.winsize = STDIN.winsize
rescue StandardError
  nil
end

#validate_binary!Object



181
182
183
# File 'lib/harnex/runtime/session.rb', line 181

def validate_binary!
  self.class.validate_binary!(command)
end