Module: Harnex

Defined in:
lib/harnex/cli.rb,
lib/harnex/core.rb,
lib/harnex/version.rb,
lib/harnex/watcher.rb,
lib/harnex/adapters.rb,
lib/harnex/commands/run.rb,
lib/harnex/adapters/base.rb,
lib/harnex/commands/logs.rb,
lib/harnex/commands/pane.rb,
lib/harnex/commands/send.rb,
lib/harnex/commands/stop.rb,
lib/harnex/commands/wait.rb,
lib/harnex/runtime/inbox.rb,
lib/harnex/adapters/codex.rb,
lib/harnex/commands/guide.rb,
lib/harnex/commands/watch.rb,
lib/harnex/adapters/claude.rb,
lib/harnex/commands/events.rb,
lib/harnex/commands/skills.rb,
lib/harnex/commands/status.rb,
lib/harnex/runtime/message.rb,
lib/harnex/runtime/session.rb,
lib/harnex/watcher/inotify.rb,
lib/harnex/watcher/polling.rb,
lib/harnex/adapters/generic.rb,
lib/harnex/commands/recipes.rb,
lib/harnex/runtime/api_server.rb,
lib/harnex/runtime/session_state.rb,
lib/harnex/commands/watch_presets.rb,
lib/harnex/runtime/file_change_hook.rb

Defined Under Namespace

Modules: Adapters, Inotify, Polling, WatchPresets, Watcher Classes: ApiServer, BinaryNotFound, CLI, Events, FileChangeHook, Guide, Inbox, Logs, Message, Pane, Recipes, RunWatcher, Runner, Sender, Session, SessionState, Skills, Status, Stopper, Waiter, WatchConfig

Constant Summary collapse

DEFAULT_HOST =
env_value("HARNEX_HOST", default: "127.0.0.1")
DEFAULT_BASE_PORT =
Integer(env_value("HARNEX_BASE_PORT", default: "43000"))
DEFAULT_PORT_SPAN =
Integer(env_value("HARNEX_PORT_SPAN", default: "4000"))
DEFAULT_ID =
"default"
WATCH_DEBOUNCE_SECONDS =
1.0
STATE_DIR =
File.expand_path(env_value("HARNEX_STATE_DIR", default: "~/.local/state/harnex"))
SESSIONS_DIR =
File.join(STATE_DIR, "sessions")
ID_ADJECTIVES =
%w[
  bold blue calm cool dark dry fast gold gray green
  keen loud mint pale pink red shy slim soft warm
].freeze
ID_NOUNS =
%w[
  ant bat bee cat cod cow cub doe elk fox
  hen jay kit owl pug ram ray seal wasp yak
].freeze
VERSION =
"0.4.0"
RELEASE_DATE =
"2026-04-30"

Class Method Summary collapse

Class Method Details

.active_session_ids(repo_root) ⇒ Object



112
113
114
# File 'lib/harnex/core.rb', line 112

def active_session_ids(repo_root)
  active_sessions(repo_root).map { |session| session["id"].to_s.downcase }.to_set
end

.active_sessions(repo_root = nil, id: nil, cli: nil) ⇒ Object



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
# File 'lib/harnex/core.rb', line 155

def active_sessions(repo_root = nil, id: nil, cli: nil)
  FileUtils.mkdir_p(SESSIONS_DIR)
  pattern =
    if repo_root
      File.join(SESSIONS_DIR, "#{repo_key(repo_root)}--*.json")
    else
      File.join(SESSIONS_DIR, "*.json")
    end

  target_id_key = id.nil? ? nil : id_key(id)
  normalized_cli = cli_key(cli)

  Dir.glob(pattern).sort.filter_map do |path|
    data = JSON.parse(File.read(path))
    if data["pid"] && alive_pid?(data["pid"])
      session = data.merge("registry_path" => path)
      next if target_id_key && id_key(session["id"].to_s) != target_id_key
      next if normalized_cli && cli_key(session_cli(session)) != normalized_cli

      session
    else
      FileUtils.rm_f(path)
      nil
    end
  rescue JSON::ParserError
    FileUtils.rm_f(path)
    nil
  end
end

.alive_pid?(pid) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
# File 'lib/harnex/core.rb', line 185

def alive_pid?(pid)
  Process.kill(0, Integer(pid))
  true
rescue Errno::ESRCH
  false
rescue Errno::EPERM
  true
end

.allocate_port(repo_root, id, requested_port = nil, host: DEFAULT_HOST) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/harnex/core.rb', line 265

def allocate_port(repo_root, id, requested_port = nil, host: DEFAULT_HOST)
  if requested_port
    return requested_port if port_available?(host, requested_port)

    raise "port #{requested_port} is already in use on #{host}"
  end

  seed = Digest::SHA256.hexdigest("#{repo_root}\0#{normalize_id(id)}").to_i(16)
  offset = seed % DEFAULT_PORT_SPAN

  DEFAULT_PORT_SPAN.times do |index|
    port = DEFAULT_BASE_PORT + ((offset + index) % DEFAULT_PORT_SPAN)
    return port if port_available?(host, port)
  end

  raise "could not find a free port in #{DEFAULT_BASE_PORT}-#{DEFAULT_BASE_PORT + DEFAULT_PORT_SPAN - 1}"
