agent-council 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CAUTIONS.md +254 -0
- data/DESIGN.md +137 -25
- data/INSTALL.md +14 -9
- data/README.md +413 -130
- data/RELEASE_NOTES.md +228 -110
- data/RUNAWAY_AGENTS.md +192 -10
- data/USE_CASES.md +3 -3
- data/examples/configs/antigravity.yml +20 -0
- data/examples/configs/mixed-harness.yml +17 -7
- data/examples/prompts/review-latest-commit-prompt.md +9 -0
- data/examples/prompts/review-pull-request-prompt.md +6 -0
- data/examples/prompts/test-coverage-analysis-prompt.md +56 -42
- data/exe/agent-council +6 -1
- data/exe/agent-council-ps +9 -0
- data/exe/pi-models +3 -33
- data/lib/agent_council/harnesses/antigravity_harness.rb +249 -0
- data/lib/agent_council/harnesses/claude_code_harness.rb +40 -60
- data/lib/agent_council/harnesses/codex_harness.rb +42 -89
- data/lib/agent_council/harnesses/harness.rb +193 -0
- data/lib/agent_council/harnesses/opencode_harness.rb +234 -0
- data/lib/agent_council/harnesses/pi_harness.rb +29 -59
- data/lib/agent_council/harnesses/scaffold.rb +6 -4
- data/lib/agent_council/harnesses.rb +14 -26
- data/lib/agent_council/pi_models.rb +40 -0
- data/lib/agent_council/plain_line.rb +24 -0
- data/lib/agent_council/platform.rb +2 -2
- data/lib/agent_council/progress_printer.rb +59 -0
- data/lib/agent_council/ps.rb +548 -0
- data/lib/agent_council/safe_stdout.rb +48 -0
- data/lib/agent_council/version.rb +1 -1
- data/lib/agent_council/worker_pool.rb +28 -15
- data/lib/agent_council.rb +167 -127
- metadata +17 -6
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# AgentCouncil::Ps — what agent-council-ps shows: each running
|
|
4
|
+
# agent-council process, its agents (the children it spawned, each leading
|
|
5
|
+
# a process group of its own; see WorkerPool), and the commands each agent
|
|
6
|
+
# is running, read from ps. It runs outside agent-council, so it sees only
|
|
7
|
+
# what the operating system shows: an agent's event stream goes to
|
|
8
|
+
# agent-council through a pipe and isn't visible here.
|
|
9
|
+
|
|
10
|
+
require 'io/console'
|
|
11
|
+
require 'open3'
|
|
12
|
+
require_relative 'harnesses'
|
|
13
|
+
require_relative 'plain_line'
|
|
14
|
+
require_relative 'platform'
|
|
15
|
+
|
|
16
|
+
module AgentCouncil
|
|
17
|
+
module Ps
|
|
18
|
+
class Error < StandardError; end
|
|
19
|
+
|
|
20
|
+
# A bad command line.
|
|
21
|
+
class UsageError < Error; end
|
|
22
|
+
|
|
23
|
+
# A process as ps lists it. elapsed and cpu are seconds. argv is its
|
|
24
|
+
# exact argument list where /proc gives it (see proc_argv), else nil;
|
|
25
|
+
# args is its command line as ps shows it, arguments joined by spaces.
|
|
26
|
+
Entry = Struct.new(:pid, :ppid, :pgid, :elapsed, :cpu, :rss_kb, :args, :argv) do
|
|
27
|
+
def tokens = argv || args.split
|
|
28
|
+
def command_name = File.basename(tokens.first.to_s)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# pid, ppid, pgid, elapsed time, CPU time, RSS (KiB), command line. -ww
|
|
32
|
+
# keeps macOS's ps from cutting the command line at the terminal's width.
|
|
33
|
+
PS_ARGV = %w[ps -ww -A -o pid=,ppid=,pgid=,etime=,time=,rss=,args=].freeze
|
|
34
|
+
|
|
35
|
+
# A line of PS_ARGV's output: pid, ppid, pgid, etime, time, rss, and
|
|
36
|
+
# the command line (which may be empty).
|
|
37
|
+
ENTRY_LINE = /\A\s*(\d+)\s+(\d+)\s+(\d+)\s+([\d:.-]+)\s+([\d:.-]+)\s+(\d+)(?:\s+(.*))?\z/
|
|
38
|
+
|
|
39
|
+
# Ruby interpreters agent-council may run under, by command name: MRI
|
|
40
|
+
# (possibly versioned, e.g. ruby3.4), or JRuby, whose launcher runs java.
|
|
41
|
+
RUBY = /\A(ruby[\d.]*|jruby)\z/
|
|
42
|
+
# JRuby's main class: org.jruby.main.Main from 10.1, org.jruby.Main
|
|
43
|
+
# before.
|
|
44
|
+
JRUBY_MAINS = %w[org.jruby.main.Main org.jruby.Main].freeze
|
|
45
|
+
|
|
46
|
+
# RVM's executable-hooks wrapper, which runs installed gems' commands
|
|
47
|
+
# under RVM: ruby_executable_hooks, or under JRuby,
|
|
48
|
+
# jruby_executable_hooks.
|
|
49
|
+
EXECUTABLE_HOOKS = /\A\w*_executable_hooks\z/
|
|
50
|
+
|
|
51
|
+
# Ruby options whose argument can be the next word (MRI's, and the
|
|
52
|
+
# ones JRuby shares with it), so that word isn't taken for the script.
|
|
53
|
+
RUBY_OPTIONS_WITH_ARGUMENT = %w[
|
|
54
|
+
-C -E -I -r --backtrace-limit --crash-report --disable --dump --enable --encoding
|
|
55
|
+
--external-encoding --internal-encoding --parser
|
|
56
|
+
].freeze
|
|
57
|
+
|
|
58
|
+
# Bundler's command, by name: bundle or bundler, possibly versioned
|
|
59
|
+
# (e.g. bundle3.4), as RUBY allows ruby3.4.
|
|
60
|
+
BUNDLE = /\Abundler?[\d.]*\z/
|
|
61
|
+
# The word that runs a given version of a gem's command, as in
|
|
62
|
+
# `bundle _4.0.19_ exec`.
|
|
63
|
+
GEM_VERSION_SELECTOR = /\A_\S+_\z/
|
|
64
|
+
|
|
65
|
+
# What the report says when it finds no run to show: none at all, or
|
|
66
|
+
# none with the pids asked for.
|
|
67
|
+
module_function def no_runs_line(pids)
|
|
68
|
+
return 'No running agent-council found.' if pids.empty?
|
|
69
|
+
|
|
70
|
+
"No running agent-council, or agent of one, has pid #{pids.join(' or ')}."
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
DEFAULT_WATCH_SECONDS = 5
|
|
74
|
+
|
|
75
|
+
HELP = <<~HELP.freeze
|
|
76
|
+
Usage: agent-council-ps [-w[SECONDS] | --watch[=SECONDS]] [PID ...]
|
|
77
|
+
|
|
78
|
+
Shows each running agent-council process, its agents, and the commands
|
|
79
|
+
each agent is running, so you can tell what an agent that's taking
|
|
80
|
+
longer than expected is doing. Each agent's pid is the one agent-council
|
|
81
|
+
printed as it started: under "Started Agents:", or for the consolidator,
|
|
82
|
+
under its "Consolidation Report" heading.
|
|
83
|
+
|
|
84
|
+
For each agent it shows its harness and, where the harness's command
|
|
85
|
+
line shows it, its model (not for pi, which hides its command line);
|
|
86
|
+
how long it has run; its CPU time and recent CPU use (an agent waiting
|
|
87
|
+
on its model uses little); its memory; and the commands and helper
|
|
88
|
+
processes (such as MCP servers) it started that are still running,
|
|
89
|
+
with how long each has run.
|
|
90
|
+
|
|
91
|
+
PID: show only the agent-council runs with this pid, or with an agent
|
|
92
|
+
with this pid (default: every run)
|
|
93
|
+
|
|
94
|
+
Options:
|
|
95
|
+
-w, --watch refresh every #{DEFAULT_WATCH_SECONDS} seconds until Ctrl-C; -wSECONDS
|
|
96
|
+
or --watch=SECONDS to choose the interval (e.g. -w30). The
|
|
97
|
+
interval must be attached: in "-w 30", 30 is a PID.
|
|
98
|
+
-h, --help show this help
|
|
99
|
+
|
|
100
|
+
The first list comes after a second, the time CPU use is sampled over.
|
|
101
|
+
|
|
102
|
+
To stop one agent and let the run go on: kill -TERM -PID
|
|
103
|
+
See RUNAWAY_AGENTS.md in the gem for more.
|
|
104
|
+
HELP
|
|
105
|
+
|
|
106
|
+
# Seconds in a ps etime or time value: [[DD-]HH:]MM:SS, with fractional
|
|
107
|
+
# seconds on macOS (whose time may also run past 59 minutes).
|
|
108
|
+
module_function def parse_duration(text)
|
|
109
|
+
text = text.to_s
|
|
110
|
+
days, clock = text.include?('-') ? text.split('-', 2) : [0, text]
|
|
111
|
+
clock.split(':').map(&:to_f).reduce(0) { |sum, part| (sum * 60) + part } + (days.to_i * 86_400)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Text from ps, lsof, or /proc as UTF-8, invalid bytes becoming U+FFFD,
|
|
115
|
+
# whatever the locale, so a command line with other bytes can't make
|
|
116
|
+
# parsing raise.
|
|
117
|
+
module_function def utf8(text) = String.new(text, encoding: Encoding::UTF_8).scrub
|
|
118
|
+
|
|
119
|
+
# The processes in ps output (see PS_ARGV), by pid. A line that isn't
|
|
120
|
+
# one (see ENTRY_LINE) continues the last one's command line, in case a
|
|
121
|
+
# ps prints a newline in one.
|
|
122
|
+
module_function def parse_ps(output)
|
|
123
|
+
entries = []
|
|
124
|
+
utf8(output).each_line(chomp: true) do |line|
|
|
125
|
+
unless (match = ENTRY_LINE.match(line))
|
|
126
|
+
entries.last.args += " #{line.strip}" if entries.last
|
|
127
|
+
next
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
pid, ppid, pgid, etime, time, rss, args = match.captures
|
|
131
|
+
entries << Entry.new(pid.to_i, ppid.to_i, pgid.to_i, parse_duration(etime), parse_duration(time),
|
|
132
|
+
rss.to_i, args.to_s)
|
|
133
|
+
end
|
|
134
|
+
entries.to_h { |entry| [entry.pid, entry] }
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
module_function def ps_snapshot
|
|
138
|
+
output, status = Open3.capture2(*PS_ARGV)
|
|
139
|
+
raise Error, "ps failed (#{status})" unless status.success?
|
|
140
|
+
|
|
141
|
+
processes = parse_ps(output)
|
|
142
|
+
processes.each_value do |process|
|
|
143
|
+
process.cpu = proc_cpu(process.pid) || process.cpu
|
|
144
|
+
process.argv = proc_argv(process.pid)
|
|
145
|
+
end
|
|
146
|
+
rescue SystemCallError => e
|
|
147
|
+
raise Error, "can't run ps: #{e.message}"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Linux's ps gives CPU time in whole seconds, too coarse for CPU use over
|
|
151
|
+
# a few seconds; /proc gives it in clock ticks. nil where there's no
|
|
152
|
+
# /proc, as on macOS, whose ps gives hundredths.
|
|
153
|
+
module_function def proc_cpu(pid)
|
|
154
|
+
ticks = parse_proc_stat(File.binread("/proc/#{pid}/stat"))
|
|
155
|
+
ticks && (ticks.to_f / clock_ticks)
|
|
156
|
+
rescue SystemCallError
|
|
157
|
+
nil
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# The CPU time (user plus system, in clock ticks) in the contents of a
|
|
161
|
+
# /proc/PID/stat, or nil if it can't be read from them. Those are fields
|
|
162
|
+
# 14 and 15; the command name (field 2) is in parentheses and may
|
|
163
|
+
# contain spaces and parentheses, so fields are counted from its last ")".
|
|
164
|
+
module_function def parse_proc_stat(stat)
|
|
165
|
+
close = stat.rindex(')') or return nil
|
|
166
|
+
utime, stime = stat[(close + 1)..].split[11, 2]
|
|
167
|
+
return nil unless [utime, stime].all? { |field| field&.match?(/\A\d+\z/) }
|
|
168
|
+
|
|
169
|
+
utime.to_i + stime.to_i
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# A process's exact arguments, from /proc/PID/cmdline, or nil where
|
|
173
|
+
# there's no /proc (as on macOS) or it's empty (as for a kernel
|
|
174
|
+
# thread or a zombie). ps joins the arguments with spaces, so a path
|
|
175
|
+
# containing one would otherwise be split.
|
|
176
|
+
module_function def proc_argv(pid)
|
|
177
|
+
argv = utf8(File.binread("/proc/#{pid}/cmdline")).split("\0")
|
|
178
|
+
argv unless argv.empty?
|
|
179
|
+
rescue SystemCallError
|
|
180
|
+
nil
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
module_function def clock_ticks
|
|
184
|
+
@clock_ticks ||= begin
|
|
185
|
+
require 'etc'
|
|
186
|
+
Etc.sysconf(Etc::SC_CLK_TCK) || 100
|
|
187
|
+
rescue StandardError, NotImplementedError
|
|
188
|
+
100
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Whether process is agent-council: a Ruby interpreter running the
|
|
193
|
+
# agent-council script (as its shebang or a binstub runs it), or under
|
|
194
|
+
# JRuby, java running JRuby's main class with it; under either, it may
|
|
195
|
+
# run it through `bundle exec`. Only the script is checked, a word whose
|
|
196
|
+
# basename is exactly agent-council, so a process merely mentioning
|
|
197
|
+
# agent-council, such as an agent whose system prompt names it, or
|
|
198
|
+
# agent-council-ps itself, isn't taken for it.
|
|
199
|
+
#
|
|
200
|
+
# Under MRI, `bundle exec` loads the script into Bundler's own process
|
|
201
|
+
# and retitles it with the script's path and arguments, so a process
|
|
202
|
+
# whose command starts with the script's path is one too; see
|
|
203
|
+
# bundler_title?. RVM's executable-hooks wrapper, which runs installed
|
|
204
|
+
# gems' commands under RVM, retitles its process too, as "ruby",
|
|
205
|
+
# the script's path, and its arguments; under JRuby, it's shown as
|
|
206
|
+
# run (see ruby_script_index).
|
|
207
|
+
#
|
|
208
|
+
# A retitled process's exact arguments (from /proc, as on Linux) are
|
|
209
|
+
# its title, one string, which is split at spaces. Where there's no
|
|
210
|
+
# /proc, as on macOS, or it can't be read (argv is nil), the words are
|
|
211
|
+
# ps's command line split at spaces. In either case, a script whose
|
|
212
|
+
# path has a space in it isn't found; only a process's exact
|
|
213
|
+
# arguments, not retitled, keep it whole. Without /proc, any command
|
|
214
|
+
# starting with a path whose basename is agent-council is taken for a
|
|
215
|
+
# run Bundler retitled. cwd: gives a process's working directory (see
|
|
216
|
+
# bundler_title?).
|
|
217
|
+
module_function def agent_council?(process, cwd: method(:process_cwd))
|
|
218
|
+
return true if process.argv && bundler_title?(process, cwd)
|
|
219
|
+
|
|
220
|
+
tokens = process.argv&.size == 1 ? process.argv.first.split : process.tokens
|
|
221
|
+
command = File.basename(tokens.first.to_s)
|
|
222
|
+
offset =
|
|
223
|
+
if RUBY.match?(command)
|
|
224
|
+
1
|
|
225
|
+
elsif command == 'java' && (main = tokens.index { |token| JRUBY_MAINS.include?(token) })
|
|
226
|
+
main + 1
|
|
227
|
+
end
|
|
228
|
+
return process.argv.nil? && script?(tokens.first) unless offset
|
|
229
|
+
|
|
230
|
+
index = ruby_script_index(tokens.drop(offset))
|
|
231
|
+
!index.nil? && script?(tokens[offset + index])
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
module_function def script?(word) = File.basename(word.to_s) == 'agent-council'
|
|
235
|
+
|
|
236
|
+
# Whether a process's exact arguments (from /proc, as on Linux) are a
|
|
237
|
+
# title Bundler gave it: one string, starting with the path of an
|
|
238
|
+
# existing file named agent-council (relative to the process's working
|
|
239
|
+
# directory, if not absolute, so not if that can't be read, as for
|
|
240
|
+
# another user's process). It's split at spaces, so a script whose
|
|
241
|
+
# path has a space in it isn't found. The file must exist, so that,
|
|
242
|
+
# say, a script named `agent-council backup`, or a program at
|
|
243
|
+
# `.../agent-council work/bin/tool`, isn't taken for one, unless a file
|
|
244
|
+
# named agent-council is in the same directory as well (a limitation
|
|
245
|
+
# RUNAWAY_AGENTS.md documents).
|
|
246
|
+
module_function def bundler_title?(process, cwd)
|
|
247
|
+
path = process.argv.first.to_s.split.first
|
|
248
|
+
script?(path) && existing_file?(path, process, cwd)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# Whether path names an existing file, relative to the process's
|
|
252
|
+
# working directory if not absolute. A path that can't be expanded,
|
|
253
|
+
# such as one starting with "~" and an unknown user's name, doesn't.
|
|
254
|
+
module_function def existing_file?(path, process, cwd)
|
|
255
|
+
dir = cwd.call(process.pid) unless path.start_with?('/')
|
|
256
|
+
return false unless path.start_with?('/') || dir
|
|
257
|
+
|
|
258
|
+
File.file?(File.expand_path(path, dir || '/'))
|
|
259
|
+
rescue ArgumentError
|
|
260
|
+
false
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# The index in a Ruby interpreter's arguments of its script: the first
|
|
264
|
+
# word that isn't an option or an option's argument, or if that's
|
|
265
|
+
# bundle running exec (as JRuby shows `bundle exec`, not retitling its
|
|
266
|
+
# process), the script it runs (see bundle_exec_command_index); nil if
|
|
267
|
+
# there's none, or if -e gives the code to run instead. RVM's wrapper
|
|
268
|
+
# (see EXECUTABLE_HOOKS), which JRuby shows too, is passed over: the
|
|
269
|
+
# script is the word after it, or the one bundle exec runs.
|
|
270
|
+
module_function def ruby_script_index(args)
|
|
271
|
+
index = 0
|
|
272
|
+
while index < args.size
|
|
273
|
+
arg = args[index]
|
|
274
|
+
return (index + 1 if index + 1 < args.size) if arg == '--'
|
|
275
|
+
return nil if arg.start_with?('-e')
|
|
276
|
+
|
|
277
|
+
unless arg.start_with?('-')
|
|
278
|
+
# RVM's wrapper runs the script it's given.
|
|
279
|
+
index += 1 if EXECUTABLE_HOOKS.match?(File.basename(arg))
|
|
280
|
+
return nil unless index < args.size
|
|
281
|
+
return index unless (exec = bundle_exec_index(args, index))
|
|
282
|
+
|
|
283
|
+
return bundle_exec_command_index(args, exec + 1)
|
|
284
|
+
end
|
|
285
|
+
index += RUBY_OPTIONS_WITH_ARGUMENT.include?(arg) ? 2 : 1
|
|
286
|
+
end
|
|
287
|
+
nil
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# If args[index] is Bundler's command (see BUNDLE) running exec,
|
|
291
|
+
# possibly with a version first (see GEM_VERSION_SELECTOR), the index
|
|
292
|
+
# of exec; else nil.
|
|
293
|
+
module_function def bundle_exec_index(args, index)
|
|
294
|
+
return unless BUNDLE.match?(File.basename(args[index]))
|
|
295
|
+
|
|
296
|
+
index += 1 if GEM_VERSION_SELECTOR.match?(args[index + 1].to_s)
|
|
297
|
+
index + 1 if args[index + 1] == 'exec'
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# The index in args of the command `bundle exec` runs, the first word
|
|
301
|
+
# from index on that isn't one of exec's options (such as
|
|
302
|
+
# --keep-file-descriptors) or --gemfile's argument; nil if none.
|
|
303
|
+
module_function def bundle_exec_command_index(args, index)
|
|
304
|
+
while index < args.size
|
|
305
|
+
return index unless args[index].start_with?('-')
|
|
306
|
+
|
|
307
|
+
index += args[index] == '--gemfile' ? 2 : 1
|
|
308
|
+
end
|
|
309
|
+
nil
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
module_function def children(processes, pid)
|
|
313
|
+
processes.each_value.select { |process| process.ppid == pid }
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# Every process descended from pid, depth first, with its depth (1 for a
|
|
317
|
+
# child).
|
|
318
|
+
module_function def descendants(processes, pid, depth = 1)
|
|
319
|
+
children(processes, pid).sort_by(&:pid).flat_map do |child|
|
|
320
|
+
[[child, depth], *descendants(processes, child.pid, depth + 1)]
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# The harness an agent's command line runs (its NAME), or nil. A node
|
|
325
|
+
# harness's can show as "node /path/to/pi ...", so it's the first word
|
|
326
|
+
# naming one.
|
|
327
|
+
module_function def harness_name(process)
|
|
328
|
+
process.tokens.each do |token|
|
|
329
|
+
break if token.start_with?('-')
|
|
330
|
+
|
|
331
|
+
name = File.basename(token)
|
|
332
|
+
return name if Harnesses::BY_NAME.key?(name)
|
|
333
|
+
end
|
|
334
|
+
nil
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# "provider / model" from an agent's command line, as far as it shows
|
|
338
|
+
# them, read by its harness (see Harnesses::Harness.model_label), or,
|
|
339
|
+
# for a command line that names none (as a harness run some other way
|
|
340
|
+
# might not), by Harness's default. pi replaces its command line with
|
|
341
|
+
# "pi", so a pi agent's gives neither (and this returns "").
|
|
342
|
+
module_function def model_label(process)
|
|
343
|
+
harness = Harnesses::BY_NAME[harness_name(process)] || Harnesses::Harness
|
|
344
|
+
harness.model_label(process.tokens)
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
# The agent-council processes to show and their agents: [[council,
|
|
348
|
+
# [agent, ...]], ...]. An agent is a child leading its own process group.
|
|
349
|
+
# pids: only the runs that have one of these pids, or an agent with one
|
|
350
|
+
# (all if empty). cwd: as for agent_council?; it's called at most once
|
|
351
|
+
# per process.
|
|
352
|
+
module_function def runs(processes, pids: [], cwd: method(:process_cwd))
|
|
353
|
+
cwd = memoized(cwd)
|
|
354
|
+
result = processes.each_value.select do |process|
|
|
355
|
+
agent_council?(process, cwd: cwd)
|
|
356
|
+
end.sort_by(&:pid).map do |council|
|
|
357
|
+
[council, children(processes, council.pid).select { |child| child.pgid == child.pid }.sort_by(&:pid)]
|
|
358
|
+
end
|
|
359
|
+
return result if pids.empty?
|
|
360
|
+
|
|
361
|
+
result.select { |council, agents| [council, *agents].map(&:pid).intersect?(pids) }
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# A callable's results, by argument; see memoized.
|
|
365
|
+
Memo = Struct.new(:callable, :results) do
|
|
366
|
+
def call(arg) = results.fetch(arg) { results[arg] = callable.call(arg) }
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# callable, called at most once per argument; callable itself if it's
|
|
370
|
+
# already memoized, so that passing one on doesn't wrap it again.
|
|
371
|
+
module_function def memoized(callable)
|
|
372
|
+
callable.is_a?(Memo) ? callable : Memo.new(callable, {})
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# HH:MM:SS, rounded to the second, as agent-council prints durations
|
|
376
|
+
# (hours may exceed 23).
|
|
377
|
+
module_function def format_duration(seconds)
|
|
378
|
+
seconds = seconds.round
|
|
379
|
+
format('%02d:%02d:%02d', seconds / 3600, (seconds % 3600) / 60, seconds % 60)
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
module_function def format_rss(kib)
|
|
383
|
+
kib >= 1024 * 1024 ? format('%.1f GB', kib / 1024.0 / 1024) : "#{kib / 1024} MB"
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
# CPU use since previous (an earlier snapshot), as a percentage of one
|
|
387
|
+
# core, or nil if the process wasn't in it (or was another process with
|
|
388
|
+
# the same pid).
|
|
389
|
+
module_function def cpu_percent(process, previous, interval)
|
|
390
|
+
before = previous&.[](process.pid)
|
|
391
|
+
return nil if before.nil? || interval <= 0 || before.elapsed > process.elapsed
|
|
392
|
+
|
|
393
|
+
((process.cpu - before.cpu) / interval * 100).clamp(0, Float::INFINITY).round
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
# The report's lines for one snapshot. previous and interval (seconds
|
|
397
|
+
# between them) give recent CPU use; width cuts command lines.
|
|
398
|
+
# selected: the runs to show, by default those runs finds.
|
|
399
|
+
module_function def report(processes, pids: [], previous: nil, interval: 0, width: 100,
|
|
400
|
+
cwd: method(:process_cwd), selected: nil)
|
|
401
|
+
cwd = memoized(cwd)
|
|
402
|
+
selected ||= runs(processes, pids: pids, cwd: cwd)
|
|
403
|
+
return [no_runs_line(pids)] if selected.empty?
|
|
404
|
+
|
|
405
|
+
selected.flat_map do |council, agents|
|
|
406
|
+
run_lines(processes, council, agents, previous, interval, width, cwd.call(council.pid))
|
|
407
|
+
end
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
module_function def run_lines(processes, council, agents, previous, interval, width, dir)
|
|
411
|
+
heading = "agent-council pid #{council.pid}, running #{format_duration(council.elapsed)}"
|
|
412
|
+
heading += ", in #{AgentCouncil.plain_line(dir)}" if dir
|
|
413
|
+
lines = [heading]
|
|
414
|
+
lines << ' (no agents running)' if agents.empty?
|
|
415
|
+
agents.each do |agent|
|
|
416
|
+
lines << agent_line(agent, previous, interval)
|
|
417
|
+
lines.concat(command_lines(processes, agent, width))
|
|
418
|
+
end
|
|
419
|
+
lines << ''
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
module_function def agent_line(agent, previous, interval)
|
|
423
|
+
parts = [harness_name(agent) || agent.command_name, model_label(agent)]
|
|
424
|
+
name = parts.map { |part| AgentCouncil.plain_line(part) }.reject(&:empty?).join(' ')
|
|
425
|
+
cpu = "cpu #{format_duration(agent.cpu)}"
|
|
426
|
+
percent = cpu_percent(agent, previous, interval)
|
|
427
|
+
cpu += " (#{percent}% now)" if percent
|
|
428
|
+
" - pid #{agent.pid} #{name} running #{format_duration(agent.elapsed)} #{cpu} " \
|
|
429
|
+
"#{format_rss(agent.rss_kb)}"
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
# The commands the agent started that are still running, by the
|
|
433
|
+
# process tree (its own commands may be in other process groups).
|
|
434
|
+
module_function def command_lines(processes, agent, width)
|
|
435
|
+
descendants(processes, agent.pid).map do |process, depth|
|
|
436
|
+
prefix = "#{' ' * depth} #{process.pid} #{format_duration(process.elapsed)} "
|
|
437
|
+
command = AgentCouncil.plain_line(process.args)
|
|
438
|
+
room = [width - prefix.size, 20].max
|
|
439
|
+
command = "#{command[0, room - 1]}…" if command.size > room
|
|
440
|
+
"#{prefix}#{command}"
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
# A process's working directory, where agent-council puts its reports
|
|
445
|
+
# by default, or nil if it can't be read.
|
|
446
|
+
module_function def process_cwd(pid)
|
|
447
|
+
link = "/proc/#{pid}/cwd"
|
|
448
|
+
return utf8(File.readlink(link)) if File.symlink?(link)
|
|
449
|
+
|
|
450
|
+
output, status = Open3.capture2('lsof', '-a', '-p', pid.to_s, '-d', 'cwd', '-Fn', err: File::NULL)
|
|
451
|
+
utf8(output)[/^n(.+)$/, 1] if status.success?
|
|
452
|
+
rescue SystemCallError
|
|
453
|
+
nil
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
# Parses the command line: [watch_seconds or nil, pids], or raises
|
|
457
|
+
# UsageError. A watch interval must be attached (-w30, --watch=30),
|
|
458
|
+
# so that in "-w 48212" the number is a pid.
|
|
459
|
+
module_function def parse_args(argv)
|
|
460
|
+
watch = nil
|
|
461
|
+
pids = []
|
|
462
|
+
argv.each do |arg|
|
|
463
|
+
case arg
|
|
464
|
+
when '-w', '--watch' then watch = DEFAULT_WATCH_SECONDS
|
|
465
|
+
when /\A(?:-w|--watch=)(.*)\z/m then watch = parse_interval(::Regexp.last_match(1))
|
|
466
|
+
when /\A\d+\z/ then pids << arg.to_i
|
|
467
|
+
else raise UsageError, "unknown argument: #{arg}"
|
|
468
|
+
end
|
|
469
|
+
end
|
|
470
|
+
[watch, pids]
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
module_function def parse_interval(text)
|
|
474
|
+
seconds = Float(text, exception: false) if text.match?(/\A(\d+(\.\d*)?|\.\d+)\z/)
|
|
475
|
+
raise UsageError, "the watch interval must be a number of seconds, not #{text.inspect}" unless seconds
|
|
476
|
+
raise UsageError, 'the watch interval must be more than 0' unless seconds.positive?
|
|
477
|
+
|
|
478
|
+
seconds
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
module_function def terminal_width(out)
|
|
482
|
+
columns = out.winsize[1] if out.tty? && out.respond_to?(:winsize)
|
|
483
|
+
columns&.positive? ? columns : 100
|
|
484
|
+
rescue SystemCallError
|
|
485
|
+
100
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
# Prints one snapshot, with CPU use over a second, and returns the exit
|
|
489
|
+
# status: 0, or 1 if no run was found (or none with the pids asked
|
|
490
|
+
# for). Watching, it prints the first after that second too, then one
|
|
491
|
+
# every watch seconds, with CPU use since the last, until interrupted
|
|
492
|
+
# (Interrupt is raised to the caller). snapshot, pause, and cwd are
|
|
493
|
+
# seams for the specs.
|
|
494
|
+
module_function def run(argv, out: $stdout, snapshot: method(:ps_snapshot), pause: method(:sleep),
|
|
495
|
+
cwd: method(:process_cwd))
|
|
496
|
+
watch, pids = parse_args(argv)
|
|
497
|
+
previous = snapshot.call
|
|
498
|
+
previous_at = monotonic_now
|
|
499
|
+
pause.call(1)
|
|
500
|
+
loop do
|
|
501
|
+
processes = snapshot.call
|
|
502
|
+
now = monotonic_now
|
|
503
|
+
snapshot_cwd = memoized(cwd)
|
|
504
|
+
selected = runs(processes, pids: pids, cwd: snapshot_cwd)
|
|
505
|
+
lines = report(processes, pids: pids, previous: previous, interval: now - previous_at,
|
|
506
|
+
width: terminal_width(out), cwd: snapshot_cwd, selected: selected)
|
|
507
|
+
if watch
|
|
508
|
+
out.print("\e[H\e[2J") if out.tty?
|
|
509
|
+
out.puts("#{Time.now.strftime('%H:%M:%S')}, every #{format('%g', watch)}s; Ctrl-C to stop")
|
|
510
|
+
end
|
|
511
|
+
out.puts(lines)
|
|
512
|
+
out.flush
|
|
513
|
+
return selected.empty? ? 1 : 0 unless watch
|
|
514
|
+
|
|
515
|
+
previous = processes
|
|
516
|
+
previous_at = now
|
|
517
|
+
pause.call(watch)
|
|
518
|
+
end
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
# The agent-council-ps command (exe/agent-council-ps). Prints its help and
|
|
522
|
+
# exits 0, or exits with #run's status; exits 2 on a bad command line, 1
|
|
523
|
+
# on another Error (ps failing) or on native Windows, and 0 when
|
|
524
|
+
# interrupted, as Ctrl-C ends watching.
|
|
525
|
+
module_function def main(argv)
|
|
526
|
+
AgentCouncil.abort_if_native_windows('agent-council-ps')
|
|
527
|
+
if argv.include?('-h') || argv.include?('--help')
|
|
528
|
+
puts HELP
|
|
529
|
+
exit
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
begin
|
|
533
|
+
exit run(argv)
|
|
534
|
+
rescue UsageError => e
|
|
535
|
+
warn "agent-council-ps: #{e.message}"
|
|
536
|
+
warn "Try 'agent-council-ps --help' for usage."
|
|
537
|
+
exit 2
|
|
538
|
+
rescue Error => e
|
|
539
|
+
warn "agent-council-ps: #{e.message}"
|
|
540
|
+
exit 1
|
|
541
|
+
rescue Interrupt
|
|
542
|
+
exit 0
|
|
543
|
+
end
|
|
544
|
+
end
|
|
545
|
+
|
|
546
|
+
module_function def monotonic_now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
547
|
+
end
|
|
548
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# AgentCouncil::SafeStdout — what main sets $stdout to, so that output that
|
|
4
|
+
# can't be written doesn't end a run.
|
|
5
|
+
|
|
6
|
+
module AgentCouncil
|
|
7
|
+
# Wraps an IO, passing output and flushes through to it until one fails
|
|
8
|
+
# (EPIPE once a pipe's reader has gone, as with `| head`, or a hung-up
|
|
9
|
+
# terminal), then discarding output from then on. Raising would end
|
|
10
|
+
# agent-council, losing run.json and the consolidation and leaving the
|
|
11
|
+
# agents, in process groups of their own, running. Every write is
|
|
12
|
+
# covered: MRI's buffered ones fail on a later flush (including the one
|
|
13
|
+
# it makes before spawning a process, which calls flush here), JRuby's
|
|
14
|
+
# unbuffered ones on the write itself. Each method is the IO's own, so
|
|
15
|
+
# behaves as it does (encoding included). No lock: a trap can flush, and
|
|
16
|
+
# concurrent failures at worst discard into two sinks.
|
|
17
|
+
class SafeStdout
|
|
18
|
+
def initialize(io)
|
|
19
|
+
@io = io
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
%i[write puts print printf flush].each do |name|
|
|
23
|
+
define_method(name) do |*args|
|
|
24
|
+
result = @io.public_send(name, *args)
|
|
25
|
+
{ write: result, flush: self }[name] # as IO's: puts, print, and printf return nil
|
|
26
|
+
rescue IOError, SystemCallError
|
|
27
|
+
@io = File.open(File::NULL, 'w')
|
|
28
|
+
retry
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def <<(object)
|
|
33
|
+
write(object.to_s)
|
|
34
|
+
self
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def sync = @io.sync
|
|
38
|
+
|
|
39
|
+
def sync=(value)
|
|
40
|
+
@io.sync = value
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def tty? = @io.tty?
|
|
44
|
+
alias isatty tty?
|
|
45
|
+
|
|
46
|
+
def fileno = @io.fileno
|
|
47
|
+
end
|
|
48
|
+
end
|