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/adapters/claude.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/runtime/file_change_hook.rb
Defined Under Namespace
Modules: Adapters, Inotify, Polling, Watcher
Classes: ApiServer, BinaryNotFound, CLI, FileChangeHook, Guide, Inbox, Logs, Message, Pane, Recipes, 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.3.1"
- RELEASE_DATE =
"2026-04-19"
Class Method Summary
collapse
-
.active_session_ids(repo_root) ⇒ Object
-
.active_sessions(repo_root = nil, id: nil, cli: nil) ⇒ Object
-
.alive_pid?(pid) ⇒ Boolean
-
.allocate_port(repo_root, id, requested_port = nil, host: DEFAULT_HOST) ⇒ Object
-
.build_adapter(cli, argv) ⇒ Object
-
.build_watch_config(path, repo_root) ⇒ Object
-
.cli_key(cli) ⇒ Object
-
.current_session_context(env = ENV) ⇒ Object
-
.env_value(name, default: nil) ⇒ Object
-
.exit_status_path(repo_root, id) ⇒ Object
-
.format_relay_message(text, from:, id:, at: Time.now) ⇒ Object
-
.generate_id(repo_root) ⇒ Object
-
.id_key(id) ⇒ Object
-
.normalize_id(id) ⇒ Object
-
.output_log_path(repo_root, id) ⇒ Object
-
.parent_pid(pid) ⇒ Object
-
.port_available?(host, port) ⇒ Boolean
-
.read_registry(repo_root, id = DEFAULT_ID, cli: nil) ⇒ Object
-
.registry_path(repo_root, id = DEFAULT_ID) ⇒ Object
-
.repo_key(repo_root) ⇒ Object
-
.resolve_repo_root(path = Dir.pwd) ⇒ Object
-
.session_cli(session) ⇒ Object
-
.session_file_slug(repo_root, id) ⇒ Object
-
.tmux_pane_for_pid(pid) ⇒ Object
-
.write_registry(path, payload) ⇒ Object
Class Method Details
.active_session_ids(repo_root) ⇒ Object
85
86
87
|
# File 'lib/harnex/core.rb', line 85
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/harnex/core.rb', line 122
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
152
153
154
155
156
157
158
159
|
# File 'lib/harnex/core.rb', line 152
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/harnex/core.rb', line 232
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
258
259
260
261
262
|
# File 'lib/harnex/core.rb', line 258
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'lib/harnex/core.rb', line 268
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
55
56
57
58
59
60
|
# File 'lib/harnex/core.rb', line 55
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/harnex/core.rb', line 62
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
12
13
14
|
# File 'lib/harnex/core.rb', line 12
def env_value(name, default: nil)
ENV.fetch(name, default)
end
|
.exit_status_path(repo_root, id) ⇒ Object
104
105
106
107
108
|
# File 'lib/harnex/core.rb', line 104
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
|
77
78
79
80
81
82
83
|
# File 'lib/harnex/core.rb', line 77
def format_relay_message(text, from:, id:, at: Time.now)
= "[harnex relay from=#{from} id=#{normalize_id(id)} at=#{at.iso8601}]"
body = text.to_s
return if body.empty?
"#{}\n#{body}"
end
|
.generate_id(repo_root) ⇒ Object
89
90
91
92
93
94
95
96
97
|
# File 'lib/harnex/core.rb', line 89
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
51
52
53
|
# File 'lib/harnex/core.rb', line 51
def id_key(id)
normalize_id(id).downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "")
end
|
.normalize_id(id) ⇒ Object
44
45
46
47
48
49
|
# File 'lib/harnex/core.rb', line 44
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
110
111
112
113
114
|
# File 'lib/harnex/core.rb', line 110
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
216
217
218
219
220
221
222
223
224
|
# File 'lib/harnex/core.rb', line 216
def parent_pid(pid)
stat = File.read("/proc/#{pid}/stat")
parts = stat.match(/\A\d+\s+\(.*?\)\s+\S+\s+(\d+)/)
parts ? parts[1].to_i : nil
rescue Errno::ENOENT, Errno::EACCES
nil
end
|
.port_available?(host, port) ⇒ Boolean
250
251
252
253
254
255
256
|
# File 'lib/harnex/core.rb', line 250
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
161
162
163
164
165
166
|
# File 'lib/harnex/core.rb', line 161
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
99
100
101
102
|
# File 'lib/harnex/core.rb', line 99
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
40
41
42
|
# File 'lib/harnex/core.rb', line 40
def repo_key(repo_root)
Digest::SHA256.hexdigest(repo_root)[0, 16]
end
|
.resolve_repo_root(path = Dir.pwd) ⇒ Object
33
34
35
36
37
38
|
# File 'lib/harnex/core.rb', line 33
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
264
265
266
|
# File 'lib/harnex/core.rb', line 264
def session_cli(session)
(session["cli"] || Array(session["command"]).first).to_s
end
|
.session_file_slug(repo_root, id) ⇒ Object
116
117
118
119
120
|
# File 'lib/harnex/core.rb', line 116
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
168
169
170
171
172
173
174
175
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
|
# File 'lib/harnex/core.rb', line 168
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
matches = panes.select { |p| p[:pane_pid] == target_pid }
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
226
227
228
229
230
|
# File 'lib/harnex/core.rb', line 226
def write_registry(path, payload)
tmp = "#{path}.tmp.#{Process.pid}"
File.write(tmp, JSON.pretty_generate(payload))
File.rename(tmp, path)
end
|