end

.build_adapter(cli, argv) ⇒ Object

Raises:

  • (ArgumentError)


291
292
293
294
295
# File 'lib/harnex/core.rb', line 291

def build_adapter(cli, argv)
  raise ArgumentError, "cli is required" if cli.to_s.strip.empty?

  Adapters.build(cli, argv)
end

.build_watch_config(path, repo_root) ⇒ Object

Raises:

  • (ArgumentError)


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/harnex/core.rb', line 301

def build_watch_config(path, repo_root)
  return nil if path.nil?

  raise "file watch is unsupported on this system" unless Watcher.available?

  display_path = path.to_s.strip
  raise ArgumentError, "--watch requires a value" if display_path.empty?

  absolute_path = File.expand_path(display_path, repo_root)
  FileUtils.mkdir_p(File.dirname(absolute_path))

  WatchConfig.new(
    absolute_path: absolute_path,
    display_path: display_path,
    hook_message: "file-change-hook: read #{display_path}",
    debounce_seconds: WATCH_DEBOUNCE_SECONDS
  )
end

.cli_key(cli) ⇒ Object



82
83
84
85
86
87
# File 'lib/harnex/core.rb', line 82

def cli_key(cli)
  value = cli.to_s.strip.downcase
  return nil if value.empty?

  value.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "")
end

.current_session_context(env = ENV) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/harnex/core.rb', line 89

def current_session_context(env = ENV)
  session_id = env["HARNEX_SESSION_ID"].to_s.strip
  cli = env["HARNEX_SESSION_CLI"].to_s.strip
  id = env["HARNEX_ID"].to_s.strip
  repo_root = env["HARNEX_SESSION_REPO_ROOT"].to_s.strip
  return nil if session_id.empty? || cli.empty? || id.empty?

  {
    session_id: session_id,
    cli: cli,
    id: id,
    repo_root: repo_root.empty? ? nil : repo_root
  }
end

.env_value(name, default: nil) ⇒ Object



13
14
15
# File 'lib/harnex/core.rb', line 13

def env_value(name, default: nil)
  ENV.fetch(name, default)
end

.events_log_path(repo_root, id) ⇒ Object



143
144
145
146
147
# File 'lib/harnex/core.rb', line 143

def events_log_path(repo_root, id)
  events_dir = File.join(STATE_DIR, "events")
  FileUtils.mkdir_p(events_dir)
  File.join(events_dir, "#{session_file_slug(repo_root, id)}.jsonl")
end

.exit_status_path(repo_root, id) ⇒ Object



131
132
133
134
135
# File 'lib/harnex/core.rb', line 131

def exit_status_path(repo_root, id)
  exit_dir = File.join(STATE_DIR, "exits")
  FileUtils.mkdir_p(exit_dir)
  File.join(exit_dir, "#{session_file_slug(repo_root, id)}.json")
end

.format_relay_message(text, from:, id:, at: Time.now) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/harnex/core.rb', line 104

def format_relay_message(text, from:, id:, at: Time.now)
  header = "[harnex relay from=#{from} id=#{normalize_id(id)} at=#{at.iso8601}]"
  body = text.to_s
  return header if body.empty?

  "#{header}\n#{body}"
end

.generate_id(repo_root) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/harnex/core.rb', line 116

def generate_id(repo_root)
  taken = active_session_ids(repo_root)
  ID_ADJECTIVES.product(ID_NOUNS).shuffle.each do |adj, noun|
    candidate = "#{adj}-#{noun}"
    return candidate unless taken.include?(candidate)
  end

  "session-#{SecureRandom.hex(4)}"
end

.id_key(id) ⇒ Object



78
79
80
# File 'lib/harnex/core.rb', line 78

def id_key(id)
  normalize_id(id).downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "")
end

.normalize_id(id) ⇒ Object



71
72
73
74
75
76
# File 'lib/harnex/core.rb', line 71

def normalize_id(id)
  value = id.to_s.strip
  raise "id is required" if value.empty?

  value
end

.output_log_path(repo_root, id) ⇒ Object



137
138
139
140
141
# File 'lib/harnex/core.rb', line 137

def output_log_path(repo_root, id)
  output_dir = File.join(STATE_DIR, "output")
  FileUtils.mkdir_p(output_dir)
  File.join(output_dir, "#{session_file_slug(repo_root, id)}.log")
end

.parent_pid(pid) ⇒ Object



249
250
251
252
253
254
255
256
257
# File 'lib/harnex/core.rb', line 249

def parent_pid(pid)
  stat = File.read("/proc/#{pid}/stat")
  # Field 4 is ppid (fields are space-separated, field 1 is pid,
  # field 2 is (comm) which may contain spaces, field 3 is state, field 4 is ppid)
  parts = stat.match(/\A\d+\s+\(.*?\)\s+\S+\s+(\d+)/)
  parts ? parts[1].to_i : nil
