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
|
@@ -15,9 +15,9 @@ module AgentCouncil
|
|
|
15
15
|
# the grace period for the workers to finish, and SIGKILLs agents still
|
|
16
16
|
# running. The commands an agent's shell tool started run in process
|
|
17
17
|
# groups of their own, which these signals don't reach; pi, Claude
|
|
18
|
-
# Code, and
|
|
19
|
-
# they may keep running. A second
|
|
20
|
-
# than HUP SIGKILLs them at once. Any HUP after the first signal is
|
|
18
|
+
# Code, Codex, and Antigravity stop them on SIGTERM, but OpenCode doesn't, and an
|
|
19
|
+
# agent that is SIGKILLed can't, so they may keep running. A second
|
|
20
|
+
# trapped signal other than HUP SIGKILLs them at once. Any HUP after the first signal is
|
|
21
21
|
# ignored: a closing terminal can deliver two at once (one forwarded by the
|
|
22
22
|
# shell, one from the kernel), which must not skip the graceful stop. A
|
|
23
23
|
# signal agent-council was started ignoring (e.g. HUP under nohup) stays
|
|
@@ -25,9 +25,9 @@ module AgentCouncil
|
|
|
25
25
|
# .exit_by_signal).
|
|
26
26
|
#
|
|
27
27
|
# Agents run in worker threads, never the main thread, so the main thread
|
|
28
|
-
# is always free to take the Interrupt. Workers report
|
|
29
|
-
# a queue and only the main thread
|
|
30
|
-
# the Interrupt has been taken.
|
|
28
|
+
# is always free to take the Interrupt. Workers report each agent's spawn
|
|
29
|
+
# and their own completion through a queue and only the main thread
|
|
30
|
+
# prints, so nothing more is printed once the Interrupt has been taken.
|
|
31
31
|
class WorkerPool
|
|
32
32
|
# Not QUIT on JRuby: the JVM reserves it for thread dumps, so trapping
|
|
33
33
|
# it only prints a warning.
|
|
@@ -53,7 +53,9 @@ module AgentCouncil
|
|
|
53
53
|
def initialize(grace:)
|
|
54
54
|
@grace = grace
|
|
55
55
|
@threads = []
|
|
56
|
-
|
|
56
|
+
# As they happen: [:spawned, tag, pid] when an agent is spawned
|
|
57
|
+
# (see #capture3) and [:finished, index] when a thread finishes.
|
|
58
|
+
@events = Queue.new
|
|
57
59
|
@uncollected = 0 # threads not yet #collect'ed
|
|
58
60
|
@children = {} # agent pids (each also its process group id)
|
|
59
61
|
@children_lock = Mutex.new
|
|
@@ -94,20 +96,27 @@ module AgentCouncil
|
|
|
94
96
|
Thread.current.report_on_exception = false
|
|
95
97
|
Thread.handle_interrupt(Interrupt => :immediate, &block)
|
|
96
98
|
ensure
|
|
97
|
-
@
|
|
99
|
+
@events << [:finished, index]
|
|
98
100
|
end
|
|
99
101
|
end
|
|
100
102
|
end
|
|
101
103
|
|
|
102
104
|
# Waits for the threads started since the last call, yielding each
|
|
103
105
|
# one's value as it finishes. Returns their values in start order.
|
|
104
|
-
# Re-raises if a worker raised.
|
|
105
|
-
|
|
106
|
+
# Re-raises if a worker raised. on_spawn: called with the tag and pid
|
|
107
|
+
# of each agent the threads spawn (see #capture3), as it's spawned.
|
|
108
|
+
def collect(on_spawn: nil)
|
|
106
109
|
count = @uncollected
|
|
107
110
|
@uncollected = 0
|
|
108
|
-
finished =
|
|
109
|
-
|
|
110
|
-
|
|
111
|
+
finished = []
|
|
112
|
+
while finished.size < count
|
|
113
|
+
kind, *args = @events.pop
|
|
114
|
+
if kind == :spawned
|
|
115
|
+
on_spawn&.call(*args)
|
|
116
|
+
else
|
|
117
|
+
index = args.first
|
|
118
|
+
finished << [index, @threads[index].value.tap { |value| yield value if block_given? }]
|
|
119
|
+
end
|
|
111
120
|
end
|
|
112
121
|
finished.sort_by(&:first).map(&:last)
|
|
113
122
|
end
|
|
@@ -115,8 +124,10 @@ module AgentCouncil
|
|
|
115
124
|
# Open3.capture3, but with the agent in its own process group and
|
|
116
125
|
# tracked for #shutdown while it runs, and its output read as UTF-8
|
|
117
126
|
# whatever the locale. An agent spawned after shutdown has started
|
|
118
|
-
# signalling gets the same signal at once.
|
|
119
|
-
|
|
127
|
+
# signalling gets the same signal at once. tag: identifies the agent
|
|
128
|
+
# to #collect's on_spawn, which gets it with the agent's pid (an
|
|
129
|
+
# untagged agent isn't reported).
|
|
130
|
+
def capture3(env, *argv, stdin_data:, tag: nil)
|
|
120
131
|
Open3.popen3(env, *argv, **SPAWN_OPTIONS) do |stdin, stdout, stderr, wait_thr|
|
|
121
132
|
[stdout, stderr].each { |io| io.set_encoding(Encoding::UTF_8) }
|
|
122
133
|
pid = wait_thr.pid
|
|
@@ -128,6 +139,7 @@ module AgentCouncil
|
|
|
128
139
|
@stop_signal
|
|
129
140
|
end
|
|
130
141
|
signal_child(pid, signal) if signal
|
|
142
|
+
@events << [:spawned, tag, pid] if tag
|
|
131
143
|
out_reader = Thread.new { stdout.read }
|
|
132
144
|
err_reader = Thread.new { stderr.read }
|
|
133
145
|
begin
|
|
@@ -198,6 +210,7 @@ module AgentCouncil
|
|
|
198
210
|
end
|
|
199
211
|
|
|
200
212
|
# A hung-up terminal can make writing fail; that mustn't stop shutdown.
|
|
213
|
+
# (main's SafeStdout doesn't raise, but a $stdout set otherwise may.)
|
|
201
214
|
private def flush_stdout
|
|
202
215
|
$stdout.flush
|
|
203
216
|
rescue IOError, SystemCallError
|
data/lib/agent_council.rb
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
# layout, per-agent subprocess execution, and the CLI entry point.
|
|
5
5
|
# Harness-specific behavior lives under AgentCouncil::Harnesses (e.g.
|
|
6
6
|
# AgentCouncil::Harnesses::PiHarness), looked up per agent from its config
|
|
7
|
-
# entry's 'harness' key with Harnesses.
|
|
7
|
+
# entry's 'harness' key with Harnesses.class_for. A harness's
|
|
8
8
|
# usage(stdout) returns token counts in the shape documented on
|
|
9
|
-
#
|
|
9
|
+
# Harnesses::Harness.usage, or nil when the harness reports none.
|
|
10
10
|
|
|
11
11
|
require 'json'
|
|
12
12
|
require 'yaml'
|
|
@@ -24,6 +24,9 @@ require_relative 'agent_council/version'
|
|
|
24
24
|
require_relative 'agent_council/platform'
|
|
25
25
|
require_relative 'agent_council/harnesses'
|
|
26
26
|
require_relative 'agent_council/worker_pool'
|
|
27
|
+
require_relative 'agent_council/progress_printer'
|
|
28
|
+
require_relative 'agent_council/safe_stdout'
|
|
29
|
+
require_relative 'agent_council/plain_line'
|
|
27
30
|
|
|
28
31
|
module AgentCouncil
|
|
29
32
|
# Runtime data files (prompt templates) ship inside lib/, resolved
|
|
@@ -131,22 +134,20 @@ module AgentCouncil
|
|
|
131
134
|
end
|
|
132
135
|
|
|
133
136
|
# The variables set for an agent's process: the scratch dir as its
|
|
134
|
-
# TMPDIR, which the scaffolding tells agents to use, and
|
|
135
|
-
#
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
# shared and never cleaned up. pi and Codex ignore it.
|
|
139
|
-
module_function def agent_env(scratch_dir)
|
|
140
|
-
{ 'TMPDIR' => scratch_dir, 'CLAUDE_CODE_TMPDIR' => scratch_dir }
|
|
137
|
+
# TMPDIR, which the scaffolding tells agents to use, and whatever its
|
|
138
|
+
# harness adds (see Harnesses::Harness.env).
|
|
139
|
+
module_function def agent_env(scratch_dir, harness)
|
|
140
|
+
harness.env(scratch_dir).merge('TMPDIR' => scratch_dir)
|
|
141
141
|
end
|
|
142
142
|
|
|
143
143
|
# Returns [label, ok, detail, usage, warnings]; never raises. detail is
|
|
144
144
|
# the report's filename on success, the error otherwise. usage is the
|
|
145
|
-
# agent's token usage (see Harnesses), or nil; failed
|
|
146
|
-
# since their tokens were still spent. warnings:
|
|
147
|
-
# about the agent (see run_warnings), failed or
|
|
148
|
-
# kept the agent from doing may be why it
|
|
149
|
-
# never ran. Returned, not printed, since
|
|
145
|
+
# agent's token usage (see Harnesses::Harness.usage), or nil; failed
|
|
146
|
+
# agents report it too, since their tokens were still spent. warnings:
|
|
147
|
+
# the harness's warnings about the agent (see run_warnings), failed or
|
|
148
|
+
# not, since what the harness kept the agent from doing may be why it
|
|
149
|
+
# failed; empty if the harness never ran. Returned, not printed, since
|
|
150
|
+
# this runs in a worker thread.
|
|
150
151
|
# index: the agent's 1-based position in the config's agent list, used to
|
|
151
152
|
# disambiguate repeated provider/model entries in the filename and label;
|
|
152
153
|
# filename: overrides the output name; meta: becomes YAML front matter,
|
|
@@ -157,7 +158,8 @@ module AgentCouncil
|
|
|
157
158
|
# and returning it). If the response begins
|
|
158
159
|
# with an H1 of its own, it is dropped: the header leads the body (and
|
|
159
160
|
# carries the title, in consolidated reports).
|
|
160
|
-
# pool: the WorkerPool to spawn the agent through, so Ctrl-C can stop it
|
|
161
|
+
# pool: the WorkerPool to spawn the agent through, so Ctrl-C can stop it
|
|
162
|
+
# (and its #collect can report the agent's pid, tagged with its label);
|
|
161
163
|
# without one it's spawned untracked.
|
|
162
164
|
module_function def run_agent(agent, prompt, run_dir, file_prefix, index: nil, filename: nil, meta: {},
|
|
163
165
|
header: nil, pool: nil)
|
|
@@ -173,11 +175,17 @@ module AgentCouncil
|
|
|
173
175
|
# with errors ignored: a child that is still exiting (e.g. one just
|
|
174
176
|
# killed after the Ctrl-C grace period) can still be writing here
|
|
175
177
|
# (node's compile cache, say). That must not surface as an error.
|
|
176
|
-
harness = Harnesses.
|
|
178
|
+
harness = Harnesses.class_for(agent)
|
|
177
179
|
scratch_dir = Dir.mktmpdir('agent-council-')
|
|
178
180
|
argv = harness.argv(provider, model)
|
|
181
|
+
env = agent_env(scratch_dir, harness)
|
|
182
|
+
stdin = harness.stdin_data(prompt)
|
|
179
183
|
(stdout, stderr, status), elapsed = timed do
|
|
180
|
-
|
|
184
|
+
if pool
|
|
185
|
+
pool.capture3(env, *argv, stdin_data: stdin, tag: label)
|
|
186
|
+
else
|
|
187
|
+
Open3.capture3(env, *argv, stdin_data: stdin)
|
|
188
|
+
end
|
|
181
189
|
end
|
|
182
190
|
# Harness output is UTF-8. WorkerPool#capture3 already returns it as such,
|
|
183
191
|
# but Open3.capture3 (used without a pool) returns the locale's encoding.
|
|
@@ -194,7 +202,10 @@ module AgentCouncil
|
|
|
194
202
|
|
|
195
203
|
unless status.success?
|
|
196
204
|
detail = failure_reason(harness.exit_detail(stdout, stderr))
|
|
197
|
-
|
|
205
|
+
# A signal is reason enough; "no error output" would add nothing.
|
|
206
|
+
detail = nil if status.signaled? && detail == Harnesses::Harness::NO_ERROR_OUTPUT
|
|
207
|
+
reason = [exit_description(status), detail]
|
|
208
|
+
return [label, false, "#{harness::NAME} #{reason.compact.join(': ')}", usage, warnings]
|
|
198
209
|
end
|
|
199
210
|
|
|
200
211
|
error = harness.error_message(stdout)
|
|
@@ -237,10 +248,10 @@ module AgentCouncil
|
|
|
237
248
|
|
|
238
249
|
# The harness's warnings about an agent: what the harness kept it
|
|
239
250
|
# from doing, or errors it hit without failing (see
|
|
240
|
-
# ClaudeCodeHarness.warnings
|
|
241
|
-
#
|
|
242
|
-
#
|
|
243
|
-
# too.
|
|
251
|
+
# ClaudeCodeHarness.warnings, CodexHarness.warnings,
|
|
252
|
+
# OpenCodeHarness.warnings, and AntigravityHarness.warnings), which a report
|
|
253
|
+
# may lack, or which may be why the agent failed. Advisory only: failing to read them must not
|
|
254
|
+
# fail an agent whose report is saved, so that becomes a warning too.
|
|
244
255
|
# Each is a plain_line, so wherever it goes (console, report, run.json,
|
|
245
256
|
# and so anything that prints run.json's values) it can't carry a
|
|
246
257
|
# terminal escape from a command the model wrote.
|
|
@@ -253,38 +264,21 @@ module AgentCouncil
|
|
|
253
264
|
|
|
254
265
|
# A harness's reason for a failure, as run_agent reports it: a
|
|
255
266
|
# plain_line, since it can quote the model or the CLI, cut to 300
|
|
256
|
-
# characters plus "..." so it doesn't swamp the
|
|
257
|
-
# list, or the consolidation prompt;
|
|
267
|
+
# characters plus "..." so it doesn't swamp the finish line, the Errors
|
|
268
|
+
# list, or the consolidation prompt; Harnesses::Harness::NO_ERROR_OUTPUT if
|
|
269
|
+
# nothing is left.
|
|
258
270
|
module_function def failure_reason(text)
|
|
259
271
|
reason = plain_line(text)
|
|
260
|
-
return
|
|
272
|
+
return Harnesses::Harness::NO_ERROR_OUTPUT if reason.empty?
|
|
261
273
|
|
|
262
274
|
reason.length > 300 ? "#{reason[0, 300]}..." : reason
|
|
263
275
|
end
|
|
264
276
|
|
|
265
|
-
# Lines showing an agent's warnings under its
|
|
277
|
+
# Lines showing an agent's warnings under its finish line or result table.
|
|
266
278
|
module_function def warning_lines(warnings)
|
|
267
279
|
Array(warnings).map { |warning| " Warning: #{plain_line(warning)}" }
|
|
268
280
|
end
|
|
269
281
|
|
|
270
|
-
# Text on one line with no control characters: every run of whitespace
|
|
271
|
-
# or control characters (newlines, carriage returns, the ESC that starts
|
|
272
|
-
# a terminal escape sequence) becomes one space; the rest of an escape
|
|
273
|
-
# sequence, such as "[2J", stays as harmless text. Warnings can quote a
|
|
274
|
-
# model's refused command, so this keeps one from moving the cursor or
|
|
275
|
-
# clearing the screen when printed, or from breaking the line it appears
|
|
276
|
-
# on in a report. Text in any encoding is read as UTF-8 (a SystemCallError's
|
|
277
|
-
# message can be binary). Invisible format characters (Unicode category Cf, such
|
|
278
|
-
# as bidirectional overrides and zero-width spaces) are removed, so text
|
|
279
|
-
# can't display reordered or hide characters. Invalid bytes become U+FFFD,
|
|
280
|
-
# since this also runs in rescue clauses and must not raise.
|
|
281
|
-
module_function def plain_line(text)
|
|
282
|
-
# Alternation, not one bracket class: the two overlap, which ruby -w
|
|
283
|
-
# warns of.
|
|
284
|
-
String.new(text.to_s, encoding: ENCODING).scrub.gsub(/\p{Cf}/, '')
|
|
285
|
-
.gsub(/(?:[[:space:]]|[[:cntrl:]])+/, ' ').strip
|
|
286
|
-
end
|
|
287
|
-
|
|
288
282
|
# Creates a new run dir in day_dir and returns its name: base, or base-2,
|
|
289
283
|
# base-3, ... if that is taken. Dir.mkdir fails if the dir exists, so the
|
|
290
284
|
# claim is atomic: runs started in the same second (or whose local time
|
|
@@ -321,6 +315,10 @@ module AgentCouncil
|
|
|
321
315
|
|
|
322
316
|
# --- report front matter ---
|
|
323
317
|
|
|
318
|
+
# A report's YAML front matter block, with the YAML itself captured, and
|
|
319
|
+
# the blank line after it, if any.
|
|
320
|
+
FRONT_MATTER_REGEX = /\A---\n(.+?)\n---\n\n?/m
|
|
321
|
+
|
|
324
322
|
module_function def with_front_matter(fields, body)
|
|
325
323
|
header = YAML.dump(fields.transform_keys(&:to_s)).delete_prefix("---\n").chomp
|
|
326
324
|
# Documented format: ISO 8601 timestamps, with or without a UTC offset,
|
|
@@ -339,7 +337,7 @@ module AgentCouncil
|
|
|
339
337
|
# [fields, body] from a report file, or [nil, nil] if it has no YAML
|
|
340
338
|
# front matter block (or the block cannot be safe-loaded).
|
|
341
339
|
module_function def parse_front_matter(text)
|
|
342
|
-
match = text.match(
|
|
340
|
+
match = text.match(FRONT_MATTER_REGEX)
|
|
343
341
|
return [nil, nil] unless match
|
|
344
342
|
|
|
345
343
|
fields =
|
|
@@ -353,13 +351,14 @@ module AgentCouncil
|
|
|
353
351
|
[fields, match.post_match]
|
|
354
352
|
end
|
|
355
353
|
|
|
356
|
-
# The
|
|
357
|
-
#
|
|
354
|
+
# The substantive prompt for each analyst harness (piped verbatim unless the
|
|
355
|
+
# harness overrides stdin_data): the user's prompt, less one trailing
|
|
356
|
+
# newline, then a blank line and the report-format addendum.
|
|
358
357
|
module_function def compose_analyst_prompt(prompt, addendum)
|
|
359
358
|
"#{prompt.chomp}\n\n#{addendum}"
|
|
360
359
|
end
|
|
361
360
|
|
|
362
|
-
# An agent's label in
|
|
361
|
+
# An agent's label in start and finish lines, report sources, and consolidation
|
|
363
362
|
# prompts: "#<index> <provider> / <model>", or without the index when
|
|
364
363
|
# there is none (the consolidator). missing_agents matches reports by it.
|
|
365
364
|
module_function def agent_label(provider, model, index = nil)
|
|
@@ -422,8 +421,7 @@ module AgentCouncil
|
|
|
422
421
|
# <model>", for the findings file's legend.
|
|
423
422
|
module_function def full_label(fields)
|
|
424
423
|
index = fields['agent_index']
|
|
425
|
-
|
|
426
|
-
index ? "##{index} #{parts}" : parts
|
|
424
|
+
index ? "##{index} #{agent_descriptor(fields)}" : agent_descriptor(fields)
|
|
427
425
|
end
|
|
428
426
|
|
|
429
427
|
# [[findings, meta], nil] if text can be split on its marker line, else
|
|
@@ -483,7 +481,7 @@ module AgentCouncil
|
|
|
483
481
|
unsplit_name = consolidated_filename(file_prefix, consolidator)
|
|
484
482
|
findings_name = consolidated_filename(file_prefix, consolidator, 'findings')
|
|
485
483
|
content = File.read(path, encoding: ENCODING)
|
|
486
|
-
front = content[
|
|
484
|
+
front = content[FRONT_MATTER_REGEX] || ''
|
|
487
485
|
body = content.delete_prefix(front)
|
|
488
486
|
# The header run_agent put before the model's text must still be there,
|
|
489
487
|
# or it would end up in the findings file.
|
|
@@ -509,10 +507,7 @@ module AgentCouncil
|
|
|
509
507
|
"provider, model):\n\n#{legend}\n\n#{findings}")
|
|
510
508
|
end
|
|
511
509
|
# Replaced atomically, so the whole report survives a failed write.
|
|
512
|
-
|
|
513
|
-
written << File.basename(temp)
|
|
514
|
-
File.write(temp, "#{front}#{head}\n#{meta.lstrip}")
|
|
515
|
-
File.rename(temp, path)
|
|
510
|
+
atomic_write(path, "#{front}#{head}\n#{meta.lstrip}")
|
|
516
511
|
[findings_name, filename]
|
|
517
512
|
rescue => e
|
|
518
513
|
# A failed split must not fail the consolidation, whose model call is
|
|
@@ -588,8 +583,8 @@ module AgentCouncil
|
|
|
588
583
|
<<~TABLE
|
|
589
584
|
### Sources
|
|
590
585
|
|
|
591
|
-
| Source | Duration | Input | Cached | Output | Cost |
|
|
592
|
-
| :----- | -------: | ----: | -----: | -----: |
|
|
586
|
+
| Source | Duration | Input | Cached | Output | Est. Cost |
|
|
587
|
+
| :----- | -------: | ----: | -----: | -----: | --------: |
|
|
593
588
|
#{rows.join("\n")}
|
|
594
589
|
TABLE
|
|
595
590
|
end
|
|
@@ -669,9 +664,9 @@ module AgentCouncil
|
|
|
669
664
|
format('%02d:%02d:%02d', total / 3600, total % 3600 / 60, total % 60)
|
|
670
665
|
end
|
|
671
666
|
|
|
672
|
-
# Token usage (see Harnesses) as e.g. "1.2M in (900.0k
|
|
673
|
-
# $0.85", where "in" counts all input, cached or
|
|
674
|
-
# when unknown. nil for nil usage.
|
|
667
|
+
# Token usage (see Harnesses::Harness.usage) as e.g. "1.2M in (900.0k
|
|
668
|
+
# cached) / 30.5k out, $0.85", where "in" counts all input, cached or
|
|
669
|
+
# not. The cost is left out when unknown. nil for nil usage.
|
|
675
670
|
module_function def format_usage(usage)
|
|
676
671
|
input, cached, output, cost = usage_figures(usage)
|
|
677
672
|
return nil unless input
|
|
@@ -690,7 +685,7 @@ module AgentCouncil
|
|
|
690
685
|
end
|
|
691
686
|
|
|
692
687
|
# [input (cached or not), cached input, output, cost (nil when unknown)]
|
|
693
|
-
# from a usage hash (see Harnesses), or nil for unknown usage.
|
|
688
|
+
# from a usage hash (see Harnesses::Harness.usage), or nil for unknown usage.
|
|
694
689
|
module_function def usage_figures(usage)
|
|
695
690
|
return nil unless usage.is_a?(Hash)
|
|
696
691
|
|
|
@@ -710,8 +705,8 @@ module AgentCouncil
|
|
|
710
705
|
dollars < 0.01 ? '<$0.01' : format('$%.2f', dollars)
|
|
711
706
|
end
|
|
712
707
|
|
|
713
|
-
# The sum of usage hashes (see Harnesses), ignoring nils
|
|
714
|
-
# not a hash); nil if there are none.
|
|
708
|
+
# The sum of usage hashes (see Harnesses::Harness.usage), ignoring nils
|
|
709
|
+
# (and anything else not a hash); nil if there are none.
|
|
715
710
|
# The cost is summed only if every usage has one, so a partial total
|
|
716
711
|
# isn't presented as the whole.
|
|
717
712
|
module_function def sum_usage(usages)
|
|
@@ -754,9 +749,9 @@ module AgentCouncil
|
|
|
754
749
|
# Writes the run dir's run.json: every analyst's and consolidation's
|
|
755
750
|
# entry (see run_entry), their total usage, how many entries it covers
|
|
756
751
|
# (those that reported usage), and the summed cost of the entries whose
|
|
757
|
-
# cost is known, with how many they are (see known_cost). Replaced
|
|
758
|
-
#
|
|
759
|
-
#
|
|
752
|
+
# cost is known, with how many they are (see known_cost). Replaced
|
|
753
|
+
# atomically (see atomic_write). A failure to write it is only warned
|
|
754
|
+
# about: the reports are what matter.
|
|
760
755
|
module_function def write_run_file(run_dir, analysts, consolidations)
|
|
761
756
|
usages = (analysts + consolidations).map { |entry| entry['usage'] }
|
|
762
757
|
cost, cost_runs = known_cost(usages)
|
|
@@ -764,14 +759,24 @@ module AgentCouncil
|
|
|
764
759
|
'total_usage' => sum_usage(usages), 'total_usage_runs' => usages.grep(Hash).size,
|
|
765
760
|
'known_cost' => cost, 'known_cost_runs' => cost_runs }
|
|
766
761
|
path = File.join(run_dir, RUN_FILENAME)
|
|
767
|
-
|
|
768
|
-
File.write(temp, "#{JSON.pretty_generate(data)}\n")
|
|
769
|
-
File.rename(temp, path)
|
|
762
|
+
atomic_write(path, "#{JSON.pretty_generate(data)}\n")
|
|
770
763
|
rescue SystemCallError => e
|
|
771
|
-
FileUtils.rm_f(temp) if temp
|
|
772
764
|
warn "Could not write #{relative_path(path)}: #{e.message}"
|
|
773
765
|
end
|
|
774
766
|
|
|
767
|
+
# Writes content to path by way of a temporary file beside it, renamed
|
|
768
|
+
# into place, so a reader or a Ctrl-C never sees it half written and a
|
|
769
|
+
# failed write leaves what was there before. The temporary file is
|
|
770
|
+
# removed if the write or rename fails, and the error raised.
|
|
771
|
+
module_function def atomic_write(path, content)
|
|
772
|
+
temp = "#{path}.#{Process.pid}.tmp"
|
|
773
|
+
File.write(temp, content)
|
|
774
|
+
File.rename(temp, path)
|
|
775
|
+
rescue
|
|
776
|
+
FileUtils.rm_f(temp)
|
|
777
|
+
raise
|
|
778
|
+
end
|
|
779
|
+
|
|
775
780
|
# [analysts, consolidations] from a run dir's run.json, or nil if it is
|
|
776
781
|
# missing or malformed.
|
|
777
782
|
module_function def read_run_file(run_dir)
|
|
@@ -789,7 +794,7 @@ module AgentCouncil
|
|
|
789
794
|
[result, Process.clock_gettime(Process::CLOCK_MONOTONIC) - started]
|
|
790
795
|
end
|
|
791
796
|
|
|
792
|
-
RESULT_HEADINGS =
|
|
797
|
+
RESULT_HEADINGS = ['Ok', 'Duration', 'Agent', 'In', 'Cached', 'Out', 'Est. Cost'].freeze
|
|
793
798
|
# The usage columns, right-aligned so their figures line up.
|
|
794
799
|
RIGHT_ALIGNED_COLUMNS = (3..6)
|
|
795
800
|
|
|
@@ -852,6 +857,31 @@ module AgentCouncil
|
|
|
852
857
|
text.length + text.scan(WIDE_CHAR).size - text.scan(ZERO_WIDTH_CHAR).size
|
|
853
858
|
end
|
|
854
859
|
|
|
860
|
+
# How an agent's process ended, for a failure's reason: "exited N", or
|
|
861
|
+
# "was stopped by SIGNAME" for one a signal stopped (e.g. the user's
|
|
862
|
+
# kill -TERM -PID), which has no exit status.
|
|
863
|
+
module_function def exit_description(status)
|
|
864
|
+
return "exited #{status.exitstatus}" unless status.signaled?
|
|
865
|
+
|
|
866
|
+
name =
|
|
867
|
+
begin
|
|
868
|
+
Signal.signame(status.termsig)
|
|
869
|
+
rescue ArgumentError # JRuby, for a signal with no name; MRI returns nil
|
|
870
|
+
nil
|
|
871
|
+
end
|
|
872
|
+
"was stopped by #{name ? "SIG#{name}" : "signal #{status.termsig}"}"
|
|
873
|
+
end
|
|
874
|
+
|
|
875
|
+
# Prints how to see what the agents are doing while they run.
|
|
876
|
+
module_function def puts_ps_hint
|
|
877
|
+
puts "\nRun this in another terminal to see (sub)process statuses:\nagent-council-ps #{Process.pid}"
|
|
878
|
+
end
|
|
879
|
+
|
|
880
|
+
# Prints the consolidator's pid line as it starts (see ProgressPrinter).
|
|
881
|
+
module_function def puts_spawned(label, pid)
|
|
882
|
+
ProgressPrinter.puts_live([ProgressPrinter.spawned_line(label, pid)])
|
|
883
|
+
end
|
|
884
|
+
|
|
855
885
|
# Prints the "--- Consolidation complete ---" (or error) marker.
|
|
856
886
|
module_function def puts_consolidation_status(ok)
|
|
857
887
|
puts "\n--- Consolidation #{ok ? 'complete' : 'error'} ---"
|
|
@@ -969,7 +999,8 @@ module AgentCouncil
|
|
|
969
999
|
end
|
|
970
1000
|
|
|
971
1001
|
# "harness / provider / model" for a validated config entry, printed at
|
|
972
|
-
# startup so it is clear which agents are still running later
|
|
1002
|
+
# startup so it is clear which agents are still running later (or for a
|
|
1003
|
+
# report's front matter, which has the same keys; see full_label).
|
|
973
1004
|
module_function def agent_descriptor(entry)
|
|
974
1005
|
"#{Harnesses.name_of(entry)} / #{entry['provider']} / #{entry['model']}"
|
|
975
1006
|
end
|
|
@@ -1001,6 +1032,7 @@ module AgentCouncil
|
|
|
1001
1032
|
agent-council --consolidate RUN_DIR
|
|
1002
1033
|
Repository: #{PROJECT_URL}
|
|
1003
1034
|
Documentation (Local): #{File.join(PROJECT_ROOT, 'README.md')}
|
|
1035
|
+
Cautions (Local): #{File.join(PROJECT_ROOT, 'CAUTIONS.md')}
|
|
1004
1036
|
Examples (Local): #{EXAMPLES_DIR}
|
|
1005
1037
|
Version: #{VERSION}
|
|
1006
1038
|
-------------------------------------------------------------------------------
|
|
@@ -1011,6 +1043,8 @@ module AgentCouncil
|
|
|
1011
1043
|
RUN_DIR With --consolidate, a finished run's directory (no PROMPT_FILE or CONFIG_FILE)
|
|
1012
1044
|
|
|
1013
1045
|
Agents analyze the current directory: run from the project to be analyzed.
|
|
1046
|
+
agent-council does not sandbox agents; they get shell access and send what
|
|
1047
|
+
they read to providers. Costs are rough estimates. Read CAUTIONS.md first.
|
|
1014
1048
|
|
|
1015
1049
|
Options:
|
|
1016
1050
|
BANNER
|
|
@@ -1066,6 +1100,7 @@ module AgentCouncil
|
|
|
1066
1100
|
end
|
|
1067
1101
|
|
|
1068
1102
|
module_function def main(argv)
|
|
1103
|
+
$stdout = SafeStdout.new($stdout) unless $stdout.is_a?(SafeStdout)
|
|
1069
1104
|
abort_if_native_windows
|
|
1070
1105
|
options = {}
|
|
1071
1106
|
parser = option_parser(options)
|
|
@@ -1154,12 +1189,7 @@ module AgentCouncil
|
|
|
1154
1189
|
end
|
|
1155
1190
|
analyst_prompt = compose_analyst_prompt(prompt, File.read(ANALYST_FORMAT_PATH, encoding: ENCODING))
|
|
1156
1191
|
|
|
1157
|
-
config =
|
|
1158
|
-
begin
|
|
1159
|
-
YAML.safe_load_file(config_path)
|
|
1160
|
-
rescue Psych::Exception => e
|
|
1161
|
-
abort "Invalid YAML in #{config_path}: #{e.message}"
|
|
1162
|
-
end
|
|
1192
|
+
config = load_config(config_path)
|
|
1163
1193
|
abort "Config file must be a YAML mapping: #{config_path}" unless config.is_a?(Hash)
|
|
1164
1194
|
|
|
1165
1195
|
agents = config['agents']
|
|
@@ -1216,16 +1246,14 @@ module AgentCouncil
|
|
|
1216
1246
|
# A default config's path is absolute (resolve_config_path makes it
|
|
1217
1247
|
# so), to show which of the defaults was picked up.
|
|
1218
1248
|
puts
|
|
1219
|
-
|
|
1220
|
-
puts
|
|
1221
|
-
puts "Prompt: #{relative_path(prompt_path)}"
|
|
1222
|
-
puts "Run Dir: #{relative_path(run_dir)}"
|
|
1223
|
-
puts "\nReview Agents (#{agents.size}):"
|
|
1249
|
+
puts_paths(argv[1] ? relative_path(config_path) : "[default] #{config_path}", prompt_path, run_dir)
|
|
1250
|
+
puts "\nConfigured Review Agents (#{agents.size}):"
|
|
1224
1251
|
agents.each_with_index do |agent, i|
|
|
1225
1252
|
puts " ##{i + 1} #{agent_descriptor(agent)}"
|
|
1226
1253
|
end
|
|
1227
|
-
puts "\
|
|
1228
|
-
|
|
1254
|
+
puts "\nConfigured Consolidator Agent:\n #{agent_descriptor(consolidator)}" if consolidator
|
|
1255
|
+
puts_ps_hint
|
|
1256
|
+
puts "\nStarted Agents:"
|
|
1229
1257
|
pool = WorkerPool.new(grace: INTERRUPT_GRACE_SECONDS)
|
|
1230
1258
|
pool.trap_signals
|
|
1231
1259
|
agents.each_with_index do |agent, i|
|
|
@@ -1243,11 +1271,18 @@ module AgentCouncil
|
|
|
1243
1271
|
end
|
|
1244
1272
|
end
|
|
1245
1273
|
# Printed here, in the main thread, as everything is (see WorkerPool).
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1274
|
+
progress = ProgressPrinter.new(agents.size)
|
|
1275
|
+
results =
|
|
1276
|
+
begin
|
|
1277
|
+
pool.collect(on_spawn: progress.method(:started)) do |label, ok, detail, elapsed, _usage, warnings|
|
|
1278
|
+
duration = format_duration(elapsed)
|
|
1279
|
+
line = ok ? " ✓ #{duration} #{label}" : " ✗ #{duration} #{label}\n #{detail}"
|
|
1280
|
+
progress.finished(label, [line, *warning_lines(warnings)])
|
|
1281
|
+
end
|
|
1282
|
+
rescue
|
|
1283
|
+
progress.flush_held # a worker raised; not after Ctrl-C, which prints nothing more
|
|
1284
|
+
raise
|
|
1285
|
+
end
|
|
1251
1286
|
|
|
1252
1287
|
failed = results.count { |_, ok, _| !ok }
|
|
1253
1288
|
puts "\nReviews (in #{relative_path(reviews_dir)}):"
|
|
@@ -1267,19 +1302,10 @@ module AgentCouncil
|
|
|
1267
1302
|
label, _, detail, _, _, warnings = result
|
|
1268
1303
|
[label, failure_with_warnings(detail, warnings), Harnesses.name_of(agent)]
|
|
1269
1304
|
end
|
|
1270
|
-
pool
|
|
1271
|
-
|
|
1272
|
-
run_consolidation(consolidator, prompt, run_dir, file_prefix,
|
|
1273
|
-
meta: report_meta, prompt_path: prompt_path, failures: failures, analyst_runs: analyst_runs,
|
|
1274
|
-
pool: pool)
|
|
1275
|
-
end
|
|
1276
|
-
end
|
|
1277
|
-
(label, ok, detail, usage, meta_name, warnings), elapsed = pool.collect.first
|
|
1278
|
-
puts format_result_table([[label, ok, detail, elapsed, usage]])
|
|
1279
|
-
puts warning_lines(warnings)
|
|
1280
|
-
entry = run_entry(consolidator, ok, detail, elapsed, usage, meta_report: meta_name, warnings: warnings)
|
|
1305
|
+
entry = run_and_show_consolidation(pool, consolidator, prompt, run_dir, file_prefix,
|
|
1306
|
+
meta: report_meta, prompt_path: prompt_path, failures: failures, analyst_runs: analyst_runs)
|
|
1281
1307
|
write_run_file(run_dir, analyst_runs, [entry])
|
|
1282
|
-
puts_consolidation_status(ok)
|
|
1308
|
+
puts_consolidation_status(entry['ok'])
|
|
1283
1309
|
end
|
|
1284
1310
|
|
|
1285
1311
|
exit(failed.zero? ? 0 : 1)
|
|
@@ -1316,12 +1342,7 @@ module AgentCouncil
|
|
|
1316
1342
|
abort "Prompt copy not found: #{prompt_path}" unless File.file?(prompt_path)
|
|
1317
1343
|
abort "Config copy not found: #{config_path}" unless File.file?(config_path)
|
|
1318
1344
|
|
|
1319
|
-
config =
|
|
1320
|
-
begin
|
|
1321
|
-
YAML.safe_load_file(config_path)
|
|
1322
|
-
rescue Psych::Exception => e
|
|
1323
|
-
abort "Invalid YAML in #{config_path}: #{e.message}"
|
|
1324
|
-
end
|
|
1345
|
+
config = load_config(config_path)
|
|
1325
1346
|
consolidator = config['consolidator'] if config.is_a?(Hash)
|
|
1326
1347
|
abort "No consolidator in #{config_path}; add one to consolidate" unless consolidator
|
|
1327
1348
|
abort_on_entry_errors({ "'consolidator'" => consolidator }, config_path,
|
|
@@ -1356,43 +1377,62 @@ module AgentCouncil
|
|
|
1356
1377
|
'run_stamp')
|
|
1357
1378
|
meta['timestamp'] = iso_timestamp(meta['timestamp']) if meta['timestamp'].is_a?(Time)
|
|
1358
1379
|
|
|
1359
|
-
|
|
1360
|
-
puts "
|
|
1361
|
-
|
|
1362
|
-
puts "Run Dir: #{relative_path(run_dir)}"
|
|
1363
|
-
puts "\nConsolidator Agent:\n #{agent_descriptor(consolidator)}"
|
|
1380
|
+
puts_paths(relative_path(config_path), prompt_path, run_dir)
|
|
1381
|
+
puts "\nConfigured Consolidator Agent:\n #{agent_descriptor(consolidator)}"
|
|
1382
|
+
puts_ps_hint
|
|
1364
1383
|
puts "\nConsolidation Report (in #{relative_path(run_dir)}, " \
|
|
1365
1384
|
"from #{reports.size} review#{'s' unless reports.size == 1}):"
|
|
1366
1385
|
analysts, consolidations = read_run_file(run_dir)
|
|
1367
1386
|
failures = missing_agents(config, reports, analysts)
|
|
1368
1387
|
pool = WorkerPool.new(grace: INTERRUPT_GRACE_SECONDS)
|
|
1369
1388
|
pool.trap_signals
|
|
1370
|
-
pool
|
|
1371
|
-
|
|
1372
|
-
run_consolidation(consolidator, prompt, run_dir, file_prefix,
|
|
1373
|
-
meta: meta, prompt_path: relative_path(prompt_path), failures: failures,
|
|
1374
|
-
analyst_runs: analysts || [], pool: pool)
|
|
1375
|
-
end
|
|
1376
|
-
end
|
|
1377
|
-
(label, ok, detail, usage, meta_name, warnings), elapsed = pool.collect.first
|
|
1378
|
-
puts format_result_table([[label, ok, detail, elapsed, usage]])
|
|
1379
|
-
puts warning_lines(warnings)
|
|
1389
|
+
entry = run_and_show_consolidation(pool, consolidator, prompt, run_dir, file_prefix,
|
|
1390
|
+
meta: meta, prompt_path: relative_path(prompt_path), failures: failures, analyst_runs: analysts || [])
|
|
1380
1391
|
# Added to the run's run.json. Without one (deleted, or unreadable),
|
|
1381
1392
|
# none is written: a total covering only this consolidation would
|
|
1382
1393
|
# pass for the run's.
|
|
1383
1394
|
if analysts
|
|
1384
|
-
entry = run_entry(consolidator, ok, detail, elapsed, usage, meta_report: meta_name, warnings: warnings)
|
|
1385
1395
|
write_run_file(run_dir, analysts, consolidations + [entry])
|
|
1386
1396
|
else
|
|
1387
1397
|
warn "No readable #{RUN_FILENAME} in #{relative_path(run_dir)}, " \
|
|
1388
1398
|
"so this consolidation won't be recorded in one"
|
|
1389
1399
|
end
|
|
1390
|
-
puts_consolidation_status(ok)
|
|
1391
|
-
exit(ok ? 0 : 1)
|
|
1400
|
+
puts_consolidation_status(entry['ok'])
|
|
1401
|
+
exit(entry['ok'] ? 0 : 1)
|
|
1392
1402
|
rescue Interrupt
|
|
1393
1403
|
exit_interrupted(pool)
|
|
1394
1404
|
end
|
|
1395
1405
|
|
|
1406
|
+
# The parsed YAML of config_path, aborting if it isn't valid YAML.
|
|
1407
|
+
module_function def load_config(config_path)
|
|
1408
|
+
YAML.safe_load_file(config_path)
|
|
1409
|
+
rescue Psych::Exception => e
|
|
1410
|
+
abort "Invalid YAML in #{config_path}: #{e.message}"
|
|
1411
|
+
end
|
|
1412
|
+
|
|
1413
|
+
# Prints the paths a run, or --consolidate, works with; config is the
|
|
1414
|
+
# Config line's text.
|
|
1415
|
+
module_function def puts_paths(config, prompt_path, run_dir)
|
|
1416
|
+
puts "Working Dir: #{Dir.pwd}"
|
|
1417
|
+
puts "Config: #{config}"
|
|
1418
|
+
puts "Prompt: #{relative_path(prompt_path)}"
|
|
1419
|
+
puts "Run Dir: #{relative_path(run_dir)}"
|
|
1420
|
+
end
|
|
1421
|
+
|
|
1422
|
+
# Runs consolidator in pool, as the only agent it has running (options
|
|
1423
|
+
# are run_consolidation's), prints its result row and any warnings, and
|
|
1424
|
+
# returns its run.json entry (see run_entry).
|
|
1425
|
+
module_function def run_and_show_consolidation(pool, consolidator, prompt, run_dir, file_prefix, **options)
|
|
1426
|
+
pool.start do
|
|
1427
|
+
timed { run_consolidation(consolidator, prompt, run_dir, file_prefix, pool: pool, **options) }
|
|
1428
|
+
end
|
|
1429
|
+
(label, ok, detail, usage, meta_name, warnings), elapsed =
|
|
1430
|
+
pool.collect(on_spawn: method(:puts_spawned)).first
|
|
1431
|
+
puts format_result_table([[label, ok, detail, elapsed, usage]])
|
|
1432
|
+
puts warning_lines(warnings)
|
|
1433
|
+
run_entry(consolidator, ok, detail, elapsed, usage, meta_report: meta_name, warnings: warnings)
|
|
1434
|
+
end
|
|
1435
|
+
|
|
1396
1436
|
# True if name is a plain file name: no directory part or null byte, and
|
|
1397
1437
|
# not empty, "." or "..".
|
|
1398
1438
|
# Applied to every front matter value that becomes part of a path, so a
|
|
@@ -1452,7 +1492,7 @@ module AgentCouncil
|
|
|
1452
1492
|
# executable of every harness the config entries use (validated ones, so
|
|
1453
1493
|
# each harness is known).
|
|
1454
1494
|
module_function def abort_unless_harnesses_installed(entries)
|
|
1455
|
-
names = entries.map { |entry| Harnesses.
|
|
1495
|
+
names = entries.map { |entry| Harnesses.class_for(entry)::NAME }.uniq
|
|
1456
1496
|
missing = names.reject { |name| executable_on_path?(name) }
|
|
1457
1497
|
return if missing.empty?
|
|
1458
1498
|
|