rescue Errno::ENOENT, Errno::EACCES
  nil
end

.parse_duration_seconds(value, option_name:) ⇒ Object

Raises:

  • (OptionParser::InvalidArgument)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/harnex/core.rb', line 41

def parse_duration_seconds(value, option_name:)
  text = value.to_s.strip
  raise OptionParser::InvalidArgument, "#{option_name} requires a value" if text.empty?

  match = text.match(/\A([0-9]+(?:\.[0-9]+)?)([smhSMH]?)\z/)
  unless match
    raise OptionParser::InvalidArgument,
          "#{option_name} must be a positive duration (examples: 30, 30s, 5m, 2h)"
  end

  amount = Float(match[1])
  multiplier =
    case match[2].downcase
    when "", "s" then 1.0
    when "m" then 60.0
    when "h" then 3600.0
    else
      raise OptionParser::InvalidArgument, "#{option_name} has an unsupported duration suffix"
    end

  seconds = amount * multiplier
  raise OptionParser::InvalidArgument, "#{option_name} must be greater than 0" if seconds <= 0.0

  seconds
end

.port_available?(host, port) ⇒ Boolean

Returns:

  • (Boolean)


283
284
285
286
287
288
289
# File 'lib/harnex/core.rb', line 283

def port_available?(host, port)
  server = TCPServer.new(host, port)
  server.close
  true
rescue Errno::EADDRINUSE, Errno::EACCES
  false
end

.read_registry(repo_root, id = DEFAULT_ID, cli: nil) ⇒ Object



194
195
196
197
198
199
# File 'lib/harnex/core.rb', line 194

def read_registry(repo_root, id = DEFAULT_ID, cli: nil)
  sessions = active_sessions(repo_root, id: id, cli: cli)
  return nil unless sessions.length == 1

  sessions.first
end

.registry_path(repo_root, id = DEFAULT_ID) ⇒ Object



126
127
128
129
# File 'lib/harnex/core.rb', line 126

def registry_path(repo_root, id = DEFAULT_ID)
  FileUtils.mkdir_p(SESSIONS_DIR)
  File.join(SESSIONS_DIR, "#{session_file_slug(repo_root, id)}.json")
end

.repo_key(repo_root) ⇒ Object



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

def repo_key(repo_root)
  Digest::SHA256.hexdigest(repo_root)[0, 16]
end

.resolve_repo_root(path = Dir.pwd) ⇒ Object



34
35
36
37
38
39
# File 'lib/harnex/core.rb', line 34

def resolve_repo_root(path = Dir.pwd)
  output, status = Open3.capture2("git", "rev-parse", "--show-toplevel", chdir: path)
  status.success? ? output.strip : File.expand_path(path)
rescue StandardError
  File.expand_path(path)
end

.session_cli(session) ⇒ Object



297
298
299
# File 'lib/harnex/core.rb', line 297

def session_cli(session)
  (session["cli"] || Array(session["command"]).first).to_s
end

.session_file_slug(repo_root, id) ⇒ Object



149
150
151
152
153
# File 'lib/harnex/core.rb', line 149

def session_file_slug(repo_root, id)
  slug = id_key(id)
  slug = "default" if slug.empty?
  "#{repo_key(repo_root)}--#{slug}"
end

.tmux_pane_for_pid(pid) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
# File 'lib/harnex/core.rb', line 201

def tmux_pane_for_pid(pid)
  target_pid = Integer(pid)
  stdout, status = Open3.capture2(
    "tmux", "list-panes", "-a", "-F",
    "\#{pane_id}\t\#{pane_pid}\t\#{session_name}\t\#{window_name}"
  )
  return nil unless status.success?

  panes = stdout.each_line.filter_map do |line|
    pane_id, pane_pid, session_name, window_name = line.chomp.split("\t", 4)
    next if pane_id.to_s.empty?

    {
      target: pane_id,
      pane_id: pane_id,
      pane_pid: pane_pid.to_i,
      session_name: session_name,
      window_name: window_name
    }
  end

  pane_pids = panes.map { |p| p[:pane_pid] }.to_set

  # Direct match first
  matches = panes.select { |p| p[:pane_pid] == target_pid }

  # If no direct match, walk up the process tree from target_pid
  # to find an ancestor that is a tmux pane root process.
  if matches.empty?
    ancestor = parent_pid(target_pid)
    while ancestor && ancestor > 1
      if pane_pids.include?(ancestor)
        matches = panes.select { |p| p[:pane_pid] == ancestor }
        break
      end
      ancestor = parent_pid(ancestor)
    end
  end

  return nil unless matches.length == 1

  result = matches.first
  result.delete(:pane_pid)
  result
rescue ArgumentError, Errno::ENOENT
  nil
end

.write_registry(path, payload) ⇒ Object



259
260
261
262
263
# File 'lib/harnex/core.rb', line 259

def write_registry(path, payload)
  tmp = "#{path}.tmp.#{Process.pid}"
  File.write(tmp, JSON.pretty_generate(payload))
  File.rename(tmp, path)
end